on d462530 help sign in
streamly/test/Arrays.hs
114 lines 3.5 kB history raw
1{-# LANGUAGE CPP #-}
2{-# LANGUAGE FlexibleContexts #-}
4module Main (main) where
6import Foreign.Storable (Storable(..))
8import Test.Hspec.QuickCheck
9import Test.QuickCheck (Property, forAll, Gen, vectorOf, arbitrary, choose)
10import Test.QuickCheck.Monadic (monadicIO, assert)
12import Test.Hspec as H
14import qualified Streamly.Mem.Array as A
15import qualified Streamly.Mem.Array.Stream as AS
16import qualified Streamly.Prelude as S
18-- Coverage build takes too long with default number of tests
19maxTestCount :: Int
20#ifdef DEVBUILD
21maxTestCount = 100
22#else
23maxTestCount = 10
24#endif
26allocOverhead :: Int
27allocOverhead = 2 * sizeOf (undefined :: Int)
29-- XXX this should be in sync with the defaultChunkSize in Array code, or we
30-- should expose that and use that. For fast testing we could reduce the
31-- defaultChunkSize under CPP conditionals.
32--
33defaultChunkSize :: Int
34defaultChunkSize = 32 * k - allocOverhead
35 where k = 1024
37maxArrLen :: Int
38maxArrLen = defaultChunkSize * 8
40testLength :: Property
41testLength =
42 forAll (choose (0, maxArrLen)) $ \len ->
43 forAll (vectorOf len (arbitrary :: Gen Int)) $ \list ->
44 monadicIO $ do
45 arr <- A.writeN len
46 $ S.fromList list
47 assert (A.length arr == len)
49testFromToStreamN :: Property
50testFromToStreamN =
51 forAll (choose (0, maxArrLen)) $ \len ->
52 forAll (vectorOf len (arbitrary :: Gen Int)) $ \list ->
53 monadicIO $ do
54 arr <- A.writeN len
55 $ S.fromList list
56 xs <- S.toList
57 $ A.read arr
58 assert (xs == list)
60testToStreamRev :: Property
61testToStreamRev =
62 forAll (choose (0, maxArrLen)) $ \len ->
63 forAll (vectorOf len (arbitrary :: Gen Int)) $ \list ->
64 monadicIO $ do
65 arr <- A.writeN len
66 $ S.fromList list
67 xs <- S.toList
68 $ A.readRev arr
69 assert (xs == reverse list)
71testArraysOf :: Property
72testArraysOf =
73 forAll (choose (0, maxArrLen)) $ \len ->
74 forAll (vectorOf len (arbitrary :: Gen Int)) $ \list ->
75 monadicIO $ do
76 xs <- S.toList
77 $ S.concatMap A.read
78 $ AS.arraysOf 240
79 $ S.fromList list
80 assert (xs == list)
82testFlattenArrays :: Property
83testFlattenArrays =
84 forAll (choose (0, maxArrLen)) $ \len ->
85 forAll (vectorOf len (arbitrary :: Gen Int)) $ \list ->
86 monadicIO $ do
87 xs <- S.toList
88 $ AS.flatten
89 $ AS.arraysOf 240
90 $ S.fromList list
91 assert (xs == list)
93testFromToStream :: Property
94testFromToStream =
95 forAll (choose (0, maxArrLen)) $ \len ->
96 forAll (vectorOf len (arbitrary :: Gen Int)) $ \list ->
97 monadicIO $ do
98 arr <- A.write $ S.fromList list
99 xs <- S.toList
100 $ A.read arr
101 assert (xs == list)
103main :: IO ()
104main = hspec
105 $ H.parallel
106 $ modifyMaxSuccess (const maxTestCount)
107 $ do
108 describe "Construction" $ do
109 prop "length . writeN n === n" $ testLength
110 prop "read . writeN n === id" $ testFromToStreamN
111 prop "readRev . write === reverse" $ testToStreamRev
112 prop "arraysOf concats to original" $ testArraysOf
113 prop "flattenArrays concats to original" $ testFlattenArrays
114 prop "read . write === id" $ testFromToStream