-
Notifications
You must be signed in to change notification settings - Fork 28
/
dict_test.go
178 lines (159 loc) · 3.31 KB
/
dict_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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package cedar
import (
"bufio"
"fmt"
"io"
"log"
"os"
"testing"
)
type item struct {
key []byte
value int
}
var dict []item
var trie = New()
func loadDict() {
f, err := os.Open("testdata/dict.txt")
if err != nil {
panic("failed to open testdata/dict.txt")
}
defer f.Close()
in := bufio.NewReader(f)
added := make(map[string]struct{})
var key string
var freq int
var pos string
for {
_, err := fmt.Fscanln(in, &key, &freq, &pos)
if err == io.EOF {
break
}
if _, ok := added[string(key)]; !ok {
dict = append(dict, item{[]byte(key), freq})
added[string(key)] = struct{}{}
}
}
}
func TestLargeDict(t *testing.T) {
loadDict()
size := len(dict)
log.Println("dict size:", size)
exist := func(i int) {
item := dict[i]
// fmt.Println(i, string(item.key))
id, err := trie.Jump(item.key, 0)
failIfError(err)
key, err := trie.Key(id)
failIfError(err)
value, err := trie.Value(id)
failIfError(err)
if string(key) != string(item.key) || value != item.value {
v, _ := trie.Get(item.key)
fmt.Println(i, string(key), string(item.key), value, item.value, v)
panic("large dict test fail: no equal")
}
}
notExist := func(i int) {
_, err := trie.Get(dict[i].key)
// fmt.Println(i, err)
if err != ErrNoPath && err != ErrNoValue {
panic("large dict test fail: should not exist")
}
}
checkSize := func(exp int) {
if keys, _, _, _ := trie.Status(); keys != exp {
panic("not correct status")
}
}
// Insert the first half of the dict.
for i := 0; i < size/2; i++ {
item := dict[i]
if i%2 == 0 {
if err := trie.Insert(item.key, item.value); err != nil {
panic(err)
}
} else {
if err := trie.Update(item.key, item.value); err != nil {
panic(err)
}
}
}
checkSize(size / 2)
// Check the first half of the dict.
for i := 0; i < size/2; i++ {
exist(i)
}
log.Println("first half OK")
// Delete even items in the first half.
for i := 0; i < size/2; i += 2 {
err := trie.Delete(dict[i].key)
failIfError(err)
}
checkSize(size / 2 / 2)
// Make sure even items were deleted, and the rest are fine.
for i := 0; i < size/2; i++ {
if i%2 == 0 {
notExist(i)
} else {
exist(i)
}
}
log.Println("first half odd OK")
// Insert the second half of the dict.
for i := size / 2; i < size; i++ {
item := dict[i]
trie.Insert(item.key, item.value)
}
checkSize(size/2/2 + (size - size/2))
for i := 0; i < size/2; i++ {
if i%2 == 0 {
notExist(i)
} else {
exist(i)
}
}
log.Println("first half odd still OK")
// Delete even items in the second half.
half := size / 2
if half%2 == 1 {
half++
}
for i := half; i < size; i += 2 {
err := trie.Delete(dict[i].key)
failIfError(err)
}
// Make sure even items were deleted, and the rest are fine.
for i := 0; i < size; i++ {
if i%2 == 0 {
notExist(i)
} else {
exist(i)
}
}
log.Println("odd OK")
// Insert all even terms.
for i := 0; i < size; i += 2 {
item := dict[i]
notExist(i)
trie.Update([]byte(item.key), item.value)
}
for i := 0; i < size; i += 1 {
exist(i)
}
log.Println("all OK")
// Insert every item again, should be fine.
for i := 1; i < size; i++ {
item := dict[i]
trie.Insert([]byte(item.key), item.value)
}
for i := 1; i < size; i += 2 {
exist(i)
}
log.Println("still OK")
}
func failIfError(err error) {
if err != nil {
panic(err)
}
}