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
.linuxtools
.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
.linuxtools
.ctf
.core
.event
.io
.BitBuffer
;
24 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.ArrayDeclaration
;
25 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.Definition
;
26 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.EnumDeclaration
;
27 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.FloatDeclaration
;
28 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.FloatDefinition
;
29 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.IntegerDeclaration
;
30 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.SequenceDeclaration
;
31 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.StringDeclaration
;
32 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.StructDeclaration
;
33 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.StructDefinition
;
34 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.VariantDeclaration
;
35 import org
.eclipse
.linuxtools
.ctf
.core
.trace
.CTFReaderException
;
36 import org
.eclipse
.linuxtools
.tmf
.ctf
.core
.CtfTmfEventField
;
37 import org
.junit
.Before
;
38 import org
.junit
.Test
;
41 * The class <code>CtfTmfEventFieldTest</code> contains tests for the class
42 * <code>{@link CtfTmfEventField}</code>.
44 * @author Matthew Khouzam
47 public class CtfTmfEventFieldTest
{
49 private static final String ROOT
= "root";
50 private static final String SEQ
= "seq";
51 private static final String ARRAY_STR
= "array_str";
52 private static final String ARRAY_FLOAT
= "array_float";
53 private static final String ARRAY_INT
= "array_int";
54 private static final String ARRAY_STRUCT
= "array_struct";
55 private static final String ARRAY_VARIANT
= "array_variant";
56 private static final String ARRAY_ENUM
= "array_enum";
57 private static final String STR
= "str";
58 private static final String FLOAT
= "float";
59 private static final String LEN
= "len";
60 private static final String INT
= "int";
61 private static final String NAME
= "test";
62 private static final String STRUCT
= "struct";
63 private static final String VARIANT
= "variant";
64 private static final String ENUM
= "enum";
66 private static final byte TEST_NUMBER
= 2;
67 private static final String TEST_STRING
= "two";
69 private static final int ARRAY_SIZE
= 2;
71 private StructDefinition fixture
;
74 * Perform pre-test initialization.
76 * @throws UnsupportedEncodingException
77 * Thrown when UTF-8 encoding is not available.
78 * @throws CTFReaderException
82 public void setUp() throws UnsupportedEncodingException
, CTFReaderException
{
83 final byte[] testStringBytes
= TEST_STRING
.getBytes("UTF-8");
86 ByteBuffer bb
= ByteBuffer
.allocateDirect(capacity
);
88 StructDeclaration sDec
= new StructDeclaration(1l);
89 StringDeclaration strDec
= new StringDeclaration();
90 IntegerDeclaration intDec
= IntegerDeclaration
.UINT_8_DECL
;
91 FloatDeclaration flDec
= new FloatDeclaration(8, 24,
92 ByteOrder
.BIG_ENDIAN
, 8);
93 SequenceDeclaration seqDec
= new SequenceDeclaration(LEN
, intDec
);
94 StructDeclaration structDec
= new StructDeclaration(8);
95 EnumDeclaration enumDec
= new EnumDeclaration(intDec
);
96 VariantDeclaration varDec
= new VariantDeclaration();
97 ArrayDeclaration arrStrDec
= new ArrayDeclaration(ARRAY_SIZE
, strDec
);
98 ArrayDeclaration arrFloatDec
= new ArrayDeclaration(ARRAY_SIZE
, flDec
);
99 ArrayDeclaration arrIntDec
= new ArrayDeclaration(ARRAY_SIZE
, intDec
);
100 ArrayDeclaration arrStructDec
= new ArrayDeclaration(ARRAY_SIZE
, structDec
);
101 ArrayDeclaration arrVariantDec
= new ArrayDeclaration(ARRAY_SIZE
, varDec
);
102 ArrayDeclaration arrEnumDec
= new ArrayDeclaration(ARRAY_SIZE
, enumDec
);
104 sDec
.addField(INT
, intDec
);
107 sDec
.addField(ARRAY_INT
, arrIntDec
);
108 for (int i
= 0; i
< ARRAY_SIZE
; ++i
) {
112 sDec
.addField(LEN
, intDec
);
115 sDec
.addField(FLOAT
, flDec
);
116 bb
.putFloat(TEST_NUMBER
);
118 sDec
.addField(ARRAY_FLOAT
, arrFloatDec
);
119 for (int i
= 0; i
< ARRAY_SIZE
; ++i
) {
120 bb
.putFloat(TEST_NUMBER
);
123 sDec
.addField(STR
, strDec
);
124 bb
.put(testStringBytes
);
127 sDec
.addField(ARRAY_STR
, arrStrDec
);
128 for (int i
= 0; i
< ARRAY_SIZE
; ++i
) {
129 bb
.put(testStringBytes
);
133 sDec
.addField(SEQ
, seqDec
);
137 structDec
.addField(STR
, strDec
);
138 structDec
.addField(INT
, intDec
);
139 sDec
.addField(STRUCT
, structDec
);
140 bb
.put(testStringBytes
);
144 sDec
.addField(ARRAY_STRUCT
, arrStructDec
);
145 for (int i
= 0; i
< ARRAY_SIZE
; ++i
) {
146 bb
.put(testStringBytes
);
151 enumDec
.add(0, 1, LEN
);
152 enumDec
.add(2, 3, FLOAT
);
153 sDec
.addField(ENUM
, enumDec
);
156 sDec
.addField(ARRAY_ENUM
, arrEnumDec
);
157 for (int i
= 0; i
< ARRAY_SIZE
; ++i
) {
161 varDec
.addField(LEN
, intDec
);
162 varDec
.addField(FLOAT
, flDec
);
164 sDec
.addField(VARIANT
, varDec
);
165 bb
.putFloat(TEST_NUMBER
);
167 sDec
.addField(ARRAY_VARIANT
, arrVariantDec
);
168 for (int i
= 0; i
< ARRAY_SIZE
; ++i
) {
169 bb
.putFloat(TEST_NUMBER
);
172 fixture
= sDec
.createDefinition(fixture
, ROOT
, new BitBuffer(bb
));
177 * Run the CtfTmfEventField parseField(Definition,String) method test.
180 public void testParseField_float() {
181 FloatDefinition fieldDef
= (FloatDefinition
) fixture
.lookupDefinition(FLOAT
);
182 CtfTmfEventField result
= CtfTmfEventField
.parseField(fieldDef
, "_" + NAME
);
183 assertEquals("test=2.0", result
.toString());
187 * Run the CtfTmfEventField parseField(Definition,String) method test for an
188 * array of floats field.
191 public void testParseField_array_float() {
192 Definition fieldDef
= fixture
.lookupArray(ARRAY_FLOAT
);
193 CtfTmfEventField result
= CtfTmfEventField
.parseField(fieldDef
, NAME
);
194 assertEquals("test=[2.0, 2.0]", result
.toString());
198 * Run the CtfTmfEventField parseField(Definition,String) method test.
201 public void testParseField_int() {
202 Definition fieldDef
= fixture
.lookupDefinition(INT
);
203 CtfTmfEventField result
= CtfTmfEventField
.parseField(fieldDef
, NAME
);
204 assertEquals("test=2", result
.toString());
208 * Run the CtfTmfEventField parseField(Definition,String) method test for an
209 * array of integers field.
212 public void testParseField_array_int() {
213 Definition fieldDef
= fixture
.lookupArray(ARRAY_INT
);
214 CtfTmfEventField result
= CtfTmfEventField
.parseField(fieldDef
, NAME
);
215 assertEquals("test=[2, 2]", result
.toString());
219 * Run the CtfTmfEventField parseField(Definition,String) method test.
222 public void testParseField_sequence() {
223 Definition fieldDef
= fixture
.lookupDefinition(SEQ
);
224 CtfTmfEventField result
= CtfTmfEventField
.parseField(fieldDef
, NAME
);
225 assertEquals("test=[2, 2]", result
.toString());
229 * Run the CtfTmfEventField parseField(Definition,String) method test.
232 public void testParseField_sequence_value() {
233 Definition fieldDef
= fixture
.lookupDefinition(SEQ
);
234 CtfTmfEventField result
= CtfTmfEventField
.parseField(fieldDef
, NAME
);
235 long[] values
= (long[]) result
.getValue();
236 long[] expected
= new long[] { 2, 2 };
237 assertArrayEquals(expected
, values
);
241 * Run the CtfTmfEventField parseField(Definition,String) method test.
244 public void testParseField_string() {
245 Definition fieldDef
= fixture
.lookupDefinition(STR
);
246 CtfTmfEventField result
= CtfTmfEventField
.parseField(fieldDef
, NAME
);
247 assertEquals("test=two", result
.toString());
251 * Run the CtfTmfEventField parseField(Definition,String) method test for an
252 * array of strings field.
255 public void testParseField_array_string() {
256 Definition fieldDef
= fixture
.lookupArray(ARRAY_STR
);
257 CtfTmfEventField result
= CtfTmfEventField
.parseField(fieldDef
, NAME
);
258 assertEquals("test=[two, two]", result
.toString());
262 * Run the CtfTmfEventField parseField(Definition,String) method test.
265 public void testParseField_struct() {
266 Definition fieldDef
= fixture
.lookupDefinition(STRUCT
);
267 CtfTmfEventField result
= CtfTmfEventField
.parseField(fieldDef
, NAME
);
268 assertEquals("test=[str=two, int=2]", result
.toString());
272 * Run the CtfTmfEventField parseField(Definition,String) method test for an
273 * array of structs field.
276 public void testParseField_array_struct() {
277 Definition fieldDef
= fixture
.lookupArray(ARRAY_STRUCT
);
278 CtfTmfEventField result
= CtfTmfEventField
.parseField(fieldDef
, NAME
);
279 assertEquals("test=[[str=two, int=2], [str=two, int=2]]", result
.toString());
283 * Run the CtfTmfEventField parseField(Definition,String) method test.
286 public void testParseField_enum() {
287 Definition fieldDef
= fixture
.lookupDefinition(ENUM
);
288 CtfTmfEventField result
= CtfTmfEventField
.parseField(fieldDef
, NAME
);
289 assertEquals("test=float", result
.toString());
293 * Run the CtfTmfEventField parseField(Definition,String) method test for an
294 * array of enums field.
297 public void testParseField_array_enum() {
298 Definition fieldDef
= fixture
.lookupArray(ARRAY_ENUM
);
299 CtfTmfEventField result
= CtfTmfEventField
.parseField(fieldDef
, NAME
);
300 assertEquals("test=[float, float]", result
.toString());
304 * Run the CtfTmfEventField parseField(Definition,String) method test.
307 public void testParseField_variant() {
308 Definition fieldDef
= fixture
.lookupDefinition(VARIANT
);
309 CtfTmfEventField result
= CtfTmfEventField
.parseField(fieldDef
, NAME
);
310 assertEquals("test=float=2.0", result
.toString());
314 * Run the CtfTmfEventField parseField(Definition,String) method test for an
315 * array of variants field.
318 public void testParseField_array_variant() {
319 Definition fieldDef
= fixture
.lookupArray(ARRAY_VARIANT
);
320 CtfTmfEventField result
= CtfTmfEventField
.parseField(fieldDef
, NAME
);
321 assertEquals("test=[float=2.0, float=2.0]", result
.toString());