Fix a pile of Javadoc warnings
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.core.tests / src / org / eclipse / linuxtools / tmf / core / tests / trace / TmfExperimentTest.java
CommitLineData
d18dd09b 1/*******************************************************************************
0316808c 2 * Copyright (c) 2009, 2010, 2012 Ericsson
9b749023 3 *
d18dd09b
ASL
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
9b749023 8 *
d18dd09b
ASL
9 * Contributors:
10 * Francois Chouinard - Initial API and implementation
0316808c 11 * Francois Chouinard - Adjusted for new Trace Model
d18dd09b
ASL
12 *******************************************************************************/
13
9e0640dc 14package org.eclipse.linuxtools.tmf.core.tests.trace;
d18dd09b
ASL
15
16import java.io.File;
17import java.io.IOException;
18import java.net.URISyntaxException;
19import java.net.URL;
20import java.util.Vector;
21
22import junit.framework.TestCase;
23
9e0640dc 24import org.eclipse.core.resources.IFile;
d18dd09b
ASL
25import org.eclipse.core.runtime.FileLocator;
26import org.eclipse.core.runtime.Path;
17324c9a 27import org.eclipse.linuxtools.internal.tmf.core.trace.TmfExperimentContext;
9e0640dc 28import org.eclipse.linuxtools.internal.tmf.core.trace.TmfExperimentLocation;
72f1e62a 29import org.eclipse.linuxtools.tmf.core.event.ITmfEvent;
6c13869b
FC
30import org.eclipse.linuxtools.tmf.core.event.TmfEvent;
31import org.eclipse.linuxtools.tmf.core.event.TmfTimeRange;
32import org.eclipse.linuxtools.tmf.core.event.TmfTimestamp;
b4f71e4a 33import org.eclipse.linuxtools.tmf.core.exceptions.TmfTraceException;
9b749023 34import org.eclipse.linuxtools.tmf.core.request.TmfDataRequest;
6c13869b
FC
35import org.eclipse.linuxtools.tmf.core.request.TmfEventRequest;
36import org.eclipse.linuxtools.tmf.core.tests.TmfCoreTestPlugin;
37import org.eclipse.linuxtools.tmf.core.trace.ITmfContext;
0316808c 38import org.eclipse.linuxtools.tmf.core.trace.ITmfLocation;
6c13869b 39import org.eclipse.linuxtools.tmf.core.trace.ITmfTrace;
9e0640dc
FC
40import org.eclipse.linuxtools.tmf.core.trace.TmfExperiment;
41import org.eclipse.linuxtools.tmf.core.trace.TmfLocation;
42import org.eclipse.linuxtools.tmf.tests.stubs.trace.TmfExperimentStub;
4918b8f2 43import org.eclipse.linuxtools.tmf.tests.stubs.trace.TmfTraceStub;
d18dd09b
ASL
44
45/**
0316808c 46 * Test suite for the TmfExperiment class (single trace).
d18dd09b 47 */
2d223a34 48@SuppressWarnings({ "nls" })
d18dd09b
ASL
49public class TmfExperimentTest extends TestCase {
50
0316808c
FC
51 // ------------------------------------------------------------------------
52 // Attributes
53 // ------------------------------------------------------------------------
54
d18dd09b
ASL
55 private static final String DIRECTORY = "testfiles";
56 private static final String TEST_STREAM = "A-Test-10K";
57 private static final String EXPERIMENT = "MyExperiment";
58 private static int NB_EVENTS = 10000;
0316808c 59 private static int BLOCK_SIZE = 1000;
d18dd09b 60
9e0640dc 61 private ITmfTrace<TmfEvent>[] fTestTraces;
0879b6b9 62 private TmfExperimentStub<ITmfEvent> fExperiment;
d18dd09b
ASL
63
64 private static byte SCALE = (byte) -3;
65
66 // ------------------------------------------------------------------------
67 // Housekeeping
68 // ------------------------------------------------------------------------
69
9e0640dc
FC
70 @SuppressWarnings("unchecked")
71 private synchronized ITmfTrace<?>[] setupTrace(final String path) {
72 if (fTestTraces == null) {
73 fTestTraces = new ITmfTrace[1];
25e48683
FC
74 try {
75 final URL location = FileLocator.find(TmfCoreTestPlugin.getDefault().getBundle(), new Path(path), null);
76 final File test = new File(FileLocator.toFileURL(location).toURI());
20658947 77 final TmfTraceStub trace = new TmfTraceStub(test.getPath(), 0, true);
9e0640dc 78 fTestTraces[0] = trace;
b4f71e4a
FC
79 } catch (final TmfTraceException e) {
80 e.printStackTrace();
25e48683
FC
81 } catch (final URISyntaxException e) {
82 e.printStackTrace();
83 } catch (final IOException e) {
84 e.printStackTrace();
85 }
86 }
9e0640dc 87 return fTestTraces;
d18dd09b
ASL
88 }
89
9e0640dc 90 private synchronized void setupExperiment() {
20658947 91 if (fExperiment == null) {
9e0640dc
FC
92 fExperiment = new TmfExperimentStub<ITmfEvent>(EXPERIMENT, fTestTraces, BLOCK_SIZE);
93 fExperiment.getIndexer().buildIndex(0, TmfTimeRange.ETERNITY, true);
20658947 94 }
d18dd09b
ASL
95 }
96
25e48683
FC
97 public TmfExperimentTest(final String name) throws Exception {
98 super(name);
99 }
d18dd09b 100
25e48683
FC
101 @Override
102 protected void setUp() throws Exception {
103 super.setUp();
104 setupTrace(DIRECTORY + File.separator + TEST_STREAM);
105 setupExperiment();
106 }
d18dd09b 107
25e48683
FC
108 @Override
109 protected void tearDown() throws Exception {
110 super.tearDown();
111 }
d18dd09b
ASL
112
113 // ------------------------------------------------------------------------
114 // Constructor
115 // ------------------------------------------------------------------------
116
9e0640dc
FC
117 public void testSimpleTmfExperimentConstructor() {
118
119 TmfExperiment<TmfEvent> experiment = new TmfExperiment<TmfEvent>(TmfEvent.class, EXPERIMENT, fTestTraces);
120 assertEquals("GetId", EXPERIMENT, experiment.getName());
121 assertEquals("GetCacheSize", TmfExperiment.DEFAULT_INDEX_PAGE_SIZE, experiment.getCacheSize());
122 experiment.dispose();
123
124 experiment = new TmfExperiment<TmfEvent>(TmfEvent.class, EXPERIMENT, null);
125 experiment.dispose();
126 }
127
128 public void testNormalTmfExperimentConstructor() {
cb866e08 129
25e48683 130 assertEquals("GetId", EXPERIMENT, fExperiment.getName());
d18dd09b
ASL
131 assertEquals("GetNbEvents", NB_EVENTS, fExperiment.getNbEvents());
132
07671572
FC
133 final long nbExperimentEvents = fExperiment.getNbEvents();
134 assertEquals("GetNbEvents", NB_EVENTS, nbExperimentEvents);
135
25e48683 136 final long nbTraceEvents = fExperiment.getTraces()[0].getNbEvents();
82e04272
FC
137 assertEquals("GetNbEvents", NB_EVENTS, nbTraceEvents);
138
25e48683 139 final TmfTimeRange timeRange = fExperiment.getTimeRange();
d18dd09b
ASL
140 assertEquals("getStartTime", 1, timeRange.getStartTime().getValue());
141 assertEquals("getEndTime", NB_EVENTS, timeRange.getEndTime().getValue());
25e48683 142 }
d18dd09b 143
9e0640dc
FC
144 @SuppressWarnings("static-access")
145 public void testSetCurrentExperiment() {
146
147 TmfExperiment<TmfEvent> experiment = new TmfExperiment<TmfEvent>(TmfEvent.class, EXPERIMENT, fTestTraces);
148 experiment.setCurrentExperiment(experiment);
149 assertEquals("getCurrentExperiment", experiment, experiment.getCurrentExperiment());
150
151 TmfExperiment<TmfEvent> experiment2 = new TmfExperiment<TmfEvent>(TmfEvent.class, EXPERIMENT, null);
152 experiment.setCurrentExperiment(experiment2);
153 assertEquals("getCurrentExperiment", experiment2, experiment.getCurrentExperiment());
154
155 experiment.dispose();
156 experiment2.dispose();
157 }
158
159 // ------------------------------------------------------------------------
160 // getTimestamp
161 // ------------------------------------------------------------------------
162
163 public void testGetTimestamp() throws Exception {
17324c9a
FC
164 assertEquals("getTimestamp", new TmfTimestamp( 1, (byte) -3), fExperiment.getTimestamp( 0));
165 assertEquals("getTimestamp", new TmfTimestamp( 2, (byte) -3), fExperiment.getTimestamp( 1));
166 assertEquals("getTimestamp", new TmfTimestamp( 11, (byte) -3), fExperiment.getTimestamp( 10));
167 assertEquals("getTimestamp", new TmfTimestamp( 101, (byte) -3), fExperiment.getTimestamp( 100));
168 assertEquals("getTimestamp", new TmfTimestamp( 1001, (byte) -3), fExperiment.getTimestamp(1000));
169 assertEquals("getTimestamp", new TmfTimestamp( 2001, (byte) -3), fExperiment.getTimestamp(2000));
170 assertEquals("getTimestamp", new TmfTimestamp( 2501, (byte) -3), fExperiment.getTimestamp(2500));
171 assertEquals("getTimestamp", new TmfTimestamp(10000, (byte) -3), fExperiment.getTimestamp(9999));
9e0640dc
FC
172 assertNull("getTimestamp", fExperiment.getTimestamp(10000));
173 }
174
175 // ------------------------------------------------------------------------
176 // Bookmarks file handling
177 // ------------------------------------------------------------------------
178
179 public void testBookmarks() throws Exception {
180 assertNull("GetBookmarksFile", fExperiment.getBookmarksFile());
9e0640dc
FC
181 IFile bookmarks = (IFile) fTestTraces[0].getResource();
182 fExperiment.setBookmarksFile(bookmarks);
183 assertEquals("GetBookmarksFile", bookmarks, fExperiment.getBookmarksFile());
184 }
185
186 // ------------------------------------------------------------------------
187 // seekEvent by location
188 // ------------------------------------------------------------------------
189
9e0640dc 190 public void testSeekBadLocation() throws Exception {
9b749023 191 ITmfContext context = fExperiment.seekEvent(new TmfLocation<Long>(0L));
9e0640dc
FC
192 assertNull("seekEvent", context);
193 }
194
195 public void testSeekNoTrace() throws Exception {
196 TmfExperiment<TmfEvent> experiment = new TmfExperiment<TmfEvent>(TmfEvent.class, EXPERIMENT, null);
9e0640dc
FC
197 ITmfContext context = experiment.seekEvent((TmfExperimentLocation) null);
198 assertNull("seekEvent", context);
199 experiment.dispose();
200 }
201
202 // ------------------------------------------------------------------------
203 // seekEvent on ratio
204 // ------------------------------------------------------------------------
205
206 public void testSeekEventOnRatio() throws Exception {
207
208 // First event
209 ITmfContext context = fExperiment.seekEvent(0.0);
210 assertEquals("Context rank", 0, context.getRank());
211 ITmfEvent event = fExperiment.parseEvent(context);
212 assertEquals("Event timestamp", 1, event.getTimestamp().getValue());
213 assertEquals("Context rank", 0, context.getRank());
214
215 // Middle event
216 int midTrace = NB_EVENTS / 2;
217 context = fExperiment.seekEvent(0.5);
218 assertEquals("Context rank", midTrace, context.getRank());
219 event = fExperiment.parseEvent(context);
220 assertEquals("Event timestamp", midTrace + 1, event.getTimestamp().getValue());
221 assertEquals("Context rank", midTrace, context.getRank());
9b749023 222
9e0640dc
FC
223 // Last event
224 context = fExperiment.seekEvent(1.0);
225 assertEquals("Context rank", NB_EVENTS, context.getRank());
226 event = fExperiment.parseEvent(context);
227 assertNull("Event timestamp", event);
228 assertEquals("Context rank", NB_EVENTS, context.getRank());
9b749023 229
9e0640dc
FC
230 // Beyond last event
231 context = fExperiment.seekEvent(1.1);
232 assertEquals("Context rank", NB_EVENTS, context.getRank());
233 event = fExperiment.parseEvent(context);
234 assertNull("Event timestamp", event);
235 assertEquals("Context rank", NB_EVENTS, context.getRank());
9b749023 236
9e0640dc
FC
237 // Negative ratio
238 context = fExperiment.seekEvent(-0.5);
239 assertEquals("Context rank", 0, context.getRank());
240 event = fExperiment.parseEvent(context);
241 assertEquals("Event timestamp", 1, event.getTimestamp().getValue());
242 assertEquals("Context rank", 0, context.getRank());
243 }
244
0879b6b9 245 @SuppressWarnings("rawtypes")
9e0640dc
FC
246 public void testGetLocationRatio() throws Exception {
247
248 // First event
249 ITmfContext context = fExperiment.seekEvent((ITmfLocation) null);
250 double ratio = fExperiment.getLocationRatio(context.getLocation());
5cc97265
FC
251 context = fExperiment.seekEvent(ratio);
252 double ratio2 = fExperiment.getLocationRatio(context.getLocation());
253 assertEquals("getLocationRatio", ratio, ratio2);
9e0640dc
FC
254
255 // Middle event
256 context = fExperiment.seekEvent(NB_EVENTS / 2);
257 ratio = fExperiment.getLocationRatio(context.getLocation());
5cc97265
FC
258 context = fExperiment.seekEvent(ratio);
259 ratio2 = fExperiment.getLocationRatio(context.getLocation());
260 assertEquals("getLocationRatio", ratio, ratio2);
9e0640dc
FC
261
262 // Last event
263 context = fExperiment.seekEvent(NB_EVENTS - 1);
264 ratio = fExperiment.getLocationRatio(context.getLocation());
5cc97265
FC
265 context = fExperiment.seekEvent(ratio);
266 ratio2 = fExperiment.getLocationRatio(context.getLocation());
267 assertEquals("getLocationRatio", ratio, ratio2);
9e0640dc
FC
268 }
269
17324c9a 270// @SuppressWarnings("rawtypes")
9e0640dc
FC
271// public void testGetCurrentLocation() throws Exception {
272// ITmfContext context = fExperiment.seekEvent((ITmfLocation) null);
273// ITmfLocation location = fExperiment.getCurrentLocation();
274// assertEquals("getCurrentLocation", location, context.getLocation());
275// }
276
d18dd09b 277 // ------------------------------------------------------------------------
0316808c 278 // seekEvent on rank
d18dd09b
ASL
279 // ------------------------------------------------------------------------
280
0316808c 281 public void testSeekRankOnCacheBoundary() throws Exception {
d18dd09b 282
0316808c 283 long cacheSize = fExperiment.getCacheSize();
9b635e61 284
0316808c
FC
285 // On lower bound, returns the first event (TS = 1)
286 ITmfContext context = fExperiment.seekEvent(0);
287 assertEquals("Context rank", 0, context.getRank());
25e48683 288
c32744d6 289 ITmfEvent event = fExperiment.getNext(context);
d18dd09b 290 assertEquals("Event timestamp", 1, event.getTimestamp().getValue());
0316808c 291 assertEquals("Context rank", 1, context.getRank());
25e48683 292
0316808c
FC
293 // Position trace at event rank [cacheSize]
294 context = fExperiment.seekEvent(cacheSize);
295 assertEquals("Context rank", cacheSize, context.getRank());
25e48683 296
c32744d6 297 event = fExperiment.getNext(context);
0316808c
FC
298 assertEquals("Event timestamp", cacheSize + 1, event.getTimestamp().getValue());
299 assertEquals("Context rank", cacheSize + 1, context.getRank());
d18dd09b 300
0316808c
FC
301 // Position trace at event rank [4 * cacheSize]
302 context = fExperiment.seekEvent(4 * cacheSize);
303 assertEquals("Context rank", 4 * cacheSize, context.getRank());
25e48683 304
c32744d6 305 event = fExperiment.getNext(context);
0316808c
FC
306 assertEquals("Event timestamp", 4 * cacheSize + 1, event.getTimestamp().getValue());
307 assertEquals("Context rank", 4 * cacheSize + 1, context.getRank());
d18dd09b
ASL
308 }
309
0316808c 310 public void testSeekRankNotOnCacheBoundary() throws Exception {
d18dd09b 311
0316808c 312 long cacheSize = fExperiment.getCacheSize();
25e48683 313
0316808c
FC
314 // Position trace at event rank 9
315 ITmfContext context = fExperiment.seekEvent(9);
316 assertEquals("Context rank", 9, context.getRank());
25e48683 317
c32744d6 318 ITmfEvent event = fExperiment.getNext(context);
d18dd09b 319 assertEquals("Event timestamp", 10, event.getTimestamp().getValue());
0316808c 320 assertEquals("Context rank", 10, context.getRank());
d18dd09b 321
0316808c
FC
322 // Position trace at event rank [cacheSize - 1]
323 context = fExperiment.seekEvent(cacheSize - 1);
324 assertEquals("Context rank", cacheSize - 1, context.getRank());
25e48683 325
c32744d6 326 event = fExperiment.getNext(context);
0316808c
FC
327 assertEquals("Event timestamp", cacheSize, event.getTimestamp().getValue());
328 assertEquals("Context rank", cacheSize, context.getRank());
d18dd09b 329
0316808c
FC
330 // Position trace at event rank [cacheSize + 1]
331 context = fExperiment.seekEvent(cacheSize + 1);
332 assertEquals("Context rank", cacheSize + 1, context.getRank());
25e48683 333
c32744d6 334 event = fExperiment.getNext(context);
0316808c
FC
335 assertEquals("Event timestamp", cacheSize + 2, event.getTimestamp().getValue());
336 assertEquals("Context rank", cacheSize + 2, context.getRank());
d18dd09b 337
25e48683 338 // Position trace at event rank 4500
0316808c
FC
339 context = fExperiment.seekEvent(4500);
340 assertEquals("Context rank", 4500, context.getRank());
25e48683 341
c32744d6 342 event = fExperiment.getNext(context);
d18dd09b 343 assertEquals("Event timestamp", 4501, event.getTimestamp().getValue());
0316808c 344 assertEquals("Context rank", 4501, context.getRank());
d18dd09b
ASL
345 }
346
0316808c 347 public void testSeekRankOutOfScope() throws Exception {
d18dd09b 348
25e48683 349 // Position trace at beginning
0316808c 350 ITmfContext context = fExperiment.seekEvent(-1);
9b635e61 351 assertEquals("Event rank", 0, context.getRank());
25e48683 352
c32744d6 353 ITmfEvent event = fExperiment.getNext(context);
d18dd09b 354 assertEquals("Event timestamp", 1, event.getTimestamp().getValue());
0316808c 355 assertEquals("Context rank", 1, context.getRank());
d18dd09b 356
25e48683 357 // Position trace at event passed the end
0316808c
FC
358 context = fExperiment.seekEvent(NB_EVENTS);
359 assertEquals("Context rank", NB_EVENTS, context.getRank());
360
c32744d6 361 event = fExperiment.getNext(context);
0316808c
FC
362 assertNull("Event", event);
363 assertEquals("Context rank", NB_EVENTS, context.getRank());
d18dd09b
ASL
364 }
365
366 // ------------------------------------------------------------------------
367 // seekEvent on timestamp
368 // ------------------------------------------------------------------------
369
0316808c 370 public void testSeekTimestampOnCacheBoundary() throws Exception {
d18dd09b 371
0316808c 372 long cacheSize = fExperiment.getCacheSize();
25e48683 373
0316808c
FC
374 // Position trace at event rank 0
375 ITmfContext context = fExperiment.seekEvent(new TmfTimestamp(1, SCALE, 0));
376 assertEquals("Context rank", 0, context.getRank());
25e48683 377
c32744d6 378 ITmfEvent event = fExperiment.getNext(context);
d18dd09b 379 assertEquals("Event timestamp", 1, event.getTimestamp().getValue());
0316808c 380 assertEquals("Context rank", 1, context.getRank());
25e48683 381
0316808c
FC
382 // Position trace at event rank [cacheSize]
383 context = fExperiment.seekEvent(new TmfTimestamp(cacheSize + 1, SCALE, 0));
384 assertEquals("Event rank", cacheSize, context.getRank());
25e48683 385
c32744d6 386 event = fExperiment.getNext(context);
0316808c
FC
387 assertEquals("Event timestamp", cacheSize + 1, event.getTimestamp().getValue());
388 assertEquals("Context rank", cacheSize + 1, context.getRank());
d18dd09b 389
0316808c
FC
390 // Position trace at event rank [4 * cacheSize]
391 context = fExperiment.seekEvent(new TmfTimestamp(4 * cacheSize + 1, SCALE, 0));
392 assertEquals("Context rank", 4 * cacheSize, context.getRank());
25e48683 393
c32744d6 394 event = fExperiment.getNext(context);
0316808c
FC
395 assertEquals("Event timestamp", 4 * cacheSize + 1, event.getTimestamp().getValue());
396 assertEquals("Context rank", 4 * cacheSize + 1, context.getRank());
d18dd09b
ASL
397 }
398
0316808c 399 public void testSeekTimestampNotOnCacheBoundary() throws Exception {
d18dd09b 400
0316808c
FC
401 // Position trace at event rank 1 (TS = 2)
402 ITmfContext context = fExperiment.seekEvent(new TmfTimestamp(2, SCALE, 0));
403 assertEquals("Context rank", 1, context.getRank());
25e48683 404
c32744d6 405 ITmfEvent event = fExperiment.getNext(context);
d18dd09b 406 assertEquals("Event timestamp", 2, event.getTimestamp().getValue());
0316808c 407 assertEquals("Context rank", 2, context.getRank());
d18dd09b 408
0316808c 409 // Position trace at event rank 9 (TS = 10)
25e48683 410 context = fExperiment.seekEvent(new TmfTimestamp(10, SCALE, 0));
0316808c 411 assertEquals("Context rank", 9, context.getRank());
25e48683 412
c32744d6 413 event = fExperiment.getNext(context);
d18dd09b 414 assertEquals("Event timestamp", 10, event.getTimestamp().getValue());
0316808c 415 assertEquals("Context rank", 10, context.getRank());
d18dd09b 416
0316808c 417 // Position trace at event rank 999 (TS = 1000)
d18dd09b 418 context = fExperiment.seekEvent(new TmfTimestamp(1000, SCALE, 0));
0316808c 419 assertEquals("Context rank", 999, context.getRank());
25e48683 420
c32744d6 421 event = fExperiment.getNext(context);
d18dd09b 422 assertEquals("Event timestamp", 1000, event.getTimestamp().getValue());
0316808c 423 assertEquals("Context rank", 1000, context.getRank());
d18dd09b 424
0316808c 425 // Position trace at event rank 1001 (TS = 1002)
d18dd09b 426 context = fExperiment.seekEvent(new TmfTimestamp(1002, SCALE, 0));
0316808c 427 assertEquals("Context rank", 1001, context.getRank());
25e48683 428
c32744d6 429 event = fExperiment.getNext(context);
d18dd09b 430 assertEquals("Event timestamp", 1002, event.getTimestamp().getValue());
0316808c 431 assertEquals("Context rank", 1002, context.getRank());
d18dd09b 432
0316808c 433 // Position trace at event rank 4500 (TS = 4501)
d18dd09b 434 context = fExperiment.seekEvent(new TmfTimestamp(4501, SCALE, 0));
0316808c 435 assertEquals("Context rank", 4500, context.getRank());
25e48683 436
c32744d6 437 event = fExperiment.getNext(context);
d18dd09b 438 assertEquals("Event timestamp", 4501, event.getTimestamp().getValue());
0316808c 439 assertEquals("Context rank", 4501, context.getRank());
d18dd09b
ASL
440 }
441
0316808c 442 public void testSeekTimestampOutOfScope() throws Exception {
d18dd09b 443
25e48683 444 // Position trace at beginning
0316808c 445 ITmfContext context = fExperiment.seekEvent(new TmfTimestamp(-1, SCALE, 0));
d18dd09b 446 assertEquals("Event rank", 0, context.getRank());
25e48683 447
c32744d6 448 ITmfEvent event = fExperiment.getNext(context);
d18dd09b
ASL
449 assertEquals("Event timestamp", 1, event.getTimestamp().getValue());
450 assertEquals("Event rank", 1, context.getRank());
451
25e48683 452 // Position trace at event passed the end
d18dd09b 453 context = fExperiment.seekEvent(new TmfTimestamp(NB_EVENTS + 1, SCALE, 0));
c32744d6 454 event = fExperiment.getNext(context);
0316808c 455 assertNull("Event location", event);
9b635e61 456 assertEquals("Event rank", ITmfContext.UNKNOWN_RANK, context.getRank());
d18dd09b
ASL
457 }
458
459 // ------------------------------------------------------------------------
0316808c 460 // seekEvent by location (context rank is undefined)
d18dd09b
ASL
461 // ------------------------------------------------------------------------
462
0316808c 463 public void testSeekLocationOnCacheBoundary() throws Exception {
9b749023 464
0316808c 465 long cacheSize = fExperiment.getCacheSize();
d18dd09b 466
0316808c
FC
467 // Position trace at event rank 0
468 ITmfContext tmpContext = fExperiment.seekEvent(0);
469 ITmfContext context = fExperiment.seekEvent(tmpContext.getLocation());
25e48683 470
c32744d6 471 ITmfEvent event = fExperiment.getNext(context);
d18dd09b 472 assertEquals("Event timestamp", 1, event.getTimestamp().getValue());
25e48683 473
c32744d6 474 event = fExperiment.getNext(context);
0316808c 475 assertEquals("Event timestamp", 2, event.getTimestamp().getValue());
d18dd09b 476
0316808c
FC
477 // Position trace at event rank 'cacheSize'
478 tmpContext = fExperiment.seekEvent(cacheSize);
479 context = fExperiment.seekEvent(tmpContext.getLocation());
25e48683 480
c32744d6 481 event = fExperiment.getNext(context);
0316808c 482 assertEquals("Event timestamp", cacheSize + 1, event.getTimestamp().getValue());
25e48683 483
c32744d6 484 event = fExperiment.getNext(context);
0316808c 485 assertEquals("Event timestamp", cacheSize + 2, event.getTimestamp().getValue());
d18dd09b 486
0316808c
FC
487 // Position trace at event rank 4 * 'cacheSize'
488 tmpContext = fExperiment.seekEvent(4 * cacheSize);
489 context = fExperiment.seekEvent(tmpContext.getLocation());
25e48683 490
c32744d6 491 event = fExperiment.getNext(context);
0316808c 492 assertEquals("Event timestamp", 4 * cacheSize + 1, event.getTimestamp().getValue());
25e48683 493
c32744d6 494 event = fExperiment.getNext(context);
0316808c 495 assertEquals("Event timestamp", 4 * cacheSize + 2, event.getTimestamp().getValue());
d18dd09b
ASL
496 }
497
0316808c 498 public void testSeekLocationNotOnCacheBoundary() throws Exception {
25e48683 499
0316808c 500 long cacheSize = fExperiment.getCacheSize();
25e48683 501
0316808c
FC
502 // Position trace at event 'cacheSize' - 1
503 ITmfContext tmpContext = fExperiment.seekEvent(cacheSize - 1);
504 ITmfContext context = fExperiment.seekEvent(tmpContext.getLocation());
d18dd09b 505
c32744d6 506 ITmfEvent event = fExperiment.getNext(context);
0316808c 507 assertEquals("Event timestamp", cacheSize, event.getTimestamp().getValue());
25e48683 508
c32744d6 509 event = fExperiment.getNext(context);
0316808c 510 assertEquals("Event timestamp", cacheSize + 1, event.getTimestamp().getValue());
d18dd09b 511
0316808c
FC
512 // Position trace at event rank 2 * 'cacheSize' - 1
513 tmpContext = fExperiment.seekEvent(2 * cacheSize - 1);
514 context = fExperiment.seekEvent(tmpContext.getLocation());
515 context = fExperiment.seekEvent(2 * cacheSize - 1);
25e48683 516
c32744d6 517 event = fExperiment.getNext(context);
0316808c 518 assertEquals("Event timestamp", 2 * cacheSize, event.getTimestamp().getValue());
25e48683 519
c32744d6 520 event = fExperiment.getNext(context);
0316808c 521 assertEquals("Event timestamp", 2 * cacheSize + 1, event.getTimestamp().getValue());
d18dd09b 522
25e48683 523 // Position trace at event rank 4500
0316808c
FC
524 tmpContext = fExperiment.seekEvent(4500);
525 context = fExperiment.seekEvent(tmpContext.getLocation());
25e48683 526
c32744d6 527 event = fExperiment.getNext(context);
d18dd09b 528 assertEquals("Event timestamp", 4501, event.getTimestamp().getValue());
25e48683 529
c32744d6 530 event = fExperiment.getNext(context);
0316808c 531 assertEquals("Event timestamp", 4502, event.getTimestamp().getValue());
d18dd09b
ASL
532 }
533
0316808c 534 public void testSeekLocationOutOfScope() throws Exception {
d18dd09b 535
25e48683 536 // Position trace at beginning
0316808c 537 ITmfContext context = fExperiment.seekEvent((ITmfLocation<?>) null);
25e48683 538
c32744d6 539 ITmfEvent event = fExperiment.getNext(context);
d18dd09b 540 assertEquals("Event timestamp", 1, event.getTimestamp().getValue());
d18dd09b 541 }
25e48683 542
9b635e61 543 // ------------------------------------------------------------------------
17324c9a 544 // getNext - updates the context
9b635e61
FC
545 // ------------------------------------------------------------------------
546
17324c9a
FC
547 private void validateContextRanks(ITmfContext context) {
548 assertTrue("Experiment context type", context instanceof TmfExperimentContext);
549 TmfExperimentContext ctx = (TmfExperimentContext) context;
9b635e61 550
17324c9a
FC
551 int nbTraces = ctx.getContexts().length;
552
553 // expRank = sum(trace ranks) - nbTraces + 1 (if lastTraceRead != NO_TRACE)
554 long expRank = -nbTraces + ((ctx.getLastTrace() != TmfExperimentContext.NO_TRACE) ? 1 : 0);
555 for (int i = 0; i < nbTraces; i++) {
556 long rank = ctx.getContexts()[i].getRank();
557 if (rank == -1) {
558 expRank = -1;
559 break;
560 }
561 expRank += rank;
562 }
563 assertEquals("Experiment context rank", expRank, ctx.getRank());
564 }
565
566 public void testGetNextAfteSeekingOnTS_1() throws Exception {
9b635e61 567
17324c9a
FC
568 final long INITIAL_TS = 1;
569 final int NB_READS = 20;
570
571 // On lower bound, returns the first event (ts = 1)
572 final ITmfContext context = fExperiment.seekEvent(new TmfTimestamp(INITIAL_TS, SCALE, 0));
573
574 validateContextRanks(context);
575
576 // Read NB_EVENTS
577 ITmfEvent event;
578 for (int i = 0; i < NB_READS; i++) {
c32744d6 579 event = fExperiment.getNext(context);
17324c9a
FC
580 assertEquals("Event timestamp", INITIAL_TS + i, event.getTimestamp().getValue());
581 assertEquals("Event rank", INITIAL_TS + i, context.getRank());
9b635e61 582 }
17324c9a
FC
583
584 // Make sure we stay positioned
585 event = fExperiment.parseEvent(context);
586 assertEquals("Event timestamp", INITIAL_TS + NB_READS, event.getTimestamp().getValue());
587 assertEquals("Event rank", INITIAL_TS + NB_READS - 1, context.getRank());
588
589 validateContextRanks(context);
9b635e61
FC
590 }
591
17324c9a
FC
592 public void testGetNextAfteSeekingOnTS_2() throws Exception {
593
594 final long INITIAL_TS = 2;
595 final int NB_READS = 20;
596
597 // On lower bound, returns the first event (ts = 2)
598 final ITmfContext context = fExperiment.seekEvent(new TmfTimestamp(INITIAL_TS, SCALE, 0));
599
600 validateContextRanks(context);
601
602 // Read NB_EVENTS
603 ITmfEvent event;
604 for (int i = 0; i < NB_READS; i++) {
605 event = fExperiment.getNext(context);
606 assertEquals("Event timestamp", INITIAL_TS + i, event.getTimestamp().getValue());
607 assertEquals("Event rank", INITIAL_TS + i, context.getRank());
608 }
609
610 // Make sure we stay positioned
611 event = fExperiment.parseEvent(context);
612 assertEquals("Event timestamp", INITIAL_TS + NB_READS, event.getTimestamp().getValue());
613 assertEquals("Event rank", INITIAL_TS + NB_READS - 1, context.getRank());
614
615 validateContextRanks(context);
616 }
617
618 public void testGetNextAfteSeekingOnTS_3() throws Exception {
619
620 final long INITIAL_TS = 500;
621 final int NB_READS = 20;
622
623 // On lower bound, returns the first event (ts = 500)
624 final ITmfContext context = fExperiment.seekEvent(new TmfTimestamp(INITIAL_TS, SCALE, 0));
625
626 validateContextRanks(context);
627
628 // Read NB_EVENTS
629 ITmfEvent event;
630 for (int i = 0; i < NB_READS; i++) {
631 event = fExperiment.getNext(context);
632 assertEquals("Event timestamp", INITIAL_TS + i, event.getTimestamp().getValue());
633 assertEquals("Event rank", INITIAL_TS + i, context.getRank());
634 }
635
636 // Make sure we stay positioned
637 event = fExperiment.parseEvent(context);
638 assertEquals("Event timestamp", INITIAL_TS + NB_READS, event.getTimestamp().getValue());
639 assertEquals("Event rank", INITIAL_TS + NB_READS - 1, context.getRank());
640
641 validateContextRanks(context);
642 }
643
644 public void testGetNextAfterSeekingOnRank_1() throws Exception {
645
646 final long INITIAL_RANK = 0L;
647 final int NB_READS = 20;
648
649 // On lower bound, returns the first event (rank = 0)
650 final ITmfContext context = fExperiment.seekEvent(INITIAL_RANK);
651
652 validateContextRanks(context);
653
654 // Read NB_EVENTS
655 ITmfEvent event;
656 for (int i = 0; i < NB_READS; i++) {
657 event = fExperiment.getNext(context);
658 assertEquals("Event timestamp", INITIAL_RANK + i + 1, event.getTimestamp().getValue());
659 assertEquals("Event rank", INITIAL_RANK + i + 1, context.getRank());
660 }
661
662 // Make sure we stay positioned
663 event = fExperiment.parseEvent(context);
664 assertEquals("Event timestamp", INITIAL_RANK + NB_READS + 1, event.getTimestamp().getValue());
665 assertEquals("Event rank", INITIAL_RANK + NB_READS, context.getRank());
666
667 validateContextRanks(context);
668 }
669
670 public void testGetNextAfterSeekingOnRank_2() throws Exception {
671
672 final long INITIAL_RANK = 1L;
673 final int NB_READS = 20;
674
675 // On lower bound, returns the first event (rank = 0)
676 final ITmfContext context = fExperiment.seekEvent(INITIAL_RANK);
677
678 validateContextRanks(context);
679
680 // Read NB_EVENTS
681 ITmfEvent event;
682 for (int i = 0; i < NB_READS; i++) {
683 event = fExperiment.getNext(context);
684 assertEquals("Event timestamp", INITIAL_RANK + i + 1, event.getTimestamp().getValue());
685 assertEquals("Event rank", INITIAL_RANK + i + 1, context.getRank());
686 }
687
688 // Make sure we stay positioned
689 event = fExperiment.parseEvent(context);
690 assertEquals("Event timestamp", INITIAL_RANK + NB_READS + 1, event.getTimestamp().getValue());
691 assertEquals("Event rank", INITIAL_RANK + NB_READS, context.getRank());
692
693 validateContextRanks(context);
694 }
695
696 public void testGetNextAfterSeekingOnRank_3() throws Exception {
697
698 final long INITIAL_RANK = 500L;
699 final int NB_READS = 20;
700
701 // On lower bound, returns the first event (rank = 0)
702 final ITmfContext context = fExperiment.seekEvent(INITIAL_RANK);
703
704 validateContextRanks(context);
705
706 // Read NB_EVENTS
707 ITmfEvent event;
708 for (int i = 0; i < NB_READS; i++) {
709 event = fExperiment.getNext(context);
710 assertEquals("Event timestamp", INITIAL_RANK + i + 1, event.getTimestamp().getValue());
711 assertEquals("Event rank", INITIAL_RANK + i + 1, context.getRank());
712 }
713
714 // Make sure we stay positioned
715 event = fExperiment.parseEvent(context);
716 assertEquals("Event timestamp", INITIAL_RANK + NB_READS + 1, event.getTimestamp().getValue());
717 assertEquals("Event rank", INITIAL_RANK + NB_READS, context.getRank());
718
719 validateContextRanks(context);
720 }
721
722 public void testGetNextAfterSeekingOnLocation_1() throws Exception {
723
724 final ITmfLocation<?> INITIAL_LOC = null;
725 final long INITIAL_TS = 1;
726 final int NB_READS = 20;
727
728 // On lower bound, returns the first event (ts = 1)
729 final ITmfContext context = fExperiment.seekEvent(INITIAL_LOC);
730
731 validateContextRanks(context);
732
733 // Read NB_EVENTS
734 ITmfEvent event;
735 for (int i = 0; i < NB_READS; i++) {
736 event = fExperiment.getNext(context);
737 assertEquals("Event timestamp", INITIAL_TS + i, event.getTimestamp().getValue());
738 assertEquals("Event rank", INITIAL_TS + i, context.getRank());
739 }
740
741 // Make sure we stay positioned
742 event = fExperiment.parseEvent(context);
743 assertEquals("Event timestamp", INITIAL_TS + NB_READS, event.getTimestamp().getValue());
744 assertEquals("Event rank", INITIAL_TS + NB_READS - 1, context.getRank());
745
746 validateContextRanks(context);
747 }
748
749 public void testGetNextAfterSeekingOnLocation_2() throws Exception {
750
751 final ITmfLocation<?> INITIAL_LOC = fExperiment.seekEvent(1L).getLocation();
752 final long INITIAL_TS = 2;
753 final int NB_READS = 20;
754
755 // On lower bound, returns the first event (ts = 2)
756 final ITmfContext context = fExperiment.seekEvent(INITIAL_LOC);
757
758 validateContextRanks(context);
759
760 // Read NB_EVENTS
761 ITmfEvent event;
762 for (int i = 0; i < NB_READS; i++) {
763 event = fExperiment.getNext(context);
764 assertEquals("Event timestamp", INITIAL_TS + i, event.getTimestamp().getValue());
765 }
766
767 // Make sure we stay positioned
768 event = fExperiment.parseEvent(context);
769 assertEquals("Event timestamp", INITIAL_TS + NB_READS, event.getTimestamp().getValue());
770
771 validateContextRanks(context);
772 }
773
774 public void testGetNextAfterSeekingOnLocation_3() throws Exception {
775
776 final ITmfLocation<?> INITIAL_LOC = fExperiment.seekEvent(500L).getLocation();
777 final long INITIAL_TS = 501;
778 final int NB_READS = 20;
779
780 // On lower bound, returns the first event (ts = 501)
781 final ITmfContext context = fExperiment.seekEvent(INITIAL_LOC);
782
783 validateContextRanks(context);
784
785 // Read NB_EVENTS
786 ITmfEvent event;
787 for (int i = 0; i < NB_READS; i++) {
788 event = fExperiment.getNext(context);
789 assertEquals("Event timestamp", INITIAL_TS + i, event.getTimestamp().getValue());
790 }
791
792 // Make sure we stay positioned
793 event = fExperiment.parseEvent(context);
794 assertEquals("Event timestamp", INITIAL_TS + NB_READS, event.getTimestamp().getValue());
795
796 validateContextRanks(context);
797 }
798
799 public void testGetNextLocation() throws Exception {
800 ITmfContext context1 = fExperiment.seekEvent(0);
801 fExperiment.getNext(context1);
802 ITmfLocation<?> location = context1.getLocation().clone();
803 ITmfEvent event1 = fExperiment.getNext(context1);
804 ITmfContext context2 = fExperiment.seekEvent(location);
805 ITmfEvent event2 = fExperiment.getNext(context2);
806 assertEquals("Event timestamp", event1.getTimestamp().getValue(), event2.getTimestamp().getValue());
807 }
808
809 public void testGetNextEndLocation() throws Exception {
810 ITmfContext context1 = fExperiment.seekEvent(fExperiment.getNbEvents() - 1);
811 fExperiment.getNext(context1);
812 ITmfLocation<?> location = context1.getLocation().clone();
813 ITmfContext context2 = fExperiment.seekEvent(location);
814 ITmfEvent event = fExperiment.getNext(context2);
815 assertNull("Event", event);
816 }
817
d18dd09b
ASL
818 // ------------------------------------------------------------------------
819 // processRequest
820 // ------------------------------------------------------------------------
821
822 public void testProcessRequestForNbEvents() throws Exception {
cb866e08 823
25e48683 824 final int blockSize = 100;
d18dd09b
ASL
825 final int nbEvents = 1000;
826 final Vector<TmfEvent> requestedEvents = new Vector<TmfEvent>();
827
25e48683 828 final TmfTimeRange range = new TmfTimeRange(TmfTimestamp.BIG_BANG, TmfTimestamp.BIG_CRUNCH);
d18dd09b
ASL
829 final TmfEventRequest<TmfEvent> request = new TmfEventRequest<TmfEvent>(TmfEvent.class, range, nbEvents, blockSize) {
830 @Override
25e48683
FC
831 public void handleData(final TmfEvent event) {
832 super.handleData(event);
f9673903 833 requestedEvents.add(event);
d18dd09b
ASL
834 }
835 };
836 fExperiment.sendRequest(request);
837 request.waitForCompletion();
838
839 assertEquals("nbEvents", nbEvents, requestedEvents.size());
840 assertTrue("isCompleted", request.isCompleted());
841 assertFalse("isCancelled", request.isCancelled());
842
843 // Ensure that we have distinct events.
25e48683 844 // Don't go overboard: we are not validating the stub!
20658947 845 for (int i = 0; i < nbEvents; i++) {
d18dd09b 846 assertEquals("Distinct events", i+1, requestedEvents.get(i).getTimestamp().getValue());
20658947 847 }
d18dd09b 848 }
25e48683 849
d18dd09b 850 public void testProcessRequestForNbEvents2() throws Exception {
cb866e08 851
25e48683 852 final int blockSize = 2 * NB_EVENTS;
d18dd09b
ASL
853 final int nbEvents = 1000;
854 final Vector<TmfEvent> requestedEvents = new Vector<TmfEvent>();
855
25e48683 856 final TmfTimeRange range = new TmfTimeRange(TmfTimestamp.BIG_BANG, TmfTimestamp.BIG_CRUNCH);
d18dd09b 857 final TmfEventRequest<TmfEvent> request = new TmfEventRequest<TmfEvent>(TmfEvent.class, range, nbEvents, blockSize) {
25e48683
FC
858 @Override
859 public void handleData(final TmfEvent event) {
860 super.handleData(event);
861 requestedEvents.add(event);
862 }
d18dd09b
ASL
863 };
864 fExperiment.sendRequest(request);
865 request.waitForCompletion();
866
867 assertEquals("nbEvents", nbEvents, requestedEvents.size());
868 assertTrue("isCompleted", request.isCompleted());
869 assertFalse("isCancelled", request.isCancelled());
870
871 // Ensure that we have distinct events.
25e48683 872 // Don't go overboard: we are not validating the stub!
20658947 873 for (int i = 0; i < nbEvents; i++) {
d18dd09b 874 assertEquals("Distinct events", i+1, requestedEvents.get(i).getTimestamp().getValue());
20658947 875 }
d18dd09b 876 }
25e48683 877
d18dd09b 878 public void testProcessRequestForAllEvents() throws Exception {
cb866e08 879
9b749023 880 final int nbEvents = TmfDataRequest.ALL_DATA;
d18dd09b
ASL
881 final int blockSize = 1;
882 final Vector<TmfEvent> requestedEvents = new Vector<TmfEvent>();
25e48683 883 final long nbExpectedEvents = NB_EVENTS;
d18dd09b 884
25e48683 885 final TmfTimeRange range = new TmfTimeRange(TmfTimestamp.BIG_BANG, TmfTimestamp.BIG_CRUNCH);
d18dd09b 886 final TmfEventRequest<TmfEvent> request = new TmfEventRequest<TmfEvent>(TmfEvent.class, range, nbEvents, blockSize) {
25e48683
FC
887 @Override
888 public void handleData(final TmfEvent event) {
889 super.handleData(event);
890 requestedEvents.add(event);
891 }
d18dd09b
ASL
892 };
893 fExperiment.sendRequest(request);
894 request.waitForCompletion();
895
896 assertEquals("nbEvents", nbExpectedEvents, requestedEvents.size());
897 assertTrue("isCompleted", request.isCompleted());
898 assertFalse("isCancelled", request.isCancelled());
899
900 // Ensure that we have distinct events.
25e48683 901 // Don't go overboard: we are not validating the stub!
20658947 902 for (int i = 0; i < nbExpectedEvents; i++) {
d18dd09b 903 assertEquals("Distinct events", i+1, requestedEvents.get(i).getTimestamp().getValue());
20658947 904 }
d18dd09b 905 }
25e48683 906
d18dd09b
ASL
907 // ------------------------------------------------------------------------
908 // cancel
909 // ------------------------------------------------------------------------
910
911 public void testCancel() throws Exception {
cb866e08 912
25e48683 913 final int nbEvents = NB_EVENTS;
0316808c 914 final int blockSize = BLOCK_SIZE;
d18dd09b
ASL
915 final Vector<TmfEvent> requestedEvents = new Vector<TmfEvent>();
916
25e48683 917 final TmfTimeRange range = new TmfTimeRange(TmfTimestamp.BIG_BANG, TmfTimestamp.BIG_CRUNCH);
d18dd09b 918 final TmfEventRequest<TmfEvent> request = new TmfEventRequest<TmfEvent>(TmfEvent.class, range, nbEvents, blockSize) {
25e48683
FC
919 int nbRead = 0;
920 @Override
921 public void handleData(final TmfEvent event) {
922 super.handleData(event);
923 requestedEvents.add(event);
20658947 924 if (++nbRead == blockSize) {
25e48683 925 cancel();
20658947 926 }
25e48683 927 }
cb866e08
FC
928 @Override
929 public void handleCancel() {
20658947 930 if (requestedEvents.size() < blockSize) {
25e48683 931 System.out.println("aie");
20658947 932 }
cb866e08 933 }
d18dd09b
ASL
934 };
935 fExperiment.sendRequest(request);
936 request.waitForCompletion();
937
938 assertEquals("nbEvents", blockSize, requestedEvents.size());
939 assertTrue("isCompleted", request.isCompleted());
940 assertTrue("isCancelled", request.isCancelled());
941 }
25e48683 942
17324c9a 943}
This page took 0.093725 seconds and 5 git commands to generate.