ctf: Move plugins to their own sub-directory
[deliverable/tracecompass.git] / ctf / org.eclipse.tracecompass.ctf.core.tests / src / org / eclipse / tracecompass / ctf / core / tests / event / CTFEventFieldTest.java
1 /*******************************************************************************
2 * Copyright (c) 2013, 2014 Ericsson
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
7 *
8 * Contributors:
9 * Matthew Khouzam - Initial API and implementation
10 *******************************************************************************/
11
12 package org.eclipse.tracecompass.ctf.core.tests.event;
13
14 import static org.junit.Assert.assertEquals;
15 import static org.junit.Assert.assertNotNull;
16
17 import java.nio.ByteBuffer;
18 import java.nio.ByteOrder;
19
20 import org.eclipse.jdt.annotation.NonNull;
21 import org.eclipse.tracecompass.ctf.core.CTFException;
22 import org.eclipse.tracecompass.ctf.core.event.io.BitBuffer;
23 import org.eclipse.tracecompass.ctf.core.event.types.Definition;
24 import org.eclipse.tracecompass.ctf.core.event.types.Encoding;
25 import org.eclipse.tracecompass.ctf.core.event.types.IDefinition;
26 import org.eclipse.tracecompass.ctf.core.event.types.IntegerDeclaration;
27 import org.eclipse.tracecompass.ctf.core.event.types.IntegerDefinition;
28 import org.eclipse.tracecompass.ctf.core.event.types.StringDeclaration;
29 import org.eclipse.tracecompass.ctf.core.event.types.StringDefinition;
30 import org.eclipse.tracecompass.ctf.core.event.types.StructDeclaration;
31 import org.eclipse.tracecompass.ctf.core.event.types.StructDefinition;
32 import org.eclipse.tracecompass.internal.ctf.core.event.types.SequenceDeclaration;
33 import org.junit.Test;
34
35 /**
36 * The class <code>CTFEventFieldTest</code> contains tests for the class
37 * <code>{@link CTFEventField}</code>.
38 *
39 * @author ematkho
40 * @version $Revision: 1.0 $
41 */
42 @SuppressWarnings("javadoc")
43 public class CTFEventFieldTest {
44
45 @NonNull
46 private static final String fieldName = "id";
47
48 /**
49 * Run the CTFEventField parseField(Definition,String) method test.
50 *
51 * @throws CTFException
52 */
53 @Test
54 public void testParseField_complex() throws CTFException {
55 int len = 32;
56 IntegerDeclaration id = IntegerDeclaration.createDeclaration(
57 len,
58 false,
59 len,
60 ByteOrder.LITTLE_ENDIAN,
61 Encoding.ASCII,
62 "",
63 len);
64 String lengthName = "LengthName";
65 StructDeclaration structDec = new StructDeclaration(0);
66 structDec.addField(lengthName, id);
67 StructDefinition structDef = new StructDefinition(
68 structDec,
69 null,
70 lengthName,
71 new Definition[] {
72 new IntegerDefinition(
73 id,
74 null,
75 lengthName,
76 32)
77 });
78
79 SequenceDeclaration sd = new SequenceDeclaration(lengthName, id);
80 ByteBuffer byb = testMemory(ByteBuffer.allocate(1024));
81 for (int i = 0; i < 1024; i++) {
82 byb.put((byte) i);
83 }
84 BitBuffer bb = new BitBuffer(byb);
85 IDefinition fieldDef = sd.createDefinition(structDef, "fff-fffield", bb);
86
87 assertNotNull(fieldDef);
88 }
89
90 @NonNull
91 private static ByteBuffer testMemory(ByteBuffer buffer) {
92 if (buffer == null) {
93 throw new IllegalStateException("Failed to allocate memory");
94 }
95 return buffer;
96 }
97
98 /**
99 * Run the CTFEventField parseField(Definition,String) method test.
100 *
101 * @throws CTFException
102 */
103 @Test
104 public void testParseField_simple() throws CTFException {
105 final StringDeclaration elemType = StringDeclaration.getStringDeclaration(Encoding.UTF8);
106 byte[] bytes = { 'T', 'e', 's', 't', '\0' };
107 ByteBuffer bb = testMemory(ByteBuffer.wrap(bytes));
108 IDefinition fieldDef = elemType.createDefinition(null, fieldName, new BitBuffer(bb));
109
110 assertNotNull(fieldDef);
111 }
112
113 /**
114 * Run the CTFEventField parseField(Definition,String) method test.
115 */
116 @Test
117 public void testParseField_simple2() {
118 IntegerDefinition fieldDef = new IntegerDefinition(
119 IntegerDeclaration.createDeclaration(1, false, 1, ByteOrder.BIG_ENDIAN,
120 Encoding.ASCII, "", 8), null, fieldName, 1L);
121
122 assertNotNull(fieldDef);
123 }
124
125 /**
126 *
127 */
128 @Test
129 public void testParseField_simple3() {
130 StringDefinition fieldDef = new StringDefinition(
131 StringDeclaration.getStringDeclaration(Encoding.UTF8), null, fieldName, "Hello World");
132
133 String other = "\"Hello World\"";
134 assertNotNull(fieldDef);
135 assertEquals(fieldDef.toString(), other);
136 }
137
138 }
This page took 0.033223 seconds and 5 git commands to generate.