on master help sign in
Make the roundtrip test of Double approximate.
942b727Bryan O'Sullivan
tests/Makefile+1−1
@@ -3,7 +3,7 @@ ghc := ghc
3all: qc
3all: qc
4
4
5qc: Properties.hs
5qc: Properties.hs
6− $(ghc) -o --make $@ $<
6+ $(ghc) -Wall -o --make $@ $<
7
7
8clean:
8clean:
9 -rm -f qc *.o *.hi
9 -rm -f qc *.o *.hi
tests/Properties.hs+16−5
@@ -17,12 +17,23 @@ encodeDouble num denom
17 where d = num / denom
17 where d = num / denom
18encodeInteger i = encode (Number (I i)) == L.pack (show i)
18encodeInteger i = encode (Number (I i)) == L.pack (show i)
19
19
20−roundTrip :: (Eq a, FromJSON a, ToJSON a) => a -> Bool
20+roundTrip :: (FromJSON a, ToJSON a) => (a -> a -> Bool) -> a -> Bool
21+roundTrip eq i =
21−roundTrip i = (fmap fromJSON . L.maybeResult . L.parse value . encode . toJSON) i == Just (Success i)
22+ case fmap fromJSON . L.parse value . encode . toJSON $ i of
23+ L.Done _ (Success v) -> v `eq` i
24+ _ -> False
22
25
23−roundTripBool (v::Bool) = roundTrip v
26+roundTripBool (v::Bool) = roundTrip (==) v
24−roundTripDouble (v::Double) = roundTrip v
27+roundTripDouble (v::Double) = roundTrip approxEq v
25−roundTripInteger (v::Integer) = roundTrip 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
26
37
27main = defaultMain tests
38main = defaultMain tests
28
39