This repository has been archived by the owner on Jun 25, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
decimal_test.go
312 lines (265 loc) · 6.3 KB
/
decimal_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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
package ion
import (
"fmt"
"math/big"
"testing"
)
func TestDecimalToString(t *testing.T) {
test := func(n int64, scale int32, expected string) {
t.Run(expected, func(t *testing.T) {
d := Decimal{
n: big.NewInt(n),
scale: scale,
}
actual := d.String()
if actual != expected {
t.Errorf("expected '%v', got '%v'", expected, actual)
}
})
}
test(0, 0, "0.")
test(0, -1, "0d1")
test(0, 1, "0d-1")
test(1, 0, "1.")
test(1, -1, "1d1")
test(1, 1, "1d-1")
test(-1, 0, "-1.")
test(-1, -1, "-1d1")
test(-1, 1, "-1d-1")
test(123, 0, "123.")
test(-456, 0, "-456.")
test(123, -5, "123d5")
test(-456, -5, "-456d5")
test(123, 1, "12.3")
test(123, 2, "1.23")
test(123, 3, "1.23d-1")
test(123, 4, "1.23d-2")
test(-456, 1, "-45.6")
test(-456, 2, "-4.56")
test(-456, 3, "-4.56d-1")
test(-456, 4, "-4.56d-2")
}
func TestParseDecimal(t *testing.T) {
test := func(in string, n *big.Int, scale int32) {
t.Run(in, func(t *testing.T) {
d, err := ParseDecimal(in)
if err != nil {
t.Fatal(err)
}
if n.Cmp(d.n) != 0 {
t.Errorf("wrong n; expected %v, got %v", n, d.n)
}
if scale != d.scale {
t.Errorf("wrong scale; expected %v, got %v", scale, d.scale)
}
})
}
test("0", big.NewInt(0), 0)
test("-0", big.NewInt(0), 0)
test("0D0", big.NewInt(0), 0)
test("-0d-1", big.NewInt(0), 1)
test("1.", big.NewInt(1), 0)
test("1.0", big.NewInt(10), 1)
test("0.123", big.NewInt(123), 3)
test("1d0", big.NewInt(1), 0)
test("1d1", big.NewInt(1), -1)
test("1d+1", big.NewInt(1), -1)
test("1d-1", big.NewInt(1), 1)
test("-0.12d4", big.NewInt(-12), -2)
}
func absF(d *Decimal) *Decimal { return d.Abs() }
func negF(d *Decimal) *Decimal { return d.Neg() }
type unaryop struct {
sym string
fun func(d *Decimal) *Decimal
}
var abs = &unaryop{"abs", absF}
var neg = &unaryop{"neg", negF}
func testUnaryOp(t *testing.T, a, e string, op *unaryop) {
t.Run(op.sym+"("+a+")="+e, func(t *testing.T) {
aa, _ := ParseDecimal(a)
ee, _ := ParseDecimal(e)
actual := op.fun(aa)
if !actual.Equal(ee) {
t.Errorf("expected %v, got %v", ee, actual)
}
})
}
func TestAbs(t *testing.T) {
test := func(a, e string) {
testUnaryOp(t, a, e, abs)
}
test("0", "0")
test("1d100", "1d100")
test("-1d100", "1d100")
test("1.2d-3", "1.2d-3")
test("-1.2d-3", "1.2d-3")
}
func TestNeg(t *testing.T) {
test := func(a, e string) {
testUnaryOp(t, a, e, neg)
}
test("0", "0")
test("1d100", "-1d100")
test("-1d100", "1d100")
test("1.2d-3", "-1.2d-3")
test("-1.2d-3", "1.2d-3")
}
func TestTrunc(t *testing.T) {
test := func(a string, eval int64) {
t.Run(fmt.Sprintf("trunc(%v)=%v", a, eval), func(t *testing.T) {
aa := MustParseDecimal(a)
val, err := aa.Trunc()
if err != nil {
t.Fatal(err)
}
if val != eval {
t.Errorf("expected %v, got %v", eval, val)
}
})
}
test("0.", 0)
test("0.01", 0)
test("1.", 1)
test("-1.", -1)
test("1.01", 1)
test("-1.01", -1)
test("101", 101)
test("1d3", 1000)
}
func addF(a, b *Decimal) *Decimal { return a.Add(b) }
func subF(a, b *Decimal) *Decimal { return a.Sub(b) }
func mulF(a, b *Decimal) *Decimal { return a.Mul(b) }
type binop struct {
sym string
fun func(a, b *Decimal) *Decimal
}
func TestShiftL(t *testing.T) {
test := func(a string, b int, e string) {
aa, _ := ParseDecimal(a)
ee, _ := ParseDecimal(e)
actual := aa.ShiftL(b)
if !actual.Equal(ee) {
t.Errorf("expected %v, got %v", ee, actual)
}
}
test("0", 10, "0")
test("1", 0, "1")
test("123", 1, "1230")
test("123", 100, "123d100")
test("1.23d-100", 102, "123")
}
func TestShiftR(t *testing.T) {
test := func(a string, b int, e string) {
aa, _ := ParseDecimal(a)
ee, _ := ParseDecimal(e)
actual := aa.ShiftR(b)
if !actual.Equal(ee) {
t.Errorf("expected %v, got %v", ee, actual)
}
}
test("0", 10, "0")
test("1", 0, "1")
test("123", 1, "12.3")
test("123", 100, "1.23d-98")
test("1.23d100", 98, "123")
}
var add = &binop{"+", addF}
var sub = &binop{"-", subF}
var mul = &binop{"*", mulF}
func testBinaryOp(t *testing.T, a, b, e string, op *binop) {
t.Run(a+op.sym+b+"="+e, func(t *testing.T) {
aa, _ := ParseDecimal(a)
bb, _ := ParseDecimal(b)
ee, _ := ParseDecimal(e)
actual := op.fun(aa, bb)
if !actual.Equal(ee) {
t.Errorf("expected %v, got %v", ee, actual)
}
})
}
func TestAdd(t *testing.T) {
test := func(a, b, e string) {
testBinaryOp(t, a, b, e, add)
}
test("1", "0", "1")
test("1", "1", "2")
test("1", "0.1", "1.1")
test("0.3", "0.06", "0.36")
test("1", "100", "101")
test("1d100", "1d98", "101d98")
test("1d-100", "1d-98", "1.01d-98")
}
func TestSub(t *testing.T) {
test := func(a, b, e string) {
testBinaryOp(t, a, b, e, sub)
}
test("1", "0", "1")
test("1", "1", "0")
test("1", "0.1", "0.9")
test("0.3", "0.06", "0.24")
test("1", "100", "-99")
test("1d100", "1d98", "99d98")
test("1d-100", "1d-98", "-99d-100")
}
func TestMul(t *testing.T) {
test := func(a, b, e string) {
testBinaryOp(t, a, b, e, mul)
}
test("1", "0", "0")
test("1", "1", "1")
test("2", "-1", "-2")
test("7", "6", "42")
test("10", "0.3", "3")
test("3d100", "2d50", "6d150")
test("3d-100", "2d-50", "6d-150")
test("2d100", "4d-98", "8d2")
}
func TestTruncate(t *testing.T) {
test := func(a string, p int, expected string) {
t.Run(fmt.Sprintf("trunc(%v,%v)", a, p), func(t *testing.T) {
aa := MustParseDecimal(a)
actual := aa.Truncate(p).String()
if actual != expected {
t.Errorf("expected %v, got %v", expected, actual)
}
})
}
test("1", 1, "1.")
test("1", 10, "1.")
test("10", 1, "1d1")
test("1999", 1, "1d3")
test("1.2345", 3, "1.23")
test("100d100", 2, "10d101")
test("1.2345d-100", 2, "1.2d-100")
}
func TestCmp(t *testing.T) {
test := func(a, b string, expected int) {
t.Run("("+a+","+b+")", func(t *testing.T) {
ad, _ := ParseDecimal(a)
bd, _ := ParseDecimal(b)
actual := ad.Cmp(bd)
if actual != expected {
t.Errorf("expected %v, got %v", expected, actual)
}
})
}
test("0", "0", 0)
test("0", "1", -1)
test("0", "-1", 1)
test("1d2", "100", 0)
test("100", "1d2", 0)
test("1d2", "10", 1)
test("10", "1d2", -1)
test("0.01", "1d-2", 0)
test("1d-2", "0.01", 0)
test("0.01", "1d-3", 1)
test("1d-3", "0.01", -1)
}
func TestUpscale(t *testing.T) {
d, _ := ParseDecimal("1d1")
actual := d.upscale(4).String()
if actual != "10.0000" {
t.Errorf("expected 10.0000, got %v", actual)
}
}