Skip to content

Commit cccfe28

Browse files
Add sequence search tests for signed, unit and Word64 elements
The tests are marked pending: packing a negative element into a word without masking it gives a false match, decoding a buffered element without sign extension fails for Int8, and the Karp-Rabin hash calls fromEnum, which fails on Word64 values above maxBound :: Int.
1 parent db0666a commit cccfe28

2 files changed

Lines changed: 140 additions & 1 deletion

File tree

‎test/Streamly/Test/Data/Fold/Combinators.hs‎

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@
1010
module Streamly.Test.Data.Fold.Combinators (main) where
1111

1212
import Data.IORef (newIORef, atomicModifyIORef)
13-
import Data.Int (Int64)
13+
import Data.Int (Int8, Int64)
1414
import Data.Semigroup (Sum(..))
15+
import Data.Word (Word8, Word64)
1516

1617
import qualified Prelude
1718
import qualified Streamly.Internal.Data.Array as Array
@@ -414,6 +415,56 @@ sampleFromthenOffset =
414415
(Stream.fromList [0..9 :: Int])
415416
`shouldReturn` [1,4,7]
416417

418+
-- The patterns of size at most one word are packed into a word, the elements
419+
-- with the top bit set must be masked and sign-extended correctly.
420+
takeEndBySeqSigned :: Expectation
421+
takeEndBySeqSigned = do
422+
Stream.fold
423+
(Fold.takeEndBySeq (Array.fromList [1,-1 :: Int8]) Fold.toList)
424+
(Stream.fromList [-1,-1,5])
425+
`shouldReturn` [-1,-1,5]
426+
Stream.fold
427+
(Fold.takeEndBySeq_ (Array.fromList [1,2 :: Int8]) Fold.toList)
428+
(Stream.fromList [-1,3,4])
429+
`shouldReturn` [-1,3,4]
430+
Stream.fold
431+
(Fold.takeEndBySeq_ (Array.fromList [1,2 :: Int8]) Fold.toList)
432+
(Stream.fromList [-1,-128,127,1,2,5])
433+
`shouldReturn` [-1,-128,127]
434+
Stream.fold
435+
(Fold.takeEndBySeq_ (Array.fromList [1,2 :: Word8]) Fold.toList)
436+
(Stream.fromList [200,255,1,2,5])
437+
`shouldReturn` [200,255]
438+
439+
-- Decoding the elements of a word pattern must not evaluate toEnum 1 for a
440+
-- type with a single value.
441+
takeEndBySeqUnit :: Expectation
442+
takeEndBySeqUnit = do
443+
Stream.fold
444+
(Fold.takeEndBySeq_ (Array.fromList [(), ()]) Fold.toList)
445+
(Stream.fromList [()])
446+
`shouldReturn` [()]
447+
Stream.fold
448+
(Fold.takeEndBySeq_ (Array.fromList [(), ()]) Fold.toList)
449+
(Stream.fromList [(), (), ()])
450+
`shouldReturn` []
451+
452+
-- Karp-Rabin search must not use fromEnum, it fails on large Word64 values.
453+
takeEndBySeqWord64 :: Expectation
454+
takeEndBySeqWord64 = do
455+
Stream.fold
456+
(Fold.takeEndBySeq (Array.fromList [1,2 :: Word64]) Fold.toList)
457+
(Stream.fromList [maxBound,7,1,2,9])
458+
`shouldReturn` [maxBound,7,1,2]
459+
Stream.fold
460+
(Fold.takeEndBySeq_ (Array.fromList [1,2 :: Word64]) Fold.toList)
461+
(Stream.fromList [maxBound,7,1,2,9])
462+
`shouldReturn` [maxBound,7]
463+
Stream.fold
464+
(Fold.takeEndBySeq_ (Array.fromList [maxBound,2,3 :: Word64]) Fold.toList)
465+
(Stream.fromList [5,maxBound,2,maxBound,2,3,9])
466+
`shouldReturn` [5,maxBound,2]
467+
417468
distributeScan :: Expectation
418469
distributeScan = do
419470
ref <- newIORef [Fold.take 2 Fold.sum, Fold.take 2 Fold.length :: Fold.Fold IO Int Int]
@@ -532,6 +583,9 @@ main = hspec $ do
532583
it "slide2" slide2
533584
it "takeEndBySeq" takeEndBySeq
534585
it "takeEndBySeq_" takeEndBySeq_
586+
xit "takeEndBySeq signed elements" takeEndBySeqSigned
587+
xit "takeEndBySeq unit" takeEndBySeqUnit
588+
xit "takeEndBySeq Word64" takeEndBySeqWord64
535589
it "distributeScan" distributeScan
536590
it "distributeScan last input" distributeScanLast
537591
it "sampleFromthen offset" sampleFromthenOffset

‎test/Streamly/Test/Data/Stream/Parse.hs‎

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import Control.Monad.IO.Class (MonadIO)
1919
import Data.Functor.Identity (Identity)
2020
import Data.List (group, intercalate)
2121
import Data.Semigroup (Sum(..), getSum)
22+
import Data.Int (Int8)
2223
import Data.Word (Word8, Word64)
2324
import Streamly.Internal.Data.Array (Array)
2425
import 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

9931073
moduleName :: 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

Comments
 (0)