/usr/share/gocode/src/github.com/tchap/go-patricia/patricia/patricia_dense_test.go is in golang-github-tchap-go-patricia-dev 2.2.6-5.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 212 213 214 215 216 217 218 | // Copyright (c) 2014 The go-patricia AUTHORS
//
// Use of this source code is governed by The MIT License
// that can be found in the LICENSE file.
package patricia
import (
"runtime"
"testing"
"github.com/satori/go.uuid"
)
// Tests -----------------------------------------------------------------------
// overhead is allowed tolerance for Go's runtime/GC to increase the allocated memory
// (to avoid failing tests on insignificant growth amounts)
const overhead = 4000
func TestTrie_InsertDense(t *testing.T) {
trie := NewTrie()
data := []testData{
{"aba", 0, success},
{"abb", 1, success},
{"abc", 2, success},
{"abd", 3, success},
{"abe", 4, success},
{"abf", 5, success},
{"abg", 6, success},
{"abh", 7, success},
{"abi", 8, success},
{"abj", 9, success},
{"abk", 0, success},
{"abl", 1, success},
{"abm", 2, success},
{"abn", 3, success},
{"abo", 4, success},
{"abp", 5, success},
{"abq", 6, success},
{"abr", 7, success},
{"abs", 8, success},
{"abt", 9, success},
{"abu", 0, success},
{"abv", 1, success},
{"abw", 2, success},
{"abx", 3, success},
{"aby", 4, success},
{"abz", 5, success},
}
for _, v := range data {
t.Logf("INSERT prefix=%v, item=%v, success=%v", v.key, v.value, v.retVal)
if ok := trie.Insert(Prefix(v.key), v.value); ok != v.retVal {
t.Errorf("Unexpected return value, expected=%v, got=%v", v.retVal, ok)
}
}
}
func TestTrie_InsertDensePreceeding(t *testing.T) {
trie := NewTrie()
start := byte(70)
// create a dense node
for i := byte(0); i <= DefaultMaxChildrenPerSparseNode; i++ {
if !trie.Insert(Prefix([]byte{start + i}), true) {
t.Errorf("insert failed, prefix=%v", start+i)
}
}
// insert some preceeding keys
for i := byte(1); i < start; i *= i + 1 {
if !trie.Insert(Prefix([]byte{start - i}), true) {
t.Errorf("insert failed, prefix=%v", start-i)
}
}
}
func TestTrie_InsertDenseDuplicatePrefixes(t *testing.T) {
trie := NewTrie()
data := []testData{
{"aba", 0, success},
{"abb", 1, success},
{"abc", 2, success},
{"abd", 3, success},
{"abe", 4, success},
{"abf", 5, success},
{"abg", 6, success},
{"abh", 7, success},
{"abi", 8, success},
{"abj", 9, success},
{"abk", 0, success},
{"abl", 1, success},
{"abm", 2, success},
{"abn", 3, success},
{"abo", 4, success},
{"abp", 5, success},
{"abq", 6, success},
{"abr", 7, success},
{"abs", 8, success},
{"abt", 9, success},
{"abu", 0, success},
{"abv", 1, success},
{"abw", 2, success},
{"abx", 3, success},
{"aby", 4, success},
{"abz", 5, success},
{"aba", 0, failure},
{"abb", 1, failure},
{"abc", 2, failure},
{"abd", 3, failure},
{"abe", 4, failure},
}
for _, v := range data {
t.Logf("INSERT prefix=%v, item=%v, success=%v", v.key, v.value, v.retVal)
if ok := trie.Insert(Prefix(v.key), v.value); ok != v.retVal {
t.Errorf("Unexpected return value, expected=%v, got=%v", v.retVal, ok)
}
}
}
func TestTrie_DeleteDense(t *testing.T) {
trie := NewTrie()
data := []testData{
{"aba", 0, success},
{"abb", 1, success},
{"abc", 2, success},
{"abd", 3, success},
{"abe", 4, success},
{"abf", 5, success},
{"abg", 6, success},
{"abh", 7, success},
{"abi", 8, success},
{"abj", 9, success},
{"abk", 0, success},
{"abl", 1, success},
{"abm", 2, success},
{"abn", 3, success},
{"abo", 4, success},
{"abp", 5, success},
{"abq", 6, success},
{"abr", 7, success},
{"abs", 8, success},
{"abt", 9, success},
{"abu", 0, success},
{"abv", 1, success},
{"abw", 2, success},
{"abx", 3, success},
{"aby", 4, success},
{"abz", 5, success},
}
for _, v := range data {
t.Logf("INSERT prefix=%v, item=%v, success=%v", v.key, v.value, v.retVal)
if ok := trie.Insert(Prefix(v.key), v.value); ok != v.retVal {
t.Errorf("Unexpected return value, expected=%v, got=%v", v.retVal, ok)
}
}
for _, v := range data {
t.Logf("DELETE word=%v, success=%v", v.key, v.retVal)
if ok := trie.Delete([]byte(v.key)); ok != v.retVal {
t.Errorf("Unexpected return value, expected=%v, got=%v", v.retVal, ok)
}
}
}
func TestTrie_DeleteLeakageDense(t *testing.T) {
trie := NewTrie()
genTestData := func() *testData {
// Generate a random hash as a key.
key := uuid.NewV4()
return &testData{key: key.String(), value: "v", retVal: success}
}
testSize := 100
data := make([]*testData, 0, testSize)
for i := 0; i < testSize; i++ {
data = append(data, genTestData())
}
oldBytes := heapAllocatedBytes()
// repeat insertion/deletion for 10K times to catch possible memory issues
for i := 0; i < 10000; i++ {
for _, v := range data {
if ok := trie.Insert(Prefix(v.key), v.value); ok != v.retVal {
t.Errorf("Unexpected return value, expected=%v, got=%v", v.retVal, ok)
}
}
for _, v := range data {
if ok := trie.Delete([]byte(v.key)); ok != v.retVal {
t.Errorf("Unexpected return value, expected=%v, got=%v", v.retVal, ok)
}
}
}
if newBytes := heapAllocatedBytes(); newBytes > oldBytes+overhead {
t.Logf("Size=%d, Total=%d, Trie state:\n%s\n", trie.size(), trie.total(), trie.dump())
t.Errorf("Heap space leak, grew %d bytes (%d to %d)\n", newBytes-oldBytes, oldBytes, newBytes)
}
if numChildren := trie.children.length(); numChildren != 0 {
t.Errorf("Trie is not empty: %v children found", numChildren)
}
}
func heapAllocatedBytes() uint64 {
runtime.GC()
ms := runtime.MemStats{}
runtime.ReadMemStats(&ms)
return ms.Alloc
}
|