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;
|
|
|
|
use Psl\Iter;
|
2019-12-24 01:52:07 +01:00
|
|
|
|
2020-10-15 10:18:03 +02:00
|
|
|
final class RandomTest extends TestCase
|
2019-12-24 01:52:07 +01:00
|
|
|
{
|
2020-09-01 07:50:23 +02:00
|
|
|
public function testRandom(): void
|
|
|
|
{
|
|
|
|
$iterable = [1, 2, 3, 4, 5];
|
2020-09-05 17:23:37 +02:00
|
|
|
$value = Iter\random($iterable);
|
2020-09-01 07:50:23 +02:00
|
|
|
|
2020-10-15 10:18:03 +02:00
|
|
|
static::assertTrue(Iter\contains($iterable, $value));
|
2020-09-01 07:50:23 +02:00
|
|
|
|
|
|
|
$iterable = Iter\to_iterator([1, 2, 3, 4, 5]);
|
2020-09-05 17:23:37 +02:00
|
|
|
$value = Iter\random($iterable);
|
2020-09-01 07:50:23 +02:00
|
|
|
|
2020-10-15 10:18:03 +02:00
|
|
|
static::assertTrue(Iter\contains($iterable, $value));
|
2020-09-01 07:50:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testRandomWithEmptyIterator(): void
|
|
|
|
{
|
|
|
|
$this->expectException(Psl\Exception\InvariantViolationException::class);
|
|
|
|
$this->expectExceptionMessage('Expected a non-empty iterable.');
|
|
|
|
|
|
|
|
Iter\random([]);
|
|
|
|
}
|
2019-12-24 01:52:07 +01:00
|
|
|
}
|