-
Notifications
You must be signed in to change notification settings - Fork 0
/
reflect_test.go
165 lines (137 loc) · 3.44 KB
/
reflect_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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package packet
import (
"fmt"
"reflect"
"testing"
"github.com/stretchr/testify/assert"
)
// this is a collection of benchmark for refect Set*
func setSingleValueReflect(i *int) {
t := reflect.ValueOf(i)
if t.Kind() == reflect.Ptr {
v := t.Elem()
v.SetInt(10)
}
}
func TestMarkUnmarshalSingle(test *testing.T) {
var i int
setSingleValueReflect(&i)
assert.Equal(test, 10, i)
}
func BenchmarkUnmarshalSingle(b *testing.B) {
var i int
for n := 0; n < b.N; n++ {
setSingleValueReflect(&i)
}
}
type ObjReflect struct {
A int
B uint
C string
}
func setObjValue(i *ObjReflect) {
t := reflect.ValueOf(i)
if t.Kind() == reflect.Ptr {
v := t.Elem()
if v.Kind() == reflect.Struct {
for i := 0; i < v.NumField(); i++ {
f := v.Field(i)
switch f.Kind() {
case reflect.Int:
f.SetInt(10)
case reflect.String:
f.SetString("10")
case reflect.Uint:
f.SetUint(10)
}
}
}
}
}
func TestUnmarshalObjectReflect(test *testing.T) {
o := &ObjReflect{}
setObjValue(o)
assert.Equal(test, &ObjReflect{10, 10, "10"}, o)
}
func BenchmarkUnmarshalObjReflect(b *testing.B) {
o := &ObjReflect{}
for n := 0; n < b.N; n++ {
setObjValue(o)
}
}
type ObjReflectWithBytesArray struct {
A byte
B byte
C byte
D [5]byte
}
func setObjWithBytesValue(o interface{}, data []byte) {
t := reflect.ValueOf(o)
if t.Kind() == reflect.Ptr {
v := t.Elem()
if v.Kind() == reflect.Struct {
k := 0
for i := 0; i < v.NumField(); i++ {
f := v.Field(i)
switch f.Kind() {
case reflect.Uint8:
f.SetUint(uint64(data[k]))
k++
case reflect.Array:
if f.Cap() > 0 && f.Index(0).Kind() == reflect.Uint8 {
for j := 0; j < f.Cap(); j++ {
fv := f.Index(j)
fv.SetUint(uint64(data[k]))
k++
}
k += f.Cap()
}
case reflect.Slice:
// slink the rest of the array
newCap := len(data) - k
newf := reflect.MakeSlice(f.Type(), f.Len(), newCap)
reflect.Copy(newf, f)
f.Set(newf)
f.SetBytes(data[k:])
}
}
}
} else {
panic(fmt.Errorf("can not handle type %+v", t))
}
}
var objectReflectWithBytes = []byte{0xfa, 0x16, 0x3e, 0x85, 0x92, 0x77, 0xfa, 0x16}
func TestReflectUnmarshalObjWithBytes(test *testing.T) {
o := &ObjReflectWithBytesArray{}
setObjWithBytesValue(o, objectReflectWithBytes)
assert.Equal(test, &ObjReflectWithBytesArray{0xfa, 0x16, 0x3e, [5]byte{0x85, 0x92, 0x77, 0xfa, 0x16}}, o)
}
func BenchmarkReflectUnmarshalObjWithBytes(b *testing.B) {
o := &ObjReflectWithBytesArray{}
for n := 0; n < b.N; n++ {
setObjWithBytesValue(o, []byte{0xfa, 0x16, 0x3e, 0x85, 0x92, 0x77, 0xfa, 0x16})
}
}
func BenchmarkReflectUnmarshalObjWithReader(b *testing.B) {
o := &ObjWithBytesArray{}
for n := 0; n < b.N; n++ {
setObjWithBytesValue(o, []byte{0xfa, 0x16, 0x3e, 0x85, 0x92, 0x77, 0xfa, 0x16})
}
}
func TestReflectUnmarshalObjWithBytesSlice(test *testing.T) {
o := &ObjWithBytesSlice{}
setObjWithBytesValue(o, objectReflectWithBytes)
assert.Equal(test, &ObjWithBytesSlice{0xfa, 0x16, 0x3e, []byte{0x85, 0x92, 0x77, 0xfa, 0x16}}, o)
}
func BenchmarkReflectUnmarshalObjWithBytesSlice(b *testing.B) {
o := &ObjWithBytesSlice{}
for n := 0; n < b.N; n++ {
setObjWithBytesValue(o, []byte{0xfa, 0x16, 0x3e, 0x85, 0x92, 0x77, 0xfa, 0x16})
}
}
func BenchmarkReflectUnmarshalObjWithReaderSlice(b *testing.B) {
o := &ObjWithBytesSlice{}
for n := 0; n < b.N; n++ {
setObjWithBytesValue(o, []byte{0xfa, 0x16, 0x3e, 0x85, 0x92, 0x77, 0xfa, 0x16})
}
}