Add tests for Streamly.Data.SmallArray.

This commit is contained in:
Pranay Sashank 2019-12-04 16:24:24 +05:30
parent d73f65cd76
commit ee32239753
2 changed files with 129 additions and 0 deletions

View File

@ -481,6 +481,22 @@ test-suite unpinned-array-test
transformers >= 0.4 && < 0.6
default-language: Haskell2010
test-suite smallarray-test
import: test-options
type: exitcode-stdio-1.0
main-is: Streamly/Test/Data/SmallArray.hs
js-sources: jsbits/clock.js
hs-source-dirs: test
build-depends:
streamly
, base >= 4.8 && < 5
, QuickCheck >= 2.10 && < 2.14
, hspec >= 2.0 && < 3
if impl(ghc < 8.0)
build-depends:
transformers >= 0.4 && < 0.6
default-language: Haskell2010
test-suite string-test
import: test-options
type: exitcode-stdio-1.0

View File

@ -0,0 +1,113 @@
-- |
-- Module : Main
-- Copyright : (c) 2019 Composewell Technologies
--
-- License : BSD-3-Clause
-- Maintainer : streamly@composewell.com
-- Stability : experimental
-- Portability : GHC
--
{-# LANGUAGE CPP #-}
module Main (main) where
import Foreign.Storable (Storable(..))
import Test.Hspec.QuickCheck
import Test.QuickCheck (Property, forAll, Gen, vectorOf, arbitrary, choose)
import Test.QuickCheck.Monadic (monadicIO, assert, run)
import Test.Hspec as H
import Streamly (SerialT)
import Streamly.Data.SmallArray (SmallArray)
import qualified Streamly.Data.SmallArray as A
import qualified Streamly.Prelude as S
-- Coverage build takes too long with default number of tests
maxTestCount :: Int
#ifdef DEVBUILD
maxTestCount = 100
#else
maxTestCount = 10
#endif
allocOverhead :: Int
allocOverhead = 2 * sizeOf (undefined :: Int)
-- XXX this should be in sync with the defaultChunkSize in Array code, or we
-- should expose that and use that. For fast testing we could reduce the
-- defaultChunkSize under CPP conditionals.
--
defaultChunkSize :: Int
defaultChunkSize = 32 * k - allocOverhead
where k = 1024
maxArrLen :: Int
maxArrLen = defaultChunkSize * 8
genericTestFrom ::
(Int -> SerialT IO Int -> IO (SmallArray Int))
-> Property
genericTestFrom arrFold =
forAll (choose (0, maxArrLen)) $ \len ->
forAll (vectorOf len (arbitrary :: Gen Int)) $ \list ->
monadicIO $ do
arr <- run $ arrFold len $ S.fromList list
assert (A.length arr == len)
testLength :: Property
testLength = genericTestFrom (\n -> S.fold (A.writeN n))
testLengthFromStreamN :: Property
testLengthFromStreamN = genericTestFrom A.fromStreamN
genericTestFromTo ::
(Int -> SerialT IO Int -> IO (SmallArray Int))
-> (SmallArray Int -> SerialT IO Int)
-> ([Int] -> [Int] -> Bool)
-> Property
genericTestFromTo arrFold arrUnfold listEq =
forAll (choose (0, maxArrLen)) $ \len ->
forAll (vectorOf len (arbitrary :: Gen Int)) $ \list ->
monadicIO $ do
arr <- run $ arrFold len $ S.fromList list
xs <- run $ S.toList $ arrUnfold arr
assert (listEq xs list)
testFoldNUnfold :: Property
testFoldNUnfold =
genericTestFromTo (\n -> S.fold (A.writeN n)) (S.unfold A.read) (==)
testFoldNToStream :: Property
testFoldNToStream =
genericTestFromTo (\n -> S.fold (A.writeN n)) A.toStream (==)
testFoldNToStreamRev :: Property
testFoldNToStreamRev =
genericTestFromTo
(\n -> S.fold (A.writeN n))
A.toStreamRev
(\xs list -> xs == reverse list)
testFromStreamNUnfold :: Property
testFromStreamNUnfold = genericTestFromTo A.fromStreamN (S.unfold A.read) (==)
testFromStreamNToStream :: Property
testFromStreamNToStream = genericTestFromTo A.fromStreamN A.toStream (==)
main :: IO ()
main =
hspec $
H.parallel $
modifyMaxSuccess (const maxTestCount) $
describe "Construction" $ do
prop "length . writeN n === n" testLength
prop "length . fromStreamN n === n" testLengthFromStreamN
prop "read . writeN === id " testFoldNUnfold
prop "toStream . writeN === id" testFoldNToStream
prop "toStreamRev . writeN === reverse" testFoldNToStreamRev
prop "read . fromStreamN === id" testFromStreamNUnfold
prop "toStream . fromStreamN === id" testFromStreamNToStream