Commit | Line | Data |
---|---|---|
9ab4ca26 AM |
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; | |
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 | ||
54 | private static CtfTmfTrace fTrace; | |
55 | ||
56 | // ------------------------------------------------------------------------ | |
57 | // Setup | |
58 | // ------------------------------------------------------------------------ | |
59 | ||
60 | /** | |
61 | * Test setup | |
62 | */ | |
63 | @BeforeClass | |
64 | public static void setupClass() { | |
65 | assumeTrue(testTrace.exists()); | |
66 | fTrace = testTrace.getTrace(); | |
67 | fTrace.indexTrace(true); | |
68 | } | |
69 | ||
70 | /** | |
71 | * Clean-up | |
72 | */ | |
73 | @AfterClass | |
74 | public static void tearDownClass() { | |
75 | fTrace.dispose(); | |
76 | } | |
77 | ||
78 | // ------------------------------------------------------------------------ | |
79 | // Test methods | |
80 | // ------------------------------------------------------------------------ | |
81 | ||
82 | /** | |
83 | * Verify the contents of the first event | |
84 | */ | |
85 | @Test | |
86 | public void testFirstEvent() { | |
87 | CtfTmfEvent event = getEvent(0); | |
88 | assertEquals("Simple Event", event.getEventName()); | |
89 | assertEquals(1234567, event.getTimestamp().getValue()); | |
90 | assertEquals(42, ((Long) event.getContent().getField("integer_field").getValue()).intValue()); | |
91 | assertEquals(3.1415, ((Double) event.getContent().getField("float_field").getValue()).doubleValue(), DELTA); | |
92 | } | |
93 | ||
94 | /** | |
95 | * Verify the contents of the second event (the first "spammy event") | |
96 | */ | |
97 | @Test | |
98 | public void testSecondEvent() { | |
99 | CtfTmfEvent event = getEvent(1); | |
100 | assertEquals("Spammy_Event", event.getEventName()); | |
101 | assertEquals(1234568, event.getTimestamp().getValue()); | |
102 | assertEquals(0, ((Long) event.getContent().getField("field_1").getValue()).intValue()); | |
103 | assertEquals("This is a test", event.getContent().getField("a_string").getValue()); | |
104 | } | |
105 | ||
106 | /** | |
107 | * Verify the contents of the last "spammy event" | |
108 | */ | |
109 | @Test | |
110 | public void testSecondToLastEvent() { | |
111 | CtfTmfEvent event = getEvent(100000); | |
112 | assertEquals("Spammy_Event", event.getEventName()); | |
113 | assertEquals(1334567, event.getTimestamp().getValue()); | |
114 | assertEquals(99999, ((Long) event.getContent().getField("field_1").getValue()).intValue()); | |
115 | assertEquals("This is a test", event.getContent().getField("a_string").getValue()); | |
116 | } | |
117 | ||
118 | /** | |
119 | * Verify the contents of the last, complex event | |
120 | */ | |
121 | @Test | |
122 | public void testLastEvent() { | |
123 | /* | |
124 | * Last event as seen in Babeltrace: | |
125 | * [19:00:00.001334568] (+0.000000001) Complex Test Event: { }, { | |
126 | * uint_35 = 0xDDF00D, | |
127 | * int_16 = -12345, | |
128 | * complex_structure = { | |
129 | * variant_selector = ( INT16_TYPE : container = 1 ), | |
130 | * a_string = "Test string", | |
131 | * variant_value = { INT16_TYPE = -200 }, | |
132 | * inner_structure = { | |
133 | * seq_len = 0xA, | |
134 | * a_sequence = [ [0] = 4, [1] = 3, [2] = 2, [3] = 1, [4] = 0, [5] = -1, [6] = -2, [7] = -3, [8] = -4, [9] = -5 ] | |
135 | * } | |
136 | * } | |
137 | * } | |
138 | */ | |
139 | ||
140 | CtfTmfEvent event = getEvent(100001); | |
141 | assertEquals("Complex Test Event", event.getEventName()); | |
142 | assertEquals(1334568, event.getTimestamp().getValue()); | |
143 | assertEquals(0xddf00d, ((Long) event.getContent().getField("uint_35").getValue()).intValue()); | |
144 | assertEquals(-12345, ((Long) event.getContent().getField("int_16").getValue()).intValue()); | |
145 | ||
146 | ITmfEventField[] complexStruct = | |
147 | (ITmfEventField[]) event.getContent().getField("complex_structure").getValue(); | |
148 | ||
149 | assertEquals("variant_selector", complexStruct[0].getName()); | |
150 | CtfEnumPair variant1 = (CtfEnumPair) complexStruct[0].getValue(); | |
151 | assertEquals("INT16_TYPE", variant1.getStringValue()); | |
152 | assertEquals(Long.valueOf(1), variant1.getLongValue()); | |
153 | ||
154 | assertEquals("a_string", complexStruct[1].getName()); | |
155 | assertEquals("Test string", complexStruct[1].getValue()); | |
156 | ||
157 | assertEquals("variant_value", complexStruct[2].getName()); | |
158 | ITmfEventField variantField = (ITmfEventField) complexStruct[2].getValue(); | |
159 | assertEquals("INT16_TYPE", variantField.getName()); | |
160 | assertEquals(Long.valueOf(-200), variantField.getValue()); | |
161 | ||
162 | ITmfEventField[] innerStruct = (ITmfEventField[]) complexStruct[3].getValue(); | |
163 | ||
164 | assertEquals("seq_len", innerStruct[0].getName()); | |
165 | assertEquals(Long.valueOf(10), innerStruct[0].getValue()); | |
166 | ||
167 | assertEquals("a_sequence", innerStruct[1].getName()); | |
168 | long[] seqValues = (long[]) innerStruct[1].getValue(); | |
169 | long[] expectedValues = { 4, 3, 2, 1, 0, -1, -2, -3, -4, -5 }; | |
170 | assertArrayEquals(expectedValues, seqValues); | |
171 | } | |
172 | ||
173 | // ------------------------------------------------------------------------ | |
174 | // Private stuff | |
175 | // ------------------------------------------------------------------------ | |
176 | ||
177 | private synchronized CtfTmfEvent getEvent(long index) { | |
178 | TestEventRequest req = new TestEventRequest(index); | |
179 | fTrace.sendRequest(req); | |
180 | try { | |
181 | req.waitForCompletion(); | |
182 | } catch (InterruptedException e) { | |
183 | return null; | |
184 | } | |
185 | return req.getEvent(); | |
186 | } | |
187 | ||
188 | private class TestEventRequest extends TmfEventRequest { | |
189 | ||
190 | private CtfTmfEvent fRetEvent = null; | |
191 | ||
192 | public TestEventRequest(long index) { | |
193 | super(CtfTmfEvent.class, | |
194 | TmfTimeRange.ETERNITY, | |
195 | index, | |
196 | 1, | |
9ab4ca26 AM |
197 | ExecutionType.FOREGROUND); |
198 | } | |
199 | ||
200 | @Override | |
201 | public void handleData(ITmfEvent event) { | |
202 | fRetEvent = (CtfTmfEvent) event; | |
203 | } | |
204 | ||
205 | public CtfTmfEvent getEvent() { | |
206 | return fRetEvent; | |
207 | } | |
208 | } | |
209 | ||
210 | } |