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