This week's book giveaways are in the Refactoring and Agile forums.We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!

# assertEquals(double expected,double actual,double delta)

miguel lisboa
Ranch Hand
Posts: 1281
inside my jUnit test, i've this:

and tests ok; but if i change param's order, goes ok again:

I just dont get it...
Can someone pls explain me why this looks like beeing the same?

Ilja Preuss
author
Sheriff
Posts: 14112
Do you understand what the third parameter is doing?

miguel lisboa
Ranch Hand
Posts: 1281
i guess is suposed to control precision - decimal roundups, and similar
[ January 21, 2005: Message edited by: miguel lisboa ]

Lasse Koskela
author
Sheriff
Posts: 11962
5
Yes.

miguel lisboa
Ranch Hand
Posts: 1281
why on earth is it possible that i interchange expected, obtained with DELTA first or at the end and obtain the same result?

Ilja Preuss
author
Sheriff
Posts: 14112
assertEquals proofs that

abs(expected - actual) <= delta

abs(expected - actual) <= 0

it follows that

expected == actual

from that follows that

expected <= actual

and therefore

abs(expected) <= actual

therefore

abs(-expected) <= actual

therefore

abs(0 - expected) <= actual

which is exactly what your latter line of code is asserting. From that follows that when the first line succeeds, the second have to, too.

Notice that the reverse is *not* true - even when the first is failing, the second still can succeed! (Bonus points if you can explain why that is the case, using the above steps... )

miguel lisboa
Ranch Hand
Posts: 1281
Notice that the reverse is *not* true - even when the first is failing, the second still can succeed!

whenever expected < actual, with delta = 0
As to the bonus, i think you deserve it for your great explanation
thanks a lot

Ilja Preuss
author
Sheriff
Posts: 14112
Originally posted by miguel lisboa:
As to the bonus, i think you deserve it for your great explanation

I guess my half-finished mathematics study shows through...

thanks a lot

You're very welcome!