-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathfloat_test.go
80 lines (70 loc) · 2.25 KB
/
float_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
package cast_test
import (
"testing"
"github.com/datasweet/cast"
"github.com/stretchr/testify/assert"
)
func TestAsFloat64(t *testing.T) {
testFloat64(t, nil, 0, false)
testFloat64(t, "123", 123, true)
testFloat64(t, "wrong", 0, false)
testFloat64(t, true, 1, true)
testFloat64(t, false, 0, true)
testFloat64(t, 123, 123, true)
testFloat64(t, int8(123), 123, true)
testFloat64(t, int16(123), 123, true)
testFloat64(t, int32(123), 123, true)
testFloat64(t, int64(123), 123, true)
testFloat64(t, uint(123), 123, true)
testFloat64(t, uint8(123), 123, true)
testFloat64(t, uint16(123), 123, true)
testFloat64(t, uint32(123), 123, true)
testFloat64(t, uint64(123), 123, true)
testFloat64(t, float32(123), 123, true)
testFloat64(t, float64(123), 123, true)
}
func TestAsFloat64Slice(t *testing.T) {
arr, ok := cast.AsFloat64Slice(1, 2, true)
assert.True(t, ok)
assert.Equal(t, []float64{1, 2, 1}, arr)
arr, ok = cast.AsFloat64Slice(1, 2, true, "no")
assert.False(t, ok)
assert.Equal(t, []float64{1, 2, 1, 0}, arr)
}
func TestAsFloat32(t *testing.T) {
testFloat32(t, nil, 0, false)
testFloat32(t, "123", 123, true)
testFloat32(t, "wrong", 0, false)
testFloat32(t, true, 1, true)
testFloat32(t, false, 0, true)
testFloat32(t, 123, 123, true)
testFloat32(t, int8(123), 123, true)
testFloat32(t, int16(123), 123, true)
testFloat32(t, int32(123), 123, true)
testFloat32(t, int64(123), 123, true)
testFloat32(t, uint(123), 123, true)
testFloat32(t, uint8(123), 123, true)
testFloat32(t, uint16(123), 123, true)
testFloat32(t, uint32(123), 123, true)
testFloat32(t, uint64(123), 123, true)
testFloat32(t, float32(123), 123, true)
testFloat32(t, float64(123), 123, true)
}
func TestAsFloat32Slice(t *testing.T) {
arr, ok := cast.AsFloat32Slice(1, 2, true)
assert.True(t, ok)
assert.Equal(t, []float32{1, 2, 1}, arr)
arr, ok = cast.AsFloat32Slice(1, 2, true, "no")
assert.False(t, ok)
assert.Equal(t, []float32{1, 2, 1, 0}, arr)
}
func testFloat64(t *testing.T, value interface{}, expected float64, ok bool) {
b, o := cast.AsFloat64(value)
assert.Equal(t, expected, b)
assert.Equal(t, ok, o)
}
func testFloat32(t *testing.T, value interface{}, expected float32, ok bool) {
b, o := cast.AsFloat32(value)
assert.Equal(t, expected, b)
assert.Equal(t, ok, o)
}