1--TEST--
2Random: Randomizer: shuffleBytes(): Basic functionality
3--FILE--
4<?php
5
6use Random\Engine;
7use Random\Engine\Mt19937;
8use Random\Engine\PcgOneseq128XslRr64;
9use Random\Engine\Secure;
10use Random\Engine\Test\TestShaEngine;
11use Random\Engine\Xoshiro256StarStar;
12use Random\Randomizer;
13
14require __DIR__ . "/../../engines.inc";
15
16$engines = [];
17$engines[] = new Mt19937(null, MT_RAND_MT19937);
18$engines[] = new Mt19937(null, MT_RAND_PHP);
19$engines[] = new PcgOneseq128XslRr64();
20$engines[] = new Xoshiro256StarStar();
21$engines[] = new Secure();
22$engines[] = new TestShaEngine();
23
24function sort_bytes(string $bytes): string
25{
26    $bytes = str_split($bytes);
27    sort($bytes);
28    return implode('', $bytes);
29}
30
31foreach ($engines as $engine) {
32    echo $engine::class, PHP_EOL;
33
34    $randomizer = new Randomizer($engine);
35
36    // This test is slow, test all numbers smaller than 50 and then in steps of 677 (which is prime).
37    for ($i = 1; $i < 5_000; $i += ($i < 50 ? 1 : 677)) {
38        $bytes = sort_bytes(random_bytes($i));
39
40        $result = $randomizer->shuffleBytes($bytes);
41
42        $result = sort_bytes($result);
43
44        if ($result !== $bytes) {
45            die("failure: not a permutation at {$i}");
46        }
47    }
48}
49
50die('success');
51
52?>
53--EXPECT--
54Random\Engine\Mt19937
55Random\Engine\Mt19937
56Random\Engine\PcgOneseq128XslRr64
57Random\Engine\Xoshiro256StarStar
58Random\Engine\Secure
59Random\Engine\Test\TestShaEngine
60success
61