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

34 lines
784 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;
use Psl\Iter;
2019-12-24 01:52:07 +01: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];
$value = Iter\random($iterable);
2020-09-01 07:50:23 +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]);
$value = Iter\random($iterable);
2020-09-01 07:50:23 +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
}