on master help sign in
Add test for writeN fold applied multiple times
b38ae55Harendra Kumar
test/Streamly/Test/Common/Array.hs+22−15
@@ -10,6 +10,13 @@
10
10
11-- This is a common array test module that gets included in different
11-- This is a common array test module that gets included in different
12-- Array test modules with the corresponding macro defined.
12-- Array test modules with the corresponding macro defined.
13+--
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
13
20
14import Foreign.Storable (Storable(..))
21import Foreign.Storable (Storable(..))
15
22
@@ -18,10 +25,18 @@ import Test.QuickCheck (Property, forAll, Gen, vectorOf, arbitrary, choose)
18import Test.QuickCheck.Monadic (monadicIO, assert, run)
25import Test.QuickCheck.Monadic (monadicIO, assert, run)
19import Test.Hspec as H
26import Test.Hspec as H
20
27
28+import Streamly.Data.Fold (Fold)
21import Streamly.Prelude (SerialT)
29import Streamly.Prelude (SerialT)
22import Streamly.Test.Common (listEquals)
30import Streamly.Test.Common (listEquals)
23
31
24import qualified Streamly.Prelude as S
32import qualified Streamly.Prelude as S
33+
34+#if defined(TEST_ARRAY) ||\
35+ defined(DATA_ARRAY_PRIM) ||\
36+ defined(DATA_ARRAY_PRIM_PINNED)
37+import qualified Streamly.Internal.Data.Fold as Fold
38+#endif
39+
25#ifdef TEST_SMALL_ARRAY
40#ifdef TEST_SMALL_ARRAY
26import qualified Streamly.Internal.Data.SmallArray as A
41import qualified Streamly.Internal.Data.SmallArray as A
27type Array = A.SmallArray
42type Array = A.SmallArray
@@ -31,18 +46,15 @@ import Data.Word(Word8)
31import qualified Streamly.Internal.Data.Array.Foreign as A
46import qualified Streamly.Internal.Data.Array.Foreign as A
32import qualified Streamly.Internal.Data.Array.Foreign.Type as A
47import qualified Streamly.Internal.Data.Array.Foreign.Type as A
33import qualified Streamly.Internal.Data.Array.Foreign.Mut.Type as MA
48import qualified Streamly.Internal.Data.Array.Foreign.Mut.Type as MA
34−import qualified Streamly.Internal.Data.Stream.IsStream as IP
35import qualified Streamly.Internal.Data.Array.Stream.Foreign as AS
49import qualified Streamly.Internal.Data.Array.Stream.Foreign as AS
36type Array = A.Array
50type Array = A.Array
37#elif defined(DATA_ARRAY_PRIM_PINNED)
51#elif defined(DATA_ARRAY_PRIM_PINNED)
38import qualified Streamly.Internal.Data.Array.Prim.Pinned as A
52import qualified Streamly.Internal.Data.Array.Prim.Pinned as A
39import qualified Streamly.Internal.Data.Array.Prim.Pinned.Type as A
53import qualified Streamly.Internal.Data.Array.Prim.Pinned.Type as A
40−import qualified Streamly.Internal.Data.Stream.IsStream as IP
41type Array = A.Array
54type Array = A.Array
42#elif defined(DATA_ARRAY_PRIM)
55#elif defined(DATA_ARRAY_PRIM)
43import qualified Streamly.Internal.Data.Array.Prim as A
56import qualified Streamly.Internal.Data.Array.Prim as A
44import qualified Streamly.Internal.Data.Array.Prim.Type as A
57import qualified Streamly.Internal.Data.Array.Prim.Type as A
45−import qualified Streamly.Internal.Data.Stream.IsStream as IP
46type Array = A.Array
58type Array = A.Array
47#else
59#else
48import qualified Streamly.Internal.Data.Array as A
60import qualified Streamly.Internal.Data.Array as A
@@ -166,25 +178,17 @@ testFromList =
166 assert (xs == list)
178 assert (xs == list)
167#endif
179#endif
168
180
169−#if defined(TEST_ARRAY) ||\
181+foldManyWith :: (Int -> Fold IO Int (Array Int)) -> Property
170− defined(DATA_ARRAY_PRIM) ||\
182+foldManyWith f =
171− defined(DATA_ARRAY_PRIM_PINNED)
172−
173−testArraysOf :: Property
174−testArraysOf =
175 forAll (choose (0, maxArrLen)) $ \len ->
183 forAll (choose (0, maxArrLen)) $ \len ->
176 forAll (vectorOf len (arbitrary :: Gen Int)) $ \list ->
184 forAll (vectorOf len (arbitrary :: Gen Int)) $ \list ->
177 monadicIO $ do
185 monadicIO $ do
178 xs <- run
186 xs <- run
179 $ S.toList
187 $ S.toList
180 $ S.unfoldMany A.read
188 $ S.unfoldMany A.read
181− $ arraysOf 240
189+ $ S.foldMany (f 240)
182 $ S.fromList list
190 $ S.fromList list
183 assert (xs == list)
191 assert (xs == list)
184− where
185− arraysOf n = IP.chunksOf n (A.writeNUnsafe n)
186−
187−#endif
188
192
189#ifdef TEST_ARRAY
193#ifdef TEST_ARRAY
190
194
@@ -261,8 +265,11 @@ main =
261#if defined(TEST_ARRAY) ||\
265#if defined(TEST_ARRAY) ||\
262 defined(DATA_ARRAY_PRIM) ||\
266 defined(DATA_ARRAY_PRIM) ||\
263 defined(DATA_ARRAY_PRIM_PINNED)
267 defined(DATA_ARRAY_PRIM_PINNED)
264− prop "arraysOf concats to original" testArraysOf
268+ prop "foldMany with writeNUnsafe concats to original"
269+ (foldManyWith (\n -> Fold.take n (A.writeNUnsafe n)))
265#endif
270#endif
271+ prop "foldMany with writeN concats to original"
272+ (foldManyWith A.writeN)
266
273
267#ifdef TEST_ARRAY
274#ifdef TEST_ARRAY
268 prop "AS.concat . (A.fromList . (:[]) <$>) === id" $ concatArrayW8
275 prop "AS.concat . (A.fromList . (:[]) <$>) === id" $ concatArrayW8