@@ -1,183 +0,0 @@
3−-- Copyright : (c) 2019 Composewell Technologies
5−-- License : BSD-3-Clause
6−-- Maintainer : streamly@composewell.com
7−-- Stability : experimental
11−-- This is a common array test module that gets included in different
12−-- Array test modules with the corresponding macro defined.
14−-- Meanings of CPP macros:
15−-- Default => Data.Array
16−-- TEST_ARRAY => Data.Array.Foreign
17−-- TEST_SMALL_ARRAY => Data.SmallArray
18−-- DATA_ARRAY_PRIM => Data.Array.Prim
19−-- DATA_ARRAY_PRIM_PINNED => Data.Array.Prim.Pinned
21−import Foreign.Storable (Storable(..))
23−import Test.Hspec.QuickCheck
24−import Test.QuickCheck (Property, forAll, Gen, vectorOf, arbitrary, choose)
25−import Test.QuickCheck.Monadic (monadicIO, assert, run)
26−import Test.Hspec as H
28−import Streamly.Data.Fold (Fold)
29−import Streamly.Prelude (SerialT)
30−import Streamly.Test.Common (listEquals)
32−import qualified Streamly.Prelude as S
34−#ifdef TEST_SMALL_ARRAY
35−import qualified Streamly.Internal.Data.SmallArray as A
36−type Array = A.SmallArray
40−#ifdef TEST_SMALL_ARRAY
41−moduleName = "Data.SmallArray"
44−-- Coverage build takes too long with default number of tests
53−allocOverhead = 2 * sizeOf (undefined :: Int)
55−-- XXX this should be in sync with the defaultChunkSize in Array code, or we
56−-- should expose that and use that. For fast testing we could reduce the
57−-- defaultChunkSize under CPP conditionals.
59−defaultChunkSize :: Int
60−defaultChunkSize = 32 * k - allocOverhead
64−maxArrLen = defaultChunkSize * 8
67− (Int -> SerialT IO Int -> IO (Array Int))
69−genericTestFrom arrFold =
70− forAll (choose (0, maxArrLen)) $ \len ->
71− forAll (vectorOf len (arbitrary :: Gen Int)) $ \list ->
73− arr <- run $ arrFold len $ S.fromList list
74− assert (A.length arr == len)
76−testLength :: Property
77−testLength = genericTestFrom (\n -> S.fold (A.writeN n))
79−testLengthFromStreamN :: Property
80−testLengthFromStreamN = genericTestFrom A.fromStreamN
82−#ifndef TEST_SMALL_ARRAY
83−testLengthFromStream :: Property
84−testLengthFromStream = genericTestFrom (const A.fromStream)
88− (Int -> SerialT IO Int -> IO (Array Int))
89− -> (Array Int -> SerialT IO Int)
90− -> ([Int] -> [Int] -> Bool)
92−genericTestFromTo arrFold arrUnfold listEq =
93− forAll (choose (0, maxArrLen)) $ \len ->
94− forAll (vectorOf len (arbitrary :: Gen Int)) $ \list ->
96− arr <- run $ arrFold len $ S.fromList list
97− xs <- run $ S.toList $ arrUnfold arr
98− assert (listEq xs list)
100−testFoldNUnfold :: Property
102− genericTestFromTo (\n -> S.fold (A.writeN n)) (S.unfold A.read) (==)
104−testFoldNToStream :: Property
106− genericTestFromTo (\n -> S.fold (A.writeN n)) A.toStream (==)
108−testFoldNToStreamRev :: Property
109−testFoldNToStreamRev =
111− (\n -> S.fold (A.writeN n))
113− (\xs list -> xs == reverse list)
115−testFromStreamNUnfold :: Property
116−testFromStreamNUnfold = genericTestFromTo A.fromStreamN (S.unfold A.read) (==)
118−testFromStreamNToStream :: Property
119−testFromStreamNToStream = genericTestFromTo A.fromStreamN A.toStream (==)
121−testFromListN :: Property
123− forAll (choose (0, maxArrLen)) $ \len ->
124− forAll (choose (0, len)) $ \n ->
125− forAll (vectorOf len (arbitrary :: Gen Int)) $ \list ->
127− let arr = A.fromListN n list
128− xs <- run $ S.toList $ (S.unfold A.read) arr
129− listEquals (==) xs (take n list)
131−#ifndef TEST_SMALL_ARRAY
132−testFromStreamToStream :: Property
133−testFromStreamToStream = genericTestFromTo (const A.fromStream) A.toStream (==)
135−testFoldUnfold :: Property
136−testFoldUnfold = genericTestFromTo (const (S.fold A.write)) (S.unfold A.read) (==)
138−testFromList :: Property
140− forAll (choose (0, maxArrLen)) $ \len ->
141− forAll (vectorOf len (arbitrary :: Gen Int)) $ \list ->
143− let arr = A.fromList list
144− xs <- run $ S.toList $ (S.unfold A.read) arr
148−foldManyWith :: (Int -> Fold IO Int (Array Int)) -> Property
150− forAll (choose (0, maxArrLen)) $ \len ->
151− forAll (vectorOf len (arbitrary :: Gen Int)) $ \list ->
155− $ S.unfoldMany A.read
156− $ S.foldMany (f 240)
164− modifyMaxSuccess (const maxTestCount) $ do
165− describe moduleName $ do
166− describe "Construction" $ do
167− prop "length . writeN n === n" testLength
168− prop "length . fromStreamN n === n" testLengthFromStreamN
169− prop "read . writeN === id " testFoldNUnfold
170− prop "toStream . writeN === id" testFoldNToStream
171− prop "toStreamRev . writeN === reverse" testFoldNToStreamRev
172− prop "read . fromStreamN === id" testFromStreamNUnfold
173− prop "toStream . fromStreamN === id" testFromStreamNToStream
174− prop "fromListN" testFromListN
176−#ifndef TEST_SMALL_ARRAY
177− prop "length . fromStream === n" testLengthFromStream
178− prop "toStream . fromStream === id" testFromStreamToStream
179− prop "read . write === id" testFoldUnfold
180− prop "fromList" testFromList
182− prop "foldMany with writeN concats to original"
183− (foldManyWith A.writeN)