|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright 2017, 2018 Alexey Kopytko <[email protected]> |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +declare(strict_types=1); |
| 19 | + |
| 20 | +namespace Tests\Pipeline; |
| 21 | + |
| 22 | +use PHPUnit\Framework\TestCase; |
| 23 | +use function Pipeline\map; |
| 24 | +use function Pipeline\take; |
| 25 | +use function range; |
| 26 | + |
| 27 | +/** |
| 28 | + * @covers \Pipeline\Standard |
| 29 | + * |
| 30 | + * @internal |
| 31 | + */ |
| 32 | +final class RunningCountTest extends TestCase |
| 33 | +{ |
| 34 | + public function testRunningCount(): void |
| 35 | + { |
| 36 | + $countEven = 1; |
| 37 | + |
| 38 | + $pipeline = map(fn () => yield from range(0, 100)) |
| 39 | + ->runningCount($countAll) |
| 40 | + ->filter(fn (int $n) => 0 === $n % 2) |
| 41 | + ->runningCount($countEven) |
| 42 | + ->filter(fn (int $n) => $n % 3); |
| 43 | + |
| 44 | + $this->assertSame(0, $countAll); |
| 45 | + $this->assertSame(1, $countEven); |
| 46 | + |
| 47 | + $this->assertSame(34, $pipeline->count()); |
| 48 | + |
| 49 | + $this->assertSame(101, $countAll); |
| 50 | + $this->assertSame(51, $countEven - 1); |
| 51 | + } |
| 52 | + |
| 53 | + public function testRunningCountLazy(): void |
| 54 | + { |
| 55 | + $countEven = 1; |
| 56 | + |
| 57 | + $pipeline = map(fn () => yield from range(0, 100)) |
| 58 | + ->runningCount($countAll) |
| 59 | + ->filter(fn (int $n) => 0 === $n % 2) |
| 60 | + ->runningCount($countEven) |
| 61 | + ->filter(fn (int $n) => $n % 3); |
| 62 | + |
| 63 | + $this->assertSame(0, $countAll); |
| 64 | + $this->assertSame(1, $countEven); |
| 65 | + |
| 66 | + foreach ($pipeline as $item) { |
| 67 | + $this->assertSame(2, $item); |
| 68 | + |
| 69 | + break; |
| 70 | + } |
| 71 | + |
| 72 | + // Because we need to inspect 3 numbers to get to 2: filtered out 0 and 1 |
| 73 | + $this->assertSame(3, $countAll); |
| 74 | + $this->assertSame(3, $countEven); |
| 75 | + } |
| 76 | + |
| 77 | + public function testRunningCountArray(): void |
| 78 | + { |
| 79 | + $countEven = 1; |
| 80 | + |
| 81 | + $pipeline = take(range(0, 100)) |
| 82 | + ->runningCount($countAll) |
| 83 | + ->filter(fn (int $n) => 0 === $n % 2) |
| 84 | + ->runningCount($countEven) |
| 85 | + ->filter(fn (int $n) => $n % 3); |
| 86 | + |
| 87 | + $this->assertSame(101, $countAll); |
| 88 | + $this->assertSame(51, $countEven - 1); |
| 89 | + |
| 90 | + $this->assertSame(34, $pipeline->count()); |
| 91 | + } |
| 92 | +} |
0 commit comments