diff --git a/phpunit.xml.dist b/phpunit.xml.dist
index 18552dd..6b1f1b1 100644
--- a/phpunit.xml.dist
+++ b/phpunit.xml.dist
@@ -16,6 +16,9 @@
tests/Psl/Arr
+
+ tests/Psl/Random
+
diff --git a/src/Psl/Random/bytes.php b/src/Psl/Random/bytes.php
index b007228..ce5c820 100644
--- a/src/Psl/Random/bytes.php
+++ b/src/Psl/Random/bytes.php
@@ -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);
}
diff --git a/tests/Psl/Random/BytesTest.php b/tests/Psl/Random/BytesTest.php
index 12cc54f..2818e8a 100644
--- a/tests/Psl/Random/BytesTest.php
+++ b/tests/Psl/Random/BytesTest.php
@@ -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);
+ }
}
diff --git a/tests/Psl/Random/FloatTest.php b/tests/Psl/Random/FloatTest.php
index a0b010b..96fb2fc 100644
--- a/tests/Psl/Random/FloatTest.php
+++ b/tests/Psl/Random/FloatTest.php
@@ -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);
+ }
}
diff --git a/tests/Psl/Random/IntTest.php b/tests/Psl/Random/IntTest.php
index 789c576..4eba963 100644
--- a/tests/Psl/Random/IntTest.php
+++ b/tests/Psl/Random/IntTest.php
@@ -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);
+ }
}
diff --git a/tests/Psl/Random/StringTest.php b/tests/Psl/Random/StringTest.php
index 7bb4f8c..6018565 100644
--- a/tests/Psl/Random/StringTest.php
+++ b/tests/Psl/Random/StringTest.php
@@ -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));
+ // }
}