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