forked from ericlagergren/decimal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbig_test.go
270 lines (250 loc) · 6.26 KB
/
big_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
package decimal_test
import (
"math"
"math/big"
"math/rand"
"strconv"
"strings"
"sync"
"testing"
"github.com/grodriguez85/decimal"
"github.com/grodriguez85/decimal/internal/test"
)
func TestBig_Abs(t *testing.T) { test.Abs.Test(t) }
func TestBig_Add(t *testing.T) { test.Add.Test(t) }
func TestBig_Class(t *testing.T) { test.Class.Test(t) }
func TestBig_Cmp(t *testing.T) { test.Cmp.Test(t) }
func TestBig_FMA(t *testing.T) { test.FMA.Test(t) }
func TestBig_Mul(t *testing.T) { test.Mul.Test(t) }
func TestBig_Neg(t *testing.T) { test.Neg.Test(t) }
func TestBig_Quantize(t *testing.T) { test.Quant.Test(t) }
func TestBig_Quo(t *testing.T) { test.Quo.Test(t) }
func TestBig_QuoInt(t *testing.T) { test.QuoInt.Test(t) }
func TestBig_Rat(t *testing.T) { test.CTR.Test(t) }
func TestBig_Reduce(t *testing.T) { test.Reduce.Test(t) }
func TestBig_Rem(t *testing.T) { test.Rem.Test(t) }
func TestBig_RoundToInt(t *testing.T) { test.RoundToInt.Test(t) }
func TestBig_SetString(t *testing.T) { test.CTS.Test(t) /* Same as CFS */ }
func TestBig_Sign(t *testing.T) { test.Sign.Test(t) }
func TestBig_SignBit(t *testing.T) { test.Signbit.Test(t) }
func TestBig_String(t *testing.T) { test.CTS.Test(t) }
func TestBig_Sub(t *testing.T) { test.Sub.Test(t) }
var rnd = rand.New(rand.NewSource(0))
func rndn(min, max int) int {
return rnd.Intn(max-min) + min
}
func randDec() string {
b := make([]byte, rndn(5, 50))
for i := range b {
b[i] = '0' + byte(rndn(0, 10))
}
if rnd.Intn(10) != 0 {
b[rndn(2, len(b))] = '.'
}
if b[0] == '0' {
if b[1] == '0' && b[2] != '.' {
b = b[1:]
}
b[0] = '-'
}
return string(b)
}
var randDecs = func() (a [5000]string) {
for i := range a {
a[i] = randDec()
}
return a
}()
func TestBig_Float(t *testing.T) {
for i, test := range randDecs {
flt, ok := new(big.Float).SetString(test)
if !ok {
t.Fatal("!ok")
}
fv := new(big.Float).SetPrec(flt.Prec())
xf := new(decimal.Big).SetFloat(flt).Float(fv)
if xf.String() != flt.String() {
t.Fatalf("#%d: wanted %f, got %f", i, flt, xf)
}
}
}
func TestBig_Int(t *testing.T) {
for i, test := range randDecs {
a, ok := new(decimal.Big).SetString(test)
if !ok {
t.Fatalf("#%d: !ok", i)
}
iv := test
switch x := strings.IndexByte(test, '.'); {
case x > 0:
iv = test[:x]
case x == 0:
iv = "0"
}
b, ok := new(big.Int).SetString(iv, 10)
if !ok {
t.Fatal("!ok")
}
if n := a.Int(nil); n.Cmp(b) != 0 {
t.Fatalf("#%d: wanted %q, got %q", i, b, n)
}
}
}
func TestBig_Int64(t *testing.T) {
for i, test := range randDecs {
a, ok := new(decimal.Big).SetString(test)
if !ok {
t.Fatalf("#%d: !ok", i)
}
iv := test
switch x := strings.IndexByte(test, '.'); {
case x > 0:
iv = test[:x]
case x == 0:
iv = "0"
}
n, ok := a.Int64()
gv, err := strconv.ParseInt(iv, 10, 64)
if (err == nil) != ok {
t.Fatalf("#%d: wanted %t, got %t", i, err == nil, ok)
}
if ok && (n != gv) {
t.Fatalf("#%d: wanted %d, got %d", i, gv, n)
}
}
}
func TestBig_Uint64(t *testing.T) {
for i, test := range randDecs {
a, ok := new(decimal.Big).SetString(test)
if !ok {
t.Fatalf("#%d: !ok", i)
}
iv := test
switch x := strings.IndexByte(test, '.'); {
case x > 0:
iv = test[:x]
case x == 0:
iv = "0"
}
n, ok := a.Uint64()
if _, err := strconv.ParseUint(iv, 10, 64); (err == nil) != ok {
t.Fatalf("#%d: wanted %t, got %t", i, err == nil, ok)
}
if !ok {
continue
}
if ns := strconv.FormatUint(n, 10); ns != iv {
t.Fatalf("#%d: wanted %q, got %q", i, iv, ns)
}
}
}
func TestBig_IsInt(t *testing.T) {
allZeros := func(s string) bool {
for _, c := range s {
if c != '0' {
return false
}
}
return true
}
for i, test := range randDecs {
x, ok := new(decimal.Big).SetString(test)
if !ok {
t.Fatal("TestBig_IsInt !ok")
}
j := strings.IndexByte(test, '.')
if got := x.IsInt(); got != (j < 0 || allZeros(test[j+1:])) {
t.Fatalf("#%d: (%q).IsInt() == %t", i, test, got)
}
}
}
// func TestBig_Format(t *testing.T) {
// tests := [...]struct {
// format string
// a string
// b string
// }{
// 0: {format: "%e", a: "1.234", b: "1.234"},
// 1: {format: "%s", a: "1.2134124124", b: "1.2134124124"},
// 2: {format: "%e", a: "1.00003e-12", b: "1.00003e-12"},
// 3: {format: "%E", a: "1.000003E-12", b: "1.000003E-12"},
// }
// for i, v := range tests {
// x, ok := new(decimal.Big).SetString(v.a)
// if !ok {
// t.Fatal("invalid SetString")
// }
// if fs := fmt.Sprintf(v.format, x); fs != v.b {
// t.Fatalf("#%d: wanted %q, got %q:", i, v.b, fs)
// }
// }
// }
func TestParallel(t *testing.T) {
x := decimal.New(4, 0)
y := decimal.New(3, 0)
var wg sync.WaitGroup
for i := 0; i < 50; i++ {
wg.Add(1)
go func() {
m := new(decimal.Big)
m.Add(x, y)
m.Mul(m, y)
m.Quo(m, x)
m.Sub(m, y)
m.FMA(m, x, y)
wg.Done()
}()
}
wg.Wait()
}
func TestBig_Prec(t *testing.T) {
// confirmed to work inside internal/arith/intlen_test.go
}
func TestBig_Round(t *testing.T) {
for i, test := range [...]struct {
v string
to int
res string
}{
0: {"5.5", 1, "6"},
1: {"1.234", 2, "1.2"},
2: {"1", 1, "1"},
3: {"9.876", 0, "9.876"},
4: {"5.65", 2, "5.6"},
5: {"5.0002", 2, "5"},
6: {"0.000158674", 6, "0.000158674"},
7: {"1.58089722856961873690377135139876745465351534188711107066818e+12288", 50, "1.5808972285696187369037713513987674546535153418871e+12288"},
} {
bd, _ := new(decimal.Big).SetString(test.v)
r, _ := new(decimal.Big).SetString(test.res)
if bd.Round(test.to).Cmp(r) != 0 {
t.Fatalf(`#%d:
wanted: %q
got : %q
`, i, test.res, bd)
}
}
}
func TestBig_Scan(t *testing.T) {
// TODO(eric): write this test
}
func TestBig_SetFloat64(t *testing.T) {
if testing.Short() {
t.Skip("skipping testing all 32-bit floats in short mode")
}
const eps = 1e-15
z := decimal.WithPrecision(17)
for x := uint32(0); x != math.MaxUint32; x++ {
f := float64(math.Float32frombits(x))
zf, _ := z.SetFloat64(f).Float64()
if math.Float64bits(zf) != math.Float64bits(f) {
if isSpecial(f) || isSpecial(zf) || math.Abs(zf-f) > eps {
t.Fatalf(`#%d:
wanted: %g
got : %g
`, x, f, zf)
}
}
}
}
func isSpecial(f float64) bool { return math.IsInf(f, 0) || math.IsNaN(f) }