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