diff --git a/src/Standard.php b/src/Standard.php index 8e1486a..7f9b800 100644 --- a/src/Standard.php +++ b/src/Standard.php @@ -778,6 +778,9 @@ public function reservoir(int $size, ?callable $weightFunc = null): array } if ($size <= 0) { + // Discard the state to emulate full consumption + $this->pipeline = null; + return []; } diff --git a/tests/ReservoirTest.php b/tests/ReservoirTest.php index 64e2abb..1eef213 100644 --- a/tests/ReservoirTest.php +++ b/tests/ReservoirTest.php @@ -158,9 +158,15 @@ public static function provideWeightedInputs(): iterable */ public function testWeightedSampleFromGenerator(array $input, int $size, callable $weightFn, array $expected): void { - $this->assertSame($expected, map(static function () use ($input) { + $pipeline = map(static function () use ($input) { yield from $input; - })->reservoir($size, $weightFn)); + }); + + $this->assertSame($expected, $pipeline->reservoir($size, $weightFn)); + + if ($size <= 0) { + $this->assertSame([], $pipeline->toArray()); + } } /**