-
Notifications
You must be signed in to change notification settings - Fork 91
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support of 'random_int' and 'random_bytes' builtins (#977)
- Loading branch information
Showing
12 changed files
with
185 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
@ok php8 | ||
<?php | ||
|
||
function test_random_bytes() { | ||
for ($i = 1; $i < 50; $i += 1) { | ||
var_dump(strlen(random_bytes($i))); | ||
} | ||
} | ||
|
||
test_random_bytes(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
@ok php8 | ||
<?php | ||
|
||
function test_random_int() { | ||
$x = random_int(100, 500); | ||
var_dump($x >= 100 && $x <= 500); | ||
|
||
$x = random_int(-450, -200); | ||
var_dump($x >= -450 && $x <= -200); | ||
|
||
$x = random_int(-124, 107); | ||
var_dump($x >= -124 && $x <= 107); | ||
|
||
$x = random_int(0, PHP_INT_MAX); | ||
var_dump($x >= 0 && $x <= PHP_INT_MAX); | ||
|
||
$x = random_int(-PHP_INT_MAX - 1, 0); | ||
var_dump($x >= -PHP_INT_MAX - 1 && $x <= 0); | ||
|
||
$x = random_int(-PHP_INT_MAX - 1, PHP_INT_MAX); | ||
var_dump($x >= (-PHP_INT_MAX - 1) && $x <= PHP_INT_MAX); | ||
|
||
var_dump(random_int(0, 0)); | ||
var_dump(random_int(1, 1)); | ||
var_dump(random_int(-4, -4)); | ||
var_dump(random_int(9287167122323, 9287167122323)); | ||
var_dump(random_int(-8548276162, -8548276162)); | ||
|
||
var_dump(random_int(PHP_INT_MAX, PHP_INT_MAX)); | ||
var_dump(random_int(-PHP_INT_MAX - 1, -PHP_INT_MAX - 1)); | ||
} | ||
|
||
test_random_int(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
@ok php8.2 | ||
KPHP_REQUIRE_FUNCTIONS_TYPING=1 | ||
<?php | ||
|
||
/* | ||
Random\RandomException (extends Exception) | ||
*/ | ||
|
||
function fmt_throwable(Throwable $e): string { | ||
return get_class($e) . ":{$e->getLine()}:{$e->getMessage()}:{$e->getCode()}"; | ||
} | ||
|
||
function test_random_exception() { | ||
$to_throw = new Random\RandomException(__FUNCTION__, __LINE__ + 1); | ||
|
||
try { | ||
throw $to_throw; | ||
} catch (LogicException $e) { | ||
var_dump([__LINE__ => 'unreachable']); | ||
} catch (RuntimeException $e) { | ||
var_dump([__LINE__ => 'unreachable']); | ||
} catch (Exception $e) { | ||
var_dump([__LINE__ => fmt_throwable($e)]); | ||
var_dump([__LINE__ => $e->getMessage()]); | ||
} | ||
|
||
try { | ||
throw $to_throw; | ||
} catch (Random\RandomException $e) { | ||
var_dump([__LINE__ => fmt_throwable($e)]); | ||
var_dump([__LINE__ => $e->getMessage()]); | ||
} | ||
} | ||
|
||
test_random_exception(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters