Skip to content

Commit

Permalink
Merge pull request #505 from finanzcheck/master
Browse files Browse the repository at this point in the history
Fix numeric comparison for negative numbers.
  • Loading branch information
sagikazarmark committed Dec 5, 2018
2 parents 5a7dc2c + 025043c commit dbaa351
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 17 deletions.
10 changes: 5 additions & 5 deletions src/Money.php
Expand Up @@ -155,7 +155,7 @@ public function compare(Money $other)
*/
public function greaterThan(Money $other)
{
return $this->compare($other) === 1;
return $this->compare($other) > 0;
}

/**
Expand All @@ -177,7 +177,7 @@ public function greaterThanOrEqual(Money $other)
*/
public function lessThan(Money $other)
{
return $this->compare($other) === -1;
return $this->compare($other) < 0;
}

/**
Expand Down Expand Up @@ -387,7 +387,7 @@ public function allocate(array $ratios)
$remainder = $this->getCalculator()->subtract($remainder, $share);
}

for ($i = 0; $this->getCalculator()->compare($remainder, 0) === 1; ++$i) {
for ($i = 0; $this->getCalculator()->compare($remainder, 0) > 0; ++$i) {
if (!$ratios[$i]) {
continue;
}
Expand Down Expand Up @@ -489,7 +489,7 @@ public function isZero()
*/
public function isPositive()
{
return $this->getCalculator()->compare($this->amount, 0) === 1;
return $this->getCalculator()->compare($this->amount, 0) > 0;
}

/**
Expand All @@ -499,7 +499,7 @@ public function isPositive()
*/
public function isNegative()
{
return $this->getCalculator()->compare($this->amount, 0) === -1;
return $this->getCalculator()->compare($this->amount, 0) < 0;
}

/**
Expand Down
44 changes: 32 additions & 12 deletions tests/Calculator/CalculatorTestCase.php
Expand Up @@ -107,12 +107,26 @@ public function it_rounds_a_value($value, $mode, $expected)
}

/**
* @dataProvider compareExamples
* @dataProvider compareLessExamples
* @test
*/
public function it_compares_values($left, $right, $expected)
public function it_compares_values_less($left, $right)
{
$this->assertEquals($expected, $this->getCalculator()->compare($left, $right));
// Compare with both orders. One must return a value less than zero,
// the other must return a value greater than zero.
$this->assertLessThan(0, $this->getCalculator()->compare($left, $right));
$this->assertGreaterThan(0, $this->getCalculator()->compare($right, $left));
}

/**
* @dataProvider compareEqualExamples
* @test
*/
public function it_compares_values($left, $right)
{
// Compare with both orders, both must return zero.
$this->assertEquals(0, $this->getCalculator()->compare($left, $right));
$this->assertEquals(0, $this->getCalculator()->compare($right, $left));
}

/**
Expand Down Expand Up @@ -218,17 +232,23 @@ public function shareExamples()
];
}

public function compareExamples()
public function compareLessExamples()
{
return [
[0, 1],
['0', '1'],
['0.0005', '1'],
['0.000000000000000000000000005', '1'],
['-1000', '1000', -1],
];
}

public function compareEqualExamples()
{
return [
[1, 0, 1],
[1, 1, 0],
[0, 1, -1],
['1', '0', 1],
['1', '1', 0],
['0', '1', -1],
['1', '0.0005', 1],
['1', '0.000000000000000000000000005', 1],
[1, 1],
['1', '1'],
['-1000', '-1000'],
];
}

Expand Down

0 comments on commit dbaa351

Please sign in to comment.