Testing haskell equality on Doubles with HUnit?
Does HUnit have some way of doing approximate equalities? Clearly, this fails:
test1 = TestCase (assertEqual "Should equal" (get_scores) [(0.3, 0.3), (0.6, 0.36), (1.0, 0.3399999)])
For floating point approximate comparison, there's a library with some nice utils:
http://hackage.haskell.org/package/ieee754
Note: I don't know if there's a correct/official/accepted way to do this.
Here's the source code for assertEqual
:
assertEqual :: (Eq a, Show a) => String -- ^ The message prefix
-> a -- ^ The expected value
-> a -- ^ The actual value
-> Assertion
assertEqual preface expected actual =
unless (actual == expected) (assertFailure msg)
where msg = (if null preface then "" else preface ++ "\n") ++
"expected: " ++ show expected ++ "\n but got: " ++ show actual
Based on that and on JUnit's function for testing double equality, we could create our own in the same style:
import Control.Monad (unless)
assertEquals :: String -- ^ The message prefix
-> Double -- ^ The maximum difference between expected and actual
-> Double -- ^ The expected value
-> Double -- ^ The actual value
-> Assertion
assertEquals preface delta expected actual =
unless (abs (expected - actual) < delta) (assertFailure msg)
where msg = ... same as above ...
Since this question was first asked, the HUnit-approx
package has been released.
It uses an implicit variable, epsilon
, and an approximate equality operator, ~?~
, instead of ~=?
.
A short example:
tweak :: Float -> Float
tweak x = x * 1.0001
egApproxTest = let ?epsilon = 0.01 in
[ "strictEqual" ~: 0 ~=? tweak 0,
"approxEqual" ~: 2 ~?~ tweak 2 ]