endtoend-test-psl/tests/Psl/Iter/ReductionsTest.php

29 lines
790 B
PHP
Raw Normal View History

2019-12-24 01:52:07 +01:00
<?php
declare(strict_types=1);
namespace Psl\Tests\Iter;
use PHPUnit\Framework\TestCase;
2020-09-01 07:50:23 +02:00
use Psl\Iter;
2019-12-24 01:52:07 +01:00
class ReductionsTest extends TestCase
{
2020-09-01 07:50:23 +02:00
/**
* @dataProvider provideData
*/
public function testReductions(array $expected, iterable $iterable, callable $function, $initial = null): void
{
$result = Iter\reductions($iterable, $function, $initial);
self::assertSame($expected, Iter\to_array_with_keys($result));
}
public function provideData(): iterable
{
2020-09-01 07:56:15 +02:00
yield [[], [], fn ($accumulator, $k, $v) => $accumulator, null];
yield [[1, 3, 6], [1, 2, 3], fn ($accumulator, $k, $v) => $accumulator + $v, 0];
yield [[1, 3, 6], Iter\to_iterator([1, 2, 3]), fn ($accumulator, $k, $v) => $accumulator + $v, 0];
2020-09-01 07:50:23 +02:00
}
2019-12-24 01:52:07 +01:00
}