-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtrie_test.go
48 lines (39 loc) · 1.06 KB
/
trie_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
package pork
import "testing"
func expectMiss(t *testing.T, trie *Trie, k string) {
_, _, found := trie.Get(k)
if found {
t.Errorf("expected NOT to find %s but did", k)
}
}
func expectHit(t *testing.T, trie *Trie, k, p, v string) {
rp, rv, found := trie.Get(k)
if !found {
t.Errorf("expected to find %s but did not", k)
}
if p != rp {
t.Errorf("prefix: expected %s, got %s\n", p, rp)
}
value := rv.(string)
if v != rv {
t.Errorf("value: expected %s, got %s\n", v, value)
}
}
func TestTrie(t *testing.T) {
trie := NewTrie()
trie.Put("k", "norton")
trie.Put("kel", "naughton")
expectHit(t, trie, "kellegous", "kel", "naughton")
expectHit(t, trie, "kathy", "k", "norton")
expectHit(t, trie, "kel", "kel", "naughton")
expectHit(t, trie, "k", "k", "norton")
expectMiss(t, trie, "foot")
// Empty, catch all?
trie.Put("", "bottom")
expectHit(t, trie, "foo", "", "bottom")
// Updates?
trie.Put("", "new bottom")
trie.Put("kel", "new naughton")
expectHit(t, trie, "foo", "", "new bottom")
expectHit(t, trie, "kelwelwoo", "kel", "new naughton")
}