mirror of
https://github.com/danog/endtoend-test-psl.git
synced 2025-01-21 21:01:17 +01:00
[Random] add tests
This commit is contained in:
parent
d4cdaad0c4
commit
6954045938
@ -16,6 +16,9 @@
|
||||
<testsuite name="Psl Arr">
|
||||
<directory>tests/Psl/Arr</directory>
|
||||
</testsuite>
|
||||
<testsuite name="Psl Random">
|
||||
<directory>tests/Psl/Random</directory>
|
||||
</testsuite>
|
||||
</testsuites>
|
||||
|
||||
<filter>
|
||||
|
@ -4,10 +4,17 @@ declare(strict_types=1);
|
||||
|
||||
namespace Psl\Random;
|
||||
|
||||
use Psl;
|
||||
|
||||
/**
|
||||
* Returns a cryptographically secure random bytes.
|
||||
*/
|
||||
function bytes(int $length): string
|
||||
{
|
||||
Psl\invariant($length >= 0, 'Expected positive length, got %d', $length);
|
||||
if (0 === $length) {
|
||||
return '';
|
||||
}
|
||||
|
||||
return \random_bytes($length);
|
||||
}
|
||||
|
@ -5,8 +5,27 @@ declare(strict_types=1);
|
||||
namespace Psl\Tests\Random;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Psl\Exception;
|
||||
use Psl\Random;
|
||||
use Psl\Str;
|
||||
|
||||
/**
|
||||
* @covers \Psl\Random\bytes
|
||||
*/
|
||||
class BytesTest extends TestCase
|
||||
{
|
||||
// TODO: add tests.
|
||||
public function testBytes(): void
|
||||
{
|
||||
$random = Random\bytes(32);
|
||||
|
||||
self::assertSame(32, Str\Byte\length($random));
|
||||
}
|
||||
|
||||
public function testBytesThrowsForNegativeLength(): void
|
||||
{
|
||||
$this->expectException(Exception\InvariantViolationException::class);
|
||||
$this->expectExceptionMessage('Expected positive length, got -1');
|
||||
|
||||
Random\bytes(-1);
|
||||
}
|
||||
}
|
||||
|
@ -5,8 +5,19 @@ declare(strict_types=1);
|
||||
namespace Psl\Tests\Random;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Psl\Random;
|
||||
|
||||
/**
|
||||
* @covers \Psl\Random\float
|
||||
*/
|
||||
class FloatTest extends TestCase
|
||||
{
|
||||
// TODO: add tests.
|
||||
public function testFloat(): void
|
||||
{
|
||||
$random = Random\float();
|
||||
|
||||
self::assertIsFloat($random);
|
||||
self::assertGreaterThanOrEqual(0, $random);
|
||||
self::assertLessThanOrEqual(1, $random);
|
||||
}
|
||||
}
|
||||
|
@ -5,8 +5,47 @@ declare(strict_types=1);
|
||||
namespace Psl\Tests\Random;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Psl\Exception;
|
||||
use Psl\Math;
|
||||
use Psl\Random;
|
||||
|
||||
/**
|
||||
* @covers \Psl\Random\int
|
||||
*/
|
||||
class IntTest extends TestCase
|
||||
{
|
||||
// TODO: add tests.
|
||||
public function testInt(): void
|
||||
{
|
||||
$random = Random\int();
|
||||
|
||||
self::assertIsInt($random);
|
||||
self::assertGreaterThanOrEqual(Math\INT64_MIN, $random);
|
||||
self::assertLessThanOrEqual(Math\INT64_MAX, $random);
|
||||
}
|
||||
|
||||
public function testIntWithASpecificMin(): void
|
||||
{
|
||||
$random = Random\int(10);
|
||||
|
||||
self::assertIsInt($random);
|
||||
self::assertGreaterThanOrEqual(10, $random);
|
||||
self::assertLessThanOrEqual(Math\INT64_MAX, $random);
|
||||
}
|
||||
|
||||
public function testIntWithASpecificRange(): void
|
||||
{
|
||||
$random = Random\int(20, 1200);
|
||||
|
||||
self::assertIsInt($random);
|
||||
self::assertGreaterThanOrEqual(20, $random);
|
||||
self::assertLessThanOrEqual(1200, $random);
|
||||
}
|
||||
|
||||
public function testIntWithMinGreaterThanMax(): void
|
||||
{
|
||||
$this->expectException(Exception\InvariantViolationException::class);
|
||||
$this->expectExceptionMessage('Expected $min (20) to be less than or equal to $max (5).');
|
||||
|
||||
Random\int(20, 5);
|
||||
}
|
||||
}
|
||||
|
@ -4,9 +4,59 @@ declare(strict_types=1);
|
||||
|
||||
namespace Psl\Tests\Random;
|
||||
|
||||
use Psl\Str;
|
||||
use Psl\Random;
|
||||
use Psl\Exception;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
/**
|
||||
* @covers \Psl\Random\string
|
||||
*/
|
||||
class StringTest extends TestCase
|
||||
{
|
||||
// TODO: add tests.
|
||||
public function testString(): void
|
||||
{
|
||||
$random = Random\string(32);
|
||||
|
||||
self::assertSame(32, Str\length($random));
|
||||
foreach (Str\chunk($random) as $char) {
|
||||
self::assertTrue(Str\contains(Str\ALPHABET_ALPHANUMERIC, $char));
|
||||
}
|
||||
}
|
||||
|
||||
public function testStringWithSpecificChars(): void
|
||||
{
|
||||
$random = Random\string(32, 'abc');
|
||||
|
||||
self::assertSame(32, Str\length($random));
|
||||
foreach (Str\chunk($random) as $char) {
|
||||
self::assertTrue(Str\contains('abc', $char));
|
||||
}
|
||||
}
|
||||
|
||||
public function testStringThrowsForNegativeLength(): void
|
||||
{
|
||||
$this->expectException(Exception\InvariantViolationException::class);
|
||||
$this->expectExceptionMessage('Expected positive length, got -1');
|
||||
|
||||
Random\string(-1);
|
||||
}
|
||||
|
||||
public function testStringAlphabetMin(): void
|
||||
{
|
||||
$this->expectException(Exception\InvariantViolationException::class);
|
||||
$this->expectExceptionMessage('Expected $alphabet\'s length to be in [2^1, 2^56]');
|
||||
|
||||
Random\string(32, 'a');
|
||||
}
|
||||
|
||||
// public function testStringAlphabetMax(): void
|
||||
// {
|
||||
// $this->markTestSkipped('Memory exhausting');
|
||||
//
|
||||
// $this->expectException(Exception\InvariantViolationException::class);
|
||||
// $this->expectExceptionMessage('Expected $alphabet\'s length to be in [2^1, 2^56]');
|
||||
//
|
||||
// Random\string(32, Str\repeat('a', (2 ** 56) + 1));
|
||||
// }
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user