-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflag_reflect_number_float_test.go
136 lines (119 loc) · 4.43 KB
/
flag_reflect_number_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
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
package clif
import (
"context"
"errors"
"math"
"math/big"
"reflect"
"strconv"
"testing"
)
func TestNewValueFromFloat_success(t *testing.T) {
t.Parallel()
type testCase struct {
input FlagValue
expected float64
target any
}
testCases := map[string]testCase{
// float32
"float32/0": {input: FlagValue{Set: true, Raw: "0"}, expected: float64(0), target: float32(0)},
"float32/-1": {input: FlagValue{Set: true, Raw: "-1"}, expected: float64(-1), target: float32(0)},
"float32/1": {input: FlagValue{Set: true, Raw: "1"}, expected: float64(1), target: float32(0)},
"float32/max-float": {input: FlagValue{Set: true, Raw: strconv.FormatFloat(float64(math.MaxFloat32), 'f', -1, 32)}, expected: math.MaxFloat32, target: float32(0)},
// float64
"float64/0": {input: FlagValue{Set: true, Raw: "0"}, expected: float64(0), target: float64(0)},
"float64/-1": {input: FlagValue{Set: true, Raw: "-1"}, expected: float64(-1), target: float64(0)},
"float64/1": {input: FlagValue{Set: true, Raw: "1"}, expected: float64(1), target: float64(0)},
"float64/max-float": {input: FlagValue{Set: true, Raw: strconv.FormatFloat(float64(math.MaxFloat64), 'f', -1, 64)}, expected: math.MaxFloat64, target: float64(0)},
"float64/inf": {input: FlagValue{Set: true, Raw: "inf"}, expected: math.Inf(1), target: float64(0)},
"float64/infinity": {input: FlagValue{Set: true, Raw: "infinity"}, expected: math.Inf(1), target: float64(0)},
"float64/-inf": {input: FlagValue{Set: true, Raw: "-inf"}, expected: math.Inf(-1), target: float64(0)},
"float64/-infinity": {input: FlagValue{Set: true, Raw: "-infinity"}, expected: math.Inf(-1), target: float64(0)},
"float64/+inf": {input: FlagValue{Set: true, Raw: "+inf"}, expected: math.Inf(1), target: float64(0)},
"float64/+infinity": {input: FlagValue{Set: true, Raw: "+infinity"}, expected: math.Inf(1), target: float64(0)},
"float64/nan": {input: FlagValue{Set: true, Raw: "NaN"}, expected: math.NaN(), target: float64(0)},
}
for name, test := range testCases {
t.Run(name, func(t *testing.T) {
t.Parallel()
ctx := context.Background()
res, err := newValueFromFloat(ctx, test.input, reflect.ValueOf(test.target))
if err != nil {
t.Fatalf("Unexpected error %s", err)
}
got := res.Float()
if got != test.expected {
if math.IsNaN(test.expected) && math.IsNaN(got) {
return
}
if math.IsInf(test.expected, -1) && math.IsInf(got, -1) {
return
}
if math.IsInf(test.expected, 1) && math.IsInf(got, 1) {
return
}
t.Fatalf("Expected %v, got %v", test.expected, got)
}
})
}
}
func TestNewValueFromFloat_error_syntax(t *testing.T) {
t.Parallel()
testCases := map[string]string{
"empty": "",
"invalidValue": "not a number",
}
for name, input := range testCases {
t.Run(name, func(t *testing.T) {
t.Parallel()
ctx := context.Background()
var target float64
_, err := newValueFromFloat(ctx, FlagValue{Set: true, Raw: input}, reflect.ValueOf(target))
if err == nil {
t.Fatal("Expected error, got none")
}
numError := &strconv.NumError{}
if !errors.As(err, &numError) {
t.Fatalf("Expected strconv.NumError, got %T: %v", err, err)
}
if !errors.Is(numError.Err, strconv.ErrSyntax) {
t.Fatalf("Expected strconv.NumError to be reporting a strconv.ErrSyntax, got %v instead", numError.Err)
}
})
}
}
func TestNewValueFromFloat_error_overflow(t *testing.T) {
t.Parallel()
type testCase struct {
target any
value *big.Float
}
testCases := map[string]testCase{
"float32": {
value: big.NewFloat(0).Add(big.NewFloat(float64(math.MaxFloat32)), big.NewFloat(math.MaxFloat32/2)),
target: float32(0),
},
"float64": {
value: big.NewFloat(0).Add(big.NewFloat(float64(math.MaxFloat64)), big.NewFloat(1000)),
target: float64(0),
},
}
for name, test := range testCases {
t.Run(name, func(t *testing.T) {
t.Parallel()
ctx := context.Background()
_, err := newValueFromFloat(ctx, FlagValue{Set: true, Raw: test.value.String()}, reflect.ValueOf(test.target))
if err == nil {
t.Fatal("Expected error, got none")
}
numError := &strconv.NumError{}
if !errors.As(err, &numError) {
t.Fatalf("Expected strconv.NumError, got %T: %v", err, err)
}
if !errors.Is(numError.Err, strconv.ErrRange) {
t.Fatalf("Expected strconv.NumError to be reporting a strconv.ErrRange, got %v instead", numError.Err)
}
})
}
}