Debian Codegraph @

patricia/patricia_dense_test.go

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
1 // Copyright (c) 2014 The go-patricia AUTHORS
2 //
3 // Use of this source code is governed by The MIT License
4 // that can be found in the LICENSE file.
5
6 package patricia
7
8 import (
9 "math/rand"
10 "runtime"
11 "strconv"
12 "testing"
13 )
14
15 // Tests -----------------------------------------------------------------------
16
17 // overhead is allowed tolerance for Go's runtime/GC to increase the allocated memory
18 // (to avoid failing tests on insignificant growth amounts)
•19 const overhead = 50000
20
21 func TestTrie_InsertDense(t *testing.T) {
22 trie := NewTrie()
23
24 data := []testData{
25 {"aba", 0, success},
26 {"abb", 1, success},
27 {"abc", 2, success},
28 {"abd", 3, success},
29 {"abe", 4, success},
30 {"abf", 5, success},
31 {"abg", 6, success},
32 {"abh", 7, success},
33 {"abi", 8, success},
34 {"abj", 9, success},
35 {"abk", 0, success},
36 {"abl", 1, success},
37 {"abm", 2, success},
38 {"abn", 3, success},
39 {"abo", 4, success},
40 {"abp", 5, success},
41 {"abq", 6, success},
42 {"abr", 7, success},
43 {"abs", 8, success},
44 {"abt", 9, success},
45 {"abu", 0, success},
46 {"abv", 1, success},
47 {"abw", 2, success},
48 {"abx", 3, success},
49 {"aby", 4, success},
50 {"abz", 5, success},
51 }
52
53 for _, v := range data {
54 t.Logf("INSERT prefix=%v, item=%v, success=%v", v.key, v.value, v.retVal)
55 if ok := trie.Insert(Prefix(v.key), v.value); ok != v.retVal {
56 t.Errorf("Unexpected return value, expected=%v, got=%v", v.retVal, ok)
57 }
58 }
59 }
60
61 func TestTrie_InsertDenseDuplicatePrefixes(t *testing.T) {
62 trie := NewTrie()
63
64 data := []testData{
65 {"aba", 0, success},
66 {"abb", 1, success},
67 {"abc", 2, success},
68 {"abd", 3, success},
69 {"abe", 4, success},
70 {"abf", 5, success},
71 {"abg", 6, success},
72 {"abh", 7, success},
73 {"abi", 8, success},
74 {"abj", 9, success},
75 {"abk", 0, success},
76 {"abl", 1, success},
77 {"abm", 2, success},
78 {"abn", 3, success},
79 {"abo", 4, success},
80 {"abp", 5, success},
81 {"abq", 6, success},
82 {"abr", 7, success},
83 {"abs", 8, success},
84 {"abt", 9, success},
85 {"abu", 0, success},
86 {"abv", 1, success},
87 {"abw", 2, success},
88 {"abx", 3, success},
89 {"aby", 4, success},
90 {"abz", 5, success},
91 {"aba", 0, failure},
92 {"abb", 1, failure},
93 {"abc", 2, failure},
94 {"abd", 3, failure},
95 {"abe", 4, failure},
96 }
97
98 for _, v := range data {
99 t.Logf("INSERT prefix=%v, item=%v, success=%v", v.key, v.value, v.retVal)
100 if ok := trie.Insert(Prefix(v.key), v.value); ok != v.retVal {
101 t.Errorf("Unexpected return value, expected=%v, got=%v", v.retVal, ok)
102 }
103 }
104 }
105
106 func TestTrie_CloneDense(t *testing.T) {
107 trie := NewTrie()
108
109 data := []testData{
110 {"aba", 0, success},
111 {"abb", 1, success},
112 {"abc", 2, success},
113 {"abd", 3, success},
114 {"abe", 4, success},
115 {"abf", 5, success},
116 {"abg", 6, success},
117 {"abh", 7, success},
118 {"abi", 8, success},
119 {"abj", 9, success},
120 {"abk", 0, success},
121 {"abl", 1, success},
122 {"abm", 2, success},
123 {"abn", 3, success},
124 {"abo", 4, success},
125 {"abp", 5, success},
126 {"abq", 6, success},
127 {"abr", 7, success},
128 {"abs", 8, success},
129 {"abt", 9, success},
130 {"abu", 0, success},
131 {"abv", 1, success},
132 {"abw", 2, success},
133 {"abx", 3, success},
134 {"aby", 4, success},
135 {"abz", 5, success},
136 }
137
138 for _, v := range data {
139 t.Logf("INSERT prefix=%v, item=%v, success=%v", v.key, v.value, v.retVal)
140 if ok := trie.Insert(Prefix(v.key), v.value); ok != v.retVal {
141 t.Errorf("Unexpected return value, expected=%v, got=%v", v.retVal, ok)
142 }
143 }
144
145 t.Log("CLONE")
146 clone := trie.Clone()
147
148 for _, v := range data {
149 t.Logf("GET prefix=%v, item=%v", v.key, v.value)
150 if item := clone.Get(Prefix(v.key)); item != v.value {
151 t.Errorf("Unexpected return value, expected=%v, got=%v", v.value, item)
152 }
153 }
154
155 prefix := "xxx"
156 item := 666
157 t.Logf("INSERT prefix=%v, item=%v", prefix, item)
158 if ok := trie.Insert(Prefix(prefix), item); !ok {
159 t.Errorf("Unexpected return value, expected=true, got=%v", ok)
160 }
161 t.Logf("GET cloned prefix=%v", prefix)
162 if item := clone.Get(Prefix(prefix)); item != nil {
163 t.Errorf("Unexpected return value, expected=nil, got=%v", item)
164 }
165 }
166
167 func TestTrie_DeleteDense(t *testing.T) {
168 trie := NewTrie()
169
170 data := []testData{
171 {"aba", 0, success},
172 {"abb", 1, success},
173 {"abc", 2, success},
174 {"abd", 3, success},
175 {"abe", 4, success},
176 {"abf", 5, success},
177 {"abg", 6, success},
178 {"abh", 7, success},
179 {"abi", 8, success},
180 {"abj", 9, success},
181 {"abk", 0, success},
182 {"abl", 1, success},
183 {"abm", 2, success},
184 {"abn", 3, success},
185 {"abo", 4, success},
186 {"abp", 5, success},
187 {"abq", 6, success},
188 {"abr", 7, success},
189 {"abs", 8, success},
190 {"abt", 9, success},
191 {"abu", 0, success},
192 {"abv", 1, success},
193 {"abw", 2, success},
194 {"abx", 3, success},
195 {"aby", 4, success},
196 {"abz", 5, success},
197 }
198
199 for _, v := range data {
200 t.Logf("INSERT prefix=%v, item=%v, success=%v", v.key, v.value, v.retVal)
201 if ok := trie.Insert(Prefix(v.key), v.value); ok != v.retVal {
202 t.Errorf("Unexpected return value, expected=%v, got=%v", v.retVal, ok)
203 }
204 }
205
206 for _, v := range data {
207 t.Logf("DELETE word=%v, success=%v", v.key, v.retVal)
208 if ok := trie.Delete([]byte(v.key)); ok != v.retVal {
209 t.Errorf("Unexpected return value, expected=%v, got=%v", v.retVal, ok)
210 }
211 }
212 }
213
214 func TestTrie_DeleteLeakageDense(t *testing.T) {
215 trie := NewTrie()
216
217 genTestData := func() *testData {
218 // Generate a random integer as a key.
219 key := strconv.FormatUint(rand.Uint64(), 10)
220 return &testData{key: key, value: "v", retVal: success}
221 }
222
223 testSize := 100
224 data := make([]*testData, 0, testSize)
225 for i := 0; i < testSize; i++ {
226 data = append(data, genTestData())
227 }
228
229 oldBytes := heapAllocatedBytes()
230
231 // repeat insertion/deletion for 10K times to catch possible memory issues
232 for i := 0; i < 10000; i++ {
233 for _, v := range data {
234 if ok := trie.Insert(Prefix(v.key), v.value); ok != v.retVal {
235 t.Errorf("Unexpected return value, expected=%v, got=%v", v.retVal, ok)
236 }
237 }
238
239 for _, v := range data {
240 if ok := trie.Delete([]byte(v.key)); ok != v.retVal {
241 t.Errorf("Unexpected return value, expected=%v, got=%v", v.retVal, ok)
242 }
243 }
244 }
245
246 if newBytes := heapAllocatedBytes(); newBytes > oldBytes+overhead {
247 t.Logf("Size=%d, Total=%d, Trie state:\n%s\n", trie.size(), trie.total(), trie.dump())
248 t.Errorf("Heap space leak, grew %d bytes (%d to %d)\n", newBytes-oldBytes, oldBytes, newBytes)
249 }
250
251 if numChildren := trie.children.length(); numChildren != 0 {
252 t.Errorf("Trie is not empty: %v children found", numChildren)
253 }
254 }
255
256 func heapAllocatedBytes() uint64 {
257 runtime.GC()
258
259 ms := runtime.MemStats{}
260 runtime.ReadMemStats(&ms)
261 return ms.Alloc
262 }