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
.core
.tests
.ctfadaptor
;
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
.Encoding
;
27 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.EnumDeclaration
;
28 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.FloatDeclaration
;
29 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.FloatDefinition
;
30 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.IntegerDeclaration
;
31 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.SequenceDeclaration
;
32 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.StringDeclaration
;
33 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.StructDeclaration
;
34 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.StructDefinition
;
35 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.VariantDeclaration
;
36 import org
.eclipse
.linuxtools
.ctf
.core
.trace
.CTFReaderException
;
37 import org
.eclipse
.linuxtools
.tmf
.core
.ctfadaptor
.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>.
48 public class CtfTmfEventFieldTest
{
50 private static final String ROOT
= "root";
51 private static final String SEQ
= "seq";
52 private static final String ARRAY_STR
= "array_str";
53 private static final String ARRAY_FLOAT
= "array_float";
54 private static final String ARRAY_INT
= "array_int";
55 private static final String ARRAY_STRUCT
= "array_struct";
56 private static final String ARRAY_VARIANT
= "array_variant";
57 private static final 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
= new IntegerDeclaration(8, false, 8,
92 ByteOrder
.BIG_ENDIAN
, Encoding
.NONE
, null, 8);
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
);
177 fixture
.read(new BitBuffer(bb
));
181 * Run the CtfTmfEventField parseField(Definition,String) method test.
184 public void testParseField_float() {
185 FloatDefinition fieldDef
= (FloatDefinition
) fixture
.lookupDefinition(FLOAT
);
186 CtfTmfEventField result
= CtfTmfEventField
.parseField(fieldDef
, "_" + NAME
);
187 assertEquals("test=2.0", result
.toString());
191 * Run the CtfTmfEventField parseField(Definition,String) method test for an
192 * array of floats field.
195 public void testParseField_array_float() {
196 Definition fieldDef
= fixture
.lookupArray(ARRAY_FLOAT
);
197 CtfTmfEventField result
= CtfTmfEventField
.parseField(fieldDef
, NAME
);
198 assertEquals("test=[2.0, 2.0]", result
.toString());
202 * Run the CtfTmfEventField parseField(Definition,String) method test.
205 public void testParseField_int() {
206 Definition fieldDef
= fixture
.lookupDefinition(INT
);
207 CtfTmfEventField result
= CtfTmfEventField
.parseField(fieldDef
, NAME
);
208 assertEquals("test=02", result
.toString());
212 * Run the CtfTmfEventField parseField(Definition,String) method test for an
213 * array of integers field.
216 public void testParseField_array_int() {
217 Definition fieldDef
= fixture
.lookupArray(ARRAY_INT
);
218 CtfTmfEventField result
= CtfTmfEventField
.parseField(fieldDef
, NAME
);
219 assertEquals("test=[02, 02]", result
.toString());
223 * Run the CtfTmfEventField parseField(Definition,String) method test.
226 public void testParseField_sequence() {
227 Definition fieldDef
= fixture
.lookupDefinition(SEQ
);
228 CtfTmfEventField result
= CtfTmfEventField
.parseField(fieldDef
, NAME
);
229 assertEquals("test=[02, 02]", result
.toString());
233 * Run the CtfTmfEventField parseField(Definition,String) method test.
236 public void testParseField_sequence_value() {
237 Definition fieldDef
= fixture
.lookupDefinition(SEQ
);
238 CtfTmfEventField result
= CtfTmfEventField
.parseField(fieldDef
, NAME
);
239 long[] values
= (long[]) result
.getValue();
240 long[] expected
= new long[] { 2, 2 };
241 assertArrayEquals(expected
, values
);
245 * Run the CtfTmfEventField parseField(Definition,String) method test.
248 public void testParseField_string() {
249 Definition fieldDef
= fixture
.lookupDefinition(STR
);
250 CtfTmfEventField result
= CtfTmfEventField
.parseField(fieldDef
, NAME
);
251 assertEquals("test=two", result
.toString());
255 * Run the CtfTmfEventField parseField(Definition,String) method test for an
256 * array of strings field.
259 public void testParseField_array_string() {
260 Definition fieldDef
= fixture
.lookupArray(ARRAY_STR
);
261 CtfTmfEventField result
= CtfTmfEventField
.parseField(fieldDef
, NAME
);
262 assertEquals("test=[two, two]", result
.toString());
266 * Run the CtfTmfEventField parseField(Definition,String) method test.
269 public void testParseField_struct() {
270 Definition fieldDef
= fixture
.lookupDefinition(STRUCT
);
271 CtfTmfEventField result
= CtfTmfEventField
.parseField(fieldDef
, NAME
);
272 assertEquals("test=[str=two, int=02]", result
.toString());
276 * Run the CtfTmfEventField parseField(Definition,String) method test for an
277 * array of structs field.
280 public void testParseField_array_struct() {
281 Definition fieldDef
= fixture
.lookupArray(ARRAY_STRUCT
);
282 CtfTmfEventField result
= CtfTmfEventField
.parseField(fieldDef
, NAME
);
283 assertEquals("test=[[str=two, int=02], [str=two, int=02]]", result
.toString());
287 * Run the CtfTmfEventField parseField(Definition,String) method test.
290 public void testParseField_enum() {
291 Definition fieldDef
= fixture
.lookupDefinition(ENUM
);
292 CtfTmfEventField result
= CtfTmfEventField
.parseField(fieldDef
, NAME
);
293 assertEquals("test=float", result
.toString());
297 * Run the CtfTmfEventField parseField(Definition,String) method test for an
298 * array of enums field.
301 public void testParseField_array_enum() {
302 Definition fieldDef
= fixture
.lookupArray(ARRAY_ENUM
);
303 CtfTmfEventField result
= CtfTmfEventField
.parseField(fieldDef
, NAME
);
304 assertEquals("test=[float, float]", result
.toString());
308 * Run the CtfTmfEventField parseField(Definition,String) method test.
311 public void testParseField_variant() {
312 Definition fieldDef
= fixture
.lookupDefinition(VARIANT
);
313 CtfTmfEventField result
= CtfTmfEventField
.parseField(fieldDef
, NAME
);
314 assertEquals("test=float=2.0", result
.toString());
318 * Run the CtfTmfEventField parseField(Definition,String) method test for an
319 * array of variants field.
322 public void testParseField_array_variant() {
323 Definition fieldDef
= fixture
.lookupArray(ARRAY_VARIANT
);
324 CtfTmfEventField result
= CtfTmfEventField
.parseField(fieldDef
, NAME
);
325 assertEquals("test=[float=2.0, float=2.0]", result
.toString());