on 55eb264 help sign in
124 lines 3.9 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)
23import Streamly.Data.Array (Array)
25import qualified Streamly.Data.Array as A
26import qualified Streamly.Prelude as S
28-- Coverage build takes too long with default number of tests
29maxTestCount :: Int
30#ifdef DEVBUILD
31maxTestCount = 100
32#else
33maxTestCount = 10
34#endif
36allocOverhead :: Int
37allocOverhead = 2 * sizeOf (undefined :: Int)
39-- XXX this should be in sync with the defaultChunkSize in Array code, or we
40-- should expose that and use that. For fast testing we could reduce the
41-- defaultChunkSize under CPP conditionals.
42--
43defaultChunkSize :: Int
44defaultChunkSize = 32 * k - allocOverhead
45 where k = 1024
47maxArrLen :: Int
48maxArrLen = defaultChunkSize * 8
50genericTestFrom ::
51 (Int -> SerialT IO Int -> IO (Array Int))
52 -> Property
53genericTestFrom arrFold =
54 forAll (choose (0, maxArrLen)) $ \len ->
55 forAll (vectorOf len (arbitrary :: Gen Int)) $ \list ->
56 monadicIO $ do
57 arr <- run $ arrFold len $ S.fromList list
58 assert (A.length arr == len)
60testLength :: Property
61testLength = genericTestFrom (\n -> S.fold (A.writeN n))
63testLengthFromStreamN :: Property
64testLengthFromStreamN = genericTestFrom A.fromStreamN
66testLengthFromStream :: Property
67testLengthFromStream = genericTestFrom (const A.fromStream)
69genericTestFromTo ::
70 (Int -> SerialT IO Int -> IO (Array Int))
71 -> (Array Int -> SerialT IO Int)
72 -> ([Int] -> [Int] -> Bool)
73 -> Property
74genericTestFromTo arrFold arrUnfold listEq =
75 forAll (choose (0, maxArrLen)) $ \len ->
76 forAll (vectorOf len (arbitrary :: Gen Int)) $ \list ->
77 monadicIO $ do
78 arr <- run $ arrFold len $ S.fromList list
79 xs <- run $ S.toList $ arrUnfold arr
80 assert (listEq xs list)
82testFoldNUnfold :: Property
83testFoldNUnfold =
84 genericTestFromTo (\n -> S.fold (A.writeN n)) (S.unfold A.read) (==)
86testFoldNToStream :: Property
87testFoldNToStream =
88 genericTestFromTo (\n -> S.fold (A.writeN n)) A.toStream (==)
90testFoldNToStreamRev :: Property
91testFoldNToStreamRev =
92 genericTestFromTo
93 (\n -> S.fold (A.writeN n))
94 A.toStreamRev
95 (\xs list -> xs == reverse list)
97testFromStreamNUnfold :: Property
98testFromStreamNUnfold = genericTestFromTo A.fromStreamN (S.unfold A.read) (==)
100testFromStreamNToStream :: Property
101testFromStreamNToStream = genericTestFromTo A.fromStreamN A.toStream (==)
103testFromStreamToStream :: Property
104testFromStreamToStream = genericTestFromTo (const A.fromStream) A.toStream (==)
106testFoldUnfold :: Property
107testFoldUnfold = genericTestFromTo (const (S.fold A.write)) (S.unfold A.read) (==)
109main :: IO ()
110main =
111 hspec $
112 H.parallel $
113 modifyMaxSuccess (const maxTestCount) $
114 describe "Construction" $ do
115 prop "length . writeN n === n" testLength
116 prop "length . fromStreamN n === n" testLengthFromStreamN
117 prop "length . fromStream === n" testLengthFromStream
118 prop "read . writeN === id " testFoldNUnfold
119 prop "toStream . writeN === id" testFoldNToStream
120 prop "toStreamRev . writeN === reverse" testFoldNToStreamRev
121 prop "read . fromStreamN === id" testFromStreamNUnfold
122 prop "toStream . fromStreamN === id" testFromStreamNToStream
123 prop "toStream . fromStream === id" testFromStreamToStream
124 prop "read . write === id" testFoldUnfold