Commit | Line | Data |
---|---|---|
032ecd45 | 1 | /******************************************************************************* |
60ae41e1 | 2 | * Copyright (c) 2013, 2014 Ericsson |
032ecd45 MAL |
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 | ||
2bdf0193 | 13 | package org.eclipse.tracecompass.tmf.core.tests.trace.indexer; |
032ecd45 MAL |
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 | ||
2bdf0193 AM |
25 | import org.eclipse.tracecompass.internal.tmf.core.trace.indexer.ICheckpointCollection; |
26 | import org.eclipse.tracecompass.tmf.core.timestamp.TmfTimeRange; | |
27 | import org.eclipse.tracecompass.tmf.core.timestamp.TmfTimestamp; | |
28 | import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace; | |
29 | import org.eclipse.tracecompass.tmf.core.trace.indexer.checkpoint.TmfCheckpoint; | |
30 | import org.eclipse.tracecompass.tmf.core.trace.location.TmfLongLocation; | |
31 | import org.eclipse.tracecompass.tmf.tests.stubs.trace.TmfTraceStub; | |
032ecd45 MAL |
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()); | |
032ecd45 MAL |
129 | fCheckpointCollection.dispose(); |
130 | ||
131 | fCheckpointCollection = createCollection(); | |
132 | assertFalse(fCheckpointCollection.isCreatedFromScratch()); | |
133 | } | |
134 | } | |
135 | ||
136 | /** | |
137 | * Test that a new checkpoint collection is considered created from scratch | |
138 | * and vice versa | |
139 | */ | |
140 | @Test | |
141 | public void testIsCreatedFromScratch() { | |
142 | assertTrue(fCheckpointCollection.isCreatedFromScratch()); | |
032ecd45 MAL |
143 | |
144 | if (isPersistableCollection()) { | |
145 | fCheckpointCollection.dispose(); | |
146 | fCheckpointCollection = createCollection(); | |
147 | assertFalse(fCheckpointCollection.isCreatedFromScratch()); | |
148 | } | |
149 | } | |
150 | ||
151 | /** | |
152 | * Test setTimeRange, getTimeRange | |
153 | */ | |
154 | @Test | |
155 | public void testSetGetTimeRange() { | |
156 | if (isPersistableCollection()) { | |
157 | TmfTimeRange timeRange = new TmfTimeRange(new TmfTimestamp(0), new TmfTimestamp(100)); | |
158 | fCheckpointCollection.setTimeRange(timeRange); | |
159 | assertEquals(timeRange, fCheckpointCollection.getTimeRange()); | |
160 | } | |
161 | } | |
162 | ||
163 | /** | |
164 | * Create a collection for the test | |
165 | * | |
166 | * @return the collection | |
167 | */ | |
168 | abstract protected ICheckpointCollection createCollection(); | |
169 | ||
170 | /** | |
171 | * Test setNbEvents, getNbEvents | |
172 | */ | |
173 | @Test | |
174 | public void testSetGetNbEvents() { | |
175 | if (isPersistableCollection()) { | |
176 | int expected = 12345; | |
177 | fCheckpointCollection.setNbEvents(expected); | |
178 | assertEquals(expected, fCheckpointCollection.getNbEvents()); | |
179 | } | |
180 | } | |
181 | ||
182 | /** | |
0861e8e8 | 183 | * Test get size |
032ecd45 MAL |
184 | */ |
185 | @Test | |
0861e8e8 | 186 | public void testGetSize() { |
032ecd45 MAL |
187 | assertEquals(0, fCheckpointCollection.size()); |
188 | int expected = CHECKPOINTS_INSERT_NUM; | |
189 | for (int i = 0; i < expected; ++i) { | |
0861e8e8 | 190 | fCheckpointCollection.insert(new TmfCheckpoint(new TmfTimestamp(i), new TmfLongLocation(i), i)); |
032ecd45 MAL |
191 | } |
192 | assertEquals(expected, fCheckpointCollection.size()); | |
193 | } | |
194 | ||
195 | /** | |
196 | * Test delete | |
197 | */ | |
198 | @Test | |
199 | public void testDelete() { | |
200 | if (isPersistableCollection()) { | |
201 | assertTrue(fFile.exists()); | |
202 | fCheckpointCollection.delete(); | |
203 | assertFalse(fFile.exists()); | |
204 | } | |
205 | } | |
206 | ||
207 | /** | |
208 | * Test version change | |
209 | * | |
210 | * @throws IOException | |
211 | * can throw this | |
212 | */ | |
213 | @Test | |
214 | public void testVersionChange() throws IOException { | |
032ecd45 | 215 | fCheckpointCollection.dispose(); |
ccf2bbb4 AM |
216 | try (RandomAccessFile f = new RandomAccessFile(fFile, "rw");) { |
217 | f.writeInt(-1); | |
218 | } | |
032ecd45 MAL |
219 | |
220 | fCheckpointCollection = createCollection(); | |
221 | assertTrue(fCheckpointCollection.isCreatedFromScratch()); | |
222 | } | |
223 | ||
224 | /** | |
225 | * Test a single insertion | |
226 | */ | |
227 | @Test | |
228 | public void testInsert() { | |
229 | TmfCheckpoint checkpoint = new TmfCheckpoint(new TmfTimestamp(12345), new TmfLongLocation(123456L), 0); | |
230 | fCheckpointCollection.insert(checkpoint); | |
231 | ||
232 | long found = fCheckpointCollection.binarySearch(checkpoint); | |
233 | assertEquals(0, found); | |
0861e8e8 | 234 | assertEquals(1, fCheckpointCollection.size()); |
032ecd45 MAL |
235 | } |
236 | ||
237 | /** | |
238 | * Generate many checkpoints and insert them in the collection | |
239 | * | |
240 | * @return the list of generated checkpoints | |
241 | */ | |
242 | protected ArrayList<Integer> insertAlot() { | |
243 | for (int i = 0; i < CHECKPOINTS_INSERT_NUM; i++) { | |
244 | TmfCheckpoint checkpoint = new TmfCheckpoint(new TmfTimestamp(12345 + i), new TmfLongLocation(123456L + i), i); | |
245 | fCheckpointCollection.insert(checkpoint); | |
246 | } | |
247 | ||
0861e8e8 | 248 | assertEquals(CHECKPOINTS_INSERT_NUM, fCheckpointCollection.size()); |
032ecd45 MAL |
249 | if (isPersistableCollection()) { |
250 | fCheckpointCollection.dispose(); | |
251 | } | |
252 | ||
253 | boolean random = true; | |
ccf2bbb4 | 254 | ArrayList<Integer> list = new ArrayList<>(); |
032ecd45 MAL |
255 | for (int i = 0; i < CHECKPOINTS_INSERT_NUM; i++) { |
256 | if (random) { | |
257 | Random rand = new Random(); | |
258 | list.add(rand.nextInt(CHECKPOINTS_INSERT_NUM)); | |
259 | } else { | |
260 | list.add(i); | |
261 | } | |
262 | } | |
263 | return list; | |
264 | } | |
265 | ||
266 | /** | |
267 | * Test many checkpoint insertions. Make sure they can be found after | |
268 | * re-opening the file | |
269 | */ | |
270 | @Test | |
271 | public void testInsertAlot() { | |
272 | ArrayList<Integer> list = insertAlot(); | |
273 | ||
274 | if (isPersistableCollection()) { | |
275 | fCheckpointCollection = createCollection(); | |
276 | } | |
0861e8e8 | 277 | assertEquals(CHECKPOINTS_INSERT_NUM, fCheckpointCollection.size()); |
032ecd45 MAL |
278 | |
279 | for (int i = 0; i < CHECKPOINTS_INSERT_NUM; i++) { | |
280 | Integer randomCheckpoint = list.get(i); | |
281 | TmfCheckpoint checkpoint = new TmfCheckpoint(new TmfTimestamp(12345 + randomCheckpoint), new TmfLongLocation(123456L + randomCheckpoint), 0); | |
282 | long found = fCheckpointCollection.binarySearch(checkpoint); | |
283 | assertEquals(randomCheckpoint.intValue(), found); | |
284 | } | |
0861e8e8 MAL |
285 | |
286 | assertEquals(CHECKPOINTS_INSERT_NUM, fCheckpointCollection.size()); | |
032ecd45 MAL |
287 | } |
288 | ||
289 | /** | |
290 | * Test many checkpoint insertions using the same timestamp. Make sure they | |
291 | * can be found after re-opening the file | |
292 | */ | |
293 | @Test | |
294 | public void testInsertSameTimestamp() { | |
295 | for (int i = 0; i < CHECKPOINTS_INSERT_NUM; i++) { | |
296 | TmfCheckpoint checkpoint = new TmfCheckpoint(new TmfTimestamp(12345), new TmfLongLocation(123456L + i), i); | |
297 | fCheckpointCollection.insert(checkpoint); | |
298 | } | |
0861e8e8 | 299 | assertEquals(CHECKPOINTS_INSERT_NUM, fCheckpointCollection.size()); |
032ecd45 | 300 | |
032ecd45 MAL |
301 | if (isPersistableCollection()) { |
302 | fCheckpointCollection.dispose(); | |
303 | } | |
304 | ||
305 | boolean random = true; | |
ccf2bbb4 | 306 | ArrayList<Integer> list = new ArrayList<>(); |
032ecd45 MAL |
307 | for (int i = 0; i < CHECKPOINTS_INSERT_NUM; i++) { |
308 | if (random) { | |
309 | Random rand = new Random(); | |
310 | list.add(rand.nextInt(CHECKPOINTS_INSERT_NUM)); | |
311 | } else { | |
312 | list.add(i); | |
313 | } | |
314 | } | |
315 | ||
316 | if (isPersistableCollection()) { | |
317 | fCheckpointCollection = createCollection(); | |
318 | } | |
0861e8e8 | 319 | assertEquals(CHECKPOINTS_INSERT_NUM, fCheckpointCollection.size()); |
032ecd45 MAL |
320 | |
321 | for (int i = 0; i < CHECKPOINTS_INSERT_NUM; i++) { | |
322 | Integer randomCheckpoint = list.get(i); | |
323 | TmfCheckpoint checkpoint = new TmfCheckpoint(new TmfTimestamp(12345), new TmfLongLocation(123456L + randomCheckpoint), 0); | |
324 | long found = fCheckpointCollection.binarySearch(checkpoint); | |
325 | assertEquals(randomCheckpoint.intValue(), found); | |
326 | } | |
327 | } | |
328 | ||
329 | /** | |
330 | * Tests that binarySearch find the correct checkpoint when the time stamp | |
331 | * is between checkpoints | |
332 | */ | |
333 | @Test | |
334 | public void testBinarySearchFindInBetween() { | |
335 | for (long i = 0; i < CHECKPOINTS_INSERT_NUM; i++) { | |
336 | TmfCheckpoint checkpoint = new TmfCheckpoint(new TmfTimestamp(2 * i), new TmfLongLocation(2 * i), i); | |
337 | fCheckpointCollection.insert(checkpoint); | |
338 | } | |
0861e8e8 | 339 | assertEquals(CHECKPOINTS_INSERT_NUM, fCheckpointCollection.size()); |
032ecd45 MAL |
340 | |
341 | TmfCheckpoint searchedCheckpoint = new TmfCheckpoint(new TmfTimestamp(123), new TmfLongLocation(123L), 123); | |
342 | int expectedInsertionPoint = 61; | |
343 | int expectedRank = -(expectedInsertionPoint + 2); | |
344 | ||
345 | long rank = fCheckpointCollection.binarySearch(searchedCheckpoint); | |
346 | assertEquals(expectedRank, rank); | |
347 | } | |
348 | ||
349 | ||
350 | /** | |
351 | * Tests that binarySearch finds the correct checkpoint when searching for a | |
352 | * checkpoint with a null location. It should return the previous checkpoint | |
353 | * from the first checkpoint that matches the timestamp. | |
354 | */ | |
355 | @Test | |
356 | public void testBinarySearchInBetweenSameTimestamp() { | |
357 | int checkpointNum = 0; | |
0861e8e8 | 358 | for (; checkpointNum < CHECKPOINTS_INSERT_NUM / 2; checkpointNum++) { |
0126a8ca | 359 | TmfCheckpoint checkpoint = new TmfCheckpoint(new TmfTimestamp(0), new TmfLongLocation(checkpointNum), checkpointNum); |
032ecd45 MAL |
360 | fCheckpointCollection.insert(checkpoint); |
361 | } | |
362 | ||
0861e8e8 | 363 | for (; checkpointNum < CHECKPOINTS_INSERT_NUM; checkpointNum++) { |
0126a8ca | 364 | TmfCheckpoint checkpoint = new TmfCheckpoint(new TmfTimestamp(1), new TmfLongLocation(checkpointNum), checkpointNum); |
032ecd45 MAL |
365 | fCheckpointCollection.insert(checkpoint); |
366 | } | |
0861e8e8 | 367 | assertEquals(CHECKPOINTS_INSERT_NUM, fCheckpointCollection.size()); |
032ecd45 MAL |
368 | |
369 | final TmfCheckpoint searchedCheckpoint = new TmfCheckpoint(new TmfTimestamp(1), null, 0); | |
370 | ||
371 | long found = fCheckpointCollection.binarySearch(searchedCheckpoint); | |
372 | ||
0861e8e8 | 373 | int expectedInsertionPoint = CHECKPOINTS_INSERT_NUM / 2 - 1; |
032ecd45 MAL |
374 | int expectedRank = -(expectedInsertionPoint + 2); |
375 | ||
376 | assertEquals(expectedRank, found); | |
377 | } | |
0861e8e8 MAL |
378 | |
379 | /** | |
380 | * Test checkpoint insertions after reopening the file. | |
381 | */ | |
382 | @Test | |
383 | public void testInsertAfterReopen() { | |
384 | if (!isPersistableCollection()) { | |
385 | return; | |
386 | } | |
387 | ||
388 | fCheckpointCollection.insert(new TmfCheckpoint(new TmfTimestamp(12345), new TmfLongLocation(123456L), 0)); | |
389 | ||
390 | assertEquals(1, fCheckpointCollection.size()); | |
391 | ||
392 | fCheckpointCollection.dispose(); | |
393 | fCheckpointCollection = createCollection(); | |
394 | assertEquals(1, fCheckpointCollection.size()); | |
395 | ||
396 | TmfCheckpoint checkpoint = new TmfCheckpoint(new TmfTimestamp(12345), new TmfLongLocation(123456L), 0); | |
397 | long found = fCheckpointCollection.binarySearch(checkpoint); | |
398 | assertEquals(0, found); | |
399 | ||
400 | fCheckpointCollection.insert(new TmfCheckpoint(new TmfTimestamp(12345 + 1), new TmfLongLocation(123456L + 1), 1)); | |
401 | assertEquals(2, fCheckpointCollection.size()); | |
402 | ||
403 | fCheckpointCollection.dispose(); | |
404 | fCheckpointCollection = createCollection(); | |
405 | assertEquals(2, fCheckpointCollection.size()); | |
406 | } | |
032ecd45 | 407 | } |