/** * Test that the generator only returns results in the specified range. * * @covers \DrupalReleaseDate\NumberGenerator\QuadraticWeighted<extended> */ function testRange() { $min = 2; $max = 15; $probabilitySum = $this->calculateProbabilitySum($min, $max); $generator = new QuadraticWeighted($this->weightGeneratorStub, $min, $max); $this->weightGeneratorStub->method('generate')->will($this->onConsecutiveCalls(-1, 0, 1, $probabilitySum / 2, $probabilitySum - 1, $probabilitySum, $probabilitySum + 1)); for ($i = 0; $i < 7; $i++) { $rand = $generator->generate(); $this->assertGreaterThanOrEqual($min, $rand); $this->assertLessThanOrEqual($max, $rand); } }
/** * Check that the distribution of results from a generator is accurate * within its range. * * The generator under test must use a Cyclic generator for its weights. * * @param NumberGeneratorInterface $generator * @param int $min * @param int $max * @param int|float $slope * @param int|float $base * @param int|float $step */ protected function checkDistribution(NumberGeneratorInterface $generator, $min, $max, $slope = 1, $base = 1, $step = 1) { $probabilitySum = $this->calculateProbabilitySum($min, $max, $slope, $base); $results = array_fill($min, $max - $min + 1, 0); for ($i = 0; $i < $probabilitySum * 10 / $step; $i++) { $results[$generator->generate()]++; } foreach ($results as $value => $count) { $this->assertEquals(($slope * ($value - $min) + $base) * 10 / $step, $count); } }