1 package org
.eclipse
.linuxtools
.ctf
.core
.tests
.types
;
3 import static org
.junit
.Assert
.assertEquals
;
4 import static org
.junit
.Assert
.assertNotNull
;
6 import java
.nio
.ByteBuffer
;
7 import java
.nio
.ByteOrder
;
8 import java
.util
.ArrayList
;
11 import org
.eclipse
.linuxtools
.ctf
.core
.event
.io
.BitBuffer
;
12 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.EnumDeclaration
;
13 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.FloatDeclaration
;
14 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.IntegerDeclaration
;
15 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.IntegerDefinition
;
16 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.StringDeclaration
;
17 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.StructDeclaration
;
18 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.VariantDeclaration
;
19 import org
.eclipse
.linuxtools
.ctf
.core
.trace
.CTFReaderException
;
20 import org
.eclipse
.linuxtools
.internal
.ctf
.core
.event
.types
.composite
.EventHeaderCompactDeclaration
;
21 import org
.eclipse
.linuxtools
.internal
.ctf
.core
.event
.types
.composite
.EventHeaderDefinition
;
22 import org
.eclipse
.linuxtools
.internal
.ctf
.core
.event
.types
.composite
.EventHeaderLargeDeclaration
;
23 import org
.junit
.Before
;
24 import org
.junit
.Test
;
27 * Event header declaration tests
29 * @author Matthew Khouzam
32 public class EventHeaderDeclarationTest
{
34 private static final int ID
= 2222;
35 private static final int TIMESTAMP
= 1000;
36 private static final int VALID_LARGE
= 1;
37 private static final int VALID_COMPACT
= 0;
39 private final List
<StructDeclaration
> declarations
= new ArrayList
<>();
52 * struct event_header_compact {
53 * enum : uint5_t { compact = 0 ... 30, extended = 31 } id;
56 * uint27_clock_monotonic_t timestamp;
60 * uint64_clock_monotonic_t timestamp;
67 StructDeclaration base
= new StructDeclaration(8);
68 base
.addField("id", new EnumDeclaration(IntegerDeclaration
.UINT_5B_DECL
));
69 VariantDeclaration variantV
= new VariantDeclaration();
70 StructDeclaration compact
= new StructDeclaration(8);
71 compact
.addField("timestamp", IntegerDeclaration
.UINT_27B_DECL
);
72 variantV
.addField("compact", compact
);
73 StructDeclaration large
= new StructDeclaration(8);
74 large
.addField("id", IntegerDeclaration
.UINT_32B_DECL
);
75 large
.addField("timestamp", IntegerDeclaration
.UINT_64B_DECL
);
76 variantV
.addField("extended", large
);
77 base
.addField("v", variantV
);
78 declarations
.add(base
);
84 * struct event_header_large {
85 * enum : uint16_t { compact = 0 ... 65534, extended = 65535 } id;
88 * uint32_clock_monotonic_t timestamp;
92 * uint64_clock_monotonic_t timestamp;
99 base
= new StructDeclaration(8);
100 base
.addField("id", new EnumDeclaration(IntegerDeclaration
.UINT_16B_DECL
));
101 variantV
= new VariantDeclaration();
102 compact
= new StructDeclaration(8);
103 compact
.addField("timestamp", IntegerDeclaration
.UINT_32B_DECL
);
104 variantV
.addField("compact", compact
);
105 large
= new StructDeclaration(8);
106 large
.addField("id", IntegerDeclaration
.UINT_32B_DECL
);
107 large
.addField("timestamp", IntegerDeclaration
.UINT_64B_DECL
);
108 variantV
.addField("extended", large
);
109 base
.addField("v", variantV
);
110 declarations
.add(base
);
112 // bad - well, sounds nice though
113 base
= new StructDeclaration(8);
114 base
.addField("potato salad", new FloatDeclaration(8, 8, ByteOrder
.BIG_ENDIAN
, 8));
115 base
.addField("bbq ribs", new FloatDeclaration(8, 8, ByteOrder
.BIG_ENDIAN
, 8));
116 declarations
.add(base
);
118 base
= new StructDeclaration(8);
119 base
.addField("id", new EnumDeclaration(IntegerDeclaration
.UINT_16B_DECL
));
120 base
.addField("v", new FloatDeclaration(8, 8, ByteOrder
.BIG_ENDIAN
, 8));
121 declarations
.add(base
);
123 base
= new StructDeclaration(8);
124 base
.addField("id", new EnumDeclaration(IntegerDeclaration
.UINT_5B_DECL
));
125 base
.addField("v", 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_5B_DECL
));
130 variantV
= new VariantDeclaration();
131 compact
= new StructDeclaration(8);
132 compact
.addField("timestamp", IntegerDeclaration
.UINT_27B_DECL
);
133 variantV
.addField("compact1", compact
);
134 large
= new StructDeclaration(8);
135 large
.addField("id", IntegerDeclaration
.UINT_32B_DECL
);
136 large
.addField("timestamp", IntegerDeclaration
.UINT_64B_DECL
);
137 variantV
.addField("extended", large
);
138 base
.addField("v", variantV
);
139 declarations
.add(base
);
142 base
= new StructDeclaration(8);
143 base
.addField("id", new EnumDeclaration(IntegerDeclaration
.UINT_5B_DECL
));
144 variantV
= new VariantDeclaration();
145 compact
= new StructDeclaration(8);
146 compact
.addField("timestamp", IntegerDeclaration
.UINT_27B_DECL
);
147 variantV
.addField("compact", compact
);
148 large
= new StructDeclaration(8);
149 large
.addField("id", IntegerDeclaration
.UINT_32B_DECL
);
150 large
.addField("timestamp1", IntegerDeclaration
.UINT_64B_DECL
);
151 variantV
.addField("extended", large
);
152 base
.addField("v", variantV
);
153 declarations
.add(base
);
156 base
= new StructDeclaration(8);
157 base
.addField("id", new EnumDeclaration(IntegerDeclaration
.UINT_5B_DECL
));
158 variantV
= new VariantDeclaration();
159 compact
= new StructDeclaration(8);
160 compact
.addField("timestamp", IntegerDeclaration
.UINT_27B_DECL
);
161 variantV
.addField("compact", compact
);
162 large
= new StructDeclaration(8);
163 large
.addField("id", IntegerDeclaration
.UINT_32B_DECL
);
164 large
.addField("timestamp", new StringDeclaration());
165 variantV
.addField("extended", large
);
166 base
.addField("v", variantV
);
167 declarations
.add(base
);
170 base
= new StructDeclaration(8);
171 base
.addField("id", new EnumDeclaration(IntegerDeclaration
.UINT_5B_DECL
));
172 variantV
= new VariantDeclaration();
173 compact
= new StructDeclaration(8);
174 compact
.addField("timestamp", IntegerDeclaration
.UINT_27B_DECL
);
175 variantV
.addField("compact", compact
);
176 variantV
.addField("surprise!", compact
);
177 large
= new StructDeclaration(8);
178 large
.addField("id", IntegerDeclaration
.UINT_32B_DECL
);
179 large
.addField("timestamp", new StringDeclaration());
180 variantV
.addField("extended", large
);
181 base
.addField("v", variantV
);
182 declarations
.add(base
);
185 base
= new StructDeclaration(8);
186 base
.addField("id", new EnumDeclaration(IntegerDeclaration
.UINT_16B_DECL
));
187 variantV
= new VariantDeclaration();
188 compact
= new StructDeclaration(8);
189 compact
.addField("timestamp", IntegerDeclaration
.UINT_27B_DECL
);
190 variantV
.addField("compact", compact
);
191 variantV
.addField("surprise!", compact
);
192 large
= new StructDeclaration(8);
193 large
.addField("id", IntegerDeclaration
.UINT_32B_DECL
);
194 large
.addField("timestamp", new StringDeclaration());
195 variantV
.addField("extended", large
);
196 base
.addField("v", variantV
);
197 declarations
.add(base
);
199 base
= new StructDeclaration(8);
200 base
.addField("id", new FloatDeclaration(8, 8, ByteOrder
.BIG_ENDIAN
, 8));
201 base
.addField("v", new FloatDeclaration(8, 8, ByteOrder
.BIG_ENDIAN
, 8));
202 declarations
.add(base
);
204 base
= new StructDeclaration(8);
205 base
.addField("id", IntegerDeclaration
.INT_32B_DECL
);
206 base
.addField("timestamp", IntegerDeclaration
.INT_32B_DECL
);
207 declarations
.add(base
);
209 base
= new StructDeclaration(8);
210 base
.addField("id", new EnumDeclaration(IntegerDeclaration
.INT_8_DECL
));
211 base
.addField("timestamp", IntegerDeclaration
.INT_32B_DECL
);
212 declarations
.add(base
);
216 * Validate a compact declaration
219 public void validateCompact() {
220 assertEquals(true, EventHeaderCompactDeclaration
.isCompactEventHeader(declarations
.get(VALID_COMPACT
)));
224 * Fail if it validates
227 public void validateCompactFail() {
228 for (int i
= 0; i
< declarations
.size(); i
++) {
229 if (i
== VALID_COMPACT
) {
232 assertEquals(false, EventHeaderCompactDeclaration
.isCompactEventHeader(declarations
.get(i
)));
237 * Validate a large declaration
240 public void validateLarge() {
241 assertEquals(true, EventHeaderLargeDeclaration
.isLargeEventHeader(declarations
.get(VALID_LARGE
)));
245 * Fail if it validates
248 public void validateLargeFail() {
249 for (int i
= 0; i
< declarations
.size(); i
++) {
250 if (i
== VALID_LARGE
) {
253 assertEquals(false, EventHeaderLargeDeclaration
.isLargeEventHeader(declarations
.get(i
)));
258 * Test an compact compact header
260 * @throws CTFReaderException
261 * if {@link BitBuffer} is null
264 public void testCompactCompact() throws CTFReaderException
{
265 ByteBuffer buffer
= ByteBuffer
.allocate(16);
266 buffer
.putInt(0x80000042);
267 byte[] validCompact1
= buffer
.array();
269 EventHeaderCompactDeclaration decl
= new EventHeaderCompactDeclaration(ByteOrder
.BIG_ENDIAN
);
270 final ByteBuffer input
= ByteBuffer
.wrap(validCompact1
);
271 assertNotNull(input
);
272 EventHeaderDefinition def
= decl
.createDefinition(null, "bla", new BitBuffer(input
));
274 assertEquals(16, def
.getId());
275 assertEquals(0x42, def
.getTimestamp());
279 * Test an extended compact header
281 * @throws CTFReaderException
282 * if {@link BitBuffer} is null
285 public void testCompactExtended() throws CTFReaderException
{
286 ByteBuffer buffer
= ByteBuffer
.allocate(16);
287 buffer
.put((byte) 0xFF);
289 buffer
.putLong(TIMESTAMP
);
290 byte[] validCompact2
= buffer
.array();
292 EventHeaderCompactDeclaration decl
= new EventHeaderCompactDeclaration(ByteOrder
.BIG_ENDIAN
);
293 final ByteBuffer input
= ByteBuffer
.wrap(validCompact2
);
294 assertNotNull(input
);
295 EventHeaderDefinition def
= decl
.createDefinition(null, "bla", new BitBuffer(input
));
297 assertEquals(ID
, def
.getId());
298 assertEquals(TIMESTAMP
, def
.getTimestamp());
302 * Test an compact large header
304 * @throws CTFReaderException
305 * if {@link BitBuffer} is null
308 public void testLargeCompact() throws CTFReaderException
{
309 ByteBuffer buffer
= ByteBuffer
.allocate(16);
310 buffer
.putShort((short) ID
);
311 buffer
.putInt(TIMESTAMP
);
312 byte[] validLarge1
= buffer
.array();
314 EventHeaderLargeDeclaration decl
= new EventHeaderLargeDeclaration(ByteOrder
.BIG_ENDIAN
);
315 final ByteBuffer input
= ByteBuffer
.wrap(validLarge1
);
316 assertNotNull(input
);
317 EventHeaderDefinition def
= decl
.createDefinition(null, "bla", new BitBuffer(input
));
319 assertEquals(ID
, def
.getId());
320 assertEquals(TIMESTAMP
, def
.getTimestamp());
321 assertEquals(ID
, ((IntegerDefinition
) def
.getDefinition("id")).getValue());
322 assertEquals(TIMESTAMP
, ((IntegerDefinition
) def
.getDefinition("timestamp")).getValue());
326 * Test an large large header
328 * @throws CTFReaderException
329 * if {@link BitBuffer} is null
332 public void testLargeExtended() throws CTFReaderException
{
333 ByteBuffer buffer
= ByteBuffer
.allocate(16);
334 buffer
.putShort((short) -1);
336 buffer
.putLong(TIMESTAMP
);
337 byte[] validLarge2
= buffer
.array();
339 EventHeaderLargeDeclaration decl
= new EventHeaderLargeDeclaration(ByteOrder
.BIG_ENDIAN
);
340 final ByteBuffer input
= ByteBuffer
.wrap(validLarge2
);
341 assertNotNull(input
);
342 EventHeaderDefinition def
= decl
.createDefinition(null, "bla", new BitBuffer(input
));
344 assertEquals(ID
, def
.getId());
345 assertEquals(TIMESTAMP
, def
.getTimestamp());
346 assertEquals(ID
, ((IntegerDefinition
) def
.getDefinition("id")).getValue());
347 assertEquals(TIMESTAMP
, ((IntegerDefinition
) def
.getDefinition("timestamp")).getValue());
351 * Test maximum sizes, make sure they don't change unannounced
354 public void testMaxSizes() {
355 assertEquals(112, (new EventHeaderLargeDeclaration(ByteOrder
.BIG_ENDIAN
)).getMaximumSize());
356 assertEquals(104, (new EventHeaderCompactDeclaration(ByteOrder
.BIG_ENDIAN
)).getMaximumSize());