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 intDec
= IntegerDeclaration
.UINT_8_DECL
;
93 FloatDeclaration flDec
= new FloatDeclaration(8, 24,
94 ByteOrder
.BIG_ENDIAN
, 8);
95 SequenceDeclaration seqDec
= new SequenceDeclaration(LEN
, intDec
);
96 StructDeclaration structDec
= new StructDeclaration(8);
97 EnumDeclaration enumDec
= new EnumDeclaration(intDec
);
98 VariantDeclaration varDec
= new VariantDeclaration();
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
);
106 sDec
.addField(INT
, intDec
);
109 sDec
.addField(ARRAY_INT
, arrIntDec
);
110 for (int i
= 0; i
< ARRAY_SIZE
; ++i
) {
114 sDec
.addField(LEN
, intDec
);
117 sDec
.addField(FLOAT
, flDec
);
118 bb
.putFloat(TEST_NUMBER
);
120 sDec
.addField(ARRAY_FLOAT
, arrFloatDec
);
121 for (int i
= 0; i
< ARRAY_SIZE
; ++i
) {
122 bb
.putFloat(TEST_NUMBER
);
125 sDec
.addField(STR
, strDec
);
126 bb
.put(testStringBytes
);
129 sDec
.addField(ARRAY_STR
, arrStrDec
);
130 for (int i
= 0; i
< ARRAY_SIZE
; ++i
) {
131 bb
.put(testStringBytes
);
135 sDec
.addField(SEQ
, seqDec
);
139 structDec
.addField(STR
, strDec
);
140 structDec
.addField(INT
, intDec
);
141 sDec
.addField(STRUCT
, structDec
);
142 bb
.put(testStringBytes
);
146 sDec
.addField(ARRAY_STRUCT
, arrStructDec
);
147 for (int i
= 0; i
< ARRAY_SIZE
; ++i
) {
148 bb
.put(testStringBytes
);
153 enumDec
.add(0, 1, LEN
);
154 enumDec
.add(2, 3, FLOAT
);
155 sDec
.addField(ENUM
, enumDec
);
158 sDec
.addField(ARRAY_ENUM
, arrEnumDec
);
159 for (int i
= 0; i
< ARRAY_SIZE
; ++i
) {
163 varDec
.addField(LEN
, intDec
);
164 varDec
.addField(FLOAT
, flDec
);
166 sDec
.addField(VARIANT
, varDec
);
167 bb
.putFloat(TEST_NUMBER
);
169 sDec
.addField(ARRAY_VARIANT
, arrVariantDec
);
170 for (int i
= 0; i
< ARRAY_SIZE
; ++i
) {
171 bb
.putFloat(TEST_NUMBER
);
174 fixture
= sDec
.createDefinition(fixture
, ROOT
, new BitBuffer(bb
));
179 * Run the CtfTmfEventField parseField(Definition,String) method test.
182 public void testParseField_float() {
183 FloatDefinition fieldDef
= (FloatDefinition
) fixture
.lookupDefinition(FLOAT
);
184 CtfTmfEventField result
= CtfTmfEventField
.parseField((IDefinition
)fieldDef
, "_" + NAME
);
185 assertEquals("test=2.0", result
.toString());
189 * Run the CtfTmfEventField parseField(Definition,String) method test for an
190 * array of floats field.
193 public void testParseField_array_float() {
194 IDefinition fieldDef
= fixture
.lookupArrayDefinition(ARRAY_FLOAT
);
195 CtfTmfEventField result
= CtfTmfEventField
.parseField(fieldDef
, NAME
);
196 assertEquals("test=[2.0, 2.0]", result
.toString());
200 * Run the CtfTmfEventField parseField(Definition,String) method test.
203 public void testParseField_int() {
204 IDefinition fieldDef
= fixture
.lookupDefinition(INT
);
205 CtfTmfEventField result
= CtfTmfEventField
.parseField(fieldDef
, NAME
);
206 assertEquals("test=2", result
.toString());
210 * Run the CtfTmfEventField parseField(Definition,String) method test for an
211 * array of integers field.
214 public void testParseField_array_int() {
215 IDefinition fieldDef
= fixture
.lookupArrayDefinition(ARRAY_INT
);
216 CtfTmfEventField result
= CtfTmfEventField
.parseField(fieldDef
, NAME
);
217 assertEquals("test=[2, 2]", result
.toString());
221 * Run the CtfTmfEventField parseField(Definition,String) method test.
224 public void testParseField_sequence() {
225 IDefinition fieldDef
= fixture
.lookupDefinition(SEQ
);
226 CtfTmfEventField result
= CtfTmfEventField
.parseField(fieldDef
, NAME
);
227 assertEquals("test=[2, 2]", result
.toString());
231 * Run the CtfTmfEventField parseField(Definition,String) method test.
234 public void testParseField_sequence_value() {
235 IDefinition fieldDef
= fixture
.lookupDefinition(SEQ
);
236 CtfTmfEventField result
= CtfTmfEventField
.parseField(fieldDef
, NAME
);
237 long[] values
= (long[]) result
.getValue();
238 long[] expected
= new long[] { 2, 2 };
239 assertArrayEquals(expected
, values
);
243 * Run the CtfTmfEventField parseField(Definition,String) method test.
246 public void testParseField_string() {
247 IDefinition fieldDef
= fixture
.lookupDefinition(STR
);
248 CtfTmfEventField result
= CtfTmfEventField
.parseField(fieldDef
, NAME
);
249 assertEquals("test=two", result
.toString());
253 * Run the CtfTmfEventField parseField(Definition,String) method test for an
254 * array of strings field.
257 public void testParseField_array_string() {
258 IDefinition fieldDef
= fixture
.lookupArrayDefinition(ARRAY_STR
);
259 CtfTmfEventField result
= CtfTmfEventField
.parseField(fieldDef
, NAME
);
260 assertEquals("test=[two, two]", result
.toString());
264 * Run the CtfTmfEventField parseField(Definition,String) method test.
267 public void testParseField_struct() {
268 IDefinition fieldDef
= fixture
.lookupDefinition(STRUCT
);
269 CtfTmfEventField result
= CtfTmfEventField
.parseField(fieldDef
, NAME
);
270 assertEquals("test=[str=two, int=2]", result
.toString());
274 * Run the CtfTmfEventField parseField(Definition,String) method test for an
275 * array of structs field.
278 public void testParseField_array_struct() {
279 IDefinition fieldDef
= fixture
.lookupArrayDefinition(ARRAY_STRUCT
);
280 CtfTmfEventField result
= CtfTmfEventField
.parseField(fieldDef
, NAME
);
281 assertEquals("test=[[str=two, int=2], [str=two, int=2]]", result
.toString());
285 * Run the CtfTmfEventField parseField(Definition,String) method test.
288 public void testParseField_enum() {
289 IDefinition fieldDef
= fixture
.lookupDefinition(ENUM
);
290 CtfTmfEventField result
= CtfTmfEventField
.parseField(fieldDef
, NAME
);
291 assertEquals("test=float", result
.toString());
295 * Run the CtfTmfEventField parseField(Definition,String) method test for an
296 * array of enums field.
299 public void testParseField_array_enum() {
300 IDefinition fieldDef
= fixture
.lookupArrayDefinition(ARRAY_ENUM
);
301 CtfTmfEventField result
= CtfTmfEventField
.parseField(fieldDef
, NAME
);
302 assertEquals("test=[float, float]", result
.toString());
306 * Run the CtfTmfEventField parseField(Definition,String) method test.
309 public void testParseField_variant() {
310 IDefinition fieldDef
= fixture
.lookupDefinition(VARIANT
);
311 CtfTmfEventField result
= CtfTmfEventField
.parseField(fieldDef
, NAME
);
312 assertEquals("test=float=2.0", result
.toString());
316 * Run the CtfTmfEventField parseField(Definition,String) method test for an
317 * array of variants field.
320 public void testParseField_array_variant() {
321 IDefinition fieldDef
= fixture
.lookupArrayDefinition(ARRAY_VARIANT
);
322 CtfTmfEventField result
= CtfTmfEventField
.parseField(fieldDef
, NAME
);
323 assertEquals("test=[float=2.0, float=2.0]", result
.toString());