001: /*
002: * Copyright Aduna (http://www.aduna-software.com/) (c) 2007.
003: *
004: * Licensed under the Aduna BSD-style license.
005: */
006: package org.openrdf.sail.nativerdf.btree;
007:
008: import java.io.File;
009: import java.util.ArrayList;
010: import java.util.Collections;
011: import java.util.List;
012:
013: import junit.framework.TestCase;
014:
015: /**
016: * @author Arjohn Kampman
017: */
018: public class BTreeTest extends TestCase {
019:
020: /*-----------*
021: * Constants *
022: *-----------*/
023:
024: private static List<byte[]> TEST_VALUES = new ArrayList<byte[]>(256);
025:
026: private static List<byte[]> RANDOMIZED_TEST_VALUES = new ArrayList<byte[]>(
027: 256);
028:
029: static {
030: for (int i = 0; i < 256; i++) {
031: byte[] value = new byte[1];
032: value[0] = (byte) i;
033: TEST_VALUES.add(value);
034: }
035:
036: RANDOMIZED_TEST_VALUES.addAll(TEST_VALUES);
037: Collections.shuffle(RANDOMIZED_TEST_VALUES);
038: }
039:
040: /*-----------*
041: * Variables *
042: *-----------*/
043:
044: private File dataFile;
045:
046: private BTree btree;
047:
048: /*---------*
049: * Methods *
050: *---------*/
051:
052: @Override
053: protected void setUp() throws Exception {
054: super .setUp();
055: dataFile = File.createTempFile("btree", null);
056: btree = new BTree(dataFile, 85, 1);
057: }
058:
059: @Override
060: protected void tearDown() throws Exception {
061: try {
062: btree.close();
063: } finally {
064: try {
065: dataFile.delete();
066: } finally {
067: super .tearDown();
068: }
069: }
070: }
071:
072: public void testAddAscending() throws Exception {
073: for (byte[] value : TEST_VALUES) {
074: btree.insert(value);
075: }
076: }
077:
078: public void testAddDescending() throws Exception {
079: for (int i = TEST_VALUES.size() - 1; i >= 0; i--) {
080: btree.insert(TEST_VALUES.get(i));
081: }
082: }
083:
084: public void testAddRandom() throws Exception {
085: for (byte[] value : RANDOMIZED_TEST_VALUES) {
086: btree.insert(value);
087: }
088: }
089:
090: public void testRemoveAscending() throws Exception {
091: testAddRandom();
092:
093: for (byte[] value : TEST_VALUES) {
094: btree.remove(value);
095: }
096: }
097:
098: public void testRemoveDescending() throws Exception {
099: testAddRandom();
100:
101: for (int i = TEST_VALUES.size() - 1; i >= 0; i--) {
102: btree.remove(TEST_VALUES.get(i));
103: }
104: }
105:
106: public void testRemoveRandom() throws Exception {
107: testAddAscending();
108:
109: for (byte[] value : RANDOMIZED_TEST_VALUES) {
110: btree.remove(value);
111: }
112: }
113:
114: public void testConcurrentAccess() throws Exception {
115: int meanIdx = TEST_VALUES.size() / 2;
116: btree.insert(TEST_VALUES.get(meanIdx - 1));
117: btree.insert(TEST_VALUES.get(meanIdx));
118: btree.insert(TEST_VALUES.get(meanIdx + 1));
119:
120: RecordIterator iter1 = btree.iterateAll();
121: iter1.next();
122:
123: RecordIterator iter2 = btree.iterateAll();
124: iter2.next();
125: iter2.next();
126: iter2.next();
127:
128: for (byte[] value : TEST_VALUES) {
129: btree.insert(value);
130: }
131:
132: iter2.close();
133: iter1.close();
134: }
135:
136: /* Test for SES-527
137: public void testRootNodeSplit()
138: throws Exception
139: {
140: // Fill the root node
141: for (int i = 0; i < 15; i++) {
142: btree.insert(TEST_VALUES.get(i));
143: }
144:
145: // Fire up an iterator
146: RecordIterator iter = btree.iterateAll();
147: iter.next();
148:
149: // Force the root node to split
150: btree.insert(TEST_VALUES.get(15));
151:
152: // Verify that the iterator returns all 15 elements
153: int count = 0;
154: while (iter.next() != null) {
155: count++;
156: }
157:
158: iter.close();
159:
160: assertEquals(15, count);
161: }
162: */
163: }
|