on 2e4b883 help sign in
106 lines 3.7 kB history raw
1{-# OPTIONS_GHC -Wno-deprecations #-}
3module Main (main) where
5import Data.Word (Word8)
6import Streamly.Test.Common (listEquals, chooseInt)
7import Test.Hspec (hspec, describe, shouldBe)
8import Test.Hspec.QuickCheck
9import Test.QuickCheck (forAll, Property, vectorOf, Gen, Arbitrary (arbitrary))
10import Test.QuickCheck.Monadic (monadicIO, run)
12import qualified Streamly.Internal.Data.Array as Array
13import qualified Streamly.Internal.Data.Stream.Chunked as ArrayStream
14import qualified Streamly.Internal.Data.Fold as Fold
15import qualified Streamly.Internal.Data.Parser as Parser
16import qualified Streamly.Internal.Data.Stream.IsStream as Stream
17import qualified Test.Hspec as Hspec
19import Prelude hiding (sequence)
21#if MIN_VERSION_QuickCheck(2,14,0)
23import Test.QuickCheck (chooseAny)
25#else
27import System.Random (Random(random))
28import Test.QuickCheck.Gen (Gen(MkGen))
30-- | Generates a random element over the natural range of `a`.
31chooseAny :: Random a => Gen a
32chooseAny = MkGen (\r _ -> let (x,_) = random r in x)
34#endif
36maxTestCount :: Int
37maxTestCount = 100
39parseBreak :: Property
40parseBreak = do
41 let len = 200
42 -- ls = input list (stream)
43 -- clen = chunk size
44 -- tlen = parser take size
45 forAll
46 ((,,)
47 <$> vectorOf len (chooseAny :: Gen Int)
48 <*> chooseInt (1, len)
49 <*> chooseInt (0, len))
50 $ \(ls, clen, tlen) ->
51 monadicIO $ do
52 (ls1, str) <-
53 let input =
54 Stream.chunksOf
55 clen (Array.writeN clen) (Stream.fromList ls)
56 parser = Parser.fromFold (Fold.take tlen Fold.toList)
57 in run $ ArrayStream.parseBreak parser input
58 ls2 <- run $ Stream.toList $ ArrayStream.concat str
59 listEquals (==) (ls1 ++ ls2) ls
61splitOnSuffix :: Word8 -> [Word8] -> [[Word8]] -> IO ()
62splitOnSuffix sep inp out = do
63 res <-
64 Stream.toList
65 $ ArrayStream.splitOnSuffix sep
66 $ Stream.chunksOf 2 (Array.writeN 2) $ Stream.fromList inp
67 fmap Array.toList res `shouldBe` out
69-------------------------------------------------------------------------------
70-- Main
71-------------------------------------------------------------------------------
73moduleName :: String
74moduleName = "Data.Array.Stream"
76-- Instead of hard coding 10000 here we can have maxStreamLength for operations
77-- that use stream of arrays.
79concatArrayW8 :: Property
80concatArrayW8 =
81 forAll (vectorOf 10000 (arbitrary :: Gen Word8))
82 $ \w8List -> do
83 let w8ArrList = Array.fromList . (: []) <$> w8List
84 f2 <- Stream.toList $ ArrayStream.concat $ Stream.fromList w8ArrList
85 w8List `shouldBe` f2
88main :: IO ()
89main =
90 hspec $
91 Hspec.parallel $
92 modifyMaxSuccess (const maxTestCount) $ do
93 describe moduleName $ do
94 describe "Stream parsing" $ do
95 prop "parseBreak" parseBreak
96 prop "concatArrayW8" concatArrayW8
97 describe "splifOnSuffix" $ do
98 Hspec.it "splitOnSuffix 0 [1, 2, 0, 4, 0, 5, 6]"
99 $ splitOnSuffix 0 [1, 2, 0, 4, 0, 5, 6]
100 [[1, 2], [4], [5, 6]]
101 Hspec.it "splitOnSuffix 0 [1, 2, 0, 4, 0, 5, 6, 0]"
102 $ splitOnSuffix 0 [1, 2, 0, 4, 0, 5, 6, 0]
103 [[1, 2], [4], [5, 6]]
104 Hspec.it "splitOnSuffix 0 [0, 1, 2, 0, 4, 0, 5, 6]"
105 $ splitOnSuffix 0 [0, 1, 2, 0, 4, 0, 5, 6]
106 [[], [1, 2], [4], [5, 6]]