1 /*******************************************************************************
2 * Copyright (c) 2013, 2014 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
9 * Matthew Khouzam - Initial API and implementation
10 *******************************************************************************/
12 package org
.eclipse
.linuxtools
.ctf
.core
.tests
.types
;
14 import static org
.junit
.Assert
.assertEquals
;
15 import static org
.junit
.Assert
.assertNotNull
;
16 import static org
.junit
.Assert
.assertNull
;
18 import java
.nio
.ByteOrder
;
19 import java
.util
.HashMap
;
22 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.ArrayDeclaration
;
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
;
28 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.FloatDeclaration
;
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
;
33 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.StringDeclaration
;
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
;
39 import org
.junit
.Before
;
40 import org
.junit
.Test
;
43 * The class <code>VariantDefinitionTest</code> contains tests for the class
44 * <code>{@link VariantDefinition}</code>.
47 * @version $Revision: 1.0 $
49 public class VariantDefinitionTest
{
51 private VariantDefinition fixture
;
53 StructDefinition structDefinition
;
54 private static final String TEST_STRUCT_ID
= "testStruct";
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";
64 private static final String TAG_ID
= "a";
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";
71 private static final String LENGTH_SEQ
= "_len";
72 private static final String VAR_FIELD_NAME
= "var";
73 private static final String ENUM_8
= null;
76 * Perform pre-test initialization.
78 * Not sure it needs to be that complicated, oh well...
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
);
92 FloatDeclaration fDec
= new FloatDeclaration(8, 24, ByteOrder
.BIG_ENDIAN
, 8);
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
);
109 sDec
.addField(TAG_ID
, tagDec
);
110 sDec
.addField(LENGTH_SEQ
, lenDec
);
111 // sDec.addField(SEQUENCE_ID, seqDec);
113 sDec
.addField(VAR_FIELD_NAME
, varDec
);
114 varDec
.setTag(TAG_ID
);
116 structDefinition
= sDec
.createDefinition(null, TEST_STRUCT_ID
);
117 fixture
= (VariantDefinition
) structDefinition
.getDefinitions().get(VAR_FIELD_NAME
);
121 * Run the VariantDefinition(VariantDeclaration,DefinitionScope,String)
124 public void testVariantDefinition() {
125 VariantDeclaration declaration
= new VariantDeclaration();
126 declaration
.setTag("");
127 VariantDeclaration variantDeclaration
= new VariantDeclaration();
128 variantDeclaration
.setTag("");
129 VariantDefinition variantDefinition
= new VariantDefinition(
130 variantDeclaration
, structDefinition
, "");
131 IDefinitionScope definitionScope
= new StructDefinition(
132 new StructDeclaration(1L), variantDefinition
, "");
133 String fieldName
= "";
135 VariantDefinition result
= new VariantDefinition(declaration
,
136 definitionScope
, fieldName
);
137 assertNotNull(result
);
141 * Run the Definition getCurrentField() method test.
144 public void testGetCurrentField() {
145 Definition result
= fixture
.getCurrentField();
147 fixture
.setCurrentField(ENUM_1
);
148 result
= fixture
.getCurrentField();
149 assertNotNull(result
);
153 * Run the String getCurrentFieldName() method test.
156 public void testGetCurrentFieldName() {
157 fixture
.setCurrentField(ENUM_1
);
158 String result
= fixture
.getCurrentFieldName();
159 assertNotNull(result
);
163 * Run the VariantDeclaration getDeclaration() method test.
166 public void testGetDeclaration() {
167 VariantDeclaration result
= fixture
.getDeclaration();
168 assertNotNull(result
);
172 * Run the HashMap<String, Definition> getDefinitions() method test.
175 public void testGetDefinitions() {
176 Map
<String
, Definition
> result
= fixture
.getDefinitions();
177 assertNotNull(result
);
181 * Run the String getPath() method test.
184 public void testGetPath() {
185 String result
= fixture
.getPath();
186 assertNotNull(result
);
190 * Run the EnumDefinition getTagDefinition() method test.
193 public void testGetTagDefinition() {
194 EnumDefinition result
= fixture
.getTagDefinition();
195 assertNotNull(result
);
199 * Run the ArrayDefinition lookupArray(String) method test.
202 public void testLookupArray() {
203 ArrayDefinition result
= fixture
.lookupArray(ENUM_3
);
204 assertNotNull(result
);
208 * Run the Definition lookupDefinition(String) method test.
211 public void testLookupDefinition() {
212 Definition result
= fixture
.lookupDefinition(ENUM_1
);
213 assertNotNull(result
);
217 * Run the EnumDefinition lookupEnum(String) method test.
220 public void testLookupEnum() {
221 EnumDefinition result
= fixture
.lookupEnum(ENUM_5
);
222 assertNotNull(result
);
226 * Run the IntegerDefinition lookupInteger(String) method test.
229 public void testLookupInteger() {
230 IntegerDefinition result
= fixture
.lookupInteger(ENUM_2
);
231 assertNotNull(result
);
235 * Run the SequenceDefinition lookupSequence(String) method test.
238 public void testLookupSequence_1() {
239 SequenceDefinition result
= fixture
.lookupSequence(ENUM_4
);
244 * Run the StringDefinition lookupString(String) method test.
247 public void testLookupString() {
248 StringDefinition result
= fixture
.lookupString(ENUM_1
);
249 assertNotNull(result
);
253 * Run the StructDefinition lookupStruct(String) method test.
256 public void testLookupStruct() {
257 StructDefinition result
= fixture
.lookupStruct(ENUM_6
);
258 assertNotNull(result
);
262 * Run the VariantDefinition lookupVariant(String) method test.
265 public void testLookupVariant() {
266 VariantDefinition result
= fixture
.lookupVariant(ENUM_8
);
271 * Run the void setCurrentField(String) method test.
274 public void testSetCurrentField() {
275 fixture
.setCurrentField(ENUM_1
);
279 * Run the void setDeclaration(VariantDeclaration) method test.
282 public void testSetDeclaration() {
283 VariantDeclaration declaration
= new VariantDeclaration();
284 fixture
.setDeclaration(declaration
);
288 * Run the void setDefinitions(HashMap<String,Definition>) method test.
291 public void testSetDefinitions() {
292 HashMap
<String
, Definition
> definitions
= new HashMap
<>();
293 fixture
.setDefinitions(definitions
);
297 * Run the void setTagDefinition(EnumDefinition) method test.
300 public void testSetTagDefinition(){
301 VariantDeclaration vDecl
;
302 VariantDefinition vDef
;
303 StructDefinition structDef
;
304 EnumDefinition tagDefinition
;
307 vDecl
= new VariantDeclaration();
309 vDef
= new VariantDefinition(vDecl
, structDefinition
, fName
);
310 structDef
= new StructDefinition(new StructDeclaration(1L), vDef
, fName
);
311 tagDefinition
= new EnumDefinition(new EnumDeclaration(
312 new IntegerDeclaration(1, false, 1, ByteOrder
.BIG_ENDIAN
,
313 Encoding
.ASCII
, fName
, 8)), structDef
, fName
);
315 fixture
.setTagDefinition(tagDefinition
);
319 * Run the String toString() method test.
322 public void testToString() {
323 String result
= fixture
.toString();
324 assertEquals("{ null = null }", result
);
326 fixture
.setCurrentField(ENUM_2
);
327 result
= fixture
.toString();
328 assertEquals("{ b = 0 }", result
);