Commit | Line | Data |
---|---|---|
4bd7f2db AM |
1 | /******************************************************************************* |
2 | * Copyright (c) 2013 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 | ||
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 | ||
18 | import java.nio.ByteOrder; | |
19 | import java.util.HashMap; | |
20 | ||
024373d7 | 21 | import org.eclipse.linuxtools.ctf.core.event.types.ArrayDeclaration; |
866e5b51 FC |
22 | import org.eclipse.linuxtools.ctf.core.event.types.ArrayDefinition; |
23 | import org.eclipse.linuxtools.ctf.core.event.types.Definition; | |
24 | import org.eclipse.linuxtools.ctf.core.event.types.Encoding; | |
25 | import org.eclipse.linuxtools.ctf.core.event.types.EnumDeclaration; | |
26 | import org.eclipse.linuxtools.ctf.core.event.types.EnumDefinition; | |
024373d7 | 27 | import org.eclipse.linuxtools.ctf.core.event.types.FloatDeclaration; |
866e5b51 FC |
28 | import org.eclipse.linuxtools.ctf.core.event.types.IDefinitionScope; |
29 | import org.eclipse.linuxtools.ctf.core.event.types.IntegerDeclaration; | |
30 | import org.eclipse.linuxtools.ctf.core.event.types.IntegerDefinition; | |
31 | import org.eclipse.linuxtools.ctf.core.event.types.SequenceDefinition; | |
024373d7 | 32 | import org.eclipse.linuxtools.ctf.core.event.types.StringDeclaration; |
866e5b51 FC |
33 | import org.eclipse.linuxtools.ctf.core.event.types.StringDefinition; |
34 | import org.eclipse.linuxtools.ctf.core.event.types.StructDeclaration; | |
35 | import org.eclipse.linuxtools.ctf.core.event.types.StructDefinition; | |
36 | import org.eclipse.linuxtools.ctf.core.event.types.VariantDeclaration; | |
37 | import org.eclipse.linuxtools.ctf.core.event.types.VariantDefinition; | |
866e5b51 FC |
38 | import org.junit.After; |
39 | import org.junit.Before; | |
40 | import org.junit.Test; | |
41 | ||
42 | /** | |
43 | * The class <code>VariantDefinitionTest</code> contains tests for the class | |
44 | * <code>{@link VariantDefinition}</code>. | |
284fdee8 | 45 | * |
866e5b51 FC |
46 | * @author ematkho |
47 | * @version $Revision: 1.0 $ | |
48 | */ | |
be6df2d8 | 49 | @SuppressWarnings("javadoc") |
866e5b51 FC |
50 | public class VariantDefinitionTest { |
51 | ||
52 | private VariantDefinition fixture; | |
53 | ||
54 | /** | |
55 | * Launch the test. | |
284fdee8 | 56 | * |
866e5b51 FC |
57 | * @param args |
58 | * the command line arguments | |
59 | */ | |
60 | public static void main(String[] args) { | |
61 | new org.junit.runner.JUnitCore().run(VariantDefinitionTest.class); | |
62 | } | |
4a9c1f07 | 63 | |
024373d7 | 64 | StructDefinition structDefinition; |
4a9c1f07 AM |
65 | private static final String TEST_STRUCT_ID = "testStruct"; |
66 | ||
67 | private static final String ENUM_7 = "g"; | |
68 | private static final String ENUM_6 = "f"; | |
69 | private static final String ENUM_5 = "e"; | |
70 | private static final String ENUM_4 = "d"; | |
71 | private static final String ENUM_3 = "c"; | |
72 | private static final String ENUM_2 = "b"; | |
73 | private static final String ENUM_1 = "a"; | |
74 | ||
75 | private static final String TAG_ID = "a"; | |
76 | ||
77 | // private static final String INT_ID = "_id"; | |
78 | // private static final String STRING_ID = "_args"; | |
79 | // private static final String ENUM_ID = "_enumArgs"; | |
80 | // private static final String SEQUENCE_ID = "_seq"; | |
81 | ||
82 | private static final String LENGTH_SEQ = "_len"; | |
83 | private static final String VAR_FIELD_NAME = "var"; | |
024373d7 | 84 | private static final String ENUM_8 = null; |
866e5b51 FC |
85 | /** |
86 | * Perform pre-test initialization. | |
284fdee8 | 87 | * |
866e5b51 | 88 | * Not sure it needs to be that complicated, oh well... |
fd74e6c1 MK |
89 | * |
90 | * @throws CTFReaderException | |
866e5b51 FC |
91 | */ |
92 | @Before | |
024373d7 MK |
93 | public void setUp() { |
94 | StructDeclaration sDec = new StructDeclaration(12); | |
95 | StructDeclaration smallStruct = new StructDeclaration(8); | |
96 | IntegerDeclaration iDec = new IntegerDeclaration(32, false, 32, ByteOrder.BIG_ENDIAN, Encoding.NONE, null, 8); | |
97 | IntegerDeclaration lenDec = new IntegerDeclaration(8, false, 8, ByteOrder.BIG_ENDIAN, Encoding.NONE, null, 8); | |
98 | StringDeclaration strDec = new StringDeclaration(); | |
99 | EnumDeclaration enDec = new EnumDeclaration(iDec); | |
100 | // SequenceDeclaration seqDec = new SequenceDeclaration(LENGTH_SEQ, iDec); | |
101 | VariantDeclaration varDec = new VariantDeclaration(); | |
102 | EnumDeclaration tagDec = new EnumDeclaration(iDec); | |
103 | ArrayDeclaration arrDec = new ArrayDeclaration(2, iDec); | |
07002e0a | 104 | FloatDeclaration fDec = new FloatDeclaration(8, 24, ByteOrder.BIG_ENDIAN, 8); |
024373d7 MK |
105 | tagDec.add(0, 1, ENUM_1); |
106 | tagDec.add(2, 3, ENUM_2); | |
107 | tagDec.add(4, 5, ENUM_3); | |
108 | //tagDec.add(6, 7, ENUM_4); // this should not work | |
109 | tagDec.add(8, 9, ENUM_5); | |
110 | tagDec.add(10, 11, ENUM_6); | |
111 | tagDec.add(12, 13, ENUM_7); | |
112 | varDec.addField(ENUM_4, lenDec); | |
113 | varDec.addField(ENUM_7, fDec); | |
114 | varDec.addField(ENUM_6, smallStruct); | |
115 | varDec.addField(ENUM_5, enDec); | |
116 | //varDec.addField(ENUM_4, seqDec);// this should not work | |
117 | varDec.addField(ENUM_3, arrDec); | |
118 | varDec.addField(ENUM_2, iDec); | |
119 | varDec.addField(ENUM_1, strDec); | |
120 | ||
121 | sDec.addField(TAG_ID, tagDec); | |
122 | sDec.addField(LENGTH_SEQ, lenDec); | |
123 | // sDec.addField(SEQUENCE_ID, seqDec); | |
124 | ||
125 | sDec.addField(VAR_FIELD_NAME, varDec); | |
126 | varDec.setTag(TAG_ID); | |
127 | ||
128 | structDefinition = sDec.createDefinition(null, TEST_STRUCT_ID); | |
419f09a8 | 129 | fixture = (VariantDefinition) structDefinition.getDefinitions().get(VAR_FIELD_NAME); |
866e5b51 FC |
130 | } |
131 | ||
132 | /** | |
133 | * Perform post-test clean-up. | |
134 | */ | |
135 | @After | |
136 | public void tearDown() { | |
137 | // Add additional tear down code here | |
138 | } | |
139 | ||
140 | /** | |
141 | * Run the VariantDefinition(VariantDeclaration,DefinitionScope,String) | |
fd74e6c1 MK |
142 | * |
143 | * @throws CTFReaderException | |
866e5b51 FC |
144 | */ |
145 | @Test | |
024373d7 | 146 | public void testVariantDefinition() { |
866e5b51 | 147 | VariantDeclaration declaration = new VariantDeclaration(); |
4a9c1f07 | 148 | declaration.setTag(""); |
866e5b51 | 149 | VariantDeclaration variantDeclaration = new VariantDeclaration(); |
4a9c1f07 | 150 | variantDeclaration.setTag(""); |
866e5b51 | 151 | VariantDefinition variantDefinition = new VariantDefinition( |
4a9c1f07 | 152 | variantDeclaration, structDefinition, ""); |
866e5b51 | 153 | IDefinitionScope definitionScope = new StructDefinition( |
4a9c1f07 AM |
154 | new StructDeclaration(1L), variantDefinition, ""); |
155 | String fieldName = ""; | |
866e5b51 FC |
156 | |
157 | VariantDefinition result = new VariantDefinition(declaration, | |
158 | definitionScope, fieldName); | |
159 | assertNotNull(result); | |
160 | } | |
161 | ||
162 | /** | |
163 | * Run the Definition getCurrentField() method test. | |
164 | */ | |
165 | @Test | |
166 | public void testGetCurrentField() { | |
167 | Definition result = fixture.getCurrentField(); | |
168 | assertNull(result); | |
024373d7 MK |
169 | fixture.setCurrentField(ENUM_1); |
170 | result = fixture.getCurrentField(); | |
171 | assertNotNull(result); | |
866e5b51 FC |
172 | } |
173 | ||
174 | /** | |
175 | * Run the String getCurrentFieldName() method test. | |
176 | */ | |
177 | @Test | |
178 | public void testGetCurrentFieldName() { | |
024373d7 | 179 | fixture.setCurrentField(ENUM_1); |
866e5b51 FC |
180 | String result = fixture.getCurrentFieldName(); |
181 | assertNotNull(result); | |
182 | } | |
183 | ||
184 | /** | |
185 | * Run the VariantDeclaration getDeclaration() method test. | |
186 | */ | |
187 | @Test | |
188 | public void testGetDeclaration() { | |
189 | VariantDeclaration result = fixture.getDeclaration(); | |
190 | assertNotNull(result); | |
191 | } | |
192 | ||
193 | /** | |
194 | * Run the HashMap<String, Definition> getDefinitions() method test. | |
195 | */ | |
196 | @Test | |
197 | public void testGetDefinitions() { | |
198 | HashMap<String, Definition> result = fixture.getDefinitions(); | |
199 | assertNotNull(result); | |
200 | } | |
201 | ||
202 | /** | |
203 | * Run the String getPath() method test. | |
204 | */ | |
205 | @Test | |
206 | public void testGetPath() { | |
207 | String result = fixture.getPath(); | |
208 | assertNotNull(result); | |
209 | } | |
210 | ||
211 | /** | |
212 | * Run the EnumDefinition getTagDefinition() method test. | |
213 | */ | |
214 | @Test | |
215 | public void testGetTagDefinition() { | |
216 | EnumDefinition result = fixture.getTagDefinition(); | |
217 | assertNotNull(result); | |
218 | } | |
219 | ||
220 | /** | |
221 | * Run the ArrayDefinition lookupArray(String) method test. | |
222 | */ | |
223 | @Test | |
224 | public void testLookupArray() { | |
024373d7 MK |
225 | ArrayDefinition result = fixture.lookupArray(ENUM_3); |
226 | assertNotNull(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); | |
866e5b51 FC |
236 | } |
237 | ||
238 | /** | |
239 | * Run the EnumDefinition lookupEnum(String) method test. | |
240 | */ | |
241 | @Test | |
242 | public void testLookupEnum() { | |
024373d7 MK |
243 | EnumDefinition result = fixture.lookupEnum(ENUM_5); |
244 | assertNotNull(result); | |
866e5b51 FC |
245 | } |
246 | ||
247 | /** | |
248 | * Run the IntegerDefinition lookupInteger(String) method test. | |
249 | */ | |
250 | @Test | |
251 | public void testLookupInteger() { | |
024373d7 MK |
252 | IntegerDefinition result = fixture.lookupInteger(ENUM_2); |
253 | assertNotNull(result); | |
866e5b51 FC |
254 | } |
255 | ||
256 | /** | |
257 | * Run the SequenceDefinition lookupSequence(String) method test. | |
258 | */ | |
259 | @Test | |
79cb3749 | 260 | public void testLookupSequence_1() { |
024373d7 | 261 | SequenceDefinition result = fixture.lookupSequence(ENUM_4); |
866e5b51 FC |
262 | assertNull(result); |
263 | } | |
264 | ||
265 | /** | |
266 | * Run the StringDefinition lookupString(String) method test. | |
267 | */ | |
268 | @Test | |
269 | public void testLookupString() { | |
024373d7 MK |
270 | StringDefinition result = fixture.lookupString(ENUM_1); |
271 | assertNotNull(result); | |
866e5b51 FC |
272 | } |
273 | ||
274 | /** | |
275 | * Run the StructDefinition lookupStruct(String) method test. | |
276 | */ | |
277 | @Test | |
278 | public void testLookupStruct() { | |
024373d7 MK |
279 | StructDefinition result = fixture.lookupStruct(ENUM_6); |
280 | assertNotNull(result); | |
866e5b51 FC |
281 | } |
282 | ||
283 | /** | |
284 | * Run the VariantDefinition lookupVariant(String) method test. | |
285 | */ | |
286 | @Test | |
287 | public void testLookupVariant() { | |
024373d7 | 288 | VariantDefinition result = fixture.lookupVariant(ENUM_8); |
866e5b51 FC |
289 | assertNull(result); |
290 | } | |
291 | ||
292 | /** | |
293 | * Run the void setCurrentField(String) method test. | |
294 | */ | |
295 | @Test | |
296 | public void testSetCurrentField() { | |
024373d7 | 297 | fixture.setCurrentField(ENUM_1); |
866e5b51 FC |
298 | } |
299 | ||
300 | /** | |
301 | * Run the void setDeclaration(VariantDeclaration) method test. | |
302 | */ | |
303 | @Test | |
304 | public void testSetDeclaration() { | |
305 | VariantDeclaration declaration = new VariantDeclaration(); | |
306 | fixture.setDeclaration(declaration); | |
307 | } | |
308 | ||
309 | /** | |
310 | * Run the void setDefinitions(HashMap<String,Definition>) method test. | |
311 | */ | |
312 | @Test | |
313 | public void testSetDefinitions() { | |
314 | HashMap<String, Definition> definitions = new HashMap<String, Definition>(); | |
315 | fixture.setDefinitions(definitions); | |
316 | } | |
317 | ||
318 | /** | |
319 | * Run the void setTagDefinition(EnumDefinition) method test. | |
fd74e6c1 MK |
320 | * |
321 | * @throws CTFReaderException | |
866e5b51 FC |
322 | */ |
323 | @Test | |
024373d7 | 324 | public void testSetTagDefinition(){ |
866e5b51 FC |
325 | VariantDeclaration vDecl; |
326 | VariantDefinition vDef; | |
327 | StructDefinition structDef; | |
328 | EnumDefinition tagDefinition; | |
4a9c1f07 | 329 | String fName = ""; |
866e5b51 FC |
330 | |
331 | vDecl = new VariantDeclaration(); | |
332 | vDecl.setTag(fName); | |
024373d7 | 333 | vDef = new VariantDefinition(vDecl, structDefinition, fName); |
866e5b51 FC |
334 | structDef = new StructDefinition(new StructDeclaration(1L), vDef, fName); |
335 | tagDefinition = new EnumDefinition(new EnumDeclaration( | |
336 | new IntegerDeclaration(1, true, 1, ByteOrder.BIG_ENDIAN, | |
fd74e6c1 | 337 | Encoding.ASCII, fName, 8)), structDef, fName); |
866e5b51 FC |
338 | |
339 | fixture.setTagDefinition(tagDefinition); | |
340 | } | |
419f09a8 SM |
341 | |
342 | /** | |
343 | * Run the String toString() method test. | |
344 | */ | |
345 | @Test | |
346 | public void testToString() { | |
347 | String result = fixture.toString(); | |
4a9c1f07 | 348 | assertEquals("{ null = null }", result); |
419f09a8 SM |
349 | |
350 | fixture.setCurrentField(ENUM_2); | |
351 | result = fixture.toString(); | |
4a9c1f07 | 352 | assertEquals("{ b = 0 }", result); |
419f09a8 | 353 | } |
866e5b51 | 354 | } |