[Random] add tests (#4)

[Random] add tests
This commit is contained in:
Saif Eddin G 2019-12-25 19:13:19 +01:00 committed by GitHub
commit f9b3017591
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 133 additions and 4 deletions

View File

@ -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>

View File

@ -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);
}

View File

@ -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);
}
}

View File

@ -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);
}
}

View File

@ -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);
}
}

View File

@ -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));
// }
}