-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconversion_test.go
299 lines (253 loc) · 6.37 KB
/
conversion_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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
package conversion
import (
"bytes"
"fmt"
"math"
"reflect"
"github.com/stretchr/testify/assert"
"testing"
"log"
)
var (
optBig = &Option{Endian: BigEndian}
optLittle = &Option{Endian: LittleEndian}
)
var (
testInputF64 = float64(-561.2863)
testInputF64Bytes = []byte{0xc0, 0x81, 0x8a, 0x4a, 0x57, 0xa7, 0x86, 0xc2}
)
var (
testInputF32 = float32(-561.2863)
testInputF32Bytes = []byte{0xc4, 0x0c, 0x52, 0x53}
)
func ExampleIntToFloat32() {
fmt.Println(math.MinInt64, math.MaxInt64)
fmt.Printf("%.2f\n", IntToFloat32(math.MinInt64))
fmt.Printf("%.2f\n", IntToFloat32(math.MaxInt64))
// Output:
// -9223372036854775808 9223372036854775807
// -9223372036854775808.00
// 9223372036854775808.00
}
// areSameErrors returns true if both err and err2 are <nil> or have the same error message.
func areSameErrors(err error, err2 error) bool {
if err != nil && err2 != nil {
return err.Error() == err2.Error()
}
return err == err2
}
func ExampleFloat32ToBytes() {
x := float32(-561.2863) // -561.2863, 0xc40c5253
bsBig := Float32ToBytes(x, optBig)
fmt.Printf("%#02v\n", bsBig)
bsLittle := Float32ToBytes(x, optLittle)
fmt.Printf("%#02v\n", bsLittle)
// Output: []byte{0xc4, 0x0c, 0x52, 0x53}
// []byte{0x53, 0x52, 0x0c, 0xc4}
}
func ExampleBytesToFloat32() {
xs := []byte{0xc4, 0x0c, 0x52, 0x53} // -561.2863, 0xc40c5253
fx, err := BytesToFloat32(xs, optBig)
if err != nil {
log.Fatal(err)
}
fmt.Printf("%v\n", fx)
// Output: -561.2863
}
func TestBytesToFloat32_failBadInput(t *testing.T) {
// bad input as 32bits should have a length of 4.
xs := []byte{0xc4, 0x0c, 0x52}
_, err := BytesToFloat32(xs, optBig)
assert.EqualError(t, err, "length of []byte should be 4 or bigger")
}
func TestBytesToFloat32_littleEndian(t *testing.T) {
xs := []byte{0x53, 0x52, 0x0c, 0xc4} // -561.2863
fx, err := BytesToFloat32(xs, optLittle)
assert.NoError(t, err)
assert.InEpsilon(t, -561.2863, fx, 1e-4)
}
func ExampleFloat64ToBytes() {
x := -561.2863 // -561.2863, 0xc0818a4a57a786c2
bs := Float64ToBytes(x, optBig)
fmt.Printf("%#02v\n", bs)
// Output: []byte{0xc0, 0x81, 0x8a, 0x4a, 0x57, 0xa7, 0x86, 0xc2}
}
func TestFloat64ToBytes_littleEndian(t *testing.T) {
x := -561.2863
bs := Float64ToBytes(x, optLittle)
assert.Equal(t, []byte{0xc2, 0x86, 0xa7, 0x57, 0x4a, 0x8a, 0x81, 0xc0}, bs)
}
func ExampleBytesToFloat64() {
xs := []byte{0xc0, 0x81, 0x8a, 0x4a, 0x57, 0xa7, 0x86, 0xc2} // -561.2863, 0xc0818a4a57a786c2
fx, err := BytesToFloat64(xs, optBig)
if err != nil {
log.Fatal(err)
}
fmt.Printf("%v\n", fx)
// Output: -561.2863
}
func TestBytesToFloat64_invalidInput(t *testing.T) {
invalidInput := []byte{0x1, 0x2, 0x3}
_, err := BytesToFloat64(invalidInput, nil)
assert.EqualError(t, err, "length of []byte should be 8 or bigger")
}
func TestBytesToFloat64_littleEndian(t *testing.T) {
xs := []byte{0xc2, 0x86, 0xa7, 0x57, 0x4a, 0x8a, 0x81, 0xc0} // -561.2863, 0xc0818a4a57a786c2
fx, err := BytesToFloat64(xs, optLittle)
assert.NoError(t, err)
assert.InEpsilon(t, -561.2863, fx, 1e-4)
}
func TestFloat32sToBytes(t *testing.T) {
t.Parallel()
type args struct {
xs []float32
o *Option
}
testCases := map[string]struct {
args args
want []byte
wantErr error
}{
"it should convert []float32 to []byte": {
args: args{[]float32{float32(-561.2863)}, nil},
want: []byte{0xc4, 0x0c, 0x52, 0x53},
wantErr: nil,
},
"it should return empty []byte if []float32 is empty ": {
args: args{[]float32{}, nil},
want: []byte{},
wantErr: nil,
},
}
for testName, testCase := range testCases {
tn, tc := testName, testCase
t.Run(tn, func(t *testing.T) {
t.Parallel()
got, got1 := Float32sToBytes(tc.args.xs, tc.args.o)
if !bytes.Equal(got, tc.want) {
t.Errorf("Float32sToBytes() got = %v, want %v", got, tc.want)
}
if !areSameErrors(got1, tc.wantErr) {
t.Errorf("Float32sToBytes() got1 = %v, want %v", got1, tc.wantErr)
}
})
}
}
func TestBytesToFloat32s(t *testing.T) {
t.Parallel()
type args struct {
xs []byte
o *Option
}
testCases := map[string]struct {
args args
want []float32
wantErr bool
}{
"it should convert []byte to []float32": {
args: args{
xs: testInputF32Bytes,
o: nil,
},
want: []float32{testInputF32},
wantErr: false,
},
"it should return an error when the input is invalid": {
args: args{
xs: []byte{0x0c, 0x52, 0x53},
o: nil,
},
want: nil,
wantErr: true,
},
}
for testName, testCase := range testCases {
tn, tc := testName, testCase
t.Run(tn, func(t *testing.T) {
t.Parallel()
got, err := BytesToFloat32s(tc.args.xs, tc.args.o)
if tc.wantErr {
assert.Error(t, err)
} else {
assert.NoError(t, err)
}
assert.Equal(t, tc.want, got)
})
}
}
func TestFloat64sToBytes(t *testing.T) {
t.Parallel()
type args struct {
xs []float64
o *Option
}
testCases := map[string]struct {
name string
args args
want []byte
wantErr error
}{
"it should convert []float64 to []byte": {
args: args{[]float64{testInputF64}, nil},
want: testInputF64Bytes,
wantErr: nil,
},
}
for testName, testCase := range testCases {
tn, tc := testName, testCase
t.Run(tn, func(t *testing.T) {
t.Parallel()
got, got1 := Float64sToBytes(tc.args.xs, tc.args.o)
if !bytes.Equal(got, tc.want) {
t.Errorf("Float64sToBytes() got = %v, want %v", got, tc.want)
}
if !areSameErrors(got1, tc.wantErr) {
t.Errorf("Float64sToBytes() got1 = %v, want %v", got1, tc.wantErr)
}
})
}
}
func TestBytesToFloat64s(t *testing.T) {
t.Parallel()
type args struct {
xs []byte
o *Option
}
testCases := map[string]struct {
args args
want []float64
wantErr bool
}{
"it should convert []byte to []float64": {
args: args{
xs: testInputF64Bytes,
o: nil,
},
want: []float64{testInputF64},
wantErr: false,
},
"it should fail if the input is not []float64": {
args: args{
xs: []byte{0x1, 0x2, 0x3},
o: nil,
},
want: nil,
wantErr: true,
},
}
for testName, testCase := range testCases {
tn, tc := testName, testCase
t.Run(tn, func(t *testing.T) {
t.Parallel()
got, err := BytesToFloat64s(tc.args.xs, tc.args.o)
if tc.wantErr {
assert.Error(t, err)
} else {
assert.NoError(t, err)
}
if !reflect.DeepEqual(got, tc.want) {
t.Errorf("BytesToFloat64s() got = %v, want %v", got, tc.want)
}
})
}
}