on 4008a26 help sign in
178 lines 5.5 kB history raw
1{-# LANGUAGE CPP #-}
3-- |
4-- Module : Main
5-- Copyright : (c) 2019 Composewell Technologies
6--
7-- License : BSD-3-Clause
8-- Maintainer : streamly@composewell.com
9-- Stability : experimental
10-- Portability : GHC
11--
12module Main (main) where
14import Foreign.Storable (Storable(..))
16import Test.Hspec.QuickCheck
17import Test.QuickCheck (Property, forAll, Gen, vectorOf, arbitrary, choose)
18import Test.QuickCheck.Monadic (monadicIO, assert, run)
20import Test.Hspec as H
22import Streamly (SerialT)
24import qualified Streamly.Prelude as S
26#ifdef TEST_SMALL_ARRAY
27import qualified Streamly.Internal.Data.SmallArray as A
28type Array = A.SmallArray
29#elif defined(TEST_ARRAY)
30import qualified Streamly.Memory.Array as A
31import qualified Streamly.Internal.Memory.Array as A
32import qualified Streamly.Internal.Prelude as IP
33type Array = A.Array
34#elif defined(TEST_PRIM_ARRAY)
35import qualified Streamly.Data.PrimArray as A
36type Array = A.PrimArray
37#else
38import qualified Streamly.Internal.Data.Array as A
39type Array = A.Array
40#endif
42-- Coverage build takes too long with default number of tests
43maxTestCount :: Int
44#ifdef DEVBUILD
45maxTestCount = 100
46#else
47maxTestCount = 10
48#endif
50allocOverhead :: Int
51allocOverhead = 2 * sizeOf (undefined :: Int)
53-- XXX this should be in sync with the defaultChunkSize in Array code, or we
54-- should expose that and use that. For fast testing we could reduce the
55-- defaultChunkSize under CPP conditionals.
56--
57defaultChunkSize :: Int
58defaultChunkSize = 32 * k - allocOverhead
59 where k = 1024
61maxArrLen :: Int
62maxArrLen = defaultChunkSize * 8
64genericTestFrom ::
65 (Int -> SerialT IO Int -> IO (Array Int))
66 -> Property
67genericTestFrom arrFold =
68 forAll (choose (0, maxArrLen)) $ \len ->
69 forAll (vectorOf len (arbitrary :: Gen Int)) $ \list ->
70 monadicIO $ do
71 arr <- run $ arrFold len $ S.fromList list
72 assert (A.length arr == len)
74testLength :: Property
75testLength = genericTestFrom (\n -> S.fold (A.writeN n))
77testLengthFromStreamN :: Property
78testLengthFromStreamN = genericTestFrom A.fromStreamN
80#ifndef TEST_SMALL_ARRAY
81testLengthFromStream :: Property
82testLengthFromStream = genericTestFrom (const A.fromStream)
83#endif
85genericTestFromTo ::
86 (Int -> SerialT IO Int -> IO (Array Int))
87 -> (Array Int -> SerialT IO Int)
88 -> ([Int] -> [Int] -> Bool)
89 -> Property
90genericTestFromTo arrFold arrUnfold listEq =
91 forAll (choose (0, maxArrLen)) $ \len ->
92 forAll (vectorOf len (arbitrary :: Gen Int)) $ \list ->
93 monadicIO $ do
94 arr <- run $ arrFold len $ S.fromList list
95 xs <- run $ S.toList $ arrUnfold arr
96 assert (listEq xs list)
98testFoldNUnfold :: Property
99testFoldNUnfold =
100 genericTestFromTo (\n -> S.fold (A.writeN n)) (S.unfold A.read) (==)
102testFoldNToStream :: Property
103testFoldNToStream =
104 genericTestFromTo (\n -> S.fold (A.writeN n)) A.toStream (==)
106testFoldNToStreamRev :: Property
107testFoldNToStreamRev =
108 genericTestFromTo
109 (\n -> S.fold (A.writeN n))
110 A.toStreamRev
111 (\xs list -> xs == reverse list)
113testFromStreamNUnfold :: Property
114testFromStreamNUnfold = genericTestFromTo A.fromStreamN (S.unfold A.read) (==)
116testFromStreamNToStream :: Property
117testFromStreamNToStream = genericTestFromTo A.fromStreamN A.toStream (==)
119#ifndef TEST_SMALL_ARRAY
120testFromStreamToStream :: Property
121testFromStreamToStream = genericTestFromTo (const A.fromStream) A.toStream (==)
123testFoldUnfold :: Property
124testFoldUnfold = genericTestFromTo (const (S.fold A.write)) (S.unfold A.read) (==)
125#endif
127#ifdef TEST_ARRAY
128testArraysOf :: Property
129testArraysOf =
130 forAll (choose (0, maxArrLen)) $ \len ->
131 forAll (vectorOf len (arbitrary :: Gen Int)) $ \list ->
132 monadicIO $ do
133 xs <- S.toList
134 $ S.concatUnfold A.read
135 $ IP.arraysOf 240
136 $ S.fromList list
137 assert (xs == list)
139lastN :: Int -> [a] -> [a]
140lastN n l = drop (length l - n) l
142testLastN :: Property
143testLastN =
144 forAll (choose (0, maxArrLen)) $ \len ->
145 forAll (choose (0, len)) $ \n ->
146 forAll (vectorOf len (arbitrary :: Gen Int)) $ \list ->
147 monadicIO $ do
148 xs <- fmap A.toList
149 $ S.fold (A.lastN n)
150 $ S.fromList list
151 assert (xs == lastN n list)
152#endif
154main :: IO ()
155main =
156 hspec $
157 H.parallel $
158 modifyMaxSuccess (const maxTestCount) $ do
159 describe "Construction" $ do
160 prop "length . writeN n === n" testLength
161 prop "length . fromStreamN n === n" testLengthFromStreamN
162 prop "read . writeN === id " testFoldNUnfold
163 prop "toStream . writeN === id" testFoldNToStream
164 prop "toStreamRev . writeN === reverse" testFoldNToStreamRev
165 prop "read . fromStreamN === id" testFromStreamNUnfold
166 prop "toStream . fromStreamN === id" testFromStreamNToStream
167#ifndef TEST_SMALL_ARRAY
168 prop "length . fromStream === n" testLengthFromStream
169 prop "toStream . fromStream === id" testFromStreamToStream
170 prop "read . write === id" testFoldUnfold
171#endif
172#ifdef TEST_ARRAY
173 prop "arraysOf concats to original" testArraysOf
174#endif
175#ifdef TEST_ARRAY
176 describe "Fold" $ do
177 prop "lastN" $ testLastN
178#endif