btf: Move plugins to the Trace Compass namespace
[deliverable/tracecompass.git] / org.eclipse.linuxtools.ctf.core.tests / src / org / eclipse / linuxtools / ctf / core / tests / types / VariantDeclarationTest.java
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
7 *
8 * Contributors:
9 * Matthew Khouzam - Initial API and implementation
10 *******************************************************************************/
11
12 package org.eclipse.linuxtools.ctf.core.tests.types;
13
14 import static org.junit.Assert.assertEquals;
15 import static org.junit.Assert.assertNotNull;
16 import static org.junit.Assume.assumeTrue;
17
18 import java.nio.ByteBuffer;
19
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;
38
39 import com.google.common.collect.ImmutableList;
40
41 /**
42 * The class <code>VariantDeclarationTest</code> contains tests for the class
43 * <code>{@link VariantDeclaration}</code>.
44 *
45 * @author ematkho
46 * @version $Revision: 1.0 $
47 */
48 public class VariantDeclarationTest {
49
50 private static final CtfTestTrace testTrace = CtfTestTrace.KERNEL;
51
52 private VariantDeclaration fixture;
53
54 /**
55 * Perform pre-test initialization.
56 */
57 @Before
58 public void setUp() {
59 fixture = new VariantDeclaration();
60 }
61
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");
69
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(
76 enumDeclaration,
77 null,
78 "tag",
79 new IntegerDefinition(
80 IntegerDeclaration.UINT_8_DECL,
81 null,
82 "test",
83 0)
84 );
85 VariantDefinition variantDefinition = new VariantDefinition(
86 variantDeclaration,
87 testTrace.getTrace(),
88 "tag",
89 "tag",
90 new StringDefinition(
91 new StringDeclaration(),
92 null,
93 "f",
94 "tag"
95 ));
96
97 IDefinitionScope definitionScope = new StructDefinition(
98 declaration,
99 variantDefinition,
100 "",
101 ImmutableList.of("tag", variantDefinition.getCurrentFieldName()),
102 new Definition[] { tagDef, variantDefinition }
103 );
104
105 return definitionScope;
106 }
107
108 /**
109 * Run the VariantDeclaration() constructor test.
110 */
111 @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()));
117 }
118
119 /**
120 * Run the void addField(String,Declaration) method test.
121 */
122 @Test
123 public void testAddField() {
124 fixture.setTag("");
125 String tag = "";
126 IDeclaration declaration = new StringDeclaration();
127 fixture.addField(tag, declaration);
128 }
129
130 /**
131 * Run the VariantDefinition createDefinition(DefinitionScope,String) method
132 * test.
133 *
134 * @throws CTFReaderException
135 * Should not happen
136 */
137 @Test
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");
146 }
147 BitBuffer bb = new BitBuffer(allocate);
148 VariantDefinition result = fixture.createDefinition(definitionScope, fieldName, bb);
149
150 assertNotNull(result);
151 }
152
153 /**
154 * Run the boolean hasField(String) method test.
155 */
156 @Test
157 public void testHasField() {
158 fixture.setTag("");
159 String tag = "";
160 boolean result = fixture.hasField(tag);
161
162 assertEquals(false, result);
163 }
164
165 /**
166 * Run the boolean isTagged() method test.
167 */
168 @Test
169 public void testIsTagged() {
170 fixture.setTag("");
171 boolean result = fixture.isTagged();
172
173 assertEquals(true, result);
174 }
175
176 /**
177 * Run the boolean isTagged() method test.
178 */
179 @Test
180 public void testIsTagged_null() {
181 fixture.setTag((String) null);
182 boolean result = fixture.isTagged();
183
184 assertEquals(false, result);
185 }
186
187 /**
188 * Run the void setTag(String) method test.
189 */
190 @Test
191 public void testSetTag() {
192 fixture.setTag("");
193 String tag = "";
194 fixture.setTag(tag);
195 }
196
197 /**
198 * Run the String toString() method test.
199 */
200 @Test
201 public void testToString() {
202 fixture.setTag("");
203 String result = fixture.toString();
204 String left = "[declaration] variant[";
205 String right = result.substring(0, left.length());
206
207 assertEquals(left, right);
208 }
209 }
This page took 0.037955 seconds and 5 git commands to generate.