|
| 1 | +<?php |
| 2 | +declare(strict_types=1); |
| 3 | + |
| 4 | +namespace Blibio\Combinatorics\Test; |
| 5 | + |
| 6 | +use Blibio\Combinatorics\AbstractStrategy; |
| 7 | +use InvalidArgumentException; |
| 8 | +use Override; |
| 9 | +use PHPUnit\Framework\TestCase; |
| 10 | + |
| 11 | +/** |
| 12 | + * Tests for AbstractStrategy algorithmic and behavioral concerns |
| 13 | + * These tests apply to all concrete strategy implementations |
| 14 | + */ |
| 15 | +final class AbstractStrategyTest extends TestCase |
| 16 | +{ |
| 17 | + /** |
| 18 | + * @template U |
| 19 | + * @param array<array-key, U> $elements |
| 20 | + * @param int<1, max> $k |
| 21 | + * @return TestableStrategy<U> |
| 22 | + */ |
| 23 | + private function createTestableStrategy(array $elements, int $k): TestableStrategy |
| 24 | + { |
| 25 | + return new TestableStrategy($elements, $k); |
| 26 | + } |
| 27 | + |
| 28 | + public function testGenerateProducesExpectedResults(): void |
| 29 | + { |
| 30 | + $strategy = $this->createTestableStrategy(['A', 'B', 'C'], 2); |
| 31 | + |
| 32 | + $results = []; |
| 33 | + foreach ($strategy as $combo) { |
| 34 | + $results[] = $combo; |
| 35 | + } |
| 36 | + |
| 37 | + $expected = [ |
| 38 | + ['A', 'B'], |
| 39 | + ['A', 'C'], |
| 40 | + ['B', 'C'], |
| 41 | + ]; |
| 42 | + |
| 43 | + self::assertEquals($expected, $results); |
| 44 | + self::assertCount(3, $results); |
| 45 | + } |
| 46 | + |
| 47 | + // === Validation Tests (Base Class Logic) === |
| 48 | + |
| 49 | + public function testThrowsOnEmptyArray(): void |
| 50 | + { |
| 51 | + $this->expectException(InvalidArgumentException::class); |
| 52 | + $this->expectExceptionMessage('Cannot generate combinations/permutations from empty array.'); |
| 53 | + |
| 54 | + $this->createTestableStrategy([], 1); |
| 55 | + } |
| 56 | + |
| 57 | + public function testThrowsOnNumLessThanZero(): void |
| 58 | + { |
| 59 | + $this->expectException(InvalidArgumentException::class); |
| 60 | + $this->expectExceptionMessage('$k must be greater than zero, got: -1'); |
| 61 | + |
| 62 | + /** @phpstan-ignore argument.type */ |
| 63 | + $this->createTestableStrategy(['A'], -1); |
| 64 | + } |
| 65 | + |
| 66 | + // === Algorithmic/Behavioral Tests === |
| 67 | + |
| 68 | + public function testIteratorReusability(): void |
| 69 | + { |
| 70 | + $strategy = $this->createTestableStrategy(['A', 'B'], 1); |
| 71 | + |
| 72 | + // First iteration |
| 73 | + $firstPass = []; |
| 74 | + foreach ($strategy as $combo) { |
| 75 | + $firstPass[] = $combo; |
| 76 | + } |
| 77 | + |
| 78 | + // Second iteration on iterator |
| 79 | + $secondPass = []; |
| 80 | + foreach ($strategy as $combo) { |
| 81 | + $secondPass[] = $combo; |
| 82 | + } |
| 83 | + |
| 84 | + self::assertEquals($firstPass, $secondPass); |
| 85 | + self::assertEquals([['A'], ['B']], $firstPass); |
| 86 | + } |
| 87 | + |
| 88 | + public function testDuplicateElementsAreTreatedAsDistinct(): void |
| 89 | + { |
| 90 | + // Each array position is treated as a distinct identity |
| 91 | + $strategy = $this->createTestableStrategy(['A', 'A', 'B'], 2); |
| 92 | + |
| 93 | + $results = []; |
| 94 | + foreach ($strategy as $combo) { |
| 95 | + $results[] = $combo; |
| 96 | + } |
| 97 | + |
| 98 | + // Should get 3 combinations: first-A+second-A, first-A+B, second-A+B |
| 99 | + $expected = [ |
| 100 | + ['A', 'A'], // position 0 + position 1 |
| 101 | + ['A', 'B'], // position 0 + position 2 |
| 102 | + ['A', 'B'], // position 1 + position 2 |
| 103 | + ]; |
| 104 | + |
| 105 | + self::assertEquals($expected, $results); |
| 106 | + self::assertCount(3, $results); |
| 107 | + } |
| 108 | + |
| 109 | + public function testIteratorToArrayPreservesAllResults(): void |
| 110 | + { |
| 111 | + $strategy = $this->createTestableStrategy(['A', 'B', 'C'], 2); |
| 112 | + |
| 113 | + // Using iterator_to_array with preserve_keys = true should preserve all results |
| 114 | + $arrayResults = iterator_to_array($strategy, true); |
| 115 | + |
| 116 | + // Expected: 3 combinations |
| 117 | + $expected = [ |
| 118 | + ['A', 'B'], |
| 119 | + ['A', 'C'], |
| 120 | + ['B', 'C'], |
| 121 | + ]; |
| 122 | + |
| 123 | + // This should have all 3 results, not just the last one |
| 124 | + self::assertCount(3, $arrayResults); |
| 125 | + self::assertEquals($expected, array_values($arrayResults)); |
| 126 | + } |
| 127 | +} |
| 128 | + |
| 129 | +/** |
| 130 | + * Simple mock implementation for testing AbstractStrategy behavior. |
| 131 | + * Uses basic "combination without repetition" logic for simplicity. |
| 132 | + * |
| 133 | + * @template T |
| 134 | + * @extends AbstractStrategy<T> |
| 135 | + */ |
| 136 | +final readonly class TestableStrategy extends AbstractStrategy |
| 137 | +{ |
| 138 | + #[Override] |
| 139 | + protected function next(array $elements, int $i): array |
| 140 | + { |
| 141 | + return array_slice($elements, $i + 1); |
| 142 | + } |
| 143 | + |
| 144 | + #[Override] |
| 145 | + public function count(): int |
| 146 | + { |
| 147 | + // Simple combination formula for testing |
| 148 | + /** @var int<0, max> */ |
| 149 | + return gmp_intval( |
| 150 | + gmp_div( |
| 151 | + gmp_fact($this->n), |
| 152 | + gmp_mul(gmp_fact($this->k), gmp_fact(gmp_sub($this->n, $this->k))) |
| 153 | + ) |
| 154 | + ); |
| 155 | + } |
| 156 | +} |
0 commit comments