-
Notifications
You must be signed in to change notification settings - Fork 0
/
convert_test.go
217 lines (206 loc) · 4.89 KB
/
convert_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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
package gdk
import (
"reflect"
"testing"
)
func TestToInt64(t *testing.T) {
tests := []struct {
in interface{}
expected int64
err error
}{
{int8(15), 15, nil},
{15, 15, nil},
{uint(15), 15, nil},
{uint8(15), 15, nil},
{uint16(15), 15, nil},
{uint32(15), 15, nil},
{uint64(15), 15, nil},
{"one", 0, ERR_NEED_NUMERIC},
}
for _, test := range tests {
if out, err := ToInt64(test.in); out != test.expected || (err != nil && err != test.err) {
t.Errorf("ToInt64 invalid, test:%v, out:%v, err:%v", test, out, err)
}
}
}
func TestBytesToFloat64(t *testing.T) {
tests := []struct {
in []byte
expected float64
err error
}{
{Float64ToBytes(10.21), 10.21, nil},
{[]byte("20.10"), 0, ERR_BYTES_INVALILD},
}
for _, test := range tests {
if out, err := BytesToFloat64(test.in); out != test.expected || err != nil && err != ERR_BYTES_INVALILD {
t.Errorf("BytesToFloat64 invalid, test:%v, out:%v, err:%v", test, out, err)
}
}
}
func TestFloat64ToStr(t *testing.T) {
tests := []struct {
in float64
prec int
expected string
}{
{10.21, 3, "10.210"},
{10.21, 1, "10.2"},
{10.21, 2, "10.21"},
{10.215, 3, "10.215"},
{10.215, 2, "10.21"},
}
for _, test := range tests {
if out := Float64ToStr(test.in, test.prec); out != test.expected {
t.Errorf("Float64ToStr invalid, test:%v, out:%v", test, out)
}
}
}
func TestStrToFloat64(t *testing.T) {
tests := []struct {
in string
precision int
expected float64
}{
{"10.211", 2, 10.21},
{"10.218", 3, 10.218},
{"10.211", 1, 10.2},
{"10.211", 0, 10},
{"10.211", 5, 10.21100},
}
for _, test := range tests {
if out := StrToFloat64(test.in, test.precision); out != test.expected {
t.Errorf("StrToFloat64 invalid, test:%v, out:%v", test, out)
}
}
}
func TestStrToFloat64Round(t *testing.T) {
tests := []struct {
in string
precision int
round bool
expected float64
}{
{"10.211", 2, false, 10.21},
{"10.218", 3, true, 10.218},
{"10.213", 2, true, 10.21},
{"10.218", 2, true, 10.22},
{"10.218", 1, true, 10.2},
{"10.211", 0, true, 10},
{"10.211", 5, false, 10.21100},
}
for _, test := range tests {
if out := StrToFloat64Round(test.in, test.precision, test.round); out != test.expected {
t.Errorf("StrToFloat64Round invalid, test:%v, out:%v", test, out)
}
}
}
func TestFloatPrecision(t *testing.T) {
tests := []struct {
in float64
precision int
round bool
expected float64
}{
{10.211, 2, false, 10.21},
{10.218, 3, true, 10.218},
{10.213, 2, true, 10.21},
{10.218, 2, true, 10.22},
{10.218, 1, true, 10.2},
{10.211, 0, true, 10},
{10.211, 5, false, 10.21100},
}
for _, test := range tests {
if out := Float64Precision(test.in, test.precision, test.round); out != test.expected {
t.Errorf("Float64Precision invalid, test:%v, out:%v", test, out)
}
}
}
type Person struct {
Name string
Age int
Height int
}
type Human struct {
name string
Age int
Height int
Score *int
}
func TestStructToMap(t *testing.T) {
tests := []struct {
in Person
expected map[string]interface{}
}{
{in: Person{"mike", 10, 10}, expected: map[string]interface{}{
"Age": 10,
"Height": 10,
"Name": "mike",
}},
{in: Person{"jack", 1, 10}, expected: map[string]interface{}{
"Age": 1,
"Height": 10,
"Name": "jack",
}},
}
for _, test := range tests {
if out := StructToMap(test.in); !reflect.DeepEqual(out, test.expected) {
t.Errorf("StructToMap invalid, test:%v, out:%v", test, out)
}
}
}
func TestMapToStruct(t *testing.T) {
tests := []struct {
in map[string]interface{}
expected *Person
}{
{expected: &Person{"mike", 10, 10}, in: map[string]interface{}{
"Age": 10,
"Height": 10,
"Name": "mike",
}},
{expected: &Person{"jack", 10, 102}, in: map[string]interface{}{
"Age": 10,
"Height": 102,
"Name": "jack",
}},
{expected: &Person{"mike", 0, 10}, in: map[string]interface{}{
"Age": nil,
"Height": 10,
"Name": "mike",
}},
}
for _, test := range tests {
if out, err := MapToStruct(test.in, &Person{}); err != nil || !reflect.DeepEqual(out, test.expected) {
t.Errorf("MapToStruct invalid, test:%v, out:%v, err:%v", test, out, err)
}
}
negativetests := []struct {
in map[string]interface{}
data interface{}
expected *Person
}{
{data: &Person{}, expected: nil, in: map[string]interface{}{
"Ages": 10,
"Height": 102,
"Address": map[string]interface{}{
"Address": "china",
"code": 00000,
},
}},
{data: &Human{}, expected: nil, in: map[string]interface{}{
"name": "mike",
"Age": 10,
"Height": 102,
}},
{data: &Human{}, expected: nil, in: map[string]interface{}{
"Score": "string",
}},
}
for _, test := range negativetests {
if out, err := MapToStruct(test.in, test.data); err == nil {
t.Errorf("MapToStruct invalid, test:%v, out:%v", test, out)
}
}
}