@@ -1,22 +1,25 @@
1module Main (main) where
1module Main (main) where
3+import Control.Exception (SomeException(..), displayException)
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, elements, Property, property, listOf,
12+ vectorOf, counterexample, Gen, chooseAny)
13+import Test.QuickCheck.Monadic (monadicIO, PropertyM, assert, monitor, run)
15+import Prelude hiding (sequence)
3import qualified Streamly.Internal.Data.Parser as P
17import qualified Streamly.Internal.Data.Parser as P
4import qualified Streamly.Internal.Data.Stream.IsStream as S
18import qualified Streamly.Internal.Data.Stream.IsStream as S
5import qualified Streamly.Internal.Data.Fold as FL
19import qualified Streamly.Internal.Data.Fold as FL
6−
20+import qualified Streamly.Internal.Memory.Array as A
7−-- import Data.List (partition)
9−import Test.Hspec(Spec, hspec, describe)
10import qualified Test.Hspec as H
21import qualified Test.Hspec as H
11−import Test.Hspec.QuickCheck
22+import qualified Prelude
12−import Test.QuickCheck (arbitrary, forAll, choose, elements, Property,
13− property, listOf, vectorOf, counterexample, Gen)
15−import Test.QuickCheck.Monadic (monadicIO, PropertyM, assert, monitor)
16−import Control.Exception (SomeException(..), displayException)
17−import Control.Monad.IO.Class (MonadIO(..))
18−import Control.Monad (when)
19−import Data.List ((\\))
21maxTestCount :: Int
24maxTestCount :: Int
22maxTestCount = 100
25maxTestCount = 100
@@ -451,7 +454,7 @@ sliceSepByMax =
451-- Left _ -> True)
454-- Left _ -> True)
453many :: Property
456many :: Property
455 forAll (listOf (chooseInt (0, 1))) $ \ls ->
458 forAll (listOf (chooseInt (0, 1))) $ \ls ->
457 concatFold = FL.Fold (\concatList curr_list -> return $ concatList ++ curr_list) (return []) return
460 concatFold = FL.Fold (\concatList curr_list -> return $ concatList ++ curr_list) (return []) return
@@ -462,13 +465,13 @@ many =
462 Left _ -> property False
465 Left _ -> property False
464-- many_empty :: Property
467-- many_empty :: Property
465−-- many_empty =
468+-- many_empty =
466-- property (case S.parse (P.many FL.toList (P.die "die")) (S.fromList [1 :: Int]) of
469-- property (case S.parse (P.many FL.toList (P.die "die")) (S.fromList [1 :: Int]) of
467-- Right res_list -> checkListEqual res_list ([] :: [Int])
470-- Right res_list -> checkListEqual res_list ([] :: [Int])
468-- Left _ -> property False)
471-- Left _ -> property False)
470some :: Property
473some :: Property
472 forAll (listOf (chooseInt (0, 1))) $ \genLs ->
475 forAll (listOf (chooseInt (0, 1))) $ \genLs ->
474 ls = 0 : genLs
477 ls = 0 : genLs
@@ -480,16 +483,152 @@ some =
480 Left _ -> False
483 Left _ -> False
482-- someFail :: Property
485-- someFail :: Property
483−-- someFail =
486+-- someFail =
484-- property (case S.parse (P.some FL.toList (P.die "die")) (S.fromList [1 :: Int]) of
487-- property (case S.parse (P.some FL.toList (P.die "die")) (S.fromList [1 :: Int]) of
485-- Right _ -> False
488-- Right _ -> False
486-- Left _ -> True)
489-- Left _ -> True)
491+-------------------------------------------------------------------------------
493+-------------------------------------------------------------------------------
495+applicative :: Property
497+ forAll (listOf (chooseAny :: Gen Int)) $ \ list1 ->
498+ forAll (listOf (chooseAny :: Gen Int)) $ \ list2 ->
501+ <$> P.take (length list1) FL.toList
502+ <*> P.take (length list2) FL.toList
505+ run $ S.parse parser (S.fromList $ list1 ++ list2)
506+ listEquals (==) olist1 list1
507+ listEquals (==) olist2 list2
511+ forAll (vectorOf 11 (listOf (chooseAny :: Gen Int))) $ \ ins ->
512+ let parsers = fmap (\xs -> P.take (length xs) FL.toList) ins
516+ (Prelude.sequence parsers)
517+ (S.fromList $ concat ins)
518+ listEquals (==) outs ins
522+ forAll (listOf (chooseAny :: Gen Int)) $ \ list1 ->
523+ forAll (listOf (chooseAny :: Gen Int)) $ \ list2 ->
525+ olist1 <- P.take (length list1) FL.toList
526+ olist2 <- P.take (length list2) FL.toList
527+ return (olist1, olist2)
530+ run $ S.parse parser (S.fromList $ list1 ++ list2)
531+ listEquals (==) olist1 list1
532+ listEquals (==) olist2 list2
534+-------------------------------------------------------------------------------
536+-------------------------------------------------------------------------------
538+parseMany :: Property
540+ forAll (chooseInt (1,100)) $ \len ->
541+ forAll (listOf (vectorOf len (chooseAny :: Gen Int))) $ \ ins ->
547+ (P.take len FL.toList) (S.fromList $ concat ins)
549+ listEquals (==) outs ins
551+-------------------------------------------------------------------------------
552+-- Test for a particular case hit during fs events testing
553+-------------------------------------------------------------------------------
556+evId = [96,238,17,9,0,0,0,0]
559+evFlags = [0,4,1,0,0,0,0,0]
562+evPathLen = [71,0,0,0,0,0,0,0]
566+ [47,85,115,101,114,115,47,118,111,108,47,118,101,109,98,97,47,99,111,109
567+ ,112,111,115,101,119,101 ,108,108,45,116,101,99,104,47,69,110,103,47,112
568+ ,114,111,106,101,99,116,115,47,115,116,114,101,97,109,108,121,47,115,116
569+ ,114,101,97,109,108,121,47,116,109,112,47,122,122
573+event = evId ++ evFlags ++ evPathLen ++ evPath
577+ , eventFlags :: Word32
578+ , eventAbsPath :: A.Array Word8
579+ } deriving (Show, Ord, Eq)
581+readOneEvent :: P.Parser IO Word8 Event
583+ arr <- P.takeEQ 24 (A.writeN 24)
584+ let arr1 = A.unsafeCast arr :: A.Array Word64
585+ eid = A.unsafeIndex arr1 0
586+ eflags = A.unsafeIndex arr1 1
587+ pathLen = fromIntegral $ A.unsafeIndex arr1 2
588+ -- XXX handle if pathLen is 0
589+ path <- P.takeEQ pathLen (A.writeN pathLen)
592+ , eventFlags = fromIntegral eflags
593+ , eventAbsPath = path
596+parseMany2Events :: Property
602+ $ S.parseMany readOneEvent
603+ $ S.fromList (concat (replicate 2 event))
605+ assert (length xs == 2)
606+ -- XXX assuming little endian machine
608+ { eventId = 152170080
609+ , eventFlags = 66560
610+ , eventAbsPath = A.fromList evPath
612+ in listEquals (==) xs (replicate 2 ev)
614+-------------------------------------------------------------------------------
616+-------------------------------------------------------------------------------
488main :: IO ()
618main :: IO ()
491 H.parallel $
621 H.parallel $
492 modifyMaxSuccess (const maxTestCount) $ do
622 modifyMaxSuccess (const maxTestCount) $ do
623+ describe "Instances" $ do
624+ prop "applicative" applicative
626+ prop "sequence" sequence
628+ describe "Stream parsing" $ do
629+ prop "parseMany" parseMany
630+ prop "parseMany2Events" parseMany2Events
493 describe "test for accumulator" $ do
632 describe "test for accumulator" $ do
494 prop "P.fromFold FL.sum = FL.sum" fromFold
633 prop "P.fromFold FL.sum = FL.sum" fromFold
495 prop "P.any = Prelude.any" Main.any
634 prop "P.any = Prelude.any" Main.any