Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Make the roundtrip test of Double approximate.

  • Loading branch information...
commit 4da0fe4bbb96137b5b0be20a2ccdf5a9b68e489b 1 parent 7cf05cd
@bos authored
Showing with 17 additions and 6 deletions.
  1. +1 −1  tests/Makefile
  2. +16 −5 tests/Properties.hs
View
2  tests/Makefile
@@ -3,7 +3,7 @@ ghc := ghc
all: qc
qc: Properties.hs
- $(ghc) -o --make $@ $<
+ $(ghc) -Wall -o --make $@ $<
clean:
-rm -f qc *.o *.hi
View
21 tests/Properties.hs
@@ -17,12 +17,23 @@ encodeDouble num denom
where d = num / denom
encodeInteger i = encode (Number (I i)) == L.pack (show i)
-roundTrip :: (Eq a, FromJSON a, ToJSON a) => a -> Bool
-roundTrip i = (fmap fromJSON . L.maybeResult . L.parse value . encode . toJSON) i == Just (Success i)
+roundTrip :: (FromJSON a, ToJSON a) => (a -> a -> Bool) -> a -> Bool
+roundTrip eq i =
+ case fmap fromJSON . L.parse value . encode . toJSON $ i of
+ L.Done _ (Success v) -> v `eq` i
+ _ -> False
-roundTripBool (v::Bool) = roundTrip v
-roundTripDouble (v::Double) = roundTrip v
-roundTripInteger (v::Integer) = roundTrip v
+roundTripBool (v::Bool) = roundTrip (==) v
+roundTripDouble (v::Double) = roundTrip approxEq v
+roundTripInteger (v::Integer) = roundTrip (==) v
+
+approxEq :: Double -> Double -> Bool
+approxEq a b = a == b ||
+ d < maxAbsoluteError ||
+ d / max (abs b) (abs a) <= maxRelativeError
+ where d = abs (a - b)
+ maxAbsoluteError = 1e-15
+ maxRelativeError = 1e-15
main = defaultMain tests
Please sign in to comment.
Something went wrong with that request. Please try again.