Commit | Line | Data |
---|---|---|
95bf10e7 | 1 | /******************************************************************************* |
61759503 | 2 | * Copyright (c) 2012, 2013 Ericsson |
95bf10e7 AM |
3 | * |
4 | * All rights reserved. This program and the accompanying materials are | |
5 | * made available under the terms of the Eclipse Public License v1.0 which | |
6 | * accompanies this distribution, and is available at | |
7 | * http://www.eclipse.org/legal/epl-v10.html | |
8 | * | |
9 | * Contributors: | |
10 | * Matthew Khouzam - Initial generation with CodePro tools | |
11 | * Alexandre Montplaisir - Clean up, consolidate redundant tests | |
12 | *******************************************************************************/ | |
13 | ||
91e7f946 | 14 | package org.eclipse.linuxtools.tmf.ctf.core.tests; |
81c8e6f7 | 15 | |
cefe3edf | 16 | import static org.junit.Assert.assertArrayEquals; |
4d384009 | 17 | import static org.junit.Assert.assertEquals; |
81c8e6f7 | 18 | |
4d384009 FD |
19 | import java.io.UnsupportedEncodingException; |
20 | import java.nio.ByteBuffer; | |
81c8e6f7 MK |
21 | import java.nio.ByteOrder; |
22 | ||
486efb2e | 23 | import org.eclipse.linuxtools.ctf.core.event.io.BitBuffer; |
81c8e6f7 MK |
24 | import org.eclipse.linuxtools.ctf.core.event.types.ArrayDeclaration; |
25 | import org.eclipse.linuxtools.ctf.core.event.types.Definition; | |
26 | import org.eclipse.linuxtools.ctf.core.event.types.Encoding; | |
404b264a | 27 | import org.eclipse.linuxtools.ctf.core.event.types.EnumDeclaration; |
81c8e6f7 MK |
28 | import org.eclipse.linuxtools.ctf.core.event.types.FloatDeclaration; |
29 | import org.eclipse.linuxtools.ctf.core.event.types.FloatDefinition; | |
30 | import org.eclipse.linuxtools.ctf.core.event.types.IntegerDeclaration; | |
31 | import org.eclipse.linuxtools.ctf.core.event.types.SequenceDeclaration; | |
32 | import org.eclipse.linuxtools.ctf.core.event.types.StringDeclaration; | |
33 | import org.eclipse.linuxtools.ctf.core.event.types.StructDeclaration; | |
34 | import org.eclipse.linuxtools.ctf.core.event.types.StructDefinition; | |
404b264a | 35 | import org.eclipse.linuxtools.ctf.core.event.types.VariantDeclaration; |
db8e8f7d | 36 | import org.eclipse.linuxtools.ctf.core.trace.CTFReaderException; |
91e7f946 | 37 | import org.eclipse.linuxtools.tmf.ctf.core.CtfTmfEventField; |
81c8e6f7 MK |
38 | import org.junit.Before; |
39 | import org.junit.Test; | |
40 | ||
41 | /** | |
42 | * The class <code>CtfTmfEventFieldTest</code> contains tests for the class | |
43 | * <code>{@link CtfTmfEventField}</code>. | |
44 | * | |
81c8e6f7 | 45 | * @author ematkho |
95bf10e7 | 46 | * @version 1.0 |
81c8e6f7 MK |
47 | */ |
48 | public class CtfTmfEventFieldTest { | |
49 | ||
68b18f2f AM |
50 | private static final String ROOT = "root"; |
51 | private static final String SEQ = "seq"; | |
4591bed9 FD |
52 | private static final String ARRAY_STR = "array_str"; |
53 | private static final String ARRAY_FLOAT = "array_float"; | |
54 | private static final String ARRAY_INT = "array_int"; | |
55 | private static final String ARRAY_STRUCT = "array_struct"; | |
56 | private static final String ARRAY_VARIANT = "array_variant"; | |
57 | private static final String ARRAY_ENUM = "array_enum"; | |
68b18f2f AM |
58 | private static final String STR = "str"; |
59 | private static final String FLOAT = "float"; | |
60 | private static final String LEN = "len"; | |
61 | private static final String INT = "int"; | |
62 | private static final String NAME = "test"; | |
7a6cee1a | 63 | private static final String STRUCT = "struct"; |
404b264a GB |
64 | private static final String VARIANT = "variant"; |
65 | private static final String ENUM = "enum"; | |
81c8e6f7 | 66 | |
4d384009 FD |
67 | private static final byte TEST_NUMBER = 2; |
68 | private static final String TEST_STRING = "two"; | |
69 | ||
4591bed9 FD |
70 | private static final int ARRAY_SIZE = 2; |
71 | ||
95bf10e7 AM |
72 | private StructDefinition fixture; |
73 | ||
95bf10e7 AM |
74 | /** |
75 | * Perform pre-test initialization. | |
4d384009 FD |
76 | * |
77 | * @throws UnsupportedEncodingException | |
78 | * Thrown when UTF-8 encoding is not available. | |
79 | * @throws CTFReaderException | |
80 | * error | |
95bf10e7 AM |
81 | */ |
82 | @Before | |
4d384009 FD |
83 | public void setUp() throws UnsupportedEncodingException, CTFReaderException { |
84 | final byte[] testStringBytes = TEST_STRING.getBytes("UTF-8"); | |
85 | ||
86 | int capacity = 2048; | |
87 | ByteBuffer bb = ByteBuffer.allocateDirect(capacity); | |
88 | ||
95bf10e7 AM |
89 | StructDeclaration sDec = new StructDeclaration(1l); |
90 | StringDeclaration strDec = new StringDeclaration(); | |
91 | IntegerDeclaration intDec = new IntegerDeclaration(8, false, 8, | |
92 | ByteOrder.BIG_ENDIAN, Encoding.NONE, null, 8); | |
93 | FloatDeclaration flDec = new FloatDeclaration(8, 24, | |
94 | ByteOrder.BIG_ENDIAN, 8); | |
95bf10e7 | 95 | SequenceDeclaration seqDec = new SequenceDeclaration(LEN, intDec); |
4d384009 | 96 | StructDeclaration structDec = new StructDeclaration(8); |
404b264a GB |
97 | EnumDeclaration enumDec = new EnumDeclaration(intDec); |
98 | VariantDeclaration varDec = new VariantDeclaration(); | |
4591bed9 FD |
99 | ArrayDeclaration arrStrDec = new ArrayDeclaration(ARRAY_SIZE, strDec); |
100 | ArrayDeclaration arrFloatDec = new ArrayDeclaration(ARRAY_SIZE, flDec); | |
101 | ArrayDeclaration arrIntDec = new ArrayDeclaration(ARRAY_SIZE, intDec); | |
102 | ArrayDeclaration arrStructDec = new ArrayDeclaration(ARRAY_SIZE, structDec); | |
103 | ArrayDeclaration arrVariantDec = new ArrayDeclaration(ARRAY_SIZE, varDec); | |
104 | ArrayDeclaration arrEnumDec = new ArrayDeclaration(ARRAY_SIZE, enumDec); | |
4d384009 | 105 | |
95bf10e7 | 106 | sDec.addField(INT, intDec); |
4d384009 FD |
107 | bb.put(TEST_NUMBER); |
108 | ||
4591bed9 FD |
109 | sDec.addField(ARRAY_INT, arrIntDec); |
110 | for (int i = 0; i < ARRAY_SIZE; ++i) { | |
111 | bb.put(TEST_NUMBER); | |
112 | } | |
113 | ||
95bf10e7 | 114 | sDec.addField(LEN, intDec); |
4d384009 FD |
115 | bb.put(TEST_NUMBER); |
116 | ||
95bf10e7 | 117 | sDec.addField(FLOAT, flDec); |
4d384009 FD |
118 | bb.putFloat(TEST_NUMBER); |
119 | ||
4591bed9 FD |
120 | sDec.addField(ARRAY_FLOAT, arrFloatDec); |
121 | for (int i = 0; i < ARRAY_SIZE; ++i) { | |
122 | bb.putFloat(TEST_NUMBER); | |
123 | } | |
124 | ||
95bf10e7 | 125 | sDec.addField(STR, strDec); |
4d384009 FD |
126 | bb.put(testStringBytes); |
127 | bb.put((byte) 0); | |
128 | ||
4591bed9 FD |
129 | sDec.addField(ARRAY_STR, arrStrDec); |
130 | for (int i = 0; i < ARRAY_SIZE; ++i) { | |
131 | bb.put(testStringBytes); | |
132 | bb.put((byte) 0); | |
133 | } | |
4d384009 | 134 | |
95bf10e7 | 135 | sDec.addField(SEQ, seqDec); |
4d384009 FD |
136 | bb.put(TEST_NUMBER); |
137 | bb.put(TEST_NUMBER); | |
138 | ||
459f705b | 139 | structDec.addField(STR, strDec); |
7a6cee1a GB |
140 | structDec.addField(INT, intDec); |
141 | sDec.addField(STRUCT, structDec); | |
4d384009 FD |
142 | bb.put(testStringBytes); |
143 | bb.put((byte) 0); | |
144 | bb.put(TEST_NUMBER); | |
145 | ||
4591bed9 FD |
146 | sDec.addField(ARRAY_STRUCT, arrStructDec); |
147 | for (int i = 0; i < ARRAY_SIZE; ++i) { | |
148 | bb.put(testStringBytes); | |
149 | bb.put((byte) 0); | |
150 | bb.put(TEST_NUMBER); | |
151 | } | |
152 | ||
404b264a GB |
153 | enumDec.add(0, 1, LEN); |
154 | enumDec.add(2, 3, FLOAT); | |
459f705b | 155 | sDec.addField(ENUM, enumDec); |
4d384009 FD |
156 | bb.put(TEST_NUMBER); |
157 | ||
4591bed9 FD |
158 | sDec.addField(ARRAY_ENUM, arrEnumDec); |
159 | for (int i = 0; i < ARRAY_SIZE; ++i) { | |
160 | bb.put(TEST_NUMBER); | |
161 | } | |
162 | ||
404b264a GB |
163 | varDec.addField(LEN, intDec); |
164 | varDec.addField(FLOAT, flDec); | |
165 | varDec.setTag(ENUM); | |
166 | sDec.addField(VARIANT, varDec); | |
4d384009 FD |
167 | bb.putFloat(TEST_NUMBER); |
168 | ||
4591bed9 FD |
169 | sDec.addField(ARRAY_VARIANT, arrVariantDec); |
170 | for (int i = 0; i < ARRAY_SIZE; ++i) { | |
171 | bb.putFloat(TEST_NUMBER); | |
172 | } | |
173 | ||
95bf10e7 | 174 | fixture = sDec.createDefinition(fixture, ROOT); |
4d384009 | 175 | |
95bf10e7 AM |
176 | bb.position(0); |
177 | fixture.read(new BitBuffer(bb)); | |
178 | } | |
179 | ||
95bf10e7 AM |
180 | /** |
181 | * Run the CtfTmfEventField parseField(Definition,String) method test. | |
81c8e6f7 MK |
182 | */ |
183 | @Test | |
95bf10e7 | 184 | public void testParseField_float() { |
68b18f2f AM |
185 | FloatDefinition fieldDef = (FloatDefinition) fixture.lookupDefinition(FLOAT); |
186 | CtfTmfEventField result = CtfTmfEventField.parseField(fieldDef, "_" + NAME); | |
4d384009 | 187 | assertEquals("test=2.0", result.toString()); |
81c8e6f7 MK |
188 | } |
189 | ||
190 | /** | |
4591bed9 FD |
191 | * Run the CtfTmfEventField parseField(Definition,String) method test for an |
192 | * array of floats field. | |
81c8e6f7 MK |
193 | */ |
194 | @Test | |
4591bed9 FD |
195 | public void testParseField_array_float() { |
196 | Definition fieldDef = fixture.lookupArray(ARRAY_FLOAT); | |
68b18f2f | 197 | CtfTmfEventField result = CtfTmfEventField.parseField(fieldDef, NAME); |
4591bed9 | 198 | assertEquals("test=[2.0, 2.0]", result.toString()); |
81c8e6f7 MK |
199 | } |
200 | ||
201 | /** | |
202 | * Run the CtfTmfEventField parseField(Definition,String) method test. | |
81c8e6f7 MK |
203 | */ |
204 | @Test | |
95bf10e7 | 205 | public void testParseField_int() { |
81c8e6f7 MK |
206 | Definition fieldDef = fixture.lookupDefinition(INT); |
207 | CtfTmfEventField result = CtfTmfEventField.parseField(fieldDef, NAME); | |
68b18f2f | 208 | assertEquals("test=02", result.toString()); |
81c8e6f7 MK |
209 | } |
210 | ||
4591bed9 FD |
211 | /** |
212 | * Run the CtfTmfEventField parseField(Definition,String) method test for an | |
213 | * array of integers field. | |
214 | */ | |
215 | @Test | |
216 | public void testParseField_array_int() { | |
217 | Definition fieldDef = fixture.lookupArray(ARRAY_INT); | |
218 | CtfTmfEventField result = CtfTmfEventField.parseField(fieldDef, NAME); | |
219 | assertEquals("test=[02, 02]", result.toString()); | |
220 | } | |
221 | ||
81c8e6f7 MK |
222 | /** |
223 | * Run the CtfTmfEventField parseField(Definition,String) method test. | |
81c8e6f7 MK |
224 | */ |
225 | @Test | |
95bf10e7 | 226 | public void testParseField_sequence() { |
81c8e6f7 MK |
227 | Definition fieldDef = fixture.lookupDefinition(SEQ); |
228 | CtfTmfEventField result = CtfTmfEventField.parseField(fieldDef, NAME); | |
404b264a | 229 | assertEquals("test=[02, 02]", result.toString()); |
a6223d74 MK |
230 | } |
231 | ||
232 | /** | |
233 | * Run the CtfTmfEventField parseField(Definition,String) method test. | |
234 | */ | |
235 | @Test | |
236 | public void testParseField_sequence_value() { | |
237 | Definition fieldDef = fixture.lookupDefinition(SEQ); | |
238 | CtfTmfEventField result = CtfTmfEventField.parseField(fieldDef, NAME); | |
cefe3edf AM |
239 | long[] values = (long[]) result.getValue(); |
240 | long[] expected = new long[] { 2, 2 }; | |
241 | assertArrayEquals(expected, values); | |
81c8e6f7 MK |
242 | } |
243 | ||
244 | /** | |
245 | * Run the CtfTmfEventField parseField(Definition,String) method test. | |
81c8e6f7 MK |
246 | */ |
247 | @Test | |
95bf10e7 | 248 | public void testParseField_string() { |
81c8e6f7 MK |
249 | Definition fieldDef = fixture.lookupDefinition(STR); |
250 | CtfTmfEventField result = CtfTmfEventField.parseField(fieldDef, NAME); | |
4d384009 | 251 | assertEquals("test=two", result.toString()); |
81c8e6f7 | 252 | } |
7a6cee1a | 253 | |
4591bed9 FD |
254 | /** |
255 | * Run the CtfTmfEventField parseField(Definition,String) method test for an | |
256 | * array of strings field. | |
257 | */ | |
258 | @Test | |
259 | public void testParseField_array_string() { | |
260 | Definition fieldDef = fixture.lookupArray(ARRAY_STR); | |
261 | CtfTmfEventField result = CtfTmfEventField.parseField(fieldDef, NAME); | |
262 | assertEquals("test=[two, two]", result.toString()); | |
263 | } | |
264 | ||
7a6cee1a GB |
265 | /** |
266 | * Run the CtfTmfEventField parseField(Definition,String) method test. | |
267 | */ | |
268 | @Test | |
269 | public void testParseField_struct() { | |
270 | Definition fieldDef = fixture.lookupDefinition(STRUCT); | |
271 | CtfTmfEventField result = CtfTmfEventField.parseField(fieldDef, NAME); | |
4d384009 | 272 | assertEquals("test=[str=two, int=02]", result.toString()); |
7a6cee1a | 273 | } |
404b264a | 274 | |
4591bed9 FD |
275 | /** |
276 | * Run the CtfTmfEventField parseField(Definition,String) method test for an | |
277 | * array of structs field. | |
278 | */ | |
279 | @Test | |
280 | public void testParseField_array_struct() { | |
281 | Definition fieldDef = fixture.lookupArray(ARRAY_STRUCT); | |
282 | CtfTmfEventField result = CtfTmfEventField.parseField(fieldDef, NAME); | |
283 | assertEquals("test=[[str=two, int=02], [str=two, int=02]]", result.toString()); | |
284 | } | |
285 | ||
404b264a GB |
286 | /** |
287 | * Run the CtfTmfEventField parseField(Definition,String) method test. | |
288 | */ | |
289 | @Test | |
290 | public void testParseField_enum() { | |
291 | Definition fieldDef = fixture.lookupDefinition(ENUM); | |
292 | CtfTmfEventField result = CtfTmfEventField.parseField(fieldDef, NAME); | |
293 | assertEquals("test=float", result.toString()); | |
294 | } | |
295 | ||
4591bed9 FD |
296 | /** |
297 | * Run the CtfTmfEventField parseField(Definition,String) method test for an | |
298 | * array of enums field. | |
299 | */ | |
300 | @Test | |
301 | public void testParseField_array_enum() { | |
302 | Definition fieldDef = fixture.lookupArray(ARRAY_ENUM); | |
303 | CtfTmfEventField result = CtfTmfEventField.parseField(fieldDef, NAME); | |
304 | assertEquals("test=[float, float]", result.toString()); | |
305 | } | |
306 | ||
404b264a GB |
307 | /** |
308 | * Run the CtfTmfEventField parseField(Definition,String) method test. | |
309 | */ | |
310 | @Test | |
311 | public void testParseField_variant() { | |
312 | Definition fieldDef = fixture.lookupDefinition(VARIANT); | |
313 | CtfTmfEventField result = CtfTmfEventField.parseField(fieldDef, NAME); | |
4d384009 | 314 | assertEquals("test=float=2.0", result.toString()); |
404b264a | 315 | } |
4591bed9 FD |
316 | |
317 | /** | |
318 | * Run the CtfTmfEventField parseField(Definition,String) method test for an | |
319 | * array of variants field. | |
320 | */ | |
321 | @Test | |
322 | public void testParseField_array_variant() { | |
323 | Definition fieldDef = fixture.lookupArray(ARRAY_VARIANT); | |
324 | CtfTmfEventField result = CtfTmfEventField.parseField(fieldDef, NAME); | |
325 | assertEquals("test=[float=2.0, float=2.0]", result.toString()); | |
326 | } | |
95bf10e7 | 327 | } |