Move alltests plugin to the Trace Compass namespace
[deliverable/tracecompass.git] / org.eclipse.tracecompass.tmf.core.tests / src / org / eclipse / linuxtools / tmf / core / tests / trace / indexer / AbstractCheckpointCollectionTest.java
1 /*******************************************************************************
2 * Copyright (c) 2013, 2014 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 * Marc-Andre Laperle - Initial API and implementation
11 *******************************************************************************/
12
13 package org.eclipse.linuxtools.tmf.core.tests.trace.indexer;
14
15 import static org.junit.Assert.assertEquals;
16 import static org.junit.Assert.assertFalse;
17 import static org.junit.Assert.assertTrue;
18
19 import java.io.File;
20 import java.io.IOException;
21 import java.io.RandomAccessFile;
22 import java.util.ArrayList;
23 import java.util.Random;
24
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;
35
36 /**
37 * Common code for ICheckpointCollection test classes
38 *
39 * @author Marc-Andre Laperle
40 */
41 public abstract class AbstractCheckpointCollectionTest {
42
43 private static final String INDEX_FILE_NAME = "checkpoint.idx"; //$NON-NLS-1$
44
45 /**
46 * The number of checkpoints to be inserted in insert tests
47 */
48 protected static final int CHECKPOINTS_INSERT_NUM = 50000;
49 /**
50 * The collection being tested
51 */
52 protected ICheckpointCollection fCheckpointCollection = null;
53
54 private TmfTraceStub fTrace;
55 private File fFile = new File(INDEX_FILE_NAME);
56
57 /**
58 * Setup the test. Make sure the index is deleted.
59 */
60 @Before
61 public void setUp() {
62 fTrace = new TmfTraceStub();
63 if (fFile.exists()) {
64 fFile.delete();
65 }
66 fCheckpointCollection = createCollection();
67 }
68
69 /**
70 * Tear down the test. Make sure the index is deleted.
71 */
72 @After
73 public void tearDown() {
74 fTrace.dispose();
75 fTrace = null;
76 if (fCheckpointCollection != null) {
77 fCheckpointCollection.dispose();
78 }
79 if (fFile.exists()) {
80 fFile.delete();
81 }
82 }
83
84 /**
85 * Get the trace being tested.
86 *
87 * @return the trace being tested.
88 */
89 public ITmfTrace getTrace() {
90 return fTrace;
91 }
92
93 /**
94 * Returns whether or not the collection is persisted to disk
95 *
96 * @return true if the collection is persisted to disk, false otherwise
97 */
98 public boolean isPersistableCollection() {
99 return false;
100 }
101
102 /**
103 * Get the file used for the index being tested.
104 *
105 * @return the file used for the index being tested.
106 */
107 public File getFile() {
108 return fFile;
109 }
110
111 /**
112 * Test constructing a new checkpoint collection
113 */
114 @Test
115 public void testConstructor() {
116 if (isPersistableCollection()) {
117 assertTrue(fFile.exists());
118 }
119 assertTrue(fCheckpointCollection.isCreatedFromScratch());
120 }
121
122 /**
123 * Test constructing a new checkpoint collection, existing file
124 */
125 @Test
126 public void testConstructorExistingFile() {
127 if (isPersistableCollection()) {
128 assertTrue(fFile.exists());
129 fCheckpointCollection.setIndexComplete();
130 fCheckpointCollection.dispose();
131
132 fCheckpointCollection = createCollection();
133 assertFalse(fCheckpointCollection.isCreatedFromScratch());
134 }
135 }
136
137 /**
138 * Test that a new checkpoint collection is considered created from scratch
139 * and vice versa
140 */
141 @Test
142 public void testIsCreatedFromScratch() {
143 assertTrue(fCheckpointCollection.isCreatedFromScratch());
144 fCheckpointCollection.setIndexComplete();
145
146 if (isPersistableCollection()) {
147 fCheckpointCollection.dispose();
148 fCheckpointCollection = createCollection();
149 assertFalse(fCheckpointCollection.isCreatedFromScratch());
150 }
151 }
152
153 /**
154 * Test setTimeRange, getTimeRange
155 */
156 @Test
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());
162 }
163 }
164
165 /**
166 * Create a collection for the test
167 *
168 * @return the collection
169 */
170 abstract protected ICheckpointCollection createCollection();
171
172 /**
173 * Test setNbEvents, getNbEvents
174 */
175 @Test
176 public void testSetGetNbEvents() {
177 if (isPersistableCollection()) {
178 int expected = 12345;
179 fCheckpointCollection.setNbEvents(expected);
180 assertEquals(expected, fCheckpointCollection.getNbEvents());
181 }
182 }
183
184 /**
185 * Test setSize, size
186 */
187 @Test
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));
193 }
194 assertEquals(expected, fCheckpointCollection.size());
195 }
196
197 /**
198 * Test delete
199 */
200 @Test
201 public void testDelete() {
202 if (isPersistableCollection()) {
203 assertTrue(fFile.exists());
204 fCheckpointCollection.delete();
205 assertFalse(fFile.exists());
206 }
207 }
208
209 /**
210 * Test version change
211 *
212 * @throws IOException
213 * can throw this
214 */
215 @Test
216 public void testVersionChange() throws IOException {
217 fCheckpointCollection.setIndexComplete();
218 fCheckpointCollection.dispose();
219 try (RandomAccessFile f = new RandomAccessFile(fFile, "rw");) {
220 f.writeInt(-1);
221 }
222
223 fCheckpointCollection = createCollection();
224 assertTrue(fCheckpointCollection.isCreatedFromScratch());
225 }
226
227 /**
228 * Test a single insertion
229 */
230 @Test
231 public void testInsert() {
232 TmfCheckpoint checkpoint = new TmfCheckpoint(new TmfTimestamp(12345), new TmfLongLocation(123456L), 0);
233 fCheckpointCollection.insert(checkpoint);
234
235 long found = fCheckpointCollection.binarySearch(checkpoint);
236 assertEquals(0, found);
237 }
238
239 /**
240 * Generate many checkpoints and insert them in the collection
241 *
242 * @return the list of generated checkpoints
243 */
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);
248 }
249
250 fCheckpointCollection.setIndexComplete();
251 if (isPersistableCollection()) {
252 fCheckpointCollection.dispose();
253 }
254
255 boolean random = true;
256 ArrayList<Integer> list = new ArrayList<>();
257 for (int i = 0; i < CHECKPOINTS_INSERT_NUM; i++) {
258 if (random) {
259 Random rand = new Random();
260 list.add(rand.nextInt(CHECKPOINTS_INSERT_NUM));
261 } else {
262 list.add(i);
263 }
264 }
265 return list;
266 }
267
268 /**
269 * Test many checkpoint insertions. Make sure they can be found after
270 * re-opening the file
271 */
272 @Test
273 public void testInsertAlot() {
274 ArrayList<Integer> list = insertAlot();
275
276 if (isPersistableCollection()) {
277 fCheckpointCollection = createCollection();
278 }
279
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);
285 }
286 }
287
288 /**
289 * Test many checkpoint insertions using the same timestamp. Make sure they
290 * can be found after re-opening the file
291 */
292 @Test
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);
297 }
298
299 fCheckpointCollection.setIndexComplete();
300 if (isPersistableCollection()) {
301 fCheckpointCollection.dispose();
302 }
303
304 boolean random = true;
305 ArrayList<Integer> list = new ArrayList<>();
306 for (int i = 0; i < CHECKPOINTS_INSERT_NUM; i++) {
307 if (random) {
308 Random rand = new Random();
309 list.add(rand.nextInt(CHECKPOINTS_INSERT_NUM));
310 } else {
311 list.add(i);
312 }
313 }
314
315 if (isPersistableCollection()) {
316 fCheckpointCollection = createCollection();
317 }
318
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);
324 }
325 }
326
327 /**
328 * Tests that binarySearch find the correct checkpoint when the time stamp
329 * is between checkpoints
330 */
331 @Test
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);
336 }
337
338 TmfCheckpoint searchedCheckpoint = new TmfCheckpoint(new TmfTimestamp(123), new TmfLongLocation(123L), 123);
339 int expectedInsertionPoint = 61;
340 int expectedRank = -(expectedInsertionPoint + 2);
341
342 long rank = fCheckpointCollection.binarySearch(searchedCheckpoint);
343 assertEquals(expectedRank, rank);
344 }
345
346
347 /**
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.
351 */
352 @Test
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);
358 }
359
360 for (; checkpointNum < 200; checkpointNum++) {
361 TmfCheckpoint checkpoint = new TmfCheckpoint(new TmfTimestamp(1), new TmfLongLocation(checkpointNum), checkpointNum);
362 fCheckpointCollection.insert(checkpoint);
363 }
364
365 final TmfCheckpoint searchedCheckpoint = new TmfCheckpoint(new TmfTimestamp(1), null, 0);
366
367 long found = fCheckpointCollection.binarySearch(searchedCheckpoint);
368
369 int expectedInsertionPoint = 99;
370 int expectedRank = -(expectedInsertionPoint + 2);
371
372 assertEquals(expectedRank, found);
373 }
374 }
This page took 0.040501 seconds and 5 git commands to generate.