tmf: Fix flaky ImportAndReadSmokeTest test
[deliverable/tracecompass.git] / ctf / org.eclipse.tracecompass.tmf.ctf.core.tests / src / org / eclipse / tracecompass / tmf / ctf / core / tests / event / CtfTmfEventFieldTest.java
CommitLineData
95bf10e7 1/*******************************************************************************
ed902a2b 2 * Copyright (c) 2012, 2014 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
9722e5d7 14package org.eclipse.tracecompass.tmf.ctf.core.tests.event;
81c8e6f7 15
cefe3edf 16import static org.junit.Assert.assertArrayEquals;
4d384009 17import static org.junit.Assert.assertEquals;
81c8e6f7 18
4d384009
FD
19import java.io.UnsupportedEncodingException;
20import java.nio.ByteBuffer;
81c8e6f7
MK
21import java.nio.ByteOrder;
22
c1831960 23import org.eclipse.jdt.annotation.NonNull;
680f9173 24import org.eclipse.tracecompass.ctf.core.CTFException;
f357bcd4 25import org.eclipse.tracecompass.ctf.core.event.io.BitBuffer;
d890ec37 26import org.eclipse.tracecompass.ctf.core.event.types.Encoding;
f357bcd4
AM
27import org.eclipse.tracecompass.ctf.core.event.types.EnumDeclaration;
28import org.eclipse.tracecompass.ctf.core.event.types.FloatDeclaration;
29import org.eclipse.tracecompass.ctf.core.event.types.FloatDefinition;
30import org.eclipse.tracecompass.ctf.core.event.types.IDefinition;
31import org.eclipse.tracecompass.ctf.core.event.types.IntegerDeclaration;
32import org.eclipse.tracecompass.ctf.core.event.types.StringDeclaration;
33import org.eclipse.tracecompass.ctf.core.event.types.StructDeclaration;
34import org.eclipse.tracecompass.ctf.core.event.types.StructDefinition;
35import org.eclipse.tracecompass.ctf.core.event.types.VariantDeclaration;
f357bcd4
AM
36import org.eclipse.tracecompass.internal.ctf.core.event.types.ArrayDeclaration;
37import org.eclipse.tracecompass.internal.ctf.core.event.types.SequenceDeclaration;
9722e5d7 38import org.eclipse.tracecompass.tmf.ctf.core.event.CtfTmfEventField;
81c8e6f7
MK
39import org.junit.Before;
40import org.junit.Test;
41
42/**
43 * The class <code>CtfTmfEventFieldTest</code> contains tests for the class
44 * <code>{@link CtfTmfEventField}</code>.
45 *
a4fa4e36 46 * @author Matthew Khouzam
95bf10e7 47 * @version 1.0
81c8e6f7
MK
48 */
49public class CtfTmfEventFieldTest {
50
c1831960 51 private static final @NonNull String ROOT = "root";
68b18f2f 52 private static final String SEQ = "seq";
166eb6c4
GB
53 private static final @NonNull String ARRAY_STR = "array_str";
54 private static final @NonNull String ARRAY_FLOAT = "array_float";
55 private static final @NonNull String ARRAY_INT = "array_int";
56 private static final @NonNull String ARRAY_STRUCT = "array_struct";
57 private static final @NonNull String ARRAY_VARIANT = "array_variant";
58 private static final @NonNull String ARRAY_ENUM = "array_enum";
68b18f2f
AM
59 private static final String STR = "str";
60 private static final String FLOAT = "float";
61 private static final String LEN = "len";
62 private static final String INT = "int";
63 private static final String NAME = "test";
7a6cee1a 64 private static final String STRUCT = "struct";
404b264a
GB
65 private static final String VARIANT = "variant";
66 private static final String ENUM = "enum";
81c8e6f7 67
4d384009
FD
68 private static final byte TEST_NUMBER = 2;
69 private static final String TEST_STRING = "two";
70
4591bed9
FD
71 private static final int ARRAY_SIZE = 2;
72
95bf10e7
AM
73 private StructDefinition fixture;
74
95bf10e7
AM
75 /**
76 * Perform pre-test initialization.
4d384009
FD
77 *
78 * @throws UnsupportedEncodingException
79 * Thrown when UTF-8 encoding is not available.
680f9173 80 * @throws CTFException
4d384009 81 * error
95bf10e7
AM
82 */
83 @Before
680f9173 84 public void setUp() throws UnsupportedEncodingException, CTFException {
4d384009
FD
85 final byte[] testStringBytes = TEST_STRING.getBytes("UTF-8");
86
87 int capacity = 2048;
88 ByteBuffer bb = ByteBuffer.allocateDirect(capacity);
89
95bf10e7 90 StructDeclaration sDec = new StructDeclaration(1l);
d890ec37 91 StringDeclaration strDec = StringDeclaration.getStringDeclaration(Encoding.UTF8);
a4fa4e36 92 IntegerDeclaration intDec = IntegerDeclaration.UINT_8_DECL;
95bf10e7
AM
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
a4fa4e36 174 fixture = sDec.createDefinition(fixture, ROOT, new BitBuffer(bb));
4d384009 175
95bf10e7
AM
176 }
177
95bf10e7
AM
178 /**
179 * Run the CtfTmfEventField parseField(Definition,String) method test.
81c8e6f7
MK
180 */
181 @Test
95bf10e7 182 public void testParseField_float() {
68b18f2f 183 FloatDefinition fieldDef = (FloatDefinition) fixture.lookupDefinition(FLOAT);
cc98c947 184 CtfTmfEventField result = CtfTmfEventField.parseField((IDefinition)fieldDef, "_" + NAME);
4d384009 185 assertEquals("test=2.0", result.toString());
81c8e6f7
MK
186 }
187
188 /**
4591bed9
FD
189 * Run the CtfTmfEventField parseField(Definition,String) method test for an
190 * array of floats field.
81c8e6f7
MK
191 */
192 @Test
4591bed9 193 public void testParseField_array_float() {
cc98c947 194 IDefinition fieldDef = fixture.lookupArrayDefinition(ARRAY_FLOAT);
68b18f2f 195 CtfTmfEventField result = CtfTmfEventField.parseField(fieldDef, NAME);
4591bed9 196 assertEquals("test=[2.0, 2.0]", result.toString());
81c8e6f7
MK
197 }
198
199 /**
200 * Run the CtfTmfEventField parseField(Definition,String) method test.
81c8e6f7
MK
201 */
202 @Test
95bf10e7 203 public void testParseField_int() {
cc98c947 204 IDefinition fieldDef = fixture.lookupDefinition(INT);
81c8e6f7 205 CtfTmfEventField result = CtfTmfEventField.parseField(fieldDef, NAME);
a4fa4e36 206 assertEquals("test=2", result.toString());
81c8e6f7
MK
207 }
208
4591bed9
FD
209 /**
210 * Run the CtfTmfEventField parseField(Definition,String) method test for an
211 * array of integers field.
212 */
213 @Test
214 public void testParseField_array_int() {
cc98c947 215 IDefinition fieldDef = fixture.lookupArrayDefinition(ARRAY_INT);
4591bed9 216 CtfTmfEventField result = CtfTmfEventField.parseField(fieldDef, NAME);
a4fa4e36 217 assertEquals("test=[2, 2]", result.toString());
4591bed9
FD
218 }
219
81c8e6f7
MK
220 /**
221 * Run the CtfTmfEventField parseField(Definition,String) method test.
81c8e6f7
MK
222 */
223 @Test
95bf10e7 224 public void testParseField_sequence() {
cc98c947 225 IDefinition fieldDef = fixture.lookupDefinition(SEQ);
81c8e6f7 226 CtfTmfEventField result = CtfTmfEventField.parseField(fieldDef, NAME);
a4fa4e36 227 assertEquals("test=[2, 2]", result.toString());
a6223d74
MK
228 }
229
230 /**
231 * Run the CtfTmfEventField parseField(Definition,String) method test.
232 */
233 @Test
234 public void testParseField_sequence_value() {
cc98c947 235 IDefinition fieldDef = fixture.lookupDefinition(SEQ);
a6223d74 236 CtfTmfEventField result = CtfTmfEventField.parseField(fieldDef, NAME);
cefe3edf
AM
237 long[] values = (long[]) result.getValue();
238 long[] expected = new long[] { 2, 2 };
239 assertArrayEquals(expected, values);
81c8e6f7
MK
240 }
241
242 /**
243 * Run the CtfTmfEventField parseField(Definition,String) method test.
81c8e6f7
MK
244 */
245 @Test
95bf10e7 246 public void testParseField_string() {
cc98c947 247 IDefinition fieldDef = fixture.lookupDefinition(STR);
81c8e6f7 248 CtfTmfEventField result = CtfTmfEventField.parseField(fieldDef, NAME);
4d384009 249 assertEquals("test=two", result.toString());
81c8e6f7 250 }
7a6cee1a 251
4591bed9
FD
252 /**
253 * Run the CtfTmfEventField parseField(Definition,String) method test for an
254 * array of strings field.
255 */
256 @Test
257 public void testParseField_array_string() {
cc98c947 258 IDefinition fieldDef = fixture.lookupArrayDefinition(ARRAY_STR);
4591bed9
FD
259 CtfTmfEventField result = CtfTmfEventField.parseField(fieldDef, NAME);
260 assertEquals("test=[two, two]", result.toString());
261 }
262
7a6cee1a
GB
263 /**
264 * Run the CtfTmfEventField parseField(Definition,String) method test.
265 */
266 @Test
267 public void testParseField_struct() {
cc98c947 268 IDefinition fieldDef = fixture.lookupDefinition(STRUCT);
7a6cee1a 269 CtfTmfEventField result = CtfTmfEventField.parseField(fieldDef, NAME);
a4fa4e36 270 assertEquals("test=[str=two, int=2]", result.toString());
7a6cee1a 271 }
404b264a 272
4591bed9
FD
273 /**
274 * Run the CtfTmfEventField parseField(Definition,String) method test for an
275 * array of structs field.
276 */
277 @Test
278 public void testParseField_array_struct() {
cc98c947 279 IDefinition fieldDef = fixture.lookupArrayDefinition(ARRAY_STRUCT);
4591bed9 280 CtfTmfEventField result = CtfTmfEventField.parseField(fieldDef, NAME);
a4fa4e36 281 assertEquals("test=[[str=two, int=2], [str=two, int=2]]", result.toString());
4591bed9
FD
282 }
283
404b264a
GB
284 /**
285 * Run the CtfTmfEventField parseField(Definition,String) method test.
286 */
287 @Test
288 public void testParseField_enum() {
cc98c947 289 IDefinition fieldDef = fixture.lookupDefinition(ENUM);
404b264a
GB
290 CtfTmfEventField result = CtfTmfEventField.parseField(fieldDef, NAME);
291 assertEquals("test=float", result.toString());
292 }
293
4591bed9
FD
294 /**
295 * Run the CtfTmfEventField parseField(Definition,String) method test for an
296 * array of enums field.
297 */
298 @Test
299 public void testParseField_array_enum() {
cc98c947 300 IDefinition fieldDef = fixture.lookupArrayDefinition(ARRAY_ENUM);
4591bed9
FD
301 CtfTmfEventField result = CtfTmfEventField.parseField(fieldDef, NAME);
302 assertEquals("test=[float, float]", result.toString());
303 }
304
404b264a
GB
305 /**
306 * Run the CtfTmfEventField parseField(Definition,String) method test.
307 */
308 @Test
309 public void testParseField_variant() {
cc98c947 310 IDefinition fieldDef = fixture.lookupDefinition(VARIANT);
404b264a 311 CtfTmfEventField result = CtfTmfEventField.parseField(fieldDef, NAME);
4d384009 312 assertEquals("test=float=2.0", result.toString());
404b264a 313 }
4591bed9
FD
314
315 /**
316 * Run the CtfTmfEventField parseField(Definition,String) method test for an
317 * array of variants field.
318 */
319 @Test
320 public void testParseField_array_variant() {
cc98c947 321 IDefinition fieldDef = fixture.lookupArrayDefinition(ARRAY_VARIANT);
4591bed9
FD
322 CtfTmfEventField result = CtfTmfEventField.parseField(fieldDef, NAME);
323 assertEquals("test=[float=2.0, float=2.0]", result.toString());
324 }
95bf10e7 325}
This page took 0.109139 seconds and 5 git commands to generate.