-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfloat_test.go
239 lines (189 loc) · 4.37 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
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
package round_test
import (
"fmt"
"math"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/portfoliotree/round"
)
func TestRecursive(t *testing.T) {
t.Run("float64", func(t *testing.T) {
float := 9.87654321
err := round.Recursive(&float, 3)
require.NoError(t, err)
assert.Equal(t, 9.877, float)
})
t.Run("float64 ptr", func(t *testing.T) {
float := 9.87654321
data := &float
err := round.Recursive(&data, 3)
require.NoError(t, err)
assert.Equal(t, 9.877, float)
})
t.Run("field", func(t *testing.T) {
data := struct {
Float float64
}{
Float: 9.87654321,
}
err := round.Recursive(&data, 3)
require.NoError(t, err)
assert.Equal(t, 9.877, data.Float)
})
t.Run("slice", func(t *testing.T) {
data := []float64{
1.87654321,
2.87654321,
3.87654321,
}
err := round.Recursive(&data, 3)
require.NoError(t, err)
assert.Equal(t, []float64{
1.877,
2.877,
3.877,
}, data)
})
t.Run("array", func(t *testing.T) {
const size = 3
data := [size]float64{
1.87654321,
2.87654321,
3.87654321,
}
err := round.Recursive(&data, 3)
require.NoError(t, err)
assert.Equal(t, [size]float64{
1.877,
2.877,
3.877,
}, data)
})
t.Run("map values", func(t *testing.T) {
data := map[string]float64{
"a": 1.87654321,
"b": 2.87654321,
"c": 3.87654321,
}
err := round.Recursive(&data, 3)
require.NoError(t, err)
assert.Equal(t, map[string]float64{
"a": 1.877,
"b": 2.877,
"c": 3.877,
}, data)
})
t.Run("private field", func(t *testing.T) {
data := struct {
float float64
}{
float: 9.87654321,
}
err := round.Recursive(&data, 3)
require.NoError(t, err)
assert.Equal(t, 9.87654321, data.float)
})
t.Run("slice field", func(t *testing.T) {
data := struct {
Floats []float64
}{
Floats: []float64{
1.87654321,
2.87654321,
3.87654321,
},
}
err := round.Recursive(&data, 3)
require.NoError(t, err)
assert.Equal(t, []float64{
1.877,
2.877,
3.877,
}, data.Floats)
})
t.Run("field with precision tag", func(t *testing.T) {
n := 5.4321
data := struct {
Floats []float64 `precision:"1"`
Ptr *float64 `precision:"0"`
}{
Floats: []float64{
1.87654321,
2.87654321,
3.87654321,
},
Ptr: &n,
}
err := round.Recursive(&data, 3)
require.NoError(t, err)
assert.Equal(t, []float64{
1.9,
2.9,
3.9,
}, data.Floats)
assert.Equal(t, float64(5), *data.Ptr)
})
t.Run("math.Inf", func(t *testing.T) {
float := math.Inf(1)
err := round.Recursive(&float, 3)
require.NoError(t, err)
assert.Equal(t, math.Inf(1), float)
})
t.Run("negative math.Inf", func(t *testing.T) {
float := math.Inf(-1)
err := round.Recursive(&float, 3)
require.NoError(t, err)
assert.Equal(t, math.Inf(-1), float, "it should not be changed")
})
t.Run("negative math.Inf", func(t *testing.T) {
float := math.Inf(-1)
err := round.Recursive(&float, 3)
require.NoError(t, err)
assert.Equal(t, math.Inf(-1), float, "it should not be changed")
})
t.Run("handles errors", func(t *testing.T) {
f := math.NaN()
data := struct {
Floats []*float64
}{
Floats: []*float64{
&f,
},
}
err := round.Recursive(&data, 0)
assert.ErrorContains(t, err, "not a number")
assert.ErrorContains(t, err, ".Floats[0]")
})
t.Run("malformed precision tag", func(t *testing.T) {
var data struct {
Float float64 `precision:"abc"`
}
err := round.Recursive(&data, 0)
assert.ErrorContains(t, err, "precision tag")
})
t.Run("negative precision tag", func(t *testing.T) {
var data struct {
Float float64 `precision:"-1"`
}
data.Float = 987.654
err := round.Recursive(&data, 0)
require.NoError(t, err)
assert.Equal(t, float64(990), data.Float)
})
}
func ExampleDecimal() {
const pi = 3.14159265359
// rounding up the 4th decimal place
piToFourDigits := round.Decimal(pi, 4)
fmt.Printf("%g == %g is %t\n", piToFourDigits, 3.1416, piToFourDigits == 3.1416)
fmt.Printf("%g < %g is %t\n", piToFourDigits, pi, piToFourDigits < 3.1416)
// rounding down the 2nd decimal place
piToTwoDigits := round.Decimal(pi, 2)
fmt.Printf("%g < %g is %t\n", piToTwoDigits, pi, piToTwoDigits < pi)
fmt.Printf("%q\n", fmt.Sprintf("%g", piToTwoDigits))
// Output: 3.1416 == 3.1416 is true
// 3.1416 < 3.14159265359 is false
// 3.14 < 3.14159265359 is true
// "3.14"
}