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 | ||
17 | import java.util.ArrayList; | |
18 | ||
2bdf0193 AM |
19 | import org.eclipse.tracecompass.internal.tmf.core.trace.indexer.BTree; |
20 | import org.eclipse.tracecompass.internal.tmf.core.trace.indexer.BTreeCheckpointVisitor; | |
21 | import org.eclipse.tracecompass.internal.tmf.core.trace.indexer.IBTreeVisitor; | |
22 | import org.eclipse.tracecompass.tmf.core.timestamp.TmfTimestamp; | |
23 | import org.eclipse.tracecompass.tmf.core.trace.indexer.ITmfPersistentlyIndexable; | |
24 | import org.eclipse.tracecompass.tmf.core.trace.indexer.checkpoint.ITmfCheckpoint; | |
25 | import org.eclipse.tracecompass.tmf.core.trace.indexer.checkpoint.TmfCheckpoint; | |
26 | import org.eclipse.tracecompass.tmf.core.trace.location.TmfLongLocation; | |
032ecd45 MAL |
27 | import org.junit.Test; |
28 | ||
29 | /** | |
30 | * Tests for the BTree class | |
31 | * | |
32 | * @author Marc-Andre Laperle | |
33 | */ | |
34 | public class BTreeTest extends AbstractCheckpointCollectionTest { | |
35 | ||
36 | private final int DEGREE = 15; | |
37 | private BTree fBTree; | |
38 | ||
39 | @Override | |
40 | protected BTree createCollection() { | |
334305a9 | 41 | fCheckpointCollection = fBTree = new BTree(DEGREE, getFile(), (ITmfPersistentlyIndexable) getTrace()); |
032ecd45 MAL |
42 | return fBTree; |
43 | } | |
44 | ||
45 | @Override | |
46 | public boolean isPersistableCollection() { | |
47 | return true; | |
48 | } | |
49 | ||
50 | /** | |
51 | * Tests that accepts find the correct checkpoint and ends with a perfect | |
52 | * match | |
53 | */ | |
54 | @Test | |
55 | public void testAccept() { | |
56 | for (int i = 0; i < CHECKPOINTS_INSERT_NUM; i++) { | |
b2c971ec | 57 | TmfCheckpoint checkpoint = new TmfCheckpoint(TmfTimestamp.fromSeconds(i), new TmfLongLocation(i), 0); |
032ecd45 MAL |
58 | fBTree.insert(checkpoint); |
59 | } | |
60 | ||
b2c971ec | 61 | final TmfCheckpoint checkpoint = new TmfCheckpoint(TmfTimestamp.fromSeconds(123), new TmfLongLocation(123L), 0); |
032ecd45 MAL |
62 | |
63 | class TestVisitor implements IBTreeVisitor { | |
64 | public int fLastCompare = 0; | |
65 | ITmfCheckpoint fFoundCheckpoint; | |
66 | ||
67 | @Override | |
68 | public int compare(ITmfCheckpoint checkRec) { | |
69 | fLastCompare = checkRec.compareTo(checkpoint); | |
70 | if (fLastCompare == 0) { | |
71 | fFoundCheckpoint = checkRec; | |
72 | } | |
73 | return fLastCompare; | |
74 | } | |
75 | } | |
76 | final TestVisitor t = new TestVisitor(); | |
77 | ||
78 | fBTree.accept(t); | |
79 | ||
80 | assertEquals(checkpoint, t.fFoundCheckpoint); | |
81 | assertEquals(0, t.fLastCompare); | |
82 | } | |
83 | ||
84 | /** | |
85 | * Test many checkpoint insertions. Make sure they can be found after | |
86 | * re-opening the file | |
87 | */ | |
88 | @Test | |
89 | public void testInsertAlotCheckEquals() { | |
90 | ArrayList<Integer> list = insertAlot(); | |
91 | ||
92 | fBTree = createCollection(); | |
93 | ||
94 | for (int i = 0; i < CHECKPOINTS_INSERT_NUM; i++) { | |
95 | Integer checkpointIndex = list.get(i); | |
b2c971ec | 96 | TmfCheckpoint checkpoint = new TmfCheckpoint(TmfTimestamp.fromSeconds(12345 + checkpointIndex), new TmfLongLocation(123456L + checkpointIndex), 0); |
032ecd45 MAL |
97 | BTreeCheckpointVisitor treeVisitor = new BTreeCheckpointVisitor(checkpoint); |
98 | fBTree.accept(treeVisitor); | |
99 | assertEquals(checkpoint, treeVisitor.getCheckpoint()); | |
100 | } | |
101 | } | |
032ecd45 | 102 | } |