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
.ByteBuffer
;
19 import java
.nio
.ByteOrder
;
21 import org
.eclipse
.jdt
.annotation
.NonNull
;
22 import org
.eclipse
.linuxtools
.ctf
.core
.event
.io
.BitBuffer
;
23 import org
.eclipse
.linuxtools
.ctf
.core
.event
.scope
.IDefinitionScope
;
24 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.AbstractArrayDefinition
;
25 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.CompoundDeclaration
;
26 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.Definition
;
27 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.Encoding
;
28 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.EnumDeclaration
;
29 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.EnumDefinition
;
30 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.FloatDeclaration
;
31 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.IDefinition
;
32 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.IntegerDeclaration
;
33 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.IntegerDefinition
;
34 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.StringDeclaration
;
35 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.StringDefinition
;
36 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.StructDeclaration
;
37 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.StructDefinition
;
38 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.VariantDeclaration
;
39 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.VariantDefinition
;
40 import org
.eclipse
.linuxtools
.ctf
.core
.tests
.io
.Util
;
41 import org
.eclipse
.linuxtools
.ctf
.core
.trace
.CTFReaderException
;
42 import org
.eclipse
.linuxtools
.internal
.ctf
.core
.event
.types
.ArrayDeclaration
;
43 import org
.junit
.Before
;
44 import org
.junit
.Test
;
46 import com
.google
.common
.collect
.ImmutableList
;
49 * The class <code>VariantDefinitionTest</code> contains tests for the class
50 * <code>{@link VariantDefinition}</code>.
53 * @version $Revision: 1.0 $
55 public class VariantDefinitionTest
{
57 private VariantDefinition fixture
;
59 StructDefinition fStructDefinition
;
60 private static final @NonNull String TEST_STRUCT_ID
= "testStruct";
61 private static final @NonNull String ENUM_7
= "g";
62 private static final @NonNull String ENUM_6
= "f";
63 private static final @NonNull String ENUM_5
= "e";
64 private static final @NonNull String ENUM_4
= "d";
65 private static final @NonNull String ENUM_3
= "c";
66 private static final @NonNull String ENUM_2
= "b";
67 private static final @NonNull String ENUM_1
= "a";
68 private static final @NonNull String TAG_ID
= "a";
69 private static final @NonNull String LENGTH_SEQ
= "_len";
70 private static final @NonNull String VAR_FIELD_NAME
= "var";
71 private static final @NonNull String ENUM_8
= "bbq ribs";
74 * Perform pre-test initialization.
76 * Not sure it needs to be that complicated, oh well...
78 * @throws CTFReaderException
82 public void setUp() throws CTFReaderException
{
83 StructDeclaration sDec
= new StructDeclaration(12);
84 StructDeclaration smallStruct
= new StructDeclaration(8);
85 IntegerDeclaration iDec
= IntegerDeclaration
.createDeclaration(32, false, 32, ByteOrder
.BIG_ENDIAN
, Encoding
.NONE
, "", 8);
86 IntegerDeclaration lenDec
= IntegerDeclaration
.createDeclaration(8, false, 8, ByteOrder
.BIG_ENDIAN
, Encoding
.NONE
, "", 8);
87 StringDeclaration strDec
= new StringDeclaration();
88 EnumDeclaration enDec
= new EnumDeclaration(iDec
);
89 VariantDeclaration varDec
= new VariantDeclaration();
90 EnumDeclaration tagDec
= new EnumDeclaration(iDec
);
91 CompoundDeclaration 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(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_3
, arrDec
);
104 varDec
.addField(ENUM_2
, iDec
);
105 varDec
.addField(ENUM_1
, strDec
);
107 sDec
.addField(TAG_ID
, tagDec
);
108 sDec
.addField(LENGTH_SEQ
, lenDec
);
110 sDec
.addField(VAR_FIELD_NAME
, varDec
);
111 varDec
.setTag(TAG_ID
);
113 final ByteBuffer byteBuffer
= Util
.testMemory(ByteBuffer
.allocate(100));
114 BitBuffer bb
= new BitBuffer(byteBuffer
);
116 byteBuffer
.putInt(1);
117 byteBuffer
.putInt(2);
118 byteBuffer
.putInt(3);
120 fStructDefinition
= sDec
.createDefinition(null, TEST_STRUCT_ID
, bb
);
121 fixture
= (VariantDefinition
) fStructDefinition
.getDefinition(VAR_FIELD_NAME
);
125 * Run the VariantDefinition(VariantDeclaration,DefinitionScope,String)
127 * @throws CTFReaderException
131 public void testVariantDefinition() throws CTFReaderException
{
132 VariantDeclaration declaration
= new VariantDeclaration();
133 declaration
.setTag("");
134 VariantDeclaration variantDeclaration
= new VariantDeclaration();
135 variantDeclaration
.addField("", new EnumDeclaration(IntegerDeclaration
.INT_32B_DECL
));
136 variantDeclaration
.addField("a", IntegerDeclaration
.INT_64B_DECL
);
137 declaration
.addField(ENUM_3
, new StringDeclaration());
138 variantDeclaration
.setTag("a");
140 byte[] bytes
= new byte[128];
141 ByteBuffer byb
= ByteBuffer
.wrap(bytes
);
144 byb
.putShort((short) 2);
145 byb
.put(new String("hello").getBytes());
147 BitBuffer bb
= new BitBuffer(byb
);
148 VariantDefinition variantDefinition
= variantDeclaration
.createDefinition(fStructDefinition
, "field", bb
);
149 EnumDeclaration declaration2
= new EnumDeclaration(IntegerDeclaration
.INT_8_DECL
);
150 declaration2
.add(0, 2, ENUM_3
);
151 EnumDefinition enumDefinition
= new EnumDefinition(
155 new IntegerDefinition(
156 IntegerDeclaration
.INT_8_DECL
,
161 IDefinitionScope definitionScope
= new StructDefinition(
162 new StructDeclaration(1L),
165 ImmutableList
.<String
> of("", "variant"),
166 new Definition
[] { enumDefinition
, variantDefinition
}
168 String fieldName
= "";
169 declaration
.setTag("");
170 VariantDefinition result
= declaration
.createDefinition(definitionScope
, fieldName
, bb
);
171 assertNotNull(result
);
175 * Run the Definition getCurrentField() method test.
178 public void testGetCurrentField() {
179 IDefinition result
= fixture
.getCurrentField();
180 assertNotNull(result
);
184 * Run the String getCurrentFieldName() method test.
187 public void testGetCurrentFieldName() {
188 String result
= fixture
.getCurrentFieldName();
189 assertNotNull(result
);
193 * Run the VariantDeclaration getDeclaration() method test.
196 public void testGetDeclaration() {
197 VariantDeclaration result
= fixture
.getDeclaration();
198 assertNotNull(result
);
202 * Run the HashMap<String, Definition> getDefinitions() method test.
205 public void testGetDefinitions() {
206 IDefinition result
= fixture
.getCurrentField();
207 assertNotNull(result
);
211 * Run the String getPath() method test.
214 public void testGetPath() {
215 String result
= fixture
.getScopePath().toString();
216 assertNotNull(result
);
220 * Run the ArrayDefinition lookupArray(String) method test.
223 public void testLookupArray() {
224 AbstractArrayDefinition result
= fixture
.lookupArrayDefinition(ENUM_3
);
229 * Run the Definition lookupDefinition(String) method test.
232 public void testLookupDefinition() {
233 IDefinition result
= fixture
.lookupDefinition(ENUM_1
);
234 assertNotNull(result
);
235 assertEquals("a", ((EnumDefinition
) result
).getStringValue());
239 * Run the EnumDefinition lookupEnum(String) method test.
242 public void testLookupEnum() {
243 EnumDefinition result
= fixture
.lookupEnum(ENUM_5
);
248 * Run the IntegerDefinition lookupInteger(String) method test.
251 public void testLookupInteger() {
252 IntegerDefinition result
= fixture
.lookupInteger(ENUM_2
);
257 * Run the StringDefinition lookupString(String) method test.
260 public void testLookupString() {
261 StringDefinition result
= fixture
.lookupString(ENUM_1
);
266 * Run the StructDefinition lookupStruct(String) method test.
269 public void testLookupStruct() {
270 StructDefinition result
= fixture
.lookupStruct(ENUM_6
);
275 * Run the VariantDefinition lookupVariant(String) method test.
278 public void testLookupVariant() {
279 VariantDefinition result
= fixture
.lookupVariant(ENUM_8
);
284 * Run the String toString() method test.
287 public void testToString() {
288 String result
= fixture
.toString();
289 assertEquals("{ a = \"\" }", result
);