ctf: Add advanced trace tests
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.core.tests / src / org / eclipse / linuxtools / tmf / core / tests / ctfadaptor / FunkyTraceTest.java
1 /*******************************************************************************
2 * Copyright (c) 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 * Alexandre Montplaisir - Initial API and implementation
11 *******************************************************************************/
12
13 package org.eclipse.linuxtools.tmf.core.tests.ctfadaptor;
14
15 import static org.junit.Assert.assertArrayEquals;
16 import static org.junit.Assert.assertEquals;
17 import static org.junit.Assume.assumeTrue;
18
19 import org.eclipse.linuxtools.tmf.core.ctfadaptor.CtfEnumPair;
20 import org.eclipse.linuxtools.tmf.core.ctfadaptor.CtfTmfEvent;
21 import org.eclipse.linuxtools.tmf.core.ctfadaptor.CtfTmfTrace;
22 import org.eclipse.linuxtools.tmf.core.event.ITmfEvent;
23 import org.eclipse.linuxtools.tmf.core.event.ITmfEventField;
24 import org.eclipse.linuxtools.tmf.core.request.TmfEventRequest;
25 import org.eclipse.linuxtools.tmf.core.tests.shared.CtfTmfTestTrace;
26 import org.eclipse.linuxtools.tmf.core.timestamp.TmfTimeRange;
27 import org.junit.AfterClass;
28 import org.junit.BeforeClass;
29 import org.junit.Test;
30
31 /**
32 * More advanced CTF tests using "funky_trace", a trace generated with the
33 * Babeltrace CTF writer API, which has lots of fun things like different
34 * integer/float sizes and non-standard struct alignments.
35 *
36 * @author Alexandre Montplaisir
37 */
38 public class FunkyTraceTest {
39
40 // ------------------------------------------------------------------------
41 // Attributes
42 // ------------------------------------------------------------------------
43
44 private static final CtfTmfTestTrace testTrace = CtfTmfTestTrace.FUNKY_TRACE;
45 private static final double DELTA = 0.0000001;
46
47 private static CtfTmfTrace fTrace;
48
49 // ------------------------------------------------------------------------
50 // Setup
51 // ------------------------------------------------------------------------
52
53 /**
54 * Test setup
55 */
56 @BeforeClass
57 public static void setupClass() {
58 assumeTrue(testTrace.exists());
59 fTrace = testTrace.getTrace();
60 fTrace.indexTrace(true);
61 }
62
63 /**
64 * Clean-up
65 */
66 @AfterClass
67 public static void tearDownClass() {
68 fTrace.dispose();
69 }
70
71 // ------------------------------------------------------------------------
72 // Test methods
73 // ------------------------------------------------------------------------
74
75 /**
76 * Verify the contents of the first event
77 */
78 @Test
79 public void testFirstEvent() {
80 CtfTmfEvent event = getEvent(0);
81 assertEquals("Simple Event", event.getEventName());
82 assertEquals(1234567, event.getTimestamp().getValue());
83 assertEquals(42, ((Long) event.getContent().getField("integer_field").getValue()).intValue());
84 assertEquals(3.1415, ((Double) event.getContent().getField("float_field").getValue()).doubleValue(), DELTA);
85 }
86
87 /**
88 * Verify the contents of the second event (the first "spammy event")
89 */
90 @Test
91 public void testSecondEvent() {
92 CtfTmfEvent event = getEvent(1);
93 assertEquals("Spammy_Event", event.getEventName());
94 assertEquals(1234568, event.getTimestamp().getValue());
95 assertEquals(0, ((Long) event.getContent().getField("field_1").getValue()).intValue());
96 assertEquals("This is a test", event.getContent().getField("a_string").getValue());
97 }
98
99 /**
100 * Verify the contents of the last "spammy event"
101 */
102 @Test
103 public void testSecondToLastEvent() {
104 CtfTmfEvent event = getEvent(100000);
105 assertEquals("Spammy_Event", event.getEventName());
106 assertEquals(1334567, event.getTimestamp().getValue());
107 assertEquals(99999, ((Long) event.getContent().getField("field_1").getValue()).intValue());
108 assertEquals("This is a test", event.getContent().getField("a_string").getValue());
109 }
110
111 /**
112 * Verify the contents of the last, complex event
113 */
114 @Test
115 public void testLastEvent() {
116 /*
117 * Last event as seen in Babeltrace:
118 * [19:00:00.001334568] (+0.000000001) Complex Test Event: { }, {
119 * uint_35 = 0xDDF00D,
120 * int_16 = -12345,
121 * complex_structure = {
122 * variant_selector = ( INT16_TYPE : container = 1 ),
123 * a_string = "Test string",
124 * variant_value = { INT16_TYPE = -200 },
125 * inner_structure = {
126 * seq_len = 0xA,
127 * a_sequence = [ [0] = 4, [1] = 3, [2] = 2, [3] = 1, [4] = 0, [5] = -1, [6] = -2, [7] = -3, [8] = -4, [9] = -5 ]
128 * }
129 * }
130 * }
131 */
132
133 CtfTmfEvent event = getEvent(100001);
134 assertEquals("Complex Test Event", event.getEventName());
135 assertEquals(1334568, event.getTimestamp().getValue());
136 assertEquals(0xddf00d, ((Long) event.getContent().getField("uint_35").getValue()).intValue());
137 assertEquals(-12345, ((Long) event.getContent().getField("int_16").getValue()).intValue());
138
139 ITmfEventField[] complexStruct =
140 (ITmfEventField[]) event.getContent().getField("complex_structure").getValue();
141
142 assertEquals("variant_selector", complexStruct[0].getName());
143 CtfEnumPair variant1 = (CtfEnumPair) complexStruct[0].getValue();
144 assertEquals("INT16_TYPE", variant1.getStringValue());
145 assertEquals(Long.valueOf(1), variant1.getLongValue());
146
147 assertEquals("a_string", complexStruct[1].getName());
148 assertEquals("Test string", complexStruct[1].getValue());
149
150 assertEquals("variant_value", complexStruct[2].getName());
151 ITmfEventField variantField = (ITmfEventField) complexStruct[2].getValue();
152 assertEquals("INT16_TYPE", variantField.getName());
153 assertEquals(Long.valueOf(-200), variantField.getValue());
154
155 ITmfEventField[] innerStruct = (ITmfEventField[]) complexStruct[3].getValue();
156
157 assertEquals("seq_len", innerStruct[0].getName());
158 assertEquals(Long.valueOf(10), innerStruct[0].getValue());
159
160 assertEquals("a_sequence", innerStruct[1].getName());
161 long[] seqValues = (long[]) innerStruct[1].getValue();
162 long[] expectedValues = { 4, 3, 2, 1, 0, -1, -2, -3, -4, -5 };
163 assertArrayEquals(expectedValues, seqValues);
164 }
165
166 // ------------------------------------------------------------------------
167 // Private stuff
168 // ------------------------------------------------------------------------
169
170 private synchronized CtfTmfEvent getEvent(long index) {
171 TestEventRequest req = new TestEventRequest(index);
172 fTrace.sendRequest(req);
173 try {
174 req.waitForCompletion();
175 } catch (InterruptedException e) {
176 return null;
177 }
178 return req.getEvent();
179 }
180
181 private class TestEventRequest extends TmfEventRequest {
182
183 private CtfTmfEvent fRetEvent = null;
184
185 public TestEventRequest(long index) {
186 super(CtfTmfEvent.class,
187 TmfTimeRange.ETERNITY,
188 index,
189 1,
190 1,
191 ExecutionType.FOREGROUND);
192 }
193
194 @Override
195 public void handleData(ITmfEvent event) {
196 fRetEvent = (CtfTmfEvent) event;
197 }
198
199 public CtfTmfEvent getEvent() {
200 return fRetEvent;
201 }
202 }
203
204 }
This page took 0.051146 seconds and 6 git commands to generate.