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 | ||
2bdf0193 | 14 | package org.eclipse.tracecompass.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 | ||
c1831960 | 23 | import org.eclipse.jdt.annotation.NonNull; |
f357bcd4 AM |
24 | import org.eclipse.tracecompass.ctf.core.event.io.BitBuffer; |
25 | import org.eclipse.tracecompass.ctf.core.event.types.EnumDeclaration; | |
26 | import org.eclipse.tracecompass.ctf.core.event.types.FloatDeclaration; | |
27 | import org.eclipse.tracecompass.ctf.core.event.types.FloatDefinition; | |
28 | import org.eclipse.tracecompass.ctf.core.event.types.IDefinition; | |
29 | import org.eclipse.tracecompass.ctf.core.event.types.IntegerDeclaration; | |
30 | import org.eclipse.tracecompass.ctf.core.event.types.StringDeclaration; | |
31 | import org.eclipse.tracecompass.ctf.core.event.types.StructDeclaration; | |
32 | import org.eclipse.tracecompass.ctf.core.event.types.StructDefinition; | |
33 | import org.eclipse.tracecompass.ctf.core.event.types.VariantDeclaration; | |
34 | import org.eclipse.tracecompass.ctf.core.trace.CTFReaderException; | |
35 | import org.eclipse.tracecompass.internal.ctf.core.event.types.ArrayDeclaration; | |
36 | import org.eclipse.tracecompass.internal.ctf.core.event.types.SequenceDeclaration; | |
2bdf0193 | 37 | import org.eclipse.tracecompass.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 | * | |
a4fa4e36 | 45 | * @author Matthew Khouzam |
95bf10e7 | 46 | * @version 1.0 |
81c8e6f7 MK |
47 | */ |
48 | public class CtfTmfEventFieldTest { | |
49 | ||
c1831960 | 50 | private static final @NonNull String ROOT = "root"; |
68b18f2f | 51 | private static final String SEQ = "seq"; |
166eb6c4 GB |
52 | private static final @NonNull String ARRAY_STR = "array_str"; |
53 | private static final @NonNull String ARRAY_FLOAT = "array_float"; | |
54 | private static final @NonNull String ARRAY_INT = "array_int"; | |
55 | private static final @NonNull String ARRAY_STRUCT = "array_struct"; | |
56 | private static final @NonNull String ARRAY_VARIANT = "array_variant"; | |
57 | private static final @NonNull 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(); | |
a4fa4e36 | 91 | IntegerDeclaration intDec = IntegerDeclaration.UINT_8_DECL; |
95bf10e7 AM |
92 | FloatDeclaration flDec = new FloatDeclaration(8, 24, |
93 | ByteOrder.BIG_ENDIAN, 8); | |
95bf10e7 | 94 | SequenceDeclaration seqDec = new SequenceDeclaration(LEN, intDec); |
4d384009 | 95 | StructDeclaration structDec = new StructDeclaration(8); |
404b264a GB |
96 | EnumDeclaration enumDec = new EnumDeclaration(intDec); |
97 | VariantDeclaration varDec = new VariantDeclaration(); | |
4591bed9 FD |
98 | ArrayDeclaration arrStrDec = new ArrayDeclaration(ARRAY_SIZE, strDec); |
99 | ArrayDeclaration arrFloatDec = new ArrayDeclaration(ARRAY_SIZE, flDec); | |
100 | ArrayDeclaration arrIntDec = new ArrayDeclaration(ARRAY_SIZE, intDec); | |
101 | ArrayDeclaration arrStructDec = new ArrayDeclaration(ARRAY_SIZE, structDec); | |
102 | ArrayDeclaration arrVariantDec = new ArrayDeclaration(ARRAY_SIZE, varDec); | |
103 | ArrayDeclaration arrEnumDec = new ArrayDeclaration(ARRAY_SIZE, enumDec); | |
4d384009 | 104 | |
95bf10e7 | 105 | sDec.addField(INT, intDec); |
4d384009 FD |
106 | bb.put(TEST_NUMBER); |
107 | ||
4591bed9 FD |
108 | sDec.addField(ARRAY_INT, arrIntDec); |
109 | for (int i = 0; i < ARRAY_SIZE; ++i) { | |
110 | bb.put(TEST_NUMBER); | |
111 | } | |
112 | ||
95bf10e7 | 113 | sDec.addField(LEN, intDec); |
4d384009 FD |
114 | bb.put(TEST_NUMBER); |
115 | ||
95bf10e7 | 116 | sDec.addField(FLOAT, flDec); |
4d384009 FD |
117 | bb.putFloat(TEST_NUMBER); |
118 | ||
4591bed9 FD |
119 | sDec.addField(ARRAY_FLOAT, arrFloatDec); |
120 | for (int i = 0; i < ARRAY_SIZE; ++i) { | |
121 | bb.putFloat(TEST_NUMBER); | |
122 | } | |
123 | ||
95bf10e7 | 124 | sDec.addField(STR, strDec); |
4d384009 FD |
125 | bb.put(testStringBytes); |
126 | bb.put((byte) 0); | |
127 | ||
4591bed9 FD |
128 | sDec.addField(ARRAY_STR, arrStrDec); |
129 | for (int i = 0; i < ARRAY_SIZE; ++i) { | |
130 | bb.put(testStringBytes); | |
131 | bb.put((byte) 0); | |
132 | } | |
4d384009 | 133 | |
95bf10e7 | 134 | sDec.addField(SEQ, seqDec); |
4d384009 FD |
135 | bb.put(TEST_NUMBER); |
136 | bb.put(TEST_NUMBER); | |
137 | ||
459f705b | 138 | structDec.addField(STR, strDec); |
7a6cee1a GB |
139 | structDec.addField(INT, intDec); |
140 | sDec.addField(STRUCT, structDec); | |
4d384009 FD |
141 | bb.put(testStringBytes); |
142 | bb.put((byte) 0); | |
143 | bb.put(TEST_NUMBER); | |
144 | ||
4591bed9 FD |
145 | sDec.addField(ARRAY_STRUCT, arrStructDec); |
146 | for (int i = 0; i < ARRAY_SIZE; ++i) { | |
147 | bb.put(testStringBytes); | |
148 | bb.put((byte) 0); | |
149 | bb.put(TEST_NUMBER); | |
150 | } | |
151 | ||
404b264a GB |
152 | enumDec.add(0, 1, LEN); |
153 | enumDec.add(2, 3, FLOAT); | |
459f705b | 154 | sDec.addField(ENUM, enumDec); |
4d384009 FD |
155 | bb.put(TEST_NUMBER); |
156 | ||
4591bed9 FD |
157 | sDec.addField(ARRAY_ENUM, arrEnumDec); |
158 | for (int i = 0; i < ARRAY_SIZE; ++i) { | |
159 | bb.put(TEST_NUMBER); | |
160 | } | |
161 | ||
404b264a GB |
162 | varDec.addField(LEN, intDec); |
163 | varDec.addField(FLOAT, flDec); | |
164 | varDec.setTag(ENUM); | |
165 | sDec.addField(VARIANT, varDec); | |
4d384009 FD |
166 | bb.putFloat(TEST_NUMBER); |
167 | ||
4591bed9 FD |
168 | sDec.addField(ARRAY_VARIANT, arrVariantDec); |
169 | for (int i = 0; i < ARRAY_SIZE; ++i) { | |
170 | bb.putFloat(TEST_NUMBER); | |
171 | } | |
172 | ||
a4fa4e36 | 173 | fixture = sDec.createDefinition(fixture, ROOT, new BitBuffer(bb)); |
4d384009 | 174 | |
95bf10e7 AM |
175 | } |
176 | ||
95bf10e7 AM |
177 | /** |
178 | * Run the CtfTmfEventField parseField(Definition,String) method test. | |
81c8e6f7 MK |
179 | */ |
180 | @Test | |
95bf10e7 | 181 | public void testParseField_float() { |
68b18f2f | 182 | FloatDefinition fieldDef = (FloatDefinition) fixture.lookupDefinition(FLOAT); |
cc98c947 | 183 | CtfTmfEventField result = CtfTmfEventField.parseField((IDefinition)fieldDef, "_" + NAME); |
4d384009 | 184 | assertEquals("test=2.0", result.toString()); |
81c8e6f7 MK |
185 | } |
186 | ||
187 | /** | |
4591bed9 FD |
188 | * Run the CtfTmfEventField parseField(Definition,String) method test for an |
189 | * array of floats field. | |
81c8e6f7 MK |
190 | */ |
191 | @Test | |
4591bed9 | 192 | public void testParseField_array_float() { |
cc98c947 | 193 | IDefinition fieldDef = fixture.lookupArrayDefinition(ARRAY_FLOAT); |
68b18f2f | 194 | CtfTmfEventField result = CtfTmfEventField.parseField(fieldDef, NAME); |
4591bed9 | 195 | assertEquals("test=[2.0, 2.0]", result.toString()); |
81c8e6f7 MK |
196 | } |
197 | ||
198 | /** | |
199 | * Run the CtfTmfEventField parseField(Definition,String) method test. | |
81c8e6f7 MK |
200 | */ |
201 | @Test | |
95bf10e7 | 202 | public void testParseField_int() { |
cc98c947 | 203 | IDefinition fieldDef = fixture.lookupDefinition(INT); |
81c8e6f7 | 204 | CtfTmfEventField result = CtfTmfEventField.parseField(fieldDef, NAME); |
a4fa4e36 | 205 | assertEquals("test=2", result.toString()); |
81c8e6f7 MK |
206 | } |
207 | ||
4591bed9 FD |
208 | /** |
209 | * Run the CtfTmfEventField parseField(Definition,String) method test for an | |
210 | * array of integers field. | |
211 | */ | |
212 | @Test | |
213 | public void testParseField_array_int() { | |
cc98c947 | 214 | IDefinition fieldDef = fixture.lookupArrayDefinition(ARRAY_INT); |
4591bed9 | 215 | CtfTmfEventField result = CtfTmfEventField.parseField(fieldDef, NAME); |
a4fa4e36 | 216 | assertEquals("test=[2, 2]", result.toString()); |
4591bed9 FD |
217 | } |
218 | ||
81c8e6f7 MK |
219 | /** |
220 | * Run the CtfTmfEventField parseField(Definition,String) method test. | |
81c8e6f7 MK |
221 | */ |
222 | @Test | |
95bf10e7 | 223 | public void testParseField_sequence() { |
cc98c947 | 224 | IDefinition fieldDef = fixture.lookupDefinition(SEQ); |
81c8e6f7 | 225 | CtfTmfEventField result = CtfTmfEventField.parseField(fieldDef, NAME); |
a4fa4e36 | 226 | assertEquals("test=[2, 2]", result.toString()); |
a6223d74 MK |
227 | } |
228 | ||
229 | /** | |
230 | * Run the CtfTmfEventField parseField(Definition,String) method test. | |
231 | */ | |
232 | @Test | |
233 | public void testParseField_sequence_value() { | |
cc98c947 | 234 | IDefinition fieldDef = fixture.lookupDefinition(SEQ); |
a6223d74 | 235 | CtfTmfEventField result = CtfTmfEventField.parseField(fieldDef, NAME); |
cefe3edf AM |
236 | long[] values = (long[]) result.getValue(); |
237 | long[] expected = new long[] { 2, 2 }; | |
238 | assertArrayEquals(expected, values); | |
81c8e6f7 MK |
239 | } |
240 | ||
241 | /** | |
242 | * Run the CtfTmfEventField parseField(Definition,String) method test. | |
81c8e6f7 MK |
243 | */ |
244 | @Test | |
95bf10e7 | 245 | public void testParseField_string() { |
cc98c947 | 246 | IDefinition fieldDef = fixture.lookupDefinition(STR); |
81c8e6f7 | 247 | CtfTmfEventField result = CtfTmfEventField.parseField(fieldDef, NAME); |
4d384009 | 248 | assertEquals("test=two", result.toString()); |
81c8e6f7 | 249 | } |
7a6cee1a | 250 | |
4591bed9 FD |
251 | /** |
252 | * Run the CtfTmfEventField parseField(Definition,String) method test for an | |
253 | * array of strings field. | |
254 | */ | |
255 | @Test | |
256 | public void testParseField_array_string() { | |
cc98c947 | 257 | IDefinition fieldDef = fixture.lookupArrayDefinition(ARRAY_STR); |
4591bed9 FD |
258 | CtfTmfEventField result = CtfTmfEventField.parseField(fieldDef, NAME); |
259 | assertEquals("test=[two, two]", result.toString()); | |
260 | } | |
261 | ||
7a6cee1a GB |
262 | /** |
263 | * Run the CtfTmfEventField parseField(Definition,String) method test. | |
264 | */ | |
265 | @Test | |
266 | public void testParseField_struct() { | |
cc98c947 | 267 | IDefinition fieldDef = fixture.lookupDefinition(STRUCT); |
7a6cee1a | 268 | CtfTmfEventField result = CtfTmfEventField.parseField(fieldDef, NAME); |
a4fa4e36 | 269 | assertEquals("test=[str=two, int=2]", result.toString()); |
7a6cee1a | 270 | } |
404b264a | 271 | |
4591bed9 FD |
272 | /** |
273 | * Run the CtfTmfEventField parseField(Definition,String) method test for an | |
274 | * array of structs field. | |
275 | */ | |
276 | @Test | |
277 | public void testParseField_array_struct() { | |
cc98c947 | 278 | IDefinition fieldDef = fixture.lookupArrayDefinition(ARRAY_STRUCT); |
4591bed9 | 279 | CtfTmfEventField result = CtfTmfEventField.parseField(fieldDef, NAME); |
a4fa4e36 | 280 | assertEquals("test=[[str=two, int=2], [str=two, int=2]]", result.toString()); |
4591bed9 FD |
281 | } |
282 | ||
404b264a GB |
283 | /** |
284 | * Run the CtfTmfEventField parseField(Definition,String) method test. | |
285 | */ | |
286 | @Test | |
287 | public void testParseField_enum() { | |
cc98c947 | 288 | IDefinition fieldDef = fixture.lookupDefinition(ENUM); |
404b264a GB |
289 | CtfTmfEventField result = CtfTmfEventField.parseField(fieldDef, NAME); |
290 | assertEquals("test=float", result.toString()); | |
291 | } | |
292 | ||
4591bed9 FD |
293 | /** |
294 | * Run the CtfTmfEventField parseField(Definition,String) method test for an | |
295 | * array of enums field. | |
296 | */ | |
297 | @Test | |
298 | public void testParseField_array_enum() { | |
cc98c947 | 299 | IDefinition fieldDef = fixture.lookupArrayDefinition(ARRAY_ENUM); |
4591bed9 FD |
300 | CtfTmfEventField result = CtfTmfEventField.parseField(fieldDef, NAME); |
301 | assertEquals("test=[float, float]", result.toString()); | |
302 | } | |
303 | ||
404b264a GB |
304 | /** |
305 | * Run the CtfTmfEventField parseField(Definition,String) method test. | |
306 | */ | |
307 | @Test | |
308 | public void testParseField_variant() { | |
cc98c947 | 309 | IDefinition fieldDef = fixture.lookupDefinition(VARIANT); |
404b264a | 310 | CtfTmfEventField result = CtfTmfEventField.parseField(fieldDef, NAME); |
4d384009 | 311 | assertEquals("test=float=2.0", result.toString()); |
404b264a | 312 | } |
4591bed9 FD |
313 | |
314 | /** | |
315 | * Run the CtfTmfEventField parseField(Definition,String) method test for an | |
316 | * array of variants field. | |
317 | */ | |
318 | @Test | |
319 | public void testParseField_array_variant() { | |
cc98c947 | 320 | IDefinition fieldDef = fixture.lookupArrayDefinition(ARRAY_VARIANT); |
4591bed9 FD |
321 | CtfTmfEventField result = CtfTmfEventField.parseField(fieldDef, NAME); |
322 | assertEquals("test=[float=2.0, float=2.0]", result.toString()); | |
323 | } | |
95bf10e7 | 324 | } |