-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandler_test.go
166 lines (151 loc) · 3.85 KB
/
handler_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
166
package jsonpack
import (
"math"
"reflect"
"testing"
ibuf "github.com/arloliu/jsonpack/buffer"
"github.com/modern-go/reflect2"
)
func testNumber(t *testing.T, buf *ibuf.Buffer, data interface{}) {
var o1 opHandler
var o2 opHandler
kind := reflect.TypeOf(data).Kind()
switch data.(type) {
case int8:
o1 = &int8Op{}
case int16:
o1 = &int16LEOp{}
o2 = &int16BEOp{}
case int32:
o1 = &int32LEOp{}
o2 = &int32BEOp{}
case int64:
o1 = &int64LEOp{}
o2 = &int64BEOp{}
case uint8:
o1 = &uint8Op{}
case uint16:
o1 = &uint16LEOp{}
o2 = &uint16BEOp{}
case uint32:
o1 = &uint32LEOp{}
o2 = &uint32BEOp{}
case uint64:
o1 = &uint64LEOp{}
o2 = &uint64BEOp{}
case float32:
o1 = &float32LEOp{}
o2 = &float32BEOp{}
case float64:
o1 = &float64LEOp{}
o2 = &float64BEOp{}
default:
t.Errorf("Unsupported type: %s", kind)
}
buf.Seek(0, false)
o1.encodeDynamic(buf, &operation{}, data)
buf.Seek(0, false)
v1, _ := o1.decodeDynamic(buf, &operation{}, data)
if v1 != data {
if o2 != nil {
t.Errorf("%s operation(LE) write fail, expect %v, got %v", kind, data, v1)
} else {
t.Errorf("%s operation write fail, expect %v, got %v", kind, data, v1)
}
}
buf.Seek(0, false)
o1.encodeStruct(buf, &structOperation{}, reflect2.PtrOf(data))
buf.Seek(0, false)
v2, _ := o1.decodeDynamic(buf, &operation{}, data)
if v2 != data {
if o2 != nil {
t.Errorf("%s operation(LE) encodeStruct fail, expect %v, got %v", kind, data, v1)
} else {
t.Errorf("%s operation encodeStruct fail, expect %v, got %v", kind, data, v1)
}
}
if o2 != nil {
buf.Seek(0, false)
o2.encodeDynamic(buf, &operation{}, data)
buf.Seek(0, false)
if v3, _ := o2.decodeDynamic(buf, &operation{}, data); v3 != data {
t.Errorf("%s operation(BE) write fail", kind)
}
buf.Seek(0, false)
o2.encodeStruct(buf, &structOperation{}, reflect2.PtrOf(data))
buf.Seek(0, false)
if v4, _ := o2.decodeDynamic(buf, &operation{}, data); v4 != data {
t.Errorf("%s operation(BE) encodeStruct fail", kind)
}
}
}
func TestOpHandler(t *testing.T) {
buf := ibuf.Create(1)
var o opHandler
o = &stringOp{}
testStr := "TestOpHandler test string"
o.encodeDynamic(buf, &operation{}, testStr)
buf.Seek(0, false)
s, _ := o.decodeDynamic(buf, &operation{}, nil)
if s.(string) != testStr {
t.Errorf("string operation fail")
}
buf.Seek(0, false)
o = &booleanOp{}
o.encodeDynamic(buf, &operation{}, true)
buf.Seek(0, false)
v1, _ := o.decodeDynamic(buf, &operation{}, nil)
if v1.(bool) != true {
t.Errorf("boolean operation fail")
}
testNumber(t, buf, int8(127))
testNumber(t, buf, int8(-128))
testNumber(t, buf, int16(math.MaxInt16))
testNumber(t, buf, int16(math.MinInt16))
testNumber(t, buf, int32(math.MaxInt32))
testNumber(t, buf, int32(math.MinInt32))
testNumber(t, buf, int64(math.MaxInt64))
testNumber(t, buf, int64(math.MinInt64))
testNumber(t, buf, uint8(255))
testNumber(t, buf, uint8(0))
testNumber(t, buf, uint16(math.MaxUint16))
testNumber(t, buf, uint16(0))
testNumber(t, buf, uint32(math.MaxUint32))
testNumber(t, buf, uint32(0))
testNumber(t, buf, uint64(math.MaxUint64))
testNumber(t, buf, uint64(0))
testNumber(t, buf, float32(math.MaxFloat32))
testNumber(t, buf, float32(-math.MaxFloat32))
testNumber(t, buf, float64(math.MaxFloat64))
testNumber(t, buf, float64(-math.MaxFloat64))
}
func BenchmarkOpHandlerTypeAssertion(b *testing.B) {
op := newOperation("", _float32BEOp, float32BEOpType)
var val int = 0
for i := 0; i < b.N; i++ {
switch op.handler.(type) {
case *objectOp:
val++
case *arrayOp:
val++
case *float32BEOp:
val++
default:
}
}
}
func BenchmarkOpHandlerCompareType(b *testing.B) {
op := newOperation("", _float32BEOp, float32BEOpType)
var val int = 0
for i := 0; i < b.N; i++ {
switch op.handlerType {
case objectOpType:
val++
case arrayOpType:
val++
case float32BEOpType:
val++
default:
}
}
}