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
.assertEquals
;
17 import static org
.junit
.Assert
.assertArrayEquals
;
19 import java
.nio
.ByteOrder
;
21 import org
.eclipse
.linuxtools
.ctf
.core
.event
.io
.BitBuffer
;
22 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.ArrayDeclaration
;
23 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.Definition
;
24 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.Encoding
;
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
.IntegerDeclaration
;
29 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.SequenceDeclaration
;
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
.tmf
.core
.ctfadaptor
.CtfTmfEventField
;
36 import org
.junit
.Before
;
37 import org
.junit
.Test
;
40 * The class <code>CtfTmfEventFieldTest</code> contains tests for the class
41 * <code>{@link CtfTmfEventField}</code>.
46 public class CtfTmfEventFieldTest
{
48 private static final String ROOT
= "root";
49 private static final String SEQ
= "seq";
50 private static final String ARRAY
= "array";
51 private static final String STR
= "str";
52 private static final String FLOAT
= "float";
53 private static final String LEN
= "len";
54 private static final String INT
= "int";
55 private static final String NAME
= "test";
56 private static final String STRUCT
= "struct";
57 private static final String VARIANT
= "variant";
58 private static final String ENUM
= "enum";
60 private StructDefinition fixture
;
63 * Perform pre-test initialization.
64 * @throws CTFReaderException error
67 public void setUp() throws CTFReaderException
{
68 StructDeclaration sDec
= new StructDeclaration(1l);
69 StringDeclaration strDec
= new StringDeclaration();
70 IntegerDeclaration intDec
= new IntegerDeclaration(8, false, 8,
71 ByteOrder
.BIG_ENDIAN
, Encoding
.NONE
, null, 8);
72 FloatDeclaration flDec
= new FloatDeclaration(8, 24,
73 ByteOrder
.BIG_ENDIAN
, 8);
74 ArrayDeclaration arrDec
= new ArrayDeclaration(2, intDec
);
75 SequenceDeclaration seqDec
= new SequenceDeclaration(LEN
, intDec
);
76 StructDeclaration structDec
= new StructDeclaration(32);
77 EnumDeclaration enumDec
= new EnumDeclaration(intDec
);
78 VariantDeclaration varDec
= new VariantDeclaration();
79 sDec
.addField(INT
, intDec
);
80 sDec
.addField(LEN
, intDec
);
81 sDec
.addField(FLOAT
, flDec
);
82 sDec
.addField(STR
, strDec
);
83 sDec
.addField(ARRAY
, arrDec
);
84 sDec
.addField(SEQ
, seqDec
);
85 structDec
.addField(STR
, strDec
);
86 structDec
.addField(INT
, intDec
);
87 sDec
.addField(STRUCT
, structDec
);
88 enumDec
.add(0, 1, LEN
);
89 enumDec
.add(2, 3, FLOAT
);
90 sDec
.addField(ENUM
, enumDec
);
91 varDec
.addField(LEN
, intDec
);
92 varDec
.addField(FLOAT
, flDec
);
94 sDec
.addField(VARIANT
, varDec
);
95 fixture
= sDec
.createDefinition(fixture
, ROOT
);
97 java
.nio
.ByteBuffer bb
= java
.nio
.ByteBuffer
.allocateDirect(capacity
);
98 for (int i
= 0; i
< capacity
; i
++) {
108 fixture
.read(new BitBuffer(bb
));
112 * Run the CtfTmfEventField parseField(Definition,String) method test.
115 public void testParseField_float() {
116 FloatDefinition fieldDef
= (FloatDefinition
) fixture
.lookupDefinition(FLOAT
);
117 CtfTmfEventField result
= CtfTmfEventField
.parseField(fieldDef
, "_" + NAME
);
118 assertEquals("test=9.551467814359616E-38", result
.toString());
122 * Run the CtfTmfEventField parseField(Definition,String) method test.
125 public void testParseField_array() {
126 Definition fieldDef
= fixture
.lookupArray(ARRAY
);
127 CtfTmfEventField result
= CtfTmfEventField
.parseField(fieldDef
, NAME
);
128 assertEquals("test=[02, 02]", result
.toString());
132 * Run the CtfTmfEventField parseField(Definition,String) method test.
135 public void testParseField_int() {
136 Definition fieldDef
= fixture
.lookupDefinition(INT
);
137 CtfTmfEventField result
= CtfTmfEventField
.parseField(fieldDef
, NAME
);
138 assertEquals("test=02", result
.toString());
142 * Run the CtfTmfEventField parseField(Definition,String) method test.
145 public void testParseField_sequence() {
146 Definition fieldDef
= fixture
.lookupDefinition(SEQ
);
147 CtfTmfEventField result
= CtfTmfEventField
.parseField(fieldDef
, NAME
);
148 assertEquals("test=[02, 02]", result
.toString());
152 * Run the CtfTmfEventField parseField(Definition,String) method test.
155 public void testParseField_sequence_value() {
156 Definition fieldDef
= fixture
.lookupDefinition(SEQ
);
157 CtfTmfEventField result
= CtfTmfEventField
.parseField(fieldDef
, NAME
);
158 long[] values
= (long[]) result
.getValue();
159 long[] expected
= new long[] { 2, 2 };
160 assertArrayEquals(expected
, values
);
164 * Run the CtfTmfEventField parseField(Definition,String) method test.
167 public void testParseField_string() {
168 Definition fieldDef
= fixture
.lookupDefinition(STR
);
169 CtfTmfEventField result
= CtfTmfEventField
.parseField(fieldDef
, NAME
);
170 assertEquals("test=\ 2\ 2\ 2\ 2\ 2\ 2\ 2\ 2\ 2\ 2\ 2\ 2\ 2\ 2", result
.toString());
174 * Run the CtfTmfEventField parseField(Definition,String) method test.
177 public void testParseField_struct() {
178 Definition fieldDef
= fixture
.lookupDefinition(STRUCT
);
179 CtfTmfEventField result
= CtfTmfEventField
.parseField(fieldDef
, NAME
);
180 assertEquals("test=[str=\ 2\ 2\ 2\ 2\ 2\ 2\ 2\ 2\ 2\ 2\ 2\ 2, int=02]", result
.toString());
184 * Run the CtfTmfEventField parseField(Definition,String) method test.
187 public void testParseField_enum() {
188 Definition fieldDef
= fixture
.lookupDefinition(ENUM
);
189 CtfTmfEventField result
= CtfTmfEventField
.parseField(fieldDef
, NAME
);
190 assertEquals("test=float", result
.toString());
194 * Run the CtfTmfEventField parseField(Definition,String) method test.
197 public void testParseField_variant() {
198 Definition fieldDef
= fixture
.lookupDefinition(VARIANT
);
199 CtfTmfEventField result
= CtfTmfEventField
.parseField(fieldDef
, NAME
);
200 assertEquals("test=float=9.551467814359616E-38", result
.toString());