@@ -1,23 +1,26 @@
1module Main (main) where
1module Main (main) where
3+import Control.Exception (SomeException(..))
4+import Control.Monad (when)
5+import Control.Monad.IO.Class (MonadIO(..))
6+import Data.List ((\\))
7+import Data.Word (Word8, Word32, Word64)
8+import Test.Hspec (Spec, hspec, describe)
9+import Test.Hspec.QuickCheck
10+import Test.QuickCheck
11+ (arbitrary, forAll, choose, chooseAny, elements, Property,
12+ property, listOf, vectorOf, counterexample, (.&&.), Gen)
13+import Test.QuickCheck.Monadic
14+ (monadicIO, PropertyM, assert, monitor, run)
3import qualified Streamly.Internal.Data.Parser.ParserD as P
16import qualified Streamly.Internal.Data.Parser.ParserD as P
4import qualified Streamly.Internal.Data.Stream.IsStream as S
17import qualified Streamly.Internal.Data.Stream.IsStream as S
5import qualified Streamly.Internal.Data.Fold as FL
18import qualified Streamly.Internal.Data.Fold as FL
6−
19+import qualified Streamly.Internal.Memory.Array as A
7−-- import Data.List (partition)
20+import qualified Prelude
9−import Test.Hspec(Spec, hspec, describe)
10import qualified Test.Hspec as H
21import qualified Test.Hspec as H
11−import Test.Hspec.QuickCheck
12−import Test.QuickCheck (arbitrary, forAll, choose, elements, Property,
13− property, listOf, vectorOf, counterexample,
16−import Test.QuickCheck.Monadic (monadicIO, PropertyM, assert, monitor)
23+import Prelude hiding (sequence)
17−import Control.Exception (SomeException(..))
18−import Control.Monad.IO.Class (MonadIO(..))
19−import Control.Monad (when)
20−import Data.List ((\\))
22maxTestCount :: Int
25maxTestCount :: Int
23maxTestCount = 100
26maxTestCount = 100
@@ -467,7 +470,7 @@ many_empty =
467 Left _ -> property False)
470 Left _ -> property False)
469some :: Property
472some :: Property
471 forAll (listOf (chooseInt (0, 1))) $ \genLs ->
474 forAll (listOf (chooseInt (0, 1))) $ \genLs ->
473 ls = 0 : genLs
476 ls = 0 : genLs
@@ -479,16 +482,151 @@ some =
479 Left _ -> False
482 Left _ -> False
481someFail :: Property
484someFail :: Property
482−someFail =
485+someFail =
483 property (case S.parseD (P.some FL.toList (P.die "die")) (S.fromList [1 :: Int]) of
486 property (case S.parseD (P.some FL.toList (P.die "die")) (S.fromList [1 :: Int]) of
484 Right _ -> False
487 Right _ -> False
485 Left _ -> True)
488 Left _ -> True)
490+-------------------------------------------------------------------------------
492+-------------------------------------------------------------------------------
494+applicative :: Property
496+ forAll (listOf (chooseAny :: Gen Int)) $ \ list1 ->
497+ forAll (listOf (chooseAny :: Gen Int)) $ \ list2 ->
500+ <$> P.take (length list1) FL.toList
501+ <*> P.take (length list2) FL.toList
504+ run $ S.parseD parser (S.fromList $ list1 ++ list2)
505+ listEquals (==) olist1 list1
506+ listEquals (==) olist2 list2
510+ forAll (vectorOf 11 (listOf (chooseAny :: Gen Int))) $ \ ins ->
511+ let parsers = fmap (\xs -> P.take (length xs) FL.toList) ins
515+ (Prelude.sequence parsers)
516+ (S.fromList $ concat ins)
517+ listEquals (==) outs ins
521+ forAll (listOf (chooseAny :: Gen Int)) $ \ list1 ->
522+ forAll (listOf (chooseAny :: Gen Int)) $ \ list2 ->
524+ olist1 <- P.take (length list1) FL.toList
525+ olist2 <- P.take (length list2) FL.toList
526+ return (olist1, olist2)
529+ run $ S.parseD parser (S.fromList $ list1 ++ list2)
530+ listEquals (==) olist1 list1
531+ listEquals (==) olist2 list2
533+-------------------------------------------------------------------------------
535+-------------------------------------------------------------------------------
537+parseMany :: Property
539+ forAll (chooseInt (1,100)) $ \len ->
540+ forAll (listOf (vectorOf len (chooseAny :: Gen Int))) $ \ ins ->
546+ (P.take len FL.toList) (S.fromList $ concat ins)
548+ listEquals (==) outs ins
550+-------------------------------------------------------------------------------
551+-- Test for a particular case hit during fs events testing
552+-------------------------------------------------------------------------------
555+evId = [96,238,17,9,0,0,0,0]
558+evFlags = [0,4,1,0,0,0,0,0]
561+evPathLen = [71,0,0,0,0,0,0,0]
565+ [47,85,115,101,114,115,47,118,111,108,47,118,101,109,98,97,47,99,111,109
566+ ,112,111,115,101,119,101 ,108,108,45,116,101,99,104,47,69,110,103,47,112
567+ ,114,111,106,101,99,116,115,47,115,116,114,101,97,109,108,121,47,115,116
568+ ,114,101,97,109,108,121,47,116,109,112,47,122,122
572+event = evId ++ evFlags ++ evPathLen ++ evPath
576+ , eventFlags :: Word32
577+ , eventAbsPath :: A.Array Word8
578+ } deriving (Show, Ord, Eq)
580+readOneEvent :: P.Parser IO Word8 Event
582+ arr <- P.takeEQ 24 (A.writeN 24)
583+ let arr1 = A.unsafeCast arr :: A.Array Word64
584+ eid = A.unsafeIndex arr1 0
585+ eflags = A.unsafeIndex arr1 1
586+ pathLen = fromIntegral $ A.unsafeIndex arr1 2
587+ path <- P.takeEQ pathLen (A.writeN pathLen)
590+ , eventFlags = fromIntegral eflags
591+ , eventAbsPath = path
594+parseMany2Events :: Property
600+ $ S.parseManyD readOneEvent
601+ $ S.fromList (concat (replicate 2 event))
603+ assert (length xs == 2)
604+ -- XXX assuming little endian machine
606+ { eventId = 152170080
607+ , eventFlags = 66560
608+ , eventAbsPath = A.fromList evPath
610+ in listEquals (==) xs (replicate 2 ev)
612+-------------------------------------------------------------------------------
614+-------------------------------------------------------------------------------
487main :: IO ()
616main :: IO ()
490 H.parallel $
619 H.parallel $
491 modifyMaxSuccess (const maxTestCount) $ do
620 modifyMaxSuccess (const maxTestCount) $ do
621+ describe "Instances" $ do
622+ prop "applicative" applicative
624+ prop "sequence" sequence
626+ describe "Stream parsing" $ do
627+ prop "parseMany" parseMany
628+ prop "parseMany2Events" parseMany2Events
492 describe "test for accumulator" $ do
630 describe "test for accumulator" $ do
493 prop "P.fromFold FL.sum = FL.sum" fromFold
631 prop "P.fromFold FL.sum = FL.sum" fromFold
494 prop "P.any = Prelude.any" Main.any
632 prop "P.any = Prelude.any" Main.any