TMF: Add utility method to return trace set, including experiment
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.ctf.core.tests / src / org / eclipse / linuxtools / tmf / ctf / core / tests / CtfTmfEventFieldTest.java
CommitLineData
95bf10e7 1/*******************************************************************************
61759503 2 * Copyright (c) 2012, 2013 Ericsson
95bf10e7
AM
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
91e7f946 14package org.eclipse.linuxtools.tmf.ctf.core.tests;
81c8e6f7 15
cefe3edf 16import static org.junit.Assert.assertArrayEquals;
4d384009 17import static org.junit.Assert.assertEquals;
81c8e6f7 18
4d384009
FD
19import java.io.UnsupportedEncodingException;
20import java.nio.ByteBuffer;
81c8e6f7
MK
21import java.nio.ByteOrder;
22
c1831960 23import org.eclipse.jdt.annotation.NonNull;
486efb2e 24import org.eclipse.linuxtools.ctf.core.event.io.BitBuffer;
81c8e6f7 25import org.eclipse.linuxtools.ctf.core.event.types.Definition;
404b264a 26import org.eclipse.linuxtools.ctf.core.event.types.EnumDeclaration;
81c8e6f7
MK
27import org.eclipse.linuxtools.ctf.core.event.types.FloatDeclaration;
28import org.eclipse.linuxtools.ctf.core.event.types.FloatDefinition;
29import org.eclipse.linuxtools.ctf.core.event.types.IntegerDeclaration;
81c8e6f7
MK
30import org.eclipse.linuxtools.ctf.core.event.types.StringDeclaration;
31import org.eclipse.linuxtools.ctf.core.event.types.StructDeclaration;
32import org.eclipse.linuxtools.ctf.core.event.types.StructDefinition;
404b264a 33import org.eclipse.linuxtools.ctf.core.event.types.VariantDeclaration;
db8e8f7d 34import org.eclipse.linuxtools.ctf.core.trace.CTFReaderException;
7b4f13e6
MK
35import org.eclipse.linuxtools.internal.ctf.core.event.types.ArrayDeclaration;
36import org.eclipse.linuxtools.internal.ctf.core.event.types.SequenceDeclaration;
91e7f946 37import org.eclipse.linuxtools.tmf.ctf.core.CtfTmfEventField;
81c8e6f7
MK
38import org.junit.Before;
39import org.junit.Test;
40
41/**
42 * The class <code>CtfTmfEventFieldTest</code> contains tests for the class
43 * <code>{@link CtfTmfEventField}</code>.
44 *
a4fa4e36 45 * @author Matthew Khouzam
95bf10e7 46 * @version 1.0
81c8e6f7
MK
47 */
48public class CtfTmfEventFieldTest {
49
c1831960 50 private static final @NonNull String ROOT = "root";
68b18f2f 51 private static final String SEQ = "seq";
166eb6c4
GB
52 private static final @NonNull String ARRAY_STR = "array_str";
53 private static final @NonNull String ARRAY_FLOAT = "array_float";
54 private static final @NonNull String ARRAY_INT = "array_int";
55 private static final @NonNull String ARRAY_STRUCT = "array_struct";
56 private static final @NonNull String ARRAY_VARIANT = "array_variant";
57 private static final @NonNull String ARRAY_ENUM = "array_enum";
68b18f2f
AM
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";
7a6cee1a 63 private static final String STRUCT = "struct";
404b264a
GB
64 private static final String VARIANT = "variant";
65 private static final String ENUM = "enum";
81c8e6f7 66
4d384009
FD
67 private static final byte TEST_NUMBER = 2;
68 private static final String TEST_STRING = "two";
69
4591bed9
FD
70 private static final int ARRAY_SIZE = 2;
71
95bf10e7
AM
72 private StructDefinition fixture;
73
95bf10e7
AM
74 /**
75 * Perform pre-test initialization.
4d384009
FD
76 *
77 * @throws UnsupportedEncodingException
78 * Thrown when UTF-8 encoding is not available.
79 * @throws CTFReaderException
80 * error
95bf10e7
AM
81 */
82 @Before
4d384009
FD
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
95bf10e7
AM
89 StructDeclaration sDec = new StructDeclaration(1l);
90 StringDeclaration strDec = new StringDeclaration();
a4fa4e36 91 IntegerDeclaration intDec = IntegerDeclaration.UINT_8_DECL;
95bf10e7
AM
92 FloatDeclaration flDec = new FloatDeclaration(8, 24,
93 ByteOrder.BIG_ENDIAN, 8);
95bf10e7 94 SequenceDeclaration seqDec = new SequenceDeclaration(LEN, intDec);
4d384009 95 StructDeclaration structDec = new StructDeclaration(8);
404b264a
GB
96 EnumDeclaration enumDec = new EnumDeclaration(intDec);
97 VariantDeclaration varDec = new VariantDeclaration();
4591bed9
FD
98 ArrayDeclaration arrStrDec = new ArrayDeclaration(ARRAY_SIZE, strDec);
99 ArrayDeclaration arrFloatDec = new ArrayDeclaration(ARRAY_SIZE, flDec);
100 ArrayDeclaration arrIntDec = new ArrayDeclaration(ARRAY_SIZE, intDec);
101 ArrayDeclaration arrStructDec = new ArrayDeclaration(ARRAY_SIZE, structDec);
102 ArrayDeclaration arrVariantDec = new ArrayDeclaration(ARRAY_SIZE, varDec);
103 ArrayDeclaration arrEnumDec = new ArrayDeclaration(ARRAY_SIZE, enumDec);
4d384009 104
95bf10e7 105 sDec.addField(INT, intDec);
4d384009
FD
106 bb.put(TEST_NUMBER);
107
4591bed9
FD
108 sDec.addField(ARRAY_INT, arrIntDec);
109 for (int i = 0; i < ARRAY_SIZE; ++i) {
110 bb.put(TEST_NUMBER);
111 }
112
95bf10e7 113 sDec.addField(LEN, intDec);
4d384009
FD
114 bb.put(TEST_NUMBER);
115
95bf10e7 116 sDec.addField(FLOAT, flDec);
4d384009
FD
117 bb.putFloat(TEST_NUMBER);
118
4591bed9
FD
119 sDec.addField(ARRAY_FLOAT, arrFloatDec);
120 for (int i = 0; i < ARRAY_SIZE; ++i) {
121 bb.putFloat(TEST_NUMBER);
122 }
123
95bf10e7 124 sDec.addField(STR, strDec);
4d384009
FD
125 bb.put(testStringBytes);
126 bb.put((byte) 0);
127
4591bed9
FD
128 sDec.addField(ARRAY_STR, arrStrDec);
129 for (int i = 0; i < ARRAY_SIZE; ++i) {
130 bb.put(testStringBytes);
131 bb.put((byte) 0);
132 }
4d384009 133
95bf10e7 134 sDec.addField(SEQ, seqDec);
4d384009
FD
135 bb.put(TEST_NUMBER);
136 bb.put(TEST_NUMBER);
137
459f705b 138 structDec.addField(STR, strDec);
7a6cee1a
GB
139 structDec.addField(INT, intDec);
140 sDec.addField(STRUCT, structDec);
4d384009
FD
141 bb.put(testStringBytes);
142 bb.put((byte) 0);
143 bb.put(TEST_NUMBER);
144
4591bed9
FD
145 sDec.addField(ARRAY_STRUCT, arrStructDec);
146 for (int i = 0; i < ARRAY_SIZE; ++i) {
147 bb.put(testStringBytes);
148 bb.put((byte) 0);
149 bb.put(TEST_NUMBER);
150 }
151
404b264a
GB
152 enumDec.add(0, 1, LEN);
153 enumDec.add(2, 3, FLOAT);
459f705b 154 sDec.addField(ENUM, enumDec);
4d384009
FD
155 bb.put(TEST_NUMBER);
156
4591bed9
FD
157 sDec.addField(ARRAY_ENUM, arrEnumDec);
158 for (int i = 0; i < ARRAY_SIZE; ++i) {
159 bb.put(TEST_NUMBER);
160 }
161
404b264a
GB
162 varDec.addField(LEN, intDec);
163 varDec.addField(FLOAT, flDec);
164 varDec.setTag(ENUM);
165 sDec.addField(VARIANT, varDec);
4d384009
FD
166 bb.putFloat(TEST_NUMBER);
167
4591bed9
FD
168 sDec.addField(ARRAY_VARIANT, arrVariantDec);
169 for (int i = 0; i < ARRAY_SIZE; ++i) {
170 bb.putFloat(TEST_NUMBER);
171 }
172
a4fa4e36 173 fixture = sDec.createDefinition(fixture, ROOT, new BitBuffer(bb));
4d384009 174
95bf10e7
AM
175 }
176
95bf10e7
AM
177 /**
178 * Run the CtfTmfEventField parseField(Definition,String) method test.
81c8e6f7
MK
179 */
180 @Test
95bf10e7 181 public void testParseField_float() {
68b18f2f
AM
182 FloatDefinition fieldDef = (FloatDefinition) fixture.lookupDefinition(FLOAT);
183 CtfTmfEventField result = CtfTmfEventField.parseField(fieldDef, "_" + NAME);
4d384009 184 assertEquals("test=2.0", result.toString());
81c8e6f7
MK
185 }
186
187 /**
4591bed9
FD
188 * Run the CtfTmfEventField parseField(Definition,String) method test for an
189 * array of floats field.
81c8e6f7
MK
190 */
191 @Test
4591bed9 192 public void testParseField_array_float() {
70f60307 193 Definition fieldDef = fixture.lookupArrayDefinition(ARRAY_FLOAT);
68b18f2f 194 CtfTmfEventField result = CtfTmfEventField.parseField(fieldDef, NAME);
4591bed9 195 assertEquals("test=[2.0, 2.0]", result.toString());
81c8e6f7
MK
196 }
197
198 /**
199 * Run the CtfTmfEventField parseField(Definition,String) method test.
81c8e6f7
MK
200 */
201 @Test
95bf10e7 202 public void testParseField_int() {
81c8e6f7
MK
203 Definition fieldDef = fixture.lookupDefinition(INT);
204 CtfTmfEventField result = CtfTmfEventField.parseField(fieldDef, NAME);
a4fa4e36 205 assertEquals("test=2", result.toString());
81c8e6f7
MK
206 }
207
4591bed9
FD
208 /**
209 * Run the CtfTmfEventField parseField(Definition,String) method test for an
210 * array of integers field.
211 */
212 @Test
213 public void testParseField_array_int() {
70f60307 214 Definition fieldDef = fixture.lookupArrayDefinition(ARRAY_INT);
4591bed9 215 CtfTmfEventField result = CtfTmfEventField.parseField(fieldDef, NAME);
a4fa4e36 216 assertEquals("test=[2, 2]", result.toString());
4591bed9
FD
217 }
218
81c8e6f7
MK
219 /**
220 * Run the CtfTmfEventField parseField(Definition,String) method test.
81c8e6f7
MK
221 */
222 @Test
95bf10e7 223 public void testParseField_sequence() {
81c8e6f7
MK
224 Definition fieldDef = fixture.lookupDefinition(SEQ);
225 CtfTmfEventField result = CtfTmfEventField.parseField(fieldDef, NAME);
a4fa4e36 226 assertEquals("test=[2, 2]", result.toString());
a6223d74
MK
227 }
228
229 /**
230 * Run the CtfTmfEventField parseField(Definition,String) method test.
231 */
232 @Test
233 public void testParseField_sequence_value() {
234 Definition fieldDef = fixture.lookupDefinition(SEQ);
235 CtfTmfEventField result = CtfTmfEventField.parseField(fieldDef, NAME);
cefe3edf
AM
236 long[] values = (long[]) result.getValue();
237 long[] expected = new long[] { 2, 2 };
238 assertArrayEquals(expected, values);
81c8e6f7
MK
239 }
240
241 /**
242 * Run the CtfTmfEventField parseField(Definition,String) method test.
81c8e6f7
MK
243 */
244 @Test
95bf10e7 245 public void testParseField_string() {
81c8e6f7
MK
246 Definition fieldDef = fixture.lookupDefinition(STR);
247 CtfTmfEventField result = CtfTmfEventField.parseField(fieldDef, NAME);
4d384009 248 assertEquals("test=two", result.toString());
81c8e6f7 249 }
7a6cee1a 250
4591bed9
FD
251 /**
252 * Run the CtfTmfEventField parseField(Definition,String) method test for an
253 * array of strings field.
254 */
255 @Test
256 public void testParseField_array_string() {
70f60307 257 Definition fieldDef = fixture.lookupArrayDefinition(ARRAY_STR);
4591bed9
FD
258 CtfTmfEventField result = CtfTmfEventField.parseField(fieldDef, NAME);
259 assertEquals("test=[two, two]", result.toString());
260 }
261
7a6cee1a
GB
262 /**
263 * Run the CtfTmfEventField parseField(Definition,String) method test.
264 */
265 @Test
266 public void testParseField_struct() {
267 Definition fieldDef = fixture.lookupDefinition(STRUCT);
268 CtfTmfEventField result = CtfTmfEventField.parseField(fieldDef, NAME);
a4fa4e36 269 assertEquals("test=[str=two, int=2]", result.toString());
7a6cee1a 270 }
404b264a 271
4591bed9
FD
272 /**
273 * Run the CtfTmfEventField parseField(Definition,String) method test for an
274 * array of structs field.
275 */
276 @Test
277 public void testParseField_array_struct() {
70f60307 278 Definition fieldDef = fixture.lookupArrayDefinition(ARRAY_STRUCT);
4591bed9 279 CtfTmfEventField result = CtfTmfEventField.parseField(fieldDef, NAME);
a4fa4e36 280 assertEquals("test=[[str=two, int=2], [str=two, int=2]]", result.toString());
4591bed9
FD
281 }
282
404b264a
GB
283 /**
284 * Run the CtfTmfEventField parseField(Definition,String) method test.
285 */
286 @Test
287 public void testParseField_enum() {
288 Definition fieldDef = fixture.lookupDefinition(ENUM);
289 CtfTmfEventField result = CtfTmfEventField.parseField(fieldDef, NAME);
290 assertEquals("test=float", result.toString());
291 }
292
4591bed9
FD
293 /**
294 * Run the CtfTmfEventField parseField(Definition,String) method test for an
295 * array of enums field.
296 */
297 @Test
298 public void testParseField_array_enum() {
70f60307 299 Definition fieldDef = fixture.lookupArrayDefinition(ARRAY_ENUM);
4591bed9
FD
300 CtfTmfEventField result = CtfTmfEventField.parseField(fieldDef, NAME);
301 assertEquals("test=[float, float]", result.toString());
302 }
303
404b264a
GB
304 /**
305 * Run the CtfTmfEventField parseField(Definition,String) method test.
306 */
307 @Test
308 public void testParseField_variant() {
309 Definition fieldDef = fixture.lookupDefinition(VARIANT);
310 CtfTmfEventField result = CtfTmfEventField.parseField(fieldDef, NAME);
4d384009 311 assertEquals("test=float=2.0", result.toString());
404b264a 312 }
4591bed9
FD
313
314 /**
315 * Run the CtfTmfEventField parseField(Definition,String) method test for an
316 * array of variants field.
317 */
318 @Test
319 public void testParseField_array_variant() {
70f60307 320 Definition fieldDef = fixture.lookupArrayDefinition(ARRAY_VARIANT);
4591bed9
FD
321 CtfTmfEventField result = CtfTmfEventField.parseField(fieldDef, NAME);
322 assertEquals("test=[float=2.0, float=2.0]", result.toString());
323 }
95bf10e7 324}
This page took 0.059393 seconds and 5 git commands to generate.