rcp: Move plugins to their own sub-directory
[deliverable/tracecompass.git] / org.eclipse.tracecompass.ctf.core.tests / src / org / eclipse / tracecompass / 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.tracecompass.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.tracecompass.ctf.core.CTFException;
23 import org.eclipse.tracecompass.ctf.core.event.io.BitBuffer;
24 import org.eclipse.tracecompass.ctf.core.event.scope.IDefinitionScope;
25 import org.eclipse.tracecompass.ctf.core.event.types.AbstractArrayDefinition;
26 import org.eclipse.tracecompass.ctf.core.event.types.CompoundDeclaration;
27 import org.eclipse.tracecompass.ctf.core.event.types.Definition;
28 import org.eclipse.tracecompass.ctf.core.event.types.Encoding;
29 import org.eclipse.tracecompass.ctf.core.event.types.EnumDeclaration;
30 import org.eclipse.tracecompass.ctf.core.event.types.EnumDefinition;
31 import org.eclipse.tracecompass.ctf.core.event.types.FloatDeclaration;
32 import org.eclipse.tracecompass.ctf.core.event.types.IDefinition;
33 import org.eclipse.tracecompass.ctf.core.event.types.IntegerDeclaration;
34 import org.eclipse.tracecompass.ctf.core.event.types.IntegerDefinition;
35 import org.eclipse.tracecompass.ctf.core.event.types.StringDeclaration;
36 import org.eclipse.tracecompass.ctf.core.event.types.StringDefinition;
37 import org.eclipse.tracecompass.ctf.core.event.types.StructDeclaration;
38 import org.eclipse.tracecompass.ctf.core.event.types.StructDefinition;
39 import org.eclipse.tracecompass.ctf.core.event.types.VariantDeclaration;
40 import org.eclipse.tracecompass.ctf.core.event.types.VariantDefinition;
41 import org.eclipse.tracecompass.ctf.core.tests.io.Util;
42 import org.eclipse.tracecompass.internal.ctf.core.event.types.ArrayDeclaration;
43 import org.junit.Before;
44 import org.junit.Test;
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 private static final @NonNull String TEST_STRUCT_ID = "testStruct";
59 private static final @NonNull String ENUM_7 = "g";
60 private static final @NonNull String ENUM_6 = "f";
61 private static final @NonNull String ENUM_5 = "e";
62 private static final @NonNull String ENUM_4 = "d";
63 private static final @NonNull String ENUM_3 = "c";
64 private static final @NonNull String ENUM_2 = "b";
65 private static final @NonNull String ENUM_1 = "a";
66 private static final @NonNull String TAG_ID = "a";
67 private static final @NonNull String LENGTH_SEQ = "_len";
68 private static final @NonNull String VAR_FIELD_NAME = "var";
69 private static final @NonNull String ENUM_8 = "bbq ribs";
70
71 /**
72 * Perform pre-test initialization.
73 *
74 * Not sure it needs to be that complicated, oh well...
75 *
76 * @throws CTFException
77 * won't happen
78 */
79 @Before
80 public void setUp() throws CTFException {
81 StructDeclaration sDec = new StructDeclaration(12);
82 StructDeclaration smallStruct = new StructDeclaration(8);
83 IntegerDeclaration iDec = IntegerDeclaration.createDeclaration(32, false, 32, ByteOrder.BIG_ENDIAN, Encoding.NONE, "", 8);
84 IntegerDeclaration lenDec = IntegerDeclaration.createDeclaration(8, false, 8, ByteOrder.BIG_ENDIAN, Encoding.NONE, "", 8);
85 StringDeclaration strDec = StringDeclaration.getStringDeclaration(Encoding.UTF8);
86 EnumDeclaration enDec = new EnumDeclaration(iDec);
87 VariantDeclaration varDec = new VariantDeclaration();
88 EnumDeclaration tagDec = new EnumDeclaration(iDec);
89 CompoundDeclaration arrDec = new ArrayDeclaration(2, iDec);
90 FloatDeclaration fDec = new FloatDeclaration(8, 24, ByteOrder.BIG_ENDIAN, 8);
91 tagDec.add(0, 1, ENUM_1);
92 tagDec.add(2, 3, ENUM_2);
93 tagDec.add(4, 5, ENUM_3);
94 tagDec.add(8, 9, ENUM_5);
95 tagDec.add(10, 11, ENUM_6);
96 tagDec.add(12, 13, ENUM_7);
97 varDec.addField(ENUM_4, lenDec);
98 varDec.addField(ENUM_7, fDec);
99 varDec.addField(ENUM_6, smallStruct);
100 varDec.addField(ENUM_5, enDec);
101 varDec.addField(ENUM_3, arrDec);
102 varDec.addField(ENUM_2, iDec);
103 varDec.addField(ENUM_1, strDec);
104
105 sDec.addField(TAG_ID, tagDec);
106 sDec.addField(LENGTH_SEQ, lenDec);
107
108 sDec.addField(VAR_FIELD_NAME, varDec);
109 varDec.setTag(TAG_ID);
110
111 final ByteBuffer byteBuffer = Util.testMemory(ByteBuffer.allocate(100));
112 BitBuffer bb = new BitBuffer(byteBuffer);
113 byteBuffer.mark();
114 byteBuffer.putInt(1);
115 byteBuffer.putInt(2);
116 byteBuffer.putInt(3);
117 byteBuffer.reset();
118 fStructDefinition = sDec.createDefinition(null, TEST_STRUCT_ID, bb);
119 fixture = (VariantDefinition) fStructDefinition.getDefinition(VAR_FIELD_NAME);
120 }
121
122 /**
123 * Run the VariantDefinition(VariantDeclaration,DefinitionScope,String)
124 *
125 * @throws CTFException
126 * should not happen
127 */
128 @Test
129 public void testVariantDefinition() throws CTFException {
130 VariantDeclaration declaration = new VariantDeclaration();
131 declaration.setTag("");
132 VariantDeclaration variantDeclaration = new VariantDeclaration();
133 variantDeclaration.addField("", new EnumDeclaration(IntegerDeclaration.INT_32B_DECL));
134 variantDeclaration.addField("a", IntegerDeclaration.INT_64B_DECL);
135 declaration.addField(ENUM_3, StringDeclaration.getStringDeclaration(Encoding.UTF8));
136 variantDeclaration.setTag("a");
137
138 byte[] bytes = new byte[128];
139 ByteBuffer byb = ByteBuffer.wrap(bytes);
140 byb.mark();
141 byb.putInt(0);
142 byb.putShort((short) 2);
143 byb.put(new String("hello").getBytes());
144 byb.reset();
145 BitBuffer bb = new BitBuffer(byb);
146 VariantDefinition variantDefinition = variantDeclaration.createDefinition(fStructDefinition, "field", bb);
147 EnumDeclaration declaration2 = new EnumDeclaration(IntegerDeclaration.INT_8_DECL);
148 declaration2.add(0, 2, ENUM_3);
149 EnumDefinition enumDefinition = new EnumDefinition(
150 declaration2,
151 null,
152 "a",
153 new IntegerDefinition(
154 IntegerDeclaration.INT_8_DECL,
155 null,
156 "A",
157 1
158 ));
159 final StructDeclaration declarationScope = new StructDeclaration(1L);
160 declarationScope.addField("", enumDefinition.getDeclaration());
161 declarationScope.addField("variant", variantDeclaration);
162 IDefinitionScope definitionScope = new StructDefinition(
163 declarationScope,
164 variantDefinition,
165 "",
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().getPath();
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.040896 seconds and 5 git commands to generate.