xref: /PHP-7.3/ext/gmp/tests/gmp_random_range.phpt (revision 1acf55eb)
1--TEST--
2gmp_random_range() basic tests
3--SKIPIF--
4<?php if (!extension_loaded("gmp")) print "skip"; ?>
5--FILE--
6<?php
7
8$minusTen = gmp_init(-10);
9$plusTen = gmp_init(10);
10$zero = gmp_init(0);
11
12var_dump(gmp_random_range());
13var_dump(gmp_random_range(10));
14var_dump(gmp_random_range(10, -10));
15
16var_dump(gmp_random_range($plusTen, $minusTen));
17var_dump(gmp_random_range($plusTen, $zero));
18
19// If these error the test fails.
20gmp_random_range(0, 10);
21gmp_random_range(1, 10);
22gmp_random_range(-1, 10);
23gmp_random_range(-10, 0);
24gmp_random_range(-10, -1);
25
26gmp_random_range(0, $plusTen);
27gmp_random_range(1, $plusTen);
28gmp_random_range(-1, $plusTen);
29
30gmp_random_range($zero, $plusTen);
31gmp_random_range($minusTen, $plusTen);
32
33// 0.5 seconds to make sure the numbers stay in range
34$start = microtime(true);
35while (1) {
36	for ($i = 0; $i < 5000; $i++) {
37		$result = gmp_random_range(0, 1000);
38		if ($result < 0 || $result > 1000) {
39			print "RANGE VIOLATION 1\n";
40			var_dump($result);
41			break 2;
42		}
43
44		$result = gmp_random_range(-1000, 0);
45		if ($result < -1000 || $result > 0) {
46			print "RANGE VIOLATION 2\n";
47			var_dump($result);
48			break 2;
49		}
50
51		$result = gmp_random_range(-500, 500);
52		if ($result < -500 || $result > 500) {
53			print "RANGE VIOLATION 3\n";
54			var_dump($result);
55			break 2;
56		}
57	}
58
59	if (microtime(true) - $start > 0.5) {
60		break;
61	}
62}
63
64echo "Done\n";
65?>
66--EXPECTF--
67Warning: gmp_random_range() expects exactly 2 parameters, 0 given in %s on line %d
68NULL
69
70Warning: gmp_random_range() expects exactly 2 parameters, 1 given in %s on line %d
71NULL
72
73Warning: gmp_random_range(): The minimum value must be less than the maximum value in %s on line %d
74bool(false)
75
76Warning: gmp_random_range(): The minimum value must be less than the maximum value in %s on line %d
77bool(false)
78
79Warning: gmp_random_range(): The minimum value must be less than the maximum value in %s on line %d
80bool(false)
81Done
82