Skip to content

Commit

Permalink
random: split Randomizer::getInt() without argument to Randomizer::ne…
Browse files Browse the repository at this point in the history
…xtInt()

Since argument overloading is not safe for reflection, the method needed
to be split appropriately.

Co-authored-by: Tim Düsterhus <timwolla@googlemail.com>

Closes GH-9057.
  • Loading branch information
zeriyoshi authored and cmb69 committed Aug 1, 2022
1 parent 59d257d commit 4e92c74
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 34 deletions.
2 changes: 2 additions & 0 deletions NEWS
Expand Up @@ -41,6 +41,8 @@ PHP NEWS
call twice) (zeriyoshi)
. Change Mt19937 to throw a ValueError instead of InvalidArgumentException
for invalid $mode. (timwolla)
. Splitted Random\Randomizer::getInt() (without arguments) to
Random\Randomizer::nextInt(). (zeriyoshi)

- Sockets:
. Added SOL_FILTER socket option for Solaris. (David Carlier)
Expand Down
4 changes: 3 additions & 1 deletion ext/random/random.stub.php
Expand Up @@ -131,7 +131,9 @@ final class Randomizer

public function __construct(?Engine $engine = null) {}

public function getInt(int $min = UNKNOWN, int $max = UNKNOWN): int {}
public function nextInt(): int {}

public function getInt(int $min, int $max): int {}

public function getBytes(int $length): string {}

Expand Down
8 changes: 6 additions & 2 deletions ext/random/random_arginfo.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 22 additions & 14 deletions ext/random/randomizer.c
Expand Up @@ -91,26 +91,34 @@ PHP_METHOD(Random_Randomizer, __construct)
}
/* }}} */

/* {{{ Generate positive random number */
PHP_METHOD(Random_Randomizer, nextInt)
{
php_random_randomizer *randomizer = Z_RANDOM_RANDOMIZER_P(ZEND_THIS);
uint64_t result;

ZEND_PARSE_PARAMETERS_NONE();

result = randomizer->algo->generate(randomizer->status);
if (randomizer->status->last_generated_size > sizeof(zend_long)) {
zend_throw_exception(spl_ce_RuntimeException, "Generated value exceeds size of int", 0);
RETURN_THROWS();
}
if (EG(exception)) {
zend_throw_exception(spl_ce_RuntimeException, "Random number generation failed", 0);
RETURN_THROWS();
}

RETURN_LONG((zend_long) (result >> 1));
}
/* }}} */

/* {{{ Generate random number in range */
PHP_METHOD(Random_Randomizer, getInt)
{
php_random_randomizer *randomizer = Z_RANDOM_RANDOMIZER_P(ZEND_THIS);
uint64_t result;
zend_long min, max;
int argc = ZEND_NUM_ARGS();

if (argc == 0) {
result = randomizer->algo->generate(randomizer->status);
if (randomizer->status->last_generated_size > sizeof(zend_long)) {
zend_throw_exception(spl_ce_RuntimeException, "Generated value exceeds size of int", 0);
RETURN_THROWS();
}
if (EG(exception)) {
zend_throw_exception(spl_ce_RuntimeException, "Random number generation failed", 0);
RETURN_THROWS();
}
RETURN_LONG((zend_long) (result >> 1));
}

ZEND_PARSE_PARAMETERS_START(2, 2)
Z_PARAM_LONG(min)
Expand Down
17 changes: 14 additions & 3 deletions ext/random/tests/03_randomizer/basic.phpt
Expand Up @@ -27,6 +27,17 @@ $engines[] = new UserEngine();
foreach ($engines as $engine) {
$randomizer = new Random\Randomizer($engine);

// nextInt
for ($i = 0; $i < 1000; $i++) {
try {
$randomizer->nextInt();
} catch (\RuntimeException $e) {
if ($e->getMessage() !== 'Generated value exceeds size of int') {
die($engine::class . ": nextInt: failure: {$e->getMessage()}");
}
}
}

// getInt
for ($i = 0; $i < 1000; $i++) {
$result = $randomizer->getInt(-50, 50);
Expand All @@ -39,7 +50,7 @@ foreach ($engines as $engine) {
for ($i = 0; $i < 1000; $i++) {
$length = \random_int(1, 1024);
if (\strlen($randomizer->getBytes($length)) !== $length) {
die($engine::class . ': getBytes: failure.');
die($engine::class . ': getBytes: failure');
}
}

Expand All @@ -53,14 +64,14 @@ foreach ($engines as $engine) {
}
}

die($engine::class . ': shuffleArray: failure.');
die($engine::class . ': shuffleArray: failure');
})();

// shuffleBytes
$string = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.';
$shuffled_string = $randomizer->shuffleBytes($string);
if ($string === $shuffled_string) {
die($engine::class . ': shuffleBytes: failure.');
die($engine::class . ': shuffleBytes: failure');
}
}

