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