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