on master help sign in
Add tests for Streamly.Data.SmallArray.
commitee32239753a515f2127f9fff053c12f5f38332b7 authorPranay Sashank <pranaysashank@gmail.com> dateWed 4 Dec 2019, 16:24 (7 years ago) parentd73f65c
2 files changed+129
streamly.cabal+16 test/Streamly/Test/Data/SmallArray.hs+113
streamly.cabal+16
@@ -481,6 +481,22 @@ test-suite unpinned-array-test
481481 transformers >= 0.4 && < 0.6
482482 default-language: Haskell2010
483483
484+test-suite smallarray-test
485+ import: test-options
486+ type: exitcode-stdio-1.0
487+ main-is: Streamly/Test/Data/SmallArray.hs
488+ js-sources: jsbits/clock.js
489+ hs-source-dirs: test
490+ build-depends:
491+ streamly
492+ , base >= 4.8 && < 5
493+ , QuickCheck >= 2.10 && < 2.14
494+ , hspec >= 2.0 && < 3
495+ if impl(ghc < 8.0)
496+ build-depends:
497+ transformers >= 0.4 && < 0.6
498+ default-language: Haskell2010
499+
484500test-suite string-test
485501 import: test-options
486502 type: exitcode-stdio-1.0
test/Streamly/Test/Data/SmallArray.hsnew file+113
@@ -0,0 +1,113 @@
1+-- |
2+-- Module : Main
3+-- Copyright : (c) 2019 Composewell Technologies
4+--
5+-- License : BSD-3-Clause
6+-- Maintainer : streamly@composewell.com
7+-- Stability : experimental
8+-- Portability : GHC
9+--
10+
11+{-# LANGUAGE CPP #-}
12+
13+module Main (main) where
14+
15+import Foreign.Storable (Storable(..))
16+
17+import Test.Hspec.QuickCheck
18+import Test.QuickCheck (Property, forAll, Gen, vectorOf, arbitrary, choose)
19+import Test.QuickCheck.Monadic (monadicIO, assert, run)
20+
21+import Test.Hspec as H
22+
23+import Streamly (SerialT)
24+import Streamly.Data.SmallArray (SmallArray)
25+
26+import qualified Streamly.Data.SmallArray as A
27+import qualified Streamly.Prelude as S
28+
29+-- Coverage build takes too long with default number of tests
30+maxTestCount :: Int
31+#ifdef DEVBUILD
32+maxTestCount = 100
33+#else
34+maxTestCount = 10
35+#endif
36+
37+allocOverhead :: Int
38+allocOverhead = 2 * sizeOf (undefined :: Int)
39+
40+-- XXX this should be in sync with the defaultChunkSize in Array code, or we
41+-- should expose that and use that. For fast testing we could reduce the
42+-- defaultChunkSize under CPP conditionals.
43+--
44+defaultChunkSize :: Int
45+defaultChunkSize = 32 * k - allocOverhead
46+ where k = 1024
47+
48+maxArrLen :: Int
49+maxArrLen = defaultChunkSize * 8
50+
51+genericTestFrom ::
52+ (Int -> SerialT IO Int -> IO (SmallArray Int))
53+ -> Property
54+genericTestFrom arrFold =
55+ forAll (choose (0, maxArrLen)) $ \len ->
56+ forAll (vectorOf len (arbitrary :: Gen Int)) $ \list ->
57+ monadicIO $ do
58+ arr <- run $ arrFold len $ S.fromList list
59+ assert (A.length arr == len)
60+
61+testLength :: Property
62+testLength = genericTestFrom (\n -> S.fold (A.writeN n))
63+
64+testLengthFromStreamN :: Property
65+testLengthFromStreamN = genericTestFrom A.fromStreamN
66+
67+genericTestFromTo ::
68+ (Int -> SerialT IO Int -> IO (SmallArray Int))
69+ -> (SmallArray Int -> SerialT IO Int)
70+ -> ([Int] -> [Int] -> Bool)
71+ -> Property
72+genericTestFromTo arrFold arrUnfold listEq =
73+ forAll (choose (0, maxArrLen)) $ \len ->
74+ forAll (vectorOf len (arbitrary :: Gen Int)) $ \list ->
75+ monadicIO $ do
76+ arr <- run $ arrFold len $ S.fromList list
77+ xs <- run $ S.toList $ arrUnfold arr
78+ assert (listEq xs list)
79+
80+testFoldNUnfold :: Property
81+testFoldNUnfold =
82+ genericTestFromTo (\n -> S.fold (A.writeN n)) (S.unfold A.read) (==)
83+
84+testFoldNToStream :: Property
85+testFoldNToStream =
86+ genericTestFromTo (\n -> S.fold (A.writeN n)) A.toStream (==)
87+
88+testFoldNToStreamRev :: Property
89+testFoldNToStreamRev =
90+ genericTestFromTo
91+ (\n -> S.fold (A.writeN n))
92+ A.toStreamRev
93+ (\xs list -> xs == reverse list)
94+
95+testFromStreamNUnfold :: Property
96+testFromStreamNUnfold = genericTestFromTo A.fromStreamN (S.unfold A.read) (==)
97+
98+testFromStreamNToStream :: Property
99+testFromStreamNToStream = genericTestFromTo A.fromStreamN A.toStream (==)
100+
101+main :: IO ()
102+main =
103+ hspec $
104+ H.parallel $
105+ modifyMaxSuccess (const maxTestCount) $
106+ describe "Construction" $ do
107+ prop "length . writeN n === n" testLength
108+ prop "length . fromStreamN n === n" testLengthFromStreamN
109+ prop "read . writeN === id " testFoldNUnfold
110+ prop "toStream . writeN === id" testFoldNToStream
111+ prop "toStreamRev . writeN === reverse" testFoldNToStreamRev
112+ prop "read . fromStreamN === id" testFromStreamNUnfold
113+ prop "toStream . fromStreamN === id" testFromStreamNToStream