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
.assertNotEquals
;
16 import static org
.junit
.Assert
.assertNotNull
;
18 import java
.nio
.ByteBuffer
;
20 import org
.eclipse
.tracecompass
.ctf
.core
.CTFException
;
21 import org
.eclipse
.tracecompass
.ctf
.core
.event
.io
.BitBuffer
;
22 import org
.eclipse
.tracecompass
.ctf
.core
.event
.scope
.IDefinitionScope
;
23 import org
.eclipse
.tracecompass
.ctf
.core
.event
.types
.Definition
;
24 import org
.eclipse
.tracecompass
.ctf
.core
.event
.types
.Encoding
;
25 import org
.eclipse
.tracecompass
.ctf
.core
.event
.types
.EnumDeclaration
;
26 import org
.eclipse
.tracecompass
.ctf
.core
.event
.types
.EnumDefinition
;
27 import org
.eclipse
.tracecompass
.ctf
.core
.event
.types
.IDeclaration
;
28 import org
.eclipse
.tracecompass
.ctf
.core
.event
.types
.IntegerDeclaration
;
29 import org
.eclipse
.tracecompass
.ctf
.core
.event
.types
.IntegerDefinition
;
30 import org
.eclipse
.tracecompass
.ctf
.core
.event
.types
.StringDeclaration
;
31 import org
.eclipse
.tracecompass
.ctf
.core
.event
.types
.StringDefinition
;
32 import org
.eclipse
.tracecompass
.ctf
.core
.event
.types
.StructDeclaration
;
33 import org
.eclipse
.tracecompass
.ctf
.core
.event
.types
.StructDefinition
;
34 import org
.eclipse
.tracecompass
.ctf
.core
.event
.types
.VariantDeclaration
;
35 import org
.eclipse
.tracecompass
.ctf
.core
.event
.types
.VariantDefinition
;
36 import org
.eclipse
.tracecompass
.ctf
.core
.tests
.shared
.CtfTestTraceUtils
;
37 import org
.eclipse
.tracecompass
.testtraces
.ctf
.CtfTestTrace
;
38 import org
.junit
.Before
;
39 import org
.junit
.Test
;
42 * The class <code>VariantDeclarationTest</code> contains tests for the class
43 * <code>{@link VariantDeclaration}</code>.
46 * @version $Revision: 1.0 $
48 public class VariantDeclarationTest
{
50 private static final CtfTestTrace testTrace
= CtfTestTrace
.KERNEL
;
52 private VariantDeclaration fixture
;
55 * Perform pre-test initialization.
59 fixture
= new VariantDeclaration();
62 private static IDefinitionScope
createDefinitionScope() throws CTFException
{
63 StructDeclaration declaration
= new StructDeclaration(8);
64 VariantDeclaration variantDeclaration
= new VariantDeclaration();
65 variantDeclaration
.addField("a", IntegerDeclaration
.INT_32B_DECL
);
66 variantDeclaration
.addField("b", IntegerDeclaration
.INT_32L_DECL
);
67 variantDeclaration
.setTag("a");
69 EnumDeclaration enumDeclaration
= new EnumDeclaration(IntegerDeclaration
.UINT_8_DECL
);
70 enumDeclaration
.add(0, 1, "a");
71 enumDeclaration
.add(2, 2, "b");
72 declaration
.addField("tag", enumDeclaration
);
73 declaration
.addField("variant", variantDeclaration
);
74 EnumDefinition tagDef
= new EnumDefinition(
78 new IntegerDefinition(
79 IntegerDeclaration
.UINT_8_DECL
,
84 VariantDefinition variantDefinition
= new VariantDefinition(
86 CtfTestTraceUtils
.getTrace(testTrace
),
90 StringDeclaration
.getStringDeclaration(Encoding
.UTF8
),
96 IDefinitionScope definitionScope
= new StructDefinition(
100 new Definition
[] { tagDef
, variantDefinition
}
103 return definitionScope
;
107 * Run the VariantDeclaration() constructor test.
110 public void testVariantDeclaration() {
111 assertNotNull(fixture
);
112 assertEquals(false, fixture
.isTagged());
113 String left
= "[declaration] variant[";
114 assertEquals(left
, fixture
.toString().substring(0, left
.length()));
118 * Run the void addField(String,Declaration) method test.
121 public void testAddField() {
124 IDeclaration declaration
= StringDeclaration
.getStringDeclaration(Encoding
.UTF8
);
125 fixture
.addField(tag
, declaration
);
129 * Run the VariantDefinition createDefinition(DefinitionScope,String) method
132 * @throws CTFException
136 public void testCreateDefinition() throws CTFException
{
137 fixture
.setTag("tag");
138 fixture
.addField("a", IntegerDeclaration
.UINT_64B_DECL
);
139 IDefinitionScope definitionScope
= createDefinitionScope();
140 String fieldName
= "";
141 ByteBuffer allocate
= ByteBuffer
.allocate(100);
142 BitBuffer bb
= new BitBuffer(allocate
);
143 VariantDefinition result
= fixture
.createDefinition(definitionScope
, fieldName
, bb
);
145 assertNotNull(result
);
149 * Run the boolean hasField(String) method test.
152 public void testHasField() {
155 boolean result
= fixture
.hasField(tag
);
157 assertEquals(false, result
);
161 * Run the boolean isTagged() method test.
164 public void testIsTagged() {
166 boolean result
= fixture
.isTagged();
168 assertEquals(true, result
);
172 * Run the boolean isTagged() method test.
175 public void testIsTagged_null() {
176 fixture
.setTag((String
) null);
177 boolean result
= fixture
.isTagged();
179 assertEquals(false, result
);
183 * Run the void setTag(String) method test.
186 public void testSetTag() {
193 * Run the String toString() method test.
196 public void testToString() {
198 String result
= fixture
.toString();
199 String left
= "[declaration] variant[";
200 String right
= result
.substring(0, left
.length());
202 assertEquals(left
, right
);
209 public void hashcodeTest() {
210 VariantDeclaration a
= new VariantDeclaration();
211 assertEquals(fixture
.hashCode(), a
.hashCode());
213 VariantDeclaration b
= new VariantDeclaration();
214 b
.addField("hi", StringDeclaration
.getStringDeclaration(Encoding
.UTF8
));
215 VariantDeclaration c
= new VariantDeclaration();
216 c
.addField("hi", StringDeclaration
.getStringDeclaration(Encoding
.UTF8
));
217 assertEquals(b
.hashCode(), c
.hashCode());
224 public void equalsTest() {
225 VariantDeclaration a
= new VariantDeclaration();
226 VariantDeclaration b
= new VariantDeclaration();
227 b
.addField("hi", StringDeclaration
.getStringDeclaration(Encoding
.UTF8
));
228 VariantDeclaration c
= new VariantDeclaration();
229 c
.addField("hi", StringDeclaration
.getStringDeclaration(Encoding
.UTF8
));
230 VariantDeclaration d
= new VariantDeclaration();
231 assertNotEquals(a
, null);
232 assertNotEquals(a
, new Object());
233 assertNotEquals(a
, b
);
234 assertNotEquals(a
, c
);
238 assertNotEquals(b
, a
);
239 assertNotEquals(c
, a
);
243 assertNotEquals(b
, c
);
245 assertNotEquals(b
, c
);
248 b
.addField("hello", IntegerDeclaration
.INT_32B_DECL
);
249 d
.addField("hello", IntegerDeclaration
.INT_32B_DECL
);
250 d
.addField("hi", StringDeclaration
.getStringDeclaration(Encoding
.UTF8
));
257 * Test the equals out of order
260 public void equalsOutOfOrderTest() {
261 VariantDeclaration a
= new VariantDeclaration();
262 VariantDeclaration b
= new VariantDeclaration();
263 b
.addField("hi", StringDeclaration
.getStringDeclaration(Encoding
.UTF8
));
264 b
.addField("hello", new VariantDeclaration());
265 a
.addField("hello", new VariantDeclaration());
266 a
.addField("hi", StringDeclaration
.getStringDeclaration(Encoding
.UTF8
));
271 * Test the equals out of order
274 public void equalsAddTwiceTest() {
275 VariantDeclaration a
= new VariantDeclaration();
276 VariantDeclaration b
= new VariantDeclaration();
277 b
.addField("hi", StringDeclaration
.getStringDeclaration(Encoding
.UTF8
));
278 a
.addField("hi", StringDeclaration
.getStringDeclaration(Encoding
.UTF8
));
280 b
.addField("hi", new VariantDeclaration());
281 assertNotEquals(b
, a
);