-
Notifications
You must be signed in to change notification settings - Fork 6
/
trie_test.go
75 lines (66 loc) · 1.84 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
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
package gochinadns
import (
"reflect"
"testing"
)
func TestTrieNormalize(t *testing.T) {
trie := new(domainTrie)
trie.Add("")
if trie.end || trie.children != nil {
t.Error("Adding an empty string as domain name should have no effect")
}
trie2 := new(domainTrie)
trie.Add("google.com.")
trie2.Add("google.com")
if !reflect.DeepEqual(trie, trie2) {
t.Error("Domain name normalization failed")
}
}
func TestTrieAdd(t *testing.T) {
trie := new(domainTrie)
trie.Add("google.com")
trie.Add(".")
trie.Add("goo.gl")
if !(trie.end && trie.children == nil) {
t.Error("A dot should end at root.")
}
if !trie.Contain("www.google.com") || !trie.Contain("ietf.org") {
t.Error("A dot should contain any domain name")
}
}
func TestTrieContain(t *testing.T) {
trie := new(domainTrie)
if trie.Contain("goo.gl") {
t.Error("An empty trie contains nothing")
}
trie.Add("google.com")
trie.Add("api.github.com")
trie.Add("cn.")
if trie.Contain("www.github.com") {
t.Error("api.github.com should not contain www.github.com")
}
if trie.Contain("github.com") {
t.Error("api.github.com should not contain github.com")
}
if trie.Contain("ietf.org") || trie.Contain("twitter.com") {
t.Error("What a shit have you written?")
}
if !trie.Contain("google.com") {
t.Error("google.com should contain itself")
}
if !trie.Contain("api.github.com") {
t.Error("api.github.com should contain itself")
}
if !trie.Contain("www.google.com") || !trie.Contain("mail.google.com") {
t.Error("www.google.com and mail.google.com should be contained by google.com")
}
if !trie.Contain("google.com.") {
t.Error("`google.com.` should be treated like `google.com`")
}
if !trie.Contain("www.google.com.") {
t.Error("`www.google.com.` should be contained by `google.com`")
}
if !trie.Contain("12306.cn") {
t.Error("cn should contain all .cn domains")
}
}