5-- Copyright : (c) 2019 Composewell Technologies
7-- License : BSD-3-Clause
8-- Maintainer : streamly@composewell.com
9-- Stability : experimental
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)
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
35import qualified Streamly.Data.Array as A
39-- Coverage build takes too long with default number of tests
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.
54defaultChunkSize :: Int
55defaultChunkSize = 32 * k - allocOverhead
59maxArrLen = defaultChunkSize * 8
62 (Int -> SerialT IO Int -> IO (Array Int))
64genericTestFrom arrFold =
65 forAll (choose (0, maxArrLen)) $ \len ->
66 forAll (vectorOf len (arbitrary :: Gen Int)) $ \list ->
68 arr <- run $ arrFold len $ S.fromList list
69 assert (A.length arr == len)
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)
83 (Int -> SerialT IO Int -> IO (Array Int))
84 -> (Array Int -> SerialT IO Int)
85 -> ([Int] -> [Int] -> Bool)
87genericTestFromTo arrFold arrUnfold listEq =
88 forAll (choose (0, maxArrLen)) $ \len ->
89 forAll (vectorOf len (arbitrary :: Gen Int)) $ \list ->
91 arr <- run $ arrFold len $ S.fromList list
92 xs <- run $ S.toList $ arrUnfold arr
93 assert (listEq xs list)
95testFoldNUnfold :: Property
97 genericTestFromTo (\n -> S.fold (A.writeN n)) (S.unfold A.read) (==)
99testFoldNToStream :: Property
101 genericTestFromTo (\n -> S.fold (A.writeN n)) A.toStream (==)
103testFoldNToStreamRev :: Property
104testFoldNToStreamRev =
106 (\n -> S.fold (A.writeN n))
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) (==)
125testArraysOf :: Property
127 forAll (choose (0, maxArrLen)) $ \len ->
128 forAll (vectorOf len (arbitrary :: Gen Int)) $ \list ->
131 $ S.concatUnfold A.read
136lastN :: Int -> [a] -> [a]
137lastN n l = drop (length l - n) l
141 forAll (choose (0, maxArrLen)) $ \len ->
142 forAll (choose (0, len)) $ \n ->
143 forAll (vectorOf len (arbitrary :: Gen Int)) $ \list ->
148 assert (xs == lastN n list)
155 modifyMaxSuccess (const maxTestCount) $ do
156 describe "Construction" $ do
157 prop "length . writeN n === n" testLength
158 prop "length . fromStreamN n === n" testLengthFromStreamN
159 prop "read . writeN === id " testFoldNUnfold
160 prop "toStream . writeN === id" testFoldNToStream
161 prop "toStreamRev . writeN === reverse" testFoldNToStreamRev
162 prop "read . fromStreamN === id" testFromStreamNUnfold
163 prop "toStream . fromStreamN === id" testFromStreamNToStream
164#ifndef TEST_SMALL_ARRAY
165 prop "length . fromStream === n" testLengthFromStream
166 prop "toStream . fromStream === id" testFromStreamToStream
167 prop "read . write === id" testFoldUnfold
170 prop "arraysOf concats to original" testArraysOf
174 prop "lastN" $ testLastN
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list