-
Notifications
You must be signed in to change notification settings - Fork 1
/
tree_utils_test.go
97 lines (79 loc) · 1.89 KB
/
tree_utils_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
package brouter
import (
"fmt"
"net/http"
"testing"
"unsafe"
"github.com/stretchr/testify/assert"
)
type testCase struct {
insertPath string
lookupPath string
paramKey []string
paramValue []string
}
type testCases []testCase
func (tcs *testCases) run(t *testing.T) *tree {
d := newTree()
done := 0
for _, tc := range *tcs {
d.insert(tc.insertPath, func(w http.ResponseWriter, r *http.Request, p Params) {
done++
})
//d.info()
}
for k, tc := range *tcs {
//p2 := make(Params, 0, d.maxParam)
h, p2 := d.lookup(tc.lookupPath)
var p Params
if p2 != nil {
p = *p2
}
cb := func() {
handleToUint := *(*uintptr)(unsafe.Pointer(&h))
assert.NotEqual(t, handleToUint, uintptr(0), fmt.Sprintf("lookup word(%s)", tc.lookupPath))
//fmt.Printf("testCases.run return h address:%x\n", handleToUint)
h(nil, nil, nil)
b := assert.Equal(t, done, k+1)
if !b {
panic(fmt.Sprintf("done(%d) != %d", done, k+1))
}
for index, needKey := range tc.paramKey {
if len(needKey) == 0 {
//fmt.Printf("index = %d, needKey = 0\n", k)
continue
}
needVal := tc.paramValue
b := assert.Equal(t, p[index].Key, needKey, fmt.Sprintf("lookup key(%s)", needKey))
if !b {
return
}
b = assert.Equal(t, p[index].Value, needVal[index], fmt.Sprintf("lookup key(%s)", needKey))
if !b {
return
}
}
}
b := assert.NotPanics(t, cb, fmt.Sprintf("lookup path is(%s)", tc.lookupPath))
if !b {
break
}
}
// d.info() //显示tree的拓扑结构
return d
}
type childNum struct {
path string
num int32
}
type childNumChecks []childNum
func (c *childNumChecks) check(t *testing.T, tree *tree) {
for _, cn := range *c {
n := tree.root.lookupNode(cn.path)
assert.NotNil(t, n, fmt.Sprintf("-->lookup path:%s", cn.path))
if n == nil {
return
}
assert.Equal(t, n.pathRef, cn.num, fmt.Sprintf("-->lookup path:%s", cn.path))
}
}