-
Notifications
You must be signed in to change notification settings - Fork 2
/
cedar_test.go
94 lines (77 loc) · 1.9 KB
/
cedar_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
package cedar
import (
"fmt"
"testing"
"github.com/vcaesar/tt"
)
var (
cd *Cedar
words = []string{
"a", "aa", "ab", "abc", "abcd", "abcdef",
"太阳系", "太阳系水星", "太阳系金星", "太阳系地球", "太阳系火星",
"太阳系木星", "太阳系土星", "太阳系天王星", "太阳系海王星",
"this", "this is", "this is a cedar.", "cedar",
}
)
func InitCd(reduced ...bool) error {
cd = New(reduced...)
return nil
}
func TestLoadData(t *testing.T) {
// add the words
for i, word := range words {
err := cd.Insert([]byte(word), i)
tt.Nil(t, err)
}
// update the words
for i, word := range words {
err := cd.Delete([]byte(word))
tt.Nil(t, err)
err = cd.Update([]byte(word), i)
tt.Nil(t, err)
}
// delete not used word
for i := 10; i < 15; i++ {
err := cd.Delete([]byte(words[i]))
tt.Nil(t, err)
}
}
func TestFind(t *testing.T) {
key, err := cd.Find([]byte("a"), 0)
tt.Nil(t, err)
tt.Equal(t, 0, key)
val, err := cd.Get([]byte("ab"))
tt.Nil(t, err)
tt.Equal(t, 2, val)
to, err := cd.Jump([]byte("abc"), 0)
tt.Nil(t, err)
tt.Equal(t, 352, to)
val, err = cd.Value(to)
tt.Nil(t, err)
tt.Equal(t, 3, val)
val, match := cd.ExactMatch([]byte("cedar"))
tt.Equal(t, 18, val)
tt.Equal(t, true, match)
}
func TestPrefixMatch(t *testing.T) {
ids := cd.PrefixMatch([]byte("this is a cedar."), 0)
fmt.Println("ids: ", ids)
keys := []string{"this", "this is", "this is a cedar."}
values := []int{15, 16, 17}
tt.Equal(t, len(keys), len(ids))
for i, n := range ids {
v, _ := cd.Value(n)
tt.Equal(t, values[i], v)
}
}
func TestPrefixPredict(t *testing.T) {
ids := cd.PrefixPredict([]byte("太阳系"), 0)
fmt.Println("ids: ", ids)
keys := []string{"太阳系", "太阳系地球", "太阳系水星", "太阳系金星"}
values := []int{6, 9, 7, 8}
tt.Equal(t, len(keys), len(ids))
for i, n := range ids {
v, _ := cd.Value(n)
tt.Equal(t, values[i], v)
}
}