de5e605a762b0f68f31afca016fc19f7a6d17b1f
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.tests / src / org / eclipse / linuxtools / tmf / stream / TmfEventStreamTest.java
1 /*******************************************************************************
2 * Copyright (c) 2009 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 * Francois Chouinard - Initial API and implementation
11 *******************************************************************************/
12
13 package org.eclipse.linuxtools.tmf.stream;
14
15 import static org.junit.Assert.assertEquals;
16
17 import java.io.File;
18
19 import org.eclipse.linuxtools.tmf.event.TmfEvent;
20 import org.eclipse.linuxtools.tmf.event.TmfTimestamp;
21 import org.eclipse.linuxtools.tmf.stream.ITmfEventStream.StreamContext;
22 import org.eclipse.linuxtools.tmf.trace.TmfEventParserStub;
23 import org.eclipse.linuxtools.tmf.trace.TmfEventStreamStub;
24 import org.junit.BeforeClass;
25 import org.junit.Test;
26
27 /**
28 * <b><u>TmfEventStreamTest</u></b>
29 * <p>
30 * TODO: Implement me. Please.
31 */
32 public class TmfEventStreamTest {
33
34 private static final String DIRECTORY = "testfiles";
35 private static final String TEST_STREAM = "M-Test-100K";
36 private static String testfile;
37 private static final int NB_EVENTS = 100000;
38 private static TmfEventStreamStub fStream;
39
40 private static byte SCALE = (byte) -3;
41
42 /**
43 * @throws java.lang.Exception
44 */
45 @BeforeClass
46 public static void setUpBeforeClass() throws Exception {
47 String directory = new File(".").getCanonicalPath() + File.separator + DIRECTORY;
48 testfile = directory + File.separator + TEST_STREAM;
49
50 TmfEventParserStub parser = new TmfEventParserStub();
51 fStream = new TmfEventStreamStub(testfile, parser, 500);
52 fStream.indexStream(true);
53
54 }
55
56 // ========================================================================
57 // Constructor
58 // ========================================================================
59
60 @Test
61 public void testDefaultConstructor() throws Exception {
62 TmfEventParserStub parser = new TmfEventParserStub();
63 TmfEventStreamStub stream = new TmfEventStreamStub(testfile, parser);
64 stream.indexStream(true);
65
66 assertEquals("getCacheSize", TmfEventStreamStub.DEFAULT_CACHE_SIZE, stream.getCacheSize());
67 assertEquals("getTraceSize", NB_EVENTS, stream.getNbEvents());
68 assertEquals("getRange-start", 1, stream.getTimeRange().getStartTime().getValue());
69 assertEquals("getRange-end", NB_EVENTS, stream.getTimeRange().getEndTime().getValue());
70 }
71
72 @Test
73 public void testNormalConstructor() throws Exception {
74 TmfEventParserStub parser = new TmfEventParserStub();
75 TmfEventStreamStub stream = new TmfEventStreamStub(testfile, parser, 500);
76 stream.indexStream(true);
77
78 assertEquals("getCacheSize", 500, stream.getCacheSize());
79 assertEquals("getTraceSize", NB_EVENTS, stream.getNbEvents());
80 assertEquals("getRange-start", 1, stream.getTimeRange().getStartTime().getValue());
81 assertEquals("getRange-end", NB_EVENTS, stream.getTimeRange().getEndTime().getValue());
82 }
83
84 // ========================================================================
85 // seek
86 // ========================================================================
87
88 @Test
89 public void testSeekOnCacheBoundary() throws Exception {
90 StreamContext context = new StreamContext(null);
91
92 TmfEvent event = fStream.getEvent(context, new TmfTimestamp(0, SCALE, 0));
93 assertEquals("Event timestamp", 1, event.getTimestamp().getValue());
94
95 event = fStream.getEvent(context, new TmfTimestamp(1000, SCALE, 0));
96 assertEquals("Event timestamp", 1000, event.getTimestamp().getValue());
97
98 event = fStream.getEvent(context, new TmfTimestamp(4000, SCALE, 0));
99 assertEquals("Event timestamp", 4000, event.getTimestamp().getValue());
100 }
101
102 @Test
103 public void testSeekNotOnCacheBoundary() throws Exception {
104 StreamContext context = new StreamContext(null);
105
106 TmfEvent event = fStream.getEvent(context, new TmfTimestamp(1, SCALE, 0));
107 assertEquals("Event timestamp", 1, event.getTimestamp().getValue());
108
109 event = fStream.getEvent(context, new TmfTimestamp(999, SCALE, 0));
110 assertEquals("Event timestamp", 999, event.getTimestamp().getValue());
111
112 event = fStream.getEvent(context, new TmfTimestamp(4499, SCALE, 0));
113 assertEquals("Event timestamp", 4499, event.getTimestamp().getValue());
114 }
115
116 @Test
117 public void testSeekForEventOutOfBounds() throws Exception {
118 StreamContext context = new StreamContext(null);
119
120 // On lower bound, returns the first event (ts = 1)
121 TmfEvent event = fStream.getEvent(context, new TmfTimestamp(-1, SCALE, 0));
122 assertEquals("Event timestamp", 1, event.getTimestamp().getValue());
123
124 // On higher bound, returns null (no event)
125 event = fStream.getEvent(context, new TmfTimestamp(NB_EVENTS + 1, SCALE, 0));
126 assertEquals("Event timestamp", null, event);
127 }
128
129 // ========================================================================
130 // getNextEvent
131 // ========================================================================
132
133 @Test
134 public void testGetNextEvent() throws Exception {
135 StreamContext context = new StreamContext(null);
136
137 // On lower bound, returns the first event (ts = 0)
138 TmfEvent event = fStream.getEvent(context, new TmfTimestamp(0, SCALE, 0));
139 assertEquals("Event timestamp", 1, event.getTimestamp().getValue());
140
141 for (int i = 2; i < 20; i++) {
142 event = fStream.getNextEvent(context);
143 assertEquals("Event timestamp", i, event.getTimestamp().getValue());
144 }
145 }
146
147 }
This page took 0.033756 seconds and 4 git commands to generate.