@@ -19,10 +19,18 @@ import qualified Data.ByteString.Internal as BI
1919import qualified Data.ByteString.Lazy as L
2020import qualified Data.ByteString.Lazy.Internal as L
2121import Data.ByteString.Short (ShortByteString )
22+ import Data.Complex (Complex (.. ))
23+ import Data.Functor.Identity (Identity (.. ))
2224import Data.Int
25+ import qualified Data.IntMap as IntMap
26+ import qualified Data.IntSet as IntSet
2327import Data.IORef (newIORef , readIORef , writeIORef )
28+ import Data.List.NonEmpty (NonEmpty (.. ))
29+ import qualified Data.Map as Map
2430import Data.Ratio
2531import qualified Data.Sequence as Seq
32+ import qualified Data.Set as Set
33+ import qualified Data.Tree as Tree
2634import Data.Typeable
2735import Data.Word
2836import Foreign (Ptr , castPtr , free , mallocBytes , peek , peekByteOff , poke )
@@ -53,6 +61,7 @@ import Test.QuickCheck hiding (total)
5361import qualified Action (tests )
5462import Arbitrary ()
5563import Data.Binary
64+ import qualified Data.Binary.Builder as Builder
5665import Data.Binary.Get
5766import qualified Data.Binary.Get.Internal as GetI
5867import 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