Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Floating point test assertion - why do these "identical" arrays fail?

Tags:

php

phpunit

I'm using assertSame() in PHPUnit to compare a database result with expected values. The results are floating point numbers.

PHPUnit returns this message (but I can't spot any differences):

Failed asserting that Array (
    '1_1' => 11.111111111111
    '1_2' => 33.333333333333
    '1_3' => 55.555555555556
    '1_4' => 0.0
    '1_5' => null
    '1_total' => 100.0
) is identical to Array (
    '1_1' => 11.111111111111
    '1_2' => 33.333333333333
    '1_3' => 55.555555555556
    '1_4' => 0.0
    '1_5' => null
    '1_total' => 100.0
)

Why is this failing and what is the correct way to compare an arrays of floating point values?

like image 751
mtmacdonald Avatar asked May 19 '14 10:05

mtmacdonald


1 Answers

assertEquals has a $floating_delta argument for this type of cases:

$this->assertEquals($expected_array, $actual_array, '', 0.00001);

PHPUnit docs

like image 141
gontrollez Avatar answered Sep 27 '22 21:09

gontrollez