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