|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace KaririCode\DataStructure\Tests; |
| 6 | + |
| 7 | +use KaririCode\Contract\DataStructure\Behavioral\Comparable; |
| 8 | +use KaririCode\DataStructure\Heap\BinaryHeap; |
| 9 | +use PHPUnit\Framework\TestCase; |
| 10 | + |
| 11 | +final class BinaryHeapTest extends TestCase |
| 12 | +{ |
| 13 | + public function testAdd(): void |
| 14 | + { |
| 15 | + $heap = new BinaryHeap(); |
| 16 | + $heap->add(3); |
| 17 | + $heap->add(1); |
| 18 | + $heap->add(2); |
| 19 | + |
| 20 | + $this->assertSame(1, $heap->peek()); |
| 21 | + } |
| 22 | + |
| 23 | + public function testPoll(): void |
| 24 | + { |
| 25 | + $heap = new BinaryHeap(); |
| 26 | + $heap->add(3); |
| 27 | + $heap->add(1); |
| 28 | + $heap->add(2); |
| 29 | + |
| 30 | + $this->assertSame(1, $heap->poll()); |
| 31 | + $this->assertSame(2, $heap->poll()); |
| 32 | + $this->assertSame(3, $heap->poll()); |
| 33 | + $this->assertNull($heap->poll()); |
| 34 | + } |
| 35 | + |
| 36 | + public function testHeapifyDown(): void |
| 37 | + { |
| 38 | + $heap = new BinaryHeap(); |
| 39 | + $heap->add(5); |
| 40 | + $heap->add(3); |
| 41 | + $heap->add(8); |
| 42 | + $heap->add(1); |
| 43 | + $heap->add(6); |
| 44 | + |
| 45 | + $this->assertSame(1, $heap->poll()); |
| 46 | + $this->assertSame(3, $heap->poll()); |
| 47 | + $this->assertSame(5, $heap->poll()); |
| 48 | + $this->assertSame(6, $heap->poll()); |
| 49 | + $this->assertSame(8, $heap->poll()); |
| 50 | + } |
| 51 | + |
| 52 | + public function testHeapifyDownComplex(): void |
| 53 | + { |
| 54 | + $heap = new BinaryHeap(); |
| 55 | + $heap->add(10); |
| 56 | + $heap->add(15); |
| 57 | + $heap->add(20); |
| 58 | + $heap->add(17); |
| 59 | + $heap->add(25); |
| 60 | + |
| 61 | + $this->assertSame(10, $heap->poll()); |
| 62 | + $this->assertSame(15, $heap->poll()); |
| 63 | + $this->assertSame(17, $heap->poll()); |
| 64 | + $this->assertSame(20, $heap->poll()); |
| 65 | + $this->assertSame(25, $heap->poll()); |
| 66 | + } |
| 67 | + |
| 68 | + public function testCompare(): void |
| 69 | + { |
| 70 | + $minHeap = new BinaryHeap('min'); |
| 71 | + $minHeap->add(10); |
| 72 | + $minHeap->add(5); |
| 73 | + $minHeap->add(20); |
| 74 | + $this->assertSame(5, $minHeap->poll()); |
| 75 | + $this->assertSame(10, $minHeap->poll()); |
| 76 | + $this->assertSame(20, $minHeap->poll()); |
| 77 | + |
| 78 | + $maxHeap = new BinaryHeap('max'); |
| 79 | + $maxHeap->add(10); |
| 80 | + $maxHeap->add(5); |
| 81 | + $maxHeap->add(20); |
| 82 | + $this->assertSame(20, $maxHeap->poll()); |
| 83 | + $this->assertSame(10, $maxHeap->poll()); |
| 84 | + $this->assertSame(5, $maxHeap->poll()); |
| 85 | + } |
| 86 | + |
| 87 | + public function testRemove(): void |
| 88 | + { |
| 89 | + $heap = new BinaryHeap(); |
| 90 | + $heap->add(3); |
| 91 | + $heap->add(1); |
| 92 | + $heap->add(2); |
| 93 | + |
| 94 | + $this->assertTrue($heap->remove(1)); |
| 95 | + $this->assertFalse($heap->remove(4)); |
| 96 | + $this->assertSame(2, $heap->peek()); |
| 97 | + } |
| 98 | + |
| 99 | + public function testPeek(): void |
| 100 | + { |
| 101 | + $heap = new BinaryHeap(); |
| 102 | + $this->assertNull($heap->peek()); |
| 103 | + |
| 104 | + $heap->add(3); |
| 105 | + $this->assertSame(3, $heap->peek()); |
| 106 | + |
| 107 | + $heap->add(1); |
| 108 | + $this->assertSame(1, $heap->peek()); |
| 109 | + } |
| 110 | + |
| 111 | + public function testSize(): void |
| 112 | + { |
| 113 | + $heap = new BinaryHeap(); |
| 114 | + $this->assertSame(0, $heap->size()); |
| 115 | + |
| 116 | + $heap->add(3); |
| 117 | + $this->assertSame(1, $heap->size()); |
| 118 | + |
| 119 | + $heap->add(1); |
| 120 | + $heap->add(2); |
| 121 | + $this->assertSame(3, $heap->size()); |
| 122 | + |
| 123 | + $heap->poll(); |
| 124 | + $this->assertSame(2, $heap->size()); |
| 125 | + } |
| 126 | + |
| 127 | + public function testIsEmpty(): void |
| 128 | + { |
| 129 | + $heap = new BinaryHeap(); |
| 130 | + $this->assertTrue($heap->isEmpty()); |
| 131 | + |
| 132 | + $heap->add(3); |
| 133 | + $this->assertFalse($heap->isEmpty()); |
| 134 | + |
| 135 | + $heap->poll(); |
| 136 | + $this->assertTrue($heap->isEmpty()); |
| 137 | + } |
| 138 | + |
| 139 | + public function testInsert(): void |
| 140 | + { |
| 141 | + $heap = new BinaryHeap(); |
| 142 | + $heap->add(3); |
| 143 | + $heap->add(1); |
| 144 | + $heap->add(2); |
| 145 | + |
| 146 | + $heap->insert(1, 0); |
| 147 | + $this->assertSame(0, $heap->peek()); |
| 148 | + |
| 149 | + $this->expectException(\OutOfRangeException::class); |
| 150 | + $heap->insert(-1, 4); |
| 151 | + } |
| 152 | + |
| 153 | + public function testComparableElements(): void |
| 154 | + { |
| 155 | + $element1 = $this->createMock(Comparable::class); |
| 156 | + $element2 = $this->createMock(Comparable::class); |
| 157 | + $element3 = $this->createMock(Comparable::class); |
| 158 | + |
| 159 | + $element1->method('compareTo')->willReturn(1); |
| 160 | + $element2->method('compareTo')->willReturn(0); |
| 161 | + $element3->method('compareTo')->willReturn(-1); |
| 162 | + |
| 163 | + $heap = new BinaryHeap('max'); |
| 164 | + $heap->add($element1); |
| 165 | + $heap->add($element2); |
| 166 | + $heap->add($element3); |
| 167 | + |
| 168 | + $this->assertSame($element1, $heap->peek()); |
| 169 | + } |
| 170 | + |
| 171 | + public function testHeapifyDownWithRightChildComparison(): void |
| 172 | + { |
| 173 | + $heap = new BinaryHeap(); |
| 174 | + $heap->add(10); |
| 175 | + $heap->add(15); |
| 176 | + $heap->add(20); |
| 177 | + $heap->add(17); |
| 178 | + $heap->add(8); |
| 179 | + $heap->add(25); |
| 180 | + |
| 181 | + $this->assertSame(8, $heap->poll()); |
| 182 | + $this->assertSame(10, $heap->poll()); |
| 183 | + $this->assertSame(15, $heap->poll()); |
| 184 | + $this->assertSame(17, $heap->poll()); |
| 185 | + $this->assertSame(20, $heap->poll()); |
| 186 | + $this->assertSame(25, $heap->poll()); |
| 187 | + } |
| 188 | + |
| 189 | + public function testCompareWithMinType(): void |
| 190 | + { |
| 191 | + $heap = new BinaryHeap('min'); |
| 192 | + |
| 193 | + $element1 = $this->createMock(Comparable::class); |
| 194 | + $element2 = $this->createMock(Comparable::class); |
| 195 | + |
| 196 | + $element1->method('compareTo')->willReturn(1); |
| 197 | + $element2->method('compareTo')->willReturn(-1); |
| 198 | + |
| 199 | + $reflection = new \ReflectionClass($heap); |
| 200 | + $method = $reflection->getMethod('compare'); |
| 201 | + $method->setAccessible(true); |
| 202 | + |
| 203 | + $result1 = $method->invokeArgs($heap, [$element1, $element2]); |
| 204 | + $result2 = $method->invokeArgs($heap, [$element2, $element1]); |
| 205 | + |
| 206 | + $this->assertFalse($result1); |
| 207 | + $this->assertTrue($result2); |
| 208 | + } |
| 209 | + |
| 210 | + public function testCompareWithMaxType(): void |
| 211 | + { |
| 212 | + $heap = new BinaryHeap('max'); |
| 213 | + |
| 214 | + $element1 = $this->createMock(Comparable::class); |
| 215 | + $element2 = $this->createMock(Comparable::class); |
| 216 | + |
| 217 | + $element1->method('compareTo')->willReturn(1); |
| 218 | + $element2->method('compareTo')->willReturn(-1); |
| 219 | + |
| 220 | + $reflection = new \ReflectionClass($heap); |
| 221 | + $method = $reflection->getMethod('compare'); |
| 222 | + $method->setAccessible(true); |
| 223 | + |
| 224 | + $result1 = $method->invokeArgs($heap, [$element1, $element2]); |
| 225 | + $result2 = $method->invokeArgs($heap, [$element2, $element1]); |
| 226 | + |
| 227 | + $this->assertTrue($result1); |
| 228 | + $this->assertFalse($result2); |
| 229 | + } |
| 230 | +} |
0 commit comments