ctf: fix bug 491382. Properly display bytefields
[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";
367e2932 52 private static final @NonNull 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";
367e2932
AM
59 private static final @NonNull String STR = "str";
60 private static final @NonNull String FLOAT = "float";
61 private static final @NonNull String LEN = "len";
62 private static final @NonNull String INT = "int";
fafdd006 63 private static final @NonNull String NAME = "test";
367e2932
AM
64 private static final @NonNull String STRUCT = "struct";
65 private static final @NonNull String VARIANT = "variant";
66 private static final @NonNull String ENUM = "enum";
81c8e6f7 67
4d384009 68 private static final byte TEST_NUMBER = 2;
367e2932 69 private static final @NonNull String TEST_STRING = "two";
4d384009 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);
c9c50ae1
MK
92 IntegerDeclaration byteDec = IntegerDeclaration.UINT_8_DECL;
93 IntegerDeclaration intDec = IntegerDeclaration.INT_8_DECL;
95bf10e7
AM
94 FloatDeclaration flDec = new FloatDeclaration(8, 24,
95 ByteOrder.BIG_ENDIAN, 8);
c9c50ae1 96 SequenceDeclaration seqDec = new SequenceDeclaration(LEN, byteDec);
4d384009 97 StructDeclaration structDec = new StructDeclaration(8);
c9c50ae1 98 EnumDeclaration enumDec = new EnumDeclaration(byteDec);
404b264a 99 VariantDeclaration varDec = new VariantDeclaration();
4591bed9
FD
100 ArrayDeclaration arrStrDec = new ArrayDeclaration(ARRAY_SIZE, strDec);
101 ArrayDeclaration arrFloatDec = new ArrayDeclaration(ARRAY_SIZE, flDec);
102 ArrayDeclaration arrIntDec = new ArrayDeclaration(ARRAY_SIZE, intDec);
103 ArrayDeclaration arrStructDec = new ArrayDeclaration(ARRAY_SIZE, structDec);
104 ArrayDeclaration arrVariantDec = new ArrayDeclaration(ARRAY_SIZE, varDec);
105 ArrayDeclaration arrEnumDec = new ArrayDeclaration(ARRAY_SIZE, enumDec);
4d384009 106
c9c50ae1 107 sDec.addField(INT, byteDec);
4d384009
FD
108 bb.put(TEST_NUMBER);
109
4591bed9
FD
110 sDec.addField(ARRAY_INT, arrIntDec);
111 for (int i = 0; i < ARRAY_SIZE; ++i) {
112 bb.put(TEST_NUMBER);
113 }
114
c9c50ae1 115 sDec.addField(LEN, byteDec);
4d384009
FD
116 bb.put(TEST_NUMBER);
117
95bf10e7 118 sDec.addField(FLOAT, flDec);
4d384009
FD
119 bb.putFloat(TEST_NUMBER);
120
4591bed9
FD
121 sDec.addField(ARRAY_FLOAT, arrFloatDec);
122 for (int i = 0; i < ARRAY_SIZE; ++i) {
123 bb.putFloat(TEST_NUMBER);
124 }
125
95bf10e7 126 sDec.addField(STR, strDec);
4d384009
FD
127 bb.put(testStringBytes);
128 bb.put((byte) 0);
129
4591bed9
FD
130 sDec.addField(ARRAY_STR, arrStrDec);
131 for (int i = 0; i < ARRAY_SIZE; ++i) {
132 bb.put(testStringBytes);
133 bb.put((byte) 0);
134 }
4d384009 135
95bf10e7 136 sDec.addField(SEQ, seqDec);
4d384009
FD
137 bb.put(TEST_NUMBER);
138 bb.put(TEST_NUMBER);
139
459f705b 140 structDec.addField(STR, strDec);
c9c50ae1 141 structDec.addField(INT, byteDec);
7a6cee1a 142 sDec.addField(STRUCT, structDec);
4d384009
FD
143 bb.put(testStringBytes);
144 bb.put((byte) 0);
145 bb.put(TEST_NUMBER);
146
4591bed9
FD
147 sDec.addField(ARRAY_STRUCT, arrStructDec);
148 for (int i = 0; i < ARRAY_SIZE; ++i) {
149 bb.put(testStringBytes);
150 bb.put((byte) 0);
151 bb.put(TEST_NUMBER);
152 }
153
404b264a
GB
154 enumDec.add(0, 1, LEN);
155 enumDec.add(2, 3, FLOAT);
459f705b 156 sDec.addField(ENUM, enumDec);
4d384009
FD
157 bb.put(TEST_NUMBER);
158
4591bed9
FD
159 sDec.addField(ARRAY_ENUM, arrEnumDec);
160 for (int i = 0; i < ARRAY_SIZE; ++i) {
161 bb.put(TEST_NUMBER);
162 }
163
c9c50ae1 164 varDec.addField(LEN, byteDec);
404b264a
GB
165 varDec.addField(FLOAT, flDec);
166 varDec.setTag(ENUM);
167 sDec.addField(VARIANT, varDec);
4d384009
FD
168 bb.putFloat(TEST_NUMBER);
169
4591bed9
FD
170 sDec.addField(ARRAY_VARIANT, arrVariantDec);
171 for (int i = 0; i < ARRAY_SIZE; ++i) {
172 bb.putFloat(TEST_NUMBER);
173 }
174
a4fa4e36 175 fixture = sDec.createDefinition(fixture, ROOT, new BitBuffer(bb));
4d384009 176
95bf10e7
AM
177 }
178
95bf10e7
AM
179 /**
180 * Run the CtfTmfEventField parseField(Definition,String) method test.
81c8e6f7
MK
181 */
182 @Test
95bf10e7 183 public void testParseField_float() {
68b18f2f 184 FloatDefinition fieldDef = (FloatDefinition) fixture.lookupDefinition(FLOAT);
cc98c947 185 CtfTmfEventField result = CtfTmfEventField.parseField((IDefinition)fieldDef, "_" + NAME);
4d384009 186 assertEquals("test=2.0", result.toString());
81c8e6f7
MK
187 }
188
189 /**
4591bed9
FD
190 * Run the CtfTmfEventField parseField(Definition,String) method test for an
191 * array of floats field.
81c8e6f7
MK
192 */
193 @Test
4591bed9 194 public void testParseField_array_float() {
cc98c947 195 IDefinition fieldDef = fixture.lookupArrayDefinition(ARRAY_FLOAT);
68b18f2f 196 CtfTmfEventField result = CtfTmfEventField.parseField(fieldDef, NAME);
4591bed9 197 assertEquals("test=[2.0, 2.0]", result.toString());
81c8e6f7
MK
198 }
199
200 /**
201 * Run the CtfTmfEventField parseField(Definition,String) method test.
81c8e6f7
MK
202 */
203 @Test
95bf10e7 204 public void testParseField_int() {
cc98c947 205 IDefinition fieldDef = fixture.lookupDefinition(INT);
81c8e6f7 206 CtfTmfEventField result = CtfTmfEventField.parseField(fieldDef, NAME);
a4fa4e36 207 assertEquals("test=2", result.toString());
81c8e6f7
MK
208 }
209
4591bed9
FD
210 /**
211 * Run the CtfTmfEventField parseField(Definition,String) method test for an
212 * array of integers field.
213 */
214 @Test
215 public void testParseField_array_int() {
cc98c947 216 IDefinition fieldDef = fixture.lookupArrayDefinition(ARRAY_INT);
4591bed9 217 CtfTmfEventField result = CtfTmfEventField.parseField(fieldDef, NAME);
a4fa4e36 218 assertEquals("test=[2, 2]", result.toString());
4591bed9
FD
219 }
220
81c8e6f7
MK
221 /**
222 * Run the CtfTmfEventField parseField(Definition,String) method test.
81c8e6f7
MK
223 */
224 @Test
95bf10e7 225 public void testParseField_sequence() {
cc98c947 226 IDefinition fieldDef = fixture.lookupDefinition(SEQ);
81c8e6f7 227 CtfTmfEventField result = CtfTmfEventField.parseField(fieldDef, NAME);
a4fa4e36 228 assertEquals("test=[2, 2]", result.toString());
a6223d74
MK
229 }
230
231 /**
232 * Run the CtfTmfEventField parseField(Definition,String) method test.
233 */
234 @Test
235 public void testParseField_sequence_value() {
cc98c947 236 IDefinition fieldDef = fixture.lookupDefinition(SEQ);
a6223d74 237 CtfTmfEventField result = CtfTmfEventField.parseField(fieldDef, NAME);
cefe3edf
AM
238 long[] values = (long[]) result.getValue();
239 long[] expected = new long[] { 2, 2 };
240 assertArrayEquals(expected, values);
81c8e6f7
MK
241 }
242
243 /**
244 * Run the CtfTmfEventField parseField(Definition,String) method test.
81c8e6f7
MK
245 */
246 @Test
95bf10e7 247 public void testParseField_string() {
cc98c947 248 IDefinition fieldDef = fixture.lookupDefinition(STR);
81c8e6f7 249 CtfTmfEventField result = CtfTmfEventField.parseField(fieldDef, NAME);
4d384009 250 assertEquals("test=two", result.toString());
81c8e6f7 251 }
7a6cee1a 252
4591bed9
FD
253 /**
254 * Run the CtfTmfEventField parseField(Definition,String) method test for an
255 * array of strings field.
256 */
257 @Test
258 public void testParseField_array_string() {
cc98c947 259 IDefinition fieldDef = fixture.lookupArrayDefinition(ARRAY_STR);
4591bed9
FD
260 CtfTmfEventField result = CtfTmfEventField.parseField(fieldDef, NAME);
261 assertEquals("test=[two, two]", result.toString());
262 }
263
7a6cee1a
GB
264 /**
265 * Run the CtfTmfEventField parseField(Definition,String) method test.
266 */
267 @Test
268 public void testParseField_struct() {
cc98c947 269 IDefinition fieldDef = fixture.lookupDefinition(STRUCT);
7a6cee1a 270 CtfTmfEventField result = CtfTmfEventField.parseField(fieldDef, NAME);
a4fa4e36 271 assertEquals("test=[str=two, int=2]", result.toString());
7a6cee1a 272 }
404b264a 273
4591bed9
FD
274 /**
275 * Run the CtfTmfEventField parseField(Definition,String) method test for an
276 * array of structs field.
277 */
278 @Test
279 public void testParseField_array_struct() {
cc98c947 280 IDefinition fieldDef = fixture.lookupArrayDefinition(ARRAY_STRUCT);
4591bed9 281 CtfTmfEventField result = CtfTmfEventField.parseField(fieldDef, NAME);
a4fa4e36 282 assertEquals("test=[[str=two, int=2], [str=two, int=2]]", result.toString());
4591bed9
FD
283 }
284
404b264a
GB
285 /**
286 * Run the CtfTmfEventField parseField(Definition,String) method test.
287 */
288 @Test
289 public void testParseField_enum() {
cc98c947 290 IDefinition fieldDef = fixture.lookupDefinition(ENUM);
404b264a
GB
291 CtfTmfEventField result = CtfTmfEventField.parseField(fieldDef, NAME);
292 assertEquals("test=float", result.toString());
293 }
294
4591bed9
FD
295 /**
296 * Run the CtfTmfEventField parseField(Definition,String) method test for an
297 * array of enums field.
298 */
299 @Test
300 public void testParseField_array_enum() {
cc98c947 301 IDefinition fieldDef = fixture.lookupArrayDefinition(ARRAY_ENUM);
4591bed9
FD
302 CtfTmfEventField result = CtfTmfEventField.parseField(fieldDef, NAME);
303 assertEquals("test=[float, float]", result.toString());
304 }
305
404b264a
GB
306 /**
307 * Run the CtfTmfEventField parseField(Definition,String) method test.
308 */
309 @Test
310 public void testParseField_variant() {
cc98c947 311 IDefinition fieldDef = fixture.lookupDefinition(VARIANT);
404b264a 312 CtfTmfEventField result = CtfTmfEventField.parseField(fieldDef, NAME);
4d384009 313 assertEquals("test=float=2.0", result.toString());
404b264a 314 }
4591bed9
FD
315
316 /**
317 * Run the CtfTmfEventField parseField(Definition,String) method test for an
318 * array of variants field.
319 */
320 @Test
321 public void testParseField_array_variant() {
cc98c947 322 IDefinition fieldDef = fixture.lookupArrayDefinition(ARRAY_VARIANT);
4591bed9
FD
323 CtfTmfEventField result = CtfTmfEventField.parseField(fieldDef, NAME);
324 assertEquals("test=[float=2.0, float=2.0]", result.toString());
325 }
95bf10e7 326}
This page took 0.10435 seconds and 5 git commands to generate.