Expand Down
4 changes: 2 additions & 2 deletions ext/random/tests/03_randomizer/compatibility_mt.phpt
Expand Up @@ -6,15 +6,15 @@ Random: Randomizer: Compatibility: Mt19937
$randomizer = new \Random\Randomizer(new \Random\Engine\Mt19937(1234, \MT_RAND_PHP));
\mt_srand(1234, \MT_RAND_PHP);
for ($i = 0; $i < 1000; $i++) {
if ($randomizer->getInt() !== \mt_rand()) {
if ($randomizer->nextInt() !== \mt_rand()) {
die('failure');
}
}

$randomizer = new \Random\Randomizer(new \Random\Engine\Mt19937(1234, \MT_RAND_MT19937));
\mt_srand(1234, \MT_RAND_MT19937);
for ($i = 0; $i < 1000; $i++) {
if ($randomizer->getInt() !== \mt_rand()) {
if ($randomizer->nextInt() !== \mt_rand()) {
die('failure');
}
}
Expand Down
18 changes: 6 additions & 12 deletions ext/random/tests/03_randomizer/compatibility_user.phpt
Expand Up @@ -15,8 +15,8 @@ $user_randomizer = new \Random\Randomizer(new class () implements \Random\Engine
}
});
for ($i = 0; $i < 1000; $i++) {
$native = $native_randomizer->getInt();
$user = $user_randomizer->getInt();
$native = $native_randomizer->nextInt();
$user = $user_randomizer->nextInt();
if ($native !== $user) {
die("failure Mt19937 i: {$i} native: {$native} user: {$user}");
}
Expand All @@ -36,16 +36,13 @@ try {
});

for ($i = 0; $i < 1000; $i++) {
$native = $native_randomizer->getInt();
$user = $user_randomizer->getInt();
$native = $native_randomizer->nextInt();
$user = $user_randomizer->nextInt();
if ($native !== $user) {
die("failure PcgOneseq128XslRr64 i: {$i} native: {$native} user: {$user}");
}
}
} catch (\RuntimeException $e) {
if (\PHP_INT_SIZE >= 8) {
throw $e;
}
if ($e->getMessage() !== 'Generated value exceeds size of int') {
throw $e;
}
Expand All @@ -65,16 +62,13 @@ try {
});

for ($i = 0; $i < 1000; $i++) {
$native = $native_randomizer->getInt();
$user = $user_randomizer->getInt();
$native = $native_randomizer->nextInt();
$user = $user_randomizer->nextInt();
if ($native !== $user) {
die("failure Xoshiro256StarStar i: {$i} native: {$native} user: {$user}");
}
}
} catch (\RuntimeException $e) {
if (\PHP_INT_SIZE >= 8) {
throw $e;
}
if ($e->getMessage() !== 'Generated value exceeds size of int') {
throw $e;
}
Expand Down

0 comments on commit 4e92c74

Please sign in to comment.