endtoend-test-psl/tests/Psl/Iter/ReduceWithKeysTest.php
2020-10-15 11:05:30 +02:00

27 lines
760 B
PHP

<?php
declare(strict_types=1);
namespace Psl\Tests\Iter;
use PHPUnit\Framework\TestCase;
use Psl\Iter;
final class ReduceWithKeysTest extends TestCase
{
/**
* @dataProvider provideData
*/
public function testReduceWithKeys($expected, iterable $iterable, callable $function, $initial = null): void
{
static::assertSame($expected, Iter\reduce_with_keys($iterable, $function, $initial));
}
public function provideData(): iterable
{
yield [null, [], static fn ($accumulator, $k, $v) => $accumulator, null];
yield [6, [1, 2, 3], static fn ($accumulator, $k, $v) => $accumulator + $v, 0];
yield [6, Iter\to_iterator([1, 2, 3]), static fn ($accumulator, $k, $v) => $accumulator + $v, 0];
}
}