1 package decimal
2
3 import (
4 "database/sql/driver"
5 "encoding/json"
6 "encoding/xml"
7 "fmt"
8 "math"
9 "math/big"
10 "math/rand"
11 "reflect"
12 "regexp"
13 "strconv"
14 "strings"
15 "testing"
16 "testing/quick"
17 "time"
18 )
19
20 type testEnt struct {
21 float float64
22 short string
23 exact string
24 inexact string
25 }
26
27 var testTable = []*testEnt{
28 {3.141592653589793, "3.141592653589793", "", "3.14159265358979300000000000000000000000000000000000004"},
29 {3, "3", "", "3.0000000000000000000000002"},
30 {1234567890123456, "1234567890123456", "", "1234567890123456.00000000000000002"},
31 {1234567890123456000, "1234567890123456000", "", "1234567890123456000.0000000000000008"},
32 {1234.567890123456, "1234.567890123456", "", "1234.5678901234560000000000000009"},
33 {.1234567890123456, "0.1234567890123456", "", "0.12345678901234560000000000006"},
34 {0, "0", "", "0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001"},
35 {.1111111111111110, "0.111111111111111", "", "0.111111111111111000000000000000009"},
36 {.1111111111111111, "0.1111111111111111", "", "0.111111111111111100000000000000000000023423545644534234"},
37 {.1111111111111119, "0.1111111111111119", "", "0.111111111111111900000000000000000000000000000000000134123984192834"},
38 {.000000000000000001, "0.000000000000000001", "", "0.00000000000000000100000000000000000000000000000000012341234"},
39 {.000000000000000002, "0.000000000000000002", "", "0.0000000000000000020000000000000000000012341234123"},
40 {.000000000000000003, "0.000000000000000003", "", "0.00000000000000000299999999999999999999999900000000000123412341234"},
41 {.000000000000000005, "0.000000000000000005", "", "0.00000000000000000500000000000000000023412341234"},
42 {.000000000000000008, "0.000000000000000008", "", "0.0000000000000000080000000000000000001241234432"},
43 {.1000000000000001, "0.1000000000000001", "", "0.10000000000000010000000000000012341234"},
44 {.1000000000000002, "0.1000000000000002", "", "0.10000000000000020000000000001234123412"},
45 {.1000000000000003, "0.1000000000000003", "", "0.1000000000000003000000000000001234123412"},
46 {.1000000000000005, "0.1000000000000005", "", "0.1000000000000005000000000000000006441234"},
47 {.1000000000000008, "0.1000000000000008", "", "0.100000000000000800000000000000000009999999999999999999999999999"},
48 {1e25, "10000000000000000000000000", "", ""},
49 {1.5e14, "150000000000000", "", ""},
50 {1.5e15, "1500000000000000", "", ""},
51 {1.5e16, "15000000000000000", "", ""},
52 {1.0001e25, "10001000000000000000000000", "", ""},
53 {1.0001000000000000033e25, "10001000000000000000000000", "", ""},
54 {2e25, "20000000000000000000000000", "", ""},
55 {4e25, "40000000000000000000000000", "", ""},
56 {8e25, "80000000000000000000000000", "", ""},
57 {1e250, "10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", "", ""},
58 {2e250, "20000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", "", ""},
59 {math.MaxInt64, strconv.FormatFloat(float64(math.MaxInt64), 'f', -1, 64), "", strconv.FormatInt(math.MaxInt64, 10)},
60 {1.29067116156722e-309, "0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000129067116156722", "", "0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001290671161567218558822290567835270536800098852722416870074139002112543896676308448335063375297788379444685193974290737962187240854947838776604607190387984577130572928111657710645015086812756013489109884753559084166516937690932698276436869274093950997935137476803610007959500457935217950764794724766740819156974617155861568214427828145972181876775307023388139991104942469299524961281641158436752347582767153796914843896176260096039358494077706152272661453132497761307744086665088096215425146090058519888494342944692629602847826300550628670375451325582843627504604013541465361435761965354140678551369499812124085312128659002910905639984075064968459581691226705666561364681985266583563078466180095375402399087817404368974165082030458595596655868575908243656158447265625000000000000000000000000000000000000004440000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"},
61
62 {498484681984085570, "498484681984085570", "", ""},
63 {5.8339553793802237e+23, "583395537938022370000000", "", ""},
64 }
65
66 var testTableScientificNotation = map[string]string{
67 "1e9": "1000000000",
68 "2.41E-3": "0.00241",
69 "24.2E-4": "0.00242",
70 "243E-5": "0.00243",
71 "1e-5": "0.00001",
72 "245E3": "245000",
73 "1.2345E-1": "0.12345",
74 "0e5": "0",
75 "0e-5": "0",
76 "0.e0": "0",
77 ".0e0": "0",
78 "123.456e0": "123.456",
79 "123.456e2": "12345.6",
80 "123.456e10": "1234560000000",
81 }
82
83 func init() {
84 for _, s := range testTable {
85 s.exact = strconv.FormatFloat(s.float, 'f', 1500, 64)
86 if strings.ContainsRune(s.exact, '.') {
87 s.exact = strings.TrimRight(s.exact, "0")
88 s.exact = strings.TrimRight(s.exact, ".")
89 }
90 }
91
92
93 withNeg := testTable[:]
94 for _, s := range testTable {
95 if s.float > 0 && s.short != "0" && s.exact != "0" {
96 withNeg = append(withNeg, &testEnt{-s.float, "-" + s.short, "-" + s.exact, "-" + s.inexact})
97 }
98 }
99 testTable = withNeg
100
101 for e, s := range testTableScientificNotation {
102 if string(e[0]) != "-" && s != "0" {
103 testTableScientificNotation["-"+e] = "-" + s
104 }
105 }
106 }
107
108 func TestNewFromFloat(t *testing.T) {
109 for _, x := range testTable {
110 s := x.short
111 d := NewFromFloat(x.float)
112 if d.String() != s {
113 t.Errorf("expected %s, got %s (float: %v) (%s, %d)",
114 s, d.String(), x.float,
115 d.value.String(), d.exp)
116 }
117 }
118
119 shouldPanicOn := []float64{
120 math.NaN(),
121 math.Inf(1),
122 math.Inf(-1),
123 }
124
125 for _, n := range shouldPanicOn {
126 var d Decimal
127 if !didPanic(func() { d = NewFromFloat(n) }) {
128 t.Fatalf("Expected panic when creating a Decimal from %v, got %v instead", n, d.String())
129 }
130 }
131 }
132
133 func TestNewFromFloatRandom(t *testing.T) {
134 n := 0
135 rng := rand.New(rand.NewSource(0xdead1337))
136 for {
137 n++
138 if n == 10 {
139 break
140 }
141 in := (rng.Float64() - 0.5) * math.MaxFloat64 * 2
142 want, err := NewFromString(strconv.FormatFloat(in, 'f', -1, 64))
143 if err != nil {
144 t.Error(err)
145 continue
146 }
147 got := NewFromFloat(in)
148 if !want.Equal(got) {
149 t.Errorf("in: %v, expected %s (%s, %d), got %s (%s, %d) ",
150 in, want.String(), want.value.String(), want.exp,
151 got.String(), got.value.String(), got.exp)
152 }
153 }
154 }
155
156 func TestNewFromFloatQuick(t *testing.T) {
157 err := quick.Check(func(f float64) bool {
158 want, werr := NewFromString(strconv.FormatFloat(f, 'f', -1, 64))
159 if werr != nil {
160 return true
161 }
162 got := NewFromFloat(f)
163 return got.Equal(want)
164 }, &quick.Config{})
165 if err != nil {
166 t.Error(err)
167 }
168 }
169
170 func TestNewFromFloat32Random(t *testing.T) {
171 n := 0
172 rng := rand.New(rand.NewSource(0xdead1337))
173 for {
174 n++
175 if n == 10 {
176 break
177 }
178 in := float32((rng.Float64() - 0.5) * math.MaxFloat32 * 2)
179 want, err := NewFromString(strconv.FormatFloat(float64(in), 'f', -1, 32))
180 if err != nil {
181 t.Error(err)
182 continue
183 }
184 got := NewFromFloat32(in)
185 if !want.Equal(got) {
186 t.Errorf("in: %v, expected %s (%s, %d), got %s (%s, %d) ",
187 in, want.String(), want.value.String(), want.exp,
188 got.String(), got.value.String(), got.exp)
189 }
190 }
191 }
192
193 func TestNewFromFloat32Quick(t *testing.T) {
194 err := quick.Check(func(f float32) bool {
195 want, werr := NewFromString(strconv.FormatFloat(float64(f), 'f', -1, 32))
196 if werr != nil {
197 return true
198 }
199 got := NewFromFloat32(f)
200 return got.Equal(want)
201 }, &quick.Config{})
202 if err != nil {
203 t.Error(err)
204 }
205 }
206
207 func TestNewFromString(t *testing.T) {
208 for _, x := range testTable {
209 s := x.short
210 d, err := NewFromString(s)
211 if err != nil {
212 t.Errorf("error while parsing %s", s)
213 } else if d.String() != s {
214 t.Errorf("expected %s, got %s (%s, %d)",
215 s, d.String(),
216 d.value.String(), d.exp)
217 }
218 }
219
220 for _, x := range testTable {
221 s := x.exact
222 d, err := NewFromString(s)
223 if err != nil {
224 t.Errorf("error while parsing %s", s)
225 } else if d.String() != s {
226 t.Errorf("expected %s, got %s (%s, %d)",
227 s, d.String(),
228 d.value.String(), d.exp)
229 }
230 }
231
232 for e, s := range testTableScientificNotation {
233 d, err := NewFromString(e)
234 if err != nil {
235 t.Errorf("error while parsing %s", e)
236 } else if d.String() != s {
237 t.Errorf("expected %s, got %s (%s, %d)",
238 s, d.String(),
239 d.value.String(), d.exp)
240 }
241 }
242 }
243
244 func TestNewFromFormattedString(t *testing.T) {
245 for _, testCase := range []struct {
246 Formatted string
247 Expected string
248 ReplRegex *regexp.Regexp
249 }{
250 {"$10.99", "10.99", regexp.MustCompile("[$]")},
251 {"$ 12.1", "12.1", regexp.MustCompile("[$\\s]")},
252 {"$61,690.99", "61690.99", regexp.MustCompile("[$,]")},
253 {"1_000_000.00", "1000000.00", regexp.MustCompile("[_]")},
254 {"41,410.00", "41410.00", regexp.MustCompile("[,]")},
255 {"5200 USD", "5200", regexp.MustCompile("[USD\\s]")},
256 } {
257 dFormatted, err := NewFromFormattedString(testCase.Formatted, testCase.ReplRegex)
258 if err != nil {
259 t.Fatal(err)
260 }
261
262 dExact, err := NewFromString(testCase.Expected)
263 if err != nil {
264 t.Fatal(err)
265 }
266
267 if !dFormatted.Equal(dExact) {
268 t.Errorf("expect %s, got %s", dExact, dFormatted)
269 }
270 }
271 }
272
273 func TestFloat64(t *testing.T) {
274 for _, x := range testTable {
275 if x.inexact == "" || x.inexact == "-" {
276 continue
277 }
278 s := x.exact
279 d, err := NewFromString(s)
280 if err != nil {
281 t.Errorf("error while parsing %s", s)
282 } else if f, exact := d.Float64(); !exact || f != x.float {
283 t.Errorf("cannot represent exactly %s", s)
284 }
285 s = x.inexact
286 d, err = NewFromString(s)
287 if err != nil {
288 t.Errorf("error while parsing %s", s)
289 } else if f, exact := d.Float64(); exact || f != x.float {
290 t.Errorf("%s should be represented inexactly", s)
291 }
292 }
293 }
294
295 func TestNewFromStringErrs(t *testing.T) {
296 tests := []string{
297 "",
298 "qwert",
299 "-",
300 ".",
301 "-.",
302 ".-",
303 "234-.56",
304 "234-56",
305 "2-",
306 "..",
307 "2..",
308 "..2",
309 ".5.2",
310 "8..2",
311 "8.1.",
312 "1e",
313 "1-e",
314 "1e9e",
315 "1ee9",
316 "1ee",
317 "1eE",
318 "1e-",
319 "1e-.",
320 "1e1.2",
321 "123.456e1.3",
322 "1e-1.2",
323 "123.456e-1.3",
324 "123.456Easdf",
325 "123.456e" + strconv.FormatInt(math.MinInt64, 10),
326 "123.456e" + strconv.FormatInt(math.MinInt32, 10),
327 "512.99 USD",
328 "$99.99",
329 "51,850.00",
330 "20_000_000.00",
331 "$20_000_000.00",
332 }
333
334 for _, s := range tests {
335 _, err := NewFromString(s)
336
337 if err == nil {
338 t.Errorf("error expected when parsing %s", s)
339 }
340 }
341 }
342
343 func TestNewFromStringDeepEquals(t *testing.T) {
344 type StrCmp struct {
345 str1 string
346 str2 string
347 expected bool
348 }
349 tests := []StrCmp{
350 {"1", "1", true},
351 {"1.0", "1.0", true},
352 {"10", "10.0", false},
353 {"1.1", "1.10", false},
354 {"1.001", "1.01", false},
355 }
356
357 for _, cmp := range tests {
358 d1, err1 := NewFromString(cmp.str1)
359 d2, err2 := NewFromString(cmp.str2)
360
361 if err1 != nil || err2 != nil {
362 t.Errorf("error parsing strings to decimals")
363 }
364
365 if reflect.DeepEqual(d1, d2) != cmp.expected {
366 t.Errorf("comparison result is different from expected results for %s and %s",
367 cmp.str1, cmp.str2)
368 }
369 }
370 }
371
372 func TestRequireFromString(t *testing.T) {
373 s := "1.23"
374 defer func() {
375 err := recover()
376 if err != nil {
377 t.Errorf("error while parsing %s", s)
378 }
379 }()
380
381 d := RequireFromString(s)
382 if d.String() != s {
383 t.Errorf("expected %s, got %s (%s, %d)",
384 s, d.String(),
385 d.value.String(), d.exp)
386 }
387 }
388
389 func TestRequireFromStringErrs(t *testing.T) {
390 s := "qwert"
391 var d Decimal
392 var err interface{}
393
394 func(d Decimal) {
395 defer func() {
396 err = recover()
397 }()
398
399 RequireFromString(s)
400 }(d)
401
402 if err == nil {
403 t.Errorf("panic expected when parsing %s", s)
404 }
405 }
406
407 func TestNewFromFloatWithExponent(t *testing.T) {
408 type Inp struct {
409 float float64
410 exp int32
411 }
412
413 tests := map[Inp]string{
414 Inp{123.4, -3}: "123.4",
415 Inp{123.4, -1}: "123.4",
416 Inp{123.412345, 1}: "120",
417 Inp{123.412345, 0}: "123",
418 Inp{123.412345, -5}: "123.41235",
419 Inp{123.412345, -6}: "123.412345",
420 Inp{123.412345, -7}: "123.412345",
421 Inp{123.412345, -28}: "123.4123450000000019599610823207",
422 Inp{1230000000, 3}: "1230000000",
423 Inp{123.9999999999999999, -7}: "124",
424 Inp{123.8989898999999999, -7}: "123.8989899",
425 Inp{0.49999999999999994, 0}: "0",
426 Inp{0.5, 0}: "1",
427 Inp{0., -1000}: "0",
428 Inp{0.5000000000000001, 0}: "1",
429 Inp{1.390671161567e-309, 0}: "0",
430 Inp{4.503599627370497e+15, 0}: "4503599627370497",
431 Inp{4.503599627370497e+60, 0}: "4503599627370497110902645731364739935039854989106233267453952",
432 Inp{4.503599627370497e+60, 1}: "4503599627370497110902645731364739935039854989106233267453950",
433 Inp{4.503599627370497e+60, -1}: "4503599627370497110902645731364739935039854989106233267453952",
434 Inp{50, 2}: "100",
435 Inp{49, 2}: "0",
436 Inp{50, 3}: "0",
437
438 Inp{1.390671161567e-309, -2000}: "0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001390671161567000864431395448332752540137009987788957394095829635554502771758698872408926974382819387852542087331897381878220271350970912568035007740861074263206736245957501456549756342151614772544950978154339064833880234531754156635411349342950306987480369774780312897442981323940546749863054846093718407237782253156822124910364044261653195961209878120072488178603782495270845071470243842997312255994555557251870400944414666445871039673491570643357351279578519863428540219295076767898526278029257129758694673164251056158277568765100904638511604478844087596428177947970563689475826736810456067108202083804368114484417399279328807983736233036662284338182105684628835292230438999173947056675615385756827890872955322265625",
439 Inp{1.390671161567e-309, -862}: "0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000013906711615670008644313954483327525401370099877889573940958296355545027717586988724089269743828193878525420873318973818782202713509709125680350077408610742632067362459575014565497563421516147725449509781543390648338802345317541566354113493429503069874803697747803128974429813239405467498630548460937184072377822531568221249103640442616531959612098781200724881786037824952708450714702438429973122559945555572518704009444146664458710396734915706433573512795785198634285402192950767678985262780292571297586946731642510561582775687651009046385116044788440876",
440 Inp{1.390671161567e-309, -863}: "0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000013906711615670008644313954483327525401370099877889573940958296355545027717586988724089269743828193878525420873318973818782202713509709125680350077408610742632067362459575014565497563421516147725449509781543390648338802345317541566354113493429503069874803697747803128974429813239405467498630548460937184072377822531568221249103640442616531959612098781200724881786037824952708450714702438429973122559945555572518704009444146664458710396734915706433573512795785198634285402192950767678985262780292571297586946731642510561582775687651009046385116044788440876",
441 }
442
443
444 for p, s := range tests {
445 if p.float > 0 {
446 if s != "0" {
447 tests[Inp{-p.float, p.exp}] = "-" + s
448 } else {
449 tests[Inp{-p.float, p.exp}] = "0"
450 }
451 }
452 }
453
454 for input, s := range tests {
455 d := NewFromFloatWithExponent(input.float, input.exp)
456 if d.String() != s {
457 t.Errorf("expected %s, got %s (%s, %d)",
458 s, d.String(),
459 d.value.String(), d.exp)
460 }
461 }
462
463 shouldPanicOn := []float64{
464 math.NaN(),
465 math.Inf(1),
466 math.Inf(-1),
467 }
468
469 for _, n := range shouldPanicOn {
470 var d Decimal
471 if !didPanic(func() { d = NewFromFloatWithExponent(n, 0) }) {
472 t.Fatalf("Expected panic when creating a Decimal from %v, got %v instead", n, d.String())
473 }
474 }
475 }
476
477 func TestNewFromInt(t *testing.T) {
478 tests := map[int64]string{
479 0: "0",
480 1: "1",
481 323412345: "323412345",
482 9223372036854775807: "9223372036854775807",
483 -9223372036854775808: "-9223372036854775808",
484 }
485
486
487 for p, s := range tests {
488 if p > 0 {
489 tests[-p] = "-" + s
490 }
491 }
492
493 for input, s := range tests {
494 d := NewFromInt(input)
495 if d.String() != s {
496 t.Errorf("expected %s, got %s (%s, %d)",
497 s, d.String(),
498 d.value.String(), d.exp)
499 }
500 }
501 }
502
503 func TestNewFromInt32(t *testing.T) {
504 tests := map[int32]string{
505 0: "0",
506 1: "1",
507 323412345: "323412345",
508 2147483647: "2147483647",
509 -2147483648: "-2147483648",
510 }
511
512
513 for p, s := range tests {
514 if p > 0 {
515 tests[-p] = "-" + s
516 }
517 }
518
519 for input, s := range tests {
520 d := NewFromInt32(input)
521 if d.String() != s {
522 t.Errorf("expected %s, got %s (%s, %d)",
523 s, d.String(),
524 d.value.String(), d.exp)
525 }
526 }
527 }
528
529 func TestNewFromUint64(t *testing.T) {
530 tests := map[uint64]string{
531 0: "0",
532 1: "1",
533 323412345: "323412345",
534 9223372036854775807: "9223372036854775807",
535 18446744073709551615: "18446744073709551615",
536 }
537
538 for input, s := range tests {
539 d := NewFromUint64(input)
540 if d.String() != s {
541 t.Errorf("expected %s, got %s (%s, %d)",
542 s, d.String(),
543 d.value.String(), d.exp)
544 }
545 }
546 }
547
548 func TestNewFromBigIntWithExponent(t *testing.T) {
549 type Inp struct {
550 val *big.Int
551 exp int32
552 }
553 tests := map[Inp]string{
554 Inp{big.NewInt(123412345), -3}: "123412.345",
555 Inp{big.NewInt(2234), -1}: "223.4",
556 Inp{big.NewInt(323412345), 1}: "3234123450",
557 Inp{big.NewInt(423412345), 0}: "423412345",
558 Inp{big.NewInt(52341235), -5}: "523.41235",
559 Inp{big.NewInt(623412345), -6}: "623.412345",
560 Inp{big.NewInt(723412345), -7}: "72.3412345",
561 }
562
563
564 for p, s := range tests {
565 if p.val.Cmp(Zero.value) > 0 {
566 tests[Inp{p.val.Neg(p.val), p.exp}] = "-" + s
567 }
568 }
569
570 for input, s := range tests {
571 d := NewFromBigInt(input.val, input.exp)
572 if d.String() != s {
573 t.Errorf("expected %s, got %s (%s, %d)",
574 s, d.String(),
575 d.value.String(), d.exp)
576 }
577 }
578 }
579
580 func TestNewFromBigRat(t *testing.T) {
581 mustParseRat := func(val string) *big.Rat {
582 num, _ := new(big.Rat).SetString(val)
583 return num
584 }
585
586 type Inp struct {
587 val *big.Rat
588 prec int32
589 }
590
591 tests := map[Inp]string{
592 Inp{big.NewRat(0, 1), 16}: "0",
593 Inp{big.NewRat(4, 5), 16}: "0.8",
594 Inp{big.NewRat(10, 2), 16}: "5",
595 Inp{big.NewRat(1023427554493, 43432632), 16}: "23563.5628642767953828",
596 Inp{big.NewRat(1, 434324545566634), 16}: "0.0000000000000023",
597 Inp{big.NewRat(1, 3), 16}: "0.3333333333333333",
598 Inp{big.NewRat(2, 3), 2}: "0.67",
599 Inp{big.NewRat(2, 3), 16}: "0.6666666666666667",
600 Inp{big.NewRat(10000, 3), 16}: "3333.3333333333333333",
601 Inp{mustParseRat("30702832066636633479"), 16}: "30702832066636633479",
602 Inp{mustParseRat("487028320159896636679.1827512895753"), 16}: "487028320159896636679.1827512895753",
603 Inp{mustParseRat("127028320612589896636633479.173582751289575278357832"), -2}: "127028320612589896636633500",
604 Inp{mustParseRat("127028320612589896636633479.173582751289575278357832"), 16}: "127028320612589896636633479.1735827512895753",
605 Inp{mustParseRat("127028320612589896636633479.173582751289575278357832"), 32}: "127028320612589896636633479.173582751289575278357832",
606 }
607
608
609 for p, s := range tests {
610 if p.val.Cmp(new(big.Rat)) > 0 {
611 tests[Inp{p.val.Neg(p.val), p.prec}] = "-" + s
612 }
613 }
614
615 for input, s := range tests {
616 d := NewFromBigRat(input.val, input.prec)
617 if d.String() != s {
618 t.Errorf("expected %s, got %s (%s, %d)",
619 s, d.String(),
620 d.value.String(), d.exp)
621 }
622 }
623 }
624
625 func TestCopy(t *testing.T) {
626 origin := New(1, 0)
627 cpy := origin.Copy()
628
629 if origin.value == cpy.value {
630 t.Error("expecting copy and origin to have different value pointers")
631 }
632
633 if cpy.Cmp(origin) != 0 {
634 t.Error("expecting copy and origin to be equals, but they are not")
635 }
636
637
638 cpy = cpy.Add(New(1, 0))
639
640 if cpy.Cmp(origin) == 0 {
641 t.Error("expecting copy and origin to have different values, but they are equal")
642 }
643 }
644
645 func TestJSON(t *testing.T) {
646 for _, x := range testTable {
647 s := x.short
648 var doc struct {
649 Amount Decimal `json:"amount"`
650 }
651 docStr := `{"amount":"` + s + `"}`
652 docStrNumber := `{"amount":` + s + `}`
653 err := json.Unmarshal([]byte(docStr), &doc)
654 if err != nil {
655 t.Errorf("error unmarshaling %s: %v", docStr, err)
656 } else if doc.Amount.String() != s {
657 t.Errorf("expected %s, got %s (%s, %d)",
658 s, doc.Amount.String(),
659 doc.Amount.value.String(), doc.Amount.exp)
660 }
661
662 out, err := json.Marshal(&doc)
663 if err != nil {
664 t.Errorf("error marshaling %+v: %v", doc, err)
665 } else if string(out) != docStr {
666 t.Errorf("expected %s, got %s", docStr, string(out))
667 }
668
669
670 MarshalJSONWithoutQuotes = true
671 out, err = json.Marshal(&doc)
672 if err != nil {
673 t.Errorf("error marshaling %+v: %v", doc, err)
674 } else if string(out) != docStrNumber {
675 t.Errorf("expected %s, got %s", docStrNumber, string(out))
676 }
677 MarshalJSONWithoutQuotes = false
678 }
679 }
680
681 func TestUnmarshalJSONNull(t *testing.T) {
682 var doc struct {
683 Amount Decimal `json:"amount"`
684 }
685 docStr := `{"amount": null}`
686 err := json.Unmarshal([]byte(docStr), &doc)
687 if err != nil {
688 t.Errorf("error unmarshaling %s: %v", docStr, err)
689 } else if !doc.Amount.Equal(Zero) {
690 t.Errorf("expected Zero, got %s (%s, %d)",
691 doc.Amount.String(),
692 doc.Amount.value.String(), doc.Amount.exp)
693 }
694 }
695
696 func TestBadJSON(t *testing.T) {
697 for _, testCase := range []string{
698 "]o_o[",
699 "{",
700 `{"amount":""`,
701 `{"amount":""}`,
702 `{"amount":"nope"}`,
703 `0.333`,
704 } {
705 var doc struct {
706 Amount Decimal `json:"amount"`
707 }
708 err := json.Unmarshal([]byte(testCase), &doc)
709 if err == nil {
710 t.Errorf("expected error, got %+v", doc)
711 }
712 }
713 }
714
715 func TestNullDecimalJSON(t *testing.T) {
716 for _, x := range testTable {
717 s := x.short
718 var doc struct {
719 Amount NullDecimal `json:"amount"`
720 }
721 docStr := `{"amount":"` + s + `"}`
722 docStrNumber := `{"amount":` + s + `}`
723 err := json.Unmarshal([]byte(docStr), &doc)
724 if err != nil {
725 t.Errorf("error unmarshaling %s: %v", docStr, err)
726 } else {
727 if !doc.Amount.Valid {
728 t.Errorf("expected %s to be valid (not NULL), got Valid = false", s)
729 }
730 if doc.Amount.Decimal.String() != s {
731 t.Errorf("expected %s, got %s (%s, %d)",
732 s, doc.Amount.Decimal.String(),
733 doc.Amount.Decimal.value.String(), doc.Amount.Decimal.exp)
734 }
735 }
736
737 out, err := json.Marshal(&doc)
738 if err != nil {
739 t.Errorf("error marshaling %+v: %v", doc, err)
740 } else if string(out) != docStr {
741 t.Errorf("expected %s, got %s", docStr, string(out))
742 }
743
744
745 MarshalJSONWithoutQuotes = true
746 out, err = json.Marshal(&doc)
747 if err != nil {
748 t.Errorf("error marshaling %+v: %v", doc, err)
749 } else if string(out) != docStrNumber {
750 t.Errorf("expected %s, got %s", docStrNumber, string(out))
751 }
752 MarshalJSONWithoutQuotes = false
753 }
754
755 var doc struct {
756 Amount NullDecimal `json:"amount"`
757 }
758 docStr := `{"amount": null}`
759 err := json.Unmarshal([]byte(docStr), &doc)
760 if err != nil {
761 t.Errorf("error unmarshaling %s: %v", docStr, err)
762 } else if doc.Amount.Valid {
763 t.Errorf("expected null value to have Valid = false, got Valid = true and Decimal = %s (%s, %d)",
764 doc.Amount.Decimal.String(),
765 doc.Amount.Decimal.value.String(), doc.Amount.Decimal.exp)
766 }
767
768 expected := `{"amount":null}`
769 out, err := json.Marshal(&doc)
770 if err != nil {
771 t.Errorf("error marshaling %+v: %v", doc, err)
772 } else if string(out) != expected {
773 t.Errorf("expected %s, got %s", expected, string(out))
774 }
775
776
777 MarshalJSONWithoutQuotes = true
778 expectedUnquoted := `{"amount":null}`
779 out, err = json.Marshal(&doc)
780 if err != nil {
781 t.Errorf("error marshaling %+v: %v", doc, err)
782 } else if string(out) != expectedUnquoted {
783 t.Errorf("expected %s, got %s", expectedUnquoted, string(out))
784 }
785 MarshalJSONWithoutQuotes = false
786 }
787
788 func TestNullDecimalBadJSON(t *testing.T) {
789 for _, testCase := range []string{
790 "]o_o[",
791 "{",
792 `{"amount":""`,
793 `{"amount":""}`,
794 `{"amount":"nope"}`,
795 `{"amount":nope}`,
796 `0.333`,
797 } {
798 var doc struct {
799 Amount NullDecimal `json:"amount"`
800 }
801 err := json.Unmarshal([]byte(testCase), &doc)
802 if err == nil {
803 t.Errorf("expected error, got %+v", doc)
804 }
805 }
806 }
807
808 func TestXML(t *testing.T) {
809 for _, x := range testTable {
810 s := x.short
811 var doc struct {
812 XMLName xml.Name `xml:"account"`
813 Amount Decimal `xml:"amount"`
814 }
815 docStr := `<account><amount>` + s + `</amount></account>`
816 err := xml.Unmarshal([]byte(docStr), &doc)
817 if err != nil {
818 t.Errorf("error unmarshaling %s: %v", docStr, err)
819 } else if doc.Amount.String() != s {
820 t.Errorf("expected %s, got %s (%s, %d)",
821 s, doc.Amount.String(),
822 doc.Amount.value.String(), doc.Amount.exp)
823 }
824
825 out, err := xml.Marshal(&doc)
826 if err != nil {
827 t.Errorf("error marshaling %+v: %v", doc, err)
828 } else if string(out) != docStr {
829 t.Errorf("expected %s, got %s", docStr, string(out))
830 }
831 }
832 }
833
834 func TestBadXML(t *testing.T) {
835 for _, testCase := range []string{
836 "o_o",
837 "<abc",
838 "<account><amount>7",
839 `<html><body></body></html>`,
840 `<account><amount></amount></account>`,
841 `<account><amount>nope</amount></account>`,
842 `0.333`,
843 } {
844 var doc struct {
845 XMLName xml.Name `xml:"account"`
846 Amount Decimal `xml:"amount"`
847 }
848 err := xml.Unmarshal([]byte(testCase), &doc)
849 if err == nil {
850 t.Errorf("expected error, got %+v", doc)
851 }
852 }
853 }
854
855 func TestNullDecimalXML(t *testing.T) {
856
857 for _, x := range testTable {
858 s := x.short
859 var doc struct {
860 XMLName xml.Name `xml:"account"`
861 Amount NullDecimal `xml:"amount"`
862 }
863 docStr := `<account><amount>` + s + `</amount></account>`
864 err := xml.Unmarshal([]byte(docStr), &doc)
865 if err != nil {
866 t.Errorf("error unmarshaling %s: %v", docStr, err)
867 } else if doc.Amount.Decimal.String() != s {
868 t.Errorf("expected %s, got %s (%s, %d)",
869 s, doc.Amount.Decimal.String(),
870 doc.Amount.Decimal.value.String(), doc.Amount.Decimal.exp)
871 }
872
873 out, err := xml.Marshal(&doc)
874 if err != nil {
875 t.Errorf("error marshaling %+v: %v", doc, err)
876 } else if string(out) != docStr {
877 t.Errorf("expected %s, got %s", docStr, string(out))
878 }
879 }
880
881 var doc struct {
882 XMLName xml.Name `xml:"account"`
883 Amount NullDecimal `xml:"amount"`
884 }
885
886
887 docStr := `<account><amount></amount></account>`
888 err := xml.Unmarshal([]byte(docStr), &doc)
889 if err != nil {
890 t.Errorf("error unmarshaling: %s: %v", docStr, err)
891 } else if doc.Amount.Valid {
892 t.Errorf("expected null value to have Valid = false, got Valid = true and Decimal = %s (%s, %d)",
893 doc.Amount.Decimal.String(),
894 doc.Amount.Decimal.value.String(), doc.Amount.Decimal.exp)
895 }
896
897 expected := `<account><amount></amount></account>`
898 out, err := xml.Marshal(&doc)
899 if err != nil {
900 t.Errorf("error marshaling %+v: %v", doc, err)
901 } else if string(out) != expected {
902 t.Errorf("expected %s, got %s", expected, string(out))
903 }
904
905
906 docStr = `<account></account>`
907 err = xml.Unmarshal([]byte(docStr), &doc)
908 if err != nil {
909 t.Errorf("error unmarshaling: %s: %v", docStr, err)
910 } else if doc.Amount.Valid {
911 t.Errorf("expected null value to have Valid = false, got Valid = true and Decimal = %s (%s, %d)",
912 doc.Amount.Decimal.String(),
913 doc.Amount.Decimal.value.String(), doc.Amount.Decimal.exp)
914 }
915
916 expected = `<account><amount></amount></account>`
917 out, err = xml.Marshal(&doc)
918 if err != nil {
919 t.Errorf("error marshaling %+v: %v", doc, err)
920 } else if string(out) != expected {
921 t.Errorf("expected %s, got %s", expected, string(out))
922 }
923 }
924
925 func TestNullDecimalBadXML(t *testing.T) {
926 for _, testCase := range []string{
927 "o_o",
928 "<abc",
929 "<account><amount>7",
930 `<html><body></body></html>`,
931 `<account><amount>nope</amount></account>`,
932 `0.333`,
933 } {
934 var doc struct {
935 XMLName xml.Name `xml:"account"`
936 Amount NullDecimal `xml:"amount"`
937 }
938 err := xml.Unmarshal([]byte(testCase), &doc)
939 if err == nil {
940 t.Errorf("expected error, got %+v", doc)
941 }
942 }
943 }
944
945 func TestDecimal_rescale(t *testing.T) {
946 type Inp struct {
947 int int64
948 exp int32
949 rescale int32
950 }
951 tests := map[Inp]string{
952 Inp{1234, -3, -5}: "1.234",
953 Inp{1234, -3, 0}: "1",
954 Inp{1234, 3, 0}: "1234000",
955 Inp{1234, -4, -4}: "0.1234",
956 }
957
958
959 for p, s := range tests {
960 if p.int > 0 {
961 tests[Inp{-p.int, p.exp, p.rescale}] = "-" + s
962 }
963 }
964
965 for input, s := range tests {
966 d := New(input.int, input.exp).rescale(input.rescale)
967
968 if d.String() != s {
969 t.Errorf("expected %s, got %s (%s, %d)",
970 s, d.String(),
971 d.value.String(), d.exp)
972 }
973
974
975 s2 := New(input.int, input.exp).StringScaled(input.rescale)
976 if s2 != s {
977 t.Errorf("expected %s, got %s", s, s2)
978 }
979 }
980 }
981
982 func TestDecimal_Floor(t *testing.T) {
983 assertFloor := func(input, expected Decimal) {
984 got := input.Floor()
985 if !got.Equal(expected) {
986 t.Errorf("Floor(%s): got %s, expected %s", input, got, expected)
987 }
988 }
989 type testDataString struct {
990 input string
991 expected string
992 }
993 testsWithStrings := []testDataString{
994 {"1.999", "1"},
995 {"1", "1"},
996 {"1.01", "1"},
997 {"0", "0"},
998 {"0.9", "0"},
999 {"0.1", "0"},
1000 {"-0.9", "-1"},
1001 {"-0.1", "-1"},
1002 {"-1.00", "-1"},
1003 {"-1.01", "-2"},
1004 {"-1.999", "-2"},
1005 }
1006 for _, test := range testsWithStrings {
1007 expected, _ := NewFromString(test.expected)
1008 input, _ := NewFromString(test.input)
1009 assertFloor(input, expected)
1010 }
1011
1012 type testDataDecimal struct {
1013 input Decimal
1014 expected string
1015 }
1016 testsWithDecimals := []testDataDecimal{
1017 {New(100, -1), "10"},
1018 {New(10, 0), "10"},
1019 {New(1, 1), "10"},
1020 {New(1999, -3), "1"},
1021 {New(101, -2), "1"},
1022 {New(1, 0), "1"},
1023 {New(0, 0), "0"},
1024 {New(9, -1), "0"},
1025 {New(1, -1), "0"},
1026 {New(-1, -1), "-1"},
1027 {New(-9, -1), "-1"},
1028 {New(-1, 0), "-1"},
1029 {New(-101, -2), "-2"},
1030 {New(-1999, -3), "-2"},
1031 }
1032 for _, test := range testsWithDecimals {
1033 expected, _ := NewFromString(test.expected)
1034 assertFloor(test.input, expected)
1035 }
1036 }
1037
1038 func TestDecimal_Ceil(t *testing.T) {
1039 assertCeil := func(input, expected Decimal) {
1040 got := input.Ceil()
1041 if !got.Equal(expected) {
1042 t.Errorf("Ceil(%s): got %s, expected %s", input, got, expected)
1043 }
1044 }
1045 type testDataString struct {
1046 input string
1047 expected string
1048 }
1049 testsWithStrings := []testDataString{
1050 {"1.999", "2"},
1051 {"1", "1"},
1052 {"1.01", "2"},
1053 {"0", "0"},
1054 {"0.9", "1"},
1055 {"0.1", "1"},
1056 {"-0.9", "0"},
1057 {"-0.1", "0"},
1058 {"-1.00", "-1"},
1059 {"-1.01", "-1"},
1060 {"-1.999", "-1"},
1061 }
1062 for _, test := range testsWithStrings {
1063 expected, _ := NewFromString(test.expected)
1064 input, _ := NewFromString(test.input)
1065 assertCeil(input, expected)
1066 }
1067
1068 type testDataDecimal struct {
1069 input Decimal
1070 expected string
1071 }
1072 testsWithDecimals := []testDataDecimal{
1073 {New(100, -1), "10"},
1074 {New(10, 0), "10"},
1075 {New(1, 1), "10"},
1076 {New(1999, -3), "2"},
1077 {New(101, -2), "2"},
1078 {New(1, 0), "1"},
1079 {New(0, 0), "0"},
1080 {New(9, -1), "1"},
1081 {New(1, -1), "1"},
1082 {New(-1, -1), "0"},
1083 {New(-9, -1), "0"},
1084 {New(-1, 0), "-1"},
1085 {New(-101, -2), "-1"},
1086 {New(-1999, -3), "-1"},
1087 }
1088 for _, test := range testsWithDecimals {
1089 expected, _ := NewFromString(test.expected)
1090 assertCeil(test.input, expected)
1091 }
1092 }
1093
1094 func TestDecimal_RoundAndStringFixed(t *testing.T) {
1095 type testData struct {
1096 input string
1097 places int32
1098 expected string
1099 expectedFixed string
1100 }
1101 tests := []testData{
1102 {"1.454", 0, "1", ""},
1103 {"1.454", 1, "1.5", ""},
1104 {"1.454", 2, "1.45", ""},
1105 {"1.454", 3, "1.454", ""},
1106 {"1.454", 4, "1.454", "1.4540"},
1107 {"1.454", 5, "1.454", "1.45400"},
1108 {"1.554", 0, "2", ""},
1109 {"1.554", 1, "1.6", ""},
1110 {"1.554", 2, "1.55", ""},
1111 {"0.554", 0, "1", ""},
1112 {"0.454", 0, "0", ""},
1113 {"0.454", 5, "0.454", "0.45400"},
1114 {"0", 0, "0", ""},
1115 {"0", 1, "0", "0.0"},
1116 {"0", 2, "0", "0.00"},
1117 {"0", -1, "0", ""},
1118 {"5", 2, "5", "5.00"},
1119 {"5", 1, "5", "5.0"},
1120 {"5", 0, "5", ""},
1121 {"500", 2, "500", "500.00"},
1122 {"545", -1, "550", ""},
1123 {"545", -2, "500", ""},
1124 {"545", -3, "1000", ""},
1125 {"545", -4, "0", ""},
1126 {"499", -3, "0", ""},
1127 {"499", -4, "0", ""},
1128 }
1129
1130
1131 for _, test := range tests {
1132 expected := test.expected
1133 if expected != "0" {
1134 expected = "-" + expected
1135 }
1136 expectedStr := test.expectedFixed
1137 if strings.ContainsAny(expectedStr, "123456789") && expectedStr != "" {
1138 expectedStr = "-" + expectedStr
1139 }
1140 tests = append(tests,
1141 testData{"-" + test.input, test.places, expected, expectedStr})
1142 }
1143
1144 for _, test := range tests {
1145 d, err := NewFromString(test.input)
1146 if err != nil {
1147 t.Fatal(err)
1148 }
1149
1150
1151 expected, err := NewFromString(test.expected)
1152 if err != nil {
1153 t.Fatal(err)
1154 }
1155 got := d.Round(test.places)
1156 if !got.Equal(expected) {
1157 t.Errorf("Rounding %s to %d places, got %s, expected %s",
1158 d, test.places, got, expected)
1159 }
1160
1161
1162 if test.expectedFixed == "" {
1163 test.expectedFixed = test.expected
1164 }
1165 gotStr := d.StringFixed(test.places)
1166 if gotStr != test.expectedFixed {
1167 t.Errorf("(%s).StringFixed(%d): got %s, expected %s",
1168 d, test.places, gotStr, test.expectedFixed)
1169 }
1170 }
1171 }
1172
1173 func TestDecimal_RoundCeilAndStringFixed(t *testing.T) {
1174 type testData struct {
1175 input string
1176 places int32
1177 expected string
1178 expectedFixed string
1179 }
1180 tests := []testData{
1181 {"1.454", 0, "2", ""},
1182 {"1.454", 1, "1.5", ""},
1183 {"1.454", 2, "1.46", ""},
1184 {"1.454", 3, "1.454", ""},
1185 {"1.454", 4, "1.454", "1.4540"},
1186 {"1.454", 5, "1.454", "1.45400"},
1187 {"1.554", 0, "2", ""},
1188 {"1.554", 1, "1.6", ""},
1189 {"1.554", 2, "1.56", ""},
1190 {"0.554", 0, "1", ""},
1191 {"0.454", 0, "1", ""},
1192 {"0.454", 5, "0.454", "0.45400"},
1193 {"0", 0, "0", ""},
1194 {"0", 1, "0", "0.0"},
1195 {"0", 2, "0", "0.00"},
1196 {"0", -1, "0", ""},
1197 {"5", 2, "5", "5.00"},
1198 {"5", 1, "5", "5.0"},
1199 {"5", 0, "5", ""},
1200 {"500", 2, "500", "500.00"},
1201 {"500", -2, "500", ""},
1202 {"545", -1, "550", ""},
1203 {"545", -2, "600", ""},
1204 {"545", -3, "1000", ""},
1205 {"545", -4, "10000", ""},
1206 {"499", -3, "1000", ""},
1207 {"499", -4, "10000", ""},
1208 {"1.1001", 2, "1.11", ""},
1209 {"-1.1001", 2, "-1.10", ""},
1210 {"-1.454", 0, "-1", ""},
1211 {"-1.454", 1, "-1.4", ""},
1212 {"-1.454", 2, "-1.45", ""},
1213 {"-1.454", 3, "-1.454", ""},
1214 {"-1.454", 4, "-1.454", "-1.4540"},
1215 {"-1.454", 5, "-1.454", "-1.45400"},
1216 {"-1.554", 0, "-1", ""},
1217 {"-1.554", 1, "-1.5", ""},
1218 {"-1.554", 2, "-1.55", ""},
1219 {"-0.554", 0, "0", ""},
1220 {"-0.454", 0, "0", ""},
1221 {"-0.454", 5, "-0.454", "-0.45400"},
1222 {"-5", 2, "-5", "-5.00"},
1223 {"-5", 1, "-5", "-5.0"},
1224 {"-5", 0, "-5", ""},
1225 {"-500", 2, "-500", "-500.00"},
1226 {"-500", -2, "-500", ""},
1227 {"-545", -1, "-540", ""},
1228 {"-545", -2, "-500", ""},
1229 {"-545", -3, "0", ""},
1230 {"-545", -4, "0", ""},
1231 {"-499", -3, "0", ""},
1232 {"-499", -4, "0", ""},
1233 }
1234
1235 for _, test := range tests {
1236 d, err := NewFromString(test.input)
1237 if err != nil {
1238 t.Fatal(err)
1239 }
1240
1241
1242 expected, err := NewFromString(test.expected)
1243 if err != nil {
1244 t.Fatal(err)
1245 }
1246 got := d.RoundCeil(test.places)
1247 if !got.Equal(expected) {
1248 t.Errorf("Rounding ceil %s to %d places, got %s, expected %s",
1249 d, test.places, got, expected)
1250 }
1251
1252
1253 if test.expectedFixed == "" {
1254 test.expectedFixed = test.expected
1255 }
1256 gotStr := got.StringFixed(test.places)
1257 if gotStr != test.expectedFixed {
1258 t.Errorf("(%s).StringFixed(%d): got %s, expected %s",
1259 d, test.places, gotStr, test.expectedFixed)
1260 }
1261 }
1262 }
1263
1264 func TestDecimal_RoundFloorAndStringFixed(t *testing.T) {
1265 type testData struct {
1266 input string
1267 places int32
1268 expected string
1269 expectedFixed string
1270 }
1271 tests := []testData{
1272 {"1.454", 0, "1", ""},
1273 {"1.454", 1, "1.4", ""},
1274 {"1.454", 2, "1.45", ""},
1275 {"1.454", 3, "1.454", ""},
1276 {"1.454", 4, "1.454", "1.4540"},
1277 {"1.454", 5, "1.454", "1.45400"},
1278 {"1.554", 0, "1", ""},
1279 {"1.554", 1, "1.5", ""},
1280 {"1.554", 2, "1.55", ""},
1281 {"0.554", 0, "0", ""},
1282 {"0.454", 0, "0", ""},
1283 {"0.454", 5, "0.454", "0.45400"},
1284 {"0", 0, "0", ""},
1285 {"0", 1, "0", "0.0"},
1286 {"0", 2, "0", "0.00"},
1287 {"0", -1, "0", ""},
1288 {"5", 2, "5", "5.00"},
1289 {"5", 1, "5", "5.0"},
1290 {"5", 0, "5", ""},
1291 {"500", 2, "500", "500.00"},
1292 {"500", -2, "500", ""},
1293 {"545", -1, "540", ""},
1294 {"545", -2, "500", ""},
1295 {"545", -3, "0", ""},
1296 {"545", -4, "0", ""},
1297 {"499", -3, "0", ""},
1298 {"499", -4, "0", ""},
1299 {"1.1001", 2, "1.10", ""},
1300 {"-1.1001", 2, "-1.11", ""},
1301 {"-1.454", 0, "-2", ""},
1302 {"-1.454", 1, "-1.5", ""},
1303 {"-1.454", 2, "-1.46", ""},
1304 {"-1.454", 3, "-1.454", ""},
1305 {"-1.454", 4, "-1.454", "-1.4540"},
1306 {"-1.454", 5, "-1.454", "-1.45400"},
1307 {"-1.554", 0, "-2", ""},
1308 {"-1.554", 1, "-1.6", ""},
1309 {"-1.554", 2, "-1.56", ""},
1310 {"-0.554", 0, "-1", ""},
1311 {"-0.454", 0, "-1", ""},
1312 {"-0.454", 5, "-0.454", "-0.45400"},
1313 {"-5", 2, "-5", "-5.00"},
1314 {"-5", 1, "-5", "-5.0"},
1315 {"-5", 0, "-5", ""},
1316 {"-500", 2, "-500", "-500.00"},
1317 {"-500", -2, "-500", ""},
1318 {"-545", -1, "-550", ""},
1319 {"-545", -2, "-600", ""},
1320 {"-545", -3, "-1000", ""},
1321 {"-545", -4, "-10000", ""},
1322 {"-499", -3, "-1000", ""},
1323 {"-499", -4, "-10000", ""},
1324 }
1325
1326 for _, test := range tests {
1327 d, err := NewFromString(test.input)
1328 if err != nil {
1329 t.Fatal(err)
1330 }
1331
1332
1333 expected, err := NewFromString(test.expected)
1334 if err != nil {
1335 t.Fatal(err)
1336 }
1337 got := d.RoundFloor(test.places)
1338 if !got.Equal(expected) {
1339 t.Errorf("Rounding floor %s to %d places, got %s, expected %s",
1340 d, test.places, got, expected)
1341 }
1342
1343
1344 if test.expectedFixed == "" {
1345 test.expectedFixed = test.expected
1346 }
1347 gotStr := got.StringFixed(test.places)
1348 if gotStr != test.expectedFixed {
1349 t.Errorf("(%s).StringFixed(%d): got %s, expected %s",
1350 d, test.places, gotStr, test.expectedFixed)
1351 }
1352 }
1353 }
1354
1355 func TestDecimal_RoundUpAndStringFixed(t *testing.T) {
1356 type testData struct {
1357 input string
1358 places int32
1359 expected string
1360 expectedFixed string
1361 }
1362 tests := []testData{
1363 {"1.454", 0, "2", ""},
1364 {"1.454", 1, "1.5", ""},
1365 {"1.454", 2, "1.46", ""},
1366 {"1.454", 3, "1.454", ""},
1367 {"1.454", 4, "1.454", "1.4540"},
1368 {"1.454", 5, "1.454", "1.45400"},
1369 {"1.554", 0, "2", ""},
1370 {"1.554", 1, "1.6", ""},
1371 {"1.554", 2, "1.56", ""},
1372 {"0.554", 0, "1", ""},
1373 {"0.454", 0, "1", ""},
1374 {"0.454", 5, "0.454", "0.45400"},
1375 {"0", 0, "0", ""},
1376 {"0", 1, "0", "0.0"},
1377 {"0", 2, "0", "0.00"},
1378 {"0", -1, "0", ""},
1379 {"5", 2, "5", "5.00"},
1380 {"5", 1, "5", "5.0"},
1381 {"5", 0, "5", ""},
1382 {"500", 2, "500", "500.00"},
1383 {"500", -2, "500", ""},
1384 {"545", -1, "550", ""},
1385 {"545", -2, "600", ""},
1386 {"545", -3, "1000", ""},
1387 {"545", -4, "10000", ""},
1388 {"499", -3, "1000", ""},
1389 {"499", -4, "10000", ""},
1390 {"1.1001", 2, "1.11", ""},
1391 {"-1.1001", 2, "-1.11", ""},
1392 {"-1.454", 0, "-2", ""},
1393 {"-1.454", 1, "-1.5", ""},
1394 {"-1.454", 2, "-1.46", ""},
1395 {"-1.454", 3, "-1.454", ""},
1396 {"-1.454", 4, "-1.454", "-1.4540"},
1397 {"-1.454", 5, "-1.454", "-1.45400"},
1398 {"-1.554", 0, "-2", ""},
1399 {"-1.554", 1, "-1.6", ""},
1400 {"-1.554", 2, "-1.56", ""},
1401 {"-0.554", 0, "-1", ""},
1402 {"-0.454", 0, "-1", ""},
1403 {"-0.454", 5, "-0.454", "-0.45400"},
1404 {"-5", 2, "-5", "-5.00"},
1405 {"-5", 1, "-5", "-5.0"},
1406 {"-5", 0, "-5", ""},
1407 {"-500", 2, "-500", "-500.00"},
1408 {"-500", -2, "-500", ""},
1409 {"-545", -1, "-550", ""},
1410 {"-545", -2, "-600", ""},
1411 {"-545", -3, "-1000", ""},
1412 {"-545", -4, "-10000", ""},
1413 {"-499", -3, "-1000", ""},
1414 {"-499", -4, "-10000", ""},
1415 }
1416
1417 for _, test := range tests {
1418 d, err := NewFromString(test.input)
1419 if err != nil {
1420 t.Fatal(err)
1421 }
1422
1423
1424 expected, err := NewFromString(test.expected)
1425 if err != nil {
1426 t.Fatal(err)
1427 }
1428 got := d.RoundUp(test.places)
1429 if !got.Equal(expected) {
1430 t.Errorf("Rounding up %s to %d places, got %s, expected %s",
1431 d, test.places, got, expected)
1432 }
1433
1434
1435 if test.expectedFixed == "" {
1436 test.expectedFixed = test.expected
1437 }
1438 gotStr := got.StringFixed(test.places)
1439 if gotStr != test.expectedFixed {
1440 t.Errorf("(%s).StringFixed(%d): got %s, expected %s",
1441 d, test.places, gotStr, test.expectedFixed)
1442 }
1443 }
1444 }
1445
1446 func TestDecimal_RoundDownAndStringFixed(t *testing.T) {
1447 type testData struct {
1448 input string
1449 places int32
1450 expected string
1451 expectedFixed string
1452 }
1453 tests := []testData{
1454 {"1.454", 0, "1", ""},
1455 {"1.454", 1, "1.4", ""},
1456 {"1.454", 2, "1.45", ""},
1457 {"1.454", 3, "1.454", ""},
1458 {"1.454", 4, "1.454", "1.4540"},
1459 {"1.454", 5, "1.454", "1.45400"},
1460 {"1.554", 0, "1", ""},
1461 {"1.554", 1, "1.5", ""},
1462 {"1.554", 2, "1.55", ""},
1463 {"0.554", 0, "0", ""},
1464 {"0.454", 0, "0", ""},
1465 {"0.454", 5, "0.454", "0.45400"},
1466 {"0", 0, "0", ""},
1467 {"0", 1, "0", "0.0"},
1468 {"0", 2, "0", "0.00"},
1469 {"0", -1, "0", ""},
1470 {"5", 2, "5", "5.00"},
1471 {"5", 1, "5", "5.0"},
1472 {"5", 0, "5", ""},
1473 {"500", 2, "500", "500.00"},
1474 {"500", -2, "500", ""},
1475 {"545", -1, "540", ""},
1476 {"545", -2, "500", ""},
1477 {"545", -3, "0", ""},
1478 {"545", -4, "0", ""},
1479 {"499", -3, "0", ""},
1480 {"499", -4, "0", ""},
1481 {"1.1001", 2, "1.10", ""},
1482 {"-1.1001", 2, "-1.10", ""},
1483 {"-1.454", 0, "-1", ""},
1484 {"-1.454", 1, "-1.4", ""},
1485 {"-1.454", 2, "-1.45", ""},
1486 {"-1.454", 3, "-1.454", ""},
1487 {"-1.454", 4, "-1.454", "-1.4540"},
1488 {"-1.454", 5, "-1.454", "-1.45400"},
1489 {"-1.554", 0, "-1", ""},
1490 {"-1.554", 1, "-1.5", ""},
1491 {"-1.554", 2, "-1.55", ""},
1492 {"-0.554", 0, "0", ""},
1493 {"-0.454", 0, "0", ""},
1494 {"-0.454", 5, "-0.454", "-0.45400"},
1495 {"-5", 2, "-5", "-5.00"},
1496 {"-5", 1, "-5", "-5.0"},
1497 {"-5", 0, "-5", ""},
1498 {"-500", 2, "-500", "-500.00"},
1499 {"-500", -2, "-500", ""},
1500 {"-545", -1, "-540", ""},
1501 {"-545", -2, "-500", ""},
1502 {"-545", -3, "0", ""},
1503 {"-545", -4, "0", ""},
1504 {"-499", -3, "0", ""},
1505 {"-499", -4, "0", ""},
1506 }
1507
1508 for _, test := range tests {
1509 d, err := NewFromString(test.input)
1510 if err != nil {
1511 t.Fatal(err)
1512 }
1513
1514
1515 expected, err := NewFromString(test.expected)
1516 if err != nil {
1517 t.Fatal(err)
1518 }
1519 got := d.RoundDown(test.places)
1520 if !got.Equal(expected) {
1521 t.Errorf("Rounding down %s to %d places, got %s, expected %s",
1522 d, test.places, got, expected)
1523 }
1524
1525
1526 if test.expectedFixed == "" {
1527 test.expectedFixed = test.expected
1528 }
1529 gotStr := got.StringFixed(test.places)
1530 if gotStr != test.expectedFixed {
1531 t.Errorf("(%s).StringFixed(%d): got %s, expected %s",
1532 d, test.places, gotStr, test.expectedFixed)
1533 }
1534 }
1535 }
1536
1537 func TestDecimal_BankRoundAndStringFixed(t *testing.T) {
1538 type testData struct {
1539 input string
1540 places int32
1541 expected string
1542 expectedFixed string
1543 }
1544 tests := []testData{
1545 {"1.454", 0, "1", ""},
1546 {"1.454", 1, "1.5", ""},
1547 {"1.454", 2, "1.45", ""},
1548 {"1.454", 3, "1.454", ""},
1549 {"1.454", 4, "1.454", "1.4540"},
1550 {"1.454", 5, "1.454", "1.45400"},
1551 {"1.554", 0, "2", ""},
1552 {"1.554", 1, "1.6", ""},
1553 {"1.554", 2, "1.55", ""},
1554 {"0.554", 0, "1", ""},
1555 {"0.454", 0, "0", ""},
1556 {"0.454", 5, "0.454", "0.45400"},
1557 {"0", 0, "0", ""},
1558 {"0", 1, "0", "0.0"},
1559 {"0", 2, "0", "0.00"},
1560 {"0", -1, "0", ""},
1561 {"5", 2, "5", "5.00"},
1562 {"5", 1, "5", "5.0"},
1563 {"5", 0, "5", ""},
1564 {"500", 2, "500", "500.00"},
1565 {"545", -2, "500", ""},
1566 {"545", -3, "1000", ""},
1567 {"545", -4, "0", ""},
1568 {"499", -3, "0", ""},
1569 {"499", -4, "0", ""},
1570 {"1.45", 1, "1.4", ""},
1571 {"1.55", 1, "1.6", ""},
1572 {"1.65", 1, "1.6", ""},
1573 {"545", -1, "540", ""},
1574 {"565", -1, "560", ""},
1575 {"555", -1, "560", ""},
1576 }
1577
1578
1579 for _, test := range tests {
1580 expected := test.expected
1581 if expected != "0" {
1582 expected = "-" + expected
1583 }
1584 expectedStr := test.expectedFixed
1585 if strings.ContainsAny(expectedStr, "123456789") && expectedStr != "" {
1586 expectedStr = "-" + expectedStr
1587 }
1588 tests = append(tests,
1589 testData{"-" + test.input, test.places, expected, expectedStr})
1590 }
1591
1592 for _, test := range tests {
1593 d, err := NewFromString(test.input)
1594 if err != nil {
1595 panic(err)
1596 }
1597
1598
1599 expected, err := NewFromString(test.expected)
1600 if err != nil {
1601 panic(err)
1602 }
1603 got := d.RoundBank(test.places)
1604 if !got.Equal(expected) {
1605 t.Errorf("Bank Rounding %s to %d places, got %s, expected %s",
1606 d, test.places, got, expected)
1607 }
1608
1609
1610 if test.expectedFixed == "" {
1611 test.expectedFixed = test.expected
1612 }
1613 gotStr := d.StringFixedBank(test.places)
1614 if gotStr != test.expectedFixed {
1615 t.Errorf("(%s).StringFixed(%d): got %s, expected %s",
1616 d, test.places, gotStr, test.expectedFixed)
1617 }
1618 }
1619 }
1620
1621 func TestDecimal_Uninitialized(t *testing.T) {
1622 a := Decimal{}
1623 b := Decimal{}
1624
1625 decs := []Decimal{
1626 a,
1627 a.rescale(10),
1628 a.Abs(),
1629 a.Add(b),
1630 a.Sub(b),
1631 a.Mul(b),
1632 a.Shift(0),
1633 a.Div(New(1, -1)),
1634 a.Round(2),
1635 a.Floor(),
1636 a.Ceil(),
1637 a.Truncate(2),
1638 }
1639
1640 for _, d := range decs {
1641 if d.String() != "0" {
1642 t.Errorf("expected 0, got %s", d.String())
1643 }
1644 if d.StringFixed(3) != "0.000" {
1645 t.Errorf("expected 0, got %s", d.StringFixed(3))
1646 }
1647 if d.StringScaled(-2) != "0" {
1648 t.Errorf("expected 0, got %s", d.StringScaled(-2))
1649 }
1650 }
1651
1652 if a.Cmp(b) != 0 {
1653 t.Errorf("a != b")
1654 }
1655 if a.Sign() != 0 {
1656 t.Errorf("a.Sign() != 0")
1657 }
1658 if a.Exponent() != 0 {
1659 t.Errorf("a.Exponent() != 0")
1660 }
1661 if a.IntPart() != 0 {
1662 t.Errorf("a.IntPar() != 0")
1663 }
1664 f, _ := a.Float64()
1665 if f != 0 {
1666 t.Errorf("a.Float64() != 0")
1667 }
1668 if a.Rat().RatString() != "0" {
1669 t.Errorf("a.Rat() != 0, got %s", a.Rat().RatString())
1670 }
1671 }
1672
1673 func TestDecimal_Add(t *testing.T) {
1674 type Inp struct {
1675 a string
1676 b string
1677 }
1678
1679 inputs := map[Inp]string{
1680 Inp{"2", "3"}: "5",
1681 Inp{"2454495034", "3451204593"}: "5905699627",
1682 Inp{"24544.95034", ".3451204593"}: "24545.2954604593",
1683 Inp{".1", ".1"}: "0.2",
1684 Inp{".1", "-.1"}: "0",
1685 Inp{"0", "1.001"}: "1.001",
1686 }
1687
1688 for inp, res := range inputs {
1689 a, err := NewFromString(inp.a)
1690 if err != nil {
1691 t.FailNow()
1692 }
1693 b, err := NewFromString(inp.b)
1694 if err != nil {
1695 t.FailNow()
1696 }
1697 c := a.Add(b)
1698 if c.String() != res {
1699 t.Errorf("expected %s, got %s", res, c.String())
1700 }
1701 }
1702 }
1703
1704 func TestDecimal_Sub(t *testing.T) {
1705 type Inp struct {
1706 a string
1707 b string
1708 }
1709
1710 inputs := map[Inp]string{
1711 Inp{"2", "3"}: "-1",
1712 Inp{"12", "3"}: "9",
1713 Inp{"-2", "9"}: "-11",
1714 Inp{"2454495034", "3451204593"}: "-996709559",
1715 Inp{"24544.95034", ".3451204593"}: "24544.6052195407",
1716 Inp{".1", "-.1"}: "0.2",
1717 Inp{".1", ".1"}: "0",
1718 Inp{"0", "1.001"}: "-1.001",
1719 Inp{"1.001", "0"}: "1.001",
1720 Inp{"2.3", ".3"}: "2",
1721 }
1722
1723 for inp, res := range inputs {
1724 a, err := NewFromString(inp.a)
1725 if err != nil {
1726 t.FailNow()
1727 }
1728 b, err := NewFromString(inp.b)
1729 if err != nil {
1730 t.FailNow()
1731 }
1732 c := a.Sub(b)
1733 if c.String() != res {
1734 t.Errorf("expected %s, got %s", res, c.String())
1735 }
1736 }
1737 }
1738
1739 func TestDecimal_Neg(t *testing.T) {
1740 inputs := map[string]string{
1741 "0": "0",
1742 "10": "-10",
1743 "5.56": "-5.56",
1744 "-10": "10",
1745 "-5.56": "5.56",
1746 }
1747
1748 for inp, res := range inputs {
1749 a, err := NewFromString(inp)
1750 if err != nil {
1751 t.FailNow()
1752 }
1753 b := a.Neg()
1754 if b.String() != res {
1755 t.Errorf("expected %s, got %s", res, b.String())
1756 }
1757 }
1758 }
1759
1760 func TestDecimal_NegFromEmpty(t *testing.T) {
1761 a := Decimal{}
1762 b := a.Neg()
1763 if b.String() != "0" {
1764 t.Errorf("expected %s, got %s", "0", b)
1765 }
1766 }
1767
1768 func TestDecimal_Mul(t *testing.T) {
1769 type Inp struct {
1770 a string
1771 b string
1772 }
1773
1774 inputs := map[Inp]string{
1775 Inp{"2", "3"}: "6",
1776 Inp{"2454495034", "3451204593"}: "8470964534836491162",
1777 Inp{"24544.95034", ".3451204593"}: "8470.964534836491162",
1778 Inp{".1", ".1"}: "0.01",
1779 Inp{"0", "1.001"}: "0",
1780 }
1781
1782 for inp, res := range inputs {
1783 a, err := NewFromString(inp.a)
1784 if err != nil {
1785 t.FailNow()
1786 }
1787 b, err := NewFromString(inp.b)
1788 if err != nil {
1789 t.FailNow()
1790 }
1791 c := a.Mul(b)
1792 if c.String() != res {
1793 t.Errorf("expected %s, got %s", res, c.String())
1794 }
1795 }
1796
1797
1798 c := New(1234, 5).Mul(New(45, -1))
1799 if c.String() != "555300000" {
1800 t.Errorf("Expected %s, got %s", "555300000", c.String())
1801 }
1802 }
1803
1804 func TestDecimal_Shift(t *testing.T) {
1805 type Inp struct {
1806 a string
1807 b int32
1808 }
1809
1810 inputs := map[Inp]string{
1811 Inp{"6", 3}: "6000",
1812 Inp{"10", -2}: "0.1",
1813 Inp{"2.2", 1}: "22",
1814 Inp{"-2.2", -1}: "-0.22",
1815 Inp{"12.88", 5}: "1288000",
1816 Inp{"-10234274355545544493", -3}: "-10234274355545544.493",
1817 Inp{"-4612301402398.4753343454", 5}: "-461230140239847533.43454",
1818 }
1819
1820 for inp, expectedStr := range inputs {
1821 num, _ := NewFromString(inp.a)
1822
1823 got := num.Shift(inp.b)
1824 expected, _ := NewFromString(expectedStr)
1825 if !got.Equal(expected) {
1826 t.Errorf("expected %v when shifting %v by %v, got %v",
1827 expected, num, inp.b, got)
1828 }
1829 }
1830 }
1831
1832 func TestDecimal_Div(t *testing.T) {
1833 type Inp struct {
1834 a string
1835 b string
1836 }
1837
1838 inputs := map[Inp]string{
1839 Inp{"6", "3"}: "2",
1840 Inp{"10", "2"}: "5",
1841 Inp{"2.2", "1.1"}: "2",
1842 Inp{"-2.2", "-1.1"}: "2",
1843 Inp{"12.88", "5.6"}: "2.3",
1844 Inp{"1023427554493", "43432632"}: "23563.5628642767953828",
1845 Inp{"1", "434324545566634"}: "0.0000000000000023",
1846 Inp{"1", "3"}: "0.3333333333333333",
1847 Inp{"2", "3"}: "0.6666666666666667",
1848 Inp{"10000", "3"}: "3333.3333333333333333",
1849 Inp{"10234274355545544493", "-3"}: "-3411424785181848164.3333333333333333",
1850 Inp{"-4612301402398.4753343454", "23.5"}: "-196268144782.9138440146978723",
1851 }
1852
1853 for inp, expectedStr := range inputs {
1854 num, err := NewFromString(inp.a)
1855 if err != nil {
1856 t.FailNow()
1857 }
1858 denom, err := NewFromString(inp.b)
1859 if err != nil {
1860 t.FailNow()
1861 }
1862 got := num.Div(denom)
1863 expected, _ := NewFromString(expectedStr)
1864 if !got.Equal(expected) {
1865 t.Errorf("expected %v when dividing %v by %v, got %v",
1866 expected, num, denom, got)
1867 }
1868 got2 := num.DivRound(denom, int32(DivisionPrecision))
1869 if !got2.Equal(expected) {
1870 t.Errorf("expected %v on DivRound (%v,%v), got %v", expected, num, denom, got2)
1871 }
1872 }
1873
1874 type Inp2 struct {
1875 n int64
1876 exp int32
1877 n2 int64
1878 exp2 int32
1879 }
1880
1881
1882 inputs2 := map[Inp2]string{
1883 Inp2{124, 10, 3, 1}: "41333333333.3333333333333333",
1884 Inp2{124, 10, 3, 0}: "413333333333.3333333333333333",
1885 Inp2{124, 10, 6, 1}: "20666666666.6666666666666667",
1886 Inp2{124, 10, 6, 0}: "206666666666.6666666666666667",
1887 Inp2{10, 10, 10, 1}: "1000000000",
1888 }
1889
1890 for inp, expectedAbs := range inputs2 {
1891 for i := -1; i <= 1; i += 2 {
1892 for j := -1; j <= 1; j += 2 {
1893 n := inp.n * int64(i)
1894 n2 := inp.n2 * int64(j)
1895 num := New(n, inp.exp)
1896 denom := New(n2, inp.exp2)
1897 expected := expectedAbs
1898 if i != j {
1899 expected = "-" + expectedAbs
1900 }
1901 got := num.Div(denom)
1902 if got.String() != expected {
1903 t.Errorf("expected %s when dividing %v by %v, got %v",
1904 expected, num, denom, got)
1905 }
1906 }
1907 }
1908 }
1909 }
1910
1911 func TestDecimal_QuoRem(t *testing.T) {
1912 type Inp4 struct {
1913 d string
1914 d2 string
1915 exp int32
1916 q string
1917 r string
1918 }
1919 cases := []Inp4{
1920 {"10", "1", 0, "10", "0"},
1921 {"1", "10", 0, "0", "1"},
1922 {"1", "4", 2, "0.25", "0"},
1923 {"1", "8", 2, "0.12", "0.04"},
1924 {"10", "3", 1, "3.3", "0.1"},
1925 {"100", "3", 1, "33.3", "0.1"},
1926 {"1000", "10", -3, "0", "1000"},
1927 {"1e-3", "2e-5", 0, "50", "0"},
1928 {"1e-3", "2e-3", 1, "0.5", "0"},
1929 {"4e-3", "0.8", 4, "5e-3", "0"},
1930 {"4.1e-3", "0.8", 3, "5e-3", "1e-4"},
1931 {"-4", "-3", 0, "1", "-1"},
1932 {"-4", "3", 0, "-1", "-1"},
1933 }
1934
1935 for _, inp4 := range cases {
1936 d, _ := NewFromString(inp4.d)
1937 d2, _ := NewFromString(inp4.d2)
1938 prec := inp4.exp
1939 q, r := d.QuoRem(d2, prec)
1940 expectedQ, _ := NewFromString(inp4.q)
1941 expectedR, _ := NewFromString(inp4.r)
1942 if !q.Equal(expectedQ) || !r.Equal(expectedR) {
1943 t.Errorf("bad QuoRem division %s , %s , %d got %v, %v expected %s , %s",
1944 inp4.d, inp4.d2, prec, q, r, inp4.q, inp4.r)
1945 }
1946 if !d.Equal(d2.Mul(q).Add(r)) {
1947 t.Errorf("not fitting: d=%v, d2= %v, prec=%d, q=%v, r=%v",
1948 d, d2, prec, q, r)
1949 }
1950 if !q.Equal(q.Truncate(prec)) {
1951 t.Errorf("quotient wrong precision: d=%v, d2= %v, prec=%d, q=%v, r=%v",
1952 d, d2, prec, q, r)
1953 }
1954 if r.Abs().Cmp(d2.Abs().Mul(New(1, -prec))) >= 0 {
1955 t.Errorf("remainder too large: d=%v, d2= %v, prec=%d, q=%v, r=%v",
1956 d, d2, prec, q, r)
1957 }
1958 if r.value.Sign()*d.value.Sign() < 0 {
1959 t.Errorf("signum of divisor and rest do not match: d=%v, d2= %v, prec=%d, q=%v, r=%v",
1960 d, d2, prec, q, r)
1961 }
1962 }
1963 }
1964
1965 type DivTestCase struct {
1966 d Decimal
1967 d2 Decimal
1968 prec int32
1969 }
1970
1971 func createDivTestCases() []DivTestCase {
1972 res := make([]DivTestCase, 0)
1973 var n int32 = 5
1974 a := []int{1, 2, 3, 6, 7, 10, 100, 14, 5, 400, 0, 1000000, 1000000 + 1, 1000000 - 1}
1975 for s := -1; s < 2; s = s + 2 {
1976 for s2 := -1; s2 < 2; s2 = s2 + 2 {
1977 for e1 := -n; e1 <= n; e1++ {
1978 for e2 := -n; e2 <= n; e2++ {
1979 var prec int32
1980 for prec = -n; prec <= n; prec++ {
1981 for _, v1 := range a {
1982 for _, v2 := range a {
1983 sign1 := New(int64(s), 0)
1984 sign2 := New(int64(s2), 0)
1985 d := sign1.Mul(New(int64(v1), e1))
1986 d2 := sign2.Mul(New(int64(v2), e2))
1987 res = append(res, DivTestCase{d, d2, prec})
1988 }
1989 }
1990 }
1991 }
1992 }
1993 }
1994 }
1995 return res
1996 }
1997
1998 func TestDecimal_QuoRem2(t *testing.T) {
1999 for _, tc := range createDivTestCases() {
2000 d := tc.d
2001 if sign(tc.d2) == 0 {
2002 continue
2003 }
2004 d2 := tc.d2
2005 prec := tc.prec
2006 q, r := d.QuoRem(d2, prec)
2007
2008 if !d.Equal(d2.Mul(q).Add(r)) {
2009 t.Errorf("not fitting, d=%v, d2=%v, prec=%d, q=%v, r=%v",
2010 d, d2, prec, q, r)
2011 }
2012
2013 if !q.Equal(q.Truncate(prec)) {
2014 t.Errorf("quotient wrong precision, d=%v, d2=%v, prec=%d, q=%v, r=%v",
2015 d, d2, prec, q, r)
2016 }
2017
2018 if r.Abs().Cmp(d2.Abs().Mul(New(1, -prec))) >= 0 {
2019 t.Errorf("remainder too large, d=%v, d2=%v, prec=%d, q=%v, r=%v",
2020 d, d2, prec, q, r)
2021 }
2022
2023 if r.value.Sign()*d.value.Sign() < 0 {
2024 t.Errorf("signum of divisor and rest do not match, "+
2025 "d=%v, d2=%v, prec=%d, q=%v, r=%v",
2026 d, d2, prec, q, r)
2027 }
2028 }
2029 }
2030
2031
2032
2033
2034 func (d Decimal) DivOld(d2 Decimal, prec int) Decimal {
2035
2036 ratNum := d.Rat()
2037 ratDenom := d2.Rat()
2038
2039 quoRat := big.NewRat(0, 1).Quo(ratNum, ratDenom)
2040
2041
2042 ret, err := NewFromString(quoRat.FloatString(prec))
2043 if err != nil {
2044 panic(err)
2045 }
2046 return ret
2047 }
2048
2049 func sign(d Decimal) int {
2050 return d.value.Sign()
2051 }
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064 func TestDecimal_DivRound(t *testing.T) {
2065 cases := []struct {
2066 d string
2067 d2 string
2068 prec int32
2069 result string
2070 }{
2071 {"2", "2", 0, "1"},
2072 {"1", "2", 0, "1"},
2073 {"-1", "2", 0, "-1"},
2074 {"-1", "-2", 0, "1"},
2075 {"1", "-2", 0, "-1"},
2076 {"1", "-20", 1, "-0.1"},
2077 {"1", "-20", 2, "-0.05"},
2078 {"1", "20.0000000000000000001", 1, "0"},
2079 {"1", "19.9999999999999999999", 1, "0.1"},
2080 }
2081 for _, s := range cases {
2082 d, _ := NewFromString(s.d)
2083 d2, _ := NewFromString(s.d2)
2084 result, _ := NewFromString(s.result)
2085 prec := s.prec
2086 q := d.DivRound(d2, prec)
2087 if sign(q)*sign(d)*sign(d2) < 0 {
2088 t.Errorf("sign of quotient wrong, got: %v/%v is about %v", d, d2, q)
2089 }
2090 x := q.Mul(d2).Abs().Sub(d.Abs()).Mul(New(2, 0))
2091 if x.Cmp(d2.Abs().Mul(New(1, -prec))) > 0 {
2092 t.Errorf("wrong rounding, got: %v/%v prec=%d is about %v", d, d2, prec, q)
2093 }
2094 if x.Cmp(d2.Abs().Mul(New(-1, -prec))) <= 0 {
2095 t.Errorf("wrong rounding, got: %v/%v prec=%d is about %v", d, d2, prec, q)
2096 }
2097 if !q.Equal(result) {
2098 t.Errorf("rounded division wrong %s / %s scale %d = %s, got %v", s.d, s.d2, prec, s.result, q)
2099 }
2100 }
2101 }
2102
2103 func TestDecimal_DivRound2(t *testing.T) {
2104 for _, tc := range createDivTestCases() {
2105 d := tc.d
2106 if sign(tc.d2) == 0 {
2107 continue
2108 }
2109 d2 := tc.d2
2110 prec := tc.prec
2111 q := d.DivRound(d2, prec)
2112 if sign(q)*sign(d)*sign(d2) < 0 {
2113 t.Errorf("sign of quotient wrong, got: %v/%v is about %v", d, d2, q)
2114 }
2115 x := q.Mul(d2).Abs().Sub(d.Abs()).Mul(New(2, 0))
2116 if x.Cmp(d2.Abs().Mul(New(1, -prec))) > 0 {
2117 t.Errorf("wrong rounding, got: %v/%v prec=%d is about %v", d, d2, prec, q)
2118 }
2119 if x.Cmp(d2.Abs().Mul(New(-1, -prec))) <= 0 {
2120 t.Errorf("wrong rounding, got: %v/%v prec=%d is about %v", d, d2, prec, q)
2121 }
2122 }
2123 }
2124
2125 func TestDecimal_RoundCash(t *testing.T) {
2126 tests := []struct {
2127 d string
2128 interval uint8
2129 result string
2130 }{
2131 {"3.44", 5, "3.45"},
2132 {"3.43", 5, "3.45"},
2133 {"3.42", 5, "3.40"},
2134 {"3.425", 5, "3.45"},
2135 {"3.47", 5, "3.45"},
2136 {"3.478", 5, "3.50"},
2137 {"3.48", 5, "3.50"},
2138 {"348", 5, "348"},
2139
2140 {"3.23", 10, "3.20"},
2141 {"3.33", 10, "3.30"},
2142 {"3.53", 10, "3.50"},
2143 {"3.949", 10, "3.90"},
2144 {"3.95", 10, "4.00"},
2145 {"395", 10, "395"},
2146
2147 {"3.23", 25, "3.25"},
2148 {"3.33", 25, "3.25"},
2149 {"3.53", 25, "3.50"},
2150 {"3.93", 25, "4.00"},
2151 {"3.41", 25, "3.50"},
2152
2153 {"3.249", 50, "3.00"},
2154 {"3.33", 50, "3.50"},
2155 {"3.749999999", 50, "3.50"},
2156 {"3.75", 50, "4.00"},
2157 {"3.93", 50, "4.00"},
2158 {"393", 50, "393"},
2159
2160 {"3.249", 100, "3.00"},
2161 {"3.49999", 100, "3.00"},
2162 {"3.50", 100, "4.00"},
2163 {"3.75", 100, "4.00"},
2164 {"3.93", 100, "4.00"},
2165 {"393", 100, "393"},
2166 }
2167 for i, test := range tests {
2168 d, _ := NewFromString(test.d)
2169 haveRounded := d.RoundCash(test.interval)
2170 result, _ := NewFromString(test.result)
2171
2172 if !haveRounded.Equal(result) {
2173 t.Errorf("Index %d: Cash rounding for %q interval %d want %q, have %q", i, test.d, test.interval, test.result, haveRounded)
2174 }
2175 }
2176 }
2177
2178 func TestDecimal_RoundCash_Panic(t *testing.T) {
2179 defer func() {
2180 if r := recover(); r != nil {
2181 if have, ok := r.(string); ok {
2182 const want = "Decimal does not support this Cash rounding interval `231`. Supported: 5, 10, 25, 50, 100"
2183 if want != have {
2184 t.Errorf("\nWant: %q\nHave: %q", want, have)
2185 }
2186 } else {
2187 t.Errorf("Panic should contain an error string but got:\n%+v", r)
2188 }
2189 } else {
2190 t.Error("Expecting a panic but got nothing")
2191 }
2192 }()
2193 d, _ := NewFromString("1")
2194 d.RoundCash(231)
2195 }
2196
2197 func TestDecimal_Mod(t *testing.T) {
2198 type Inp struct {
2199 a string
2200 b string
2201 }
2202
2203 inputs := map[Inp]string{
2204 Inp{"3", "2"}: "1",
2205 Inp{"3451204593", "2454495034"}: "996709559",
2206 Inp{"9999999999", "1275"}: "324",
2207 Inp{"9999999999.9999998", "1275.49"}: "239.2399998",
2208 Inp{"24544.95034", "0.3451204593"}: "0.3283950433",
2209 Inp{"0.499999999999999999", "0.25"}: "0.249999999999999999",
2210 Inp{"0.989512958912895912", "0.000001"}: "0.000000958912895912",
2211 Inp{"0.1", "0.1"}: "0",
2212 Inp{"0", "1.001"}: "0",
2213 Inp{"-7.5", "2"}: "-1.5",
2214 Inp{"7.5", "-2"}: "1.5",
2215 Inp{"-7.5", "-2"}: "-1.5",
2216 Inp{"41", "21"}: "20",
2217 Inp{"400000000001", "200000000001"}: "200000000000",
2218 }
2219
2220 for inp, res := range inputs {
2221 a, err := NewFromString(inp.a)
2222 if err != nil {
2223 t.FailNow()
2224 }
2225 b, err := NewFromString(inp.b)
2226 if err != nil {
2227 t.FailNow()
2228 }
2229 c := a.Mod(b)
2230 if c.String() != res {
2231 t.Errorf("expected %s, got %s", res, c.String())
2232 }
2233 }
2234 }
2235
2236 func TestDecimal_Overflow(t *testing.T) {
2237 if !didPanic(func() { New(1, math.MinInt32).Mul(New(1, math.MinInt32)) }) {
2238 t.Fatalf("should have gotten an overflow panic")
2239 }
2240 if !didPanic(func() { New(1, math.MaxInt32).Mul(New(1, math.MaxInt32)) }) {
2241 t.Fatalf("should have gotten an overflow panic")
2242 }
2243 }
2244
2245 func TestDecimal_ExtremeValues(t *testing.T) {
2246
2247 if testing.Short() {
2248 t.Skip()
2249 }
2250
2251
2252
2253
2254
2255
2256
2257 const timeLimit = 1 * time.Second
2258 test := func(f func()) {
2259 c := make(chan bool)
2260 go func() {
2261 f()
2262 close(c)
2263 }()
2264 select {
2265 case <-c:
2266 case <-time.After(timeLimit):
2267 }
2268 }
2269
2270 test(func() {
2271 got := New(123, math.MinInt32).Floor()
2272 if !got.Equal(NewFromFloat(0)) {
2273 t.Errorf("Error: got %s, expected 0", got)
2274 }
2275 })
2276 test(func() {
2277 got := New(123, math.MinInt32).Ceil()
2278 if !got.Equal(NewFromFloat(1)) {
2279 t.Errorf("Error: got %s, expected 1", got)
2280 }
2281 })
2282 test(func() {
2283 got := New(123, math.MinInt32).Rat().FloatString(10)
2284 expected := "0.0000000000"
2285 if got != expected {
2286 t.Errorf("Error: got %s, expected %s", got, expected)
2287 }
2288 })
2289 }
2290
2291 func TestIntPart(t *testing.T) {
2292 for _, testCase := range []struct {
2293 Dec string
2294 IntPart int64
2295 }{
2296 {"0.01", 0},
2297 {"12.1", 12},
2298 {"9999.999", 9999},
2299 {"-32768.01234", -32768},
2300 } {
2301 d, err := NewFromString(testCase.Dec)
2302 if err != nil {
2303 t.Fatal(err)
2304 }
2305 if d.IntPart() != testCase.IntPart {
2306 t.Errorf("expect %d, got %d", testCase.IntPart, d.IntPart())
2307 }
2308 }
2309 }
2310
2311 func TestBigInt(t *testing.T) {
2312 testCases := []struct {
2313 Dec string
2314 BigIntRep string
2315 }{
2316 {"0.0", "0"},
2317 {"0.00000", "0"},
2318 {"0.01", "0"},
2319 {"12.1", "12"},
2320 {"9999.999", "9999"},
2321 {"-32768.01234", "-32768"},
2322 {"-572372.0000000001", "-572372"},
2323 }
2324
2325 for _, testCase := range testCases {
2326 d, err := NewFromString(testCase.Dec)
2327 if err != nil {
2328 t.Fatal(err)
2329 }
2330 if d.BigInt().String() != testCase.BigIntRep {
2331 t.Errorf("expect %s, got %s", testCase.BigIntRep, d.BigInt())
2332 }
2333 }
2334 }
2335
2336 func TestBigFloat(t *testing.T) {
2337 testCases := []struct {
2338 Dec string
2339 BigFloatRep string
2340 }{
2341 {"0.0", "0"},
2342 {"0.00000", "0"},
2343 {"0.01", "0.01"},
2344 {"12.1", "12.1"},
2345 {"9999.999", "9999.999"},
2346 {"-32768.01234", "-32768.01234"},
2347 {"-572372.0000000001", "-572372"},
2348 {"512.012345123451234512345", "512.0123451"},
2349 {"1.010101010101010101010101010101", "1.01010101"},
2350 {"55555555.555555555555555555555", "55555555.56"},
2351 }
2352
2353 for _, testCase := range testCases {
2354 d, err := NewFromString(testCase.Dec)
2355 if err != nil {
2356 t.Fatal(err)
2357 }
2358 if d.BigFloat().String() != testCase.BigFloatRep {
2359 t.Errorf("expect %s, got %s", testCase.BigFloatRep, d.BigFloat())
2360 }
2361 }
2362 }
2363
2364 func TestDecimal_Min(t *testing.T) {
2365
2366 testCases := [][]float64{
2367 {0, 0},
2368 {1, 1},
2369 {-1, -1},
2370 {1, 1, 2},
2371 {-2, 1, 2, -2},
2372 {-3, 0, 2, -2, -3},
2373 }
2374
2375 for _, test := range testCases {
2376 expected, input := test[0], test[1:]
2377 expectedDecimal := NewFromFloat(expected)
2378 decimalInput := []Decimal{}
2379 for _, inp := range input {
2380 d := NewFromFloat(inp)
2381 decimalInput = append(decimalInput, d)
2382 }
2383 got := Min(decimalInput[0], decimalInput[1:]...)
2384 if !got.Equal(expectedDecimal) {
2385 t.Errorf("Expected %v, got %v, input=%+v", expectedDecimal, got,
2386 decimalInput)
2387 }
2388 }
2389 }
2390
2391 func TestDecimal_Max(t *testing.T) {
2392
2393 testCases := [][]float64{
2394 {0, 0},
2395 {1, 1},
2396 {-1, -1},
2397 {2, 1, 2},
2398 {2, 1, 2, -2},
2399 {3, 0, 3, -2},
2400 {-2, -3, -2},
2401 }
2402
2403 for _, test := range testCases {
2404 expected, input := test[0], test[1:]
2405 expectedDecimal := NewFromFloat(expected)
2406 decimalInput := []Decimal{}
2407 for _, inp := range input {
2408 d := NewFromFloat(inp)
2409 decimalInput = append(decimalInput, d)
2410 }
2411 got := Max(decimalInput[0], decimalInput[1:]...)
2412 if !got.Equal(expectedDecimal) {
2413 t.Errorf("Expected %v, got %v, input=%+v", expectedDecimal, got,
2414 decimalInput)
2415 }
2416 }
2417 }
2418
2419 func scanHelper(t *testing.T, dbval interface{}, expected Decimal) {
2420 t.Helper()
2421
2422 a := Decimal{}
2423 if err := a.Scan(dbval); err != nil {
2424
2425 t.Errorf("a.Scan(%v) failed with message: %s", dbval, err)
2426 } else if !a.Equal(expected) {
2427
2428 t.Errorf("%s does not equal to %s", a, expected)
2429 }
2430 }
2431
2432 func TestDecimal_Scan(t *testing.T) {
2433
2434
2435
2436 dbvalue := 54.33
2437 expected := NewFromFloat(dbvalue)
2438 scanHelper(t, dbvalue, expected)
2439
2440
2441
2442 dbvalueFloat32 := float32(54.33)
2443 expected = NewFromFloat(float64(dbvalueFloat32))
2444 scanHelper(t, dbvalueFloat32, expected)
2445
2446
2447
2448 dbvalueInt := int64(0)
2449 expected = New(dbvalueInt, 0)
2450 scanHelper(t, dbvalueInt, expected)
2451
2452
2453 dbvalueUint64 := uint64(2)
2454 expected = New(2, 0)
2455 scanHelper(t, dbvalueUint64, expected)
2456
2457
2458
2459 valueStr := "535.666"
2460 dbvalueStr := []byte(valueStr)
2461 expected, err := NewFromString(valueStr)
2462 if err != nil {
2463 t.Fatal(err)
2464 }
2465 scanHelper(t, dbvalueStr, expected)
2466 scanHelper(t, valueStr, expected)
2467
2468 type foo struct{}
2469 a := Decimal{}
2470 err = a.Scan(foo{})
2471 if err == nil {
2472 t.Errorf("a.Scan(Foo{}) should have thrown an error but did not")
2473 }
2474 }
2475
2476 func TestDecimal_Value(t *testing.T) {
2477
2478 var d Decimal
2479 if _, ok := interface{}(d).(driver.Valuer); !ok {
2480 t.Error("Decimal does not implement driver.Valuer")
2481 }
2482
2483
2484 a := New(1234, -2)
2485 expected := "12.34"
2486 value, err := a.Value()
2487 if err != nil {
2488 t.Errorf("Decimal(12.34).Value() failed with message: %s", err)
2489 } else if value.(string) != expected {
2490 t.Errorf("%s does not equal to %s", a, expected)
2491 }
2492 }
2493
2494
2495
2496 func TestDecimal_Scale(t *testing.T) {
2497 a := New(1234, -3)
2498 if a.Exponent() != -3 {
2499 t.Errorf("error")
2500 }
2501 }
2502
2503 func TestDecimal_Abs1(t *testing.T) {
2504 a := New(-1234, -4)
2505 b := New(1234, -4)
2506
2507 c := a.Abs()
2508 if c.Cmp(b) != 0 {
2509 t.Errorf("error")
2510 }
2511 }
2512
2513 func TestDecimal_Abs2(t *testing.T) {
2514 a := New(-1234, -4)
2515 b := New(1234, -4)
2516
2517 c := b.Abs()
2518 if c.Cmp(a) == 0 {
2519 t.Errorf("error")
2520 }
2521 }
2522
2523 func TestDecimal_Equalities(t *testing.T) {
2524 a := New(1234, 3)
2525 b := New(1234, 3)
2526 c := New(1234, 4)
2527
2528 if !a.Equal(b) {
2529 t.Errorf("%q should equal %q", a, b)
2530 }
2531 if a.Equal(c) {
2532 t.Errorf("%q should not equal %q", a, c)
2533 }
2534
2535
2536 if !a.Equals(b) {
2537 t.Errorf("%q should equal %q", a, b)
2538 }
2539
2540 if !c.GreaterThan(b) {
2541 t.Errorf("%q should be greater than %q", c, b)
2542 }
2543 if b.GreaterThan(c) {
2544 t.Errorf("%q should not be greater than %q", b, c)
2545 }
2546 if !a.GreaterThanOrEqual(b) {
2547 t.Errorf("%q should be greater or equal %q", a, b)
2548 }
2549 if !c.GreaterThanOrEqual(b) {
2550 t.Errorf("%q should be greater or equal %q", c, b)
2551 }
2552 if b.GreaterThanOrEqual(c) {
2553 t.Errorf("%q should not be greater or equal %q", b, c)
2554 }
2555 if !b.LessThan(c) {
2556 t.Errorf("%q should be less than %q", a, b)
2557 }
2558 if c.LessThan(b) {
2559 t.Errorf("%q should not be less than %q", a, b)
2560 }
2561 if !a.LessThanOrEqual(b) {
2562 t.Errorf("%q should be less than or equal %q", a, b)
2563 }
2564 if !b.LessThanOrEqual(c) {
2565 t.Errorf("%q should be less than or equal %q", a, b)
2566 }
2567 if c.LessThanOrEqual(b) {
2568 t.Errorf("%q should not be less than or equal %q", a, b)
2569 }
2570 }
2571
2572 func TestDecimal_ScalesNotEqual(t *testing.T) {
2573 a := New(1234, 2)
2574 b := New(1234, 3)
2575 if a.Equal(b) {
2576 t.Errorf("%q should not equal %q", a, b)
2577 }
2578 }
2579
2580 func TestDecimal_Cmp1(t *testing.T) {
2581 a := New(123, 3)
2582 b := New(-1234, 2)
2583
2584 if a.Cmp(b) != 1 {
2585 t.Errorf("Error")
2586 }
2587 }
2588
2589 func TestDecimal_Cmp2(t *testing.T) {
2590 a := New(123, 3)
2591 b := New(1234, 2)
2592
2593 if a.Cmp(b) != -1 {
2594 t.Errorf("Error")
2595 }
2596 }
2597
2598 func TestDecimal_Pow(t *testing.T) {
2599 for _, testCase := range []struct {
2600 Base string
2601 Exponent string
2602 Expected string
2603 }{
2604 {"0.0", "1.0", "0.0"},
2605 {"0.0", "5.7", "0.0"},
2606 {"0.0", "-3.2", "0.0"},
2607 {"3.13", "0.0", "1.0"},
2608 {"-591.5", "0.0", "1.0"},
2609 {"3.0", "3.0", "27.0"},
2610 {"3.0", "10.0", "59049.0"},
2611 {"3.13", "5.0", "300.4150512793"},
2612 {"4.0", "2.0", "16.0"},
2613 {"4.0", "-2.0", "0.0625"},
2614 {"629.25", "5.0", "98654323103449.5673828125"},
2615 {"5.0", "5.73", "10118.08037159375"},
2616 {"962.0", "3.2791", "6055212360.0000044205714144"},
2617 {"5.69169126", "5.18515912", "8242.26344757948412597909547972726268869189399260047793106028930864"},
2618 {"13.1337", "3.5196719618391835", "8636.856220644773844815693636723928750940666269885"},
2619 {"67762386.283696923", "4.85917691669163916681738", "112761146905370140621385730157437443321.91755738117317148674362233906499698561022574811238435007575701773212242750262081945556470501"},
2620 {"-3.0", "6.0", "729"},
2621 {"-13.757", "5.0", "-492740.983929899460557"},
2622 {"3.0", "-6.0", "0.0013717421124829"},
2623 {"13.757", "-5.0", "0.000002029463821"},
2624 {"66.12", "-7.61313", "0.000000000000013854086588876805036"},
2625 {"6696871.12", "-2.61313", "0.000000000000000001455988684546983"},
2626 {"-3.0", "-6.0", "0.0013717421124829"},
2627 {"-13.757", "-5.0", "-0.000002029463821"},
2628 } {
2629 base, _ := NewFromString(testCase.Base)
2630 exp, _ := NewFromString(testCase.Exponent)
2631 expected, _ := NewFromString(testCase.Expected)
2632
2633 result := base.Pow(exp)
2634
2635 if result.Cmp(expected) != 0 {
2636 t.Errorf("expected %s, got %s, for %s^%s", testCase.Expected, result.String(), testCase.Base, testCase.Exponent)
2637 }
2638 }
2639 }
2640
2641 func TestDecimal_PowWithPrecision(t *testing.T) {
2642 for _, testCase := range []struct {
2643 Base string
2644 Exponent string
2645 Precision int32
2646 Expected string
2647 }{
2648 {"0.0", "1.0", 2, "0.0"},
2649 {"0.0", "5.7", 2, "0.0"},
2650 {"0.0", "-3.2", 2, "0.0"},
2651 {"3.13", "0.0", 2, "1.0"},
2652 {"-591.5", "0.0", 2, "1.0"},
2653 {"3.0", "3.0", 2, "27.0"},
2654 {"3.0", "10.0", 2, "59049.0"},
2655 {"3.13", "5.0", 5, "300.4150512793"},
2656 {"4.0", "2.0", 2, "16.0"},
2657 {"4.0", "-2.0", 2, "0.06"},
2658 {"4.0", "-2.0", 4, "0.0625"},
2659 {"629.25", "5.0", 6, "98654323103449.5673828125"},
2660 {"5.0", "5.73", 20, "10118.080371595019317118681359884375"},
2661 {"962.0", "3.2791", 15, "6055212360.000004406551603058195732"},
2662 {"5.69169126", "5.18515912", 4, "8242.26344757948412587366859330429895955552280978668983459852256"},
2663 {"13.1337", "3.5196719618391835", 8, "8636.85622064477384481569363672392591908386390769375"},
2664 {"67762386.283696923", "4.85917691669163916681738", 10, "112761146905370140621385730157437443321.917557381173174638304347353880676293576708009282115993465286373470882947470198597518762"},
2665 {"-3.0", "6.0", 2, "729"},
2666 {"-13.757", "5.0", 4, "-492740.983929899460557"},
2667 {"3.0", "-6.0", 10, "0.0013717421"},
2668 {"13.757", "-5.0", 20, "0.00000202946382098037"},
2669 {"66.12", "-7.61313", 20, "0.00000000000001385381563049821591633907104023700216"},
2670 {"6696871.12", "-2.61313", 24, "0.0000000000000000014558252733872790626400278983397459207418"},
2671 {"-3.0", "-6.0", 8, "0.00137174"},
2672 {"-13.757", "-5.0", 16, "-0.000002029463821"},
2673 } {
2674 base, _ := NewFromString(testCase.Base)
2675 exp, _ := NewFromString(testCase.Exponent)
2676 expected, _ := NewFromString(testCase.Expected)
2677
2678 result, _ := base.PowWithPrecision(exp, testCase.Precision)
2679
2680 if result.Cmp(expected) != 0 {
2681 t.Errorf("expected %s, got %s, for %s^%s", testCase.Expected, result.String(), testCase.Base, testCase.Exponent)
2682 }
2683 }
2684 }
2685
2686 func TestDecimal_PowWithPrecision_Infinity(t *testing.T) {
2687 for _, testCase := range []struct {
2688 Base string
2689 Exponent string
2690 }{
2691 {"0.0", "0.0"},
2692 {"0.0", "-2.0"},
2693 {"0.0", "-4.6"},
2694 {"-66.12", "7.61313"},
2695 {"-5696871.12", "5.61313"},
2696 } {
2697 base, _ := NewFromString(testCase.Base)
2698 exp, _ := NewFromString(testCase.Exponent)
2699
2700 _, err := base.PowWithPrecision(exp, 5)
2701
2702 if err == nil {
2703 t.Errorf("lool it should be error")
2704 }
2705 }
2706 }
2707
2708 func TestDecimal_PowWithPrecision_UndefinedResult(t *testing.T) {
2709 base := RequireFromString("0")
2710 exponent := RequireFromString("0")
2711
2712 _, err := base.PowWithPrecision(exponent, 4)
2713
2714 if err == nil {
2715 t.Errorf("expected error, cannot be represent undefined value of 0**0")
2716 }
2717 }
2718
2719 func TestDecimal_PowWithPrecision_InfinityResult(t *testing.T) {
2720 for _, testCase := range []struct {
2721 Base string
2722 Exponent string
2723 }{
2724 {"0.0", "-2.0"},
2725 {"0.0", "-4.6"},
2726 {"0.0", "-9239.671333"},
2727 } {
2728 base, _ := NewFromString(testCase.Base)
2729 exp, _ := NewFromString(testCase.Exponent)
2730
2731 _, err := base.PowWithPrecision(exp, 4)
2732
2733 if err == nil {
2734 t.Errorf("expected error, cannot represent infinity value of 0 ** y, where y < 0")
2735 }
2736 }
2737 }
2738
2739 func TestDecimal_PowWithPrecision_ImaginaryResult(t *testing.T) {
2740 for _, testCase := range []struct {
2741 Base string
2742 Exponent string
2743 }{
2744 {"-0.2261", "106.12"},
2745 {"-66.12", "7.61313"},
2746 {"-5696871.12", "5.61313"},
2747 } {
2748 base, _ := NewFromString(testCase.Base)
2749 exp, _ := NewFromString(testCase.Exponent)
2750
2751 _, err := base.PowWithPrecision(exp, 4)
2752
2753 if err == nil {
2754 t.Errorf("expected error, cannot represent imaginary value of x ** y, where x < 0 and y is non-integer decimal")
2755 }
2756 }
2757 }
2758
2759 func TestDecimal_PowInt32(t *testing.T) {
2760 for _, testCase := range []struct {
2761 Decimal string
2762 Exponent int32
2763 Expected string
2764 }{
2765 {"0.0", 1, "0.0"},
2766 {"3.13", 0, "1.0"},
2767 {"-591.5", 0, "1.0"},
2768 {"3.0", 3, "27.0"},
2769 {"3.0", 10, "59049.0"},
2770 {"3.13", 5, "300.4150512793"},
2771 {"629.25", 5, "98654323103449.5673828125"},
2772 {"-3.0", 6, "729"},
2773 {"-13.757", 5, "-492740.983929899460557"},
2774 {"3.0", -6, "0.0013717421124829"},
2775 {"-13.757", -5, "-0.000002029463821"},
2776 } {
2777 base, _ := NewFromString(testCase.Decimal)
2778 expected, _ := NewFromString(testCase.Expected)
2779
2780 result, _ := base.PowInt32(testCase.Exponent)
2781
2782 if result.Cmp(expected) != 0 {
2783 t.Errorf("expected %s, got %s, for %s**%d", testCase.Expected, result.String(), testCase.Decimal, testCase.Exponent)
2784 }
2785 }
2786 }
2787
2788 func TestDecimal_PowInt32_UndefinedResult(t *testing.T) {
2789 base := RequireFromString("0")
2790
2791 _, err := base.PowInt32(0)
2792
2793 if err == nil {
2794 t.Errorf("expected error, cannot be represent undefined value of 0**0")
2795 }
2796 }
2797
2798 func TestDecimal_PowBigInt(t *testing.T) {
2799 for _, testCase := range []struct {
2800 Decimal string
2801 Exponent *big.Int
2802 Expected string
2803 }{
2804 {"3.13", big.NewInt(0), "1.0"},
2805 {"-591.5", big.NewInt(0), "1.0"},
2806 {"3.0", big.NewInt(3), "27.0"},
2807 {"3.0", big.NewInt(10), "59049.0"},
2808 {"3.13", big.NewInt(5), "300.4150512793"},
2809 {"629.25", big.NewInt(5), "98654323103449.5673828125"},
2810 {"-3.0", big.NewInt(6), "729"},
2811 {"-13.757", big.NewInt(5), "-492740.983929899460557"},
2812 {"3.0", big.NewInt(-6), "0.0013717421124829"},
2813 {"-13.757", big.NewInt(-5), "-0.000002029463821"},
2814 } {
2815 base, _ := NewFromString(testCase.Decimal)
2816 expected, _ := NewFromString(testCase.Expected)
2817
2818 result, _ := base.PowBigInt(testCase.Exponent)
2819
2820 if result.Cmp(expected) != 0 {
2821 t.Errorf("expected %s, got %s, for %s**%d", testCase.Expected, result.String(), testCase.Decimal, testCase.Exponent)
2822 }
2823 }
2824 }
2825
2826 func TestDecimal_PowBigInt_UndefinedResult(t *testing.T) {
2827 base := RequireFromString("0")
2828
2829 _, err := base.PowBigInt(big.NewInt(0))
2830
2831 if err == nil {
2832 t.Errorf("expected error, undefined value of 0**0 cannot be represented")
2833 }
2834 }
2835
2836 func TestDecimal_IsInteger(t *testing.T) {
2837 for _, testCase := range []struct {
2838 Dec string
2839 IsInteger bool
2840 }{
2841 {"0", true},
2842 {"0.0000", true},
2843 {"0.01", false},
2844 {"0.01010101010000", false},
2845 {"12.0", true},
2846 {"12.00000000000000", true},
2847 {"12.10000", false},
2848 {"9999.0000", true},
2849 {"99999999.000000000", true},
2850 {"-656323444.0000000000000", true},
2851 {"-32768.01234", false},
2852 {"-32768.0123423562623600000", false},
2853 } {
2854 d, err := NewFromString(testCase.Dec)
2855 if err != nil {
2856 t.Fatal(err)
2857 }
2858 if d.IsInteger() != testCase.IsInteger {
2859 t.Errorf("expect %t, got %t, for %s", testCase.IsInteger, d.IsInteger(), testCase.Dec)
2860 }
2861 }
2862 }
2863
2864 func TestDecimal_ExpHullAbrham(t *testing.T) {
2865 for _, testCase := range []struct {
2866 Dec string
2867 OverallPrecision uint32
2868 ExpectedDec string
2869 }{
2870 {"0", 1, "1"},
2871 {"0.00", 5, "1"},
2872 {"0.5", 5, "1.6487"},
2873 {"0.569297", 10, "1.767024397"},
2874 {"0.569297", 16, "1.76702439654095"},
2875 {"0.569297", 20, "1.7670243965409496521"},
2876 {"1.0", 0, "3"},
2877 {"1.0", 1, "3"},
2878 {"1.0", 5, "2.7183"},
2879 {"1.0", 10, "2.718281828"},
2880 {"3.0", 0, "20"},
2881 {"3.0", 2, "20"},
2882 {"5.26", 0, "200"},
2883 {"5.26", 2, "190"},
2884 {"5.26", 10, "192.4814913"},
2885 {"5.2663117716", 2, "190"},
2886 {"5.2663117716", 10, "193.7002327"},
2887 {"26.1", 2, "220000000000"},
2888 {"26.1", 10, "216314672100"},
2889 {"26.1", 20, "216314672147.05767284"},
2890 {"50.1591", 10, "6078834887000000000000"},
2891 {"50.1591", 30, "6078834886623434464595.07937141"},
2892 {"-0.5", 5, "0.60653"},
2893 {"-0.569297", 10, "0.5659231429"},
2894 {"-0.569297", 16, "0.565923142859576"},
2895 {"-0.569297", 20, "0.56592314285957604443"},
2896 {"-1.0", 1, "0.4"},
2897 {"-1.0", 5, "0.36788"},
2898 {"-3.0", 1, "0"},
2899 {"-3.0", 2, "0.05"},
2900 {"-3.0", 10, "0.0497870684"},
2901 {"-5.26", 2, "0.01"},
2902 {"-5.26", 10, "0.0051953047"},
2903 {"-5.2663117716", 2, "0.01"},
2904 {"-5.2663117716", 10, "0.0051626164"},
2905 {"-26.1", 2, "0"},
2906 {"-26.1", 15, "0.000000000004623"},
2907 {"-50.1591", 10, "0"},
2908 {"-50.1591", 30, "0.000000000000000000000164505208"},
2909 } {
2910 d, _ := NewFromString(testCase.Dec)
2911 expected, _ := NewFromString(testCase.ExpectedDec)
2912
2913 exp, err := d.ExpHullAbrham(testCase.OverallPrecision)
2914 if err != nil {
2915 t.Fatal(err)
2916 }
2917
2918 if exp.Cmp(expected) != 0 {
2919 t.Errorf("expected %s, got %s, for decimal %s", testCase.ExpectedDec, exp.String(), testCase.Dec)
2920 }
2921
2922 }
2923 }
2924
2925 func TestDecimal_ExpTaylor(t *testing.T) {
2926 for _, testCase := range []struct {
2927 Dec string
2928 Precision int32
2929 ExpectedDec string
2930 }{
2931 {"0", 1, "1"},
2932 {"0.00", 5, "1"},
2933 {"0", -1, "0"},
2934 {"0.5", 5, "1.64872"},
2935 {"0.569297", 10, "1.7670243965"},
2936 {"0.569297", 16, "1.7670243965409497"},
2937 {"0.569297", 20, "1.76702439654094965215"},
2938 {"1.0", 0, "3"},
2939 {"1.0", 1, "2.7"},
2940 {"1.0", 5, "2.71828"},
2941 {"1.0", -1, "0"},
2942 {"1.0", -5, "0"},
2943 {"3.0", 1, "20.1"},
2944 {"3.0", 2, "20.09"},
2945 {"5.26", 0, "192"},
2946 {"5.26", 2, "192.48"},
2947 {"5.26", 10, "192.4814912972"},
2948 {"5.26", -2, "200"},
2949 {"5.2663117716", 2, "193.70"},
2950 {"5.2663117716", 10, "193.7002326701"},
2951 {"26.1", 2, "216314672147.06"},
2952 {"26.1", 20, "216314672147.05767284062928674083"},
2953 {"26.1", -2, "216314672100"},
2954 {"26.1", -10, "220000000000"},
2955 {"50.1591", 10, "6078834886623434464595.0793714061"},
2956 {"-0.5", 5, "0.60653"},
2957 {"-0.569297", 10, "0.5659231429"},
2958 {"-0.569297", 16, "0.565923142859576"},
2959 {"-0.569297", 20, "0.56592314285957604443"},
2960 {"-1.0", 1, "0.4"},
2961 {"-1.0", 5, "0.36788"},
2962 {"-1.0", -1, "0"},
2963 {"-1.0", -5, "0"},
2964 {"-3.0", 1, "0.1"},
2965 {"-3.0", 2, "0.05"},
2966 {"-3.0", 10, "0.0497870684"},
2967 {"-5.26", 2, "0.01"},
2968 {"-5.26", 10, "0.0051953047"},
2969 {"-5.26", -2, "0"},
2970 {"-5.2663117716", 2, "0.01"},
2971 {"-5.2663117716", 10, "0.0051626164"},
2972 {"-26.1", 2, "0"},
2973 {"-26.1", 15, "0.000000000004623"},
2974 {"-26.1", -2, "0"},
2975 {"-26.1", -10, "0"},
2976 {"-50.1591", 10, "0"},
2977 {"-50.1591", 30, "0.000000000000000000000164505208"},
2978 } {
2979 d, _ := NewFromString(testCase.Dec)
2980 expected, _ := NewFromString(testCase.ExpectedDec)
2981
2982 exp, err := d.ExpTaylor(testCase.Precision)
2983 if err != nil {
2984 t.Fatal(err)
2985 }
2986
2987 if exp.Cmp(expected) != 0 {
2988 t.Errorf("expected %s, got %s", testCase.ExpectedDec, exp.String())
2989 }
2990 }
2991 }
2992
2993 func TestDecimal_Ln(t *testing.T) {
2994 for _, testCase := range []struct {
2995 Dec string
2996 Precision int32
2997 Expected string
2998 }{
2999 {"0.1", 25, "-2.3025850929940456840179915"},
3000 {"0.01", 25, "-4.6051701859880913680359829"},
3001 {"0.001", 25, "-6.9077552789821370520539744"},
3002 {"0.00000001", 25, "-18.4206807439523654721439316"},
3003 {"1.0", 10, "0.0"},
3004 {"1.01", 25, "0.0099503308531680828482154"},
3005 {"1.001", 25, "0.0009995003330835331668094"},
3006 {"1.0001", 25, "0.0000999950003333083353332"},
3007 {"1.1", 25, "0.0953101798043248600439521"},
3008 {"1.13", 25, "0.1222176327242492005461486"},
3009 {"3.13", 10, "1.1410330046"},
3010 {"3.13", 25, "1.1410330045520618486427824"},
3011 {"3.13", 50, "1.14103300455206184864278239988848193837089629107972"},
3012 {"3.13", 100, "1.1410330045520618486427823998884819383708962910797239760817078430268177216960996098918971117211892839"},
3013 {"5.71", 25, "1.7422190236679188486939833"},
3014 {"5.7185108151957193571930205", 50, "1.74370842450178929149992165925283704012576949094645"},
3015 {"839101.0351", 25, "13.6400864014410013994397240"},
3016 {"839101.0351094726488848490572028502", 50, "13.64008640145229044389152437468283605382056561604272"},
3017 {"5023583755703750094849.03519358513093500275017501750602739169823", 25, "49.9684305274348922267409953"},
3018 {"5023583755703750094849.03519358513093500275017501750602739169823", -1, "50.0"},
3019 {"66.12", 18, "4.191471272952823429"},
3020 } {
3021 d, _ := NewFromString(testCase.Dec)
3022 expected, _ := NewFromString(testCase.Expected)
3023
3024 ln, err := d.Ln(testCase.Precision)
3025 if err != nil {
3026 t.Fatal(err)
3027 }
3028
3029 if ln.Cmp(expected) != 0 {
3030 t.Errorf("expected %s, got %s, for decimal %s", testCase.Expected, ln.String(), testCase.Dec)
3031 }
3032 }
3033 }
3034
3035 func TestDecimal_LnZero(t *testing.T) {
3036 d := New(0, 0)
3037
3038 _, err := d.Ln(5)
3039
3040 if err == nil {
3041 t.Errorf("expected error, natural logarithm of 0 cannot be represented (-infinity)")
3042 }
3043 }
3044
3045 func TestDecimal_LnNegative(t *testing.T) {
3046 d := New(-20, 2)
3047
3048 _, err := d.Ln(5)
3049
3050 if err == nil {
3051 t.Errorf("expected error, natural logarithm cannot be calculated for nagative decimals")
3052 }
3053 }
3054
3055 func TestDecimal_NumDigits(t *testing.T) {
3056 for _, testCase := range []struct {
3057 Dec string
3058 ExpectedNumDigits int
3059 }{
3060 {"0", 1},
3061 {"0.00", 1},
3062 {"1.0", 2},
3063 {"3.0", 2},
3064 {"5.26", 3},
3065 {"5.2663117716", 11},
3066 {"3164836416948884.2162426426426267863", 35},
3067 {"26.1", 3},
3068 {"529.1591", 7},
3069 {"-1.0", 2},
3070 {"-3.0", 2},
3071 {"-5.26", 3},
3072 {"-5.2663117716", 11},
3073 {"-26.1", 3},
3074 {"", 1},
3075 } {
3076 d, _ := NewFromString(testCase.Dec)
3077
3078 nums := d.NumDigits()
3079 if nums != testCase.ExpectedNumDigits {
3080 t.Errorf("expected %d digits for decimal %s, got %d", testCase.ExpectedNumDigits, testCase.Dec, nums)
3081 }
3082 }
3083 }
3084
3085 func TestDecimal_Sign(t *testing.T) {
3086 if Zero.Sign() != 0 {
3087 t.Errorf("%q should have sign 0", Zero)
3088 }
3089
3090 one := New(1, 0)
3091 if one.Sign() != 1 {
3092 t.Errorf("%q should have sign 1", one)
3093 }
3094
3095 mone := New(-1, 0)
3096 if mone.Sign() != -1 {
3097 t.Errorf("%q should have sign -1", mone)
3098 }
3099 }
3100
3101 func didPanic(f func()) bool {
3102 ret := false
3103 func() {
3104
3105 defer func() {
3106 if message := recover(); message != nil {
3107 ret = true
3108 }
3109 }()
3110
3111
3112 f()
3113
3114 }()
3115
3116 return ret
3117
3118 }
3119
3120 func TestDecimal_Coefficient(t *testing.T) {
3121 d := New(123, 0)
3122 co := d.Coefficient()
3123 if co.Int64() != 123 {
3124 t.Error("Coefficient should be 123; Got:", co)
3125 }
3126 co.Set(big.NewInt(0))
3127 if d.IntPart() != 123 {
3128 t.Error("Modifying coefficient modified Decimal; Got:", d)
3129 }
3130 }
3131
3132 func TestDecimal_CoefficientInt64(t *testing.T) {
3133 type Inp struct {
3134 Dec string
3135 Coefficient int64
3136 }
3137
3138 testCases := []Inp{
3139 {"1", 1},
3140 {"1.111", 1111},
3141 {"1.000000", 1000000},
3142 {"1.121215125511", 1121215125511},
3143 {"100000000000000000", 100000000000000000},
3144 {"9223372036854775807", 9223372036854775807},
3145 {"10000000000000000000", -8446744073709551616},
3146 }
3147
3148
3149 for _, tc := range testCases {
3150 testCases = append(testCases, Inp{"-" + tc.Dec, -tc.Coefficient})
3151 }
3152
3153 for _, tc := range testCases {
3154 d := RequireFromString(tc.Dec)
3155 coefficient := d.CoefficientInt64()
3156 if coefficient != tc.Coefficient {
3157 t.Errorf("expect coefficient %d, got %d, for decimal %s", tc.Coefficient, coefficient, tc.Dec)
3158 }
3159 }
3160 }
3161
3162 func TestNullDecimal_Scan(t *testing.T) {
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173 a := NullDecimal{}
3174 var dbvaluePtr interface{}
3175 err := a.Scan(dbvaluePtr)
3176 if err != nil {
3177
3178 t.Errorf("a.Scan(nil) failed with message: %s", err)
3179 } else {
3180 if a.Valid {
3181 t.Errorf("%s is not null", a.Decimal)
3182 }
3183 }
3184
3185 dbvalue := 54.33
3186 expected := NewFromFloat(dbvalue)
3187
3188 err = a.Scan(dbvalue)
3189 if err != nil {
3190
3191 t.Errorf("a.Scan(54.33) failed with message: %s", err)
3192
3193 } else {
3194
3195 if !a.Valid {
3196 t.Errorf("%s is null", a.Decimal)
3197 } else if !a.Decimal.Equals(expected) {
3198 t.Errorf("%s does not equal to %s", a.Decimal, expected)
3199 }
3200 }
3201
3202
3203
3204 dbvalueInt := int64(0)
3205 expected = New(dbvalueInt, 0)
3206
3207 err = a.Scan(dbvalueInt)
3208 if err != nil {
3209
3210 t.Errorf("a.Scan(0) failed with message: %s", err)
3211
3212 } else {
3213
3214 if !a.Valid {
3215 t.Errorf("%s is null", a.Decimal)
3216 } else if !a.Decimal.Equals(expected) {
3217 t.Errorf("%v does not equal %v", a, expected)
3218 }
3219 }
3220
3221
3222
3223 valueStr := "535.666"
3224 dbvalueStr := []byte(valueStr)
3225 expected, err = NewFromString(valueStr)
3226 if err != nil {
3227 t.Fatal(err)
3228 }
3229
3230 err = a.Scan(dbvalueStr)
3231 if err != nil {
3232
3233 t.Errorf("a.Scan('535.666') failed with message: %s", err)
3234
3235 } else {
3236
3237 if !a.Valid {
3238 t.Errorf("%s is null", a.Decimal)
3239 } else if !a.Decimal.Equals(expected) {
3240 t.Errorf("%v does not equal %v", a, expected)
3241 }
3242 }
3243
3244
3245 expected, err = NewFromString(valueStr)
3246 if err != nil {
3247 t.Fatal(err)
3248 }
3249
3250 err = a.Scan(valueStr)
3251 if err != nil {
3252
3253 t.Errorf("a.Scan('535.666') failed with message: %s", err)
3254 } else {
3255
3256 if !a.Valid {
3257 t.Errorf("%s is null", a.Decimal)
3258 } else if !a.Decimal.Equals(expected) {
3259 t.Errorf("%v does not equal %v", a, expected)
3260 }
3261 }
3262 }
3263
3264 func TestNullDecimal_Value(t *testing.T) {
3265
3266 var nullDecimal NullDecimal
3267 if _, ok := interface{}(nullDecimal).(driver.Valuer); !ok {
3268 t.Error("NullDecimal does not implement driver.Valuer")
3269 }
3270
3271
3272 value, err := nullDecimal.Value()
3273 if err != nil {
3274 t.Errorf("NullDecimal{}.Valid() failed with message: %s", err)
3275 } else if value != nil {
3276 t.Errorf("%v is not nil", value)
3277 }
3278
3279
3280 a := NullDecimal{Decimal: New(1234, -2), Valid: true}
3281 expected := "12.34"
3282 value, err = a.Value()
3283 if err != nil {
3284 t.Errorf("NullDecimal(12.34).Value() failed with message: %s", err)
3285 } else if value.(string) != expected {
3286 t.Errorf("%v does not equal %v", a, expected)
3287 }
3288 }
3289
3290 func TestBinary(t *testing.T) {
3291 for _, y := range testTable {
3292 x := y.float
3293
3294
3295 d1 := NewFromFloat(x)
3296
3297
3298 b, err := d1.MarshalBinary()
3299 if err != nil {
3300 t.Errorf("error marshalling %v to binary: %v", d1, err)
3301 }
3302
3303
3304 var d2 Decimal
3305 err = (&d2).UnmarshalBinary(b)
3306 if err != nil {
3307 t.Errorf("error unmarshalling from binary: %v", err)
3308 }
3309
3310
3311 if !d1.Equals(d2) {
3312 t.Errorf("expected %v when restoring, got %v", d1, d2)
3313 }
3314 }
3315 }
3316
3317 func TestBinary_Zero(t *testing.T) {
3318 var d1 Decimal
3319
3320 b, err := d1.MarshalBinary()
3321 if err != nil {
3322 t.Fatalf("error marshalling %v to binary: %v", d1, err)
3323 }
3324
3325 var d2 Decimal
3326 err = (&d2).UnmarshalBinary(b)
3327 if err != nil {
3328 t.Errorf("error unmarshalling from binary: %v", err)
3329 }
3330
3331 if !d1.Equals(d2) {
3332 t.Errorf("expected %v when restoring, got %v", d1, d2)
3333 }
3334 }
3335
3336 func TestBinary_DataTooShort(t *testing.T) {
3337 var d Decimal
3338
3339 err := d.UnmarshalBinary(nil)
3340 if err == nil {
3341 t.Fatalf("expected error, got %v", d)
3342 }
3343 }
3344
3345 func TestBinary_InvalidValue(t *testing.T) {
3346 var d Decimal
3347
3348 err := d.UnmarshalBinary([]byte{0, 0, 0, 0, 'x'})
3349 if err == nil {
3350 t.Fatalf("expected error, got %v", d)
3351 }
3352 }
3353
3354 func slicesEqual(a, b []byte) bool {
3355 for i, val := range a {
3356 if b[i] != val {
3357 return false
3358 }
3359 }
3360 return true
3361 }
3362
3363 func TestGobEncode(t *testing.T) {
3364 for _, y := range testTable {
3365 x := y.float
3366 d1 := NewFromFloat(x)
3367
3368 b1, err := d1.GobEncode()
3369 if err != nil {
3370 t.Errorf("error encoding %v to binary: %v", d1, err)
3371 }
3372
3373 d2 := NewFromFloat(x)
3374
3375 b2, err := d2.GobEncode()
3376 if err != nil {
3377 t.Errorf("error encoding %v to binary: %v", d2, err)
3378 }
3379
3380 if !slicesEqual(b1, b2) {
3381 t.Errorf("something about the gobencode is not working properly \n%v\n%v", b1, b2)
3382 }
3383
3384 var d3 Decimal
3385 err = d3.GobDecode(b1)
3386 if err != nil {
3387 t.Errorf("Error gobdecoding %v, got %v", b1, d3)
3388 }
3389 var d4 Decimal
3390 err = d4.GobDecode(b2)
3391 if err != nil {
3392 t.Errorf("Error gobdecoding %v, got %v", b2, d4)
3393 }
3394
3395 eq := d3.Equal(d4)
3396 if eq != true {
3397 t.Errorf("Encoding then decoding mutated Decimal")
3398 }
3399
3400 eq = d1.Equal(d3)
3401 if eq != true {
3402 t.Errorf("Error gobencoding/decoding %v, got %v", d1, d3)
3403 }
3404 }
3405 }
3406
3407 func TestSum(t *testing.T) {
3408 vals := make([]Decimal, 10)
3409 var i = int64(0)
3410
3411 for key := range vals {
3412 vals[key] = New(i, 0)
3413 i++
3414 }
3415
3416 sum := Sum(vals[0], vals[1:]...)
3417 if !sum.Equal(New(45, 0)) {
3418 t.Errorf("Failed to calculate sum, expected %s got %s", New(45, 0), sum)
3419 }
3420 }
3421
3422 func TestAvg(t *testing.T) {
3423 vals := make([]Decimal, 10)
3424 var i = int64(0)
3425
3426 for key := range vals {
3427 vals[key] = New(i, 0)
3428 i++
3429 }
3430
3431 avg := Avg(vals[0], vals[1:]...)
3432 if !avg.Equal(NewFromFloat(4.5)) {
3433 t.Errorf("Failed to calculate average, expected %s got %s", NewFromFloat(4.5).String(), avg.String())
3434 }
3435 }
3436
3437 func TestRoundBankAnomaly(t *testing.T) {
3438 a := New(25, -1)
3439 b := New(250, -2)
3440
3441 if !a.Equal(b) {
3442 t.Errorf("Expected %s to equal %s", a, b)
3443 }
3444
3445 expected := New(2, 0)
3446
3447 aRounded := a.RoundBank(0)
3448 if !aRounded.Equal(expected) {
3449 t.Errorf("Expected bank rounding %s to equal %s, but it was %s", a, expected, aRounded)
3450 }
3451
3452 bRounded := b.RoundBank(0)
3453 if !bRounded.Equal(expected) {
3454 t.Errorf("Expected bank rounding %s to equal %s, but it was %s", b, expected, bRounded)
3455 }
3456 }
3457
3458
3459
3460
3461 func TestAtan(t *testing.T) {
3462 inps := []string{
3463 "-2.91919191919191919",
3464 "-1.0",
3465 "-0.25",
3466 "0.0",
3467 "0.33",
3468 "1.0",
3469 "5.0",
3470 "10",
3471 "11000020.2407442310156021090304691671842603586882014729198302312846062338790031898128063403419218957424",
3472 }
3473 sols := []string{
3474 "-1.24076438822058001027437062753106",
3475 "-0.78539816339744833061616997868383",
3476 "-0.24497866312686415",
3477 "0.0",
3478 "0.318747560420644443",
3479 "0.78539816339744833061616997868383",
3480 "1.37340076694501580123233995736766",
3481 "1.47112767430373453123233995736766",
3482 "1.57079623588597296123259450235374",
3483 }
3484 for i, inp := range inps {
3485 d, err := NewFromString(inp)
3486 if err != nil {
3487 t.FailNow()
3488 }
3489 s, err := NewFromString(sols[i])
3490 if err != nil {
3491 t.FailNow()
3492 }
3493 a := d.Atan()
3494 if !a.Equal(s) {
3495 t.Errorf("expected %s, got %s", s, a)
3496 }
3497 }
3498 }
3499
3500
3501 func TestSin(t *testing.T) {
3502 inps := []string{
3503 "-2.91919191919191919",
3504 "-1.0",
3505 "-0.25",
3506 "0.0",
3507 "0.33",
3508 "1.0",
3509 "5.0",
3510 "10",
3511 "11000020.2407442310156021090304691671842603586882014729198302312846062338790031898128063403419218957424",
3512 }
3513 sols := []string{"-0.22057186252002995641471297726318877448242875710373383657841216606788849153474483300147427943530288911869356126149550184271061369789963810497434594683859566879253561990821788142048867910104964466745284318577343435957806286762494529983369776697504436326725441516925396488258485248699247367113416543705253919473126183478178486954138205996912770183192357029798618739277146694040778731661407420114923656224752540889120768",
3514 "-0.841470984807896544828551915928318375739843472469519282898610111931110319333748010828751784005573402229699531838022117989945539661588502120624574802425114599802714611508860519655182175315926637327774878594985045816542706701485174683683726979309922117859910272413672784175028365607893544855897795184024100973080880074046886009375162838756876336134083638363801171409953672944184918309063800980214873465660723218405962257950683415203634506166523593278",
3515 "-0.2474039592545229296662577977006816864013671875",
3516 "0",
3517 "0.3240430283948683457891331120415701894104386268737728",
3518 "0.841470984807896544828551915928318375739843472469519282898610111931110319333748010828751784005573402229699531838022117989945539661588502120624574802425114599802714611508860519655182175315926637327774878594985045816542706701485174683683726979309922117859910272413672784175028365607893544855897795184024100973080880074046886009375162838756876336134083638363801171409953672944184918309063800980214873465660723218405962257950683415203634506166523593278",
3519 "-0.958924274663138409032065951037351417114444405831206421994322505831797734568720303321152847999323782235893449831846516332891972309733806145798957570823292783131379570446989311599459252931842975162373777189193072018951049969744350662993214861042908755303566670204873618202680865638534865944483058650517380292320436016362659617294570185140789829574277032406195741535138712427510938542219940873171248862329526140744770994303733112530324791184417282382",
3520 "-0.54402111088937016772477554483765124109312606762621462357463994520238396180161585438877562935656067241573063207614488370477645194661241525080677431257416988398683714890165970942834453391033857378247849486306346743023618509617104937236345831462093934032592562972419977883837745736210439651143668255744843041350221801750331646628192115694352540293150183983357476391787825596543270240461102629075832777618592034309799936",
3521 "-0.564291758480422881634770440632390475980828840253516895637281099241819037882007239070203007530085741820184955492382572029153491807930868879341091067301689987699567034024159005627332722089169680203292567574310010066799858914647295684974242359142300929248173166551428537696685165964880390889406578530338963341989826231514301546476672476399906348023294571001061677668735117509440368611093448917120819545826797975989350435900286332895885871219875665471968941335407351099209738417818747252638912592184093301853338763294381446907254104878969784040526201729163408095795934201105630182851806342356035203279670146684553491616847294749721014579109870396804713831114709372638323643327823671187472335866664108658093206409882794958673673978956925250261545083579947618620746006004554405785185537391110314728988164693223775249484198058394348289545771967707968288542718255197272633789792059019367104377340604030147471453833808674013259696102003732963091159662478879760121731138091114134586544668859915547568540172541576138084166990547345181184322550297604278946942918844039406876827936831612756344331500301118652183156052728447906384772901595431751550607818380262138322673253023464533931883787069611052589166000316238423939491520880451263927981787175602294299295744",
3522 }
3523 for i, inp := range inps {
3524 d, err := NewFromString(inp)
3525 if err != nil {
3526 t.FailNow()
3527 }
3528 s, err := NewFromString(sols[i])
3529 if err != nil {
3530 t.FailNow()
3531 }
3532 a := d.Sin()
3533 if !a.Equal(s) {
3534 t.Errorf("expected %s, got %s", s, a)
3535 }
3536 }
3537 }
3538
3539
3540 func TestCos(t *testing.T) {
3541 inps := []string{
3542 "-2.91919191919191919",
3543 "-1.0",
3544 "-0.25",
3545 "0.0",
3546 "0.33",
3547 "1.0",
3548 "5.0",
3549 "10",
3550 "11000020.2407442310156021090304691671842603586882014729198302312846062338790031898128063403419218957424",
3551 }
3552 sols := []string{
3553 "-0.975370726167463467746508538219884948528729295145689640359666742268127382748782064668565276308334226452812521220478854320025773591423493734486361306323829818426063430805234608660356853863442937297855742231573288105774823103008774355455799906250461848079705023428527473474556899228935370709945979509634251305018978306493011197513482210179171510947538040406781879762352211326273272515279567525396877609653501706919545667682725671944948392322552266752",
3554 "0.54030230586813965874561515067176071767603141150991567490927772778673118786033739102174242337864109186439207498973007363884202112942385976796862442063752663646870430360736682397798633852405003167527051283327366631405990604840629657123985368031838052877290142895506386796217551784101265975360960112885444847880134909594560331781699767647860744559228420471946006511861233129745921297270844542687374552066388998112901504",
3555 "0.968912421710644784099084544806854121387004852294921875",
3556 "1",
3557 "0.9460423435283869715490383692051286742343482760977712222",
3558 "0.54030230586813965874561515067176071767603141150991567490927772778673118786033739102174242337864109186439207498973007363884202112942385976796862442063752663646870430360736682397798633852405003167527051283327366631405990604840629657123985368031838052877290142895506386796217551784101265975360960112885444847880134909594560331781699767647860744559228420471946006511861233129745921297270844542687374552066388998112901504",
3559 "0.28366218546322646623291670213892426815646045792775066552057877370468842342090306560693620285882975471913545189522117672866861003904575909174769890684057564495184019705963607555427518763375472432216131070235796577209064861003009894615394882021220247535890708789312783718414424224334988974848162884228012265684775651099758365989567444515619764427493598258393280941942356912304265535918025036942025858493361644535438208",
3560 "-0.839071529076452222947082170022504835457755803801719612447629165523199043803440231769716865070163209041973184176293170330332317060558438085478980463542480791358920580076809381102480339018809694514100495572097422057215638383077242523713704127605770444906854175870243452753002238589530499630034663296166308443155999957196346563161387705205277189957388653461251461388391745795979375660087266037741360406956289962327970672363315696841378765492754546688",
3561 "-0.82557544253149396284458404188071504476091346830440347376462206521981928020803354950315062147200396866217255527509254080721982393941347365824137698491042935894213870423296625749297033966815252917361266452901192457318047750698424190124169875103436588397415032138037063155981648677895645409699825582226442363080800781881653440538927704569142007751338851079530521979429507520541625303794665680584709171813053216867014700596866196844144286737568957809383224972108999354839705480223052622003994027222120126949093911643497423100187973906980635670000034664323357488815820848035808846624518774608931622703631130673844138378087837990739103263093532314835289302930152150130664948083902949999427848344301686172490282395687167681679607401220592559832932068966455384902377056623736013617949634746332323529256184776892339963173795176200590119077305668901887229709592836744082027738666294887303249770621722032438202753270710379312736193201366287952361100525126056993039858894987153270630277483613793395809214871734783742285495171911648254647287555645360520115341268930844095156502348405343740866836850201634640011708462641462047870611041595707018966032206807675586825362640000739202116391403514629284000986232673698892843586989003952425039512325844566790376383098534975022847888104706525937115931692008959513984157709954859352131323440787667052399474107219968",
3562 }
3563 for i, inp := range inps {
3564 d, err := NewFromString(inp)
3565 if err != nil {
3566 t.FailNow()
3567 }
3568 s, err := NewFromString(sols[i])
3569 if err != nil {
3570 t.FailNow()
3571 }
3572 a := d.Cos()
3573 if !a.Equal(s) {
3574 t.Errorf("expected %s, got %s", s, a)
3575 }
3576 }
3577 }
3578
3579
3580 func TestTan(t *testing.T) {
3581 inps := []string{
3582 "-2.91919191919191919",
3583 "-1.0",
3584 "-0.25",
3585 "0.0",
3586 "0.33",
3587 "1.0",
3588 "5.0",
3589 "10",
3590 "11000020.2407442310156021090304691671842603586882014729198302312846062338790031898128063403419218957424",
3591 }
3592 sols := []string{
3593 "0.2261415650505790298980791606748881031998682652",
3594 "-1.5574077246549025",
3595 "-0.255341921221036275",
3596 "0.0",
3597 "0.342524867530038963",
3598 "1.5574077246549025",
3599 "-3.3805150062465829",
3600 "0.6483608274590872485524085572681343280321117494",
3601 "0.68351325561491170753499935023939368502774607234006019034769919811202010905597996164029250820702097041244539696",
3602 }
3603 for i, inp := range inps {
3604 d, err := NewFromString(inp)
3605 if err != nil {
3606 t.FailNow()
3607 }
3608 s, err := NewFromString(sols[i])
3609 if err != nil {
3610 t.FailNow()
3611 }
3612 a := d.Tan()
3613 if !a.Equal(s) {
3614 t.Errorf("expected %s, got %s", s, a)
3615 }
3616 }
3617 }
3618
3619 func TestNewNullDecimal(t *testing.T) {
3620 d := NewFromInt(1)
3621 nd := NewNullDecimal(d)
3622
3623 if !nd.Valid {
3624 t.Errorf("expected NullDecimal to be valid")
3625 }
3626 if nd.Decimal != d {
3627 t.Errorf("expected NullDecimal to hold the provided Decimal")
3628 }
3629 }
3630
3631 func ExampleNewFromFloat32() {
3632 fmt.Println(NewFromFloat32(123.123123123123).String())
3633 fmt.Println(NewFromFloat32(.123123123123123).String())
3634 fmt.Println(NewFromFloat32(-1e13).String())
3635
3636
3637
3638
3639 }
3640
3641 func ExampleNewFromFloat() {
3642 fmt.Println(NewFromFloat(123.123123123123).String())
3643 fmt.Println(NewFromFloat(.123123123123123).String())
3644 fmt.Println(NewFromFloat(-1e13).String())
3645
3646
3647
3648
3649 }
3650
View as plain text