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
.tmf
.core
.ctfadaptor
.CtfTmfEventField
;
35 import org
.junit
.Before
;
36 import org
.junit
.Test
;
39 * The class <code>CtfTmfEventFieldTest</code> contains tests for the class
40 * <code>{@link CtfTmfEventField}</code>.
45 public class CtfTmfEventFieldTest
{
47 private static final String ROOT
= "root";
48 private static final String SEQ
= "seq";
49 private static final String ARRAY
= "array";
50 private static final String STR
= "str";
51 private static final String FLOAT
= "float";
52 private static final String LEN
= "len";
53 private static final String INT
= "int";
54 private static final String NAME
= "test";
55 private static final String STRUCT
= "struct";
56 private static final String VARIANT
= "variant";
57 private static final String ENUM
= "enum";
59 private StructDefinition fixture
;
62 * Perform pre-test initialization.
66 StructDeclaration sDec
= new StructDeclaration(1l);
67 StringDeclaration strDec
= new StringDeclaration();
68 IntegerDeclaration intDec
= new IntegerDeclaration(8, false, 8,
69 ByteOrder
.BIG_ENDIAN
, Encoding
.NONE
, null, 8);
70 FloatDeclaration flDec
= new FloatDeclaration(8, 24,
71 ByteOrder
.BIG_ENDIAN
, 8);
72 ArrayDeclaration arrDec
= new ArrayDeclaration(2, intDec
);
73 SequenceDeclaration seqDec
= new SequenceDeclaration(LEN
, intDec
);
74 StructDeclaration structDec
= new StructDeclaration(32);
75 EnumDeclaration enumDec
= new EnumDeclaration(intDec
);
76 VariantDeclaration varDec
= new VariantDeclaration();
77 sDec
.addField(INT
, intDec
);
78 sDec
.addField(LEN
, intDec
);
79 sDec
.addField(FLOAT
, flDec
);
80 sDec
.addField(STR
, strDec
);
81 sDec
.addField(ARRAY
, arrDec
);
82 sDec
.addField(SEQ
, seqDec
);
83 structDec
.addField(STR
, strDec
);
84 structDec
.addField(INT
, intDec
);
85 sDec
.addField(STRUCT
, structDec
);
86 enumDec
.add(0, 1, LEN
);
87 enumDec
.add(2, 3, FLOAT
);
88 sDec
.addField(ENUM
, enumDec
);
89 varDec
.addField(LEN
, intDec
);
90 varDec
.addField(FLOAT
, flDec
);
92 sDec
.addField(VARIANT
, varDec
);
93 fixture
= sDec
.createDefinition(fixture
, ROOT
);
95 java
.nio
.ByteBuffer bb
= java
.nio
.ByteBuffer
.allocateDirect(capacity
);
96 for (int i
= 0; i
< capacity
; i
++) {
106 fixture
.read(new BitBuffer(bb
));
110 * Run the CtfTmfEventField parseField(Definition,String) method test.
113 public void testParseField_float() {
114 FloatDefinition fieldDef
= (FloatDefinition
) fixture
.lookupDefinition(FLOAT
);
115 CtfTmfEventField result
= CtfTmfEventField
.parseField(fieldDef
, "_" + NAME
);
116 assertEquals("test=9.551467814359616E-38", result
.toString());
120 * Run the CtfTmfEventField parseField(Definition,String) method test.
123 public void testParseField_array() {
124 Definition fieldDef
= fixture
.lookupArray(ARRAY
);
125 CtfTmfEventField result
= CtfTmfEventField
.parseField(fieldDef
, NAME
);
126 assertEquals("test=[02, 02]", result
.toString());
130 * Run the CtfTmfEventField parseField(Definition,String) method test.
133 public void testParseField_int() {
134 Definition fieldDef
= fixture
.lookupDefinition(INT
);
135 CtfTmfEventField result
= CtfTmfEventField
.parseField(fieldDef
, NAME
);
136 assertEquals("test=02", result
.toString());
140 * Run the CtfTmfEventField parseField(Definition,String) method test.
143 public void testParseField_sequence() {
144 Definition fieldDef
= fixture
.lookupDefinition(SEQ
);
145 CtfTmfEventField result
= CtfTmfEventField
.parseField(fieldDef
, NAME
);
146 assertEquals("test=[02, 02]", result
.toString());
150 * Run the CtfTmfEventField parseField(Definition,String) method test.
153 public void testParseField_sequence_value() {
154 Definition fieldDef
= fixture
.lookupDefinition(SEQ
);
155 CtfTmfEventField result
= CtfTmfEventField
.parseField(fieldDef
, NAME
);
156 long[] values
= (long[]) result
.getValue();
157 long[] expected
= new long[] { 2, 2 };
158 assertArrayEquals(expected
, values
);
162 * Run the CtfTmfEventField parseField(Definition,String) method test.
165 public void testParseField_string() {
166 Definition fieldDef
= fixture
.lookupDefinition(STR
);
167 CtfTmfEventField result
= CtfTmfEventField
.parseField(fieldDef
, NAME
);
168 assertEquals("test=\ 2\ 2\ 2\ 2\ 2\ 2\ 2\ 2\ 2\ 2\ 2\ 2\ 2\ 2", result
.toString());
172 * Run the CtfTmfEventField parseField(Definition,String) method test.
175 public void testParseField_struct() {
176 Definition fieldDef
= fixture
.lookupDefinition(STRUCT
);
177 CtfTmfEventField result
= CtfTmfEventField
.parseField(fieldDef
, NAME
);
178 assertEquals("test=[str=\ 2\ 2\ 2\ 2\ 2\ 2\ 2\ 2\ 2\ 2\ 2\ 2, int=02]", result
.toString());
182 * Run the CtfTmfEventField parseField(Definition,String) method test.
185 public void testParseField_enum() {
186 Definition fieldDef
= fixture
.lookupDefinition(ENUM
);
187 CtfTmfEventField result
= CtfTmfEventField
.parseField(fieldDef
, NAME
);
188 assertEquals("test=float", result
.toString());
192 * Run the CtfTmfEventField parseField(Definition,String) method test.
195 public void testParseField_variant() {
196 Definition fieldDef
= fixture
.lookupDefinition(VARIANT
);
197 CtfTmfEventField result
= CtfTmfEventField
.parseField(fieldDef
, NAME
);
198 assertEquals("test=float=9.551467814359616E-38", result
.toString());