on master help sign in
113 lines 4.0 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 (assert, monadicIO, run)
12import qualified Streamly.Internal.Data.Array as Array
13import qualified Streamly.Internal.Data.Array.Stream as ArrayStream
14import qualified Streamly.Internal.Data.Fold as Fold
15import qualified Streamly.Internal.Data.Parser as Parser
16import qualified Streamly.Internal.Data.Stream 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
39chunksOf :: Monad m
40 => Int -> Fold.Fold m a b -> Stream.Stream m a -> Stream.Stream m b
41chunksOf n f = Stream.foldMany (Fold.take n f)
43parseBreak :: Property
44parseBreak = do
45 let len = 200
46 -- ls = input list (stream)
47 -- clen = chunk size
48 -- tlen = parser take size
49 forAll
50 ((,,)
51 <$> vectorOf len (chooseAny :: Gen Int)
52 <*> chooseInt (1, len)
53 <*> chooseInt (0, len))
54 $ \(ls, clen, tlen) ->
55 monadicIO $ do
56 (ls1, str) <-
57 let input =
58 Stream.toStreamK
59 $ chunksOf
60 clen (Array.writeN clen) (Stream.fromList ls)
61 parser = Parser.fromFold (Fold.take tlen Fold.toList)
62 in run $ ArrayStream.parseBreak parser input
63 ls2 <- run $ Stream.fold Fold.toList (ArrayStream.concat $ Stream.fromStreamK str)
64 case ls1 of
65 Right x -> listEquals (==) (x ++ ls2) ls
66 Left _ -> assert False
68splitOnSuffix :: Word8 -> [Word8] -> [[Word8]] -> IO ()
69splitOnSuffix sep inp out = do
70 res <-
71 Stream.fold Fold.toList
72 $ ArrayStream.splitOnSuffix sep
73 $ chunksOf 2 (Array.writeN 2) $ Stream.fromList inp
74 fmap Array.toList res `shouldBe` out
76-------------------------------------------------------------------------------
77-- Main
78-------------------------------------------------------------------------------
80moduleName :: String
81moduleName = "Data.Array.Stream"
83-- Instead of hard coding 10000 here we can have maxStreamLength for operations
84-- that use stream of arrays.
86concatArrayW8 :: Property
87concatArrayW8 =
88 forAll (vectorOf 10000 (arbitrary :: Gen Word8))
89 $ \w8List -> do
90 let w8ArrList = Array.fromList . (: []) <$> w8List
91 f2 <- Stream.fold Fold.toList $ ArrayStream.concat $ Stream.fromList w8ArrList
92 w8List `shouldBe` f2
95main :: IO ()
96main =
97 hspec $
98 Hspec.parallel $
99 modifyMaxSuccess (const maxTestCount) $ do
100 describe moduleName $ do
101 describe "Stream parsing" $ do
102 prop "parseBreak" parseBreak
103 prop "concatArrayW8" concatArrayW8
104 describe "splifOnSuffix" $ do
105 Hspec.it "splitOnSuffix 0 [1, 2, 0, 4, 0, 5, 6]"
106 $ splitOnSuffix 0 [1, 2, 0, 4, 0, 5, 6]
107 [[1, 2], [4], [5, 6]]
108 Hspec.it "splitOnSuffix 0 [1, 2, 0, 4, 0, 5, 6, 0]"
109 $ splitOnSuffix 0 [1, 2, 0, 4, 0, 5, 6, 0]
110 [[1, 2], [4], [5, 6]]
111 Hspec.it "splitOnSuffix 0 [0, 1, 2, 0, 4, 0, 5, 6]"
112 $ splitOnSuffix 0 [0, 1, 2, 0, 4, 0, 5, 6]
113 [[], [1, 2], [4], [5, 6]]