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.Internal.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
34#elif defined(TEST_PRIM_ARRAY)
35import qualified Streamly.Internal.Data.Prim.Array as A
36type Array = A.PrimArray
38import qualified Streamly.Internal.Data.Array as A
42-- Coverage build takes too long with default number of tests
51allocOverhead = 2 * sizeOf (undefined :: Int)
53-- XXX this should be in sync with the defaultChunkSize in Array code, or we
54-- should expose that and use that. For fast testing we could reduce the
55-- defaultChunkSize under CPP conditionals.
57defaultChunkSize :: Int
58defaultChunkSize = 32 * k - allocOverhead
62maxArrLen = defaultChunkSize * 8
65 (Int -> SerialT IO Int -> IO (Array Int))
67genericTestFrom arrFold =
68 forAll (choose (0, maxArrLen)) $ \len ->
69 forAll (vectorOf len (arbitrary :: Gen Int)) $ \list ->
71 arr <- run $ arrFold len $ S.fromList list
72 assert (A.length arr == len)
75testLength = genericTestFrom (\n -> S.fold (A.writeN n))
77testLengthFromStreamN :: Property
78testLengthFromStreamN = genericTestFrom A.fromStreamN
80#ifndef TEST_SMALL_ARRAY
81testLengthFromStream :: Property
82testLengthFromStream = genericTestFrom (const A.fromStream)
86 (Int -> SerialT IO Int -> IO (Array Int))
87 -> (Array Int -> SerialT IO Int)
88 -> ([Int] -> [Int] -> Bool)
90genericTestFromTo arrFold arrUnfold listEq =
91 forAll (choose (0, maxArrLen)) $ \len ->
92 forAll (vectorOf len (arbitrary :: Gen Int)) $ \list ->
94 arr <- run $ arrFold len $ S.fromList list
95 xs <- run $ S.toList $ arrUnfold arr
96 assert (listEq xs list)
98testFoldNUnfold :: Property
100 genericTestFromTo (\n -> S.fold (A.writeN n)) (S.unfold A.read) (==)
102testFoldNToStream :: Property
104 genericTestFromTo (\n -> S.fold (A.writeN n)) A.toStream (==)
106testFoldNToStreamRev :: Property
107testFoldNToStreamRev =
109 (\n -> S.fold (A.writeN n))
111 (\xs list -> xs == reverse list)
113testFromStreamNUnfold :: Property
114testFromStreamNUnfold = genericTestFromTo A.fromStreamN (S.unfold A.read) (==)
116testFromStreamNToStream :: Property
117testFromStreamNToStream = genericTestFromTo A.fromStreamN A.toStream (==)
119#ifndef TEST_SMALL_ARRAY
120testFromStreamToStream :: Property
121testFromStreamToStream = genericTestFromTo (const A.fromStream) A.toStream (==)
123testFoldUnfold :: Property
124testFoldUnfold = genericTestFromTo (const (S.fold A.write)) (S.unfold A.read) (==)
128testArraysOf :: Property
130 forAll (choose (0, maxArrLen)) $ \len ->
131 forAll (vectorOf len (arbitrary :: Gen Int)) $ \list ->
134 $ S.concatUnfold A.read
139lastN :: Int -> [a] -> [a]
140lastN n l = drop (length l - n) l
144 forAll (choose (0, maxArrLen)) $ \len ->
145 forAll (choose (0, len)) $ \n ->
146 forAll (vectorOf len (arbitrary :: Gen Int)) $ \list ->
151 assert (xs == lastN n list)
153testLastN_LN :: Int -> Int -> IO Bool
154testLastN_LN len n = do
156 l1 <- fmap A.toList $ S.fold (A.lastN n) $ S.fromList list
157 let l2 = lastN n list
165 modifyMaxSuccess (const maxTestCount) $ do
166 describe "Construction" $ do
167 prop "length . writeN n === n" testLength
168 prop "length . fromStreamN n === n" testLengthFromStreamN
169 prop "read . writeN === id " testFoldNUnfold
170 prop "toStream . writeN === id" testFoldNToStream
171 prop "toStreamRev . writeN === reverse" testFoldNToStreamRev
172 prop "read . fromStreamN === id" testFromStreamNUnfold
173 prop "toStream . fromStreamN === id" testFromStreamNToStream
174#ifndef TEST_SMALL_ARRAY
175 prop "length . fromStream === n" testLengthFromStream
176 prop "toStream . fromStream === id" testFromStreamToStream
177 prop "read . write === id" testFoldUnfold
180 prop "arraysOf concats to original" testArraysOf
184 prop "lastN : 0 <= n <= len" $ testLastN
185 describe "lastN boundary conditions" $ do
186 it "lastN -1" (testLastN_LN 10 (-1) `shouldReturn` True)
187 it "lastN 0" (testLastN_LN 10 0 `shouldReturn` True)
188 it "lastN length" (testLastN_LN 10 10 `shouldReturn` True)
189 it "lastN (length + 1)" (testLastN_LN 10 11 `shouldReturn` True)
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