-
Notifications
You must be signed in to change notification settings - Fork 7.8k
Unify structure for ext/random's engine tests #9321
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
Large diffs are not rendered by default.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,35 @@ | ||
--TEST-- | ||
Random: Engine: serialize: native | ||
Random: Engine: Serialization of native engines must preserve the sequence | ||
--FILE-- | ||
<?php | ||
|
||
use Random\Engine\Mt19937; | ||
use Random\Engine\PcgOneseq128XslRr64; | ||
use Random\Engine\Xoshiro256StarStar; | ||
|
||
$engines = []; | ||
$engines[] = new \Random\Engine\Mt19937(1234); | ||
$engines[] = new \Random\Engine\PcgOneseq128XslRr64(1234); | ||
$engines[] = new \Random\Engine\Xoshiro256StarStar(1234); | ||
$engines[] = new Mt19937(1234); | ||
$engines[] = new PcgOneseq128XslRr64(1234); | ||
$engines[] = new Xoshiro256StarStar(1234); | ||
|
||
foreach ($engines as $engine) { | ||
for ($i = 0; $i < 1000; $i++) { | ||
for ($i = 0; $i < 10_000; $i++) { | ||
$engine->generate(); | ||
} | ||
|
||
$engine2 = unserialize(serialize($engine)); | ||
for ($i = 0; $i < 5000; $i++) { | ||
|
||
for ($i = 0; $i < 10_000; $i++) { | ||
if ($engine->generate() !== $engine2->generate()) { | ||
$className = $engine::class; | ||
die("failure class: {$className} i: {$i}"); | ||
|
||
die("failure: {$className} at {$i}"); | ||
} | ||
} | ||
} | ||
|
||
die('success'); | ||
|
||
?> | ||
--EXPECT-- | ||
success |
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 |
---|---|---|
@@ -1,48 +1,54 @@ | ||
--TEST-- | ||
Random: Engine: serialize: user | ||
Random: Engine: Serialization of user engines must preserve the sequence | ||
--FILE-- | ||
<?php | ||
|
||
final class User64 implements \Random\Engine | ||
use Random\Engine; | ||
|
||
final class User64 implements Engine | ||
{ | ||
private int $count = 0; | ||
|
||
public function generate(): string | ||
{ | ||
return \pack('P*', ++$this->count); | ||
return pack('P*', ++$this->count); | ||
} | ||
} | ||
|
||
final class User32 implements \Random\Engine | ||
final class User32 implements Engine | ||
{ | ||
private int $count = 0; | ||
|
||
public function generate(): string | ||
{ | ||
return \pack('V', ++$this->count); | ||
return pack('V', ++$this->count); | ||
} | ||
} | ||
|
||
$engines = []; | ||
if (\PHP_INT_SIZE >= 8) { | ||
$engines[] = new \User64(); | ||
if (PHP_INT_SIZE >= 8) { | ||
$engines[] = new User64(); | ||
} | ||
$engines[] = new \User32(); | ||
$engines[] = new User32(); | ||
|
||
foreach ($engines as $engine) { | ||
for ($i = 0; $i < 1000; $i++) { | ||
for ($i = 0; $i < 10_000; $i++) { | ||
$engine->generate(); | ||
} | ||
|
||
$engine2 = unserialize(serialize($engine)); | ||
for ($i = 0; $i < 5000; $i++) { | ||
|
||
for ($i = 0; $i < 10_000; $i++) { | ||
if ($engine->generate() !== $engine2->generate()) { | ||
$className = $engine::class; | ||
die("failure class: {$className} i: {$i}"); | ||
|
||
die("failure: {$className} at {$i}"); | ||
} | ||
} | ||
} | ||
|
||
die('success'); | ||
|
||
?> | ||
--EXPECT-- | ||
success |
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
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 |
---|---|---|
@@ -1,15 +1,18 @@ | ||
--TEST-- | ||
Random: Engine: Mt19937: value | ||
Random: Engine: Mt19937: For a reference seed a fixed reference value must be generated | ||
--FILE-- | ||
<?php | ||
|
||
$engine = new \Random\Engine\Mt19937(1234); | ||
use Random\Engine\Mt19937; | ||
|
||
for ($i = 0; $i < 10000; $i++) { | ||
$engine = new Mt19937(1234); | ||
|
||
for ($i = 0; $i < 10_000; $i++) { | ||
$engine->generate(); | ||
} | ||
|
||
echo \bin2hex($engine->generate()); | ||
var_dump(bin2hex($engine->generate())); | ||
|
||
?> | ||
--EXPECT-- | ||
60fe95d9 | ||
string(8) "60fe95d9" |
15 changes: 10 additions & 5 deletions
15
ext/random/tests/02_engine/pcgoneseq128xslrr64_jump_error.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
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
6 changes: 4 additions & 2 deletions
6
ext/random/tests/02_engine/pcgoneseq128xslrr64_serialize.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
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 |
---|---|---|
@@ -1,17 +1,20 @@ | ||
--TEST-- | ||
Random: Engine: PcgOneseq128XslRr64: value | ||
Random: Engine: PcgOneseq128XslRr64: For a reference seed a fixed reference value must be generated | ||
--FILE-- | ||
<?php | ||
|
||
$engine = new \Random\Engine\PcgOneseq128XslRr64(1234); | ||
use Random\Engine\PcgOneseq128XslRr64; | ||
|
||
for ($i = 0; $i < 10000; $i++) { | ||
$engine = new PcgOneseq128XslRr64(1234); | ||
|
||
for ($i = 0; $i < 10_000; $i++) { | ||
$engine->generate(); | ||
} | ||
|
||
$engine->jump(1234567); | ||
|
||
echo \bin2hex($engine->generate()); | ||
var_dump(bin2hex($engine->generate())); | ||
|
||
?> | ||
--EXPECT-- | ||
b88e2a0f23720a1d | ||
string(16) "b88e2a0f23720a1d" |
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 |
---|---|---|
@@ -1,63 +1,44 @@ | ||
--TEST-- | ||
Random: Engine: User: compatibility | ||
Random: Engine: Native engines can be wrapped without changing their sequence | ||
--FILE-- | ||
<?php | ||
|
||
$native_engine = new \Random\Engine\Mt19937(1234); | ||
$user_engine = new class () implements \Random\Engine { | ||
public function __construct(private $engine = new \Random\Engine\Mt19937(1234)) | ||
{ | ||
} | ||
|
||
public function generate(): string | ||
{ | ||
return $this->engine->generate(); | ||
} | ||
}; | ||
|
||
for ($i = 0; $i < 1000; $i++) { | ||
if ($native_engine->generate() !== $user_engine->generate()) { | ||
die('failure Mt19937'); | ||
} | ||
} | ||
use Random\Engine; | ||
use Random\Engine\Mt19937; | ||
use Random\Engine\PcgOneseq128XslRr64; | ||
use Random\Engine\Xoshiro256StarStar; | ||
|
||
$native_engine = new \Random\Engine\PcgOneseq128XslRr64(1234); | ||
$user_engine = new class () implements \Random\Engine { | ||
public function __construct(private $engine = new \Random\Engine\PcgOneseq128XslRr64(1234)) | ||
class WrapperEngine implements Engine | ||
{ | ||
public function __construct(private readonly Engine $engine) | ||
{ | ||
} | ||
|
||
public function generate(): string | ||
{ | ||
return $this->engine->generate(); | ||
} | ||
}; | ||
|
||
for ($i = 0; $i < 1000; $i++) { | ||
if ($native_engine->generate() !== $user_engine->generate()) { | ||
die('failure PcgOneseq128XslRr64'); | ||
} | ||
} | ||
|
||
$native_engine = new \Random\Engine\Xoshiro256StarStar(1234); | ||
$user_engine = new class () implements \Random\Engine { | ||
public function __construct(private $engine = new \Random\Engine\Xoshiro256StarStar(1234)) | ||
{ | ||
} | ||
$engines = []; | ||
$engines[] = new Mt19937(1234); | ||
$engines[] = new PcgOneseq128XslRr64(1234); | ||
$engines[] = new Xoshiro256StarStar(1234); | ||
|
||
public function generate(): string | ||
{ | ||
return $this->engine->generate(); | ||
} | ||
}; | ||
foreach ($engines as $engine) { | ||
$native_engine = clone $engine; | ||
$user_engine = new WrapperEngine(clone $engine); | ||
|
||
for ($i = 0; $i < 1000; $i++) { | ||
if ($native_engine->generate() !== $user_engine->generate()) { | ||
die('failure Xoshiro256StarStar'); | ||
for ($i = 0; $i < 10_000; $i++) { | ||
if ($native_engine->generate() !== $user_engine->generate()) { | ||
$className = $engine::class; | ||
die("failure: {$className} at {$i}"); | ||
} | ||
} | ||
} | ||
|
||
die('success'); | ||
|
||
?> | ||
--EXPECT-- | ||
success |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was thinking of a form like the first PR, where the implemented class name is defined in a separate file and iterated over. e.g.)
engines.inc
.https://github.com/zeriyoshi/php-src/blob/ed42ae2c2fe30ad9e608b6e693cd3ef3c60f1d85/ext/random/tests/number_generator/common.inc
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I find this not as readable, because one needs to keep the contents of the include file in the head, instead of simply reading the test from top to bottom.