File: StatTest.php

package info (click to toggle)
php-random-compat 2.0.21-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 368 kB
  • sloc: php: 1,104; sh: 48; xml: 30; makefile: 4
file content (52 lines) | stat: -rw-r--r-- 1,413 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
<?php

class StatTest extends PHPUnit\Framework\TestCase
{
    /**
     * All possible values should be > 30% but less than 170%
     * 
     * This also catches 0 and 1000
     */
    public function testDistribution()
    {
        $integers = array_fill(0, 100, 0);
        for ($i = 0; $i < 10000; ++$i) {
            ++$integers[random_int(0,99)];
        }
        for ($i = 0; $i < 100; ++$i) {
            $this->assertFalse($integers[$i] < 30);
            $this->assertFalse($integers[$i] > 170);
        }
    }
    
    /**
     * This should be between 55% and 75%, always
     */
    public function testCoverage()
    {
        $integers = array_fill(0, 2000, 0);
        for ($i = 0; $i < 2000; ++$i) {
            ++$integers[random_int(0,1999)];
        }
        $coverage = 0;
        for ($i = 0; $i < 2000; ++$i) {
            if ($integers[$i] > 0) {
                ++$coverage;
            }
        }
        $this->assertTrue($coverage >= 1150);
        $this->assertTrue($coverage <= 1350);
    }
    
    public function testCompressionRatios()
    {
        $some_bytes = random_bytes(65536);
        $compressed = gzcompress($some_bytes, 9);
        if (function_exists('mb_strlen')) {
            $length = mb_strlen($compressed, '8bit');
        } else {
            $length = strlen($compressed);
        }
        $this->assertTrue($length >= 65000 && $length <= 67000);
    }
}