1 /*******************************************************************************
2 * Copyright (c) 2012, 2014 Ericsson
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
10 * Matthew Khouzam - Initial generation with CodePro tools
11 * Alexandre Montplaisir - Clean up, consolidate redundant tests
12 *******************************************************************************/
14 package org
.eclipse
.tracecompass
.tmf
.ctf
.core
.tests
.event
;
16 import static org
.junit
.Assert
.assertArrayEquals
;
17 import static org
.junit
.Assert
.assertEquals
;
19 import java
.io
.UnsupportedEncodingException
;
20 import java
.nio
.ByteBuffer
;
21 import java
.nio
.ByteOrder
;
23 import org
.eclipse
.jdt
.annotation
.NonNull
;
24 import org
.eclipse
.tracecompass
.ctf
.core
.CTFException
;
25 import org
.eclipse
.tracecompass
.ctf
.core
.event
.io
.BitBuffer
;
26 import org
.eclipse
.tracecompass
.ctf
.core
.event
.types
.Encoding
;
27 import org
.eclipse
.tracecompass
.ctf
.core
.event
.types
.EnumDeclaration
;
28 import org
.eclipse
.tracecompass
.ctf
.core
.event
.types
.FloatDeclaration
;
29 import org
.eclipse
.tracecompass
.ctf
.core
.event
.types
.FloatDefinition
;
30 import org
.eclipse
.tracecompass
.ctf
.core
.event
.types
.IDefinition
;
31 import org
.eclipse
.tracecompass
.ctf
.core
.event
.types
.IntegerDeclaration
;
32 import org
.eclipse
.tracecompass
.ctf
.core
.event
.types
.StringDeclaration
;
33 import org
.eclipse
.tracecompass
.ctf
.core
.event
.types
.StructDeclaration
;
34 import org
.eclipse
.tracecompass
.ctf
.core
.event
.types
.StructDefinition
;
35 import org
.eclipse
.tracecompass
.ctf
.core
.event
.types
.VariantDeclaration
;
36 import org
.eclipse
.tracecompass
.internal
.ctf
.core
.event
.types
.ArrayDeclaration
;
37 import org
.eclipse
.tracecompass
.internal
.ctf
.core
.event
.types
.SequenceDeclaration
;
38 import org
.eclipse
.tracecompass
.tmf
.ctf
.core
.event
.CtfTmfEventField
;
39 import org
.junit
.Before
;
40 import org
.junit
.Test
;
43 * The class <code>CtfTmfEventFieldTest</code> contains tests for the class
44 * <code>{@link CtfTmfEventField}</code>.
46 * @author Matthew Khouzam
49 public class CtfTmfEventFieldTest
{
51 private static final @NonNull String ROOT
= "root";
52 private static final @NonNull String SEQ
= "seq";
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";
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";
63 private static final @NonNull String NAME
= "test";
64 private static final @NonNull String STRUCT
= "struct";
65 private static final @NonNull String VARIANT
= "variant";
66 private static final @NonNull String ENUM
= "enum";
68 private static final byte TEST_NUMBER
= 2;
69 private static final @NonNull String TEST_STRING
= "two";
71 private static final int ARRAY_SIZE
= 2;
73 private StructDefinition fixture
;
76 * Perform pre-test initialization.
78 * @throws UnsupportedEncodingException
79 * Thrown when UTF-8 encoding is not available.
80 * @throws CTFException
84 public void setUp() throws UnsupportedEncodingException
, CTFException
{
85 final byte[] testStringBytes
= TEST_STRING
.getBytes("UTF-8");
88 ByteBuffer bb
= ByteBuffer
.allocateDirect(capacity
);
90 StructDeclaration sDec
= new StructDeclaration(1l);
91 StringDeclaration strDec
= StringDeclaration
.getStringDeclaration(Encoding
.UTF8
);
92 IntegerDeclaration byteDec
= IntegerDeclaration
.UINT_8_DECL
;
93 IntegerDeclaration intDec
= IntegerDeclaration
.INT_8_DECL
;
94 FloatDeclaration flDec
= new FloatDeclaration(8, 24,
95 ByteOrder
.BIG_ENDIAN
, 8);
96 SequenceDeclaration seqDec
= new SequenceDeclaration(LEN
, byteDec
);
97 StructDeclaration structDec
= new StructDeclaration(8);
98 EnumDeclaration enumDec
= new EnumDeclaration(byteDec
);
99 VariantDeclaration varDec
= new VariantDeclaration();
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
);
107 sDec
.addField(INT
, byteDec
);
110 sDec
.addField(ARRAY_INT
, arrIntDec
);
111 for (int i
= 0; i
< ARRAY_SIZE
; ++i
) {
115 sDec
.addField(LEN
, byteDec
);
118 sDec
.addField(FLOAT
, flDec
);
119 bb
.putFloat(TEST_NUMBER
);
121 sDec
.addField(ARRAY_FLOAT
, arrFloatDec
);
122 for (int i
= 0; i
< ARRAY_SIZE
; ++i
) {
123 bb
.putFloat(TEST_NUMBER
);
126 sDec
.addField(STR
, strDec
);
127 bb
.put(testStringBytes
);
130 sDec
.addField(ARRAY_STR
, arrStrDec
);
131 for (int i
= 0; i
< ARRAY_SIZE
; ++i
) {
132 bb
.put(testStringBytes
);
136 sDec
.addField(SEQ
, seqDec
);
140 structDec
.addField(STR
, strDec
);
141 structDec
.addField(INT
, byteDec
);
142 sDec
.addField(STRUCT
, structDec
);
143 bb
.put(testStringBytes
);
147 sDec
.addField(ARRAY_STRUCT
, arrStructDec
);
148 for (int i
= 0; i
< ARRAY_SIZE
; ++i
) {
149 bb
.put(testStringBytes
);
154 enumDec
.add(0, 1, LEN
);
155 enumDec
.add(2, 3, FLOAT
);
156 sDec
.addField(ENUM
, enumDec
);
159 sDec
.addField(ARRAY_ENUM
, arrEnumDec
);
160 for (int i
= 0; i
< ARRAY_SIZE
; ++i
) {
164 varDec
.addField(LEN
, byteDec
);
165 varDec
.addField(FLOAT
, flDec
);
167 sDec
.addField(VARIANT
, varDec
);
168 bb
.putFloat(TEST_NUMBER
);
170 sDec
.addField(ARRAY_VARIANT
, arrVariantDec
);
171 for (int i
= 0; i
< ARRAY_SIZE
; ++i
) {
172 bb
.putFloat(TEST_NUMBER
);
175 fixture
= sDec
.createDefinition(fixture
, ROOT
, new BitBuffer(bb
));
180 * Run the CtfTmfEventField parseField(Definition,String) method test.
183 public void testParseField_float() {
184 FloatDefinition fieldDef
= (FloatDefinition
) fixture
.lookupDefinition(FLOAT
);
185 CtfTmfEventField result
= CtfTmfEventField
.parseField((IDefinition
)fieldDef
, "_" + NAME
);
186 assertEquals("test=2.0", result
.toString());
190 * Run the CtfTmfEventField parseField(Definition,String) method test for an
191 * array of floats field.
194 public void testParseField_array_float() {
195 IDefinition fieldDef
= fixture
.lookupArrayDefinition(ARRAY_FLOAT
);
196 CtfTmfEventField result
= CtfTmfEventField
.parseField(fieldDef
, NAME
);
197 assertEquals("test=[2.0, 2.0]", result
.toString());
201 * Run the CtfTmfEventField parseField(Definition,String) method test.
204 public void testParseField_int() {
205 IDefinition fieldDef
= fixture
.lookupDefinition(INT
);
206 CtfTmfEventField result
= CtfTmfEventField
.parseField(fieldDef
, NAME
);
207 assertEquals("test=2", result
.toString());
211 * Run the CtfTmfEventField parseField(Definition,String) method test for an
212 * array of integers field.
215 public void testParseField_array_int() {
216 IDefinition fieldDef
= fixture
.lookupArrayDefinition(ARRAY_INT
);
217 CtfTmfEventField result
= CtfTmfEventField
.parseField(fieldDef
, NAME
);
218 assertEquals("test=[2, 2]", result
.toString());
222 * Run the CtfTmfEventField parseField(Definition,String) method test.
225 public void testParseField_sequence() {
226 IDefinition fieldDef
= fixture
.lookupDefinition(SEQ
);
227 CtfTmfEventField result
= CtfTmfEventField
.parseField(fieldDef
, NAME
);
228 assertEquals("test=[2, 2]", result
.toString());
232 * Run the CtfTmfEventField parseField(Definition,String) method test.
235 public void testParseField_sequence_value() {
236 IDefinition fieldDef
= fixture
.lookupDefinition(SEQ
);
237 CtfTmfEventField result
= CtfTmfEventField
.parseField(fieldDef
, NAME
);
238 long[] values
= (long[]) result
.getValue();
239 long[] expected
= new long[] { 2, 2 };
240 assertArrayEquals(expected
, values
);
244 * Run the CtfTmfEventField parseField(Definition,String) method test.
247 public void testParseField_string() {
248 IDefinition fieldDef
= fixture
.lookupDefinition(STR
);
249 CtfTmfEventField result
= CtfTmfEventField
.parseField(fieldDef
, NAME
);
250 assertEquals("test=two", result
.toString());
254 * Run the CtfTmfEventField parseField(Definition,String) method test for an
255 * array of strings field.
258 public void testParseField_array_string() {
259 IDefinition fieldDef
= fixture
.lookupArrayDefinition(ARRAY_STR
);
260 CtfTmfEventField result
= CtfTmfEventField
.parseField(fieldDef
, NAME
);
261 assertEquals("test=[two, two]", result
.toString());
265 * Run the CtfTmfEventField parseField(Definition,String) method test.
268 public void testParseField_struct() {
269 IDefinition fieldDef
= fixture
.lookupDefinition(STRUCT
);
270 CtfTmfEventField result
= CtfTmfEventField
.parseField(fieldDef
, NAME
);
271 assertEquals("test=[str=two, int=2]", result
.toString());
275 * Run the CtfTmfEventField parseField(Definition,String) method test for an
276 * array of structs field.
279 public void testParseField_array_struct() {
280 IDefinition fieldDef
= fixture
.lookupArrayDefinition(ARRAY_STRUCT
);
281 CtfTmfEventField result
= CtfTmfEventField
.parseField(fieldDef
, NAME
);
282 assertEquals("test=[[str=two, int=2], [str=two, int=2]]", result
.toString());
286 * Run the CtfTmfEventField parseField(Definition,String) method test.
289 public void testParseField_enum() {
290 IDefinition fieldDef
= fixture
.lookupDefinition(ENUM
);
291 CtfTmfEventField result
= CtfTmfEventField
.parseField(fieldDef
, NAME
);
292 assertEquals("test=float", result
.toString());
296 * Run the CtfTmfEventField parseField(Definition,String) method test for an
297 * array of enums field.
300 public void testParseField_array_enum() {
301 IDefinition fieldDef
= fixture
.lookupArrayDefinition(ARRAY_ENUM
);
302 CtfTmfEventField result
= CtfTmfEventField
.parseField(fieldDef
, NAME
);
303 assertEquals("test=[float, float]", result
.toString());
307 * Run the CtfTmfEventField parseField(Definition,String) method test.
310 public void testParseField_variant() {
311 IDefinition fieldDef
= fixture
.lookupDefinition(VARIANT
);
312 CtfTmfEventField result
= CtfTmfEventField
.parseField(fieldDef
, NAME
);
313 assertEquals("test=float=2.0", result
.toString());
317 * Run the CtfTmfEventField parseField(Definition,String) method test for an
318 * array of variants field.
321 public void testParseField_array_variant() {
322 IDefinition fieldDef
= fixture
.lookupArrayDefinition(ARRAY_VARIANT
);
323 CtfTmfEventField result
= CtfTmfEventField
.parseField(fieldDef
, NAME
);
324 assertEquals("test=[float=2.0, float=2.0]", result
.toString());