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