Copyright header update, 2015 edition
[deliverable/tracecompass.git] / org.eclipse.tracecompass.ctf.core.tests / src / org / eclipse / tracecompass / ctf / core / tests / types / VariantDeclarationTest.java
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
7 *
8 * Contributors:
9 * Matthew Khouzam - Initial API and implementation
10 *******************************************************************************/
11
12 package org.eclipse.tracecompass.ctf.core.tests.types;
13
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;
18
19 import java.nio.ByteBuffer;
20
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;
40
41 import com.google.common.collect.ImmutableList;
42
43 /**
44 * The class <code>VariantDeclarationTest</code> contains tests for the class
45 * <code>{@link VariantDeclaration}</code>.
46 *
47 * @author ematkho
48 * @version $Revision: 1.0 $
49 */
50 public class VariantDeclarationTest {
51
52 private static final CtfTestTrace testTrace = CtfTestTrace.KERNEL;
53
54 private VariantDeclaration fixture;
55
56 /**
57 * Perform pre-test initialization.
58 */
59 @Before
60 public void setUp() {
61 fixture = new VariantDeclaration();
62 }
63
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");
71
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(
78 enumDeclaration,
79 null,
80 "tag",
81 new IntegerDefinition(
82 IntegerDeclaration.UINT_8_DECL,
83 null,
84 "test",
85 0)
86 );
87 VariantDefinition variantDefinition = new VariantDefinition(
88 variantDeclaration,
89 testTrace.getTrace(),
90 "tag",
91 "tag",
92 new StringDefinition(
93 StringDeclaration.getStringDeclaration(Encoding.UTF8),
94 null,
95 "f",
96 "tag"
97 ));
98
99 IDefinitionScope definitionScope = new StructDefinition(
100 declaration,
101 variantDefinition,
102 "",
103 ImmutableList.of("tag", variantDefinition.getCurrentFieldName()),
104 new Definition[] { tagDef, variantDefinition }
105 );
106
107 return definitionScope;
108 }
109
110 /**
111 * Run the VariantDeclaration() constructor test.
112 */
113 @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()));
119 }
120
121 /**
122 * Run the void addField(String,Declaration) method test.
123 */
124 @Test
125 public void testAddField() {
126 fixture.setTag("");
127 String tag = "";
128 IDeclaration declaration = StringDeclaration.getStringDeclaration(Encoding.UTF8);
129 fixture.addField(tag, declaration);
130 }
131
132 /**
133 * Run the VariantDefinition createDefinition(DefinitionScope,String) method
134 * test.
135 *
136 * @throws CTFReaderException
137 * Should not happen
138 */
139 @Test
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");
148 }
149 BitBuffer bb = new BitBuffer(allocate);
150 VariantDefinition result = fixture.createDefinition(definitionScope, fieldName, bb);
151
152 assertNotNull(result);
153 }
154
155 /**
156 * Run the boolean hasField(String) method test.
157 */
158 @Test
159 public void testHasField() {
160 fixture.setTag("");
161 String tag = "";
162 boolean result = fixture.hasField(tag);
163
164 assertEquals(false, result);
165 }
166
167 /**
168 * Run the boolean isTagged() method test.
169 */
170 @Test
171 public void testIsTagged() {
172 fixture.setTag("");
173 boolean result = fixture.isTagged();
174
175 assertEquals(true, result);
176 }
177
178 /**
179 * Run the boolean isTagged() method test.
180 */
181 @Test
182 public void testIsTagged_null() {
183 fixture.setTag((String) null);
184 boolean result = fixture.isTagged();
185
186 assertEquals(false, result);
187 }
188
189 /**
190 * Run the void setTag(String) method test.
191 */
192 @Test
193 public void testSetTag() {
194 fixture.setTag("");
195 String tag = "";
196 fixture.setTag(tag);
197 }
198
199 /**
200 * Run the String toString() method test.
201 */
202 @Test
203 public void testToString() {
204 fixture.setTag("");
205 String result = fixture.toString();
206 String left = "[declaration] variant[";
207 String right = result.substring(0, left.length());
208
209 assertEquals(left, right);
210 }
211
212 /**
213 * Test the hashcode
214 */
215 @Test
216 public void hashcodeTest() {
217 assertEquals(923521, fixture.hashCode());
218 assertEquals(fixture.hashCode(), new VariantDeclaration().hashCode());
219 }
220
221 /**
222 * Test the equals
223 */
224 @Test
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);
236 assertEquals(a, d);
237 assertEquals(a, a);
238 assertEquals(b, c);
239 assertNotEquals(b, a);
240 assertNotEquals(c, a);
241 assertEquals(d, a);
242 assertEquals(c, b);
243 b.setTag("hi");
244 assertNotEquals(b, c);
245 c.setTag("Hello");
246 assertNotEquals(b, c);
247 c.setTag("hi");
248 assertEquals(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));
252 d.setTag("hi");
253 assertEquals(b, d);
254 assertEquals(d, b);
255 }
256
257 /**
258 * Test the equals out of order
259 */
260 @Test
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));
268 assertEquals(b, a);
269 }
270
271 /**
272 * Test the equals out of order
273 */
274 @Test
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));
280 assertEquals(b, a);
281 b.addField("hi", new VariantDeclaration());
282 assertNotEquals(b, a);
283 }
284
285 }
This page took 0.037681 seconds and 5 git commands to generate.