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
;
17 import static org
.junit
.Assume
.assumeTrue
;
19 import java
.nio
.ByteBuffer
;
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
.CtfTestTrace
;
37 import org
.eclipse
.tracecompass
.ctf
.core
.trace
.CTFReaderException
;
38 import org
.junit
.Before
;
39 import org
.junit
.Test
;
41 import com
.google
.common
.collect
.ImmutableList
;
44 * The class <code>VariantDeclarationTest</code> contains tests for the class
45 * <code>{@link VariantDeclaration}</code>.
48 * @version $Revision: 1.0 $
50 public class VariantDeclarationTest
{
52 private static final CtfTestTrace testTrace
= CtfTestTrace
.KERNEL
;
54 private VariantDeclaration fixture
;
57 * Perform pre-test initialization.
61 fixture
= new VariantDeclaration();
64 private static IDefinitionScope
createDefinitionScope() throws CTFReaderException
{
65 assumeTrue(testTrace
.exists());
66 StructDeclaration declaration
= new StructDeclaration(8);
67 VariantDeclaration variantDeclaration
= new VariantDeclaration();
68 variantDeclaration
.addField("a", IntegerDeclaration
.INT_32B_DECL
);
69 variantDeclaration
.addField("b", IntegerDeclaration
.INT_32L_DECL
);
70 variantDeclaration
.setTag("a");
72 EnumDeclaration enumDeclaration
= new EnumDeclaration(IntegerDeclaration
.UINT_8_DECL
);
73 enumDeclaration
.add(0, 1, "a");
74 enumDeclaration
.add(2, 2, "b");
75 declaration
.addField("tag", enumDeclaration
);
76 declaration
.addField("variant", variantDeclaration
);
77 EnumDefinition tagDef
= new EnumDefinition(
81 new IntegerDefinition(
82 IntegerDeclaration
.UINT_8_DECL
,
87 VariantDefinition variantDefinition
= new VariantDefinition(
93 StringDeclaration
.getStringDeclaration(Encoding
.UTF8
),
99 IDefinitionScope definitionScope
= new StructDefinition(
103 ImmutableList
.of("tag", variantDefinition
.getCurrentFieldName()),
104 new Definition
[] { tagDef
, variantDefinition
}
107 return definitionScope
;
111 * Run the VariantDeclaration() constructor test.
114 public void testVariantDeclaration() {
115 assertNotNull(fixture
);
116 assertEquals(false, fixture
.isTagged());
117 String left
= "[declaration] variant[";
118 assertEquals(left
, fixture
.toString().substring(0, left
.length()));
122 * Run the void addField(String,Declaration) method test.
125 public void testAddField() {
128 IDeclaration declaration
= StringDeclaration
.getStringDeclaration(Encoding
.UTF8
);
129 fixture
.addField(tag
, declaration
);
133 * Run the VariantDefinition createDefinition(DefinitionScope,String) method
136 * @throws CTFReaderException
140 public void testCreateDefinition() throws CTFReaderException
{
141 fixture
.setTag("tag");
142 fixture
.addField("a", IntegerDeclaration
.UINT_64B_DECL
);
143 IDefinitionScope definitionScope
= createDefinitionScope();
144 String fieldName
= "";
145 ByteBuffer allocate
= ByteBuffer
.allocate(100);
146 if (allocate
== null) {
147 throw new IllegalStateException("Failed to allocate memory");
149 BitBuffer bb
= new BitBuffer(allocate
);
150 VariantDefinition result
= fixture
.createDefinition(definitionScope
, fieldName
, bb
);
152 assertNotNull(result
);
156 * Run the boolean hasField(String) method test.
159 public void testHasField() {
162 boolean result
= fixture
.hasField(tag
);
164 assertEquals(false, result
);
168 * Run the boolean isTagged() method test.
171 public void testIsTagged() {
173 boolean result
= fixture
.isTagged();
175 assertEquals(true, result
);
179 * Run the boolean isTagged() method test.
182 public void testIsTagged_null() {
183 fixture
.setTag((String
) null);
184 boolean result
= fixture
.isTagged();
186 assertEquals(false, result
);
190 * Run the void setTag(String) method test.
193 public void testSetTag() {
200 * Run the String toString() method test.
203 public void testToString() {
205 String result
= fixture
.toString();
206 String left
= "[declaration] variant[";
207 String right
= result
.substring(0, left
.length());
209 assertEquals(left
, right
);
216 public void hashcodeTest() {
217 assertEquals(923521, fixture
.hashCode());
218 assertEquals(fixture
.hashCode(), new VariantDeclaration().hashCode());
225 public void equalsTest() {
226 VariantDeclaration a
= new VariantDeclaration();
227 VariantDeclaration b
= new VariantDeclaration();
228 b
.addField("hi", StringDeclaration
.getStringDeclaration(Encoding
.UTF8
));
229 VariantDeclaration c
= new VariantDeclaration();
230 c
.addField("hi", StringDeclaration
.getStringDeclaration(Encoding
.UTF8
));
231 VariantDeclaration d
= new VariantDeclaration();
232 assertNotEquals(a
, null);
233 assertNotEquals(a
, new Object());
234 assertNotEquals(a
, b
);
235 assertNotEquals(a
, c
);
239 assertNotEquals(b
, a
);
240 assertNotEquals(c
, a
);
244 assertNotEquals(b
, c
);
246 assertNotEquals(b
, c
);
249 b
.addField("hello", IntegerDeclaration
.INT_32B_DECL
);
250 d
.addField("hello", IntegerDeclaration
.INT_32B_DECL
);
251 d
.addField("hi", StringDeclaration
.getStringDeclaration(Encoding
.UTF8
));
258 * Test the equals out of order
261 public void equalsOutOfOrderTest() {
262 VariantDeclaration a
= new VariantDeclaration();
263 VariantDeclaration b
= new VariantDeclaration();
264 b
.addField("hi", StringDeclaration
.getStringDeclaration(Encoding
.UTF8
));
265 b
.addField("hello", new VariantDeclaration());
266 a
.addField("hello", new VariantDeclaration());
267 a
.addField("hi", StringDeclaration
.getStringDeclaration(Encoding
.UTF8
));
272 * Test the equals out of order
275 public void equalsAddTwiceTest() {
276 VariantDeclaration a
= new VariantDeclaration();
277 VariantDeclaration b
= new VariantDeclaration();
278 b
.addField("hi", StringDeclaration
.getStringDeclaration(Encoding
.UTF8
));
279 a
.addField("hi", StringDeclaration
.getStringDeclaration(Encoding
.UTF8
));
281 b
.addField("hi", new VariantDeclaration());
282 assertNotEquals(b
, a
);