on ef7147c help sign in
157 lines 4.7 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.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#else
35import qualified Streamly.Data.Array as A
36type Array = A.Array
37#endif
39-- Coverage build takes too long with default number of tests
40maxTestCount :: Int
41#ifdef DEVBUILD
42maxTestCount = 100
43#else
44maxTestCount = 10
45#endif
47allocOverhead :: Int
48allocOverhead = 2 * sizeOf (undefined :: Int)
50-- XXX this should be in sync with the defaultChunkSize in Array code, or we
51-- should expose that and use that. For fast testing we could reduce the
52-- defaultChunkSize under CPP conditionals.
53--
54defaultChunkSize :: Int
55defaultChunkSize = 32 * k - allocOverhead
56 where k = 1024
58maxArrLen :: Int
59maxArrLen = defaultChunkSize * 8
61genericTestFrom ::
62 (Int -> SerialT IO Int -> IO (Array Int))
63 -> Property
64genericTestFrom arrFold =
65 forAll (choose (0, maxArrLen)) $ \len ->
66 forAll (vectorOf len (arbitrary :: Gen Int)) $ \list ->
67 monadicIO $ do
68 arr <- run $ arrFold len $ S.fromList list
69 assert (A.length arr == len)
71testLength :: Property
72testLength = genericTestFrom (\n -> S.fold (A.writeN n))
74testLengthFromStreamN :: Property
75testLengthFromStreamN = genericTestFrom A.fromStreamN
77#ifndef TEST_SMALL_ARRAY
78testLengthFromStream :: Property
79testLengthFromStream = genericTestFrom (const A.fromStream)
80#endif
82genericTestFromTo ::
83 (Int -> SerialT IO Int -> IO (Array Int))
84 -> (Array Int -> SerialT IO Int)
85 -> ([Int] -> [Int] -> Bool)
86 -> Property
87genericTestFromTo arrFold arrUnfold listEq =
88 forAll (choose (0, maxArrLen)) $ \len ->
89 forAll (vectorOf len (arbitrary :: Gen Int)) $ \list ->
90 monadicIO $ do
91 arr <- run $ arrFold len $ S.fromList list
92 xs <- run $ S.toList $ arrUnfold arr
93 assert (listEq xs list)
95testFoldNUnfold :: Property
96testFoldNUnfold =
97 genericTestFromTo (\n -> S.fold (A.writeN n)) (S.unfold A.read) (==)
99testFoldNToStream :: Property
100testFoldNToStream =
101 genericTestFromTo (\n -> S.fold (A.writeN n)) A.toStream (==)
103testFoldNToStreamRev :: Property
104testFoldNToStreamRev =
105 genericTestFromTo
106 (\n -> S.fold (A.writeN n))
107 A.toStreamRev
108 (\xs list -> xs == reverse list)
110testFromStreamNUnfold :: Property
111testFromStreamNUnfold = genericTestFromTo A.fromStreamN (S.unfold A.read) (==)
113testFromStreamNToStream :: Property
114testFromStreamNToStream = genericTestFromTo A.fromStreamN A.toStream (==)
116#ifndef TEST_SMALL_ARRAY
117testFromStreamToStream :: Property
118testFromStreamToStream = genericTestFromTo (const A.fromStream) A.toStream (==)
120testFoldUnfold :: Property
121testFoldUnfold = genericTestFromTo (const (S.fold A.write)) (S.unfold A.read) (==)
122#endif
124#ifdef TEST_ARRAY
125testArraysOf :: Property
126testArraysOf =
127 forAll (choose (0, maxArrLen)) $ \len ->
128 forAll (vectorOf len (arbitrary :: Gen Int)) $ \list ->
129 monadicIO $ do
130 xs <- S.toList
131 $ S.concatUnfold A.read
132 $ IP.arraysOf 240
133 $ S.fromList list
134 assert (xs == list)
135#endif
137main :: IO ()
138main =
139 hspec $
140 H.parallel $
141 modifyMaxSuccess (const maxTestCount) $
142 describe "Construction" $ do
143 prop "length . writeN n === n" testLength
144 prop "length . fromStreamN n === n" testLengthFromStreamN
145 prop "read . writeN === id " testFoldNUnfold
146 prop "toStream . writeN === id" testFoldNToStream
147 prop "toStreamRev . writeN === reverse" testFoldNToStreamRev
148 prop "read . fromStreamN === id" testFromStreamNUnfold
149 prop "toStream . fromStreamN === id" testFromStreamNToStream
150#ifndef TEST_SMALL_ARRAY
151 prop "length . fromStream === n" testLengthFromStream
152 prop "toStream . fromStream === id" testFromStreamToStream
153 prop "read . write === id" testFoldUnfold
154#endif
155#ifdef TEST_ARRAY
156 prop "arraysOf concats to original" testArraysOf
157#endif