Skip to content

Commit a7f58db

Browse files
committed
Improve QuickCheck coverage of Data.Binary.Put and Data.Binary.Class
- Add Put test group covering PutM Functor/Applicative/Monad instances, runPutM, execPut, flush, and lazy infinite streaming Put (right-recursive mappend and mapM_ on infinite lists). - Add refragmented roundtrip tests for Set, Map, IntSet, IntMap, Seq, Tree, Complex, Identity, NonEmpty, and Proxy. - Add prop_ratio_zero_denom verifying Binary (Ratio a) fails on zero denominator. HPC coverage (GHC 9.10.3, 198/198 QC properties passing): - Data.Binary.Put: 74% -> 98% expressions (184/187), 77% -> 89% decls - Data.Binary.Class: 62% -> 70% expressions (1018/1449), 57% -> 69% decls Assisted-By: Antigravity (Gemini 4)
1 parent edd1a11 commit a7f58db

1 file changed

Lines changed: 70 additions & 0 deletions

File tree

‎tests/QC.hs‎

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,18 @@ import qualified Data.ByteString.Internal as BI
1919
import qualified Data.ByteString.Lazy as L
2020
import qualified Data.ByteString.Lazy.Internal as L
2121
import Data.ByteString.Short (ShortByteString)
22+
import Data.Complex (Complex(..))
23+
import Data.Functor.Identity (Identity(..))
2224
import Data.Int
25+
import qualified Data.IntMap as IntMap
26+
import qualified Data.IntSet as IntSet
2327
import Data.IORef (newIORef, readIORef, writeIORef)
28+
import Data.List.NonEmpty (NonEmpty(..))
29+
import qualified Data.Map as Map
2430
import Data.Ratio
2531
import qualified Data.Sequence as Seq
32+
import qualified Data.Set as Set
33+
import qualified Data.Tree as Tree
2634
import Data.Typeable
2735
import Data.Word
2836
import Foreign (Ptr, castPtr, free, mallocBytes, peek, peekByteOff, poke)
@@ -53,6 +61,7 @@ import Test.QuickCheck hiding (total)
5361
import qualified Action (tests)
5462
import Arbitrary ()
5563
import Data.Binary
64+
import qualified Data.Binary.Builder as Builder
5665
import Data.Binary.Get
5766
import qualified Data.Binary.Get.Internal as GetI
5867
import Data.Binary.Get.Internal.Buf
@@ -889,6 +898,50 @@ prop_GenericSmall_invalidTag w =
889898
Left _ -> True
890899
Right _ -> False
891900

901+
------------------------------------------------------------------------
902+
-- PutM and container/class tests
903+
904+
prop_putM_monad_and_flush :: Word8 -> Word16 -> Word32 -> Property
905+
prop_putM_monad_and_flush w8 w16 w32 =
906+
let pm :: PutM (Word8, Word16, Word32)
907+
pm = do
908+
a <- fmap (+ 1) (putWord8 w8 *> pure w8)
909+
flush
910+
b <- pure (+) <*> (putWord16be w16 *> pure w16) <*> pure 0
911+
c <- (putWord32be w32 *> pure w32) >>= \x -> pure x
912+
return (a, b, c)
913+
(res, lbs) = runPutM pm
914+
lbsExec = Builder.toLazyByteString (execPut (putWord8 w8 `mappend` mempty `mappend` putWord16be w16 `mappend` putWord32be w32))
915+
in conjoin
916+
[ res === (w8 + 1, w16, w32)
917+
, lbs === lbsExec
918+
]
919+
920+
prop_put_infinite_streaming :: Word8 -> Property
921+
prop_put_infinite_streaming w =
922+
forAll (choose (0, 64)) $ \n ->
923+
let infMappend = putWord8 w `mappend` infMappend
924+
infSeq = mapM_ putWord8 [w ..]
925+
in conjoin
926+
[ L.unpack (L.take (fromIntegral n) (runPut infMappend)) === replicate n w
927+
, L.unpack (L.take (fromIntegral n) (runPut infSeq)) === take n [w ..]
928+
]
929+
930+
prop_ratio_zero_denom :: Int -> Bool
931+
prop_ratio_zero_denom n =
932+
case decodeOrFail (encode (n, 0 :: Int)) :: Either (L.ByteString, ByteOffset, String) (L.ByteString, ByteOffset, Ratio Int) of
933+
Left _ -> True
934+
Right _ -> False
935+
936+
genTree :: Gen (Tree.Tree Int8)
937+
genTree = sized go
938+
where
939+
go n
940+
| n <= 0 = Tree.Node <$> arbitrary <*> return []
941+
| otherwise = do
942+
k <- choose (0, min 3 n)
943+
Tree.Node <$> arbitrary <*> vectorOf k (go (n `div` 2))
944+
892945
------------------------------------------------------------------------
893946
-- Buffer representation tests (both PortableBuf and UnboxedBuf)
894947

@@ -1211,6 +1264,7 @@ tests =
12111264
#ifdef HAS_NATURAL
12121265
, testProperty "Natural invalid tag" (p prop_natural_invalid_tag)
12131266
#endif
1267+
, testProperty "Ratio zero denominator" (p prop_ratio_zero_denom)
12141268
, -- Arbitrary test input
12151269
let testInput :: [Int] ; testInput = [0 .. 10]
12161270
in testProperty "look-ahead independent of chunking" (p (prop_lookAheadIndepOfChunking testInput))
@@ -1345,6 +1399,22 @@ tests =
13451399
, test' "B.ByteString" (test :: T B.ByteString) test
13461400
, test' "L.ByteString" (test :: T L.ByteString) test
13471401
, test' "ShortByteString" (test :: T ShortByteString) test
1402+
1403+
, testWithGen "Set Int" (Set.fromList <$> (arbitrary :: Gen [Int]))
1404+
, testWithGen "Map Int Word16" (Map.fromList <$> (arbitrary :: Gen [(Int, Word16)]))
1405+
, testWithGen "IntSet" (IntSet.fromList <$> (arbitrary :: Gen [Int]))
1406+
, testWithGen "IntMap Word16" (IntMap.fromList <$> (arbitrary :: Gen [(Int, Word16)]))
1407+
, testWithGen "Seq Int" (Seq.fromList <$> (arbitrary :: Gen [Int]))
1408+
, testWithGen "Tree Int8" genTree
1409+
, testWithGen "Complex Double" (liftM2 (:+) (arbitrary :: Gen Double) arbitrary)
1410+
, testWithGen "Identity Int" (Identity <$> (arbitrary :: Gen Int))
1411+
, testWithGen "NonEmpty Int" (liftM2 (:|) (arbitrary :: Gen Int) arbitrary)
1412+
, testWithGen "Proxy Int" (return (Proxy :: Proxy Int))
1413+
]
1414+
1415+
, testGroup "Put"
1416+
[ testProperty "PutM Functor/Applicative/Monad/flush/execPut" (p prop_putM_monad_and_flush)
1417+
, testProperty "Put infinite streaming" (p prop_put_infinite_streaming)
13481418
]
13491419

13501420
, testGroup "Invariants" $ map (uncurry testProperty)

0 commit comments

Comments
 (0)