Use assertEq over assert in quickcheck properties

This commit is contained in:
Adithya Kumar 2024-07-03 16:20:50 +05:30
parent 1290c77e5e
commit eee06b68c6

View File

@ -2,6 +2,7 @@
module Streamly.Test.Unicode.Stream (main) where module Streamly.Test.Unicode.Stream (main) where
import Control.Monad (when)
import Data.Char (ord, chr) import Data.Char (ord, chr)
import Data.Word (Word8, Word16) import Data.Word (Word8, Word16)
import Test.QuickCheck import Test.QuickCheck
@ -16,7 +17,7 @@ import Test.QuickCheck
, vectorOf , vectorOf
, choose , choose
) )
import Test.QuickCheck.Monadic (run, monadicIO, assert) import Test.QuickCheck.Monadic (run, monadicIO, assert, PropertyM)
import Streamly.Data.Stream (Stream) import Streamly.Data.Stream (Stream)
import qualified Streamly.Data.Array as A import qualified Streamly.Data.Array as A
@ -40,6 +41,13 @@ maxTestCount = 10
#endif #endif
-} -}
assertEq :: (Eq a, Show a) => a -> a -> PropertyM IO ()
assertEq a b = do
when (a /= b) $ run $ do
putStrLn $ "A: " ++ show a
putStrLn $ "B: " ++ show b
assert (a == b)
-- Use quickcheck-unicode instead? -- Use quickcheck-unicode instead?
genUnicode :: Gen String genUnicode :: Gen String
genUnicode = listOf arbitraryUnicodeChar genUnicode = listOf arbitraryUnicodeChar
@ -67,7 +75,7 @@ propDecodeEncodeUtf16Id encoder decoder =
monadicIO $ do monadicIO $ do
let wrds = encoder $ Stream.fromList list let wrds = encoder $ Stream.fromList list
chrs <- run $ Stream.toList $ decoder wrds chrs <- run $ Stream.toList $ decoder wrds
assert (chrs == list) assertEq chrs list
propMkEvenW8Chunks :: Property propMkEvenW8Chunks :: Property
propMkEvenW8Chunks = propMkEvenW8Chunks =
@ -82,8 +90,8 @@ propMkEvenW8Chunks =
concatedList1 = concat list1 concatedList1 = concat list1
assert (and (map (even . length) list1)) assert (and (map (even . length) list1))
if (odd (length concatedList)) if (odd (length concatedList))
then assert (concatedList1 == init concatedList) then assertEq concatedList1 (init concatedList)
else assert (concatedList1 == concatedList) else assertEq concatedList1 concatedList
-- XXX need to use invalid characters -- XXX need to use invalid characters
propDecodeEncodeId :: Property propDecodeEncodeId :: Property
@ -92,7 +100,7 @@ propDecodeEncodeId =
monadicIO $ do monadicIO $ do
let wrds = SS.encodeUtf8 $ Stream.fromList list let wrds = SS.encodeUtf8 $ Stream.fromList list
chrs <- Stream.toList $ SS.decodeUtf8 wrds chrs <- Stream.toList $ SS.decodeUtf8 wrds
assert (chrs == list) assertEq chrs list
propDecodeEncodeIdArrays :: Property propDecodeEncodeIdArrays :: Property
propDecodeEncodeIdArrays = propDecodeEncodeIdArrays =
@ -100,7 +108,7 @@ propDecodeEncodeIdArrays =
monadicIO $ do monadicIO $ do
let wrds = Stream.chunksOf 8 $ SS.encodeUtf8' $ Stream.fromList list let wrds = Stream.chunksOf 8 $ SS.encodeUtf8' $ Stream.fromList list
chrs <- Stream.toList $ IUS.decodeUtf8Chunks wrds chrs <- Stream.toList $ IUS.decodeUtf8Chunks wrds
assert (chrs == list) assertEq chrs list
unicodeTestData :: [Char] unicodeTestData :: [Char]
unicodeTestData = "z\72150\83468;L$Wz| ?_i/J ." unicodeTestData = "z\72150\83468;L$Wz| ?_i/J ."
@ -117,7 +125,7 @@ propASCIIToLatin1 =
$ SS.encodeLatin1 $ SS.encodeLatin1
$ Stream.fromList list $ Stream.fromList list
lst <- run $ Stream.toList wrds lst <- run $ Stream.toList wrds
assert (list == lst) assertEq list lst
propUnicodeToLatin1 :: Property propUnicodeToLatin1 :: Property
propUnicodeToLatin1 = propUnicodeToLatin1 =
@ -127,7 +135,7 @@ propUnicodeToLatin1 =
$ SS.encodeLatin1 $ SS.encodeLatin1
$ Stream.fromList unicodeTestData $ Stream.fromList unicodeTestData
lst <- run $ Stream.toList wrds lst <- run $ Stream.toList wrds
assert (latin1TestData == lst) assertEq latin1TestData lst
propUnicodeToLatin1' :: Property propUnicodeToLatin1' :: Property
propUnicodeToLatin1' = propUnicodeToLatin1' =
@ -137,7 +145,7 @@ propUnicodeToLatin1' =
$ SS.encodeLatin1' $ SS.encodeLatin1'
$ Stream.fromList unicodeTestData $ Stream.fromList unicodeTestData
lst <- run $ Stream.toList wrds lst <- run $ Stream.toList wrds
assert (latin1TestData == lst) assertEq latin1TestData lst
testLines :: Property testLines :: Property
testLines = testLines =
@ -147,7 +155,7 @@ testLines =
$ fmap A.toList $ fmap A.toList
$ IUA.lines $ IUA.lines
$ Stream.fromList list $ Stream.fromList list
assert (xs == lines list) assertEq xs (lines list)
testLinesArray :: Property testLinesArray :: Property
testLinesArray = testLinesArray =
@ -157,7 +165,7 @@ testLinesArray =
$ fmap A.toList $ fmap A.toList
$ AS.splitOnSuffix 10 $ AS.splitOnSuffix 10
$ Stream.fromPure (A.fromList list) $ Stream.fromPure (A.fromList list)
assert (xs == map (map (fromIntegral . ord)) assertEq xs (map (map (fromIntegral . ord))
(lines (map (chr . fromIntegral) list))) (lines (map (chr . fromIntegral) list)))
testWords :: Property testWords :: Property
@ -168,7 +176,7 @@ testWords =
$ Stream.map A.toList $ Stream.map A.toList
$ IUA.words $ IUA.words
$ Stream.fromList list $ Stream.fromList list
assert (xs == words list) assertEq xs (words list)
testUnlines :: Property testUnlines :: Property
testUnlines = testUnlines =
@ -178,7 +186,7 @@ testUnlines =
$ IUA.unlines $ IUA.unlines
$ IUA.lines $ IUA.lines
$ Stream.fromList list $ Stream.fromList list
assert (xs == unlines (lines list)) assertEq xs (unlines (lines list))
testUnwords :: Property testUnwords :: Property
testUnwords = testUnwords =
@ -189,7 +197,7 @@ testUnwords =
$ IUA.unwords $ IUA.unwords
$ IUA.words $ IUA.words
$ Stream.fromList list $ Stream.fromList list
assert (xs == unwords (words list)) assertEq xs (unwords (words list))
moduleName :: String moduleName :: String
moduleName = "Unicode.Stream" moduleName = "Unicode.Stream"