This repository has been archived by the owner on Oct 5, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathdict_test.go
211 lines (177 loc) · 3.52 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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
package cedar
import (
"bufio"
"fmt"
"io"
"log"
"os"
"testing"
)
type item struct {
key []byte
value int
}
var (
dict []item
trie = New()
)
func loadDict() []item {
testFile := "testdata/dict.txt"
f, err := os.Open(testFile)
if err != nil {
log.Fatal("failed to open ", testFile)
}
defer f.Close()
in := bufio.NewReader(f)
added := make(map[string]struct{})
var (
key string
freq int
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{}{}
}
}
return dict
}
func exist(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("exist but no equal: ", i, string(key), string(item.key), value, item.value, v)
panic("large dict test fail: no equal")
}
}
func notExist(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")
}
}
func checkSize(exp int) {
if keys, _, _, _ := trie.Status(); keys != exp {
panic("not correct status")
}
}
func insertDict(size int) {
// 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)
}
func checkDict(size int) {
// 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))
}
func odd(size int) {
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")
}
func even(size int) {
// 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++ {
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 TestLargeDict(t *testing.T) {
loadDict()
size := len(dict)
log.Println("dict size:", size)
insertDict(size)
checkDict(size)
odd(size)
even(size)
}
func failIfError(err error) {
if err != nil {
panic(err)
}
}