summaryrefslogtreecommitdiffstats
path: root/tests/full/StatTest.php
blob: e78eb14633a7bd163dcac614b5bb95bdc22019a1 (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
<?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);
    }
}