common: Annotate some methods in ByteBuffer
[deliverable/tracecompass.git] / ctf / org.eclipse.tracecompass.ctf.core.tests / src / org / eclipse / tracecompass / ctf / core / tests / types / VariantDeclarationTest.java
CommitLineData
4bd7f2db 1/*******************************************************************************
ed902a2b 2 * Copyright (c) 2013, 2014 Ericsson
4bd7f2db
AM
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
f357bcd4 12package org.eclipse.tracecompass.ctf.core.tests.types;
866e5b51
FC
13
14import static org.junit.Assert.assertEquals;
e00e6663 15import static org.junit.Assert.assertNotEquals;
866e5b51
FC
16import static org.junit.Assert.assertNotNull;
17
a4fa4e36
MK
18import java.nio.ByteBuffer;
19
680f9173 20import org.eclipse.tracecompass.ctf.core.CTFException;
f357bcd4
AM
21import org.eclipse.tracecompass.ctf.core.event.io.BitBuffer;
22import org.eclipse.tracecompass.ctf.core.event.scope.IDefinitionScope;
23import org.eclipse.tracecompass.ctf.core.event.types.Definition;
d890ec37 24import org.eclipse.tracecompass.ctf.core.event.types.Encoding;
f357bcd4
AM
25import org.eclipse.tracecompass.ctf.core.event.types.EnumDeclaration;
26import org.eclipse.tracecompass.ctf.core.event.types.EnumDefinition;
27import org.eclipse.tracecompass.ctf.core.event.types.IDeclaration;
28import org.eclipse.tracecompass.ctf.core.event.types.IntegerDeclaration;
29import org.eclipse.tracecompass.ctf.core.event.types.IntegerDefinition;
30import org.eclipse.tracecompass.ctf.core.event.types.StringDeclaration;
31import org.eclipse.tracecompass.ctf.core.event.types.StringDefinition;
32import org.eclipse.tracecompass.ctf.core.event.types.StructDeclaration;
33import org.eclipse.tracecompass.ctf.core.event.types.StructDefinition;
34import org.eclipse.tracecompass.ctf.core.event.types.VariantDeclaration;
35import org.eclipse.tracecompass.ctf.core.event.types.VariantDefinition;
c4d57ac1
AM
36import org.eclipse.tracecompass.ctf.core.tests.shared.CtfTestTraceUtils;
37import org.eclipse.tracecompass.testtraces.ctf.CtfTestTrace;
866e5b51
FC
38import org.junit.Before;
39import org.junit.Test;
40
41/**
42 * The class <code>VariantDeclarationTest</code> contains tests for the class
43 * <code>{@link VariantDeclaration}</code>.
be6df2d8 44 *
866e5b51
FC
45 * @author ematkho
46 * @version $Revision: 1.0 $
47 */
48public class VariantDeclarationTest {
49
9ac63b5b 50 private static final CtfTestTrace testTrace = CtfTestTrace.KERNEL;
32bf80d2 51
866e5b51
FC
52 private VariantDeclaration fixture;
53
866e5b51
FC
54 /**
55 * Perform pre-test initialization.
56 */
57 @Before
58 public void setUp() {
59 fixture = new VariantDeclaration();
60 }
61
680f9173 62 private static IDefinitionScope createDefinitionScope() throws CTFException {
a4fa4e36
MK
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");
68
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(
75 enumDeclaration,
76 null,
77 "tag",
78 new IntegerDefinition(
79 IntegerDeclaration.UINT_8_DECL,
80 null,
81 "test",
82 0)
83 );
84 VariantDefinition variantDefinition = new VariantDefinition(
85 variantDeclaration,
c4d57ac1 86 CtfTestTraceUtils.getTrace(testTrace),
a4fa4e36
MK
87 "tag",
88 "tag",
89 new StringDefinition(
d890ec37 90 StringDeclaration.getStringDeclaration(Encoding.UTF8),
a4fa4e36
MK
91 null,
92 "f",
93 "tag"
94 ));
95
96 IDefinitionScope definitionScope = new StructDefinition(
97 declaration,
98 variantDefinition,
99 "",
a4fa4e36
MK
100 new Definition[] { tagDef, variantDefinition }
101 );
102
103 return definitionScope;
104 }
105
866e5b51
FC
106 /**
107 * Run the VariantDeclaration() constructor test.
108 */
109 @Test
110 public void testVariantDeclaration() {
111 assertNotNull(fixture);
112 assertEquals(false, fixture.isTagged());
4a9c1f07 113 String left = "[declaration] variant[";
866e5b51
FC
114 assertEquals(left, fixture.toString().substring(0, left.length()));
115 }
116
117 /**
118 * Run the void addField(String,Declaration) method test.
119 */
120 @Test
121 public void testAddField() {
4a9c1f07
AM
122 fixture.setTag("");
123 String tag = "";
d890ec37 124 IDeclaration declaration = StringDeclaration.getStringDeclaration(Encoding.UTF8);
866e5b51
FC
125 fixture.addField(tag, declaration);
126 }
127
128 /**
129 * Run the VariantDefinition createDefinition(DefinitionScope,String) method
130 * test.
be6df2d8 131 *
680f9173 132 * @throws CTFException
a4fa4e36 133 * Should not happen
866e5b51
FC
134 */
135 @Test
680f9173 136 public void testCreateDefinition() throws CTFException {
a4fa4e36
MK
137 fixture.setTag("tag");
138 fixture.addField("a", IntegerDeclaration.UINT_64B_DECL);
866e5b51 139 IDefinitionScope definitionScope = createDefinitionScope();
4a9c1f07 140 String fieldName = "";
aefc5c83 141 ByteBuffer allocate = ByteBuffer.allocate(100);
aefc5c83 142 BitBuffer bb = new BitBuffer(allocate);
a4fa4e36 143 VariantDefinition result = fixture.createDefinition(definitionScope, fieldName, bb);
866e5b51
FC
144
145 assertNotNull(result);
146 }
147
866e5b51
FC
148 /**
149 * Run the boolean hasField(String) method test.
150 */
151 @Test
152 public void testHasField() {
4a9c1f07
AM
153 fixture.setTag("");
154 String tag = "";
866e5b51
FC
155 boolean result = fixture.hasField(tag);
156
157 assertEquals(false, result);
158 }
159
160 /**
161 * Run the boolean isTagged() method test.
162 */
163 @Test
164 public void testIsTagged() {
4a9c1f07 165 fixture.setTag("");
866e5b51
FC
166 boolean result = fixture.isTagged();
167
168 assertEquals(true, result);
169 }
170
171 /**
172 * Run the boolean isTagged() method test.
173 */
174 @Test
175 public void testIsTagged_null() {
176 fixture.setTag((String) null);
177 boolean result = fixture.isTagged();
178
179 assertEquals(false, result);
180 }
181
182 /**
183 * Run the void setTag(String) method test.
184 */
185 @Test
186 public void testSetTag() {
4a9c1f07
AM
187 fixture.setTag("");
188 String tag = "";
866e5b51
FC
189 fixture.setTag(tag);
190 }
191
192 /**
193 * Run the String toString() method test.
194 */
195 @Test
196 public void testToString() {
4a9c1f07 197 fixture.setTag("");
866e5b51 198 String result = fixture.toString();
4a9c1f07 199 String left = "[declaration] variant[";
866e5b51
FC
200 String right = result.substring(0, left.length());
201
202 assertEquals(left, right);
203 }
e00e6663
MK
204
205 /**
206 * Test the hashcode
207 */
208 @Test
209 public void hashcodeTest() {
a3fa09b2
MK
210 VariantDeclaration a = new VariantDeclaration();
211 assertEquals(fixture.hashCode(), a.hashCode());
212
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());
e00e6663
MK
218 }
219
220 /**
221 * Test the equals
222 */
223 @Test
224 public void equalsTest() {
225 VariantDeclaration a = new VariantDeclaration();
226 VariantDeclaration b = new VariantDeclaration();
d890ec37 227 b.addField("hi", StringDeclaration.getStringDeclaration(Encoding.UTF8));
e00e6663 228 VariantDeclaration c = new VariantDeclaration();
d890ec37 229 c.addField("hi", StringDeclaration.getStringDeclaration(Encoding.UTF8));
e00e6663
MK
230 VariantDeclaration d = new VariantDeclaration();
231 assertNotEquals(a, null);
232 assertNotEquals(a, new Object());
233 assertNotEquals(a, b);
234 assertNotEquals(a, c);
235 assertEquals(a, d);
236 assertEquals(a, a);
237 assertEquals(b, c);
238 assertNotEquals(b, a);
239 assertNotEquals(c, a);
240 assertEquals(d, a);
241 assertEquals(c, b);
242 b.setTag("hi");
243 assertNotEquals(b, c);
244 c.setTag("Hello");
245 assertNotEquals(b, c);
246 c.setTag("hi");
247 assertEquals(b, c);
248 b.addField("hello", IntegerDeclaration.INT_32B_DECL);
249 d.addField("hello", IntegerDeclaration.INT_32B_DECL);
d890ec37 250 d.addField("hi", StringDeclaration.getStringDeclaration(Encoding.UTF8));
e00e6663
MK
251 d.setTag("hi");
252 assertEquals(b, d);
253 assertEquals(d, b);
254 }
255
256 /**
257 * Test the equals out of order
258 */
259 @Test
260 public void equalsOutOfOrderTest() {
261 VariantDeclaration a = new VariantDeclaration();
262 VariantDeclaration b = new VariantDeclaration();
d890ec37 263 b.addField("hi", StringDeclaration.getStringDeclaration(Encoding.UTF8));
e00e6663
MK
264 b.addField("hello", new VariantDeclaration());
265 a.addField("hello", new VariantDeclaration());
d890ec37 266 a.addField("hi", StringDeclaration.getStringDeclaration(Encoding.UTF8));
e00e6663
MK
267 assertEquals(b, a);
268 }
269
270 /**
271 * Test the equals out of order
272 */
273 @Test
274 public void equalsAddTwiceTest() {
275 VariantDeclaration a = new VariantDeclaration();
276 VariantDeclaration b = new VariantDeclaration();
d890ec37
MK
277 b.addField("hi", StringDeclaration.getStringDeclaration(Encoding.UTF8));
278 a.addField("hi", StringDeclaration.getStringDeclaration(Encoding.UTF8));
e00e6663
MK
279 assertEquals(b, a);
280 b.addField("hi", new VariantDeclaration());
281 assertNotEquals(b, a);
282 }
283
866e5b51 284}
This page took 0.076948 seconds and 5 git commands to generate.