@@ -19,6 +19,7 @@ import Control.Monad.IO.Class (MonadIO)
1919import Data.Functor.Identity (Identity )
2020import Data.List (group , intercalate )
2121import Data.Semigroup (Sum (.. ), getSum )
22+ import Data.Int (Int8 )
2223import Data.Word (Word8 , Word64 )
2324import Streamly.Internal.Data.Array (Array )
2425import Streamly.Internal.Data.Fold (Fold )
@@ -988,6 +989,85 @@ testReplaceMatchesOverlap =
988989 (Stream. fromList " aaaa" ))
989990 `shouldReturn` " bb"
990991
992+ -------------------------------------------------------------------------------
993+ -- Sequence search on signed and wide elements
994+ -------------------------------------------------------------------------------
995+
996+ -- The patterns of size at most one word are packed into a word, the elements
997+ -- with the top bit set must be masked and sign-extended correctly.
998+ testSeqSearchSigned :: Expectation
999+ testSeqSearchSigned = do
1000+ let pat = Array. fromList [1 , 2 :: Int8 ]
1001+ input = [- 1 , - 128 , 1 , 2 , 127 , - 1 ]
1002+ Stream. toList
1003+ (Stream. takeEndBySeq (Array. fromList [1 , - 1 :: Int8 ])
1004+ (Stream. fromList [- 1 , - 1 , 5 ]))
1005+ `shouldReturn` [- 1 , - 1 , 5 ]
1006+ Stream. toList (Stream. takeEndBySeq_ pat (Stream. fromList [- 1 , 3 , 4 ]))
1007+ `shouldReturn` [- 1 , 3 , 4 ]
1008+ Stream. toList
1009+ (Stream. splitSepBySeq_ (Array. fromList [1 , - 1 :: Int8 ]) Fold. toList
1010+ (Stream. fromList [- 1 , - 1 , 5 ]))
1011+ `shouldReturn` [[- 1 , - 1 , 5 ]]
1012+ Stream. toList
1013+ (Stream. splitSepBySeq_ pat Fold. toList (Stream. fromList input))
1014+ `shouldReturn` [[- 1 , - 128 ], [127 , - 1 ]]
1015+ Stream. toList
1016+ (Stream. splitOnSuffixSeq False pat Fold. toList (Stream. fromList input))
1017+ `shouldReturn` [[- 1 , - 128 ], [127 , - 1 ]]
1018+ Stream. toList
1019+ (Stream. splitOnSuffixSeq True pat Fold. toList (Stream. fromList input))
1020+ `shouldReturn` [[- 1 , - 128 , 1 , 2 ], [127 , - 1 ]]
1021+ Stream. toList (Stream. dropMatches pat (Stream. fromList input))
1022+ `shouldReturn` [- 1 , - 128 , 127 , - 1 ]
1023+ Stream. toList
1024+ (Stream. replaceMatches pat (Array. fromList [- 5 ])
1025+ (Stream. fromList input))
1026+ `shouldReturn` [- 1 , - 128 , - 5 , 127 , - 1 ]
1027+ Stream. toList (Stream. dropSuffix pat (Stream. fromList [- 1 , - 128 , 1 , 2 ]))
1028+ `shouldReturn` [- 1 , - 128 ]
1029+ Stream. toList (Stream. dropSuffix pat (Stream. fromList [- 1 , - 128 ]))
1030+ `shouldReturn` [- 1 , - 128 ]
1031+
1032+ -- Decoding the elements of a word pattern must not evaluate toEnum 1 for a
1033+ -- type with a single value.
1034+ testSeqSearchUnit :: Expectation
1035+ testSeqSearchUnit = do
1036+ let pat = Array. fromList [() , () ]
1037+ Stream. toList (Stream. takeEndBySeq_ pat (Stream. fromList [() ]))
1038+ `shouldReturn` [() ]
1039+ Stream. toList (Stream. takeEndBySeq_ pat (Stream. fromList [() , () , () ]))
1040+ `shouldReturn` []
1041+ Stream. toList
1042+ (Stream. splitSepBySeq_ pat Fold. toList (Stream. fromList [() ]))
1043+ `shouldReturn` [[() ]]
1044+
1045+ -- Karp-Rabin search must not use fromEnum, it fails on large Word64 values.
1046+ testSeqSearchWord64 :: Expectation
1047+ testSeqSearchWord64 = do
1048+ let pat = Array. fromList [maxBound , 2 :: Word64 ]
1049+ input = [5 , maxBound , 2 , maxBound , 9 ]
1050+ Stream. toList
1051+ (Stream. takeEndBySeq (Array. fromList [1 , 2 :: Word64 ])
1052+ (Stream. fromList [maxBound , 7 , 1 , 2 , 9 ]))
1053+ `shouldReturn` [maxBound , 7 , 1 , 2 ]
1054+ Stream. toList
1055+ (Stream. takeEndBySeq_ (Array. fromList [1 , 2 :: Word64 ])
1056+ (Stream. fromList [maxBound , 7 , 1 , 2 , 9 ]))
1057+ `shouldReturn` [maxBound , 7 ]
1058+ Stream. toList
1059+ (Stream. splitSepBySeq_ pat Fold. toList (Stream. fromList input))
1060+ `shouldReturn` [[5 ], [maxBound , 9 ]]
1061+ Stream. toList
1062+ (Stream. splitOnSuffixSeq False pat Fold. toList (Stream. fromList input))
1063+ `shouldReturn` [[5 ], [maxBound , 9 ]]
1064+ Stream. toList (Stream. dropMatches pat (Stream. fromList input))
1065+ `shouldReturn` [5 , maxBound , 9 ]
1066+ Stream. toList
1067+ (Stream. replaceMatches pat (Array. fromList [0 ])
1068+ (Stream. fromList input))
1069+ `shouldReturn` [5 , 0 , maxBound , 9 ]
1070+
9911071-------------------------------------------------------------------------------
9921072
9931073moduleName :: String
@@ -1103,3 +1183,8 @@ main = hspec
11031183 it " replaces with longer replacement" testReplaceMatchesLonger
11041184 it " replaces with empty (drops)" testReplaceMatchesEmptyRepl
11051185 it " overlapping pattern" testReplaceMatchesOverlap
1186+
1187+ describe " sequence search" $ do
1188+ xit " signed elements" testSeqSearchSigned
1189+ xit " unit elements" testSeqSearchUnit
1190+ xit " Word64 elements" testSeqSearchWord64
0 commit comments