tmf: Move plugins to the Trace Compass namespace
[deliverable/tracecompass.git] / org.eclipse.tracecompass.tmf.ctf.core.tests / src / org / eclipse / linuxtools / tmf / ctf / core / tests / 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.ctf.core.tests;
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.jdt.annotation.NonNull;
24 import org.eclipse.linuxtools.ctf.core.event.io.BitBuffer;
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.IDefinition;
29 import org.eclipse.linuxtools.ctf.core.event.types.IntegerDeclaration;
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.internal.ctf.core.event.types.ArrayDeclaration;
36 import org.eclipse.linuxtools.internal.ctf.core.event.types.SequenceDeclaration;
37 import org.eclipse.linuxtools.tmf.ctf.core.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 Matthew Khouzam
46 * @version 1.0
47 */
48 public class CtfTmfEventFieldTest {
49
50 private static final @NonNull String ROOT = "root";
51 private static final String SEQ = "seq";
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";
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 = IntegerDeclaration.UINT_8_DECL;
92 FloatDeclaration flDec = new FloatDeclaration(8, 24,
93 ByteOrder.BIG_ENDIAN, 8);
94 SequenceDeclaration seqDec = new SequenceDeclaration(LEN, intDec);
95 StructDeclaration structDec = new StructDeclaration(8);
96 EnumDeclaration enumDec = new EnumDeclaration(intDec);
97 VariantDeclaration varDec = new VariantDeclaration();
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);
104
105 sDec.addField(INT, intDec);
106 bb.put(TEST_NUMBER);
107
108 sDec.addField(ARRAY_INT, arrIntDec);
109 for (int i = 0; i < ARRAY_SIZE; ++i) {
110 bb.put(TEST_NUMBER);
111 }
112
113 sDec.addField(LEN, intDec);
114 bb.put(TEST_NUMBER);
115
116 sDec.addField(FLOAT, flDec);
117 bb.putFloat(TEST_NUMBER);
118
119 sDec.addField(ARRAY_FLOAT, arrFloatDec);
120 for (int i = 0; i < ARRAY_SIZE; ++i) {
121 bb.putFloat(TEST_NUMBER);
122 }
123
124 sDec.addField(STR, strDec);
125 bb.put(testStringBytes);
126 bb.put((byte) 0);
127
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 }
133
134 sDec.addField(SEQ, seqDec);
135 bb.put(TEST_NUMBER);
136 bb.put(TEST_NUMBER);
137
138 structDec.addField(STR, strDec);
139 structDec.addField(INT, intDec);
140 sDec.addField(STRUCT, structDec);
141 bb.put(testStringBytes);
142 bb.put((byte) 0);
143 bb.put(TEST_NUMBER);
144
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
152 enumDec.add(0, 1, LEN);
153 enumDec.add(2, 3, FLOAT);
154 sDec.addField(ENUM, enumDec);
155 bb.put(TEST_NUMBER);
156
157 sDec.addField(ARRAY_ENUM, arrEnumDec);
158 for (int i = 0; i < ARRAY_SIZE; ++i) {
159 bb.put(TEST_NUMBER);
160 }
161
162 varDec.addField(LEN, intDec);
163 varDec.addField(FLOAT, flDec);
164 varDec.setTag(ENUM);
165 sDec.addField(VARIANT, varDec);
166 bb.putFloat(TEST_NUMBER);
167
168 sDec.addField(ARRAY_VARIANT, arrVariantDec);
169 for (int i = 0; i < ARRAY_SIZE; ++i) {
170 bb.putFloat(TEST_NUMBER);
171 }
172
173 fixture = sDec.createDefinition(fixture, ROOT, new BitBuffer(bb));
174
175 }
176
177 /**
178 * Run the CtfTmfEventField parseField(Definition,String) method test.
179 */
180 @Test
181 public void testParseField_float() {
182 FloatDefinition fieldDef = (FloatDefinition) fixture.lookupDefinition(FLOAT);
183 CtfTmfEventField result = CtfTmfEventField.parseField((IDefinition)fieldDef, "_" + NAME);
184 assertEquals("test=2.0", result.toString());
185 }
186
187 /**
188 * Run the CtfTmfEventField parseField(Definition,String) method test for an
189 * array of floats field.
190 */
191 @Test
192 public void testParseField_array_float() {
193 IDefinition fieldDef = fixture.lookupArrayDefinition(ARRAY_FLOAT);
194 CtfTmfEventField result = CtfTmfEventField.parseField(fieldDef, NAME);
195 assertEquals("test=[2.0, 2.0]", result.toString());
196 }
197
198 /**
199 * Run the CtfTmfEventField parseField(Definition,String) method test.
200 */
201 @Test
202 public void testParseField_int() {
203 IDefinition fieldDef = fixture.lookupDefinition(INT);
204 CtfTmfEventField result = CtfTmfEventField.parseField(fieldDef, NAME);
205 assertEquals("test=2", result.toString());
206 }
207
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() {
214 IDefinition fieldDef = fixture.lookupArrayDefinition(ARRAY_INT);
215 CtfTmfEventField result = CtfTmfEventField.parseField(fieldDef, NAME);
216 assertEquals("test=[2, 2]", result.toString());
217 }
218
219 /**
220 * Run the CtfTmfEventField parseField(Definition,String) method test.
221 */
222 @Test
223 public void testParseField_sequence() {
224 IDefinition fieldDef = fixture.lookupDefinition(SEQ);
225 CtfTmfEventField result = CtfTmfEventField.parseField(fieldDef, NAME);
226 assertEquals("test=[2, 2]", result.toString());
227 }
228
229 /**
230 * Run the CtfTmfEventField parseField(Definition,String) method test.
231 */
232 @Test
233 public void testParseField_sequence_value() {
234 IDefinition fieldDef = fixture.lookupDefinition(SEQ);
235 CtfTmfEventField result = CtfTmfEventField.parseField(fieldDef, NAME);
236 long[] values = (long[]) result.getValue();
237 long[] expected = new long[] { 2, 2 };
238 assertArrayEquals(expected, values);
239 }
240
241 /**
242 * Run the CtfTmfEventField parseField(Definition,String) method test.
243 */
244 @Test
245 public void testParseField_string() {
246 IDefinition fieldDef = fixture.lookupDefinition(STR);
247 CtfTmfEventField result = CtfTmfEventField.parseField(fieldDef, NAME);
248 assertEquals("test=two", result.toString());
249 }
250
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() {
257 IDefinition fieldDef = fixture.lookupArrayDefinition(ARRAY_STR);
258 CtfTmfEventField result = CtfTmfEventField.parseField(fieldDef, NAME);
259 assertEquals("test=[two, two]", result.toString());
260 }
261
262 /**
263 * Run the CtfTmfEventField parseField(Definition,String) method test.
264 */
265 @Test
266 public void testParseField_struct() {
267 IDefinition fieldDef = fixture.lookupDefinition(STRUCT);
268 CtfTmfEventField result = CtfTmfEventField.parseField(fieldDef, NAME);
269 assertEquals("test=[str=two, int=2]", result.toString());
270 }
271
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() {
278 IDefinition fieldDef = fixture.lookupArrayDefinition(ARRAY_STRUCT);
279 CtfTmfEventField result = CtfTmfEventField.parseField(fieldDef, NAME);
280 assertEquals("test=[[str=two, int=2], [str=two, int=2]]", result.toString());
281 }
282
283 /**
284 * Run the CtfTmfEventField parseField(Definition,String) method test.
285 */
286 @Test
287 public void testParseField_enum() {
288 IDefinition fieldDef = fixture.lookupDefinition(ENUM);
289 CtfTmfEventField result = CtfTmfEventField.parseField(fieldDef, NAME);
290 assertEquals("test=float", result.toString());
291 }
292
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() {
299 IDefinition fieldDef = fixture.lookupArrayDefinition(ARRAY_ENUM);
300 CtfTmfEventField result = CtfTmfEventField.parseField(fieldDef, NAME);
301 assertEquals("test=[float, float]", result.toString());
302 }
303
304 /**
305 * Run the CtfTmfEventField parseField(Definition,String) method test.
306 */
307 @Test
308 public void testParseField_variant() {
309 IDefinition fieldDef = fixture.lookupDefinition(VARIANT);
310 CtfTmfEventField result = CtfTmfEventField.parseField(fieldDef, NAME);
311 assertEquals("test=float=2.0", result.toString());
312 }
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() {
320 IDefinition fieldDef = fixture.lookupArrayDefinition(ARRAY_VARIANT);
321 CtfTmfEventField result = CtfTmfEventField.parseField(fieldDef, NAME);
322 assertEquals("test=[float=2.0, float=2.0]", result.toString());
323 }
324 }
This page took 0.039534 seconds and 5 git commands to generate.