summaryrefslogtreecommitdiff
path: root/lib/diskio/kmp_test.go
diff options
context:
space:
mode:
Diffstat (limited to 'lib/diskio/kmp_test.go')
-rw-r--r--lib/diskio/kmp_test.go29
1 files changed, 17 insertions, 12 deletions
diff --git a/lib/diskio/kmp_test.go b/lib/diskio/kmp_test.go
index 836605a..51c7b5e 100644
--- a/lib/diskio/kmp_test.go
+++ b/lib/diskio/kmp_test.go
@@ -9,17 +9,19 @@ import (
"testing"
"github.com/stretchr/testify/assert"
+ "github.com/stretchr/testify/require"
)
func TestBuildKMPTable(t *testing.T) {
- substr := []byte("ababaa")
- table := buildKMPTable(substr)
- assert.Equal(t,
- []int{0, 0, 1, 2, 3, 1},
+ substr := SliceSequence[int64, byte]([]byte("ababaa"))
+ table, err := buildKMPTable[int64, byte](substr)
+ require.NoError(t, err)
+ require.Equal(t,
+ []int64{0, 0, 1, 2, 3, 1},
table)
for j, val := range table {
matchLen := j + 1
- assert.Equalf(t, substr[:val], substr[matchLen-val:matchLen],
+ assert.Equalf(t, substr[:val], substr[matchLen-int(val):matchLen],
"for table[%d]=%d", j, val)
}
}
@@ -27,17 +29,18 @@ func TestBuildKMPTable(t *testing.T) {
func FuzzBuildKMPTable(f *testing.F) {
f.Add([]byte("ababaa"))
f.Fuzz(func(t *testing.T, substr []byte) {
- table := buildKMPTable(substr)
- assert.Equal(t, len(substr), len(table), "length")
+ table, err := buildKMPTable[int64, byte](SliceSequence[int64, byte](substr))
+ require.NoError(t, err)
+ require.Equal(t, len(substr), len(table), "length")
for j, val := range table {
matchLen := j + 1
- assert.Equalf(t, substr[:val], substr[matchLen-val:matchLen],
+ assert.Equalf(t, substr[:val], substr[matchLen-int(val):matchLen],
"for table[%d]=%d", j, val)
}
})
}
-func NaiveFindAll(str, substr []byte) []int64 {
+func NaiveIndexAll(str, substr []byte) []int64 {
var matches []int64
for i := range str {
if bytes.HasPrefix(str[i:], substr) {
@@ -47,15 +50,17 @@ func NaiveFindAll(str, substr []byte) []int64 {
return matches
}
-func FuzzFindAll(f *testing.F) {
+func FuzzIndexAll(f *testing.F) {
f.Fuzz(func(t *testing.T, str, substr []byte) {
if len(substr) == 0 {
t.Skip()
}
t.Logf("str =%q", str)
t.Logf("substr=%q", substr)
- exp := NaiveFindAll(str, substr)
- act, err := FindAll[int64](bytes.NewReader(str), substr)
+ exp := NaiveIndexAll(str, substr)
+ act, err := IndexAll[int64, byte](
+ &ByteReaderSequence[int64]{R: bytes.NewReader(str)},
+ SliceSequence[int64, byte](substr))
assert.NoError(t, err)
assert.Equal(t, exp, act)
})