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
.tracecompass
.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
.tracecompass
.ctf
.core
.CTFException
;
23 import org
.eclipse
.tracecompass
.ctf
.core
.event
.io
.BitBuffer
;
24 import org
.eclipse
.tracecompass
.ctf
.core
.event
.scope
.IDefinitionScope
;
25 import org
.eclipse
.tracecompass
.ctf
.core
.event
.types
.AbstractArrayDefinition
;
26 import org
.eclipse
.tracecompass
.ctf
.core
.event
.types
.CompoundDeclaration
;
27 import org
.eclipse
.tracecompass
.ctf
.core
.event
.types
.Definition
;
28 import org
.eclipse
.tracecompass
.ctf
.core
.event
.types
.Encoding
;
29 import org
.eclipse
.tracecompass
.ctf
.core
.event
.types
.EnumDeclaration
;
30 import org
.eclipse
.tracecompass
.ctf
.core
.event
.types
.EnumDefinition
;
31 import org
.eclipse
.tracecompass
.ctf
.core
.event
.types
.FloatDeclaration
;
32 import org
.eclipse
.tracecompass
.ctf
.core
.event
.types
.IDefinition
;
33 import org
.eclipse
.tracecompass
.ctf
.core
.event
.types
.IntegerDeclaration
;
34 import org
.eclipse
.tracecompass
.ctf
.core
.event
.types
.IntegerDefinition
;
35 import org
.eclipse
.tracecompass
.ctf
.core
.event
.types
.StringDeclaration
;
36 import org
.eclipse
.tracecompass
.ctf
.core
.event
.types
.StringDefinition
;
37 import org
.eclipse
.tracecompass
.ctf
.core
.event
.types
.StructDeclaration
;
38 import org
.eclipse
.tracecompass
.ctf
.core
.event
.types
.StructDefinition
;
39 import org
.eclipse
.tracecompass
.ctf
.core
.event
.types
.VariantDeclaration
;
40 import org
.eclipse
.tracecompass
.ctf
.core
.event
.types
.VariantDefinition
;
41 import org
.eclipse
.tracecompass
.ctf
.core
.tests
.io
.Util
;
42 import org
.eclipse
.tracecompass
.internal
.ctf
.core
.event
.types
.ArrayDeclaration
;
43 import org
.junit
.Before
;
44 import org
.junit
.Test
;
47 * The class <code>VariantDefinitionTest</code> contains tests for the class
48 * <code>{@link VariantDefinition}</code>.
51 * @version $Revision: 1.0 $
53 public class VariantDefinitionTest
{
55 private VariantDefinition fixture
;
57 StructDefinition fStructDefinition
;
58 private static final @NonNull String TEST_STRUCT_ID
= "testStruct";
59 private static final @NonNull String ENUM_7
= "g";
60 private static final @NonNull String ENUM_6
= "f";
61 private static final @NonNull String ENUM_5
= "e";
62 private static final @NonNull String ENUM_4
= "d";
63 private static final @NonNull String ENUM_3
= "c";
64 private static final @NonNull String ENUM_2
= "b";
65 private static final @NonNull String ENUM_1
= "a";
66 private static final @NonNull String TAG_ID
= "a";
67 private static final @NonNull String LENGTH_SEQ
= "_len";
68 private static final @NonNull String VAR_FIELD_NAME
= "var";
69 private static final @NonNull String ENUM_8
= "bbq ribs";
72 * Perform pre-test initialization.
74 * Not sure it needs to be that complicated, oh well...
76 * @throws CTFException
80 public void setUp() throws CTFException
{
81 StructDeclaration sDec
= new StructDeclaration(12);
82 StructDeclaration smallStruct
= new StructDeclaration(8);
83 IntegerDeclaration iDec
= IntegerDeclaration
.createDeclaration(32, false, 32, ByteOrder
.BIG_ENDIAN
, Encoding
.NONE
, "", 8);
84 IntegerDeclaration lenDec
= IntegerDeclaration
.createDeclaration(8, false, 8, ByteOrder
.BIG_ENDIAN
, Encoding
.NONE
, "", 8);
85 StringDeclaration strDec
= StringDeclaration
.getStringDeclaration(Encoding
.UTF8
);
86 EnumDeclaration enDec
= new EnumDeclaration(iDec
);
87 VariantDeclaration varDec
= new VariantDeclaration();
88 EnumDeclaration tagDec
= new EnumDeclaration(iDec
);
89 CompoundDeclaration arrDec
= new ArrayDeclaration(2, iDec
);
90 FloatDeclaration fDec
= new FloatDeclaration(8, 24, ByteOrder
.BIG_ENDIAN
, 8);
91 tagDec
.add(0, 1, ENUM_1
);
92 tagDec
.add(2, 3, ENUM_2
);
93 tagDec
.add(4, 5, ENUM_3
);
94 tagDec
.add(8, 9, ENUM_5
);
95 tagDec
.add(10, 11, ENUM_6
);
96 tagDec
.add(12, 13, ENUM_7
);
97 varDec
.addField(ENUM_4
, lenDec
);
98 varDec
.addField(ENUM_7
, fDec
);
99 varDec
.addField(ENUM_6
, smallStruct
);
100 varDec
.addField(ENUM_5
, enDec
);
101 varDec
.addField(ENUM_3
, arrDec
);
102 varDec
.addField(ENUM_2
, iDec
);
103 varDec
.addField(ENUM_1
, strDec
);
105 sDec
.addField(TAG_ID
, tagDec
);
106 sDec
.addField(LENGTH_SEQ
, lenDec
);
108 sDec
.addField(VAR_FIELD_NAME
, varDec
);
109 varDec
.setTag(TAG_ID
);
111 final ByteBuffer byteBuffer
= Util
.testMemory(ByteBuffer
.allocate(100));
112 BitBuffer bb
= new BitBuffer(byteBuffer
);
114 byteBuffer
.putInt(1);
115 byteBuffer
.putInt(2);
116 byteBuffer
.putInt(3);
118 fStructDefinition
= sDec
.createDefinition(null, TEST_STRUCT_ID
, bb
);
119 fixture
= (VariantDefinition
) fStructDefinition
.getDefinition(VAR_FIELD_NAME
);
123 * Run the VariantDefinition(VariantDeclaration,DefinitionScope,String)
125 * @throws CTFException
129 public void testVariantDefinition() throws CTFException
{
130 VariantDeclaration declaration
= new VariantDeclaration();
131 declaration
.setTag("");
132 VariantDeclaration variantDeclaration
= new VariantDeclaration();
133 variantDeclaration
.addField("", new EnumDeclaration(IntegerDeclaration
.INT_32B_DECL
));
134 variantDeclaration
.addField("a", IntegerDeclaration
.INT_64B_DECL
);
135 declaration
.addField(ENUM_3
, StringDeclaration
.getStringDeclaration(Encoding
.UTF8
));
136 variantDeclaration
.setTag("a");
138 byte[] bytes
= new byte[128];
139 ByteBuffer byb
= ByteBuffer
.wrap(bytes
);
142 byb
.putShort((short) 2);
143 byb
.put(new String("hello").getBytes());
145 BitBuffer bb
= new BitBuffer(byb
);
146 VariantDefinition variantDefinition
= variantDeclaration
.createDefinition(fStructDefinition
, "field", bb
);
147 EnumDeclaration declaration2
= new EnumDeclaration(IntegerDeclaration
.INT_8_DECL
);
148 declaration2
.add(0, 2, ENUM_3
);
149 EnumDefinition enumDefinition
= new EnumDefinition(
153 new IntegerDefinition(
154 IntegerDeclaration
.INT_8_DECL
,
159 final StructDeclaration declarationScope
= new StructDeclaration(1L);
160 declarationScope
.addField("", enumDefinition
.getDeclaration());
161 declarationScope
.addField("variant", variantDeclaration
);
162 IDefinitionScope definitionScope
= new StructDefinition(
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().getPath();
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
);