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
9 * Matthew Khouzam - Initial API and implementation
10 *******************************************************************************/
11 package org
.eclipse
.linuxtools
.ctf
.core
.tests
.types
;
13 import static org
.junit
.Assert
.assertEquals
;
14 import static org
.junit
.Assert
.assertNotNull
;
16 import java
.nio
.ByteBuffer
;
17 import java
.nio
.ByteOrder
;
18 import java
.util
.ArrayList
;
19 import java
.util
.List
;
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
;
37 * Event header declaration tests
39 * @author Matthew Khouzam
42 public class EventHeaderDeclarationTest
{
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;
49 private final List
<StructDeclaration
> declarations
= new ArrayList
<>();
62 * struct event_header_compact {
63 * enum : uint5_t { compact = 0 ... 30, extended = 31 } id;
66 * uint27_clock_monotonic_t timestamp;
70 * uint64_clock_monotonic_t timestamp;
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
);
94 * struct event_header_large {
95 * enum : uint16_t { compact = 0 ... 65534, extended = 65535 } id;
98 * uint32_clock_monotonic_t timestamp;
102 * uint64_clock_monotonic_t timestamp;
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
);
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
);
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
);
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
);
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
);
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
);
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
);
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
);
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
);
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
);
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
);
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
);
226 * Validate a compact declaration
229 public void validateCompact() {
230 assertEquals(true, EventHeaderCompactDeclaration
.isCompactEventHeader(declarations
.get(VALID_COMPACT
)));
234 * Fail if it validates
237 public void validateCompactFail() {
238 for (int i
= 0; i
< declarations
.size(); i
++) {
239 if (i
== VALID_COMPACT
) {
242 assertEquals(false, EventHeaderCompactDeclaration
.isCompactEventHeader(declarations
.get(i
)));
247 * Validate a large declaration
250 public void validateLarge() {
251 assertEquals(true, EventHeaderLargeDeclaration
.isLargeEventHeader(declarations
.get(VALID_LARGE
)));
255 * Fail if it validates
258 public void validateLargeFail() {
259 for (int i
= 0; i
< declarations
.size(); i
++) {
260 if (i
== VALID_LARGE
) {
263 assertEquals(false, EventHeaderLargeDeclaration
.isLargeEventHeader(declarations
.get(i
)));
268 * Test an compact compact header
270 * @throws CTFReaderException
271 * if {@link BitBuffer} is null
274 public void testCompactCompact() throws CTFReaderException
{
275 ByteBuffer buffer
= ByteBuffer
.allocate(16);
276 buffer
.putInt(0x80000042);
277 byte[] validCompact1
= buffer
.array();
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
));
284 assertEquals(16, def
.getId());
285 assertEquals(0x42, def
.getTimestamp());
289 * Test an extended compact header
291 * @throws CTFReaderException
292 * if {@link BitBuffer} is null
295 public void testCompactExtended() throws CTFReaderException
{
296 ByteBuffer buffer
= ByteBuffer
.allocate(16);
297 buffer
.put((byte) 0xFF);
299 buffer
.putLong(TIMESTAMP
);
300 byte[] validCompact2
= buffer
.array();
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
));
307 assertEquals(ID
, def
.getId());
308 assertEquals(TIMESTAMP
, def
.getTimestamp());
312 * Test an compact large header
314 * @throws CTFReaderException
315 * if {@link BitBuffer} is null
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();
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
));
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());
336 * Test an large large header
338 * @throws CTFReaderException
339 * if {@link BitBuffer} is null
342 public void testLargeExtended() throws CTFReaderException
{
343 ByteBuffer buffer
= ByteBuffer
.allocate(16);
344 buffer
.putShort((short) -1);
346 buffer
.putLong(TIMESTAMP
);
347 byte[] validLarge2
= buffer
.array();
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
));
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());
361 * Test maximum sizes, make sure they don't change unannounced
364 public void testMaxSizes() {
365 assertEquals(112, (new EventHeaderLargeDeclaration(ByteOrder
.BIG_ENDIAN
)).getMaximumSize());
366 assertEquals(104, (new EventHeaderCompactDeclaration(ByteOrder
.BIG_ENDIAN
)).getMaximumSize());