File: BiasedTest.php

package info (click to toggle)
php-faker 1.20.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 12,412 kB
  • sloc: php: 115,671; xml: 213; makefile: 49
file content (79 lines) | stat: -rw-r--r-- 2,835 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
<?php

namespace Faker\Test\Provider;

use Faker\Provider\Biased;
use Faker\Test\TestCase;

/**
 * @group legacy
 */
final class BiasedTest extends TestCase
{
    public const MAX = 10;
    public const NUMBERS = 25000;
    protected $results = [];

    protected function setUp(): void
    {
        parent::setUp();
        $this->results = array_fill(1, self::MAX, 0);
    }

    public function performFake($function)
    {
        for ($i = 0; $i < self::NUMBERS; ++$i) {
            ++$this->results[$this->faker->biasedNumberBetween(1, self::MAX, $function)];
        }
    }

    public function testUnbiased()
    {
        $this->performFake(['\Faker\Provider\Biased', 'unbiased']);

        // assert that all numbers are near the expected unbiased value
        foreach ($this->results as $number => $amount) {
            // integral
            $assumed = (1 / self::MAX * $number) - (1 / self::MAX * ($number - 1));
            // calculate the fraction of the whole area
            $assumed /= 1;
            self::assertGreaterThan(self::NUMBERS * $assumed * .95, $amount, 'Value was more than 5 percent under the expected value');
            self::assertLessThan(self::NUMBERS * $assumed * 1.05, $amount, 'Value was more than 5 percent over the expected value');
        }
    }

    public function testLinearHigh()
    {
        $this->performFake(['\Faker\Provider\Biased', 'linearHigh']);

        foreach ($this->results as $number => $amount) {
            // integral
            $assumed = 0.5 * (1 / self::MAX * $number) ** 2 - 0.5 * (1 / self::MAX * ($number - 1)) ** 2;
            // calculate the fraction of the whole area
            $assumed /= 1 ** 2 * .5;
            self::assertGreaterThan(self::NUMBERS * $assumed * .9, $amount, 'Value was more than 10 percent under the expected value');
            self::assertLessThan(self::NUMBERS * $assumed * 1.1, $amount, 'Value was more than 10 percent over the expected value');
        }
    }

    public function testLinearLow()
    {
        $this->performFake(['\Faker\Provider\Biased', 'linearLow']);

        foreach ($this->results as $number => $amount) {
            // integral
            $assumed = -0.5 * (1 / self::MAX * $number) ** 2 - -0.5 * (1 / self::MAX * ($number - 1)) ** 2;
            // shift the graph up
            $assumed += 1 / self::MAX;
            // calculate the fraction of the whole area
            $assumed /= 1 ** 2 * .5;
            self::assertGreaterThan(self::NUMBERS * $assumed * .9, $amount, 'Value was more than 10 percent under the expected value');
            self::assertLessThan(self::NUMBERS * $assumed * 1.1, $amount, 'Value was more than 10 percent over the expected value');
        }
    }

    protected function getProviders(): iterable
    {
        yield new Biased($this->faker);
    }
}