1--TEST--
2Random: Randomizer: getBytesFromString(): 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$iterations = getenv("SKIP_SLOW_TESTS") ? 10 : 250;
24
25foreach ($engines as $engine) {
26    echo $engine::class, PHP_EOL;
27
28    $randomizer = new Randomizer($engine);
29    var_dump($randomizer->getBytesFromString('a', 10));
30    var_dump($randomizer->getBytesFromString(str_repeat('a', 256), 5));
31
32    for ($i = 1; $i < $iterations; $i++) {
33        $output = $randomizer->getBytesFromString(str_repeat('ab', $i), 500);
34
35        // This check can theoretically fail with a chance of 0.5**500.
36        if (!str_contains($output, 'a') || !str_contains($output, 'b')) {
37            die("failure: didn't see both a and b at {$i}");
38        }
39    }
40}
41
42die('success');
43
44?>
45--EXPECTF--
46Deprecated: Constant MT_RAND_PHP is deprecated in %s on line %d
47
48Deprecated: The MT_RAND_PHP variant of Mt19937 is deprecated in %s on line %d
49Random\Engine\Mt19937
50string(10) "aaaaaaaaaa"
51string(5) "aaaaa"
52Random\Engine\Mt19937
53string(10) "aaaaaaaaaa"
54string(5) "aaaaa"
55Random\Engine\PcgOneseq128XslRr64
56string(10) "aaaaaaaaaa"
57string(5) "aaaaa"
58Random\Engine\Xoshiro256StarStar
59string(10) "aaaaaaaaaa"
60string(5) "aaaaa"
61Random\Engine\Secure
62string(10) "aaaaaaaaaa"
63string(5) "aaaaa"
64Random\Engine\Test\TestShaEngine
65string(10) "aaaaaaaaaa"
66string(5) "aaaaa"
67success
68