|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Anarchitecture\combinatorics\Tests; |
| 6 | + |
| 7 | +use Generator; |
| 8 | +use InvalidArgumentException; |
| 9 | +use PHPUnit\Framework\TestCase; |
| 10 | + |
| 11 | +use Anarchitecture\pipe as p; |
| 12 | + |
| 13 | +use function Anarchitecture\combinatorics\iterable_combinations; |
| 14 | + |
| 15 | +final class IterableCombinationsTest extends TestCase |
| 16 | +{ |
| 17 | + public function testItThrowsWhenNIsNegative() : void { |
| 18 | + |
| 19 | + $this->expectException(InvalidArgumentException::class); |
| 20 | + |
| 21 | + iterable_combinations(-1); |
| 22 | + } |
| 23 | + |
| 24 | + public function testNZeroYieldsOneEmptyCombination() : void { |
| 25 | + |
| 26 | + $result = ['a' => 1, 'b' => 2] |
| 27 | + |> iterable_combinations(0) |
| 28 | + |> p\collect(...); |
| 29 | + |
| 30 | + self::assertSame([[]], $result); |
| 31 | + } |
| 32 | + |
| 33 | + public function testEmptyInputNZeroYieldsOneEmptyCombination() : void { |
| 34 | + |
| 35 | + $result = [] |
| 36 | + |> iterable_combinations(0) |
| 37 | + |> p\collect(...); |
| 38 | + |
| 39 | + self::assertSame([[]], $result); |
| 40 | + } |
| 41 | + |
| 42 | + public function testEmptyInputNPositiveYieldsNothing() : void { |
| 43 | + |
| 44 | + $result = [] |
| 45 | + |> iterable_combinations(2) |
| 46 | + |> p\collect(...); |
| 47 | + |
| 48 | + self::assertSame([], $result); |
| 49 | + } |
| 50 | + |
| 51 | + public function testNGreaterThanCountYieldsNothing() : void { |
| 52 | + |
| 53 | + $result = ['a' => 1, 'b' => 2] |
| 54 | + |> iterable_combinations(3) |
| 55 | + |> p\collect(...); |
| 56 | + |
| 57 | + self::assertSame([], $result); |
| 58 | + } |
| 59 | + |
| 60 | + public function testTwoOfThreePreservesKeysAndExpectedOrder() : void { |
| 61 | + |
| 62 | + $items = ['a' => 1, 'b' => 2, 'c' => 3]; |
| 63 | + |
| 64 | + $result = $items |
| 65 | + |> iterable_combinations(2) |
| 66 | + |> p\collect(...); |
| 67 | + |
| 68 | + self::assertContains(['b' => 2, 'c' => 3], $result); |
| 69 | + self::assertContains(['a' => 1, 'c' => 3], $result); |
| 70 | + self::assertContains(['a' => 1, 'b' => 2], $result); |
| 71 | + } |
| 72 | + |
| 73 | + public function testItWorksWithGeneratorInputAndPreservesKeys() : void { |
| 74 | + |
| 75 | + $items = static function (): Generator { |
| 76 | + yield 'x' => 10; |
| 77 | + yield 'y' => 20; |
| 78 | + yield 'z' => 30; |
| 79 | + }; |
| 80 | + |
| 81 | + $result = $items() |
| 82 | + |> iterable_combinations(2) |
| 83 | + |> p\collect(...); |
| 84 | + |
| 85 | + self::assertContains(['y' => 20, 'z' => 30], $result); |
| 86 | + self::assertContains(['x' => 10, 'z' => 30], $result); |
| 87 | + self::assertContains(['x' => 10, 'y' => 20], $result); |
| 88 | + } |
| 89 | + |
| 90 | + public function testCountMatchesBinomialCoefficient() : void { |
| 91 | + |
| 92 | + $items = ['a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5]; |
| 93 | + |
| 94 | + $result = $items |
| 95 | + |> iterable_combinations(3) |
| 96 | + |> p\collect(...); |
| 97 | + |
| 98 | + self::assertCount(10, $result); |
| 99 | + } |
| 100 | +} |
0 commit comments