summaryrefslogtreecommitdiffstats
path: root/tests/unit/UtilityTest.php
blob: 294a8019db9f5e0a299f141258fb4655f6c07615 (plain)
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
83
84
85
86
87
88
89
90
91
92
93
94
95
<?php
class UtilityTest extends PHPUnit_Framework_TestCase
{
    public function testStrlen()
    {
        if (!function_exists('RandomCompat_strlen')) {
            return $this->markTestSkipped(
                'We don\' need to test this in PHP 7.'
            );
        }
        $this->assertEquals(RandomCompat_strlen("\xF0\x9D\x92\xB3"), 4);
    }
    
    public function testIntval()
    {
        if (!function_exists('RandomCompat_intval')) {
            return $this->markTestSkipped(
                'We don\' need to test this in PHP 7.'
            );
        }
        // Equals
        $this->assertEquals(
            abs(RandomCompat_intval(-4.5)),
            abs(RandomCompat_intval(4.5))
        );
        
        // True
        $this->assertTrue(
            is_int(RandomCompat_intval(PHP_INT_MAX, true))
        );
        $this->assertTrue(
            is_int(RandomCompat_intval(~PHP_INT_MAX, true))
        );
        $this->assertTrue(
            is_int(RandomCompat_intval(~PHP_INT_MAX + 1, true))
        );
        $this->assertTrue(
            is_int(RandomCompat_intval("1337e3", true))
        );
        $this->assertTrue(
            is_int(RandomCompat_intval("1.", true))
        );
        
        // False
        $this->assertFalse(
            is_int(RandomCompat_intval((float) PHP_INT_MAX, true))
        );
        $this->assertFalse(
            is_int(RandomCompat_intval((float) ~PHP_INT_MAX, true))
        );
        $this->assertFalse(
            is_int(RandomCompat_intval(PHP_INT_MAX + 1, true))
        );
        $this->assertFalse(
            is_int(RandomCompat_intval(~PHP_INT_MAX - 1, true))
        );
        $this->assertFalse(
            is_int(RandomCompat_intval(~PHP_INT_MAX - 0.1, true))
        );
        $this->assertFalse(
            is_int(RandomCompat_intval(PHP_INT_MAX + 0.1, true))
        );
        $this->assertFalse(
            is_int(RandomCompat_intval("hello", true))
        );
        
        if (PHP_INT_SIZE === 8) {
            $this->assertFalse(
                is_int(RandomCompat_intval("-9223372036854775809", true))
            );
            $this->assertTrue(
                is_int(RandomCompat_intval("-9223372036854775808", true))
            );
            $this->assertFalse(
                is_int(RandomCompat_intval("9223372036854775808", true))
            );
            $this->assertTrue(
                is_int(RandomCompat_intval("9223372036854775807", true))
            );
        } else {
            $this->assertFalse(
                is_int(RandomCompat_intval("2147483648", true))
            );
            $this->assertTrue(
                is_int(RandomCompat_intval("2147483647", true))
            );
            $this->assertFalse(
                is_int(RandomCompat_intval("-2147483649", true))
            );
            $this->assertTrue(
                is_int(RandomCompat_intval("-2147483648", true))
            );
        }
    }
}