on master help sign in
Make the roundtrip test of Double approximate.
commit942b7271a878c4b66995072cf42073b4b86867c0 authorBryan O'Sullivan <bos@mailrank.com> dateWed 13 Apr 2011, 10:43 (16 years ago) parentb7bf95a
2 files changed+17−6
tests/Makefile+1−1 tests/Properties.hs+16−5
tests/Makefile+1−1
@@ -3,7 +3,7 @@ ghc := ghc
33all: qc
44
55qc: Properties.hs
6− $(ghc) -o --make $@ $<
6+ $(ghc) -Wall -o --make $@ $<
77
88clean:
99 -rm -f qc *.o *.hi
tests/Properties.hs+16−5
@@ -17,12 +17,23 @@ encodeDouble num denom
1717 where d = num / denom
1818encodeInteger i = encode (Number (I i)) == L.pack (show i)
1919
20−roundTrip :: (Eq a, FromJSON a, ToJSON a) => a -> Bool
21−roundTrip i = (fmap fromJSON . L.maybeResult . L.parse value . encode . toJSON) i == Just (Success i)
20+roundTrip :: (FromJSON a, ToJSON a) => (a -> a -> Bool) -> a -> Bool
21+roundTrip eq i =
22+ case fmap fromJSON . L.parse value . encode . toJSON $ i of
23+ L.Done _ (Success v) -> v `eq` i
24+ _ -> False
2225
23−roundTripBool (v::Bool) = roundTrip v
24−roundTripDouble (v::Double) = roundTrip v
25−roundTripInteger (v::Integer) = roundTrip v
26+roundTripBool (v::Bool) = roundTrip (==) v
27+roundTripDouble (v::Double) = roundTrip approxEq v
28+roundTripInteger (v::Integer) = roundTrip (==) v
29+
30+approxEq :: Double -> Double -> Bool
31+approxEq a b = a == b ||
32+ d < maxAbsoluteError ||
33+ d / max (abs b) (abs a) <= maxRelativeError
34+ where d = abs (a - b)
35+ maxAbsoluteError = 1e-15
36+ maxRelativeError = 1e-15
2637
2738main = defaultMain tests
2839