You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. <?php
  2. namespace Faker\Test\Provider;
  3. use Faker\Provider\Biased;
  4. use Faker\Generator;
  5. class BiasedTest extends \PHPUnit_Framework_TestCase
  6. {
  7. const MAX = 10;
  8. const NUMBERS = 25000;
  9. protected $generator;
  10. protected $results = array();
  11. protected function setUp()
  12. {
  13. $this->generator = new Generator();
  14. $this->generator->addProvider(new Biased($this->generator));
  15. $this->results = array_fill(1, self::MAX, 0);
  16. }
  17. public function performFake($function)
  18. {
  19. for($i = 0; $i < self::NUMBERS; $i++) {
  20. $this->results[$this->generator->biasedNumberBetween(1, self::MAX, $function)]++;
  21. }
  22. }
  23. public function testUnbiased()
  24. {
  25. $this->performFake(array('\Faker\Provider\Biased', 'unbiased'));
  26. // assert that all numbers are near the expected unbiased value
  27. foreach ($this->results as $number => $amount) {
  28. // integral
  29. $assumed = (1 / self::MAX * $number) - (1 / self::MAX * ($number - 1));
  30. // calculate the fraction of the whole area
  31. $assumed /= 1;
  32. $this->assertGreaterThan(self::NUMBERS * $assumed * .95, $amount, "Value was more than 5 percent under the expected value");
  33. $this->assertLessThan(self::NUMBERS * $assumed * 1.05, $amount, "Value was more than 5 percent over the expected value");
  34. }
  35. }
  36. public function testLinearHigh()
  37. {
  38. $this->performFake(array('\Faker\Provider\Biased', 'linearHigh'));
  39. foreach ($this->results as $number => $amount) {
  40. // integral
  41. $assumed = 0.5 * pow(1 / self::MAX * $number, 2) - 0.5 * pow(1 / self::MAX * ($number - 1), 2);
  42. // calculate the fraction of the whole area
  43. $assumed /= pow(1, 2) * .5;
  44. $this->assertGreaterThan(self::NUMBERS * $assumed * .9, $amount, "Value was more than 10 percent under the expected value");
  45. $this->assertLessThan(self::NUMBERS * $assumed * 1.1, $amount, "Value was more than 10 percent over the expected value");
  46. }
  47. }
  48. public function testLinearLow()
  49. {
  50. $this->performFake(array('\Faker\Provider\Biased', 'linearLow'));
  51. foreach ($this->results as $number => $amount) {
  52. // integral
  53. $assumed = -0.5 * pow(1 / self::MAX * $number, 2) - -0.5 * pow(1 / self::MAX * ($number - 1), 2);
  54. // shift the graph up
  55. $assumed += 1 / self::MAX;
  56. // calculate the fraction of the whole area
  57. $assumed /= pow(1, 2) * .5;
  58. $this->assertGreaterThan(self::NUMBERS * $assumed * .9, $amount, "Value was more than 10 percent under the expected value");
  59. $this->assertLessThan(self::NUMBERS * $assumed * 1.1, $amount, "Value was more than 10 percent over the expected value");
  60. }
  61. }
  62. }