|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +use KaririCode\DataStructure\Tree\BPlusTree; |
| 6 | +use KaririCode\DataStructure\Tree\BPlusTreeNode\BPlusTreeLeafNode; |
| 7 | +use KaririCode\DataStructure\Tree\BPlusTreeSearcher; |
| 8 | +use PHPUnit\Framework\TestCase; |
| 9 | + |
| 10 | +final class BPlusTreeSearcherTest extends TestCase |
| 11 | +{ |
| 12 | + private BPlusTree $tree; |
| 13 | + private BPlusTreeSearcher $searcher; |
| 14 | + |
| 15 | + protected function setUp(): void |
| 16 | + { |
| 17 | + $this->tree = new BPlusTree(4); // Using order 4 for the tree |
| 18 | + $this->searcher = new BPlusTreeSearcher(); |
| 19 | + } |
| 20 | + |
| 21 | + public function testFindByKey(): void |
| 22 | + { |
| 23 | + // Insert elements into the tree |
| 24 | + $this->tree->insert(10, 'Value10'); |
| 25 | + $this->tree->insert(20, 'Value20'); |
| 26 | + $this->tree->insert(30, 'Value30'); |
| 27 | + $this->tree->insert(40, 'Value40'); |
| 28 | + |
| 29 | + // Test finding existing keys |
| 30 | + $result = $this->searcher->find($this->tree, 20); |
| 31 | + $this->assertEquals('Value20', $result); |
| 32 | + |
| 33 | + $result = $this->searcher->find($this->tree, 40); |
| 34 | + $this->assertEquals('Value40', $result); |
| 35 | + |
| 36 | + // Test finding a non-existing key |
| 37 | + $result = $this->searcher->find($this->tree, 50); |
| 38 | + $this->assertNull($result); |
| 39 | + } |
| 40 | + |
| 41 | + public function testFindByValue(): void |
| 42 | + { |
| 43 | + // Insert elements into the tree |
| 44 | + $this->tree->insert(5, 'Value5'); |
| 45 | + $this->tree->insert(15, 'Value15'); |
| 46 | + $this->tree->insert(25, 'Value25'); |
| 47 | + $this->tree->insert(35, 'Value35'); |
| 48 | + |
| 49 | + // Test finding existing values |
| 50 | + $result = $this->searcher->find($this->tree, 'Value15'); |
| 51 | + $this->assertEquals(15, $result); |
| 52 | + |
| 53 | + $result = $this->searcher->find($this->tree, 'Value35'); |
| 54 | + $this->assertEquals(35, $result); |
| 55 | + |
| 56 | + // Test finding a non-existing value |
| 57 | + $result = $this->searcher->find($this->tree, 'Value50'); |
| 58 | + $this->assertNull($result); |
| 59 | + } |
| 60 | + |
| 61 | + public function testRangeSearch(): void |
| 62 | + { |
| 63 | + // Insert elements into the tree |
| 64 | + for ($i = 1; $i <= 20; ++$i) { |
| 65 | + $this->tree->insert($i * 5, 'Value' . ($i * 5)); |
| 66 | + } |
| 67 | + |
| 68 | + // Perform a range search |
| 69 | + $result = $this->searcher->rangeSearch($this->tree, 30, 70); |
| 70 | + |
| 71 | + // Expected values are from 30 to 70 (inclusive) |
| 72 | + $expected = ['Value30', 'Value35', 'Value40', 'Value45', 'Value50', 'Value55', 'Value60', 'Value65', 'Value70']; |
| 73 | + |
| 74 | + $this->assertEquals($expected, $result); |
| 75 | + |
| 76 | + // Test an empty range |
| 77 | + $result = $this->searcher->rangeSearch($this->tree, 105, 110); |
| 78 | + $this->assertEmpty($result); |
| 79 | + } |
| 80 | + |
| 81 | + public function testSearchInLeafNode(): void |
| 82 | + { |
| 83 | + // Directly test the private method searchInLeafNode using reflection |
| 84 | + $leafNode = new BPlusTreeLeafNode(4); |
| 85 | + $leafNode->keys = [10, 20, 30]; |
| 86 | + $leafNode->values = ['Value10', 'Value20', 'Value30']; |
| 87 | + |
| 88 | + // Use reflection to access the private method |
| 89 | + $reflection = new ReflectionClass(BPlusTreeSearcher::class); |
| 90 | + $method = $reflection->getMethod('searchInLeafNode'); |
| 91 | + $method->setAccessible(true); |
| 92 | + |
| 93 | + // Test finding an existing value |
| 94 | + $index = $method->invokeArgs($this->searcher, [$leafNode, 'Value20']); |
| 95 | + $this->assertEquals(20, $index); |
| 96 | + |
| 97 | + // Test finding a non-existing value |
| 98 | + $index = $method->invokeArgs($this->searcher, [$leafNode, 'Value50']); |
| 99 | + $this->assertNull($index); |
| 100 | + } |
| 101 | + |
| 102 | + public function testCompareValues(): void |
| 103 | + { |
| 104 | + // Directly test the private method compareValues using reflection |
| 105 | + $reflection = new ReflectionClass(BPlusTreeSearcher::class); |
| 106 | + $method = $reflection->getMethod('compareValues'); |
| 107 | + $method->setAccessible(true); |
| 108 | + |
| 109 | + // Test comparison of scalars |
| 110 | + $result = $method->invokeArgs($this->searcher, [10, 10]); |
| 111 | + $this->assertTrue($result); |
| 112 | + |
| 113 | + $result = $method->invokeArgs($this->searcher, [10, 20]); |
| 114 | + $this->assertFalse($result); |
| 115 | + |
| 116 | + // Test comparison of objects |
| 117 | + $obj1 = (object) ['a' => 1]; |
| 118 | + $obj2 = (object) ['a' => 1]; |
| 119 | + $obj3 = (object) ['a' => 2]; |
| 120 | + |
| 121 | + $result = $method->invokeArgs($this->searcher, [$obj1, $obj2]); |
| 122 | + $this->assertTrue($result); |
| 123 | + |
| 124 | + $result = $method->invokeArgs($this->searcher, [$obj1, $obj3]); |
| 125 | + $this->assertFalse($result); |
| 126 | + } |
| 127 | + |
| 128 | + public function testFindFirstLeafNode(): void |
| 129 | + { |
| 130 | + // Create a tree with multiple levels |
| 131 | + for ($i = 1; $i <= 50; ++$i) { |
| 132 | + $this->tree->insert($i, "Value$i"); |
| 133 | + } |
| 134 | + |
| 135 | + // Use reflection to access the private method |
| 136 | + $reflection = new ReflectionClass(BPlusTreeSearcher::class); |
| 137 | + $method = $reflection->getMethod('findFirstLeafNode'); |
| 138 | + $method->setAccessible(true); |
| 139 | + |
| 140 | + $firstLeaf = $method->invokeArgs($this->searcher, [$this->tree->getRoot()]); |
| 141 | + $this->assertInstanceOf(BPlusTreeLeafNode::class, $firstLeaf); |
| 142 | + |
| 143 | + // The first leaf node should contain the smallest keys |
| 144 | + $this->assertContains(1, $firstLeaf->keys); |
| 145 | + } |
| 146 | + |
| 147 | + public function testSearchByValueWithNonExistingValue(): void |
| 148 | + { |
| 149 | + // Insert elements into the tree |
| 150 | + $this->tree->insert(100, 'Value100'); |
| 151 | + $this->tree->insert(200, 'Value200'); |
| 152 | + |
| 153 | + // Test searching for a non-existing value |
| 154 | + $result = $this->searcher->find($this->tree, 'NonExistingValue'); |
| 155 | + $this->assertNull($result); |
| 156 | + } |
| 157 | + |
| 158 | + public function testSearchWithEmptyTree(): void |
| 159 | + { |
| 160 | + // Create an empty tree |
| 161 | + $emptyTree = new BPlusTree(4); |
| 162 | + |
| 163 | + // Test searching in an empty tree |
| 164 | + $result = $this->searcher->find($emptyTree, 10); |
| 165 | + $this->assertNull($result); |
| 166 | + |
| 167 | + $result = $this->searcher->rangeSearch($emptyTree, 10, 20); |
| 168 | + $this->assertEmpty($result); |
| 169 | + } |
| 170 | + |
| 171 | + public function testFindWithNullValue(): void |
| 172 | + { |
| 173 | + // Insert elements with null values |
| 174 | + $this->tree->insert(1, null); |
| 175 | + $this->tree->insert(2, 'Value2'); |
| 176 | + |
| 177 | + // Test finding a key with a null value |
| 178 | + $result = $this->searcher->find($this->tree, null); |
| 179 | + $this->assertEquals(1, $result); |
| 180 | + } |
| 181 | + |
| 182 | + public function testSearchWithDuplicateValues(): void |
| 183 | + { |
| 184 | + // Insert elements with duplicate values |
| 185 | + $this->tree->insert(1, 'DuplicateValue'); |
| 186 | + $this->tree->insert(2, 'DuplicateValue'); |
| 187 | + $this->tree->insert(3, 'UniqueValue'); |
| 188 | + |
| 189 | + // Test finding by value (should return the first key that matches) |
| 190 | + $result = $this->searcher->find($this->tree, 'DuplicateValue'); |
| 191 | + $this->assertEquals(1, $result); |
| 192 | + |
| 193 | + // Test that the search continues correctly after duplicates |
| 194 | + $result = $this->searcher->find($this->tree, 'UniqueValue'); |
| 195 | + $this->assertEquals(3, $result); |
| 196 | + } |
| 197 | + |
| 198 | + public function testRangeSearchWithSingleElementRange(): void |
| 199 | + { |
| 200 | + // Insert elements into the tree |
| 201 | + $this->tree->insert(10, 'Value10'); |
| 202 | + $this->tree->insert(20, 'Value20'); |
| 203 | + |
| 204 | + // Perform a range search where start and end are the same |
| 205 | + $result = $this->searcher->rangeSearch($this->tree, 20, 20); |
| 206 | + $this->assertEquals(['Value20'], $result); |
| 207 | + } |
| 208 | + |
| 209 | + public function testFindWithEmptyTree(): void |
| 210 | + { |
| 211 | + // Create an empty tree |
| 212 | + $emptyTree = new BPlusTree(4); |
| 213 | + |
| 214 | + // Ensure the root is null |
| 215 | + $this->assertNull($emptyTree->getRoot()); |
| 216 | + |
| 217 | + // Test finding an element in an empty tree |
| 218 | + $result = $this->searcher->find($emptyTree, 10); |
| 219 | + $this->assertNull($result); |
| 220 | + } |
| 221 | +} |
0 commit comments