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
|
|
|
|
2020-10-15 10:18:03 +02:00
|
|
|
final class ReduceTest extends TestCase
|
2019-12-24 01:52:07 +01:00
|
|
|
{
|
2020-09-01 07:50:23 +02:00
|
|
|
/**
|
|
|
|
* @dataProvider provideData
|
|
|
|
*/
|
|
|
|
public function testReduce($expected, iterable $iterable, callable $function, $initial = null): void
|
|
|
|
{
|
2020-10-15 10:18:03 +02:00
|
|
|
static::assertSame($expected, Iter\reduce($iterable, $function, $initial));
|
2020-09-01 07:50:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function provideData(): iterable
|
|
|
|
{
|
2020-10-15 10:18:03 +02:00
|
|
|
yield [null, [], static fn ($accumulator, $v) => $accumulator, null];
|
|
|
|
yield [6, [1, 2, 3], static fn ($accumulator, $v) => $accumulator + $v, 0];
|
|
|
|
yield [6, Iter\to_iterator([1, 2, 3]), static fn ($accumulator, $v) => $accumulator + $v, 0];
|
2020-09-01 07:50:23 +02:00
|
|
|
}
|
2019-12-24 01:52:07 +01:00
|
|
|
}
|