| 1 | /******************************************************************************* |
| 2 | * Copyright (c) 2013 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 | |
| 17 | import java.util.ArrayList; |
| 18 | |
| 19 | import org.eclipse.linuxtools.internal.tmf.core.trace.indexer.BTree; |
| 20 | import org.eclipse.linuxtools.internal.tmf.core.trace.indexer.BTreeCheckpointVisitor; |
| 21 | import org.eclipse.linuxtools.internal.tmf.core.trace.indexer.IBTreeVisitor; |
| 22 | import org.eclipse.linuxtools.tmf.core.timestamp.TmfTimestamp; |
| 23 | import org.eclipse.linuxtools.tmf.core.trace.indexer.ITmfPersistentlyIndexable; |
| 24 | import org.eclipse.linuxtools.tmf.core.trace.indexer.checkpoint.ITmfCheckpoint; |
| 25 | import org.eclipse.linuxtools.tmf.core.trace.indexer.checkpoint.TmfCheckpoint; |
| 26 | import org.eclipse.linuxtools.tmf.core.trace.location.TmfLongLocation; |
| 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() { |
| 41 | fBTree = new BTree(DEGREE, getFile(), (ITmfPersistentlyIndexable) getTrace()); |
| 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++) { |
| 57 | TmfCheckpoint checkpoint = new TmfCheckpoint(new TmfTimestamp(i), new TmfLongLocation((long) i), 0); |
| 58 | fBTree.insert(checkpoint); |
| 59 | } |
| 60 | |
| 61 | final TmfCheckpoint checkpoint = new TmfCheckpoint(new TmfTimestamp(123), new TmfLongLocation(123L), 0); |
| 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); |
| 96 | TmfCheckpoint checkpoint = new TmfCheckpoint(new TmfTimestamp(12345 + checkpointIndex), new TmfLongLocation(123456L + checkpointIndex), 0); |
| 97 | BTreeCheckpointVisitor treeVisitor = new BTreeCheckpointVisitor(checkpoint); |
| 98 | fBTree.accept(treeVisitor); |
| 99 | assertEquals(checkpoint, treeVisitor.getCheckpoint()); |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Test setSize, size |
| 105 | */ |
| 106 | @Override |
| 107 | @Test |
| 108 | public void testSetGetSize() { |
| 109 | assertEquals(0, fBTree.size()); |
| 110 | int expected = CHECKPOINTS_INSERT_NUM; |
| 111 | for (int i = 0; i < expected; ++i) { |
| 112 | fBTree.insert(new TmfCheckpoint(new TmfTimestamp(0), new TmfLongLocation(0L), 0)); |
| 113 | fBTree.setSize(fBTree.size() + 1); |
| 114 | } |
| 115 | assertEquals(expected, fBTree.size()); |
| 116 | } |
| 117 | |
| 118 | } |