on master help sign in
Move common array test code cleanly out and plug it in Data.Array
commitdb254e10b777020ce6ca76e045700f2b231d38cb authoradithyaov <adithya@composewell.com> dateSat 27 Nov 2021, 02:28 (5 years ago) parentd4e6ca6
5 files changed+164−6
streamly.cabal+2 test/Streamly/Test/Common/Array.hs−5 test/Streamly/Test/Data/Array.hs+40−1 test/Streamly/Test/Data/Array/Common.hs+109 test/Streamly/Test/Data/Array/CommonImports.hs+13
streamly.cabal+2
@@ -88,6 +88,8 @@ extra-source-files:
8888 src/inline.hs
8989 test/Streamly/Test/Common/Array.hs
9090 test/Streamly/Test/Data/*.hs
91+ test/Streamly/Test/Data/Array/CommonImports.hs
92+ test/Streamly/Test/Data/Array/Common.hs
9193 test/Streamly/Test/Data/Array/Prim.hs
9294 test/Streamly/Test/Data/Array/Prim/Pinned.hs
9395 test/Streamly/Test/Data/Array/Foreign.hs
test/Streamly/Test/Common/Array.hs−5
@@ -56,9 +56,6 @@ type Array = A.Array
5656import qualified Streamly.Internal.Data.Array.Prim as A
5757import qualified Streamly.Internal.Data.Array.Prim.Type as A
5858type Array = A.Array
59−#else
60−import qualified Streamly.Internal.Data.Array as A
61−type Array = A.Array
6259#endif
6360
6461moduleName :: String
@@ -70,8 +67,6 @@ moduleName = "Data.Array.Foreign"
7067moduleName = "Data.Array.Prim.Pinned"
7168#elif defined(DATA_ARRAY_PRIM)
7269moduleName = "Data.Array.Prim"
73−#else
74−moduleName = "Data.Array"
7570#endif
7671
7772-- Coverage build takes too long with default number of tests
test/Streamly/Test/Data/Array.hs+40−1
@@ -8,4 +8,43 @@
88
99module Streamly.Test.Data.Array where
1010
11−#include "Streamly/Test/Common/Array.hs"
11+#include "Streamly/Test/Data/Array/CommonImports.hs"
12+
13+import qualified Streamly.Internal.Data.Array as A
14+type Array = A.Array
15+
16+moduleName :: String
17+moduleName = "Data.Array"
18+
19+#include "Streamly/Test/Data/Array/Common.hs"
20+
21+testFromStreamToStream :: Property
22+testFromStreamToStream = genericTestFromTo (const A.fromStream) A.toStream (==)
23+
24+testFoldUnfold :: Property
25+testFoldUnfold = genericTestFromTo (const (S.fold A.write)) (S.unfold A.read) (==)
26+
27+testFromList :: Property
28+testFromList =
29+ forAll (choose (0, maxArrLen)) $ \len ->
30+ forAll (vectorOf len (arbitrary :: Gen Int)) $ \list ->
31+ monadicIO $ do
32+ let arr = A.fromList list
33+ xs <- run $ S.toList $ (S.unfold A.read) arr
34+ assert (xs == list)
35+
36+testLengthFromStream :: Property
37+testLengthFromStream = genericTestFrom (const A.fromStream)
38+
39+main :: IO ()
40+main =
41+ hspec $
42+ H.parallel $
43+ modifyMaxSuccess (const maxTestCount) $ do
44+ describe moduleName $ do
45+ commonMain
46+ describe "Construction" $ do
47+ prop "length . fromStream === n" testLengthFromStream
48+ prop "toStream . fromStream === id" testFromStreamToStream
49+ prop "read . write === id" testFoldUnfold
50+ prop "fromList" testFromList
test/Streamly/Test/Data/Array/Common.hsnew file+109
@@ -0,0 +1,109 @@
1+
2+-- Coverage build takes too long with default number of tests
3+maxTestCount :: Int
4+#ifdef DEVBUILD
5+maxTestCount = 100
6+#else
7+maxTestCount = 10
8+#endif
9+
10+allocOverhead :: Int
11+allocOverhead = 2 * sizeOf (undefined :: Int)
12+
13+-- XXX this should be in sync with the defaultChunkSize in Array code, or we
14+-- should expose that and use that. For fast testing we could reduce the
15+-- defaultChunkSize under CPP conditionals.
16+--
17+defaultChunkSize :: Int
18+defaultChunkSize = 32 * k - allocOverhead
19+ where k = 1024
20+
21+maxArrLen :: Int
22+maxArrLen = defaultChunkSize * 8
23+
24+genericTestFrom ::
25+ (Int -> SerialT IO Int -> IO (Array Int))
26+ -> Property
27+genericTestFrom arrFold =
28+ forAll (choose (0, maxArrLen)) $ \len ->
29+ forAll (vectorOf len (arbitrary :: Gen Int)) $ \list ->
30+ monadicIO $ do
31+ arr <- run $ arrFold len $ S.fromList list
32+ assert (A.length arr == len)
33+
34+testLength :: Property
35+testLength = genericTestFrom (\n -> S.fold (A.writeN n))
36+
37+testLengthFromStreamN :: Property
38+testLengthFromStreamN = genericTestFrom A.fromStreamN
39+
40+genericTestFromTo ::
41+ (Int -> SerialT IO Int -> IO (Array Int))
42+ -> (Array Int -> SerialT IO Int)
43+ -> ([Int] -> [Int] -> Bool)
44+ -> Property
45+genericTestFromTo arrFold arrUnfold listEq =
46+ forAll (choose (0, maxArrLen)) $ \len ->
47+ forAll (vectorOf len (arbitrary :: Gen Int)) $ \list ->
48+ monadicIO $ do
49+ arr <- run $ arrFold len $ S.fromList list
50+ xs <- run $ S.toList $ arrUnfold arr
51+ assert (listEq xs list)
52+
53+
54+testFoldNUnfold :: Property
55+testFoldNUnfold =
56+ genericTestFromTo (\n -> S.fold (A.writeN n)) (S.unfold A.read) (==)
57+
58+testFoldNToStream :: Property
59+testFoldNToStream =
60+ genericTestFromTo (\n -> S.fold (A.writeN n)) A.toStream (==)
61+
62+testFoldNToStreamRev :: Property
63+testFoldNToStreamRev =
64+ genericTestFromTo
65+ (\n -> S.fold (A.writeN n))
66+ A.toStreamRev
67+ (\xs list -> xs == reverse list)
68+
69+testFromStreamNUnfold :: Property
70+testFromStreamNUnfold = genericTestFromTo A.fromStreamN (S.unfold A.read) (==)
71+
72+testFromStreamNToStream :: Property
73+testFromStreamNToStream = genericTestFromTo A.fromStreamN A.toStream (==)
74+
75+testFromListN :: Property
76+testFromListN =
77+ forAll (choose (0, maxArrLen)) $ \len ->
78+ forAll (choose (0, len)) $ \n ->
79+ forAll (vectorOf len (arbitrary :: Gen Int)) $ \list ->
80+ monadicIO $ do
81+ let arr = A.fromListN n list
82+ xs <- run $ S.toList $ (S.unfold A.read) arr
83+ listEquals (==) xs (take n list)
84+
85+foldManyWith :: (Int -> Fold IO Int (Array Int)) -> Property
86+foldManyWith f =
87+ forAll (choose (0, maxArrLen)) $ \len ->
88+ forAll (vectorOf len (arbitrary :: Gen Int)) $ \list ->
89+ monadicIO $ do
90+ xs <- run
91+ $ S.toList
92+ $ S.unfoldMany A.read
93+ $ S.foldMany (f 240)
94+ $ S.fromList list
95+ assert (xs == list)
96+
97+commonMain :: SpecWith ()
98+commonMain = do
99+ describe "Construction" $ do
100+ prop "length . writeN n === n" testLength
101+ prop "length . fromStreamN n === n" testLengthFromStreamN
102+ prop "read . writeN === id " testFoldNUnfold
103+ prop "toStream . writeN === id" testFoldNToStream
104+ prop "toStreamRev . writeN === reverse" testFoldNToStreamRev
105+ prop "read . fromStreamN === id" testFromStreamNUnfold
106+ prop "toStream . fromStreamN === id" testFromStreamNToStream
107+ prop "fromListN" testFromListN
108+ prop "foldMany with writeN concats to original"
109+ (foldManyWith A.writeN)
test/Streamly/Test/Data/Array/CommonImports.hsnew file+13
@@ -0,0 +1,13 @@
1+
2+import Foreign.Storable (Storable(..))
3+
4+import Test.Hspec.QuickCheck
5+import Test.QuickCheck (Property, forAll, Gen, vectorOf, arbitrary, choose)
6+import Test.QuickCheck.Monadic (monadicIO, assert, run)
7+import Test.Hspec as H
8+
9+import Streamly.Data.Fold (Fold)
10+import Streamly.Prelude (SerialT)
11+import Streamly.Test.Common (listEquals)
12+
13+import qualified Streamly.Prelude as S