ctf: Throw CTFReaderException in the BitBuffer API
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.core.tests / src / org / eclipse / linuxtools / tmf / core / tests / ctfadaptor / CtfTmfEventFieldTest.java
1 /*******************************************************************************
2 * Copyright (c) 2012, 2013 Ericsson
3 *
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
8 *
9 * Contributors:
10 * Matthew Khouzam - Initial generation with CodePro tools
11 * Alexandre Montplaisir - Clean up, consolidate redundant tests
12 *******************************************************************************/
13
14 package org.eclipse.linuxtools.tmf.core.tests.ctfadaptor;
15
16 import static org.junit.Assert.assertEquals;
17 import static org.junit.Assert.assertArrayEquals;
18
19 import java.nio.ByteOrder;
20
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;
38
39 /**
40 * The class <code>CtfTmfEventFieldTest</code> contains tests for the class
41 * <code>{@link CtfTmfEventField}</code>.
42 *
43 * @author ematkho
44 * @version 1.0
45 */
46 public class CtfTmfEventFieldTest {
47
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";
59
60 private StructDefinition fixture;
61
62 /**
63 * Perform pre-test initialization.
64 * @throws CTFReaderException error
65 */
66 @Before
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);
93 varDec.setTag(ENUM);
94 sDec.addField(VARIANT, varDec);
95 fixture = sDec.createDefinition(fixture, ROOT);
96 int capacity = 2048;
97 java.nio.ByteBuffer bb = java.nio.ByteBuffer.allocateDirect(capacity);
98 for (int i = 0; i < capacity; i++) {
99 bb.put((byte) 2);
100 }
101 bb.position(20);
102 bb.put((byte) 0);
103 bb.position(40);
104 bb.put((byte) 0);
105 bb.position(60);
106 bb.put((byte) 0);
107 bb.position(0);
108 fixture.read(new BitBuffer(bb));
109 }
110
111 /**
112 * Run the CtfTmfEventField parseField(Definition,String) method test.
113 */
114 @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());
119 }
120
121 /**
122 * Run the CtfTmfEventField parseField(Definition,String) method test.
123 */
124 @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());
129 }
130
131 /**
132 * Run the CtfTmfEventField parseField(Definition,String) method test.
133 */
134 @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());
139 }
140
141 /**
142 * Run the CtfTmfEventField parseField(Definition,String) method test.
143 */
144 @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());
149 }
150
151 /**
152 * Run the CtfTmfEventField parseField(Definition,String) method test.
153 */
154 @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);
161 }
162
163 /**
164 * Run the CtfTmfEventField parseField(Definition,String) method test.
165 */
166 @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());
171 }
172
173 /**
174 * Run the CtfTmfEventField parseField(Definition,String) method test.
175 */
176 @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());
181 }
182
183 /**
184 * Run the CtfTmfEventField parseField(Definition,String) method test.
185 */
186 @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());
191 }
192
193 /**
194 * Run the CtfTmfEventField parseField(Definition,String) method test.
195 */
196 @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());
201 }
202 }
This page took 0.053818 seconds and 6 git commands to generate.