ctf: add Event header data types
[deliverable/tracecompass.git] / org.eclipse.linuxtools.ctf.core.tests / src / org / eclipse / linuxtools / ctf / core / tests / types / EventHeaderDeclarationTest.java
1 package org.eclipse.linuxtools.ctf.core.tests.types;
2
3 import static org.junit.Assert.assertEquals;
4 import static org.junit.Assert.assertNotNull;
5
6 import java.nio.ByteBuffer;
7 import java.nio.ByteOrder;
8 import java.util.ArrayList;
9 import java.util.List;
10
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;
25
26 /**
27 * Event header declaration tests
28 *
29 * @author Matthew Khouzam
30 *
31 */
32 public class EventHeaderDeclarationTest {
33
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;
38
39 private final List<StructDeclaration> declarations = new ArrayList<>();
40
41 /**
42 * Setup
43 */
44 @Before
45 public void init() {
46 declarations.clear();
47
48 /**
49 * do not reflow
50 *
51 * <pre>
52 * struct event_header_compact {
53 * enum : uint5_t { compact = 0 ... 30, extended = 31 } id;
54 * variant <id> {
55 * struct {
56 * uint27_clock_monotonic_t timestamp;
57 * } compact;
58 * struct {
59 * uint32_t id;
60 * uint64_clock_monotonic_t timestamp;
61 * } extended;
62 * } v;
63 * } align(8);
64 * </pre>
65 */
66
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);
79
80 /**
81 * Do not reflow
82 *
83 * <pre>
84 * struct event_header_large {
85 * enum : uint16_t { compact = 0 ... 65534, extended = 65535 } id;
86 * variant <id> {
87 * struct {
88 * uint32_clock_monotonic_t timestamp;
89 * } compact;
90 * struct {
91 * uint32_t id;
92 * uint64_clock_monotonic_t timestamp;
93 * } extended;
94 * } v;
95 * } align(8);
96 * </pre>
97 */
98
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);
111
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);
117 // bad
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);
122 // bad
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);
127 // bad
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);
140
141 // bad
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);
154
155 // bad
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);
168
169 // bad
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);
183
184 // bad
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);
198 // bad
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);
203 // bad
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);
208 // bad
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);
213 }
214
215 /**
216 * Validate a compact declaration
217 */
218 @Test
219 public void validateCompact() {
220 assertEquals(true, EventHeaderCompactDeclaration.isCompactEventHeader(declarations.get(VALID_COMPACT)));
221 }
222
223 /**
224 * Fail if it validates
225 */
226 @Test
227 public void validateCompactFail() {
228 for (int i = 0; i < declarations.size(); i++) {
229 if (i == VALID_COMPACT) {
230 continue;
231 }
232 assertEquals(false, EventHeaderCompactDeclaration.isCompactEventHeader(declarations.get(i)));
233 }
234 }
235
236 /**
237 * Validate a large declaration
238 */
239 @Test
240 public void validateLarge() {
241 assertEquals(true, EventHeaderLargeDeclaration.isLargeEventHeader(declarations.get(VALID_LARGE)));
242 }
243
244 /**
245 * Fail if it validates
246 */
247 @Test
248 public void validateLargeFail() {
249 for (int i = 0; i < declarations.size(); i++) {
250 if (i == VALID_LARGE) {
251 continue;
252 }
253 assertEquals(false, EventHeaderLargeDeclaration.isLargeEventHeader(declarations.get(i)));
254 }
255 }
256
257 /**
258 * Test an compact compact header
259 *
260 * @throws CTFReaderException
261 * if {@link BitBuffer} is null
262 */
263 @Test
264 public void testCompactCompact() throws CTFReaderException {
265 ByteBuffer buffer = ByteBuffer.allocate(16);
266 buffer.putInt(0x80000042);
267 byte[] validCompact1 = buffer.array();
268
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));
273 assertNotNull(def);
274 assertEquals(16, def.getId());
275 assertEquals(0x42, def.getTimestamp());
276 }
277
278 /**
279 * Test an extended compact header
280 *
281 * @throws CTFReaderException
282 * if {@link BitBuffer} is null
283 */
284 @Test
285 public void testCompactExtended() throws CTFReaderException {
286 ByteBuffer buffer = ByteBuffer.allocate(16);
287 buffer.put((byte) 0xFF);
288 buffer.putInt(ID);
289 buffer.putLong(TIMESTAMP);
290 byte[] validCompact2 = buffer.array();
291
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));
296 assertNotNull(def);
297 assertEquals(ID, def.getId());
298 assertEquals(TIMESTAMP, def.getTimestamp());
299 }
300
301 /**
302 * Test an compact large header
303 *
304 * @throws CTFReaderException
305 * if {@link BitBuffer} is null
306 */
307 @Test
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();
313
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));
318 assertNotNull(def);
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());
323 }
324
325 /**
326 * Test an large large header
327 *
328 * @throws CTFReaderException
329 * if {@link BitBuffer} is null
330 */
331 @Test
332 public void testLargeExtended() throws CTFReaderException {
333 ByteBuffer buffer = ByteBuffer.allocate(16);
334 buffer.putShort((short) -1);
335 buffer.putInt(ID);
336 buffer.putLong(TIMESTAMP);
337 byte[] validLarge2 = buffer.array();
338
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));
343 assertNotNull(def);
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());
348 }
349
350 /**
351 * Test maximum sizes, make sure they don't change unannounced
352 */
353 @Test
354 public void testMaxSizes() {
355 assertEquals(112, (new EventHeaderLargeDeclaration(ByteOrder.BIG_ENDIAN)).getMaximumSize());
356 assertEquals(104, (new EventHeaderCompactDeclaration(ByteOrder.BIG_ENDIAN)).getMaximumSize());
357 }
358 }
This page took 0.063372 seconds and 5 git commands to generate.