1 /*******************************************************************************
2 * Copyright (c) 2013 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
.Assume
.assumeTrue
;
18 import java
.nio
.ByteBuffer
;
20 import org
.eclipse
.linuxtools
.ctf
.core
.event
.io
.BitBuffer
;
21 import org
.eclipse
.linuxtools
.ctf
.core
.event
.scope
.IDefinitionScope
;
22 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.Definition
;
23 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.EnumDeclaration
;
24 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.EnumDefinition
;
25 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.IDeclaration
;
26 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.IntegerDeclaration
;
27 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.IntegerDefinition
;
28 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.StringDeclaration
;
29 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.StringDefinition
;
30 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.StructDeclaration
;
31 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.StructDefinition
;
32 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.VariantDeclaration
;
33 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.VariantDefinition
;
34 import org
.eclipse
.linuxtools
.ctf
.core
.tests
.shared
.CtfTestTrace
;
35 import org
.eclipse
.linuxtools
.ctf
.core
.trace
.CTFReaderException
;
36 import org
.junit
.Before
;
37 import org
.junit
.Test
;
39 import com
.google
.common
.collect
.ImmutableList
;
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 CTFReaderException
{
63 assumeTrue(testTrace
.exists());
64 StructDeclaration declaration
= new StructDeclaration(8);
65 VariantDeclaration variantDeclaration
= new VariantDeclaration();
66 variantDeclaration
.addField("a", IntegerDeclaration
.INT_32B_DECL
);
67 variantDeclaration
.addField("b", IntegerDeclaration
.INT_32L_DECL
);
68 variantDeclaration
.setTag("a");
70 EnumDeclaration enumDeclaration
= new EnumDeclaration(IntegerDeclaration
.UINT_8_DECL
);
71 enumDeclaration
.add(0, 1, "a");
72 enumDeclaration
.add(2, 2, "b");
73 declaration
.addField("tag", enumDeclaration
);
74 declaration
.addField("variant", variantDeclaration
);
75 EnumDefinition tagDef
= new EnumDefinition(
79 new IntegerDefinition(
80 IntegerDeclaration
.UINT_8_DECL
,
85 VariantDefinition variantDefinition
= new VariantDefinition(
91 new StringDeclaration(),
97 IDefinitionScope definitionScope
= new StructDefinition(
101 ImmutableList
.of("tag", variantDefinition
.getCurrentFieldName()),
102 new Definition
[] { tagDef
, variantDefinition
}
105 return definitionScope
;
109 * Run the VariantDeclaration() constructor test.
112 public void testVariantDeclaration() {
113 assertNotNull(fixture
);
114 assertEquals(false, fixture
.isTagged());
115 String left
= "[declaration] variant[";
116 assertEquals(left
, fixture
.toString().substring(0, left
.length()));
120 * Run the void addField(String,Declaration) method test.
123 public void testAddField() {
126 IDeclaration declaration
= new StringDeclaration();
127 fixture
.addField(tag
, declaration
);
131 * Run the VariantDefinition createDefinition(DefinitionScope,String) method
134 * @throws CTFReaderException
138 public void testCreateDefinition() throws CTFReaderException
{
139 fixture
.setTag("tag");
140 fixture
.addField("a", IntegerDeclaration
.UINT_64B_DECL
);
141 IDefinitionScope definitionScope
= createDefinitionScope();
142 String fieldName
= "";
143 ByteBuffer allocate
= ByteBuffer
.allocate(100);
144 if( allocate
== null){
145 throw new IllegalStateException("Failed to allocate memory");
147 BitBuffer bb
= new BitBuffer(allocate
);
148 VariantDefinition result
= fixture
.createDefinition(definitionScope
, fieldName
, bb
);
150 assertNotNull(result
);
154 * Run the boolean hasField(String) method test.
157 public void testHasField() {
160 boolean result
= fixture
.hasField(tag
);
162 assertEquals(false, result
);
166 * Run the boolean isTagged() method test.
169 public void testIsTagged() {
171 boolean result
= fixture
.isTagged();
173 assertEquals(true, result
);
177 * Run the boolean isTagged() method test.
180 public void testIsTagged_null() {
181 fixture
.setTag((String
) null);
182 boolean result
= fixture
.isTagged();
184 assertEquals(false, result
);
188 * Run the void setTag(String) method test.
191 public void testSetTag() {
198 * Run the String toString() method test.
201 public void testToString() {
203 String result
= fixture
.toString();
204 String left
= "[declaration] variant[";
205 String right
= result
.substring(0, left
.length());
207 assertEquals(left
, right
);