diff --git a/src/Calculator.php b/src/Calculator.php index b5c6eff..d51ab95 100644 --- a/src/Calculator.php +++ b/src/Calculator.php @@ -38,7 +38,7 @@ public function statistics(array $values) { 'max' => 0 ); foreach ($values as $set) { - $value = $set['value']; + $value = (float) $set['value']; $data['sum'] += $value; if ($data['min'] === NULL || $value < $data['min']) { $data['min'] = $value; diff --git a/tests/Unit/CalculatorTest.php b/tests/Unit/CalculatorTest.php index acbfe3e..2952a35 100644 --- a/tests/Unit/CalculatorTest.php +++ b/tests/Unit/CalculatorTest.php @@ -1,5 +1,6 @@ markTestIncomplete('Not yet implemented'); + public function testIsSigned($value, $expected) { + $calculator = new Calculator(); + $this->assertEquals($expected, $calculator->isSigned($value)); + } + + /** + * @return array + */ + public function getIsSignedTestValues() { + return array( + array(1, FALSE), + array(-1, TRUE), + array(0.514, FALSE), + array(-0.132, TRUE) + ); + } + + /** + * @param float $value + * @param mixed $modification + * @param float $expected + * @dataProvider getModifyTestValues + */ + public function testModify($value, $modification, $expected) { + $calculator = new Calculator(); + $this->assertEquals($expected, $calculator->modify($value, $modification)); + } + + /** + * @return array + */ + public function getModifyTestValues() { + return array( + array(1, 2, 2), + array(1, '+1', 2), + array(2, -1, 1) + ); + } + + /** + * @param array $values + * @param array $expected + * @dataProvider getStatisticsTestValues + */ + public function testStatistics(array $values, array $expected) { + $calculator = new Calculator(); + $this->assertEquals($expected, $calculator->statistics($values)); + } + + /** + * @return array + */ + public function getStatisticsTestValues() { + return array( + array( + array( + array('value' => 1), + array('value' => 2), + array('value' => 3), + array('value' => 4), + array('value' => 5) + ), + array('min' => 1, 'max' => 5, 'sum' => 15, 'average' => 3, 'count' => 5) + ), + array( + array( + array('value' => 0.4), + array('value' => 1.2), + array('value' => 2.4), + array('value' => 3.8), + array('value' => 0.5) + ), + array('min' => 0.4, 'max' => 3.8, 'sum' => 8.3, 'average' => 1.66, 'count' => 5) + ), + + ); } }