on 666eb07 help sign in
112 lines 3.9 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.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 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 chunksOf
59 clen (Array.writeN clen) (Stream.fromList ls)
60 parser = Parser.fromFold (Fold.take tlen Fold.toList)
61 in run $ ArrayStream.parseBreak parser input
62 ls2 <- run $ Stream.fold Fold.toList (ArrayStream.concat str)
63 case ls1 of
64 Right x -> listEquals (==) (x ++ ls2) ls
65 Left _ -> assert False
67splitOnSuffix :: Word8 -> [Word8] -> [[Word8]] -> IO ()
68splitOnSuffix sep inp out = do
69 res <-
70 Stream.fold Fold.toList
71 $ ArrayStream.splitOnSuffix sep
72 $ chunksOf 2 (Array.writeN 2) $ Stream.fromList inp
73 fmap Array.toList res `shouldBe` out
75-------------------------------------------------------------------------------
76-- Main
77-------------------------------------------------------------------------------
79moduleName :: String
80moduleName = "Data.Array.Stream"
82-- Instead of hard coding 10000 here we can have maxStreamLength for operations
83-- that use stream of arrays.
85concatArrayW8 :: Property
86concatArrayW8 =
87 forAll (vectorOf 10000 (arbitrary :: Gen Word8))
88 $ \w8List -> do
89 let w8ArrList = Array.fromList . (: []) <$> w8List
90 f2 <- Stream.fold Fold.toList $ ArrayStream.concat $ Stream.fromList w8ArrList
91 w8List `shouldBe` f2
94main :: IO ()
95main =
96 hspec $
97 Hspec.parallel $
98 modifyMaxSuccess (const maxTestCount) $ do
99 describe moduleName $ do
100 describe "Stream parsing" $ do
101 prop "parseBreak" parseBreak
102 prop "concatArrayW8" concatArrayW8
103 describe "splifOnSuffix" $ do
104 Hspec.it "splitOnSuffix 0 [1, 2, 0, 4, 0, 5, 6]"
105 $ splitOnSuffix 0 [1, 2, 0, 4, 0, 5, 6]
106 [[1, 2], [4], [5, 6]]
107 Hspec.it "splitOnSuffix 0 [1, 2, 0, 4, 0, 5, 6, 0]"
108 $ splitOnSuffix 0 [1, 2, 0, 4, 0, 5, 6, 0]
109 [[1, 2], [4], [5, 6]]
110 Hspec.it "splitOnSuffix 0 [0, 1, 2, 0, 4, 0, 5, 6]"
111 $ splitOnSuffix 0 [0, 1, 2, 0, 4, 0, 5, 6]
112 [[], [1, 2], [4], [5, 6]]