1 /*******************************************************************************
2 * Copyright (c) 2013, 2014 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 * Marc-Andre Laperle - Initial API and implementation
11 *******************************************************************************/
13 package org
.eclipse
.linuxtools
.tmf
.core
.tests
.trace
.indexer
;
15 import static org
.junit
.Assert
.assertEquals
;
16 import static org
.junit
.Assert
.assertFalse
;
17 import static org
.junit
.Assert
.assertTrue
;
20 import java
.io
.IOException
;
21 import java
.io
.RandomAccessFile
;
22 import java
.util
.ArrayList
;
23 import java
.util
.Random
;
25 import org
.eclipse
.linuxtools
.internal
.tmf
.core
.trace
.indexer
.ICheckpointCollection
;
26 import org
.eclipse
.linuxtools
.tmf
.core
.timestamp
.TmfTimeRange
;
27 import org
.eclipse
.linuxtools
.tmf
.core
.timestamp
.TmfTimestamp
;
28 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfTrace
;
29 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.indexer
.checkpoint
.TmfCheckpoint
;
30 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.location
.TmfLongLocation
;
31 import org
.eclipse
.linuxtools
.tmf
.tests
.stubs
.trace
.TmfTraceStub
;
32 import org
.junit
.After
;
33 import org
.junit
.Before
;
34 import org
.junit
.Test
;
37 * Common code for ICheckpointCollection test classes
39 * @author Marc-Andre Laperle
41 public abstract class AbstractCheckpointCollectionTest
{
43 private static final String INDEX_FILE_NAME
= "checkpoint.idx"; //$NON-NLS-1$
46 * The number of checkpoints to be inserted in insert tests
48 protected static final int CHECKPOINTS_INSERT_NUM
= 50000;
50 * The collection being tested
52 protected ICheckpointCollection fCheckpointCollection
= null;
54 private TmfTraceStub fTrace
;
55 private File fFile
= new File(INDEX_FILE_NAME
);
58 * Setup the test. Make sure the index is deleted.
62 fTrace
= new TmfTraceStub();
66 fCheckpointCollection
= createCollection();
70 * Tear down the test. Make sure the index is deleted.
73 public void tearDown() {
76 if (fCheckpointCollection
!= null) {
77 fCheckpointCollection
.dispose();
85 * Get the trace being tested.
87 * @return the trace being tested.
89 public ITmfTrace
getTrace() {
94 * Returns whether or not the collection is persisted to disk
96 * @return true if the collection is persisted to disk, false otherwise
98 public boolean isPersistableCollection() {
103 * Get the file used for the index being tested.
105 * @return the file used for the index being tested.
107 public File
getFile() {
112 * Test constructing a new checkpoint collection
115 public void testConstructor() {
116 if (isPersistableCollection()) {
117 assertTrue(fFile
.exists());
119 assertTrue(fCheckpointCollection
.isCreatedFromScratch());
123 * Test constructing a new checkpoint collection, existing file
126 public void testConstructorExistingFile() {
127 if (isPersistableCollection()) {
128 assertTrue(fFile
.exists());
129 fCheckpointCollection
.setIndexComplete();
130 fCheckpointCollection
.dispose();
132 fCheckpointCollection
= createCollection();
133 assertFalse(fCheckpointCollection
.isCreatedFromScratch());
138 * Test that a new checkpoint collection is considered created from scratch
142 public void testIsCreatedFromScratch() {
143 assertTrue(fCheckpointCollection
.isCreatedFromScratch());
144 fCheckpointCollection
.setIndexComplete();
146 if (isPersistableCollection()) {
147 fCheckpointCollection
.dispose();
148 fCheckpointCollection
= createCollection();
149 assertFalse(fCheckpointCollection
.isCreatedFromScratch());
154 * Test setTimeRange, getTimeRange
157 public void testSetGetTimeRange() {
158 if (isPersistableCollection()) {
159 TmfTimeRange timeRange
= new TmfTimeRange(new TmfTimestamp(0), new TmfTimestamp(100));
160 fCheckpointCollection
.setTimeRange(timeRange
);
161 assertEquals(timeRange
, fCheckpointCollection
.getTimeRange());
166 * Create a collection for the test
168 * @return the collection
170 abstract protected ICheckpointCollection
createCollection();
173 * Test setNbEvents, getNbEvents
176 public void testSetGetNbEvents() {
177 if (isPersistableCollection()) {
178 int expected
= 12345;
179 fCheckpointCollection
.setNbEvents(expected
);
180 assertEquals(expected
, fCheckpointCollection
.getNbEvents());
188 public void testSetGetSize() {
189 assertEquals(0, fCheckpointCollection
.size());
190 int expected
= CHECKPOINTS_INSERT_NUM
;
191 for (int i
= 0; i
< expected
; ++i
) {
192 fCheckpointCollection
.insert(new TmfCheckpoint(new TmfTimestamp(0), new TmfLongLocation(0L), 0));
194 assertEquals(expected
, fCheckpointCollection
.size());
201 public void testDelete() {
202 if (isPersistableCollection()) {
203 assertTrue(fFile
.exists());
204 fCheckpointCollection
.delete();
205 assertFalse(fFile
.exists());
210 * Test version change
212 * @throws IOException
216 public void testVersionChange() throws IOException
{
217 fCheckpointCollection
.setIndexComplete();
218 fCheckpointCollection
.dispose();
219 try (RandomAccessFile f
= new RandomAccessFile(fFile
, "rw");) {
223 fCheckpointCollection
= createCollection();
224 assertTrue(fCheckpointCollection
.isCreatedFromScratch());
228 * Test a single insertion
231 public void testInsert() {
232 TmfCheckpoint checkpoint
= new TmfCheckpoint(new TmfTimestamp(12345), new TmfLongLocation(123456L), 0);
233 fCheckpointCollection
.insert(checkpoint
);
235 long found
= fCheckpointCollection
.binarySearch(checkpoint
);
236 assertEquals(0, found
);
240 * Generate many checkpoints and insert them in the collection
242 * @return the list of generated checkpoints
244 protected ArrayList
<Integer
> insertAlot() {
245 for (int i
= 0; i
< CHECKPOINTS_INSERT_NUM
; i
++) {
246 TmfCheckpoint checkpoint
= new TmfCheckpoint(new TmfTimestamp(12345 + i
), new TmfLongLocation(123456L + i
), i
);
247 fCheckpointCollection
.insert(checkpoint
);
250 fCheckpointCollection
.setIndexComplete();
251 if (isPersistableCollection()) {
252 fCheckpointCollection
.dispose();
255 boolean random
= true;
256 ArrayList
<Integer
> list
= new ArrayList
<>();
257 for (int i
= 0; i
< CHECKPOINTS_INSERT_NUM
; i
++) {
259 Random rand
= new Random();
260 list
.add(rand
.nextInt(CHECKPOINTS_INSERT_NUM
));
269 * Test many checkpoint insertions. Make sure they can be found after
270 * re-opening the file
273 public void testInsertAlot() {
274 ArrayList
<Integer
> list
= insertAlot();
276 if (isPersistableCollection()) {
277 fCheckpointCollection
= createCollection();
280 for (int i
= 0; i
< CHECKPOINTS_INSERT_NUM
; i
++) {
281 Integer randomCheckpoint
= list
.get(i
);
282 TmfCheckpoint checkpoint
= new TmfCheckpoint(new TmfTimestamp(12345 + randomCheckpoint
), new TmfLongLocation(123456L + randomCheckpoint
), 0);
283 long found
= fCheckpointCollection
.binarySearch(checkpoint
);
284 assertEquals(randomCheckpoint
.intValue(), found
);
289 * Test many checkpoint insertions using the same timestamp. Make sure they
290 * can be found after re-opening the file
293 public void testInsertSameTimestamp() {
294 for (int i
= 0; i
< CHECKPOINTS_INSERT_NUM
; i
++) {
295 TmfCheckpoint checkpoint
= new TmfCheckpoint(new TmfTimestamp(12345), new TmfLongLocation(123456L + i
), i
);
296 fCheckpointCollection
.insert(checkpoint
);
299 fCheckpointCollection
.setIndexComplete();
300 if (isPersistableCollection()) {
301 fCheckpointCollection
.dispose();
304 boolean random
= true;
305 ArrayList
<Integer
> list
= new ArrayList
<>();
306 for (int i
= 0; i
< CHECKPOINTS_INSERT_NUM
; i
++) {
308 Random rand
= new Random();
309 list
.add(rand
.nextInt(CHECKPOINTS_INSERT_NUM
));
315 if (isPersistableCollection()) {
316 fCheckpointCollection
= createCollection();
319 for (int i
= 0; i
< CHECKPOINTS_INSERT_NUM
; i
++) {
320 Integer randomCheckpoint
= list
.get(i
);
321 TmfCheckpoint checkpoint
= new TmfCheckpoint(new TmfTimestamp(12345), new TmfLongLocation(123456L + randomCheckpoint
), 0);
322 long found
= fCheckpointCollection
.binarySearch(checkpoint
);
323 assertEquals(randomCheckpoint
.intValue(), found
);
328 * Tests that binarySearch find the correct checkpoint when the time stamp
329 * is between checkpoints
332 public void testBinarySearchFindInBetween() {
333 for (long i
= 0; i
< CHECKPOINTS_INSERT_NUM
; i
++) {
334 TmfCheckpoint checkpoint
= new TmfCheckpoint(new TmfTimestamp(2 * i
), new TmfLongLocation(2 * i
), i
);
335 fCheckpointCollection
.insert(checkpoint
);
338 TmfCheckpoint searchedCheckpoint
= new TmfCheckpoint(new TmfTimestamp(123), new TmfLongLocation(123L), 123);
339 int expectedInsertionPoint
= 61;
340 int expectedRank
= -(expectedInsertionPoint
+ 2);
342 long rank
= fCheckpointCollection
.binarySearch(searchedCheckpoint
);
343 assertEquals(expectedRank
, rank
);
348 * Tests that binarySearch finds the correct checkpoint when searching for a
349 * checkpoint with a null location. It should return the previous checkpoint
350 * from the first checkpoint that matches the timestamp.
353 public void testBinarySearchInBetweenSameTimestamp() {
354 int checkpointNum
= 0;
355 for (; checkpointNum
< 100; checkpointNum
++) {
356 TmfCheckpoint checkpoint
= new TmfCheckpoint(new TmfTimestamp(0), new TmfLongLocation(checkpointNum
), checkpointNum
);
357 fCheckpointCollection
.insert(checkpoint
);
360 for (; checkpointNum
< 200; checkpointNum
++) {
361 TmfCheckpoint checkpoint
= new TmfCheckpoint(new TmfTimestamp(1), new TmfLongLocation(checkpointNum
), checkpointNum
);
362 fCheckpointCollection
.insert(checkpoint
);
365 final TmfCheckpoint searchedCheckpoint
= new TmfCheckpoint(new TmfTimestamp(1), null, 0);
367 long found
= fCheckpointCollection
.binarySearch(searchedCheckpoint
);
369 int expectedInsertionPoint
= 99;
370 int expectedRank
= -(expectedInsertionPoint
+ 2);
372 assertEquals(expectedRank
, found
);