forked from shopspring/decimal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
decimal_test.go
3341 lines (3051 loc) · 93 KB
/
decimal_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package decimal
import (
"database/sql/driver"
"encoding/json"
"encoding/xml"
"fmt"
"math"
"math/big"
"math/rand"
"reflect"
"regexp"
"strconv"
"strings"
"testing"
"testing/quick"
"time"
)
type testEnt struct {
float float64
short string
exact string
inexact string
}
var testTable = []*testEnt{
{3.141592653589793, "3.141592653589793", "", "3.14159265358979300000000000000000000000000000000000004"},
{3, "3", "", "3.0000000000000000000000002"},
{1234567890123456, "1234567890123456", "", "1234567890123456.00000000000000002"},
{1234567890123456000, "1234567890123456000", "", "1234567890123456000.0000000000000008"},
{1234.567890123456, "1234.567890123456", "", "1234.5678901234560000000000000009"},
{.1234567890123456, "0.1234567890123456", "", "0.12345678901234560000000000006"},
{0, "0", "", "0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001"},
{.1111111111111110, "0.111111111111111", "", "0.111111111111111000000000000000009"},
{.1111111111111111, "0.1111111111111111", "", "0.111111111111111100000000000000000000023423545644534234"},
{.1111111111111119, "0.1111111111111119", "", "0.111111111111111900000000000000000000000000000000000134123984192834"},
{.000000000000000001, "0.000000000000000001", "", "0.00000000000000000100000000000000000000000000000000012341234"},
{.000000000000000002, "0.000000000000000002", "", "0.0000000000000000020000000000000000000012341234123"},
{.000000000000000003, "0.000000000000000003", "", "0.00000000000000000299999999999999999999999900000000000123412341234"},
{.000000000000000005, "0.000000000000000005", "", "0.00000000000000000500000000000000000023412341234"},
{.000000000000000008, "0.000000000000000008", "", "0.0000000000000000080000000000000000001241234432"},
{.1000000000000001, "0.1000000000000001", "", "0.10000000000000010000000000000012341234"},
{.1000000000000002, "0.1000000000000002", "", "0.10000000000000020000000000001234123412"},
{.1000000000000003, "0.1000000000000003", "", "0.1000000000000003000000000000001234123412"},
{.1000000000000005, "0.1000000000000005", "", "0.1000000000000005000000000000000006441234"},
{.1000000000000008, "0.1000000000000008", "", "0.100000000000000800000000000000000009999999999999999999999999999"},
{1e25, "10000000000000000000000000", "", ""},
{1.5e14, "150000000000000", "", ""},
{1.5e15, "1500000000000000", "", ""},
{1.5e16, "15000000000000000", "", ""},
{1.0001e25, "10001000000000000000000000", "", ""},
{1.0001000000000000033e25, "10001000000000000000000000", "", ""},
{2e25, "20000000000000000000000000", "", ""},
{4e25, "40000000000000000000000000", "", ""},
{8e25, "80000000000000000000000000", "", ""},
{1e250, "10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", "", ""},
{2e250, "20000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", "", ""},
{math.MaxInt64, strconv.FormatFloat(float64(math.MaxInt64), 'f', -1, 64), "", strconv.FormatInt(math.MaxInt64, 10)},
{1.29067116156722e-309, "0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000129067116156722", "", "0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001290671161567218558822290567835270536800098852722416870074139002112543896676308448335063375297788379444685193974290737962187240854947838776604607190387984577130572928111657710645015086812756013489109884753559084166516937690932698276436869274093950997935137476803610007959500457935217950764794724766740819156974617155861568214427828145972181876775307023388139991104942469299524961281641158436752347582767153796914843896176260096039358494077706152272661453132497761307744086665088096215425146090058519888494342944692629602847826300550628670375451325582843627504604013541465361435761965354140678551369499812124085312128659002910905639984075064968459581691226705666561364681985266583563078466180095375402399087817404368974165082030458595596655868575908243656158447265625000000000000000000000000000000000000004440000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"},
// go Issue 29491.
{498484681984085570, "498484681984085570", "", ""},
{5.8339553793802237e+23, "583395537938022370000000", "", ""},
}
var testTableScientificNotation = map[string]string{
"1e9": "1000000000",
"2.41E-3": "0.00241",
"24.2E-4": "0.00242",
"243E-5": "0.00243",
"1e-5": "0.00001",
"245E3": "245000",
"1.2345E-1": "0.12345",
"0e5": "0",
"0e-5": "0",
"0.e0": "0",
".0e0": "0",
"123.456e0": "123.456",
"123.456e2": "12345.6",
"123.456e10": "1234560000000",
}
func init() {
for _, s := range testTable {
s.exact = strconv.FormatFloat(s.float, 'f', 1500, 64)
if strings.ContainsRune(s.exact, '.') {
s.exact = strings.TrimRight(s.exact, "0")
s.exact = strings.TrimRight(s.exact, ".")
}
}
// add negatives
withNeg := testTable[:]
for _, s := range testTable {
if s.float > 0 && s.short != "0" && s.exact != "0" {
withNeg = append(withNeg, &testEnt{-s.float, "-" + s.short, "-" + s.exact, "-" + s.inexact})
}
}
testTable = withNeg
for e, s := range testTableScientificNotation {
if string(e[0]) != "-" && s != "0" {
testTableScientificNotation["-"+e] = "-" + s
}
}
}
func TestNewFromFloat(t *testing.T) {
for _, x := range testTable {
s := x.short
d := NewFromFloat(x.float)
if d.String() != s {
t.Errorf("expected %s, got %s (float: %v) (%s, %d)",
s, d.String(), x.float,
d.value.String(), d.exp)
}
}
shouldPanicOn := []float64{
math.NaN(),
math.Inf(1),
math.Inf(-1),
}
for _, n := range shouldPanicOn {
var d Decimal
if !didPanic(func() { d = NewFromFloat(n) }) {
t.Fatalf("Expected panic when creating a Decimal from %v, got %v instead", n, d.String())
}
}
}
func TestNewFromFloatRandom(t *testing.T) {
n := 0
rng := rand.New(rand.NewSource(0xdead1337))
for {
n++
if n == 10 {
break
}
in := (rng.Float64() - 0.5) * math.MaxFloat64 * 2
want, err := NewFromString(strconv.FormatFloat(in, 'f', -1, 64))
if err != nil {
t.Error(err)
continue
}
got := NewFromFloat(in)
if !want.Equal(got) {
t.Errorf("in: %v, expected %s (%s, %d), got %s (%s, %d) ",
in, want.String(), want.value.String(), want.exp,
got.String(), got.value.String(), got.exp)
}
}
}
func TestNewFromFloatQuick(t *testing.T) {
err := quick.Check(func(f float64) bool {
want, werr := NewFromString(strconv.FormatFloat(f, 'f', -1, 64))
if werr != nil {
return true
}
got := NewFromFloat(f)
return got.Equal(want)
}, &quick.Config{})
if err != nil {
t.Error(err)
}
}
func TestNewFromFloat32Random(t *testing.T) {
n := 0
rng := rand.New(rand.NewSource(0xdead1337))
for {
n++
if n == 10 {
break
}
in := float32((rng.Float64() - 0.5) * math.MaxFloat32 * 2)
want, err := NewFromString(strconv.FormatFloat(float64(in), 'f', -1, 32))
if err != nil {
t.Error(err)
continue
}
got := NewFromFloat32(in)
if !want.Equal(got) {
t.Errorf("in: %v, expected %s (%s, %d), got %s (%s, %d) ",
in, want.String(), want.value.String(), want.exp,
got.String(), got.value.String(), got.exp)
}
}
}
func TestNewFromFloat32Quick(t *testing.T) {
err := quick.Check(func(f float32) bool {
want, werr := NewFromString(strconv.FormatFloat(float64(f), 'f', -1, 32))
if werr != nil {
return true
}
got := NewFromFloat32(f)
return got.Equal(want)
}, &quick.Config{})
if err != nil {
t.Error(err)
}
}
func TestNewFromString(t *testing.T) {
for _, x := range testTable {
s := x.short
d, err := NewFromString(s)
if err != nil {
t.Errorf("error while parsing %s", s)
} else if d.String() != s {
t.Errorf("expected %s, got %s (%s, %d)",
s, d.String(),
d.value.String(), d.exp)
}
}
for _, x := range testTable {
s := x.exact
d, err := NewFromString(s)
if err != nil {
t.Errorf("error while parsing %s", s)
} else if d.String() != s {
t.Errorf("expected %s, got %s (%s, %d)",
s, d.String(),
d.value.String(), d.exp)
}
}
for e, s := range testTableScientificNotation {
d, err := NewFromString(e)
if err != nil {
t.Errorf("error while parsing %s", e)
} else if d.String() != s {
t.Errorf("expected %s, got %s (%s, %d)",
s, d.String(),
d.value.String(), d.exp)
}
}
}
func TestNewFromFormattedString(t *testing.T) {
for _, testCase := range []struct {
Formatted string
Expected string
ReplRegex *regexp.Regexp
}{
{"$10.99", "10.99", regexp.MustCompile("[$]")},
{"$ 12.1", "12.1", regexp.MustCompile("[$\\s]")},
{"$61,690.99", "61690.99", regexp.MustCompile("[$,]")},
{"1_000_000.00", "1000000.00", regexp.MustCompile("[_]")},
{"41,410.00", "41410.00", regexp.MustCompile("[,]")},
{"5200 USD", "5200", regexp.MustCompile("[USD\\s]")},
} {
dFormatted, err := NewFromFormattedString(testCase.Formatted, testCase.ReplRegex)
if err != nil {
t.Fatal(err)
}
dExact, err := NewFromString(testCase.Expected)
if err != nil {
t.Fatal(err)
}
if !dFormatted.Equal(dExact) {
t.Errorf("expect %s, got %s", dExact, dFormatted)
}
}
}
func TestFloat64(t *testing.T) {
for _, x := range testTable {
if x.inexact == "" || x.inexact == "-" {
continue
}
s := x.exact
d, err := NewFromString(s)
if err != nil {
t.Errorf("error while parsing %s", s)
} else if f, exact := d.Float64(); !exact || f != x.float {
t.Errorf("cannot represent exactly %s", s)
}
s = x.inexact
d, err = NewFromString(s)
if err != nil {
t.Errorf("error while parsing %s", s)
} else if f, exact := d.Float64(); exact || f != x.float {
t.Errorf("%s should be represented inexactly", s)
}
}
}
func TestNewFromStringErrs(t *testing.T) {
tests := []string{
"",
"qwert",
"-",
".",
"-.",
".-",
"234-.56",
"234-56",
"2-",
"..",
"2..",
"..2",
".5.2",
"8..2",
"8.1.",
"1e",
"1-e",
"1e9e",
"1ee9",
"1ee",
"1eE",
"1e-",
"1e-.",
"1e1.2",
"123.456e1.3",
"1e-1.2",
"123.456e-1.3",
"123.456Easdf",
"123.456e" + strconv.FormatInt(math.MinInt64, 10),
"123.456e" + strconv.FormatInt(math.MinInt32, 10),
"512.99 USD",
"$99.99",
"51,850.00",
"20_000_000.00",
"$20_000_000.00",
}
for _, s := range tests {
_, err := NewFromString(s)
if err == nil {
t.Errorf("error expected when parsing %s", s)
}
}
}
func TestNewFromStringDeepEquals(t *testing.T) {
type StrCmp struct {
str1 string
str2 string
expected bool
}
tests := []StrCmp{
{"1", "1", true},
{"1.0", "1.0", true},
{"10", "10.0", false},
{"1.1", "1.10", false},
{"1.001", "1.01", false},
}
for _, cmp := range tests {
d1, err1 := NewFromString(cmp.str1)
d2, err2 := NewFromString(cmp.str2)
if err1 != nil || err2 != nil {
t.Errorf("error parsing strings to decimals")
}
if reflect.DeepEqual(d1, d2) != cmp.expected {
t.Errorf("comparison result is different from expected results for %s and %s",
cmp.str1, cmp.str2)
}
}
}
func TestRequireFromString(t *testing.T) {
s := "1.23"
defer func() {
err := recover()
if err != nil {
t.Errorf("error while parsing %s", s)
}
}()
d := RequireFromString(s)
if d.String() != s {
t.Errorf("expected %s, got %s (%s, %d)",
s, d.String(),
d.value.String(), d.exp)
}
}
func TestRequireFromStringErrs(t *testing.T) {
s := "qwert"
var d Decimal
var err interface{}
func(d Decimal) {
defer func() {
err = recover()
}()
RequireFromString(s)
}(d)
if err == nil {
t.Errorf("panic expected when parsing %s", s)
}
}
func TestNewFromFloatWithExponent(t *testing.T) {
type Inp struct {
float float64
exp int32
}
// some tests are taken from here https://www.cockroachlabs.com/blog/rounding-implementations-in-go/
tests := map[Inp]string{
Inp{123.4, -3}: "123.4",
Inp{123.4, -1}: "123.4",
Inp{123.412345, 1}: "120",
Inp{123.412345, 0}: "123",
Inp{123.412345, -5}: "123.41235",
Inp{123.412345, -6}: "123.412345",
Inp{123.412345, -7}: "123.412345",
Inp{123.412345, -28}: "123.4123450000000019599610823207",
Inp{1230000000, 3}: "1230000000",
Inp{123.9999999999999999, -7}: "124",
Inp{123.8989898999999999, -7}: "123.8989899",
Inp{0.49999999999999994, 0}: "0",
Inp{0.5, 0}: "1",
Inp{0., -1000}: "0",
Inp{0.5000000000000001, 0}: "1",
Inp{1.390671161567e-309, 0}: "0",
Inp{4.503599627370497e+15, 0}: "4503599627370497",
Inp{4.503599627370497e+60, 0}: "4503599627370497110902645731364739935039854989106233267453952",
Inp{4.503599627370497e+60, 1}: "4503599627370497110902645731364739935039854989106233267453950",
Inp{4.503599627370497e+60, -1}: "4503599627370497110902645731364739935039854989106233267453952",
Inp{50, 2}: "100",
Inp{49, 2}: "0",
Inp{50, 3}: "0",
// subnormals
Inp{1.390671161567e-309, -2000}: "0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001390671161567000864431395448332752540137009987788957394095829635554502771758698872408926974382819387852542087331897381878220271350970912568035007740861074263206736245957501456549756342151614772544950978154339064833880234531754156635411349342950306987480369774780312897442981323940546749863054846093718407237782253156822124910364044261653195961209878120072488178603782495270845071470243842997312255994555557251870400944414666445871039673491570643357351279578519863428540219295076767898526278029257129758694673164251056158277568765100904638511604478844087596428177947970563689475826736810456067108202083804368114484417399279328807983736233036662284338182105684628835292230438999173947056675615385756827890872955322265625",
Inp{1.390671161567e-309, -862}: "0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000013906711615670008644313954483327525401370099877889573940958296355545027717586988724089269743828193878525420873318973818782202713509709125680350077408610742632067362459575014565497563421516147725449509781543390648338802345317541566354113493429503069874803697747803128974429813239405467498630548460937184072377822531568221249103640442616531959612098781200724881786037824952708450714702438429973122559945555572518704009444146664458710396734915706433573512795785198634285402192950767678985262780292571297586946731642510561582775687651009046385116044788440876",
Inp{1.390671161567e-309, -863}: "0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000013906711615670008644313954483327525401370099877889573940958296355545027717586988724089269743828193878525420873318973818782202713509709125680350077408610742632067362459575014565497563421516147725449509781543390648338802345317541566354113493429503069874803697747803128974429813239405467498630548460937184072377822531568221249103640442616531959612098781200724881786037824952708450714702438429973122559945555572518704009444146664458710396734915706433573512795785198634285402192950767678985262780292571297586946731642510561582775687651009046385116044788440876",
}
// add negatives
for p, s := range tests {
if p.float > 0 {
if s != "0" {
tests[Inp{-p.float, p.exp}] = "-" + s
} else {
tests[Inp{-p.float, p.exp}] = "0"
}
}
}
for input, s := range tests {
d := NewFromFloatWithExponent(input.float, input.exp)
if d.String() != s {
t.Errorf("expected %s, got %s (%s, %d)",
s, d.String(),
d.value.String(), d.exp)
}
}
shouldPanicOn := []float64{
math.NaN(),
math.Inf(1),
math.Inf(-1),
}
for _, n := range shouldPanicOn {
var d Decimal
if !didPanic(func() { d = NewFromFloatWithExponent(n, 0) }) {
t.Fatalf("Expected panic when creating a Decimal from %v, got %v instead", n, d.String())
}
}
}
func TestNewFromInt(t *testing.T) {
tests := map[int64]string{
0: "0",
1: "1",
323412345: "323412345",
9223372036854775807: "9223372036854775807",
}
// add negatives
for p, s := range tests {
if p > 0 {
tests[-p] = "-" + s
}
}
for input, s := range tests {
d := NewFromInt(input)
if d.String() != s {
t.Errorf("expected %s, got %s (%s, %d)",
s, d.String(),
d.value.String(), d.exp)
}
}
}
func TestNewFromInt32(t *testing.T) {
tests := map[int32]string{
0: "0",
1: "1",
323412345: "323412345",
2147483647: "2147483647",
}
// add negatives
for p, s := range tests {
if p > 0 {
tests[-p] = "-" + s
}
}
for input, s := range tests {
d := NewFromInt32(input)
if d.String() != s {
t.Errorf("expected %s, got %s (%s, %d)",
s, d.String(),
d.value.String(), d.exp)
}
}
}
func TestNewFromBigIntWithExponent(t *testing.T) {
type Inp struct {
val *big.Int
exp int32
}
tests := map[Inp]string{
Inp{big.NewInt(123412345), -3}: "123412.345",
Inp{big.NewInt(2234), -1}: "223.4",
Inp{big.NewInt(323412345), 1}: "3234123450",
Inp{big.NewInt(423412345), 0}: "423412345",
Inp{big.NewInt(52341235), -5}: "523.41235",
Inp{big.NewInt(623412345), -6}: "623.412345",
Inp{big.NewInt(723412345), -7}: "72.3412345",
}
// add negatives
for p, s := range tests {
if p.val.Cmp(Zero.value) > 0 {
tests[Inp{p.val.Neg(p.val), p.exp}] = "-" + s
}
}
for input, s := range tests {
d := NewFromBigInt(input.val, input.exp)
if d.String() != s {
t.Errorf("expected %s, got %s (%s, %d)",
s, d.String(),
d.value.String(), d.exp)
}
}
}
func TestCopy(t *testing.T) {
origin := New(1, 0)
cpy := origin.Copy()
if cpy.Cmp(origin) != 0 {
t.Error("expecting copy and origin to be equals, but they are not")
}
//change value
cpy = cpy.Add(New(1, 0))
if cpy.Cmp(origin) == 0 {
t.Error("expecting copy and origin to have different values, but they are equal")
}
}
func TestJSON(t *testing.T) {
for _, x := range testTable {
s := x.short
var doc struct {
Amount Decimal `json:"amount"`
}
docStr := `{"amount":"` + s + `"}`
docStrNumber := `{"amount":` + s + `}`
err := json.Unmarshal([]byte(docStr), &doc)
if err != nil {
t.Errorf("error unmarshaling %s: %v", docStr, err)
} else if doc.Amount.String() != s {
t.Errorf("expected %s, got %s (%s, %d)",
s, doc.Amount.String(),
doc.Amount.value.String(), doc.Amount.exp)
}
out, err := json.Marshal(&doc)
if err != nil {
t.Errorf("error marshaling %+v: %v", doc, err)
} else if string(out) != docStr {
t.Errorf("expected %s, got %s", docStr, string(out))
}
// make sure unquoted marshalling works too
MarshalJSONWithoutQuotes = true
out, err = json.Marshal(&doc)
if err != nil {
t.Errorf("error marshaling %+v: %v", doc, err)
} else if string(out) != docStrNumber {
t.Errorf("expected %s, got %s", docStrNumber, string(out))
}
MarshalJSONWithoutQuotes = false
}
}
func TestUnmarshalJSONNull(t *testing.T) {
var doc struct {
Amount Decimal `json:"amount"`
}
docStr := `{"amount": null}`
err := json.Unmarshal([]byte(docStr), &doc)
if err != nil {
t.Errorf("error unmarshaling %s: %v", docStr, err)
} else if !doc.Amount.Equal(Zero) {
t.Errorf("expected Zero, got %s (%s, %d)",
doc.Amount.String(),
doc.Amount.value.String(), doc.Amount.exp)
}
}
func TestBadJSON(t *testing.T) {
for _, testCase := range []string{
"]o_o[",
"{",
`{"amount":""`,
`{"amount":""}`,
`{"amount":"nope"}`,
`0.333`,
} {
var doc struct {
Amount Decimal `json:"amount"`
}
err := json.Unmarshal([]byte(testCase), &doc)
if err == nil {
t.Errorf("expected error, got %+v", doc)
}
}
}
func TestNullDecimalJSON(t *testing.T) {
for _, x := range testTable {
s := x.short
var doc struct {
Amount NullDecimal `json:"amount"`
}
docStr := `{"amount":"` + s + `"}`
docStrNumber := `{"amount":` + s + `}`
err := json.Unmarshal([]byte(docStr), &doc)
if err != nil {
t.Errorf("error unmarshaling %s: %v", docStr, err)
} else {
if !doc.Amount.Valid {
t.Errorf("expected %s to be valid (not NULL), got Valid = false", s)
}
if doc.Amount.Decimal.String() != s {
t.Errorf("expected %s, got %s (%s, %d)",
s, doc.Amount.Decimal.String(),
doc.Amount.Decimal.value.String(), doc.Amount.Decimal.exp)
}
}
out, err := json.Marshal(&doc)
if err != nil {
t.Errorf("error marshaling %+v: %v", doc, err)
} else if string(out) != docStr {
t.Errorf("expected %s, got %s", docStr, string(out))
}
// make sure unquoted marshalling works too
MarshalJSONWithoutQuotes = true
out, err = json.Marshal(&doc)
if err != nil {
t.Errorf("error marshaling %+v: %v", doc, err)
} else if string(out) != docStrNumber {
t.Errorf("expected %s, got %s", docStrNumber, string(out))
}
MarshalJSONWithoutQuotes = false
}
var doc struct {
Amount NullDecimal `json:"amount"`
}
docStr := `{"amount": null}`
err := json.Unmarshal([]byte(docStr), &doc)
if err != nil {
t.Errorf("error unmarshaling %s: %v", docStr, err)
} else if doc.Amount.Valid {
t.Errorf("expected null value to have Valid = false, got Valid = true and Decimal = %s (%s, %d)",
doc.Amount.Decimal.String(),
doc.Amount.Decimal.value.String(), doc.Amount.Decimal.exp)
}
expected := `{"amount":null}`
out, err := json.Marshal(&doc)
if err != nil {
t.Errorf("error marshaling %+v: %v", doc, err)
} else if string(out) != expected {
t.Errorf("expected %s, got %s", expected, string(out))
}
// make sure unquoted marshalling works too
MarshalJSONWithoutQuotes = true
expectedUnquoted := `{"amount":null}`
out, err = json.Marshal(&doc)
if err != nil {
t.Errorf("error marshaling %+v: %v", doc, err)
} else if string(out) != expectedUnquoted {
t.Errorf("expected %s, got %s", expectedUnquoted, string(out))
}
MarshalJSONWithoutQuotes = false
}
func TestNullDecimalBadJSON(t *testing.T) {
for _, testCase := range []string{
"]o_o[",
"{",
`{"amount":""`,
`{"amount":""}`,
`{"amount":"nope"}`,
`{"amount":nope}`,
`0.333`,
} {
var doc struct {
Amount NullDecimal `json:"amount"`
}
err := json.Unmarshal([]byte(testCase), &doc)
if err == nil {
t.Errorf("expected error, got %+v", doc)
}
}
}
func TestXML(t *testing.T) {
for _, x := range testTable {
s := x.short
var doc struct {
XMLName xml.Name `xml:"account"`
Amount Decimal `xml:"amount"`
}
docStr := `<account><amount>` + s + `</amount></account>`
err := xml.Unmarshal([]byte(docStr), &doc)
if err != nil {
t.Errorf("error unmarshaling %s: %v", docStr, err)
} else if doc.Amount.String() != s {
t.Errorf("expected %s, got %s (%s, %d)",
s, doc.Amount.String(),
doc.Amount.value.String(), doc.Amount.exp)
}
out, err := xml.Marshal(&doc)
if err != nil {
t.Errorf("error marshaling %+v: %v", doc, err)
} else if string(out) != docStr {
t.Errorf("expected %s, got %s", docStr, string(out))
}
}
}
func TestBadXML(t *testing.T) {
for _, testCase := range []string{
"o_o",
"<abc",
"<account><amount>7",
`<html><body></body></html>`,
`<account><amount></amount></account>`,
`<account><amount>nope</amount></account>`,
`0.333`,
} {
var doc struct {
XMLName xml.Name `xml:"account"`
Amount Decimal `xml:"amount"`
}
err := xml.Unmarshal([]byte(testCase), &doc)
if err == nil {
t.Errorf("expected error, got %+v", doc)
}
}
}
func TestNullDecimalXML(t *testing.T) {
// test valid values
for _, x := range testTable {
s := x.short
var doc struct {
XMLName xml.Name `xml:"account"`
Amount NullDecimal `xml:"amount"`
}
docStr := `<account><amount>` + s + `</amount></account>`
err := xml.Unmarshal([]byte(docStr), &doc)
if err != nil {
t.Errorf("error unmarshaling %s: %v", docStr, err)
} else if doc.Amount.Decimal.String() != s {
t.Errorf("expected %s, got %s (%s, %d)",
s, doc.Amount.Decimal.String(),
doc.Amount.Decimal.value.String(), doc.Amount.Decimal.exp)
}
out, err := xml.Marshal(&doc)
if err != nil {
t.Errorf("error marshaling %+v: %v", doc, err)
} else if string(out) != docStr {
t.Errorf("expected %s, got %s", docStr, string(out))
}
}
var doc struct {
XMLName xml.Name `xml:"account"`
Amount NullDecimal `xml:"amount"`
}
// test for XML with empty body
docStr := `<account><amount></amount></account>`
err := xml.Unmarshal([]byte(docStr), &doc)
if err != nil {
t.Errorf("error unmarshaling: %s: %v", docStr, err)
} else if doc.Amount.Valid {
t.Errorf("expected null value to have Valid = false, got Valid = true and Decimal = %s (%s, %d)",
doc.Amount.Decimal.String(),
doc.Amount.Decimal.value.String(), doc.Amount.Decimal.exp)
}
expected := `<account><amount></amount></account>`
out, err := xml.Marshal(&doc)
if err != nil {
t.Errorf("error marshaling %+v: %v", doc, err)
} else if string(out) != expected {
t.Errorf("expected %s, got %s", expected, string(out))
}
// test for empty XML
docStr = `<account></account>`
err = xml.Unmarshal([]byte(docStr), &doc)
if err != nil {
t.Errorf("error unmarshaling: %s: %v", docStr, err)
} else if doc.Amount.Valid {
t.Errorf("expected null value to have Valid = false, got Valid = true and Decimal = %s (%s, %d)",
doc.Amount.Decimal.String(),
doc.Amount.Decimal.value.String(), doc.Amount.Decimal.exp)
}
expected = `<account><amount></amount></account>`
out, err = xml.Marshal(&doc)
if err != nil {
t.Errorf("error marshaling %+v: %v", doc, err)
} else if string(out) != expected {
t.Errorf("expected %s, got %s", expected, string(out))
}
}
func TestNullDecimalBadXML(t *testing.T) {
for _, testCase := range []string{
"o_o",
"<abc",
"<account><amount>7",
`<html><body></body></html>`,
`<account><amount>nope</amount></account>`,
`0.333`,
} {
var doc struct {
XMLName xml.Name `xml:"account"`
Amount NullDecimal `xml:"amount"`
}
err := xml.Unmarshal([]byte(testCase), &doc)
if err == nil {
t.Errorf("expected error, got %+v", doc)
}
}
}
func TestDecimal_rescale(t *testing.T) {
type Inp struct {
int int64
exp int32
rescale int32
}
tests := map[Inp]string{
Inp{1234, -3, -5}: "1.234",
Inp{1234, -3, 0}: "1",
Inp{1234, 3, 0}: "1234000",
Inp{1234, -4, -4}: "0.1234",
}
// add negatives
for p, s := range tests {
if p.int > 0 {
tests[Inp{-p.int, p.exp, p.rescale}] = "-" + s
}
}
for input, s := range tests {
d := New(input.int, input.exp).rescale(input.rescale)
if d.String() != s {
t.Errorf("expected %s, got %s (%s, %d)",
s, d.String(),
d.value.String(), d.exp)
}
// test StringScaled
s2 := New(input.int, input.exp).StringScaled(input.rescale)
if s2 != s {
t.Errorf("expected %s, got %s", s, s2)
}
}
}
func TestDecimal_Floor(t *testing.T) {
assertFloor := func(input, expected Decimal) {
got := input.Floor()
if !got.Equal(expected) {
t.Errorf("Floor(%s): got %s, expected %s", input, got, expected)
}
}
type testDataString struct {
input string
expected string
}
testsWithStrings := []testDataString{
{"1.999", "1"},
{"1", "1"},
{"1.01", "1"},
{"0", "0"},
{"0.9", "0"},
{"0.1", "0"},
{"-0.9", "-1"},
{"-0.1", "-1"},
{"-1.00", "-1"},
{"-1.01", "-2"},
{"-1.999", "-2"},
}
for _, test := range testsWithStrings {
expected, _ := NewFromString(test.expected)
input, _ := NewFromString(test.input)
assertFloor(input, expected)
}
type testDataDecimal struct {
input Decimal
expected string
}
testsWithDecimals := []testDataDecimal{
{New(100, -1), "10"},
{New(10, 0), "10"},
{New(1, 1), "10"},
{New(1999, -3), "1"},
{New(101, -2), "1"},
{New(1, 0), "1"},
{New(0, 0), "0"},
{New(9, -1), "0"},
{New(1, -1), "0"},
{New(-1, -1), "-1"},
{New(-9, -1), "-1"},
{New(-1, 0), "-1"},
{New(-101, -2), "-2"},
{New(-1999, -3), "-2"},
}
for _, test := range testsWithDecimals {
expected, _ := NewFromString(test.expected)
assertFloor(test.input, expected)
}
}
func TestDecimal_Ceil(t *testing.T) {
assertCeil := func(input, expected Decimal) {
got := input.Ceil()
if !got.Equal(expected) {
t.Errorf("Ceil(%s): got %s, expected %s", input, got, expected)
}
}
type testDataString struct {
input string
expected string
}
testsWithStrings := []testDataString{
{"1.999", "2"},
{"1", "1"},
{"1.01", "2"},
{"0", "0"},
{"0.9", "1"},
{"0.1", "1"},
{"-0.9", "0"},
{"-0.1", "0"},
{"-1.00", "-1"},
{"-1.01", "-1"},
{"-1.999", "-1"},
}
for _, test := range testsWithStrings {
expected, _ := NewFromString(test.expected)
input, _ := NewFromString(test.input)
assertCeil(input, expected)
}
type testDataDecimal struct {
input Decimal
expected string