-
Notifications
You must be signed in to change notification settings - Fork 0
/
trie_test.go
102 lines (92 loc) · 2.01 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
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
package ipatrie
import (
"strings"
"testing"
"unsafe"
)
func verify(t testing.TB, trie *Trie, addr string, expected int) {
found := trie.Match(ParseIPv4(addr))
if expected >= 0 && !found {
t.Errorf("Expected [%s]==%v but got=%v", addr, expected, found)
}
if expected < 0 && found {
t.Errorf("Expected [%s] not-found but got=%v", addr, found)
}
}
func TestSimple(t *testing.T) {
t.Log("sizeof node", unsafe.Sizeof(TrieNode{}))
net_samples := []string{
"1.1.1.0/24",
"1.1.2.2/30",
"1.1.2.0/24",
"1.1.0.0/16",
"1.2.3.4/32",
}
trie := NewTrie()
for _, s := range net_samples {
a, m, _ := ParseCIDR(s)
trie.Insert(a, m)
}
trie.printStat(t)
addr_samples := map[string]int{
"1.1.1.0": 1,
"1.1.1.1": 1,
"1.1.1.255": 1,
"1.1.2.1": 2,
"1.1.2.2": 2,
"1.1.2.3": 2,
"1.1.2.4": 3,
"1.1.3.1": 4,
"1.2.3.4": 5,
"1.2.3.1": -1,
"2.2.4.1": -1,
"0.1.3.1": -1,
}
for s, v := range addr_samples {
verify(t, trie, s, v)
}
}
func TestSamples(t *testing.T) {
tab := detectTestTables()
var size uint32
for _, f := range tab {
f = f[9:]
s := strings.Replace(f, "table", "sample", 1)
trie := initTestData(f)
trie.printStat(t)
if trie.size > size {
// benchTable = "table-2.txt"
benchTable = f
size = trie.size
}
fileIter(s, func(fields []string) {
verify(t, trie, fields[0], parseField1(fields[1]))
})
t.Logf("test %d %s/%s", trie.size, f, s)
}
}
func BenchmarkTrie(b *testing.B) {
initBenchTrie(b)
var ips [10]uint32
for i := 0; i < len(ips); i++ {
ips[i] = randomIPv4Addr()
}
b.ResetTimer()
for i, j := 0, 0; i < b.N; i++ {
for j = 0; j < len(ips); j++ {
benchTrie.Match(ips[j])
}
}
}
func (t *Trie) printStat(tb testing.TB) {
//tb.Logf("trie left=%d right=%d value=%d size=%d", len(t.lefts), len(t.rights), len(t.values), t.size)
tb.Logf("trie size=%d", t.size)
}
func initTestData(file string) *Trie {
trie := NewTrie()
fileIter(file, func(fields []string) {
na, m, _ := ParseCIDR(fields[0])
trie.Insert(na, m)
})
return trie
}