@@ -0,0 +1,113 @@
3+-- Copyright : (c) 2019 Composewell Technologies
5+-- License : BSD-3-Clause
6+-- Maintainer : streamly@composewell.com
7+-- Stability : experimental
13+module Main (main) where
15+import Foreign.Storable (Storable(..))
17+import Test.Hspec.QuickCheck
18+import Test.QuickCheck (Property, forAll, Gen, vectorOf, arbitrary, choose)
19+import Test.QuickCheck.Monadic (monadicIO, assert, run)
21+import Test.Hspec as H
23+import Streamly (SerialT)
24+import Streamly.Data.SmallArray (SmallArray)
26+import qualified Streamly.Data.SmallArray as A
27+import qualified Streamly.Prelude as S
29+-- Coverage build takes too long with default number of tests
38+allocOverhead = 2 * sizeOf (undefined :: Int)
40+-- XXX this should be in sync with the defaultChunkSize in Array code, or we
41+-- should expose that and use that. For fast testing we could reduce the
42+-- defaultChunkSize under CPP conditionals.
44+defaultChunkSize :: Int
45+defaultChunkSize = 32 * k - allocOverhead
49+maxArrLen = defaultChunkSize * 8
52+ (Int -> SerialT IO Int -> IO (SmallArray Int))
54+genericTestFrom arrFold =
55+ forAll (choose (0, maxArrLen)) $ \len ->
56+ forAll (vectorOf len (arbitrary :: Gen Int)) $ \list ->
58+ arr <- run $ arrFold len $ S.fromList list
59+ assert (A.length arr == len)
61+testLength :: Property
62+testLength = genericTestFrom (\n -> S.fold (A.writeN n))
64+testLengthFromStreamN :: Property
65+testLengthFromStreamN = genericTestFrom A.fromStreamN
68+ (Int -> SerialT IO Int -> IO (SmallArray Int))
69+ -> (SmallArray Int -> SerialT IO Int)
70+ -> ([Int] -> [Int] -> Bool)
72+genericTestFromTo arrFold arrUnfold listEq =
73+ forAll (choose (0, maxArrLen)) $ \len ->
74+ forAll (vectorOf len (arbitrary :: Gen Int)) $ \list ->
76+ arr <- run $ arrFold len $ S.fromList list
77+ xs <- run $ S.toList $ arrUnfold arr
78+ assert (listEq xs list)
80+testFoldNUnfold :: Property
82+ genericTestFromTo (\n -> S.fold (A.writeN n)) (S.unfold A.read) (==)
84+testFoldNToStream :: Property
86+ genericTestFromTo (\n -> S.fold (A.writeN n)) A.toStream (==)
88+testFoldNToStreamRev :: Property
89+testFoldNToStreamRev =
91+ (\n -> S.fold (A.writeN n))
93+ (\xs list -> xs == reverse list)
95+testFromStreamNUnfold :: Property
96+testFromStreamNUnfold = genericTestFromTo A.fromStreamN (S.unfold A.read) (==)
98+testFromStreamNToStream :: Property
99+testFromStreamNToStream = genericTestFromTo A.fromStreamN A.toStream (==)
105+ modifyMaxSuccess (const maxTestCount) $
106+ describe "Construction" $ do
107+ prop "length . writeN n === n" testLength
108+ prop "length . fromStreamN n === n" testLengthFromStreamN
109+ prop "read . writeN === id " testFoldNUnfold
110+ prop "toStream . writeN === id" testFoldNToStream
111+ prop "toStreamRev . writeN === reverse" testFoldNToStreamRev
112+ prop "read . fromStreamN === id" testFromStreamNUnfold
113+ prop "toStream . fromStreamN === id" testFromStreamNToStream