Commit | Line | Data |
---|---|---|
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 | 12 | package org.eclipse.tracecompass.ctf.core.tests.types; |
866e5b51 FC |
13 | |
14 | import static org.junit.Assert.assertEquals; | |
e00e6663 | 15 | import static org.junit.Assert.assertNotEquals; |
866e5b51 | 16 | import static org.junit.Assert.assertNotNull; |
e5acb357 | 17 | import static org.junit.Assume.assumeTrue; |
866e5b51 | 18 | |
a4fa4e36 MK |
19 | import java.nio.ByteBuffer; |
20 | ||
f357bcd4 AM |
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; | |
d890ec37 | 24 | import org.eclipse.tracecompass.ctf.core.event.types.Encoding; |
f357bcd4 AM |
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; | |
866e5b51 FC |
38 | import org.junit.Before; |
39 | import org.junit.Test; | |
40 | ||
a4fa4e36 MK |
41 | import com.google.common.collect.ImmutableList; |
42 | ||
866e5b51 FC |
43 | /** |
44 | * The class <code>VariantDeclarationTest</code> contains tests for the class | |
45 | * <code>{@link VariantDeclaration}</code>. | |
be6df2d8 | 46 | * |
866e5b51 FC |
47 | * @author ematkho |
48 | * @version $Revision: 1.0 $ | |
49 | */ | |
50 | public class VariantDeclarationTest { | |
51 | ||
9ac63b5b | 52 | private static final CtfTestTrace testTrace = CtfTestTrace.KERNEL; |
32bf80d2 | 53 | |
866e5b51 FC |
54 | private VariantDeclaration fixture; |
55 | ||
866e5b51 FC |
56 | /** |
57 | * Perform pre-test initialization. | |
58 | */ | |
59 | @Before | |
60 | public void setUp() { | |
61 | fixture = new VariantDeclaration(); | |
62 | } | |
63 | ||
a4fa4e36 MK |
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( | |
d890ec37 | 93 | StringDeclaration.getStringDeclaration(Encoding.UTF8), |
a4fa4e36 MK |
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 | ||
866e5b51 FC |
110 | /** |
111 | * Run the VariantDeclaration() constructor test. | |
112 | */ | |
113 | @Test | |
114 | public void testVariantDeclaration() { | |
115 | assertNotNull(fixture); | |
116 | assertEquals(false, fixture.isTagged()); | |
4a9c1f07 | 117 | String left = "[declaration] variant["; |
866e5b51 FC |
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() { | |
4a9c1f07 AM |
126 | fixture.setTag(""); |
127 | String tag = ""; | |
d890ec37 | 128 | IDeclaration declaration = StringDeclaration.getStringDeclaration(Encoding.UTF8); |
866e5b51 FC |
129 | fixture.addField(tag, declaration); |
130 | } | |
131 | ||
132 | /** | |
133 | * Run the VariantDefinition createDefinition(DefinitionScope,String) method | |
134 | * test. | |
be6df2d8 | 135 | * |
a4fa4e36 MK |
136 | * @throws CTFReaderException |
137 | * Should not happen | |
866e5b51 FC |
138 | */ |
139 | @Test | |
13be1a8f | 140 | public void testCreateDefinition() throws CTFReaderException { |
a4fa4e36 MK |
141 | fixture.setTag("tag"); |
142 | fixture.addField("a", IntegerDeclaration.UINT_64B_DECL); | |
866e5b51 | 143 | IDefinitionScope definitionScope = createDefinitionScope(); |
4a9c1f07 | 144 | String fieldName = ""; |
aefc5c83 | 145 | ByteBuffer allocate = ByteBuffer.allocate(100); |
e00e6663 | 146 | if (allocate == null) { |
aefc5c83 MK |
147 | throw new IllegalStateException("Failed to allocate memory"); |
148 | } | |
149 | BitBuffer bb = new BitBuffer(allocate); | |
a4fa4e36 | 150 | VariantDefinition result = fixture.createDefinition(definitionScope, fieldName, bb); |
866e5b51 FC |
151 | |
152 | assertNotNull(result); | |
153 | } | |
154 | ||
866e5b51 FC |
155 | /** |
156 | * Run the boolean hasField(String) method test. | |
157 | */ | |
158 | @Test | |
159 | public void testHasField() { | |
4a9c1f07 AM |
160 | fixture.setTag(""); |
161 | String tag = ""; | |
866e5b51 FC |
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() { | |
4a9c1f07 | 172 | fixture.setTag(""); |
866e5b51 FC |
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() { | |
4a9c1f07 AM |
194 | fixture.setTag(""); |
195 | String tag = ""; | |
866e5b51 FC |
196 | fixture.setTag(tag); |
197 | } | |
198 | ||
199 | /** | |
200 | * Run the String toString() method test. | |
201 | */ | |
202 | @Test | |
203 | public void testToString() { | |
4a9c1f07 | 204 | fixture.setTag(""); |
866e5b51 | 205 | String result = fixture.toString(); |
4a9c1f07 | 206 | String left = "[declaration] variant["; |
866e5b51 FC |
207 | String right = result.substring(0, left.length()); |
208 | ||
209 | assertEquals(left, right); | |
210 | } | |
e00e6663 MK |
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(); | |
d890ec37 | 228 | b.addField("hi", StringDeclaration.getStringDeclaration(Encoding.UTF8)); |
e00e6663 | 229 | VariantDeclaration c = new VariantDeclaration(); |
d890ec37 | 230 | c.addField("hi", StringDeclaration.getStringDeclaration(Encoding.UTF8)); |
e00e6663 MK |
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); | |
d890ec37 | 251 | d.addField("hi", StringDeclaration.getStringDeclaration(Encoding.UTF8)); |
e00e6663 MK |
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(); | |
d890ec37 | 264 | b.addField("hi", StringDeclaration.getStringDeclaration(Encoding.UTF8)); |
e00e6663 MK |
265 | b.addField("hello", new VariantDeclaration()); |
266 | a.addField("hello", new VariantDeclaration()); | |
d890ec37 | 267 | a.addField("hi", StringDeclaration.getStringDeclaration(Encoding.UTF8)); |
e00e6663 MK |
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(); | |
d890ec37 MK |
278 | b.addField("hi", StringDeclaration.getStringDeclaration(Encoding.UTF8)); |
279 | a.addField("hi", StringDeclaration.getStringDeclaration(Encoding.UTF8)); | |
e00e6663 MK |
280 | assertEquals(b, a); |
281 | b.addField("hi", new VariantDeclaration()); | |
282 | assertNotEquals(b, a); | |
283 | } | |
284 | ||
866e5b51 | 285 | } |