Commits

Bryan O'Sullivan committed e538fa3

Make the roundtrip test of Double approximate.

Comments (0)

Files changed (2)

 all: qc
 
 qc: Properties.hs
-	$(ghc) -o --make $@ $<
+	$(ghc) -Wall -o --make $@ $<
 
 clean:
 	-rm -f qc *.o *.hi

tests/Properties.hs

   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
 
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.