Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Binary instances for NaN and negative zero are broken #46

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion binary.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,9 @@ test-suite qc
random>=1.0.1.0,
test-framework,
test-framework-quickcheck2 >= 0.3,
QuickCheck>=2.5
test-framework-hunit,
QuickCheck>=2.5,
HUnit

ghc-options: -Wall -O2
hs-source-dirs: src
Expand Down
25 changes: 24 additions & 1 deletion tests/QC.hs
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,12 @@ import Control.Exception as C (catch,evaluate,SomeException)
import System.IO.Unsafe

import Test.QuickCheck
import Test.HUnit ((@=?),Assertion,assertFailure)
-- import Text.Printf

import Test.Framework
import Test.Framework.Providers.QuickCheck2
import Test.Framework.Providers.HUnit
-- import Data.Monoid

import Action (prop_action)
Expand Down Expand Up @@ -381,6 +383,18 @@ test a = forAll positiveList (roundTrip a . refragment)
positiveList :: Gen [Int]
positiveList = fmap (filter (/=0) . map abs) $ arbitrary

-- Test binary instance for special values which aren't covered by QC
testBinary :: (Eq a, Binary a, Show a) => a -> Assertion
testBinary a = a @=? (decode . encode) a

-- We need to treat negative zero specially since equality doesn't
-- describe them properly
testNegZero :: (Binary a, RealFloat a, Show a) => a -> Assertion
testNegZero a =
case ((decode . encode) ((-0) `asTypeOf` a)) `asTypeOf` a of
z | isNegativeZero z -> return ()
| otherwise -> assertFailure $ "Got " ++ show z ++ " instead of -0.0"

tests :: [Test]
tests =
[ testGroup "Utils"
Expand Down Expand Up @@ -499,7 +513,16 @@ tests =
, ("B.ByteString", p (test :: T B.ByteString ))
, ("L.ByteString", p (test :: T L.ByteString ))
]

, testGroup "Binary special values"
[ testCase "NaN::Float" $ testBinary ( 0/0 :: Float)
, testCase "+Inf::Float" $ testBinary ( 1/0 :: Float)
, testCase "-Inf::Float" $ testBinary (-1/0 :: Float)
, testCase "-0 ::Float" $ testNegZero (-0 :: Float)
, testCase "NaN::Double" $ testBinary ( 0/0 :: Double)
, testCase "+Inf::Double" $ testBinary ( 1/0 :: Double)
, testCase "-Inf::Double" $ testBinary (-1/0 :: Double)
, testCase "-0 ::Double" $ testNegZero (-0 :: Double)
]
, testGroup "Invariants" $ map (uncurry testProperty)
[ ("B.ByteString invariant", p (prop_invariant :: B B.ByteString ))
, ("[B.ByteString] invariant", p (prop_invariant :: B [B.ByteString] ))
Expand Down