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