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