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)
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
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.
43defaultChunkSize :: Int
44defaultChunkSize = 32 * k - allocOverhead
48maxArrLen = defaultChunkSize * 8
51 (Int -> SerialT IO Int -> IO (Array Int))
53genericTestFrom arrFold =
54 forAll (choose (0, maxArrLen)) $ \len ->
55 forAll (vectorOf len (arbitrary :: Gen Int)) $ \list ->
57 arr <- run $ arrFold len $ S.fromList list
58 assert (A.length arr == len)
61testLength = genericTestFrom (\n -> S.fold (A.writeN n))
63testLengthFromStreamN :: Property
64testLengthFromStreamN = genericTestFrom A.fromStreamN
66testLengthFromStream :: Property
67testLengthFromStream = genericTestFrom (const A.fromStream)
70 (Int -> SerialT IO Int -> IO (Array Int))
71 -> (Array Int -> SerialT IO Int)
72 -> ([Int] -> [Int] -> Bool)
74genericTestFromTo arrFold arrUnfold listEq =
75 forAll (choose (0, maxArrLen)) $ \len ->
76 forAll (vectorOf len (arbitrary :: Gen Int)) $ \list ->
78 arr <- run $ arrFold len $ S.fromList list
79 xs <- run $ S.toList $ arrUnfold arr
80 assert (listEq xs list)
82testFoldNUnfold :: Property
84 genericTestFromTo (\n -> S.fold (A.writeN n)) (S.unfold A.read) (==)
86testFoldNToStream :: Property
88 genericTestFromTo (\n -> S.fold (A.writeN n)) A.toStream (==)
90testFoldNToStreamRev :: Property
93 (\n -> S.fold (A.writeN n))
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) (==)
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
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