c9ceb3fef96b64712a7d03f8603a6f4145ef898d
[deliverable/tracecompass.git] / org.eclipse.linuxtools.ctf.core.tests / src / org / eclipse / linuxtools / ctf / core / tests / trace / CTFTraceReaderTest.java
1 /*******************************************************************************
2 * Copyright (c) 2013 Ericsson
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
7 *
8 * Contributors:
9 * Matthew Khouzam - Initial API and implementation
10 *******************************************************************************/
11
12 package org.eclipse.linuxtools.ctf.core.tests.trace;
13
14 import static org.junit.Assert.assertEquals;
15 import static org.junit.Assert.assertFalse;
16 import static org.junit.Assert.assertNotNull;
17 import static org.junit.Assert.assertTrue;
18 import static org.junit.Assume.assumeTrue;
19
20 import org.eclipse.linuxtools.ctf.core.event.EventDefinition;
21 import org.eclipse.linuxtools.ctf.core.tests.shared.CtfTestTraces;
22 import org.eclipse.linuxtools.ctf.core.trace.CTFReaderException;
23 import org.eclipse.linuxtools.ctf.core.trace.CTFTrace;
24 import org.eclipse.linuxtools.ctf.core.trace.CTFTraceReader;
25 import org.junit.After;
26 import org.junit.Before;
27 import org.junit.Test;
28
29 /**
30 * The class <code>CTFTraceReaderTest</code> contains tests for the class
31 * <code>{@link CTFTraceReader}</code>.
32 *
33 * @author ematkho
34 * @version $Revision: 1.0 $
35 */
36 @SuppressWarnings("javadoc")
37 public class CTFTraceReaderTest {
38
39 private static final int TRACE_INDEX = 0;
40
41 private CTFTraceReader fixture;
42
43 /**
44 * Launch the test.
45 *
46 * @param args
47 * the command line arguments
48 */
49 public static void main(String[] args) {
50 new org.junit.runner.JUnitCore().run(CTFTraceReaderTest.class);
51 }
52
53 /**
54 * Perform pre-test initialization.
55 *
56 * @throws CTFReaderException
57 */
58 @Before
59 public void setUp() throws CTFReaderException {
60 assumeTrue(CtfTestTraces.tracesExist());
61 fixture = new CTFTraceReader(CtfTestTraces.getTestTrace(TRACE_INDEX));
62 }
63
64 /**
65 * Perform post-test clean-up.
66 */
67 @After
68 public void tearDown() {
69 // Add additional tear down code here
70 }
71
72 /**
73 * Run the CTFTraceReader(CTFTrace) constructor test. Open a known good
74 * trace.
75 *
76 * @throws CTFReaderException
77 */
78 @Test
79 public void testOpen_existing() throws CTFReaderException {
80 CTFTrace trace = CtfTestTraces.getTestTrace(TRACE_INDEX);
81
82 CTFTraceReader result = new CTFTraceReader(trace);
83 assertNotNull(result);
84 }
85
86 /**
87 * Run the CTFTraceReader(CTFTrace) constructor test. Open a non-existing
88 * trace, expect the exception.
89 *
90 * @throws CTFReaderException
91 */
92 @Test(expected = org.eclipse.linuxtools.ctf.core.trace.CTFReaderException.class)
93 public void testOpen_nonexisting() throws CTFReaderException {
94 CTFTrace trace = new CTFTrace("badfile.bad"); //$NON-NLS-1$
95
96 CTFTraceReader result = new CTFTraceReader(trace);
97 assertNotNull(result);
98 }
99
100 /**
101 * Run the CTFTraceReader(CTFTrace) constructor test. Try to pen an invalid
102 * path, expect exception.
103 *
104 * @throws CTFReaderException
105 */
106 @Test(expected = org.eclipse.linuxtools.ctf.core.trace.CTFReaderException.class)
107 public void testOpen_invalid() throws CTFReaderException {
108 CTFTrace trace = new CTFTrace(""); //$NON-NLS-1$
109
110 CTFTraceReader result = new CTFTraceReader(trace);
111 assertNotNull(result);
112 }
113
114 /**
115 * Run the boolean advance() method test. Test advancing normally.
116 */
117 @Test
118 public void testAdvance_normal() {
119 boolean result = fixture.advance();
120 assertTrue(result);
121 }
122
123 /**
124 * Run the boolean advance() method test. Test advancing when we're at the
125 * end, so we expect that there is no more events.
126 */
127 @Test
128 public void testAdvance_end() {
129 int i = 0;
130 boolean result = fixture.advance();
131 while (result) {
132 result = fixture.advance();
133 i++;
134 }
135 fixture.seek(0);
136 fixture.advance();
137 fixture.goToLastEvent();
138 i = 1;
139 result = fixture.advance();
140 while (result) {
141 result = fixture.advance();
142 i++;
143 }
144 assertFalse(result);
145 assertEquals(i, 1);
146 }
147
148 /**
149 * Run the CTFTraceReader copy constructor test.
150 */
151 @Test
152 public void testCopyFrom() {
153 CTFTraceReader result = fixture.copyFrom();
154 assertNotNull(result);
155 }
156
157 /**
158 * Test the hashCode method.
159 */
160 @Test
161 public void testHash() {
162 int result = fixture.hashCode();
163 assertTrue(0 != result);
164 }
165
166 /**
167 * Test the equals method. Uses the class-wide 'fixture' and another
168 * method-local 'fixture2', which both point to the same trace.
169 *
170 * Both trace reader are different objects, so they shouldn't "equals" each
171 * other.
172 *
173 * @throws CTFReaderException
174 */
175 @Test
176 public void testEquals() throws CTFReaderException {
177 CTFTraceReader fixture2 = new CTFTraceReader(CtfTestTraces.getTestTrace(TRACE_INDEX));
178 assertEquals(fixture, fixture2);
179 }
180
181 /**
182 * Run the getCurrentEventDef() method test. Get the first event's
183 * definition.
184 */
185 @Test
186 public void testGetCurrentEventDef_first() {
187 EventDefinition result = fixture.getCurrentEventDef();
188 assertNotNull(result);
189 }
190
191 /**
192 * Run the getCurrentEventDef() method test. Get the last event's
193 * definition.
194 */
195 @Test
196 public void testGetCurrentEventDef_last() {
197 fixture.goToLastEvent();
198 EventDefinition result = fixture.getCurrentEventDef();
199 assertNotNull(result);
200 }
201
202 /**
203 * Run the long getEndTime() method test.
204 */
205 @Test
206 public void testGetEndTime() {
207 long result = fixture.getEndTime();
208 assertTrue(0L < result);
209 }
210
211 /**
212 * Run the long getStartTime() method test.
213 */
214 @Test
215 public void testGetStartTime() {
216 long result = fixture.getStartTime();
217 assertTrue(0L < result);
218 }
219
220 /**
221 * Run the void goToLastEvent() method test.
222 */
223 @Test
224 public void testGoToLastEvent() {
225 fixture.goToLastEvent();
226 long ts1 = getTimestamp();
227 long ts2 = fixture.getEndTime();
228 assertEquals(ts1, ts2);
229 }
230
231 /**
232 * Run the boolean hasMoreEvents() method test.
233 *
234 * @throws CTFReaderException
235 */
236 @Test
237 public void testHasMoreEvents() {
238 boolean result = fixture.hasMoreEvents();
239 assertTrue(result);
240 }
241
242 /**
243 * Run the void printStats() method test with no 'width' parameter.
244 */
245 @Test
246 public void testPrintStats_noparam() {
247 fixture.advance();
248 fixture.printStats();
249 }
250
251 /**
252 * Run the void printStats(int) method test with width = 0.
253 */
254 @Test
255 public void testPrintStats_width0() {
256 fixture.advance();
257 fixture.printStats(0);
258 }
259
260 /**
261 * Run the void printStats(int) method test with width = 1.
262 */
263 @Test
264 public void testPrintStats_width1() {
265 fixture.advance();
266 fixture.printStats(1);
267 }
268
269 /**
270 * Run the void printStats(int) method test with width = 2.
271 */
272 @Test
273 public void testPrintStats_width2() {
274 fixture.advance();
275 fixture.printStats(2);
276 }
277
278 /**
279 * Run the void printStats(int) method test with width = 10.
280 */
281 @Test
282 public void testPrintStats_width10() {
283 fixture.advance();
284 fixture.printStats(10);
285 }
286
287 /**
288 * Run the void printStats(int) method test with width = 100.
289 */
290 @Test
291 public void testPrintStats_100() {
292 for (int i = 0; i < 1000; i++) {
293 fixture.advance();
294 }
295 fixture.printStats(100);
296 }
297
298 /**
299 * Run the boolean seek(long) method test.
300 */
301 @Test
302 public void testSeek() {
303 long timestamp = 1L;
304 boolean result = fixture.seek(timestamp);
305 assertTrue(result);
306 }
307
308
309
310 /**
311 * @return
312 */
313 private long getTimestamp() {
314 if (fixture.getCurrentEventDef() != null) {
315 return fixture.getTrace().timestampCyclesToNanos(fixture.getCurrentEventDef().getTimestamp());
316 }
317 return -1;
318 }
319 }
This page took 0.037095 seconds and 5 git commands to generate.