Skip to content

Commit

Permalink
Merge pull request #7 from swagger-mock/6-random-float
Browse files Browse the repository at this point in the history
#6 bugfix: random float generation improved
  • Loading branch information
strider2038 authored Mar 29, 2019
2 parents 1d9347a + ffbbaa1 commit 10a50dc
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 7 deletions.
11 changes: 7 additions & 4 deletions src/Mock/Generation/Value/Primitive/RandomNumberGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ public function generateValue(TypeInterface $type): ?float

private function generateFloatValue(NumberType $type): float
{
$minimum = $type->minimum ?? 0;
$maximum = $type->maximum ?? mt_getrandmax();
$minimum = (float) ($type->minimum ?? -mt_getrandmax() / 2);
$maximum = (float) ($type->maximum ?? mt_getrandmax() / 2);
$range = $maximum - $minimum;

$uniformValue = $this->uniformRandomValue($type->exclusiveMinimum, $type->exclusiveMaximum);
Expand All @@ -47,11 +47,14 @@ private function generateFloatValue(NumberType $type): float
return $value;
}

private function uniformRandomValue(bool $exclusiveMinimum, bool $exclusiveMaximum): float
private function uniformRandomValue(bool $exclusiveMinimum = false, bool $exclusiveMaximum = false): float
{
$minimum = $exclusiveMinimum ? 1 : 0;
$maximum = mt_getrandmax() - ($exclusiveMaximum ? 1 : 0);

return (float) random_int($minimum, $maximum) / mt_getrandmax();
$value1 = (float) random_int($minimum, $maximum) / mt_getrandmax();
$value2 = (float) random_int($minimum, $maximum) / mt_getrandmax();

return $value1 * $value2;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,6 @@ class AnyOfValueGeneratorTest extends TestCase
'property1' => 'value1',
'property2' => 'value2',
];
private const COMMON_PROPERTY_PAIR = [
'commonProperty' => 'commonPropertyValue',
];

protected function setUp(): void
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,21 @@ class RandomNumberGeneratorTest extends TestCase
private const MULTIPLE_OF = 2.5;
private const VALUE_DEVIATION = 0.001;

/** @test */
public function generateValue_numberType_randomNumberValueReturned(): void
{
$generator = $this->createRandomNumberGenerator();
$type = new NumberType();

$test = function () use ($generator, $type) {
$value = $generator->generateValue($type);

return abs($value) - floor(abs($value));
};

$this->expectClosureOccasionallyReturnsValueGreaterThan($test, self::VALUE_DEVIATION);
}

/** @test */
public function generateValue_numberTypeWithLimits_limitedNumberValueReturned(): void
{
Expand Down
13 changes: 13 additions & 0 deletions tests/Utility/TestCase/ProbabilityTestCaseTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,17 @@ protected function expectClosureOccasionallyReturnsNull(\Closure $test): void

Assert::assertNull($value);
}

protected function expectClosureOccasionallyReturnsValueGreaterThan(\Closure $test, $expectedValue): void
{
for ($i = 0; $i < 100; $i++) {
$value = $test();

if ($value > $expectedValue) {
break;
}
}

Assert::assertGreaterThan($expectedValue, $value);
}
}

0 comments on commit 10a50dc

Please sign in to comment.