-
Notifications
You must be signed in to change notification settings - Fork 0
/
suffixarray_test.go
82 lines (68 loc) · 1.83 KB
/
suffixarray_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package gostr_test // black box testing...
import (
"reflect"
"testing"
"github.com/mailund/gostr"
"github.com/mailund/gostr/test"
)
type SAAlgo = func(x string) []int32
var saAlgorithms = map[string]SAAlgo{
"Skew": gostr.Skew,
"Sais": gostr.Sais,
"SuffixTree": gostr.StSaConstruction,
}
func runBasicTest(algo SAAlgo) func(*testing.T) {
return func(t *testing.T) {
t.Helper()
tests := []struct {
name string
x string
wantSA []int32
}{
{`We handle empty strings`, "", []int32{0}},
{`Unique characters "a"`, "a", []int32{1, 0}},
{`Unique characters "ab"`, "ab", []int32{2, 0, 1}},
{`Unique characters "ba"`, "ba", []int32{2, 1, 0}},
{`Unique characters "abc"`, "abc", []int32{3, 0, 1, 2}},
{`Unique characters "bca"`, "bca", []int32{3, 2, 0, 1}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if gotSA := algo(tt.x); !reflect.DeepEqual(gotSA, tt.wantSA) {
t.Errorf("Got = %v, want %v", gotSA, tt.wantSA)
}
})
}
}
}
func Test_SuffixArraysBasic(t *testing.T) {
t.Helper()
for name, algo := range saAlgorithms {
t.Run(name, runBasicTest(algo))
}
}
func runConsistencyTest(algo SAAlgo) func(*testing.T) {
return func(t *testing.T) {
t.Helper()
rng := test.NewRandomSeed(t)
test.GenerateTestStrings(50, 150, rng,
func(x string) {
test.CheckSuffixArray(t, x, algo(x))
})
}
}
func Test_SuffixArraysConsistency(t *testing.T) {
for name, algo := range saAlgorithms {
t.Run(name, runConsistencyTest(algo))
}
}
func Test_AlphabetErrors(t *testing.T) {
alpha := gostr.NewAlphabet("foo")
x := "bar" // wrong alphabet
if _, err := gostr.SaisWithAlphabet(x, alpha); err == nil {
t.Error("Expected an error making Sais SA")
}
if _, err := gostr.SkewWithAlphabet(x, alpha); err == nil {
t.Error("Expected an error making Skew SA")
}
}