Home » Testing » PHPUnit2 » Bug #8509
Bug in assertEquals(Float $expected, Float $actual, Float $delta = 0)
Details
| Submitted | 2006-08-18 20:47 UTC |
|---|---|
| From | mstaniszczak at webframework dot org |
| Assigned | sebastian |
| Status | Closed |
| Package | PHPUnit2 |
| PHP Version | 5.1.4 |
| OS | Windows XP, Linux Slackware 10 |
| Roadmaps | (Not assigned) |
Comments
[2006-08-18 20:47 UTC] mstaniszczak at webframework dot org
Description:
------------
Bug in asserEuals for floats. In Assert.php file we are have method:
public static function assertEquals($expected, $actual, $message = '', $delta = 0) {
self::doAssertEquals($expected, $actual, $delta, TRUE, $message);
}
so wen we want to set delat, we MUST first set message.
But you can make same simple correction in doAssertEquals method. Change (line 225-227):
else if (is_float($expected) && is_float($actual) && is_float($delta)) {
$equal = (abs($expected - $actual) <= $delta);
}
to (for exampel - it isn't best solution):
else if (is_float($expected) && is_float($actual) && (is_float($delta) || (is_float($message) && $delta==0))) {
if (is_float($message) && $delta==0) {
$delta = $message;
}
$equal = (abs($expected - $actual) <= $delta);
}
Test script:
---------------
class Test extends PHPUnit2_Framework_TestCase{
public function testOne() {
$f1 = 122.2;
$f2 = 124.2;
$this->assertEquals($f1, $f2, 2.0);
}
}
Expected result:
----------------
OK (1 test)
Actual result:
--------------
FAILURES!!!
EBut this code work ok:
class Test extends PHPUnit2_Framework_TestCase{
public function testOne() {
$f1 = 122.2;
$f2 = 124.2;
$this->assertEquals($f1, $f2, '', 2.0);
}
}
But here http://www.phpunit.de/pocket_guide/3.0/en/api.html you're write that first example shoud work fine.
Sorry for my english.
Like I see you're have this bau in 3.0.0 beat version too, but you must make other solution for it.
[2006-09-05 13:47 UTC] sebastian at php dot net
Moved to http://www.phpunit.de/ticket/44.