| 1 | package trie |
| 2 | |
| 3 | import ( |
| 4 | "errors" |
| 5 | "testing" |
| 6 | ) |
| 7 | |
| 8 | // RuneTrie |
| 9 | |
| 10 | func TestRuneTrie(t *testing.T) { |
| 11 | trie := NewRuneTrie() |
| 12 | testTrie(t, trie) |
| 13 | } |
| 14 | |
| 15 | func TestRuneTrieNilBehavior(t *testing.T) { |
| 16 | trie := NewRuneTrie() |
| 17 | testNilBehavior(t, trie) |
| 18 | } |
| 19 | |
| 20 | func TestRuneTrieRoot(t *testing.T) { |
| 21 | trie := NewRuneTrie() |
| 22 | testTrieRoot(t, trie) |
| 23 | |
| 24 | trie = NewRuneTrie() |
| 25 | if !trie.isLeaf() { |
| 26 | t.Error("root of empty tree should be leaf") |
| 27 | } |
| 28 | trie.Put("", "root") |
| 29 | if !trie.isLeaf() { |
| 30 | t.Error("root should not have children, only value") |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | func TestRuneTrieWalk(t *testing.T) { |
| 35 | trie := NewRuneTrie() |
| 36 | testTrieWalk(t, trie) |
| 37 | } |
| 38 | |
| 39 | func TestRuneTrieWalkError(t *testing.T) { |
| 40 | trie := NewRuneTrie() |
| 41 | testTrieWalkError(t, trie) |
| 42 | } |
| 43 | |
| 44 | func TestRuneTrieWalkPath(t *testing.T) { |
| 45 | trie := NewRuneTrie() |
| 46 | testTrieWalkPath(t, trie) |
| 47 | } |
| 48 | |
| 49 | func TestRuneTrieWalkPathError(t *testing.T) { |
| 50 | trie := NewRuneTrie() |
| 51 | testTrieWalkPathError(t, trie) |
| 52 | } |
| 53 | |
| 54 | // PathTrie |
| 55 | |
| 56 | func TestPathTrie(t *testing.T) { |
| 57 | trie := NewPathTrie() |
| 58 | testTrie(t, trie) |
| 59 | } |
| 60 | |
| 61 | func TestPathTrieWithNilConfig(t *testing.T) { |
| 62 | trie := NewPathTrieWithConfig(nil) |
| 63 | testTrie(t, trie) |
| 64 | } |
| 65 | |
| 66 | func TestPathTrieWithEmptyConfig(t *testing.T) { |
| 67 | trie := NewPathTrieWithConfig(&PathTrieConfig{}) |
| 68 | testTrie(t, trie) |
| 69 | } |
| 70 | |
| 71 | func TestPathTrieWithConfig(t *testing.T) { |
| 72 | trie := NewPathTrieWithConfig(&PathTrieConfig{PathSegmenter}) |
| 73 | testTrie(t, trie) |
| 74 | } |
| 75 | |
| 76 | func TestPathTrieNilBehavior(t *testing.T) { |
| 77 | trie := NewPathTrie() |
| 78 | testNilBehavior(t, trie) |
| 79 | } |
| 80 | |
| 81 | func TestPathTrieRoot(t *testing.T) { |
| 82 | trie := NewPathTrie() |
| 83 | testTrieRoot(t, trie) |
| 84 | |
| 85 | trie = NewPathTrie() |
| 86 | if !trie.isLeaf() { |
| 87 | t.Error("root of empty tree should be leaf") |
| 88 | } |
| 89 | trie.Put("", "root") |
| 90 | if !trie.isLeaf() { |
| 91 | t.Error("root should not have children, only value") |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | func TestPathTrieWalk(t *testing.T) { |
| 96 | trie := NewPathTrie() |
| 97 | testTrieWalk(t, trie) |
| 98 | } |
| 99 | |
| 100 | func TestPathTrieWalkError(t *testing.T) { |
| 101 | trie := NewPathTrie() |
| 102 | testTrieWalkError(t, trie) |
| 103 | } |
| 104 | |
| 105 | func TestPathTrieWalkPath(t *testing.T) { |
| 106 | trie := NewPathTrie() |
| 107 | testTrieWalkPath(t, trie) |
| 108 | } |
| 109 | |
| 110 | func TestPathTrieWalkPathError(t *testing.T) { |
| 111 | trie := NewPathTrie() |
| 112 | testTrieWalkPathError(t, trie) |
| 113 | } |
| 114 | |
| 115 | func testTrie(t *testing.T, trie Trier) { |
| 116 | const firstPutValue = "first put" |
| 117 | cases := []struct { |
| 118 | key string |
| 119 | value interface{} |
| 120 | }{ |
| 121 | {"fish", 0}, |
| 122 | {"/cat", 1}, |
| 123 | {"/dog", 2}, |
| 124 | {"/cats", 3}, |
| 125 | {"/caterpillar", 4}, |
| 126 | {"/cat/gideon", 5}, |
| 127 | {"/cat/giddy", 6}, |
| 128 | } |
| 129 | |
| 130 | // get missing keys |
| 131 | for _, c := range cases { |
| 132 | if value := trie.Get(c.key); value != nil { |
| 133 | t.Errorf("expected key %s to be missing, found value %v", c.key, value) |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | // initial put |
| 138 | for _, c := range cases { |
| 139 | if isNew := trie.Put(c.key, firstPutValue); !isNew { |
| 140 | t.Errorf("expected key %s to be missing", c.key) |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | // subsequent put |
| 145 | for _, c := range cases { |
| 146 | if isNew := trie.Put(c.key, c.value); isNew { |
| 147 | t.Errorf("expected key %s to have a value already", c.key) |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | // get |
| 152 | for _, c := range cases { |
| 153 | if value := trie.Get(c.key); value != c.value { |
| 154 | t.Errorf("expected key %s to have value %v, got %v", c.key, c.value, value) |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | // delete, expect Delete to return true indicating a node was nil'd |
| 159 | for _, c := range cases { |
| 160 | if deleted := trie.Delete(c.key); !deleted { |
| 161 | t.Errorf("expected key %s to be deleted", c.key) |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | // delete cleaned all the way to the first character |
| 166 | // expect Delete to return false bc no node existed to nil |
| 167 | for _, c := range cases { |
| 168 | if deleted := trie.Delete(string(c.key[0])); deleted { |
| 169 | t.Errorf("expected key %s to be cleaned by delete", string(c.key[0])) |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | // get deleted keys |
| 174 | for _, c := range cases { |
| 175 | if value := trie.Get(c.key); value != nil { |
| 176 | t.Errorf("expected key %s to be deleted, got value %v", c.key, value) |
| 177 | } |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | func testNilBehavior(t *testing.T, trie Trier) { |
| 182 | cases := []struct { |
| 183 | key string |
| 184 | value interface{} |
| 185 | }{ |
| 186 | {"/cat", 1}, |
| 187 | {"/catamaran", 2}, |
| 188 | {"/caterpillar", nil}, |
| 189 | } |
| 190 | expectNilValues := []string{"/", "/c", "/ca", "/caterpillar", "/other"} |
| 191 | |
| 192 | // initial put |
| 193 | for _, c := range cases { |
| 194 | if isNew := trie.Put(c.key, c.value); !isNew { |
| 195 | t.Errorf("expected key %s to be missing", c.key) |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | // get nil |
| 200 | for _, key := range expectNilValues { |
| 201 | if value := trie.Get(key); value != nil { |
| 202 | t.Errorf("expected key %s to have value nil, got %v", key, value) |
| 203 | } |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | func testTrieRoot(t *testing.T, trie Trier) { |
| 208 | const firstPutValue = "first put" |
| 209 | const putValue = "value" |
| 210 | |
| 211 | if value := trie.Get(""); value != nil { |
| 212 | t.Errorf("expected key '' to be missing, found value %v", value) |
| 213 | } |
| 214 | if !trie.Put("", firstPutValue) { |
| 215 | t.Error("expected key '' to be missing") |
| 216 | } |
| 217 | if trie.Put("", putValue) { |
| 218 | t.Error("expected key '' to have a value already") |
| 219 | } |
| 220 | if value := trie.Get(""); value != putValue { |
| 221 | t.Errorf("expected key '' to have value %v, got %v", putValue, value) |
| 222 | } |
| 223 | if !trie.Delete("") { |
| 224 | t.Error("expected key '' to be deleted") |
| 225 | } |
| 226 | if value := trie.Get(""); value != nil { |
| 227 | t.Errorf("expected key '' to be deleted, got value %v", value) |
| 228 | } |
| 229 | } |
| 230 | |
| 231 | func testTrieWalk(t *testing.T, trie Trier) { |
| 232 | table := map[string]interface{}{ |
| 233 | "": -1, |
| 234 | "fish": 0, |
| 235 | "/cat": 1, |
| 236 | "/dog": 2, |
| 237 | "/cats": 3, |
| 238 | "/caterpillar": 4, |
| 239 | "/notes": 30, |
| 240 | "/notes/new": 31, |
| 241 | "/notes/:id": 32, |
| 242 | } |
| 243 | // key -> times walked |
| 244 | walked := make(map[string]int) |
| 245 | for key := range table { |
| 246 | walked[key] = 0 |
| 247 | } |
| 248 | |
| 249 | for key, value := range table { |
| 250 | if isNew := trie.Put(key, value); !isNew { |
| 251 | t.Errorf("expected key %s to be missing", key) |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | walker := func(key string, value interface{}) error { |
| 256 | // value for each walked key is correct |
| 257 | if value != table[key] { |
| 258 | t.Errorf("expected key %s to have value %v, got %v", key, table[key], value) |
| 259 | } |
| 260 | walked[key]++ |
| 261 | return nil |
| 262 | } |
| 263 | if err := trie.Walk(walker); err != nil { |
| 264 | t.Errorf("expected error nil, got %v", err) |
| 265 | } |
| 266 | |
| 267 | // each key/value walked exactly once |
| 268 | for key, walkedCount := range walked { |
| 269 | if walkedCount != 1 { |
| 270 | t.Errorf("expected key %s to be walked exactly once, got %v", key, walkedCount) |
| 271 | } |
| 272 | } |
| 273 | } |
| 274 | |
| 275 | func testTrieWalkError(t *testing.T, trie Trier) { |
| 276 | table := map[string]interface{}{ |
| 277 | "/L1/L2A": 1, |
| 278 | "/L1/L2B/L3A": 2, |
| 279 | "/L1/L2B/L3B/L4": 42, |
| 280 | "/L1/L2B/L3C": 4, |
| 281 | "/L1/L2C": 5, |
| 282 | } |
| 283 | |
| 284 | walkerError := errors.New("walker error") |
| 285 | walked := 0 |
| 286 | |
| 287 | for key, value := range table { |
| 288 | trie.Put(key, value) |
| 289 | } |
| 290 | walker := func(key string, value interface{}) error { |
| 291 | if value == 42 { |
| 292 | return walkerError |
| 293 | } |
| 294 | walked++ |
| 295 | return nil |
| 296 | } |
| 297 | if err := trie.Walk(walker); err != walkerError { |
| 298 | t.Errorf("expected walker error, got %v", err) |
| 299 | } |
| 300 | if len(table) == walked { |
| 301 | t.Errorf("expected nodes walked < %d, got %d", len(table), walked) |
| 302 | } |
| 303 | } |
| 304 | |
| 305 | func testTrieWalkPath(t *testing.T, trie Trier) { |
| 306 | table := map[string]interface{}{ |
| 307 | "fish": 0, |
| 308 | "/cat": 1, |
| 309 | "/dog": 2, |
| 310 | "/cats": 3, |
| 311 | "/caterpillar": 4, |
| 312 | "/notes": 30, |
| 313 | "/notes/new": 31, |
| 314 | "/notes/new/noise": 32, |
| 315 | } |
| 316 | // key -> times walked |
| 317 | walked := make(map[string]int) |
| 318 | for key := range table { |
| 319 | walked[key] = 0 |
| 320 | } |
| 321 | |
| 322 | for key, value := range table { |
| 323 | if isNew := trie.Put(key, value); !isNew { |
| 324 | t.Errorf("expected key %s to be missing", key) |
| 325 | } |
| 326 | } |
| 327 | |
| 328 | walker := func(key string, value interface{}) error { |
| 329 | // value for each walked key is correct |
| 330 | if value != table[key] { |
| 331 | t.Errorf("expected key %s to have value %v, got %v", key, table[key], value) |
| 332 | } |
| 333 | walked[key]++ |
| 334 | return nil |
| 335 | } |
| 336 | if err := trie.WalkPath("/notes/new/noise", walker); err != nil { |
| 337 | t.Errorf("expected error nil, got %v", err) |
| 338 | } |
| 339 | |
| 340 | // expect each key/value in path walked exactly once, and not other keys |
| 341 | for key, walkedCount := range walked { |
| 342 | switch key { |
| 343 | case "/notes", "/notes/new", "/notes/new/noise": |
| 344 | if walkedCount != 1 { |
| 345 | t.Errorf("expected key %s to be walked exactly once, got %v", key, walkedCount) |
| 346 | } |
| 347 | default: |
| 348 | if walkedCount != 0 { |
| 349 | t.Errorf("expected key %s to not be walked, got %v", key, walkedCount) |
| 350 | } |
| 351 | } |
| 352 | } |
| 353 | |
| 354 | for key := range table { |
| 355 | walked[key] = 0 |
| 356 | } |
| 357 | if err := trie.WalkPath("/notes/new/nose", walker); err != nil { |
| 358 | t.Errorf("expected error nil, got %v", err) |
| 359 | } |
| 360 | // expect each key/value in path walked exactly once, and not other keys |
| 361 | for key, walkedCount := range walked { |
| 362 | switch key { |
| 363 | case "/notes", "/notes/new": |
| 364 | if walkedCount != 1 { |
| 365 | t.Errorf("expected key %s to be walked exactly once, got %v", key, walkedCount) |
| 366 | } |
| 367 | default: |
| 368 | if walkedCount != 0 { |
| 369 | t.Errorf("expected key %s to not be walked, got %v", key, walkedCount) |
| 370 | } |
| 371 | } |
| 372 | } |
| 373 | |
| 374 | var foundRoot bool |
| 375 | trie.Put("", "ROOT") |
| 376 | trie.WalkPath("/notes/new/noise", func(key string, value interface{}) error { |
| 377 | if key == "" && value == "ROOT" { |
| 378 | foundRoot = true |
| 379 | } |
| 380 | return nil |
| 381 | }) |
| 382 | if !foundRoot { |
| 383 | t.Error("did not find root") |
| 384 | } |
| 385 | } |
| 386 | |
| 387 | func testTrieWalkPathError(t *testing.T, trie Trier) { |
| 388 | table := map[string]interface{}{ |
| 389 | "/L1/L2A": 1, |
| 390 | "/L1/L2A/L3B": 99, |
| 391 | "/L1/L2A/L3B/L4": 2, |
| 392 | "/L1/L2B/L3B": 3, |
| 393 | "/L1/L2C": 4, |
| 394 | } |
| 395 | |
| 396 | walkerError := errors.New("walker error") |
| 397 | |
| 398 | walked := make(map[string]int) |
| 399 | for key := range table { |
| 400 | walked[key] = 0 |
| 401 | } |
| 402 | |
| 403 | for key, value := range table { |
| 404 | trie.Put(key, value) |
| 405 | } |
| 406 | walker := func(key string, value interface{}) error { |
| 407 | if value == 99 { |
| 408 | return walkerError |
| 409 | } |
| 410 | walked[key]++ |
| 411 | return nil |
| 412 | } |
| 413 | if err := trie.WalkPath("/L1/L2A/L3B", walker); err != walkerError { |
| 414 | t.Errorf("expected walker error, got %v", err) |
| 415 | } |
| 416 | // expect each key/value in path, up to error and not including key with |
| 417 | // value 99, walked exactly once, and not other keys |
| 418 | var walkedTotal int |
| 419 | for key, walkedCount := range walked { |
| 420 | switch key { |
| 421 | case "/L1/L2A": |
| 422 | if walkedCount != 1 { |
| 423 | t.Errorf("expected key %s to be walked exactly once, got %v", key, walkedCount) |
| 424 | } |
| 425 | walkedTotal++ |
| 426 | default: |
| 427 | if walkedCount != 0 { |
| 428 | t.Errorf("expected key %s to not be walked, got %v", key, walkedCount) |
| 429 | } |
| 430 | } |
| 431 | } |
| 432 | if walkedTotal != 1 { |
| 433 | t.Errorf("expected 1 nodes walked, got %d", walkedTotal) |
| 434 | } |
| 435 | |
| 436 | rootError := errors.New("error at root") |
| 437 | trie.Put("", "ROOT") |
| 438 | err := trie.WalkPath("/L1/L2A/L3B/L4", func(key string, value interface{}) error { |
| 439 | if key == "" && value == "ROOT" { |
| 440 | return rootError |
| 441 | } |
| 442 | return nil |
| 443 | }) |
| 444 | if err != rootError { |
| 445 | t.Errorf("expected %s, got %s", rootError, err) |
| 446 | } |
| 447 | } |