-
Notifications
You must be signed in to change notification settings - Fork 0
/
converter_test.go
253 lines (246 loc) · 9.6 KB
/
converter_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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
package gotil_test
import (
"fmt"
"reflect"
"testing"
"github.com/gotilty/gotil"
)
func TestToFloat64(t *testing.T) {
// Float64 to Int
var input_f int64 = 123
var expected_f float64 = 123.00
result_f, err_f := gotil.ToFloat64(input_f)
if !reflect.DeepEqual(expected_f, result_f) {
t.Errorf("Convert.ToUint64 does not works expected\ncase: %d\nexpected: %f taken: %f error: %e", input_f, expected_f, result_f, err_f)
}
// String to Int
input_s := "123.12"
var expected_s float64 = 123.12
result_s, err_s := gotil.ToFloat64(input_s)
if !reflect.DeepEqual(expected_s, result_s) {
t.Errorf("Convert.ToUint64 does not works expected\ncase: %s\nexpected: %f taken: %f error: %e", input_s, expected_s, result_s, err_s)
}
// Bool to Int
input_b := true
var expected_b float64 = 1
result_b, err_b := gotil.ToFloat64(input_b)
if !reflect.DeepEqual(expected_b, result_b) {
t.Errorf("Convert.ToUint64 does not works expected\ncase: %s\nexpected: %f taken: %f error: %e", input_s, expected_s, result_s, err_b)
}
}
func TestToFloat32(t *testing.T) {
// Float64 to Int
var input_f int64 = 123
var expected_f float32 = 123.00
result_f, err_f := gotil.ToFloat32(input_f)
if !reflect.DeepEqual(expected_f, result_f) {
t.Errorf("Convert.ToUint64 does not works expected\ncase: %d\nexpected: %f taken: %f error: %e", input_f, expected_f, result_f, err_f)
}
// String to Int
input_s := "123.12"
var expected_s float32 = 123.12
result_s, err_s := gotil.ToFloat32(input_s)
if !reflect.DeepEqual(expected_s, result_s) {
t.Errorf("Convert.ToUint64 does not works expected\ncase: %s\nexpected: %f taken: %f error: %e", input_s, expected_s, result_s, err_s)
}
// Bool to Int
input_b := true
var expected_b float32 = 1
result_b, err_b := gotil.ToFloat32(input_b)
if !reflect.DeepEqual(expected_b, result_b) {
t.Errorf("Convert.ToUint64 does not works expected\ncase: %s\nexpected: %f taken: %f error: %e", input_s, expected_s, result_s, err_b)
}
}
func TestToInt(t *testing.T) {
// Float64 to Int
input_f := 123.12
var expected_f int = 123
result_f, err_f := gotil.ToInt(input_f)
if !reflect.DeepEqual(expected_f, result_f) {
t.Errorf("Convert.ToUint64 does not works expected\ncase: %f\nexpected: %d taken: %d error: %e", input_f, expected_f, result_f, err_f)
}
// String to Int
input_s := "123"
var expected_s int = 123
result_s, err_s := gotil.ToInt(input_s)
if !reflect.DeepEqual(expected_s, result_s) {
t.Errorf("Convert.ToUint64 does not works expected\ncase: %s\nexpected: %d taken: %d error: %e", input_s, expected_s, result_s, err_s)
}
// Bool to Int
input_b := true
var expected_b int = 1
result_b, err_b := gotil.ToInt(input_b)
if !reflect.DeepEqual(expected_b, result_b) {
t.Errorf("Convert.ToUint64 does not works expected\ncase: %s\nexpected: %d taken: %d error: %e", input_s, expected_s, result_s, err_b)
}
}
func TestToInt8(t *testing.T) {
// Float64 to Int8
input_f := 123.12
var expected_f int8 = 123
result_f, err_f := gotil.ToInt8(input_f)
if !reflect.DeepEqual(expected_f, result_f) {
t.Errorf("Convert.ToUint64 does not works expected\ncase: %f\nexpected: %d taken: %d error: %e", input_f, expected_f, result_f, err_f)
}
// String to Int8
input_s := "123"
var expected_s int8 = 123
result_s, err_s := gotil.ToInt8(input_s)
if !reflect.DeepEqual(expected_s, result_s) {
t.Errorf("Convert.ToUint64 does not works expected\ncase: %s\nexpected: %d taken: %d error: %e", input_s, expected_s, result_s, err_s)
}
// Bool to Int8
input_b := true
var expected_b int8 = 1
result_b, err_b := gotil.ToInt8(input_b)
if !reflect.DeepEqual(expected_b, result_b) {
t.Errorf("Convert.ToUint64 does not works expected\ncase: %s\nexpected: %d taken: %d error: %e", input_s, expected_s, result_s, err_b)
}
}
func TestToInt16(t *testing.T) {
// Float64 to Int16
input_f := 123.12
var expected_f int16 = 123
result_f, err_f := gotil.ToInt16(input_f)
if !reflect.DeepEqual(expected_f, result_f) {
t.Errorf("Convert.ToUint64 does not works expected\ncase: %f\nexpected: %d taken: %d error: %e", input_f, expected_f, result_f, err_f)
}
// String to Int16
input_s := "123"
var expected_s int16 = 123
result_s, err_s := gotil.ToInt16(input_s)
if !reflect.DeepEqual(expected_s, result_s) {
t.Errorf("Convert.ToUint64 does not works expected\ncase: %s\nexpected: %d taken: %d error: %e", input_s, expected_s, result_s, err_s)
}
// Bool to Int16
input_b := true
var expected_b int16 = 1
result_b, err_b := gotil.ToInt16(input_b)
if !reflect.DeepEqual(expected_b, result_b) {
t.Errorf("Convert.ToUint64 does not works expected\ncase: %s\nexpected: %d taken: %d error: %e", input_s, expected_s, result_s, err_b)
}
}
func TestToInt32(t *testing.T) {
// Float64 to Int32
input_f := 123.12
var expected_f int32 = 123
result_f, err_f := gotil.ToInt32(input_f)
if !reflect.DeepEqual(expected_f, result_f) {
t.Errorf("Convert.ToUint64 does not works expected\ncase: %f\nexpected: %d taken: %d error: %e", input_f, expected_f, result_f, err_f)
}
// String to Int32
input_s := "123"
var expected_s int32 = 123
result_s, err_s := gotil.ToInt32(input_s)
if !reflect.DeepEqual(expected_s, result_s) {
t.Errorf("Convert.ToUint64 does not works expected\ncase: %s\nexpected: %d taken: %d error: %e", input_s, expected_s, result_s, err_s)
}
// Bool to Int32
input_b := true
var expected_b int32 = 1
result_b, err_b := gotil.ToInt32(input_b)
if !reflect.DeepEqual(expected_b, result_b) {
t.Errorf("Convert.ToUint64 does not works expected\ncase: %s\nexpected: %d taken: %d error: %e", input_s, expected_s, result_s, err_b)
}
}
func TestToInt64(t *testing.T) {
// Float64 to Int64
input_f := 123.12
var expected_f int64 = 123
result_f, err_f := gotil.ToInt64(input_f)
if !reflect.DeepEqual(expected_f, result_f) {
t.Errorf("Convert.ToUint64 does not works expected\ncase: %f\nexpected: %d taken: %d error: %e", input_f, expected_f, result_f, err_f)
}
// String to Int64
input_s := "123"
var expected_s int64 = 123
result_s, err_s := gotil.ToInt64(input_s)
if !reflect.DeepEqual(expected_s, result_s) {
t.Errorf("Convert.ToUint64 does not works expected\ncase: %s\nexpected: %d taken: %d error: %e", input_s, expected_s, result_s, err_s)
}
// Bool to Int64
input_b := true
var expected_b int64 = 1
result_b, err_b := gotil.ToInt64(input_b)
if !reflect.DeepEqual(expected_b, result_b) {
t.Errorf("Convert.ToUint64 does not works expected\ncase: %s\nexpected: %d taken: %d error: %e", input_s, expected_s, result_s, err_b)
}
}
func TestToUint64(t *testing.T) {
// Float64 to Uint64
input_f := 123.12
var expected_f uint64 = 123
result_f, err_f := gotil.ToUint64(input_f)
if !reflect.DeepEqual(expected_f, result_f) {
t.Errorf("Convert.ToUint64 does not works expected\ncase: %f\nexpected: %d taken: %d error: %e", input_f, expected_f, result_f, err_f)
}
// String to Uint64
input_s := "123"
var expected_s uint64 = 123
result_s, err_s := gotil.ToUint64(input_s)
if !reflect.DeepEqual(expected_s, result_s) {
t.Errorf("Convert.ToUint64 does not works expected\ncase: %s\nexpected: %d taken: %d error: %e", input_s, expected_s, result_s, err_s)
}
// Bool to Uint64
input_b := true
var expected_b uint64 = 1
result_b, err_b := gotil.ToUint64(input_b)
if !reflect.DeepEqual(expected_b, result_b) {
t.Errorf("Convert.ToUint64 does not works expected\ncase: %s\nexpected: %d taken: %d error: %e", input_s, expected_s, result_s, err_b)
}
}
type toStringTestStruct struct {
name string
age int
}
// func TestToString(t *testing.T) {
// input_f := 123.12
// var expected_f string = "123.12"
// result_f, err_f := gotil.ToString(input_f)
// if !reflect.DeepEqual(expected_f, result_f) {
// t.Errorf("Convert.ToString does not works expected\ncase: %f\nexpected: %s taken: %s error: %e", input_f, expected_f, result_f, err_f)
// }
// input_s := 123
// var expected_s string = "123"
// result_s, err_s := gotil.ToString(input_s)
// if !reflect.DeepEqual(expected_s, result_s) {
// t.Errorf("Convert.ToString does not works expected\ncase: %d\nexpected: %s taken: %s error: %e", input_s, expected_s, result_s, err_s)
// }
// input_b := true
// var expected_b string = "true"
// result_b, err_b := gotil.ToString(input_b)
// if !reflect.DeepEqual(expected_b, result_b) {
// t.Errorf("Convert.ToString does not works expected\ncase: %t\nexpected: %s taken: %s error: %e", input_b, expected_b, result_b, err_b)
// }
// input_a := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
// var expected_a string = "[1 2 3 4 5 6 7 8 9 10]"
// result_a, err_a := gotil.ToString(input_a)
// if !reflect.DeepEqual(expected_a, result_a) {
// t.Errorf("Convert.ToString does not works expected\ncase: %b\nexpected: %s taken: %s error: %e", input_a, expected_a, result_a, err_a)
// }
// input_m := map[string][]int{"a": {1, 2, 3, 4, 5}, "b": {11, 22, 33, 44, 55}}
// var expected_m string = `map[a:[1 2 3 4 5] b:[11 22 33 44 55]]`
// result_m, err_m := gotil.ToString(input_m)
// if !reflect.DeepEqual(expected_m, result_m) {
// t.Errorf("Convert.ToString does not works expected\ncase: %v\nexpected: %s taken: %s error: %e", input_m, expected_m, result_m, err_m)
// }
// input_struct := toStringTestStruct{name: "gotil", age: 28}
// var expected_struct string = `{gotil 28}`
// result_struct, err_struct := gotil.ToString(input_struct)
// if !reflect.DeepEqual(expected_struct, result_struct) {
// t.Errorf("Convert.ToString does not works expected\ncase: %v\nexpected: %s taken: %s error: %e", input_struct, expected_struct, result_struct, err_struct)
// }
// }
func BenchmarkUint64(b *testing.B) {
for n := 0; n < b.N; n++ {
gotil.ToUint64("15")
}
}
func ExampleUint64() {
result, err := gotil.ToUint64("15")
if err == nil {
fmt.Println(result)
// Output: 15
}
return
}