summaryrefslogtreecommitdiff
path: root/lib/diskio/kmp_test.go
diff options
context:
space:
mode:
authorLuke Shumaker <lukeshu@lukeshu.com>2022-07-17 11:54:49 -0600
committerLuke Shumaker <lukeshu@lukeshu.com>2022-07-17 11:54:49 -0600
commit4952fc1880bf0f4286b17cbfbe0c49a132d09ebc (patch)
tree38052a3f6119d70216d796bcce65e8f1fb984ab2 /lib/diskio/kmp_test.go
parent987a4b238e047238bd83384c87b8317afdd45ad8 (diff)
implement wildcards in the KMP IndexAll
Diffstat (limited to 'lib/diskio/kmp_test.go')
-rw-r--r--lib/diskio/kmp_test.go54
1 files changed, 54 insertions, 0 deletions
diff --git a/lib/diskio/kmp_test.go b/lib/diskio/kmp_test.go
index 51c7b5e..59b6224 100644
--- a/lib/diskio/kmp_test.go
+++ b/lib/diskio/kmp_test.go
@@ -6,6 +6,7 @@ package diskio
import (
"bytes"
+ "io"
"testing"
"github.com/stretchr/testify/assert"
@@ -65,3 +66,56 @@ func FuzzIndexAll(f *testing.F) {
assert.Equal(t, exp, act)
})
}
+
+type RESeq string
+
+func (re RESeq) Get(i int64) (byte, error) {
+ if i < 0 || i >= int64(len(re)) {
+ return 0, io.EOF
+ }
+ chr := re[int(i)]
+ if chr == '.' {
+ return 0, ErrWildcard
+ }
+ return chr, nil
+}
+
+func TestKMPWildcard(t *testing.T) {
+ type testcase struct {
+ InStr string
+ InSubstr string
+ ExpMatches []int64
+ }
+ testcases := map[string]testcase{
+ "trivial-bar": {
+ InStr: "foo_bar",
+ InSubstr: "foo.ba.",
+ ExpMatches: []int64{0},
+ },
+ "trival-baz": {
+ InStr: "foo-baz",
+ InSubstr: "foo.ba.",
+ ExpMatches: []int64{0},
+ },
+ "suffix": {
+ InStr: "foobarbaz",
+ InSubstr: "...baz",
+ ExpMatches: []int64{3},
+ },
+ "overlap": {
+ InStr: "foobarbar",
+ InSubstr: "...bar",
+ ExpMatches: []int64{0, 3},
+ },
+ }
+ for tcName, tc := range testcases {
+ tc := tc
+ t.Run(tcName, func(t *testing.T) {
+ matches, err := IndexAll[int64, byte](
+ StringSequence[int64](tc.InStr),
+ RESeq(tc.InSubstr))
+ assert.NoError(t, err)
+ assert.Equal(t, tc.ExpMatches, matches)
+ })
+ }
+}