on d2aaef0 help sign in
91 lines 3.2 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)
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"
74main :: IO ()
75main =
76 hspec $
77 Hspec.parallel $
78 modifyMaxSuccess (const maxTestCount) $ do
79 describe moduleName $ do
80 describe "Stream parsing" $ do
81 prop "parseBreak" parseBreak
82 describe "splifOnSuffix" $ do
83 Hspec.it "splitOnSuffix 0 [1, 2, 0, 4, 0, 5, 6]"
84 $ splitOnSuffix 0 [1, 2, 0, 4, 0, 5, 6]
85 [[1, 2], [4], [5, 6]]
86 Hspec.it "splitOnSuffix 0 [1, 2, 0, 4, 0, 5, 6, 0]"
87 $ splitOnSuffix 0 [1, 2, 0, 4, 0, 5, 6, 0]
88 [[1, 2], [4], [5, 6]]
89 Hspec.it "splitOnSuffix 0 [0, 1, 2, 0, 4, 0, 5, 6]"
90 $ splitOnSuffix 0 [0, 1, 2, 0, 4, 0, 5, 6]
91 [[], [1, 2], [4], [5, 6]]