1 21 package com.db4o.db4ounit.common.btree; 22 23 import com.db4o.inside.btree.*; 24 25 import db4ounit.Assert; 26 27 public class BTreeRangeTestCase extends BTreeTestCaseBase { 28 29 public static void main(String [] args) { 30 new BTreeRangeTestCase().runSolo(); 31 } 32 33 protected void db4oSetupAfterStore() throws Exception { 34 super.db4oSetupAfterStore(); 35 add(new int[] { 3, 7, 4, 9 }); 36 } 37 38 public void testLastPointer(){ 39 assertLastPointer(8, 7); 40 assertLastPointer(11, 9); 41 assertLastPointer(4, 3); 42 } 43 44 private void assertLastPointer(final int searchValue, final int expectedValue) { 45 BTreeRange single = search(searchValue); 46 BTreeRange smallerRange = single.smaller(); 47 BTreePointer lastPointer = smallerRange.lastPointer(); 48 Assert.areEqual(new Integer (expectedValue), lastPointer.key()); 49 } 50 51 public void testSize(){ 52 assertSize(4, range(3,9)); 53 assertSize(3, range(4,9)); 54 assertSize(3, range(3,7)); 55 assertSize(4, range(2,9)); 56 assertSize(4, range(3,10)); 57 58 59 add(new int[]{5, 6, 8, 10, 2, 1}); 60 assertSize(10, range(1,10)); 61 assertSize(9, range(1,9)); 62 assertSize(9, range(2,10)); 63 assertSize(9, range(2,11)); 64 assertSize(10, range(0,10)); 65 } 66 67 private void assertSize(int size, BTreeRange range) { 68 Assert.areEqual(size, range.size()); 69 } 70 71 public void testIntersectSingleSingle() { 72 assertIntersection(new int[] { 4, 7 }, range(3, 7), range(4, 9)); 73 assertIntersection(new int[] {}, range(3, 4), range(7, 9)); 74 assertIntersection(new int[] { 3, 4, 7, 9 }, range(3, 9), range(3, 9)); 75 assertIntersection(new int[] { 3, 4, 7, 9 }, range(3, 10), range(3, 9)); 76 assertIntersection(new int[] {}, range(1, 2), range(3, 9)); 77 } 78 79 public void testIntersectSingleUnion() { 80 BTreeRange union = range(3, 3).union(range(7, 9)); 81 BTreeRange single = range(4, 7); 82 assertIntersection(new int[] { 7 }, union, single); 83 assertIntersection(new int[] { 3, 7 }, union, range(3, 7)); 84 } 85 86 public void testIntersectUnionUnion() { 87 final BTreeRange union1 = range(3, 3).union(range(7, 9)); 88 final BTreeRange union2 = range(3, 3).union(range(9, 9)); 89 assertIntersection(new int[] { 3, 9 }, union1, union2); 90 } 91 92 public void testUnion() { 93 assertUnion(new int[] { 3, 4, 7, 9 }, range(3, 4), range(7, 9)); 94 assertUnion(new int[] { 3, 4, 7, 9 }, range(3, 7), range(4, 9)); 95 assertUnion(new int[] { 3, 7, 9 }, range(3, 3), range(7, 9)); 96 assertUnion(new int[] { 3, 9 }, range(3, 3), range(9, 9)); 97 } 98 99 public void testIsEmpty() { 100 Assert.isTrue(range(0, 0).isEmpty()); 101 Assert.isFalse(range(3, 3).isEmpty()); 102 Assert.isFalse(range(9, 9).isEmpty()); 103 Assert.isTrue(range(10, 10).isEmpty()); 104 } 105 106 public void testUnionWithEmptyDoesNotCreateNewRange() { 107 final BTreeRange range = range(3, 4); 108 final BTreeRange empty = range(0, 0); 109 Assert.areSame(range, range.union(empty)); 110 Assert.areSame(range, empty.union(range)); 111 112 final BTreeRange union = range.union(range(8, 9)); 113 Assert.areSame(union, union.union(empty)); 114 Assert.areSame(union, empty.union(union)); 115 } 116 117 public void testUnionsMerge() { 118 final BTreeRange range = range(3, 3).union(range(7, 7)).union(range(4, 4)); 119 assertIsRangeSingle(range); 120 BTreeAssert.assertRange(new int[] { 3, 4, 7 }, range); 121 } 122 123 private void assertIsRangeSingle(final BTreeRange range) { 124 Assert.isInstanceOf(BTreeRangeSingle.class, range); 125 } 126 127 public void testUnionsOfUnions() { 128 final BTreeRange union1 = range(3, 4).union(range(8, 9)); 129 130 BTreeAssert.assertRange( 131 new int[] { 3, 4, 9 }, 132 union1); 133 BTreeAssert.assertRange( 134 new int[] { 3, 4, 7, 9 }, 135 union1.union(range(7, 7))); 136 137 final BTreeRange union2 = range(3, 3).union(range(7, 7)); 138 assertUnion(new int[] { 3, 4, 7, 9 }, union1, union2); 139 140 assertIsRangeSingle(union1.union(union2)); 141 assertIsRangeSingle(union2.union(union1)); 142 143 final BTreeRange union3 = range(3, 3).union(range(9, 9)); 144 assertUnion(new int[] { 3, 7, 9 }, union2, union3); 145 } 146 147 public void testExtendToLastOf() { 148 BTreeAssert.assertRange(new int[] { 3, 4, 7 }, range(3, 7)); 149 BTreeAssert.assertRange(new int[] { 4, 7, 9 }, range(4, 9)); 150 } 151 152 public void testUnionOfOverlappingSingleRangesYieldSingleRange() { 153 Assert.isInstanceOf(BTreeRangeSingle.class, range(3, 4).union(range(4, 9))); 154 } 155 156 private void assertUnion(int[] expectedKeys, BTreeRange range1, BTreeRange range2) { 157 BTreeAssert.assertRange(expectedKeys, range1.union(range2)); 158 BTreeAssert.assertRange(expectedKeys, range2.union(range1)); 159 } 160 161 private void assertIntersection(int[] expectedKeys, BTreeRange range1, BTreeRange range2) { 162 BTreeAssert.assertRange(expectedKeys, range1.intersect(range2)); 163 BTreeAssert.assertRange(expectedKeys, range2.intersect(range1)); 164 } 165 } 166 | Popular Tags |