1 /*******************************************************************************
2 * Copyright (c) 2009 Ericsson
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
10 * Francois Chouinard - Initial API and implementation
11 *******************************************************************************/
13 package org
.eclipse
.linuxtools
.tmf
.stream
;
15 import static org
.junit
.Assert
.assertEquals
;
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
;
28 * <b><u>TmfEventStreamTest</u></b>
30 * TODO: Implement me. Please.
32 public class TmfEventStreamTest
{
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
;
40 private static byte SCALE
= (byte) -3;
43 * @throws java.lang.Exception
46 public static void setUpBeforeClass() throws Exception
{
47 String directory
= new File(".").getCanonicalPath() + File
.separator
+ DIRECTORY
;
48 testfile
= directory
+ File
.separator
+ TEST_STREAM
;
50 TmfEventParserStub parser
= new TmfEventParserStub();
51 fStream
= new TmfEventStreamStub(testfile
, parser
, 500);
52 fStream
.indexStream(true);
56 // ========================================================================
58 // ========================================================================
61 public void testDefaultConstructor() throws Exception
{
62 TmfEventParserStub parser
= new TmfEventParserStub();
63 TmfEventStreamStub stream
= new TmfEventStreamStub(testfile
, parser
);
64 stream
.indexStream(true);
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());
73 public void testNormalConstructor() throws Exception
{
74 TmfEventParserStub parser
= new TmfEventParserStub();
75 TmfEventStreamStub stream
= new TmfEventStreamStub(testfile
, parser
, 500);
76 stream
.indexStream(true);
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());
84 // ========================================================================
86 // ========================================================================
89 public void testSeekOnCacheBoundary() throws Exception
{
90 StreamContext context
= new StreamContext(null);
92 TmfEvent event
= fStream
.getEvent(context
, new TmfTimestamp(0, SCALE
, 0));
93 assertEquals("Event timestamp", 1, event
.getTimestamp().getValue());
95 event
= fStream
.getEvent(context
, new TmfTimestamp(1000, SCALE
, 0));
96 assertEquals("Event timestamp", 1000, event
.getTimestamp().getValue());
98 event
= fStream
.getEvent(context
, new TmfTimestamp(4000, SCALE
, 0));
99 assertEquals("Event timestamp", 4000, event
.getTimestamp().getValue());
103 public void testSeekNotOnCacheBoundary() throws Exception
{
104 StreamContext context
= new StreamContext(null);
106 TmfEvent event
= fStream
.getEvent(context
, new TmfTimestamp(1, SCALE
, 0));
107 assertEquals("Event timestamp", 1, event
.getTimestamp().getValue());
109 event
= fStream
.getEvent(context
, new TmfTimestamp(999, SCALE
, 0));
110 assertEquals("Event timestamp", 999, event
.getTimestamp().getValue());
112 event
= fStream
.getEvent(context
, new TmfTimestamp(4499, SCALE
, 0));
113 assertEquals("Event timestamp", 4499, event
.getTimestamp().getValue());
117 public void testSeekForEventOutOfBounds() throws Exception
{
118 StreamContext context
= new StreamContext(null);
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());
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
);
129 // ========================================================================
131 // ========================================================================
134 public void testGetNextEvent() throws Exception
{
135 StreamContext context
= new StreamContext(null);
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());
141 for (int i
= 2; i
< 20; i
++) {
142 event
= fStream
.getNextEvent(context
);
143 assertEquals("Event timestamp", i
, event
.getTimestamp().getValue());