Commit | Line | Data |
---|---|---|
4bd7f2db | 1 | /******************************************************************************* |
60ae41e1 | 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 | ||
866e5b51 FC |
12 | package org.eclipse.linuxtools.ctf.core.tests.types; |
13 | ||
419f09a8 | 14 | import static org.junit.Assert.assertEquals; |
866e5b51 FC |
15 | import static org.junit.Assert.assertNotNull; |
16 | import static org.junit.Assert.assertNull; | |
17 | ||
a4fa4e36 | 18 | import java.nio.ByteBuffer; |
866e5b51 | 19 | import java.nio.ByteOrder; |
866e5b51 | 20 | |
a4fa4e36 MK |
21 | import org.eclipse.jdt.annotation.NonNull; |
22 | import org.eclipse.linuxtools.ctf.core.event.io.BitBuffer; | |
23 | import org.eclipse.linuxtools.ctf.core.event.scope.IDefinitionScope; | |
7b4f13e6 | 24 | import org.eclipse.linuxtools.ctf.core.event.types.AbstractArrayDefinition; |
9377d173 | 25 | import org.eclipse.linuxtools.ctf.core.event.types.CompoundDeclaration; |
866e5b51 FC |
26 | import org.eclipse.linuxtools.ctf.core.event.types.Definition; |
27 | import org.eclipse.linuxtools.ctf.core.event.types.Encoding; | |
28 | import org.eclipse.linuxtools.ctf.core.event.types.EnumDeclaration; | |
29 | import org.eclipse.linuxtools.ctf.core.event.types.EnumDefinition; | |
024373d7 | 30 | import org.eclipse.linuxtools.ctf.core.event.types.FloatDeclaration; |
cc98c947 | 31 | import org.eclipse.linuxtools.ctf.core.event.types.IDefinition; |
866e5b51 FC |
32 | import org.eclipse.linuxtools.ctf.core.event.types.IntegerDeclaration; |
33 | import org.eclipse.linuxtools.ctf.core.event.types.IntegerDefinition; | |
024373d7 | 34 | import org.eclipse.linuxtools.ctf.core.event.types.StringDeclaration; |
866e5b51 FC |
35 | import org.eclipse.linuxtools.ctf.core.event.types.StringDefinition; |
36 | import org.eclipse.linuxtools.ctf.core.event.types.StructDeclaration; | |
37 | import org.eclipse.linuxtools.ctf.core.event.types.StructDefinition; | |
38 | import org.eclipse.linuxtools.ctf.core.event.types.VariantDeclaration; | |
39 | import org.eclipse.linuxtools.ctf.core.event.types.VariantDefinition; | |
aefc5c83 | 40 | import org.eclipse.linuxtools.ctf.core.tests.io.Util; |
a4fa4e36 | 41 | import org.eclipse.linuxtools.ctf.core.trace.CTFReaderException; |
7b4f13e6 | 42 | import org.eclipse.linuxtools.internal.ctf.core.event.types.ArrayDeclaration; |
866e5b51 FC |
43 | import org.junit.Before; |
44 | import org.junit.Test; | |
45 | ||
a4fa4e36 MK |
46 | import com.google.common.collect.ImmutableList; |
47 | ||
866e5b51 FC |
48 | /** |
49 | * The class <code>VariantDefinitionTest</code> contains tests for the class | |
50 | * <code>{@link VariantDefinition}</code>. | |
284fdee8 | 51 | * |
866e5b51 FC |
52 | * @author ematkho |
53 | * @version $Revision: 1.0 $ | |
54 | */ | |
55 | public class VariantDefinitionTest { | |
56 | ||
57 | private VariantDefinition fixture; | |
58 | ||
a4fa4e36 | 59 | StructDefinition fStructDefinition; |
163354ef MK |
60 | private static final @NonNull String TEST_STRUCT_ID = "testStruct"; |
61 | private static final @NonNull String ENUM_7 = "g"; | |
62 | private static final @NonNull String ENUM_6 = "f"; | |
63 | private static final @NonNull String ENUM_5 = "e"; | |
64 | private static final @NonNull String ENUM_4 = "d"; | |
65 | private static final @NonNull String ENUM_3 = "c"; | |
66 | private static final @NonNull String ENUM_2 = "b"; | |
67 | private static final @NonNull String ENUM_1 = "a"; | |
68 | private static final @NonNull String TAG_ID = "a"; | |
69 | private static final @NonNull String LENGTH_SEQ = "_len"; | |
70 | private static final @NonNull String VAR_FIELD_NAME = "var"; | |
71 | private static final @NonNull String ENUM_8 = "bbq ribs"; | |
a34d8eee | 72 | |
866e5b51 FC |
73 | /** |
74 | * Perform pre-test initialization. | |
284fdee8 | 75 | * |
866e5b51 | 76 | * Not sure it needs to be that complicated, oh well... |
a4fa4e36 MK |
77 | * |
78 | * @throws CTFReaderException | |
79 | * won't happen | |
866e5b51 FC |
80 | */ |
81 | @Before | |
a4fa4e36 | 82 | public void setUp() throws CTFReaderException { |
024373d7 MK |
83 | StructDeclaration sDec = new StructDeclaration(12); |
84 | StructDeclaration smallStruct = new StructDeclaration(8); | |
a4fa4e36 MK |
85 | IntegerDeclaration iDec = IntegerDeclaration.createDeclaration(32, false, 32, ByteOrder.BIG_ENDIAN, Encoding.NONE, "", 8); |
86 | IntegerDeclaration lenDec = IntegerDeclaration.createDeclaration(8, false, 8, ByteOrder.BIG_ENDIAN, Encoding.NONE, "", 8); | |
024373d7 MK |
87 | StringDeclaration strDec = new StringDeclaration(); |
88 | EnumDeclaration enDec = new EnumDeclaration(iDec); | |
024373d7 MK |
89 | VariantDeclaration varDec = new VariantDeclaration(); |
90 | EnumDeclaration tagDec = new EnumDeclaration(iDec); | |
9377d173 | 91 | CompoundDeclaration arrDec = new ArrayDeclaration(2, iDec); |
07002e0a | 92 | FloatDeclaration fDec = new FloatDeclaration(8, 24, ByteOrder.BIG_ENDIAN, 8); |
024373d7 MK |
93 | tagDec.add(0, 1, ENUM_1); |
94 | tagDec.add(2, 3, ENUM_2); | |
95 | tagDec.add(4, 5, ENUM_3); | |
024373d7 MK |
96 | tagDec.add(8, 9, ENUM_5); |
97 | tagDec.add(10, 11, ENUM_6); | |
98 | tagDec.add(12, 13, ENUM_7); | |
99 | varDec.addField(ENUM_4, lenDec); | |
100 | varDec.addField(ENUM_7, fDec); | |
101 | varDec.addField(ENUM_6, smallStruct); | |
102 | varDec.addField(ENUM_5, enDec); | |
024373d7 MK |
103 | varDec.addField(ENUM_3, arrDec); |
104 | varDec.addField(ENUM_2, iDec); | |
105 | varDec.addField(ENUM_1, strDec); | |
106 | ||
107 | sDec.addField(TAG_ID, tagDec); | |
108 | sDec.addField(LENGTH_SEQ, lenDec); | |
024373d7 MK |
109 | |
110 | sDec.addField(VAR_FIELD_NAME, varDec); | |
111 | varDec.setTag(TAG_ID); | |
112 | ||
aefc5c83 | 113 | final ByteBuffer byteBuffer = Util.testMemory(ByteBuffer.allocate(100)); |
a4fa4e36 MK |
114 | BitBuffer bb = new BitBuffer(byteBuffer); |
115 | byteBuffer.mark(); | |
116 | byteBuffer.putInt(1); | |
117 | byteBuffer.putInt(2); | |
118 | byteBuffer.putInt(3); | |
119 | byteBuffer.reset(); | |
120 | fStructDefinition = sDec.createDefinition(null, TEST_STRUCT_ID, bb); | |
121 | fixture = (VariantDefinition) fStructDefinition.getDefinition(VAR_FIELD_NAME); | |
866e5b51 FC |
122 | } |
123 | ||
866e5b51 FC |
124 | /** |
125 | * Run the VariantDefinition(VariantDeclaration,DefinitionScope,String) | |
a4fa4e36 MK |
126 | * |
127 | * @throws CTFReaderException | |
128 | * should not happen | |
866e5b51 FC |
129 | */ |
130 | @Test | |
a4fa4e36 | 131 | public void testVariantDefinition() throws CTFReaderException { |
866e5b51 | 132 | VariantDeclaration declaration = new VariantDeclaration(); |
4a9c1f07 | 133 | declaration.setTag(""); |
866e5b51 | 134 | VariantDeclaration variantDeclaration = new VariantDeclaration(); |
a4fa4e36 MK |
135 | variantDeclaration.addField("", new EnumDeclaration(IntegerDeclaration.INT_32B_DECL)); |
136 | variantDeclaration.addField("a", IntegerDeclaration.INT_64B_DECL); | |
137 | declaration.addField(ENUM_3, new StringDeclaration()); | |
138 | variantDeclaration.setTag("a"); | |
139 | ||
140 | byte[] bytes = new byte[128]; | |
141 | ByteBuffer byb = ByteBuffer.wrap(bytes); | |
142 | byb.mark(); | |
143 | byb.putInt(0); | |
144 | byb.putShort((short) 2); | |
145 | byb.put(new String("hello").getBytes()); | |
146 | byb.reset(); | |
147 | BitBuffer bb = new BitBuffer(byb); | |
148 | VariantDefinition variantDefinition = variantDeclaration.createDefinition(fStructDefinition, "field", bb); | |
149 | EnumDeclaration declaration2 = new EnumDeclaration(IntegerDeclaration.INT_8_DECL); | |
150 | declaration2.add(0, 2, ENUM_3); | |
151 | EnumDefinition enumDefinition = new EnumDefinition( | |
152 | declaration2, | |
153 | null, | |
154 | "a", | |
155 | new IntegerDefinition( | |
156 | IntegerDeclaration.INT_8_DECL, | |
157 | null, | |
158 | "A", | |
159 | 1 | |
160 | )); | |
866e5b51 | 161 | IDefinitionScope definitionScope = new StructDefinition( |
a4fa4e36 MK |
162 | new StructDeclaration(1L), |
163 | variantDefinition, | |
164 | "", | |
165 | ImmutableList.<String> of("", "variant"), | |
166 | new Definition[] { enumDefinition, variantDefinition } | |
167 | ); | |
4a9c1f07 | 168 | String fieldName = ""; |
a4fa4e36 MK |
169 | declaration.setTag(""); |
170 | VariantDefinition result = declaration.createDefinition(definitionScope, fieldName, bb); | |
866e5b51 FC |
171 | assertNotNull(result); |
172 | } | |
173 | ||
174 | /** | |
175 | * Run the Definition getCurrentField() method test. | |
176 | */ | |
177 | @Test | |
178 | public void testGetCurrentField() { | |
cc98c947 | 179 | IDefinition result = fixture.getCurrentField(); |
024373d7 | 180 | assertNotNull(result); |
866e5b51 FC |
181 | } |
182 | ||
183 | /** | |
184 | * Run the String getCurrentFieldName() method test. | |
185 | */ | |
186 | @Test | |
187 | public void testGetCurrentFieldName() { | |
188 | String result = fixture.getCurrentFieldName(); | |
189 | assertNotNull(result); | |
190 | } | |
191 | ||
192 | /** | |
193 | * Run the VariantDeclaration getDeclaration() method test. | |
194 | */ | |
195 | @Test | |
196 | public void testGetDeclaration() { | |
197 | VariantDeclaration result = fixture.getDeclaration(); | |
198 | assertNotNull(result); | |
199 | } | |
200 | ||
201 | /** | |
202 | * Run the HashMap<String, Definition> getDefinitions() method test. | |
203 | */ | |
204 | @Test | |
205 | public void testGetDefinitions() { | |
cc98c947 | 206 | IDefinition result = fixture.getCurrentField(); |
866e5b51 FC |
207 | assertNotNull(result); |
208 | } | |
209 | ||
210 | /** | |
211 | * Run the String getPath() method test. | |
212 | */ | |
213 | @Test | |
214 | public void testGetPath() { | |
a4fa4e36 | 215 | String result = fixture.getScopePath().toString(); |
866e5b51 FC |
216 | assertNotNull(result); |
217 | } | |
218 | ||
219 | /** | |
220 | * Run the ArrayDefinition lookupArray(String) method test. | |
221 | */ | |
222 | @Test | |
223 | public void testLookupArray() { | |
70f60307 | 224 | AbstractArrayDefinition result = fixture.lookupArrayDefinition(ENUM_3); |
a4fa4e36 | 225 | assertNull(result); |
866e5b51 FC |
226 | } |
227 | ||
228 | /** | |
229 | * Run the Definition lookupDefinition(String) method test. | |
230 | */ | |
231 | @Test | |
232 | public void testLookupDefinition() { | |
cc98c947 | 233 | IDefinition result = fixture.lookupDefinition(ENUM_1); |
024373d7 | 234 | assertNotNull(result); |
a4fa4e36 | 235 | assertEquals("a", ((EnumDefinition) result).getStringValue()); |
866e5b51 FC |
236 | } |
237 | ||
238 | /** | |
239 | * Run the EnumDefinition lookupEnum(String) method test. | |
240 | */ | |
241 | @Test | |
242 | public void testLookupEnum() { | |
024373d7 | 243 | EnumDefinition result = fixture.lookupEnum(ENUM_5); |
a4fa4e36 | 244 | assertNull(result); |
866e5b51 FC |
245 | } |
246 | ||
247 | /** | |
248 | * Run the IntegerDefinition lookupInteger(String) method test. | |
249 | */ | |
250 | @Test | |
251 | public void testLookupInteger() { | |
024373d7 | 252 | IntegerDefinition result = fixture.lookupInteger(ENUM_2); |
a4fa4e36 | 253 | assertNull(result); |
866e5b51 FC |
254 | } |
255 | ||
866e5b51 FC |
256 | /** |
257 | * Run the StringDefinition lookupString(String) method test. | |
258 | */ | |
259 | @Test | |
260 | public void testLookupString() { | |
024373d7 | 261 | StringDefinition result = fixture.lookupString(ENUM_1); |
a4fa4e36 | 262 | assertNull(result); |
866e5b51 FC |
263 | } |
264 | ||
265 | /** | |
266 | * Run the StructDefinition lookupStruct(String) method test. | |
267 | */ | |
268 | @Test | |
269 | public void testLookupStruct() { | |
024373d7 | 270 | StructDefinition result = fixture.lookupStruct(ENUM_6); |
a4fa4e36 | 271 | assertNull(result); |
866e5b51 FC |
272 | } |
273 | ||
274 | /** | |
275 | * Run the VariantDefinition lookupVariant(String) method test. | |
276 | */ | |
277 | @Test | |
278 | public void testLookupVariant() { | |
024373d7 | 279 | VariantDefinition result = fixture.lookupVariant(ENUM_8); |
866e5b51 FC |
280 | assertNull(result); |
281 | } | |
282 | ||
419f09a8 SM |
283 | /** |
284 | * Run the String toString() method test. | |
285 | */ | |
286 | @Test | |
287 | public void testToString() { | |
288 | String result = fixture.toString(); | |
a4fa4e36 | 289 | assertEquals("{ a = \"\" }", result); |
419f09a8 | 290 | } |
866e5b51 | 291 | } |