-
Notifications
You must be signed in to change notification settings - Fork 0
/
gobind_test.go
87 lines (75 loc) · 1.77 KB
/
gobind_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
package gobind
import (
"testing"
)
type Result struct {
Foo string `json:"foo" tag:"0"`
Foo2 string `json:"foo2" tag:"1"`
Foo3 string `json:"foo3" tag:"2"`
Foo4 string `json:"foo4" tag:"3"`
Foo5 string `json:"foo5" tag:"4"`
Foo6 string `json:"foo6" tag:"5"`
}
type Data struct {
Foo string `json:"foo"`
Foo2 string `json:"foo2"`
Foo3 string `json:"foo3"`
Foo4 string `json:"foo4"`
Foo5 string `json:"foo5"`
Foo6 string `json:"foo6"`
}
func TestBind(t *testing.T) {
var r Result
data := Data{"foo", "foo2", "foo3", "foo4", "foo5", "foo6"}
err := Bind(&r, data, data, data, data, data, data)
if err != nil {
t.Fatal("bind fail, %s", err)
return
}
if r.Foo != "foo" {
t.Fatal("bind fail, r.Foo is not equal with foo, got:%s", r.Foo)
}
if r.Foo2 != "foo2" {
t.Fatal("bind fail, r.Foo2 is not equal with foo2, got:%s", r.Foo2)
}
if r.Foo3 != "foo3" {
t.Fatal("bind fail, r.Foo2 is not equal with foo2, got:%s", r.Foo2)
}
if r.Foo4 != "foo4" {
t.Fatal("bind fail, r.Foo2 is not equal with foo2, got:%s", r.Foo2)
}
if r.Foo5 != "foo5" {
t.Fatal("bind fail, r.Foo2 is not equal with foo2, got:%s", r.Foo2)
}
}
func BenchmarkBind(b *testing.B) {
type Result struct {
Foo string `json:"foo" tag:"0"`
Foo2 string `json:"foo2" tag:"1"`
}
type Data struct {
Foo string `json:"foo"`
Foo2 string `json:"foo2"`
}
data := Data{"foo", "foo2"}
for i := 0; i < b.N; i++ {
var r Result
err := Bind(&r, data, data)
if err != nil {
b.Fatal("bind error")
return
}
}
}
func BenchmarkDirectAssign(b *testing.B) {
data := Data{"foo", "foo2", "foo3", "foo4", "foo5", "foo6"}
for i := 0; i < b.N; i++ {
var r Result
r.Foo = data.Foo
r.Foo2 = data.Foo2
r.Foo3 = data.Foo3
r.Foo4 = data.Foo4
r.Foo5 = data.Foo5
r.Foo6 = data.Foo6
}
}