Commit | Line | Data |
---|---|---|
4bd7f2db | 1 | /******************************************************************************* |
60ae41e1 | 2 | * Copyright (c) 2013, 2014 Ericsson |
4bd7f2db AM |
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 | ||
866e5b51 FC |
12 | package org.eclipse.linuxtools.ctf.core.tests.types; |
13 | ||
419f09a8 | 14 | import static org.junit.Assert.assertEquals; |
866e5b51 FC |
15 | import static org.junit.Assert.assertNotNull; |
16 | import static org.junit.Assert.assertNull; | |
17 | ||
a4fa4e36 | 18 | import java.nio.ByteBuffer; |
866e5b51 | 19 | import java.nio.ByteOrder; |
866e5b51 | 20 | |
a4fa4e36 MK |
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; | |
024373d7 | 24 | import org.eclipse.linuxtools.ctf.core.event.types.ArrayDeclaration; |
866e5b51 FC |
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; | |
024373d7 | 30 | import org.eclipse.linuxtools.ctf.core.event.types.FloatDeclaration; |
866e5b51 FC |
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; | |
024373d7 | 34 | import org.eclipse.linuxtools.ctf.core.event.types.StringDeclaration; |
866e5b51 FC |
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; | |
a4fa4e36 | 40 | import org.eclipse.linuxtools.ctf.core.trace.CTFReaderException; |
866e5b51 FC |
41 | import org.junit.Before; |
42 | import org.junit.Test; | |
43 | ||
a4fa4e36 MK |
44 | import com.google.common.collect.ImmutableList; |
45 | ||
866e5b51 FC |
46 | /** |
47 | * The class <code>VariantDefinitionTest</code> contains tests for the class | |
48 | * <code>{@link VariantDefinition}</code>. | |
284fdee8 | 49 | * |
866e5b51 FC |
50 | * @author ematkho |
51 | * @version $Revision: 1.0 $ | |
52 | */ | |
53 | public class VariantDefinitionTest { | |
54 | ||
55 | private VariantDefinition fixture; | |
56 | ||
a4fa4e36 MK |
57 | StructDefinition fStructDefinition; |
58 | @NonNull private static final String TEST_STRUCT_ID = "testStruct"; | |
4a9c1f07 | 59 | |
a4fa4e36 MK |
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"; | |
4a9c1f07 | 67 | |
a4fa4e36 | 68 | @NonNull private static final String TAG_ID = "a"; |
4a9c1f07 | 69 | |
a4fa4e36 MK |
70 | @NonNull private static final String LENGTH_SEQ = "_len"; |
71 | @NonNull private static final String VAR_FIELD_NAME = "var"; | |
024373d7 | 72 | private static final String ENUM_8 = null; |
a34d8eee | 73 | |
866e5b51 FC |
74 | /** |
75 | * Perform pre-test initialization. | |
284fdee8 | 76 | * |
866e5b51 | 77 | * Not sure it needs to be that complicated, oh well... |
a4fa4e36 MK |
78 | * |
79 | * @throws CTFReaderException | |
80 | * won't happen | |
866e5b51 FC |
81 | */ |
82 | @Before | |
a4fa4e36 | 83 | public void setUp() throws CTFReaderException { |
024373d7 MK |
84 | StructDeclaration sDec = new StructDeclaration(12); |
85 | StructDeclaration smallStruct = new StructDeclaration(8); | |
a4fa4e36 MK |
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); | |
024373d7 MK |
88 | StringDeclaration strDec = new StringDeclaration(); |
89 | EnumDeclaration enDec = new EnumDeclaration(iDec); | |
024373d7 MK |
90 | VariantDeclaration varDec = new VariantDeclaration(); |
91 | EnumDeclaration tagDec = new EnumDeclaration(iDec); | |
92 | ArrayDeclaration arrDec = new ArrayDeclaration(2, iDec); | |
07002e0a | 93 | FloatDeclaration fDec = new FloatDeclaration(8, 24, ByteOrder.BIG_ENDIAN, 8); |
024373d7 MK |
94 | tagDec.add(0, 1, ENUM_1); |
95 | tagDec.add(2, 3, ENUM_2); | |
96 | tagDec.add(4, 5, ENUM_3); | |
024373d7 MK |
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); | |
024373d7 MK |
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); | |
024373d7 MK |
110 | |
111 | sDec.addField(VAR_FIELD_NAME, varDec); | |
112 | varDec.setTag(TAG_ID); | |
113 | ||
a4fa4e36 MK |
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); | |
866e5b51 FC |
123 | } |
124 | ||
866e5b51 FC |
125 | /** |
126 | * Run the VariantDefinition(VariantDeclaration,DefinitionScope,String) | |
a4fa4e36 MK |
127 | * |
128 | * @throws CTFReaderException | |
129 | * should not happen | |
866e5b51 FC |
130 | */ |
131 | @Test | |
a4fa4e36 | 132 | public void testVariantDefinition() throws CTFReaderException { |
866e5b51 | 133 | VariantDeclaration declaration = new VariantDeclaration(); |
4a9c1f07 | 134 | declaration.setTag(""); |
866e5b51 | 135 | VariantDeclaration variantDeclaration = new VariantDeclaration(); |
a4fa4e36 MK |
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 | )); | |
866e5b51 | 162 | IDefinitionScope definitionScope = new StructDefinition( |
a4fa4e36 MK |
163 | new StructDeclaration(1L), |
164 | variantDefinition, | |
165 | "", | |
166 | ImmutableList.<String> of("", "variant"), | |
167 | new Definition[] { enumDefinition, variantDefinition } | |
168 | ); | |
4a9c1f07 | 169 | String fieldName = ""; |
a4fa4e36 MK |
170 | declaration.setTag(""); |
171 | VariantDefinition result = declaration.createDefinition(definitionScope, fieldName, bb); | |
866e5b51 FC |
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(); | |
024373d7 | 181 | assertNotNull(result); |
866e5b51 FC |
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() { | |
a4fa4e36 | 207 | Definition result = fixture.getCurrentField(); |
866e5b51 FC |
208 | assertNotNull(result); |
209 | } | |
210 | ||
211 | /** | |
212 | * Run the String getPath() method test. | |
213 | */ | |
214 | @Test | |
215 | public void testGetPath() { | |
a4fa4e36 | 216 | String result = fixture.getScopePath().toString(); |
866e5b51 FC |
217 | assertNotNull(result); |
218 | } | |
219 | ||
220 | /** | |
221 | * Run the ArrayDefinition lookupArray(String) method test. | |
222 | */ | |
223 | @Test | |
224 | public void testLookupArray() { | |
024373d7 | 225 | ArrayDefinition result = fixture.lookupArray(ENUM_3); |
a4fa4e36 | 226 | assertNull(result); |
866e5b51 FC |
227 | } |
228 | ||
229 | /** | |
230 | * Run the Definition lookupDefinition(String) method test. | |
231 | */ | |
232 | @Test | |
233 | public void testLookupDefinition() { | |
024373d7 MK |
234 | Definition result = fixture.lookupDefinition(ENUM_1); |
235 | assertNotNull(result); | |
a4fa4e36 | 236 | assertEquals("a", ((EnumDefinition) result).getStringValue()); |
866e5b51 FC |
237 | } |
238 | ||
239 | /** | |
240 | * Run the EnumDefinition lookupEnum(String) method test. | |
241 | */ | |
242 | @Test | |
243 | public void testLookupEnum() { | |
024373d7 | 244 | EnumDefinition result = fixture.lookupEnum(ENUM_5); |
a4fa4e36 | 245 | assertNull(result); |
866e5b51 FC |
246 | } |
247 | ||
248 | /** | |
249 | * Run the IntegerDefinition lookupInteger(String) method test. | |
250 | */ | |
251 | @Test | |
252 | public void testLookupInteger() { | |
024373d7 | 253 | IntegerDefinition result = fixture.lookupInteger(ENUM_2); |
a4fa4e36 | 254 | assertNull(result); |
866e5b51 FC |
255 | } |
256 | ||
257 | /** | |
258 | * Run the SequenceDefinition lookupSequence(String) method test. | |
259 | */ | |
260 | @Test | |
79cb3749 | 261 | public void testLookupSequence_1() { |
024373d7 | 262 | SequenceDefinition result = fixture.lookupSequence(ENUM_4); |
866e5b51 FC |
263 | assertNull(result); |
264 | } | |
265 | ||
266 | /** | |
267 | * Run the StringDefinition lookupString(String) method test. | |
268 | */ | |
269 | @Test | |
270 | public void testLookupString() { | |
024373d7 | 271 | StringDefinition result = fixture.lookupString(ENUM_1); |
a4fa4e36 | 272 | assertNull(result); |
866e5b51 FC |
273 | } |
274 | ||
275 | /** | |
276 | * Run the StructDefinition lookupStruct(String) method test. | |
277 | */ | |
278 | @Test | |
279 | public void testLookupStruct() { | |
024373d7 | 280 | StructDefinition result = fixture.lookupStruct(ENUM_6); |
a4fa4e36 | 281 | assertNull(result); |
866e5b51 FC |
282 | } |
283 | ||
284 | /** | |
285 | * Run the VariantDefinition lookupVariant(String) method test. | |
286 | */ | |
287 | @Test | |
288 | public void testLookupVariant() { | |
024373d7 | 289 | VariantDefinition result = fixture.lookupVariant(ENUM_8); |
866e5b51 FC |
290 | assertNull(result); |
291 | } | |
292 | ||
419f09a8 SM |
293 | /** |
294 | * Run the String toString() method test. | |
295 | */ | |
296 | @Test | |
297 | public void testToString() { | |
298 | String result = fixture.toString(); | |
a4fa4e36 | 299 | assertEquals("{ a = \"\" }", result); |
419f09a8 | 300 | } |
866e5b51 | 301 | } |