-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfloat_test.go
More file actions
125 lines (109 loc) · 2.66 KB
/
float_test.go
File metadata and controls
125 lines (109 loc) · 2.66 KB
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
package converter
import (
"testing"
)
type floatCase struct {
name string
args any
want float64
wantErr bool
}
func TestCouldBeFloat(t *testing.T) {
tests := []struct {
name string
args interface{}
want bool
}{
{"int", 42, true},
{"float32", float32(42.0), true},
{"float64", 42.0, true},
{"string", "42", true},
{"bool", true, true},
{"complex128", complex128(42 + 42i), true},
{"unsupported type", struct{}{}, false},
{"nil", nil, false},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
if got := CouldBeFloat(tc.args); got != tc.want {
t.Errorf("CouldBeFloat() = %v, want %v", got, tc.want)
}
})
}
}
func TestToFloat32(t *testing.T) {
for _, tc := range initFloatTestCases() {
t.Run(tc.name, func(t *testing.T) {
if tc.wantErr {
defer func() {
if r := recover(); r == nil {
t.Errorf("The code did not panic")
}
}()
}
if got := ToFloat32(tc.args); float64(got) != tc.want {
t.Errorf("ToFloat32() = %v, want %v", got, tc.want)
}
})
}
}
func TestToFloat32WithErr(t *testing.T) {
for _, tc := range initFloatTestCases() {
t.Run(tc.name, func(t *testing.T) {
got, err := ToFloat32WithErr(tc.args)
if float64(got) != tc.want {
t.Errorf("ToFloat32WithErr() = %v, want %v", got, tc.want)
}
if (err != nil) != tc.wantErr {
t.Errorf("ToFloat32WithErr() error = %v, wantErr %v", err, tc.wantErr)
}
})
}
}
func TestToFloat64(t *testing.T) {
for _, tc := range initFloatTestCases() {
t.Run(tc.name, func(t *testing.T) {
if tc.wantErr {
defer func() {
if r := recover(); r == nil {
t.Errorf("The code did not panic")
}
}()
}
if got := ToFloat64(tc.args); got != tc.want {
t.Errorf("ToFloat32() = %v, want %v", got, tc.want)
}
})
}
}
func TestToFloat64WithErr(t *testing.T) {
for _, tc := range initFloatTestCases() {
t.Run(tc.name, func(t *testing.T) {
got, err := ToFloat64WithErr(tc.args)
if got != tc.want {
t.Errorf("ToFloat64WithErr() = %v, want %v", got, tc.want)
}
if (err != nil) != tc.wantErr {
t.Errorf("ToFloat64WithErr() error = %v, wantErr %v", err, tc.wantErr)
}
})
}
}
func initFloatTestCases() []floatCase {
p := 23
var pn *int
return []floatCase{
{"int", 42, 42.0, false},
{"uint", uint(42), 42.0, false},
{"float32", float32(42.0), 42.0, false},
{"float64", 42.0, 42.0, false},
{"string", "42", 42.0, false},
{"bool", true, 1.0, false},
{"bool", false, 0.0, false},
{"complex128", complex128(42 + 42i), 42.0, false},
{"unsupported type", struct{}{}, 0, true},
{"nil", nil, 0, true},
{"nil pointer", pn, 0, true},
{"pointer", &p, 23.0, false},
}
}