1 /*******************************************************************************
2 * Copyright (c) 2012, 2013 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
;
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
.linuxtools
.ctf
.core
.event
.io
.BitBuffer
;
25 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.EnumDeclaration
;
26 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.FloatDeclaration
;
27 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.FloatDefinition
;
28 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.IDefinition
;
29 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.IntegerDeclaration
;
30 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.StringDeclaration
;
31 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.StructDeclaration
;
32 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.StructDefinition
;
33 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.VariantDeclaration
;
34 import org
.eclipse
.linuxtools
.ctf
.core
.trace
.CTFReaderException
;
35 import org
.eclipse
.linuxtools
.internal
.ctf
.core
.event
.types
.ArrayDeclaration
;
36 import org
.eclipse
.linuxtools
.internal
.ctf
.core
.event
.types
.SequenceDeclaration
;
37 import org
.eclipse
.tracecompass
.tmf
.ctf
.core
.CtfTmfEventField
;
38 import org
.junit
.Before
;
39 import org
.junit
.Test
;
42 * The class <code>CtfTmfEventFieldTest</code> contains tests for the class
43 * <code>{@link CtfTmfEventField}</code>.
45 * @author Matthew Khouzam
48 public class CtfTmfEventFieldTest
{
50 private static final @NonNull String ROOT
= "root";
51 private static final String SEQ
= "seq";
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";
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";
63 private static final String STRUCT
= "struct";
64 private static final String VARIANT
= "variant";
65 private static final String ENUM
= "enum";
67 private static final byte TEST_NUMBER
= 2;
68 private static final String TEST_STRING
= "two";
70 private static final int ARRAY_SIZE
= 2;
72 private StructDefinition fixture
;
75 * Perform pre-test initialization.
77 * @throws UnsupportedEncodingException
78 * Thrown when UTF-8 encoding is not available.
79 * @throws CTFReaderException
83 public void setUp() throws UnsupportedEncodingException
, CTFReaderException
{
84 final byte[] testStringBytes
= TEST_STRING
.getBytes("UTF-8");
87 ByteBuffer bb
= ByteBuffer
.allocateDirect(capacity
);
89 StructDeclaration sDec
= new StructDeclaration(1l);
90 StringDeclaration strDec
= new StringDeclaration();
91 IntegerDeclaration intDec
= IntegerDeclaration
.UINT_8_DECL
;
92 FloatDeclaration flDec
= new FloatDeclaration(8, 24,
93 ByteOrder
.BIG_ENDIAN
, 8);
94 SequenceDeclaration seqDec
= new SequenceDeclaration(LEN
, intDec
);
95 StructDeclaration structDec
= new StructDeclaration(8);
96 EnumDeclaration enumDec
= new EnumDeclaration(intDec
);
97 VariantDeclaration varDec
= new VariantDeclaration();
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
);
105 sDec
.addField(INT
, intDec
);
108 sDec
.addField(ARRAY_INT
, arrIntDec
);
109 for (int i
= 0; i
< ARRAY_SIZE
; ++i
) {
113 sDec
.addField(LEN
, intDec
);
116 sDec
.addField(FLOAT
, flDec
);
117 bb
.putFloat(TEST_NUMBER
);
119 sDec
.addField(ARRAY_FLOAT
, arrFloatDec
);
120 for (int i
= 0; i
< ARRAY_SIZE
; ++i
) {
121 bb
.putFloat(TEST_NUMBER
);
124 sDec
.addField(STR
, strDec
);
125 bb
.put(testStringBytes
);
128 sDec
.addField(ARRAY_STR
, arrStrDec
);
129 for (int i
= 0; i
< ARRAY_SIZE
; ++i
) {
130 bb
.put(testStringBytes
);
134 sDec
.addField(SEQ
, seqDec
);
138 structDec
.addField(STR
, strDec
);
139 structDec
.addField(INT
, intDec
);
140 sDec
.addField(STRUCT
, structDec
);
141 bb
.put(testStringBytes
);
145 sDec
.addField(ARRAY_STRUCT
, arrStructDec
);
146 for (int i
= 0; i
< ARRAY_SIZE
; ++i
) {
147 bb
.put(testStringBytes
);
152 enumDec
.add(0, 1, LEN
);
153 enumDec
.add(2, 3, FLOAT
);
154 sDec
.addField(ENUM
, enumDec
);
157 sDec
.addField(ARRAY_ENUM
, arrEnumDec
);
158 for (int i
= 0; i
< ARRAY_SIZE
; ++i
) {
162 varDec
.addField(LEN
, intDec
);
163 varDec
.addField(FLOAT
, flDec
);
165 sDec
.addField(VARIANT
, varDec
);
166 bb
.putFloat(TEST_NUMBER
);
168 sDec
.addField(ARRAY_VARIANT
, arrVariantDec
);
169 for (int i
= 0; i
< ARRAY_SIZE
; ++i
) {
170 bb
.putFloat(TEST_NUMBER
);
173 fixture
= sDec
.createDefinition(fixture
, ROOT
, new BitBuffer(bb
));
178 * Run the CtfTmfEventField parseField(Definition,String) method test.
181 public void testParseField_float() {
182 FloatDefinition fieldDef
= (FloatDefinition
) fixture
.lookupDefinition(FLOAT
);
183 CtfTmfEventField result
= CtfTmfEventField
.parseField((IDefinition
)fieldDef
, "_" + NAME
);
184 assertEquals("test=2.0", result
.toString());
188 * Run the CtfTmfEventField parseField(Definition,String) method test for an
189 * array of floats field.
192 public void testParseField_array_float() {
193 IDefinition fieldDef
= fixture
.lookupArrayDefinition(ARRAY_FLOAT
);
194 CtfTmfEventField result
= CtfTmfEventField
.parseField(fieldDef
, NAME
);
195 assertEquals("test=[2.0, 2.0]", result
.toString());
199 * Run the CtfTmfEventField parseField(Definition,String) method test.
202 public void testParseField_int() {
203 IDefinition fieldDef
= fixture
.lookupDefinition(INT
);
204 CtfTmfEventField result
= CtfTmfEventField
.parseField(fieldDef
, NAME
);
205 assertEquals("test=2", result
.toString());
209 * Run the CtfTmfEventField parseField(Definition,String) method test for an
210 * array of integers field.
213 public void testParseField_array_int() {
214 IDefinition fieldDef
= fixture
.lookupArrayDefinition(ARRAY_INT
);
215 CtfTmfEventField result
= CtfTmfEventField
.parseField(fieldDef
, NAME
);
216 assertEquals("test=[2, 2]", result
.toString());
220 * Run the CtfTmfEventField parseField(Definition,String) method test.
223 public void testParseField_sequence() {
224 IDefinition fieldDef
= fixture
.lookupDefinition(SEQ
);
225 CtfTmfEventField result
= CtfTmfEventField
.parseField(fieldDef
, NAME
);
226 assertEquals("test=[2, 2]", result
.toString());
230 * Run the CtfTmfEventField parseField(Definition,String) method test.
233 public void testParseField_sequence_value() {
234 IDefinition fieldDef
= fixture
.lookupDefinition(SEQ
);
235 CtfTmfEventField result
= CtfTmfEventField
.parseField(fieldDef
, NAME
);
236 long[] values
= (long[]) result
.getValue();
237 long[] expected
= new long[] { 2, 2 };
238 assertArrayEquals(expected
, values
);
242 * Run the CtfTmfEventField parseField(Definition,String) method test.
245 public void testParseField_string() {
246 IDefinition fieldDef
= fixture
.lookupDefinition(STR
);
247 CtfTmfEventField result
= CtfTmfEventField
.parseField(fieldDef
, NAME
);
248 assertEquals("test=two", result
.toString());
252 * Run the CtfTmfEventField parseField(Definition,String) method test for an
253 * array of strings field.
256 public void testParseField_array_string() {
257 IDefinition fieldDef
= fixture
.lookupArrayDefinition(ARRAY_STR
);
258 CtfTmfEventField result
= CtfTmfEventField
.parseField(fieldDef
, NAME
);
259 assertEquals("test=[two, two]", result
.toString());
263 * Run the CtfTmfEventField parseField(Definition,String) method test.
266 public void testParseField_struct() {
267 IDefinition fieldDef
= fixture
.lookupDefinition(STRUCT
);
268 CtfTmfEventField result
= CtfTmfEventField
.parseField(fieldDef
, NAME
);
269 assertEquals("test=[str=two, int=2]", result
.toString());
273 * Run the CtfTmfEventField parseField(Definition,String) method test for an
274 * array of structs field.
277 public void testParseField_array_struct() {
278 IDefinition fieldDef
= fixture
.lookupArrayDefinition(ARRAY_STRUCT
);
279 CtfTmfEventField result
= CtfTmfEventField
.parseField(fieldDef
, NAME
);
280 assertEquals("test=[[str=two, int=2], [str=two, int=2]]", result
.toString());
284 * Run the CtfTmfEventField parseField(Definition,String) method test.
287 public void testParseField_enum() {
288 IDefinition fieldDef
= fixture
.lookupDefinition(ENUM
);
289 CtfTmfEventField result
= CtfTmfEventField
.parseField(fieldDef
, NAME
);
290 assertEquals("test=float", result
.toString());
294 * Run the CtfTmfEventField parseField(Definition,String) method test for an
295 * array of enums field.
298 public void testParseField_array_enum() {
299 IDefinition fieldDef
= fixture
.lookupArrayDefinition(ARRAY_ENUM
);
300 CtfTmfEventField result
= CtfTmfEventField
.parseField(fieldDef
, NAME
);
301 assertEquals("test=[float, float]", result
.toString());
305 * Run the CtfTmfEventField parseField(Definition,String) method test.
308 public void testParseField_variant() {
309 IDefinition fieldDef
= fixture
.lookupDefinition(VARIANT
);
310 CtfTmfEventField result
= CtfTmfEventField
.parseField(fieldDef
, NAME
);
311 assertEquals("test=float=2.0", result
.toString());
315 * Run the CtfTmfEventField parseField(Definition,String) method test for an
316 * array of variants field.
319 public void testParseField_array_variant() {
320 IDefinition fieldDef
= fixture
.lookupArrayDefinition(ARRAY_VARIANT
);
321 CtfTmfEventField result
= CtfTmfEventField
.parseField(fieldDef
, NAME
);
322 assertEquals("test=[float=2.0, float=2.0]", result
.toString());