forked from php/php-src
-
Notifications
You must be signed in to change notification settings - Fork 0
Randomize better checking #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Danack
wants to merge
11
commits into
TimWolla:randomizer-nextFloat
from
Danack:randomize-better_checking
Closed
Changes from 10 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
ae5e230
Add Randomizer::nextFloat()
TimWolla 7fb6ba2
Check that doubles are IEEE-754 in Randomizer::nextFloat()
TimWolla 985a7fa
Add Randomizer::nextFloat() tests
TimWolla 786cfaf
Add Randomizer::getFloat() implementing the y-section algorithm
TimWolla ca10d6c
Fix error message in Randomizer::getFloat()
TimWolla 5a6b02e
Fix method order in randomizer.c
TimWolla 3ad1788
Simplify `nextafter()` to `nextup()` / `nextdown()`
TimWolla 1be43c1
Implement getFloat_gamma() optimization
TimWolla 5ed75dc
Functions/methods should either work or not be present. Probably.
Danack 6d49d30
Skip tests when nextfloat unavailable.
Danack eb49d8d
Fix nextFloat test syntax
TimWolla File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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,55 @@ | ||
--TEST-- | ||
Random: Randomizer: nextFloat(): Basic functionality | ||
--SKIPIF-- | ||
<?php | ||
|
||
if (!method_exists("Randomizer", "nextFloat")) { | ||
die("skip Randomizer::nextFloat not available"); | ||
} | ||
--FILE-- | ||
<?php | ||
|
||
use Random\Engine; | ||
use Random\Engine\Mt19937; | ||
use Random\Engine\PcgOneseq128XslRr64; | ||
use Random\Engine\Secure; | ||
use Random\Engine\Test\TestShaEngine; | ||
use Random\Engine\Xoshiro256StarStar; | ||
use Random\Randomizer; | ||
|
||
require __DIR__ . "/../../engines.inc"; | ||
|
||
$engines = []; | ||
$engines[] = new Mt19937(null, MT_RAND_MT19937); | ||
$engines[] = new Mt19937(null, MT_RAND_PHP); | ||
$engines[] = new PcgOneseq128XslRr64(); | ||
$engines[] = new Xoshiro256StarStar(); | ||
$engines[] = new Secure(); | ||
$engines[] = new TestShaEngine(); | ||
|
||
foreach ($engines as $engine) { | ||
echo $engine::class, PHP_EOL; | ||
|
||
$randomizer = new Randomizer($engine); | ||
|
||
// Basic range test. | ||
for ($i = 0; $i < 10_000; $i++) { | ||
$result = $randomizer->nextFloat(); | ||
|
||
if ($result < 0 || $result >= 1) { | ||
die("failure: out of range at {$i}"); | ||
} | ||
} | ||
} | ||
|
||
die('success'); | ||
|
||
?> | ||
--EXPECT-- | ||
Random\Engine\Mt19937 | ||
Random\Engine\Mt19937 | ||
Random\Engine\PcgOneseq128XslRr64 | ||
Random\Engine\Xoshiro256StarStar | ||
Random\Engine\Secure | ||
Random\Engine\Test\TestShaEngine | ||
success |
47 changes: 47 additions & 0 deletions
47
ext/random/tests/03_randomizer/methods/nextFloat_spacing.phpt
This file contains hidden or 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,47 @@ | ||
--TEST-- | ||
Random: Randomizer: nextFloat(): Return values are evenly spaced. | ||
--SKIPIF-- | ||
<?php | ||
|
||
if (!method_exists("Randomizer", "nextFloat")) { | ||
die("skip Randomizer::nextFloat not available"); | ||
} | ||
TimWolla marked this conversation as resolved.
Show resolved
Hide resolved
|
||
--FILE-- | ||
<?php | ||
|
||
use Random\Engine; | ||
use Random\Randomizer; | ||
|
||
final class StaticEngine implements Engine | ||
{ | ||
public function __construct(private string $value) | ||
{ | ||
} | ||
|
||
public function generate(): string | ||
{ | ||
return $this->value; | ||
} | ||
} | ||
|
||
$zero = new Randomizer(new StaticEngine("\x00\x00\x00\x00\x00\x00\x00\x00")); | ||
$one = new Randomizer(new StaticEngine("\x00\x08\x00\x00\x00\x00\x00\x00")); | ||
$two = new Randomizer(new StaticEngine("\x00\x10\x00\x00\x00\x00\x00\x00")); | ||
|
||
$max_minus_two = new Randomizer(new StaticEngine("\x00\xe8\xff\xff\xff\xff\xff\xff")); | ||
$max_minus_one = new Randomizer(new StaticEngine("\x00\xf0\xff\xff\xff\xff\xff\xff")); | ||
$max = new Randomizer(new StaticEngine("\x00\xf8\xff\xff\xff\xff\xff\xff")); | ||
|
||
var_dump($one->nextFloat() - $one->nextFloat() === $zero->nextFloat()); | ||
var_dump($two->nextFloat() - $one->nextFloat() === $one->nextFloat()); | ||
var_dump($max->nextFloat() - $max_minus_one->nextFloat() === $one->nextFloat()); | ||
var_dump($max_minus_one->nextFloat() - $max_minus_two->nextFloat() === $one->nextFloat()); | ||
var_dump($max->nextFloat() - $max_minus_two->nextFloat() === $two->nextFloat()); | ||
|
||
?> | ||
--EXPECT-- | ||
bool(true) | ||
bool(true) | ||
bool(true) | ||
bool(true) | ||
bool(true) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.