LTTng: CPU usage analysis from the LTTng kernel trace
[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.assertArrayEquals;
17 import static org.junit.Assert.assertEquals;
18
19 import java.io.UnsupportedEncodingException;
20 import java.nio.ByteBuffer;
21 import java.nio.ByteOrder;
22
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;
40
41 /**
42 * The class <code>CtfTmfEventFieldTest</code> contains tests for the class
43 * <code>{@link CtfTmfEventField}</code>.
44 *
45 * @author ematkho
46 * @version 1.0
47 */
48 public class CtfTmfEventFieldTest {
49
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";
66
67 private static final byte TEST_NUMBER = 2;
68 private static final String TEST_STRING = "two";
69
70 private static final int ARRAY_SIZE = 2;
71
72 private StructDefinition fixture;
73
74 /**
75 * Perform pre-test initialization.
76 *
77 * @throws UnsupportedEncodingException
78 * Thrown when UTF-8 encoding is not available.
79 * @throws CTFReaderException
80 * error
81 */
82 @Before
83 public void setUp() throws UnsupportedEncodingException, CTFReaderException {
84 final byte[] testStringBytes = TEST_STRING.getBytes("UTF-8");
85
86 int capacity = 2048;
87 ByteBuffer bb = ByteBuffer.allocateDirect(capacity);
88
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);
105
106 sDec.addField(INT, intDec);
107 bb.put(TEST_NUMBER);
108
109 sDec.addField(ARRAY_INT, arrIntDec);
110 for (int i = 0; i < ARRAY_SIZE; ++i) {
111 bb.put(TEST_NUMBER);
112 }
113
114 sDec.addField(LEN, intDec);
115 bb.put(TEST_NUMBER);
116
117 sDec.addField(FLOAT, flDec);
118 bb.putFloat(TEST_NUMBER);
119
120 sDec.addField(ARRAY_FLOAT, arrFloatDec);
121 for (int i = 0; i < ARRAY_SIZE; ++i) {
122 bb.putFloat(TEST_NUMBER);
123 }
124
125 sDec.addField(STR, strDec);
126 bb.put(testStringBytes);
127 bb.put((byte) 0);
128
129 sDec.addField(ARRAY_STR, arrStrDec);
130 for (int i = 0; i < ARRAY_SIZE; ++i) {
131 bb.put(testStringBytes);
132 bb.put((byte) 0);
133 }
134
135 sDec.addField(SEQ, seqDec);
136 bb.put(TEST_NUMBER);
137 bb.put(TEST_NUMBER);
138
139 structDec.addField(STR, strDec);
140 structDec.addField(INT, intDec);
141 sDec.addField(STRUCT, structDec);
142 bb.put(testStringBytes);
143 bb.put((byte) 0);
144 bb.put(TEST_NUMBER);
145
146 sDec.addField(ARRAY_STRUCT, arrStructDec);
147 for (int i = 0; i < ARRAY_SIZE; ++i) {
148 bb.put(testStringBytes);
149 bb.put((byte) 0);
150 bb.put(TEST_NUMBER);
151 }
152
153 enumDec.add(0, 1, LEN);
154 enumDec.add(2, 3, FLOAT);
155 sDec.addField(ENUM, enumDec);
156 bb.put(TEST_NUMBER);
157
158 sDec.addField(ARRAY_ENUM, arrEnumDec);
159 for (int i = 0; i < ARRAY_SIZE; ++i) {
160 bb.put(TEST_NUMBER);
161 }
162
163 varDec.addField(LEN, intDec);
164 varDec.addField(FLOAT, flDec);
165 varDec.setTag(ENUM);
166 sDec.addField(VARIANT, varDec);
167 bb.putFloat(TEST_NUMBER);
168
169 sDec.addField(ARRAY_VARIANT, arrVariantDec);
170 for (int i = 0; i < ARRAY_SIZE; ++i) {
171 bb.putFloat(TEST_NUMBER);
172 }
173
174 fixture = sDec.createDefinition(fixture, ROOT);
175
176 bb.position(0);
177 fixture.read(new BitBuffer(bb));
178 }
179
180 /**
181 * Run the CtfTmfEventField parseField(Definition,String) method test.
182 */
183 @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());
188 }
189
190 /**
191 * Run the CtfTmfEventField parseField(Definition,String) method test for an
192 * array of floats field.
193 */
194 @Test
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());
199 }
200
201 /**
202 * Run the CtfTmfEventField parseField(Definition,String) method test.
203 */
204 @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());
209 }
210
211 /**
212 * Run the CtfTmfEventField parseField(Definition,String) method test for an
213 * array of integers field.
214 */
215 @Test
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());
220 }
221
222 /**
223 * Run the CtfTmfEventField parseField(Definition,String) method test.
224 */
225 @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());
230 }
231
232 /**
233 * Run the CtfTmfEventField parseField(Definition,String) method test.
234 */
235 @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);
242 }
243
244 /**
245 * Run the CtfTmfEventField parseField(Definition,String) method test.
246 */
247 @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());
252 }
253
254 /**
255 * Run the CtfTmfEventField parseField(Definition,String) method test for an
256 * array of strings field.
257 */
258 @Test
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());
263 }
264
265 /**
266 * Run the CtfTmfEventField parseField(Definition,String) method test.
267 */
268 @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());
273 }
274
275 /**
276 * Run the CtfTmfEventField parseField(Definition,String) method test for an
277 * array of structs field.
278 */
279 @Test
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());
284 }
285
286 /**
287 * Run the CtfTmfEventField parseField(Definition,String) method test.
288 */
289 @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());
294 }
295
296 /**
297 * Run the CtfTmfEventField parseField(Definition,String) method test for an
298 * array of enums field.
299 */
300 @Test
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());
305 }
306
307 /**
308 * Run the CtfTmfEventField parseField(Definition,String) method test.
309 */
310 @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());
315 }
316
317 /**
318 * Run the CtfTmfEventField parseField(Definition,String) method test for an
319 * array of variants field.
320 */
321 @Test
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());
326 }
327 }
This page took 0.05821 seconds and 5 git commands to generate.