-
Notifications
You must be signed in to change notification settings - Fork 0
/
gosaca_test.go
130 lines (115 loc) · 5.43 KB
/
gosaca_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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package gosaca
import (
"fmt"
"math/rand"
"testing"
)
func checkCorrectSuffixArray(input []byte, SA []int) error {
suffixesSeen := make(map[int]bool)
for i, s := range SA {
suffixesSeen[s] = true
// make sure suffix starting at SA[i] precedes suffix starting at SA[i+1]
if i+1 == len(SA) {
break
}
s1, s2 := SA[i], SA[i+1]
if s1 < 0 || s1 >= len(input) {
return fmt.Errorf("Invalid suffix array: SA[%d] = %d is out of range\n", i, s1)
}
if s2 < 0 || s2 >= len(input) {
return fmt.Errorf("Invalid suffix array: SA[%d] = %d is out of range\n", i+1, s2)
}
if s1 == s2 {
return fmt.Errorf("Invalid suffix array: SA[%d] = SA[%d]\n", i, i+1)
}
for {
if input[s1] < input[s2] {
// success
break
}
if input[s1] > input[s2] {
return fmt.Errorf("Invalid suffix array: suffix starting at SA[%d]=%d is greater than suffix starting at SA[%d]=%d\n", i, SA[i], i+1, SA[i+1])
}
s1++
if s1 == len(input) {
// success
break
}
s2++
if s2 == len(input) {
return fmt.Errorf("Invalid suffix array: suffix starting at SA[%d]=%d is greater than suffix starting at SA[%d]=%d\n", i, SA[i], i+1, SA[i+1])
}
}
}
if len(suffixesSeen) != len(input) {
return fmt.Errorf("Invalid suffix array: only saw %d unique suffixes (expected %d)\n", len(suffixesSeen), len(input))
}
return nil
}
func TestBasic(t *testing.T) {
ws := &WorkSpace{}
for _, input := range [][]byte{
// simple tests
[]byte("baa"),
[]byte("banana"),
[]byte("bananafoobar"),
[]byte("mmiissiippii"),
// random tests that failed during development/debugging
[]byte("anzazzdexszakdovkzahyckszpfqqfquuszaongqn"),
[]byte("dlvoppoimkrvyktwwxvbmemsvopnexqdftnuamepiu"),
[]byte("agidaenivhknajhfgekpmmugaqljpaoerhyyerhzxaehp"),
[]byte("slsluwafbeygwtsflijvcfedimtewfybyhzkzjbpewfifl"),
[]byte("eckdiyvvlrsemmcpawoirivnockdlbrmaufbehxipsqhyanosoqbp"),
[]byte("xbuxnvtzouehxqopupcwfivyhnwvkftcwfhjmjxzncnmvwlisrpxqnvczo"),
[]byte("naxkmuquvkhngkcqalbgpdxjkalbvrmbqscyikqdhrvvijkfngfikxtvalsmobje"),
[]byte("dxjpmcwlvmuswgfatoqolxcicbbvgbvrhwvibjbliiqcbolxcfnajixxbskjylcfxuhgvrwcfqahe"),
[]byte("tqxqbajhitxealorzfbmiulasimxpfxqvzenmdjhththzmyxrsqakcoqunzgopagkoevslbyndymbpiyswsdngoyuipxfxmswvkhu"),
[]byte("hngsmqhsuuwwqdwueftduujrxiyygqskpczwoyibotyqdfkdoayaujteffqmavwjkrgpbqwiobmkglovzhgdiuimkffjwpqusynihwkokduowszlhefkskkfkefbwilfzvfyzsamfkavfjoyfekfamqvtamfmniazidowdakbhedhzwzdanjavtmgciicmewqtrbcvmbljuhostzjojqzdtegzquljdhdwzqhfojvctkzekxwurtralnipuicjbol"),
[]byte("vxijixkikjtgfxpwdizlmgkslnmtdiaftiexzfkppkqwanbzgvibonysykramrtklvnqbljynrddqyqlbtpugakabinvvuzpqfxvbefhopzvgmeasomnmhnghdatmobksibaipjtvmufpqstnojqjyfmhibltyafjjednaywgpgrglhxonkhibsrlmxvubwecqeddpzpksvjtsimgtyrpvtnqrfsgolsznbdtbuttgcnlvslnmmnjlnhepapdbsbhdrvqbmtxomtty"),
[]byte("bbsomwccexbwzqmeqdgcgymeyavyydmmcowprhahcjvloltsrwbkmuvtiwysdkyxygdojbdaubsbvbluicuhrxbqxhtwuphwaytpvjcgespczoreufogbflleubowaklbcttxyjnaufhdwbzniafrghlyszgolkyjumwzwwjzwcpjjrmbwyzoymgbpypreqsngoulmaxygcazmigmpoajmswefwcflrqxhrqytqogypsyslypgvrihlfeqrxhytbvpggbqubinydvcwtnxbmwlvilhuxnsdewuaovnsvozhnrwhmqrmrutrxjgknujcxabaaqedijbrdytqmndkjlfffauohhmyswdafxweyocylutquvtlpqteuyetgaftlngwcuxhylhdkprvgfvnqncywxftnwghlnqplxfmqxehbsisxcytpmliupvwnjzmuysjjntmwezfuxauiwbfggkxceayjdkgvghxhztdjmjisbkqallwtloyepczldrcldktggbzowjtjneziyrvnmecpzkodvxizhgnehjhiwwgnmyzbjtjfcktighrbwoibvjkobxgqhbfvguldejsxvgwbfwyanvtsqgtxhnhjecrpxwtovfhgejdczmbwoifsirjfztdjfupitvaljqqiyqqpxlbhwqpwplmysqdvzriqpjdrnkajkywlxtcetbgtvxorbrtpmfayeadiaoymqaetsgocvkmymrwfhpyzylieghmuegoqqjhdelswfllzuykysjzadutkxwgfhdmihvkcfvalofynroljcncdvblnreguoyhyrzhnoubladowjdjhyuazhyapwioxhrdtvmfljazmvbxjtkrwqrkepfctekohrtvsifxvqbzekddplytdwxgudgzsvyvxlxjdyqqmpsimuwvdmjtjpcyctorbdmffbzwxexygppzsdoczuppxiqxnnwewyjyeohpgkglstinafynsoyqtjybrdwsgvssuwcikhhoyhszglpuzmttmwezfknplhzjnapnxlbepxahcjjreysmzdwroclrylkqwoxwstzridtlraybpcohjuvltzypcwqfakgwxyybqeildjyvuiaakwvdduckmsvkyaqyebtgkrflatnlyqhycbrputyqofjfplxdxprfpbvjyifzsjwmnceiaovnqgfzaofjqqoffbrpfxygxlvyekoifiihzryeagcwwglvwbovtffehxamoznrtolqgyfkxlhjpjaqyfefoxlphficbcndpssiosqhkjmegnvpxynsipougnogroestwxamfprtsxffbhslwrnmjyjdolcekuzqwoauamufvqhzsbbpfsvupjscavgpgybgkzsicpgcxukkhgaiyxqauqienozaufwenctcgcibwyfsejfdrujqutiosvfctqroncnggxdjmmpjajsrbpjjsgqulgbbiauxndntroharhqglkjzgkprcwosychvvpfyedjtrcfpgjdmesbhlyzkeukxiesbtkdjpwikdesrjbfiabtufrkoevscabjmxmkdwekstnujocxtzcwlbmafmskhslsredavkpzjhbsfhwxmoauhixwolumhbqffduilfuecubztsqur"),
} {
SA := make([]int, len(input))
ws.ComputeSuffixArray(input, SA)
if err := checkCorrectSuffixArray(input, SA); err != nil {
t.Fatalf("input %s failed: %s", string(input), err)
}
}
}
func TestRandom(t *testing.T) {
ws := &WorkSpace{}
var (
seed = 12345
nlengths = 5000
testsPerLength = 1
)
if testing.Short() {
nlengths = 1000
}
rand.Seed(int64(seed))
input := make([]byte, nlengths)
SA := make([]int, nlengths)
for i := 1; i <= nlengths; i++ {
for j := 0; j < testsPerLength; j++ {
for k := 0; k < i; k++ {
input[k] = 'a' + byte(rand.Intn(26))
}
ws.ComputeSuffixArray(input[:i], SA[:i])
if err := checkCorrectSuffixArray(input[:i], SA[:i]); err != nil {
t.Fatalf("input %s failed: %s", string(input), err)
}
}
}
}
func Benchmark900K(b *testing.B) {
b.StopTimer()
ws := &WorkSpace{}
input := make([]byte, 900*1000)
for i := range input {
input[i] = byte(rand.Intn(256))
}
SA := make([]int, len(input))
b.StartTimer()
for i := 0; i < b.N; i++ {
ws.ComputeSuffixArray(input, SA)
}
}