on fcf54e3 help sign in
streamly/test/Arrays.hs
115 lines 3.7 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.Internal.Memory.Array as IA
15import qualified Streamly.Memory.Array as A
16import qualified Streamly.Prelude as S
17import qualified Streamly.Internal.Prelude as IP
19-- Coverage build takes too long with default number of tests
20maxTestCount :: Int
21#ifdef DEVBUILD
22maxTestCount = 100
23#else
24maxTestCount = 10
25#endif
27allocOverhead :: Int
28allocOverhead = 2 * sizeOf (undefined :: Int)
30-- XXX this should be in sync with the defaultChunkSize in Array code, or we
31-- should expose that and use that. For fast testing we could reduce the
32-- defaultChunkSize under CPP conditionals.
33--
34defaultChunkSize :: Int
35defaultChunkSize = 32 * k - allocOverhead
36 where k = 1024
38maxArrLen :: Int
39maxArrLen = defaultChunkSize * 8
41testLength :: Property
42testLength =
43 forAll (choose (0, maxArrLen)) $ \len ->
44 forAll (vectorOf len (arbitrary :: Gen Int)) $ \list ->
45 monadicIO $ do
46 arr <- S.fold (A.writeN len)
47 $ S.fromList list
48 assert (A.length arr == len)
50testFromToStreamN :: Property
51testFromToStreamN =
52 forAll (choose (0, maxArrLen)) $ \len ->
53 forAll (vectorOf len (arbitrary :: Gen Int)) $ \list ->
54 monadicIO $ do
55 arr <- S.fold (A.writeN len)
56 $ S.fromList list
57 xs <- S.toList
58 $ S.unfold A.read arr
59 assert (xs == list)
61testToStreamRev :: Property
62testToStreamRev =
63 forAll (choose (0, maxArrLen)) $ \len ->
64 forAll (vectorOf len (arbitrary :: Gen Int)) $ \list ->
65 monadicIO $ do
66 arr <- S.fold (A.writeN len)
67 $ S.fromList list
68 xs <- S.toList
69 $ IA.toStreamRev arr
70 assert (xs == reverse list)
72testArraysOf :: Property
73testArraysOf =
74 forAll (choose (0, maxArrLen)) $ \len ->
75 forAll (vectorOf len (arbitrary :: Gen Int)) $ \list ->
76 monadicIO $ do
77 xs <- S.toList
78 $ S.concatUnfold A.read
79 $ IP.arraysOf 240
80 $ S.fromList list
81 assert (xs == list)
83testFlattenArrays :: Property
84testFlattenArrays =
85 forAll (choose (0, maxArrLen)) $ \len ->
86 forAll (vectorOf len (arbitrary :: Gen Int)) $ \list ->
87 monadicIO $ do
88 xs <- S.toList
89 $ S.concatUnfold A.read
90 $ IP.arraysOf 240
91 $ S.fromList list
92 assert (xs == list)
94testFromToStream :: Property
95testFromToStream =
96 forAll (choose (0, maxArrLen)) $ \len ->
97 forAll (vectorOf len (arbitrary :: Gen Int)) $ \list ->
98 monadicIO $ do
99 arr <- S.fold A.write $ S.fromList list
100 xs <- S.toList
101 $ S.unfold A.read arr
102 assert (xs == list)
104main :: IO ()
105main = hspec
106 $ H.parallel
107 $ modifyMaxSuccess (const maxTestCount)
108 $ do
109 describe "Construction" $ do
110 prop "length . writeN n === n" $ testLength
111 prop "read . writeN n === id" $ testFromToStreamN
112 prop "toStreamRev . write === reverse" $ testToStreamRev
113 prop "arraysOf concats to original" $ testArraysOf
114 prop "concats to original" $ testFlattenArrays
115 prop "read . write === id" $ testFromToStream