6a7b5b0b75a2a5fc4c03c663a10e81eee8598174
[deliverable/tracecompass.git] / org.eclipse.linuxtools.ctf.core.tests / src / org / eclipse / linuxtools / ctf / core / tests / types / EventHeaderDeclarationTest.java
1 /*******************************************************************************
2 * Copyright (c) 2014 Ericsson
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
7 *
8 * Contributors:
9 * Matthew Khouzam - Initial API and implementation
10 *******************************************************************************/
11 package org.eclipse.linuxtools.ctf.core.tests.types;
12
13 import static org.junit.Assert.assertEquals;
14 import static org.junit.Assert.assertNotNull;
15
16 import java.nio.ByteBuffer;
17 import java.nio.ByteOrder;
18 import java.util.ArrayList;
19 import java.util.List;
20
21 import org.eclipse.linuxtools.ctf.core.event.io.BitBuffer;
22 import org.eclipse.linuxtools.ctf.core.event.types.EnumDeclaration;
23 import org.eclipse.linuxtools.ctf.core.event.types.FloatDeclaration;
24 import org.eclipse.linuxtools.ctf.core.event.types.IntegerDeclaration;
25 import org.eclipse.linuxtools.ctf.core.event.types.IntegerDefinition;
26 import org.eclipse.linuxtools.ctf.core.event.types.StringDeclaration;
27 import org.eclipse.linuxtools.ctf.core.event.types.StructDeclaration;
28 import org.eclipse.linuxtools.ctf.core.event.types.VariantDeclaration;
29 import org.eclipse.linuxtools.ctf.core.trace.CTFReaderException;
30 import org.eclipse.linuxtools.internal.ctf.core.event.types.composite.EventHeaderCompactDeclaration;
31 import org.eclipse.linuxtools.internal.ctf.core.event.types.composite.EventHeaderDefinition;
32 import org.eclipse.linuxtools.internal.ctf.core.event.types.composite.EventHeaderLargeDeclaration;
33 import org.junit.Before;
34 import org.junit.Test;
35
36 /**
37 * Event header declaration tests
38 *
39 * @author Matthew Khouzam
40 *
41 */
42 public class EventHeaderDeclarationTest {
43
44 private static final int ID = 2222;
45 private static final int TIMESTAMP = 1000;
46 private static final int VALID_LARGE = 1;
47 private static final int VALID_COMPACT = 0;
48
49 private final List<StructDeclaration> declarations = new ArrayList<>();
50
51 /**
52 * Setup
53 */
54 @Before
55 public void init() {
56 declarations.clear();
57
58 /**
59 * do not reflow
60 *
61 * <pre>
62 * struct event_header_compact {
63 * enum : uint5_t { compact = 0 ... 30, extended = 31 } id;
64 * variant <id> {
65 * struct {
66 * uint27_clock_monotonic_t timestamp;
67 * } compact;
68 * struct {
69 * uint32_t id;
70 * uint64_clock_monotonic_t timestamp;
71 * } extended;
72 * } v;
73 * } align(8);
74 * </pre>
75 */
76
77 StructDeclaration base = new StructDeclaration(8);
78 base.addField("id", new EnumDeclaration(IntegerDeclaration.UINT_5B_DECL));
79 VariantDeclaration variantV = new VariantDeclaration();
80 StructDeclaration compact = new StructDeclaration(8);
81 compact.addField("timestamp", IntegerDeclaration.UINT_27B_DECL);
82 variantV.addField("compact", compact);
83 StructDeclaration large = new StructDeclaration(8);
84 large.addField("id", IntegerDeclaration.UINT_32B_DECL);
85 large.addField("timestamp", IntegerDeclaration.UINT_64B_DECL);
86 variantV.addField("extended", large);
87 base.addField("v", variantV);
88 declarations.add(base);
89
90 /**
91 * Do not reflow
92 *
93 * <pre>
94 * struct event_header_large {
95 * enum : uint16_t { compact = 0 ... 65534, extended = 65535 } id;
96 * variant <id> {
97 * struct {
98 * uint32_clock_monotonic_t timestamp;
99 * } compact;
100 * struct {
101 * uint32_t id;
102 * uint64_clock_monotonic_t timestamp;
103 * } extended;
104 * } v;
105 * } align(8);
106 * </pre>
107 */
108
109 base = new StructDeclaration(8);
110 base.addField("id", new EnumDeclaration(IntegerDeclaration.UINT_16B_DECL));
111 variantV = new VariantDeclaration();
112 compact = new StructDeclaration(8);
113 compact.addField("timestamp", IntegerDeclaration.UINT_32B_DECL);
114 variantV.addField("compact", compact);
115 large = new StructDeclaration(8);
116 large.addField("id", IntegerDeclaration.UINT_32B_DECL);
117 large.addField("timestamp", IntegerDeclaration.UINT_64B_DECL);
118 variantV.addField("extended", large);
119 base.addField("v", variantV);
120 declarations.add(base);
121
122 // bad - well, sounds nice though
123 base = new StructDeclaration(8);
124 base.addField("potato salad", new FloatDeclaration(8, 8, ByteOrder.BIG_ENDIAN, 8));
125 base.addField("bbq ribs", new FloatDeclaration(8, 8, ByteOrder.BIG_ENDIAN, 8));
126 declarations.add(base);
127 // bad
128 base = new StructDeclaration(8);
129 base.addField("id", new EnumDeclaration(IntegerDeclaration.UINT_16B_DECL));
130 base.addField("v", new FloatDeclaration(8, 8, ByteOrder.BIG_ENDIAN, 8));
131 declarations.add(base);
132 // bad
133 base = new StructDeclaration(8);
134 base.addField("id", new EnumDeclaration(IntegerDeclaration.UINT_5B_DECL));
135 base.addField("v", new FloatDeclaration(8, 8, ByteOrder.BIG_ENDIAN, 8));
136 declarations.add(base);
137 // bad
138 base = new StructDeclaration(8);
139 base.addField("id", new EnumDeclaration(IntegerDeclaration.UINT_5B_DECL));
140 variantV = new VariantDeclaration();
141 compact = new StructDeclaration(8);
142 compact.addField("timestamp", IntegerDeclaration.UINT_27B_DECL);
143 variantV.addField("compact1", compact);
144 large = new StructDeclaration(8);
145 large.addField("id", IntegerDeclaration.UINT_32B_DECL);
146 large.addField("timestamp", IntegerDeclaration.UINT_64B_DECL);
147 variantV.addField("extended", large);
148 base.addField("v", variantV);
149 declarations.add(base);
150
151 // bad
152 base = new StructDeclaration(8);
153 base.addField("id", new EnumDeclaration(IntegerDeclaration.UINT_5B_DECL));
154 variantV = new VariantDeclaration();
155 compact = new StructDeclaration(8);
156 compact.addField("timestamp", IntegerDeclaration.UINT_27B_DECL);
157 variantV.addField("compact", compact);
158 large = new StructDeclaration(8);
159 large.addField("id", IntegerDeclaration.UINT_32B_DECL);
160 large.addField("timestamp1", IntegerDeclaration.UINT_64B_DECL);
161 variantV.addField("extended", large);
162 base.addField("v", variantV);
163 declarations.add(base);
164
165 // bad
166 base = new StructDeclaration(8);
167 base.addField("id", new EnumDeclaration(IntegerDeclaration.UINT_5B_DECL));
168 variantV = new VariantDeclaration();
169 compact = new StructDeclaration(8);
170 compact.addField("timestamp", IntegerDeclaration.UINT_27B_DECL);
171 variantV.addField("compact", compact);
172 large = new StructDeclaration(8);
173 large.addField("id", IntegerDeclaration.UINT_32B_DECL);
174 large.addField("timestamp", new StringDeclaration());
175 variantV.addField("extended", large);
176 base.addField("v", variantV);
177 declarations.add(base);
178
179 // bad
180 base = new StructDeclaration(8);
181 base.addField("id", new EnumDeclaration(IntegerDeclaration.UINT_5B_DECL));
182 variantV = new VariantDeclaration();
183 compact = new StructDeclaration(8);
184 compact.addField("timestamp", IntegerDeclaration.UINT_27B_DECL);
185 variantV.addField("compact", compact);
186 variantV.addField("surprise!", compact);
187 large = new StructDeclaration(8);
188 large.addField("id", IntegerDeclaration.UINT_32B_DECL);
189 large.addField("timestamp", new StringDeclaration());
190 variantV.addField("extended", large);
191 base.addField("v", variantV);
192 declarations.add(base);
193
194 // bad
195 base = new StructDeclaration(8);
196 base.addField("id", new EnumDeclaration(IntegerDeclaration.UINT_16B_DECL));
197 variantV = new VariantDeclaration();
198 compact = new StructDeclaration(8);
199 compact.addField("timestamp", IntegerDeclaration.UINT_27B_DECL);
200 variantV.addField("compact", compact);
201 variantV.addField("surprise!", compact);
202 large = new StructDeclaration(8);
203 large.addField("id", IntegerDeclaration.UINT_32B_DECL);
204 large.addField("timestamp", new StringDeclaration());
205 variantV.addField("extended", large);
206 base.addField("v", variantV);
207 declarations.add(base);
208 // bad
209 base = new StructDeclaration(8);
210 base.addField("id", new FloatDeclaration(8, 8, ByteOrder.BIG_ENDIAN, 8));
211 base.addField("v", new FloatDeclaration(8, 8, ByteOrder.BIG_ENDIAN, 8));
212 declarations.add(base);
213 // bad
214 base = new StructDeclaration(8);
215 base.addField("id", IntegerDeclaration.INT_32B_DECL);
216 base.addField("timestamp", IntegerDeclaration.INT_32B_DECL);
217 declarations.add(base);
218 // bad
219 base = new StructDeclaration(8);
220 base.addField("id", new EnumDeclaration(IntegerDeclaration.INT_8_DECL));
221 base.addField("timestamp", IntegerDeclaration.INT_32B_DECL);
222 declarations.add(base);
223 }
224
225 /**
226 * Validate a compact declaration
227 */
228 @Test
229 public void validateCompact() {
230 assertEquals(true, EventHeaderCompactDeclaration.isCompactEventHeader(declarations.get(VALID_COMPACT)));
231 }
232
233 /**
234 * Fail if it validates
235 */
236 @Test
237 public void validateCompactFail() {
238 for (int i = 0; i < declarations.size(); i++) {
239 if (i == VALID_COMPACT) {
240 continue;
241 }
242 assertEquals(false, EventHeaderCompactDeclaration.isCompactEventHeader(declarations.get(i)));
243 }
244 }
245
246 /**
247 * Validate a large declaration
248 */
249 @Test
250 public void validateLarge() {
251 assertEquals(true, EventHeaderLargeDeclaration.isLargeEventHeader(declarations.get(VALID_LARGE)));
252 }
253
254 /**
255 * Fail if it validates
256 */
257 @Test
258 public void validateLargeFail() {
259 for (int i = 0; i < declarations.size(); i++) {
260 if (i == VALID_LARGE) {
261 continue;
262 }
263 assertEquals(false, EventHeaderLargeDeclaration.isLargeEventHeader(declarations.get(i)));
264 }
265 }
266
267 /**
268 * Test an compact compact header
269 *
270 * @throws CTFReaderException
271 * if {@link BitBuffer} is null
272 */
273 @Test
274 public void testCompactCompact() throws CTFReaderException {
275 ByteBuffer buffer = ByteBuffer.allocate(16);
276 buffer.putInt(0x80000042);
277 byte[] validCompact1 = buffer.array();
278
279 EventHeaderCompactDeclaration decl = new EventHeaderCompactDeclaration(ByteOrder.BIG_ENDIAN);
280 final ByteBuffer input = ByteBuffer.wrap(validCompact1);
281 assertNotNull(input);
282 EventHeaderDefinition def = decl.createDefinition(null, "bla", new BitBuffer(input));
283 assertNotNull(def);
284 assertEquals(16, def.getId());
285 assertEquals(0x42, def.getTimestamp());
286 }
287
288 /**
289 * Test an extended compact header
290 *
291 * @throws CTFReaderException
292 * if {@link BitBuffer} is null
293 */
294 @Test
295 public void testCompactExtended() throws CTFReaderException {
296 ByteBuffer buffer = ByteBuffer.allocate(16);
297 buffer.put((byte) 0xFF);
298 buffer.putInt(ID);
299 buffer.putLong(TIMESTAMP);
300 byte[] validCompact2 = buffer.array();
301
302 EventHeaderCompactDeclaration decl = new EventHeaderCompactDeclaration(ByteOrder.BIG_ENDIAN);
303 final ByteBuffer input = ByteBuffer.wrap(validCompact2);
304 assertNotNull(input);
305 EventHeaderDefinition def = decl.createDefinition(null, "bla", new BitBuffer(input));
306 assertNotNull(def);
307 assertEquals(ID, def.getId());
308 assertEquals(TIMESTAMP, def.getTimestamp());
309 }
310
311 /**
312 * Test an compact large header
313 *
314 * @throws CTFReaderException
315 * if {@link BitBuffer} is null
316 */
317 @Test
318 public void testLargeCompact() throws CTFReaderException {
319 ByteBuffer buffer = ByteBuffer.allocate(16);
320 buffer.putShort((short) ID);
321 buffer.putInt(TIMESTAMP);
322 byte[] validLarge1 = buffer.array();
323
324 EventHeaderLargeDeclaration decl = new EventHeaderLargeDeclaration(ByteOrder.BIG_ENDIAN);
325 final ByteBuffer input = ByteBuffer.wrap(validLarge1);
326 assertNotNull(input);
327 EventHeaderDefinition def = decl.createDefinition(null, "bla", new BitBuffer(input));
328 assertNotNull(def);
329 assertEquals(ID, def.getId());
330 assertEquals(TIMESTAMP, def.getTimestamp());
331 assertEquals(ID, ((IntegerDefinition) def.getDefinition("id")).getValue());
332 assertEquals(TIMESTAMP, ((IntegerDefinition) def.getDefinition("timestamp")).getValue());
333 }
334
335 /**
336 * Test an large large header
337 *
338 * @throws CTFReaderException
339 * if {@link BitBuffer} is null
340 */
341 @Test
342 public void testLargeExtended() throws CTFReaderException {
343 ByteBuffer buffer = ByteBuffer.allocate(16);
344 buffer.putShort((short) -1);
345 buffer.putInt(ID);
346 buffer.putLong(TIMESTAMP);
347 byte[] validLarge2 = buffer.array();
348
349 EventHeaderLargeDeclaration decl = new EventHeaderLargeDeclaration(ByteOrder.BIG_ENDIAN);
350 final ByteBuffer input = ByteBuffer.wrap(validLarge2);
351 assertNotNull(input);
352 EventHeaderDefinition def = decl.createDefinition(null, "bla", new BitBuffer(input));
353 assertNotNull(def);
354 assertEquals(ID, def.getId());
355 assertEquals(TIMESTAMP, def.getTimestamp());
356 assertEquals(ID, ((IntegerDefinition) def.getDefinition("id")).getValue());
357 assertEquals(TIMESTAMP, ((IntegerDefinition) def.getDefinition("timestamp")).getValue());
358 }
359
360 /**
361 * Test maximum sizes, make sure they don't change unannounced
362 */
363 @Test
364 public void testMaxSizes() {
365 assertEquals(112, (new EventHeaderLargeDeclaration(ByteOrder.BIG_ENDIAN)).getMaximumSize());
366 assertEquals(104, (new EventHeaderCompactDeclaration(ByteOrder.BIG_ENDIAN)).getMaximumSize());
367 }
368 }
This page took 0.037757 seconds and 4 git commands to generate.