File: RandomIntTest.php

package info (click to toggle)
php-random-compat 2.0.21-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, sid, trixie
  • size: 368 kB
  • sloc: php: 1,104; sh: 48; xml: 30; makefile: 4
file content (82 lines) | stat: -rw-r--r-- 3,029 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
<?php
class RandomIntTest extends PHPUnit\Framework\TestCase
{
    const NO_BASEDIR = 'There is no suitable CSPRNG installed on your system';
    
    public function testFuncExists()
    {
        $this->assertTrue(function_exists('random_int'));
    }
    
    public function testOutput()
    {
        try {
            $half_neg_max = (~PHP_INT_MAX / 2);

            $integers = array(
                random_int(0, 1000),
                random_int(1001,2000),
                random_int(-100, -10),
                random_int(-1000, 1000),
                random_int(~PHP_INT_MAX, PHP_INT_MAX),
                random_int("0", "1"),
                random_int(0.11111, 0.99999),
                random_int($half_neg_max, PHP_INT_MAX),
                random_int(0.0, 255.0),
                random_int(-4.5, -4.5),
                random_int("1337e3","1337e3")
            );

            $this->assertFalse($integers[0] === $integers[1]);
            $this->assertTrue($integers[0] >= 0 && $integers[0] <= 1000);
            $this->assertTrue($integers[1] >= 1001 && $integers[1] <= 2000);
            $this->assertTrue($integers[2] >= -100 && $integers[2] <= -10);
            $this->assertTrue($integers[3] >= -1000 && $integers[3] <= 1000);
            $this->assertTrue($integers[4] >= ~PHP_INT_MAX && $integers[4] <= PHP_INT_MAX);
            $this->assertTrue($integers[5] >= 0 && $integers[5] <= 1);
            $this->assertTrue($integers[6] === 0);
            $this->assertTrue($integers[7] >= $half_neg_max && $integers[7] <= PHP_INT_MAX);
            $this->assertTrue($integers[8] >= 0 && $integers[8] <= 255);
            $this->assertTrue($integers[9] === -4);
            $this->assertTrue($integers[10] === 1337000);
        } catch (Exception $ex) {
            $this->assertEquals(
                $ex->getMessage(),
                self::NO_BASEDIR
            );
            return;
        }
        
        try {
            $h = random_int("2147483648", "2147483647");
            $i = random_int("9223372036854775808", "9223372036854775807");
            $this->assertFalse(is_int($i));
            $h = random_int("-2147483648", "2147483647");
            $i = random_int("-9223372036854775808", "9223372036854775807");
            $this->fail("One of these options should have thrown an exception.");
        } catch (Error $ex) {
            $this->assertTrue($ex instanceof Error);
        } catch (Exception $ex) {
            $this->assertTrue($ex instanceof Exception);
        }
    }

    public function testRandomRange()
    {
        try {
            $try = 64;
            $maxLen = strlen(~PHP_INT_MAX);
            do {
                $rand = random_int(~PHP_INT_MAX, PHP_INT_MAX);
            } while (strlen($rand) !== $maxLen && $try--);

            $this->assertGreaterThan(0, $try);
        } catch (Exception $ex) {
            $this->assertEquals(
                $ex->getMessage(),
                self::NO_BASEDIR
            );
            return;
        }
    }
}