ctf: Depend on the tracecompass-test-traces project
[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);
e00e6663 142 if (allocate == null) {
aefc5c83
MK
143 throw new IllegalStateException("Failed to allocate memory");
144 }
145 BitBuffer bb = new BitBuffer(allocate);
a4fa4e36 146 VariantDefinition result = fixture.createDefinition(definitionScope, fieldName, bb);
866e5b51
FC
147
148 assertNotNull(result);
149 }
150
866e5b51
FC
151 /**
152 * Run the boolean hasField(String) method test.
153 */
154 @Test
155 public void testHasField() {
4a9c1f07
AM
156 fixture.setTag("");
157 String tag = "";
866e5b51
FC
158 boolean result = fixture.hasField(tag);
159
160 assertEquals(false, result);
161 }
162
163 /**
164 * Run the boolean isTagged() method test.
165 */
166 @Test
167 public void testIsTagged() {
4a9c1f07 168 fixture.setTag("");
866e5b51
FC
169 boolean result = fixture.isTagged();
170
171 assertEquals(true, result);
172 }
173
174 /**
175 * Run the boolean isTagged() method test.
176 */
177 @Test
178 public void testIsTagged_null() {
179 fixture.setTag((String) null);
180 boolean result = fixture.isTagged();
181
182 assertEquals(false, result);
183 }
184
185 /**
186 * Run the void setTag(String) method test.
187 */
188 @Test
189 public void testSetTag() {
4a9c1f07
AM
190 fixture.setTag("");
191 String tag = "";
866e5b51
FC
192 fixture.setTag(tag);
193 }
194
195 /**
196 * Run the String toString() method test.
197 */
198 @Test
199 public void testToString() {
4a9c1f07 200 fixture.setTag("");
866e5b51 201 String result = fixture.toString();
4a9c1f07 202 String left = "[declaration] variant[";
866e5b51
FC
203 String right = result.substring(0, left.length());
204
205 assertEquals(left, right);
206 }
e00e6663
MK
207
208 /**
209 * Test the hashcode
210 */
211 @Test
212 public void hashcodeTest() {
a3fa09b2
MK
213 VariantDeclaration a = new VariantDeclaration();
214 assertEquals(fixture.hashCode(), a.hashCode());
215
216 VariantDeclaration b = new VariantDeclaration();
217 b.addField("hi", StringDeclaration.getStringDeclaration(Encoding.UTF8));
218 VariantDeclaration c = new VariantDeclaration();
219 c.addField("hi", StringDeclaration.getStringDeclaration(Encoding.UTF8));
220 assertEquals(b.hashCode(), c.hashCode());
e00e6663
MK
221 }
222
223 /**
224 * Test the equals
225 */
226 @Test
227 public void equalsTest() {
228 VariantDeclaration a = new VariantDeclaration();
229 VariantDeclaration b = new VariantDeclaration();
d890ec37 230 b.addField("hi", StringDeclaration.getStringDeclaration(Encoding.UTF8));
e00e6663 231 VariantDeclaration c = new VariantDeclaration();
d890ec37 232 c.addField("hi", StringDeclaration.getStringDeclaration(Encoding.UTF8));
e00e6663
MK
233 VariantDeclaration d = new VariantDeclaration();
234 assertNotEquals(a, null);
235 assertNotEquals(a, new Object());
236 assertNotEquals(a, b);
237 assertNotEquals(a, c);
238 assertEquals(a, d);
239 assertEquals(a, a);
240 assertEquals(b, c);
241 assertNotEquals(b, a);
242 assertNotEquals(c, a);
243 assertEquals(d, a);
244 assertEquals(c, b);
245 b.setTag("hi");
246 assertNotEquals(b, c);
247 c.setTag("Hello");
248 assertNotEquals(b, c);
249 c.setTag("hi");
250 assertEquals(b, c);
251 b.addField("hello", IntegerDeclaration.INT_32B_DECL);
252 d.addField("hello", IntegerDeclaration.INT_32B_DECL);
d890ec37 253 d.addField("hi", StringDeclaration.getStringDeclaration(Encoding.UTF8));
e00e6663
MK
254 d.setTag("hi");
255 assertEquals(b, d);
256 assertEquals(d, b);
257 }
258
259 /**
260 * Test the equals out of order
261 */
262 @Test
263 public void equalsOutOfOrderTest() {
264 VariantDeclaration a = new VariantDeclaration();
265 VariantDeclaration b = new VariantDeclaration();
d890ec37 266 b.addField("hi", StringDeclaration.getStringDeclaration(Encoding.UTF8));
e00e6663
MK
267 b.addField("hello", new VariantDeclaration());
268 a.addField("hello", new VariantDeclaration());
d890ec37 269 a.addField("hi", StringDeclaration.getStringDeclaration(Encoding.UTF8));
e00e6663
MK
270 assertEquals(b, a);
271 }
272
273 /**
274 * Test the equals out of order
275 */
276 @Test
277 public void equalsAddTwiceTest() {
278 VariantDeclaration a = new VariantDeclaration();
279 VariantDeclaration b = new VariantDeclaration();
d890ec37
MK
280 b.addField("hi", StringDeclaration.getStringDeclaration(Encoding.UTF8));
281 a.addField("hi", StringDeclaration.getStringDeclaration(Encoding.UTF8));
e00e6663
MK
282 assertEquals(b, a);
283 b.addField("hi", new VariantDeclaration());
284 assertNotEquals(b, a);
285 }
286
866e5b51 287}
This page took 0.133695 seconds and 5 git commands to generate.