xref: /PHP-8.2/ext/gmp/tests/gmp_random_bits.phpt (revision 9ca68e03)
1--TEST--
2gmp_random_bits() basic tests
3--EXTENSIONS--
4gmp
5--FILE--
6<?php
7
8try {
9    var_dump(gmp_random_bits(0));
10} catch (\ValueError $e) {
11    echo $e->getMessage() . \PHP_EOL;
12}
13try {
14    var_dump(gmp_random_bits(-1));
15} catch (\ValueError $e) {
16    echo $e->getMessage() . \PHP_EOL;
17}
18
19// If these error the test fails.
20gmp_random_bits(1);
21gmp_random_bits(1024);
22
23// 0.5 seconds to make sure the numbers stay in range
24$start = microtime(true);
25$limit = (2 ** 30) - 1;
26while (1) {
27    for ($i = 0; $i < 5000; $i++) {
28        $result = gmp_random_bits(30);
29        if ($result < 0 || $result > $limit) {
30            print "RANGE VIOLATION\n";
31            var_dump($result);
32            break 2;
33        }
34    }
35
36    if (microtime(true) - $start > 0.5) {
37        break;
38    }
39}
40
41echo "Done\n";
42?>
43--EXPECTF--
44gmp_random_bits(): Argument #1 ($bits) must be between 1 and %d
45gmp_random_bits(): Argument #1 ($bits) must be between 1 and %d
46Done
47