ctf: Make events immutable
[deliverable/tracecompass.git] / org.eclipse.linuxtools.ctf.core.tests / src / org / eclipse / linuxtools / ctf / core / tests / types / VariantDefinitionTest.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.linuxtools.ctf.core.tests.types;
13
14 import static org.junit.Assert.assertEquals;
15 import static org.junit.Assert.assertNotNull;
16 import static org.junit.Assert.assertNull;
17
18 import java.nio.ByteBuffer;
19 import java.nio.ByteOrder;
20
21 import org.eclipse.jdt.annotation.NonNull;
22 import org.eclipse.linuxtools.ctf.core.event.io.BitBuffer;
23 import org.eclipse.linuxtools.ctf.core.event.scope.IDefinitionScope;
24 import org.eclipse.linuxtools.ctf.core.event.types.ArrayDeclaration;
25 import org.eclipse.linuxtools.ctf.core.event.types.ArrayDefinition;
26 import org.eclipse.linuxtools.ctf.core.event.types.Definition;
27 import org.eclipse.linuxtools.ctf.core.event.types.Encoding;
28 import org.eclipse.linuxtools.ctf.core.event.types.EnumDeclaration;
29 import org.eclipse.linuxtools.ctf.core.event.types.EnumDefinition;
30 import org.eclipse.linuxtools.ctf.core.event.types.FloatDeclaration;
31 import org.eclipse.linuxtools.ctf.core.event.types.IntegerDeclaration;
32 import org.eclipse.linuxtools.ctf.core.event.types.IntegerDefinition;
33 import org.eclipse.linuxtools.ctf.core.event.types.SequenceDefinition;
34 import org.eclipse.linuxtools.ctf.core.event.types.StringDeclaration;
35 import org.eclipse.linuxtools.ctf.core.event.types.StringDefinition;
36 import org.eclipse.linuxtools.ctf.core.event.types.StructDeclaration;
37 import org.eclipse.linuxtools.ctf.core.event.types.StructDefinition;
38 import org.eclipse.linuxtools.ctf.core.event.types.VariantDeclaration;
39 import org.eclipse.linuxtools.ctf.core.event.types.VariantDefinition;
40 import org.eclipse.linuxtools.ctf.core.trace.CTFReaderException;
41 import org.junit.Before;
42 import org.junit.Test;
43
44 import com.google.common.collect.ImmutableList;
45
46 /**
47 * The class <code>VariantDefinitionTest</code> contains tests for the class
48 * <code>{@link VariantDefinition}</code>.
49 *
50 * @author ematkho
51 * @version $Revision: 1.0 $
52 */
53 public class VariantDefinitionTest {
54
55 private VariantDefinition fixture;
56
57 StructDefinition fStructDefinition;
58 @NonNull private static final String TEST_STRUCT_ID = "testStruct";
59
60 @NonNull private static final String ENUM_7 = "g";
61 @NonNull private static final String ENUM_6 = "f";
62 @NonNull private static final String ENUM_5 = "e";
63 @NonNull private static final String ENUM_4 = "d";
64 @NonNull private static final String ENUM_3 = "c";
65 @NonNull private static final String ENUM_2 = "b";
66 @NonNull private static final String ENUM_1 = "a";
67
68 @NonNull private static final String TAG_ID = "a";
69
70 @NonNull private static final String LENGTH_SEQ = "_len";
71 @NonNull private static final String VAR_FIELD_NAME = "var";
72 private static final String ENUM_8 = null;
73
74 /**
75 * Perform pre-test initialization.
76 *
77 * Not sure it needs to be that complicated, oh well...
78 *
79 * @throws CTFReaderException
80 * won't happen
81 */
82 @Before
83 public void setUp() throws CTFReaderException {
84 StructDeclaration sDec = new StructDeclaration(12);
85 StructDeclaration smallStruct = new StructDeclaration(8);
86 IntegerDeclaration iDec = IntegerDeclaration.createDeclaration(32, false, 32, ByteOrder.BIG_ENDIAN, Encoding.NONE, "", 8);
87 IntegerDeclaration lenDec = IntegerDeclaration.createDeclaration(8, false, 8, ByteOrder.BIG_ENDIAN, Encoding.NONE, "", 8);
88 StringDeclaration strDec = new StringDeclaration();
89 EnumDeclaration enDec = new EnumDeclaration(iDec);
90 VariantDeclaration varDec = new VariantDeclaration();
91 EnumDeclaration tagDec = new EnumDeclaration(iDec);
92 ArrayDeclaration arrDec = new ArrayDeclaration(2, iDec);
93 FloatDeclaration fDec = new FloatDeclaration(8, 24, ByteOrder.BIG_ENDIAN, 8);
94 tagDec.add(0, 1, ENUM_1);
95 tagDec.add(2, 3, ENUM_2);
96 tagDec.add(4, 5, ENUM_3);
97 tagDec.add(8, 9, ENUM_5);
98 tagDec.add(10, 11, ENUM_6);
99 tagDec.add(12, 13, ENUM_7);
100 varDec.addField(ENUM_4, lenDec);
101 varDec.addField(ENUM_7, fDec);
102 varDec.addField(ENUM_6, smallStruct);
103 varDec.addField(ENUM_5, enDec);
104 varDec.addField(ENUM_3, arrDec);
105 varDec.addField(ENUM_2, iDec);
106 varDec.addField(ENUM_1, strDec);
107
108 sDec.addField(TAG_ID, tagDec);
109 sDec.addField(LENGTH_SEQ, lenDec);
110
111 sDec.addField(VAR_FIELD_NAME, varDec);
112 varDec.setTag(TAG_ID);
113
114 ByteBuffer byteBuffer = ByteBuffer.allocate(100);
115 BitBuffer bb = new BitBuffer(byteBuffer);
116 byteBuffer.mark();
117 byteBuffer.putInt(1);
118 byteBuffer.putInt(2);
119 byteBuffer.putInt(3);
120 byteBuffer.reset();
121 fStructDefinition = sDec.createDefinition(null, TEST_STRUCT_ID, bb);
122 fixture = (VariantDefinition) fStructDefinition.getDefinition(VAR_FIELD_NAME);
123 }
124
125 /**
126 * Run the VariantDefinition(VariantDeclaration,DefinitionScope,String)
127 *
128 * @throws CTFReaderException
129 * should not happen
130 */
131 @Test
132 public void testVariantDefinition() throws CTFReaderException {
133 VariantDeclaration declaration = new VariantDeclaration();
134 declaration.setTag("");
135 VariantDeclaration variantDeclaration = new VariantDeclaration();
136 variantDeclaration.addField("", new EnumDeclaration(IntegerDeclaration.INT_32B_DECL));
137 variantDeclaration.addField("a", IntegerDeclaration.INT_64B_DECL);
138 declaration.addField(ENUM_3, new StringDeclaration());
139 variantDeclaration.setTag("a");
140
141 byte[] bytes = new byte[128];
142 ByteBuffer byb = ByteBuffer.wrap(bytes);
143 byb.mark();
144 byb.putInt(0);
145 byb.putShort((short) 2);
146 byb.put(new String("hello").getBytes());
147 byb.reset();
148 BitBuffer bb = new BitBuffer(byb);
149 VariantDefinition variantDefinition = variantDeclaration.createDefinition(fStructDefinition, "field", bb);
150 EnumDeclaration declaration2 = new EnumDeclaration(IntegerDeclaration.INT_8_DECL);
151 declaration2.add(0, 2, ENUM_3);
152 EnumDefinition enumDefinition = new EnumDefinition(
153 declaration2,
154 null,
155 "a",
156 new IntegerDefinition(
157 IntegerDeclaration.INT_8_DECL,
158 null,
159 "A",
160 1
161 ));
162 IDefinitionScope definitionScope = new StructDefinition(
163 new StructDeclaration(1L),
164 variantDefinition,
165 "",
166 ImmutableList.<String> of("", "variant"),
167 new Definition[] { enumDefinition, variantDefinition }
168 );
169 String fieldName = "";
170 declaration.setTag("");
171 VariantDefinition result = declaration.createDefinition(definitionScope, fieldName, bb);
172 assertNotNull(result);
173 }
174
175 /**
176 * Run the Definition getCurrentField() method test.
177 */
178 @Test
179 public void testGetCurrentField() {
180 Definition result = fixture.getCurrentField();
181 assertNotNull(result);
182 }
183
184 /**
185 * Run the String getCurrentFieldName() method test.
186 */
187 @Test
188 public void testGetCurrentFieldName() {
189 String result = fixture.getCurrentFieldName();
190 assertNotNull(result);
191 }
192
193 /**
194 * Run the VariantDeclaration getDeclaration() method test.
195 */
196 @Test
197 public void testGetDeclaration() {
198 VariantDeclaration result = fixture.getDeclaration();
199 assertNotNull(result);
200 }
201
202 /**
203 * Run the HashMap<String, Definition> getDefinitions() method test.
204 */
205 @Test
206 public void testGetDefinitions() {
207 Definition result = fixture.getCurrentField();
208 assertNotNull(result);
209 }
210
211 /**
212 * Run the String getPath() method test.
213 */
214 @Test
215 public void testGetPath() {
216 String result = fixture.getScopePath().toString();
217 assertNotNull(result);
218 }
219
220 /**
221 * Run the ArrayDefinition lookupArray(String) method test.
222 */
223 @Test
224 public void testLookupArray() {
225 ArrayDefinition result = fixture.lookupArray(ENUM_3);
226 assertNull(result);
227 }
228
229 /**
230 * Run the Definition lookupDefinition(String) method test.
231 */
232 @Test
233 public void testLookupDefinition() {
234 Definition result = fixture.lookupDefinition(ENUM_1);
235 assertNotNull(result);
236 assertEquals("a", ((EnumDefinition) result).getStringValue());
237 }
238
239 /**
240 * Run the EnumDefinition lookupEnum(String) method test.
241 */
242 @Test
243 public void testLookupEnum() {
244 EnumDefinition result = fixture.lookupEnum(ENUM_5);
245 assertNull(result);
246 }
247
248 /**
249 * Run the IntegerDefinition lookupInteger(String) method test.
250 */
251 @Test
252 public void testLookupInteger() {
253 IntegerDefinition result = fixture.lookupInteger(ENUM_2);
254 assertNull(result);
255 }
256
257 /**
258 * Run the SequenceDefinition lookupSequence(String) method test.
259 */
260 @Test
261 public void testLookupSequence_1() {
262 SequenceDefinition result = fixture.lookupSequence(ENUM_4);
263 assertNull(result);
264 }
265
266 /**
267 * Run the StringDefinition lookupString(String) method test.
268 */
269 @Test
270 public void testLookupString() {
271 StringDefinition result = fixture.lookupString(ENUM_1);
272 assertNull(result);
273 }
274
275 /**
276 * Run the StructDefinition lookupStruct(String) method test.
277 */
278 @Test
279 public void testLookupStruct() {
280 StructDefinition result = fixture.lookupStruct(ENUM_6);
281 assertNull(result);
282 }
283
284 /**
285 * Run the VariantDefinition lookupVariant(String) method test.
286 */
287 @Test
288 public void testLookupVariant() {
289 VariantDefinition result = fixture.lookupVariant(ENUM_8);
290 assertNull(result);
291 }
292
293 /**
294 * Run the String toString() method test.
295 */
296 @Test
297 public void testToString() {
298 String result = fixture.toString();
299 assertEquals("{ a = \"\" }", result);
300 }
301 }
This page took 0.039118 seconds and 5 git commands to generate.