-
Notifications
You must be signed in to change notification settings - Fork 2
/
currency_test.go
1485 lines (1437 loc) · 40.7 KB
/
currency_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
// -----------------------------------------------------------------------------
// ZR Library zr/[currency_test.go]
// (c) balarabe@protonmail.com License: MIT
// -----------------------------------------------------------------------------
package zr
// # Currency Factory:
// Test_crcy_CurrencyOf_
//
// # String Output:
// Test_crcy_Currency_Fmt_
// Test_crcy_Currency_InWordsEN_
// Test_crcy_Currency_String_
//
// # Division:
// Test_crcy_Currency_Div_
// Test_crcy_Currency_DivFloat_
// Test_crcy_Currency_DivInt_
//
// # Multiplication:
// Test_crcy_Currency_Mul_
// Test_crcy_Currency_MulFloat_
// Test_crcy_Currency_MulInt_
//
// # Addition:
// Test_crcy_Currency_Add_
// Test_crcy_Currency_AddFloat_
// Test_crcy_Currency_AddInt_
//
// # Subtraction:
// Test_crcy_Currency_Sub_
// Test_crcy_Currency_SubFloat_
// Test_crcy_Currency_SubInt_
//
// # Information:
// Test_crcy_Currency_Float64_
// Test_crcy_Currency_Int_
// Test_crcy_Currency_Int64_
// Test_crcy_Currency_IsEqual_
// Test_crcy_Currency_IsNegative_
// Test_crcy_Currency_IsZero_
// Test_crcy_Currency_Overflow_
//
// # JSON:
// Test_crcy_Currency_MarshalJSON_
// Test_crcy_Currency_UnmarshalJSON_
//
// # Helper Functions
// Test_crcy_currencyOverflow_
//
// # Test Helper Functions
// arC(ar ...interface{}) (ret []Currency)
// arF(ar ...interface{}) (ret []float64)
// arI(ar ...interface{}) (ret []int)
// cur = CurrencyOf
// curFloatOpTest(
// t *testing.T,
// opName string,
// opFunc func(values ...float64) Currency,
// n Currency,
// values []float64,
// expect Currency,
// expectErrors int,
// )
// curIntOpTest(
// t *testing.T,
// opName string,
// opFunc func(values ...int) Currency,
// n Currency,
// values []int,
// expect Currency,
// expectErrors int,
// )
// curOpTest(
// t *testing.T,
// opName string,
// opFunc func(values ...Currency) Currency,
// n Currency,
// values []Currency,
// expect Currency,
// expectErrors int,
// )
// to test all items in currency.go use:
// go test --run Test_crcy_
//
// to generate a test coverage report for the whole module use:
// go test -coverprofile cover.out
// go tool cover -html=cover.out
import (
"encoding/json"
"fmt"
"math"
"strings"
"testing"
"time"
)
// -----------------------------------------------------------------------------
// # Currency Factory:
// go test --run Test_crcy_CurrencyOf_
func Test_crcy_CurrencyOf_(t *testing.T) {
TBegin(t)
//
// CurrencyOf(value interface{}) Currency
//
test := func(input interface{}, expect Currency) {
got := CurrencyOf(input)
if got.i64 != expect.i64 {
TFailf(t, `CurrencyOf(%v) returned %v instead of %v`,
input, got, expect)
}
}
// highest/lowest possible currency value
test(int64(922337203685476), Currency{922337203685476 * 1e4})
test(int64(-922337203685476), Currency{-922337203685476 * 1e4})
//
// strings
test("", Currency{0})
test(" ", Currency{0})
test("1.01", Currency{1.01 * 1e4})
test("922337203685476", Currency{922337203685476 * 1e4})
test("-922337203685476", Currency{-922337203685476 * 1e4})
//
// bool
test(false, Currency{0})
test(true, Currency{1 * 1e4})
//
// integers
test(int(-123456), Currency{-(123456 * 1e4)})
test(int8(math.MaxInt8), Currency{127 * 1e4})
test(int8(math.MinInt8), Currency{-128 * 1e4})
test(int16(math.MaxInt16), Currency{32767 * 1e4})
test(int16(math.MinInt16), Currency{-32768 * 1e4})
test(int32(math.MaxInt32), Currency{2147483647 * 1e4})
test(int32(math.MinInt32), Currency{-2147483648 * 1e4})
// TODO: test with acceptable limit for int64
test(int64(math.MaxInt32), Currency{2147483647 * 1e4})
test(int64(math.MinInt32), Currency{-2147483648 * 1e4})
//
// unsigned integers
test(uint(123456), Currency{123456 * 1e4})
test(uint8(0), Currency{0})
test(uint8(math.MaxUint8), Currency{255 * 1e4})
test(uint16(0), Currency{0})
test(uint16(math.MaxUint16), Currency{65535 * 1e4})
test(uint32(0), Currency{0})
test(uint32(math.MaxUint32), Currency{4294967295 * 1e4})
test(uint64(0), Currency{0})
test(uint64(922337203685476), Currency{922337203685476 * 1e4})
//
// floating-point numbers:
//
test(float32(0), Currency{0})
test(float64(0), Currency{0})
//
test(float32(0.1), Currency{1000})
//
test(float32(-0.1), Currency{-1000})
//
test(float64(0.00001), Currency{0})
test(float64(0.0001), Currency{1})
test(float64(0.001), Currency{10})
test(float64(0.01), Currency{100})
test(float64(0.1), Currency{1000})
//
test(float64(-0.00001), Currency{0})
test(float64(-0.0001), Currency{-1})
test(float64(-0.001), Currency{-10})
test(float64(-0.01), Currency{-100})
test(float64(-0.1), Currency{-1000})
//
test(float32(1000), Currency{1000 * 1e4})
test(float32(1234.5), Currency{1234.5 * 1e4})
test(float64(1000), Currency{1000 * 1e4})
test(float64(12345.6789), Currency{12345.6789 * 1e4})
//
// integer pointers
{
num := int(-123456)
test(&num, Currency{-(123456 * 1e4)})
}
{
num := int8(math.MaxInt8)
test(&num, Currency{127 * 1e4})
}
{
num := int8(math.MinInt8)
test(&num, Currency{-128 * 1e4})
}
{
num := int16(math.MaxInt16)
test(&num, Currency{32767 * 1e4})
}
{
num := int16(math.MinInt16)
test(&num, Currency{-32768 * 1e4})
}
{
num := int32(math.MaxInt32)
test(&num, Currency{2147483647 * 1e4})
}
{
num := int32(math.MinInt32)
test(&num, Currency{-2147483648 * 1e4})
}
{
num := int64(922337203685476)
test(&num, Currency{922337203685476 * 1e4})
}
{
num := int64(-922337203685476)
test(&num, Currency{-922337203685476 * 1e4})
}
// unsigned integer pointers
{
num := uint(123456)
test(&num, Currency{123456 * 1e4})
}
{
num := uint8(math.MaxUint8)
test(&num, Currency{255 * 1e4})
}
{
num := uint8(0)
test(&num, Currency{0})
}
{
num := uint16(math.MaxUint16)
test(&num, Currency{65535 * 1e4})
}
{
num := uint16(0)
test(&num, Currency{0})
}
{
num := uint32(math.MaxUint32)
test(&num, Currency{4294967295 * 1e4})
}
{
num := uint32(0)
test(&num, Currency{0})
}
// pointers to floating-point numbers
{
num := float32(1000)
test(&num, Currency{1000 * 1e4})
}
{
num := float32(1234.5)
test(&num, Currency{1234.5 * 1e4})
}
{
num := float64(1000)
test(&num, Currency{1000 * 1e4})
}
{
num := float64(12345.6789)
test(&num, Currency{12345.6789 * 1e4})
}
// pointer to string
{
s := "922337203685476"
test(&s, Currency{922337203685476 * 1e4})
}
{
s := "-922337203685476"
test(&s, Currency{-922337203685476 * 1e4})
}
// test conditions that generate errors:
DisableErrors()
// non-numeric string
{
ec1 := GetErrorCount()
test("abc", Currency{0})
ec2 := GetErrorCount()
if ec2 != ec1+1 {
TFail(t, `Expected 1 error, but got `, ec2-ec1)
}
}
// wrong type
{
ec1 := GetErrorCount()
test(time.Now(), Currency{0})
ec2 := GetErrorCount()
if ec2 != ec1+1 {
TFail(t, `Expected 1 error, but got `, ec2-ec1)
}
}
// overflow
{
ec1 := GetErrorCount()
//
// large floating-point numbers
test(float32(-10e20), Currency{math.MinInt64})
test(float32(-10e20), Currency{math.MinInt64})
test(float64(10e20), Currency{math.MaxInt64})
test(float64(10e20), Currency{math.MaxInt64})
//
// max int64
test(int64(math.MaxInt64), Currency{math.MaxInt64})
{
num := int64(math.MaxInt64)
test(&num, Currency{math.MaxInt64})
}
// min int64
test(int64(math.MinInt64), Currency{math.MinInt64})
{
num := int64(math.MinInt64)
test(&num, Currency{math.MinInt64})
}
// max uint64
test(uint64(math.MaxUint64), Currency{math.MaxInt64})
{
num := uint64(math.MaxUint64)
test(&num, Currency{math.MaxInt64})
}
ec2 := GetErrorCount()
if ec2 != ec1+10 {
TFail(t, `Expected 10 errors, but got `, ec2-ec1)
}
}
EnableErrors()
} // Test_crcy_CurrencyOf_
// -----------------------------------------------------------------------------
// # String Output:
// go test --run Test_crcy_Currency_Fmt_
func Test_crcy_Currency_Fmt_(t *testing.T) {
TBegin(t)
//
// (n Currency) Fmt(decimalPlaces int) string
//
test := func(input interface{}, decimalPlaces int, expect string) {
num := cur(input)
got := num.Fmt(decimalPlaces)
if got != expect {
TFailf(t, `Currency(%s).Fmt(%v) returned %q instead of %q`,
num, decimalPlaces, got, expect)
}
}
// variable decimal places
test("1234.00000", -1, "1,234")
test("1234.50000", -1, "1,234.5")
test("1234.56000", -1, "1,234.56")
test("1234.56700", -1, "1,234.567")
test("1234.56780", -1, "1,234.5678")
test("1234.56789", -1, "1,234.5678")
//
// more decimals than Currency's precision
test("1234.56789", 4, "1,234.5678")
test("1234.56780", 4, "1,234.5678")
test("1234.56", 2, "1,234.56")
test("1234.00", 2, "1,234.00")
test("1234.0000", 2, "1,234.00")
test("1234.5678", 0, "1,234")
test("1234.5678", 1, "1,234.5")
test("1234.5678", 2, "1,234.56")
test("1234.5678", 3, "1,234.567")
test("1234.5678", 4, "1,234.5678")
test("1234.56789", 5, "1,234.56780")
//
// 0
test("", 0, "0")
//
// 1
test(-1, 0, "-1")
test(-9, 0, "-9")
//
// 2
test(-10, 0, "-10")
test(-12, 0, "-12")
test(-99, 0, "-99")
//
// 3
test(-100, 0, "-100")
test(-201, 0, "-201")
test(-999, 0, "-999")
test(-123, 0, "-123")
//
// 4
test(-1000, 0, "-1,000")
test(-1001, 0, "-1,001")
test(-9999, 0, "-9,999")
test(-1234, 0, "-1,234")
//
// 1
test(1, 0, "1")
test(9, 0, "9")
//
// 2
test(10, 0, "10")
test(12, 0, "12")
test(99, 0, "99")
//
// 3
test(100, 0, "100")
test(201, 0, "201")
test(999, 0, "999")
test(123, 0, "123")
//
// 4
test(1000, 0, "1,000")
test(1001, 0, "1,001")
test(9999, 0, "9,999")
test(1234, 0, "1,234")
//
// large numbers
test("22337203685477", 0, "22,337,203,685,477")
test("100000000000000", 0, "100,000,000,000,000")
test("900000000000000", 0, "900,000,000,000,000")
test("99999999999999", 0, "99,999,999,999,999")
test("922337203685477", 0, "922,337,203,685,477")
test("-922337203685477", 0, "-922,337,203,685,477")
} // Test_crcy_Currency_Fmt_
// go test --run Test_crcy_Currency_InWordsEN_
func Test_crcy_Currency_InWordsEN_(t *testing.T) {
// (n Currency) InWordsEN(fmt string) string
test := func(n Currency, fmt, expect string) {
got := n.InWordsEN(fmt)
if got != expect {
TFail(t,
`Currency(`+n.String()+`).Fmt("`+fmt+`")`+
` returned "`+got+`". must be "`+expect+`"`,
)
}
}
test(cur(int64(9000000000000)), "Unit", "Nine Trillion Units")
// TODO: test(int64(math.MaxInt64), "Unit", "")
// TODO: FAIL test(0, "Unit", "Zero")
// TODO: FAIL test(1, "", "One")
// TODO: ADD TEST FOR UNIT NAMES
// TODO: ADD TEST FOR GROUP NAMES
//
test(cur(1), "Unit", "One Unit")
test(cur(2), "Unit", "Two Units")
//
test(cur(11.02), ";;Cent;Only", "Two Cents Only")
test(cur(11.02), "Dollar;;Cent", "Eleven Dollars and Two Cents")
test(cur(11.02), "Euro", "Eleven Euros")
test(cur(11.02), "Pound;;;Pence", "Eleven Pounds and Two Pence")
//
test(cur(-987), "Unit", "Nine Hundred and Eighty Seven Units")
//
test(
cur(123.456),
";Units;;Units",
"One Hundred and Twenty Three Units and Forty Five Units",
)
test(
cur(123.456),
"Large-Single;Large-Plural;Small-Single;Small-Plural",
"One Hundred and Twenty Three Large-Plural"+
" and Forty Five Small-Plural",
)
test(
cur(1.10),
"Large-Single;Large-Plural;Small-Single;Small-Plural",
"One Large-Single and Ten Small-Plural",
)
test(
cur(1.01),
"Large-Single;Large-Plural;Small-Single;Small-Plural",
"One Large-Single and One Small-Single",
)
test(
cur(1234567890),
"Unit",
"One Billion"+
" Two Hundred and Thirty Four Million"+
" Five Hundred and Sixty Seven Thousand"+
" Eight Hundred and Ninety Units",
)
test(cur(int64(9000000000)), "Unit", "Nine Billion Units")
//FAIL test(cur(9000000000000), "Unit", "Nine Trillion Units")
} // Test_crcy_Currency_InWordsEN_
// go test --run Test_crcy_Currency_String_
func Test_crcy_Currency_String_(t *testing.T) {
TBegin(t)
//
// (n Currency) String() string
//
for i, test := range []struct {
input Currency
expect string
}{
// zero
{Currency{0}, "0"},
//
// numbers with zero int part and some decimals
{Currency{9999}, "0.9999"},
{Currency{1000}, "0.1"},
{Currency{100}, "0.01"},
{Currency{10}, "0.001"},
{Currency{1}, "0.0001"},
{Currency{-1}, "-0.0001"},
{Currency{-10}, "-0.001"},
{Currency{-100}, "-0.01"},
{Currency{-1000}, "-0.1"},
{Currency{-9999}, "-0.9999"},
//
// numbers with both int part and decimals
{Currency{-10100}, "-1.01"},
{Currency{10100}, "1.01"},
{Currency{-123456789}, "-12345.6789"},
{Currency{123456789}, "12345.6789"},
{Currency{-2000000000000000001}, "-200000000000000.0001"},
{Currency{2000000000000000001}, "200000000000000.0001"},
//
// smallest and largest currency values:
{Currency{-9223372036854769999}, "-922337203685476.9999"},
{Currency{9223372036854769999}, "922337203685476.9999"},
//
// these are overflown values, but String() must print then anyway
{Currency{math.MinInt64}, "-922337203685477.5808"},
{Currency{math.MaxInt64}, "922337203685477.5807"},
} {
var (
input = test.input
init = input
got = input.String()
)
if got != test.expect {
TFail(t,
`#`, i, ` (`, input, `).String()`,
` returned "`, got, `". must be "`, test.expect, `"`,
)
}
if input.i64 != init.i64 {
TFail(t, ` mutated from `, init, ` to `, input)
}
}
} // Test_crcy_Currency_String_
// -----------------------------------------------------------------------------
// # Division:
// go test --run Test_crcy_Currency_Div_
func Test_crcy_Currency_Div_(t *testing.T) {
TBegin(t)
//
// (n Currency) Div(nums... Currency) Currency
//
test := func(n Currency, nums []Currency, expect Currency) {
init := n
for _, num := range nums {
old := n
got := n.Div(num)
// object of invoked method must not change
if n.i64 != old.i64 {
TFail(t, `(`, init, `) mutated from `, old, ` to `, n)
}
n = got
}
if n.i64 != expect.i64 {
TFail(t,
`(`, init, `).Div(`, nums, `)`,
` returned `, n, `. must be `, expect,
)
}
}
// 12345.6789 / 1.0 = 12345.6789
test(Currency{123456789}, arC(Currency{10000}), Currency{123456789})
} // Test_crcy_Currency_Div_
// go test --run Test_crcy_Currency_DivFloat_
func Test_crcy_Currency_DivFloat_(t *testing.T) {
TBegin(t)
//
// (n Currency) DivFloat(nums...float64) Currency
//
test := func(n Currency, nums []float64, expect Currency) {
curFloatOpTest(t, "DivFloat", n.DivFloat, n, nums, expect, 0)
}
// 12345.6789 / 1.0 = 12345.6789
test(Currency{123456789}, arF(1.0), Currency{123456789})
//
// 12345.6789 / 2.0 = 6172.8394
test(Currency{123456789}, arF(2.0), Currency{61728394})
} // Test_crcy_Currency_DivFloat_
// go test --run Test_crcy_Currency_DivInt_
func Test_crcy_Currency_DivInt_(t *testing.T) {
// (n Currency) DivInt(nums...int) Currency
test := func(n Currency, nums []int, expect Currency) {
curIntOpTest(t, "DivInt", n.DivInt, n, nums, expect, 0)
}
TBegin(t)
//
// TODO: declaration comment
//
// 12345.6789 / 1 = 12345.6789
test(Currency{123456789}, arI(1), Currency{123456789})
//
// 12345.6789 / 2 = 6172.8394
test(Currency{123456789}, arI(2), Currency{61728394})
} // Test_crcy_Currency_DivInt_
// -----------------------------------------------------------------------------
// # Multiplication:
// go test --run Test_crcy_Currency_Mul_
func Test_crcy_Currency_Mul_(t *testing.T) {
TBegin(t)
//
// (n Currency) Mul(nums... Currency) Currency
//
test := func(n Currency, nums []Currency, expect Currency) {
curOpTest(t, "Mul", n.Mul, n, nums, expect, 0)
}
test(cur(0), arC(0), cur(0))
test(cur(1), arC(1), cur(1))
test(cur(1), arC(1), cur(1))
test(cur(2), arC(2), cur(4))
test(cur(-2), arC(2), cur(-4))
test(cur(2), arC(-2), cur(-4))
test(cur(1000), arC(1000), cur(1000000))
//
// these would overflow if big.Int is not used
test(Currency{4611686018427384999}, arC(2), Currency{9223372036854769998})
test(Currency{922337203685476999}, arC(10), Currency{9223372036854769990})
//
// tests that cause overflow
DisableErrors()
test = func(n Currency, nums []Currency, expect Currency) {
curOpTest(t, "Mul", n.Mul, n, nums, expect, 1)
}
//
// overflow: the result fits in int64, but not in acceptable Currency range
test(Currency{MaxCurrencyI64 + 1}, arC(1), Currency{math.MaxInt64})
//
test(cur(int64(-627199999000000)), arC(int64(627199999000000)),
Currency{math.MinInt64},
)
test(cur(int64(-922337203685476)), arC(int64(922337203685476)),
Currency{math.MinInt64},
)
test(cur(int64(627199999000000)), arC(int64(627199999000000)),
Currency{math.MaxInt64},
)
test(cur(int64(922337203685476)), arC(int64(922337203685476)),
Currency{math.MaxInt64},
)
EnableErrors()
} // Test_crcy_Currency_Mul_
// go test --run Test_crcy_Currency_MulFloat_
func Test_crcy_Currency_MulFloat_(t *testing.T) {
TBegin(t)
//
// (n Currency) MulFloat(nums...float64) Currency
//
test := func(n Currency, nums []float64, expect Currency) {
curFloatOpTest(t, "MulFloat", n.MulFloat, n, nums, expect, 0)
}
// 0 * 1.0 = 0
test(Currency{0}, arF(1.0), Currency{0})
//
// 2 * 2.0 = 4
test(Currency{2 * 1e4}, arF(2.0), Currency{4 * 1e4})
//
// 12345.6789 * 1 = 12345.6789
test(Currency{123456789}, arF(1), Currency{123456789})
//
// 12345.6789 / 2 = 24691.3578
test(Currency{123456789}, arF(2), Currency{246913578})
//
test(cur(-1), arF(1e6), cur(-1000000)) // -1 million
test(cur(-1), arF(1e7), cur(-10000000)) // -10 million
test(cur(-1), arF(1e8), cur(-100000000)) // -100 million
test(cur(-1), arF(1e9), cur(-1000000000)) // -1 billion
test(cur(-1), arF(1e10), cur(int64(-10000000000))) // -10 billion
//
test(cur(1), arF(1e6), cur(1000000)) // 1 million
test(cur(1), arF(1e7), cur(10000000)) // 10 million
test(cur(1), arF(1e8), cur(100000000)) // 100 million
test(cur(1), arF(1e9), cur(1000000000)) // 1 billion
test(cur(1), arF(1e10), cur(int64(10000000000))) // 10 billion
//
// Here, if Mul() didn't use big.Int, multiplication would overflow
test(cur(1), arF(1e14), Currency{int64(1e18)})
test(cur(-1), arF(1e14), Currency{int64(-1e18)})
test(cur(123), arF(1e9), Currency{int64(123 * 1e13)}) // 123 billion
//
// overflow
DisableErrors()
test = func(n Currency, nums []float64, expect Currency) {
curFloatOpTest(t, "MulFloat", n.MulFloat, n, nums, expect, 1)
}
test(cur(1), arF(1e20), Currency{math.MaxInt64})
test(cur(123), arF(float64(MaxCurrencyI64)), Currency{math.MaxInt64})
test(cur(123), arF(MinCurrencyI64-0.001), Currency{math.MinInt64})
EnableErrors()
} // Test_crcy_Currency_MulFloat_
// go test --run Test_crcy_Currency_MulInt_
func Test_crcy_Currency_MulInt_(t *testing.T) {
// (n Currency) MulInt(nums...int) Currency
test := func(n Currency, nums []int, expect Currency) {
curIntOpTest(t, "MulInt", n.MulInt, n, nums, expect, 0)
}
// 0 * 1 = 0
test(Currency{0}, arI(1), Currency{0})
//
// 2 * 2 = 4
test(Currency{2 * 1e4}, arI(2), Currency{4 * 1e4})
//
// 12345.6789 * 1 = 12345.6789
test(Currency{123456789}, arI(1), Currency{123456789})
//
// 12345.6789 / 2 = 24691.3578
test(Currency{123456789}, arI(2), Currency{246913578})
//
// TODO: Try to cause failure in unit test:
// pass MaxInt64 * just above currency limit.
} // Test_crcy_Currency_MulInt_
// -----------------------------------------------------------------------------
// # Addition:
// go test --run Test_crcy_Currency_Add_
func Test_crcy_Currency_Add_(t *testing.T) {
TBegin(t)
//
// (n Currency) Add(nums... Currency) Currency
//
test := func(n Currency, nums []Currency, expect Currency) {
curOpTest(t, "Add", n.Add, n, nums, expect, 0)
}
// test additions within range of Currency:
test(cur(0), arC(0), cur(0))
test(cur(1), arC(1), cur(2))
test(cur(1000), arC(1000), cur(2000))
test(cur(-1), arC(-1), cur(-2))
//
// multiple additions:
test(cur(1), arC(1.1, 2.02, 3.003, 4.0004), cur(11.1234))
//
// test addition close to the limits of Currency:
//
// largest + smallest number: must 'annihilate' to zero
test(
Currency{MinCurrencyI64}, // -9223372036854769999
arC(Currency{MaxCurrencyI64}), // 9223372036854769999
Currency{0},
)
test(
Currency{MaxCurrencyI64}, // 9223372036854769999
arC(Currency{MinCurrencyI64}), // -9223372036854769999
Currency{0},
)
// -922337203685476 + -0.9999 = -922337203685476.9999
test(
Currency{-9223372036854760000},
arC(Currency{-9999}),
Currency{MinCurrencyI64},
)
test(
Currency{-9999},
arC(Currency{-9223372036854760000}),
Currency{MinCurrencyI64},
)
// -922337203685476.9998 + -0.0001 = -922337203685476.9999
test(
Currency{-9223372036854769998},
arC(Currency{-1}),
Currency{MinCurrencyI64},
)
test(
Currency{-1},
arC(Currency{-9223372036854769998}),
Currency{MinCurrencyI64},
)
// 922337203685476 + 0.9999 = 922337203685476.9999
test(
Currency{9223372036854760000},
arC(Currency{9999}),
Currency{MaxCurrencyI64},
)
test(
Currency{9999},
arC(Currency{9223372036854760000}),
Currency{MaxCurrencyI64},
)
// 922337203685476.9998 + 0.0001 = 922337203685476.9999
test(
Currency{9223372036854769998},
arC(Currency{1}),
Currency{MaxCurrencyI64},
)
test(
Currency{1},
arC(Currency{9223372036854769998}),
Currency{MaxCurrencyI64},
)
// test additions that will overflow Currency:
DisableErrors()
test = func(n Currency, nums []Currency, expect Currency) {
curOpTest(t, "Add", n.Add, n, nums, expect, 1)
}
// adding -0.0001 to the smallest number must overflow
test(
Currency{MinCurrencyI64}, // -922337203685476.9999
arC(Currency{-1}), // -0.0001
Currency{math.MinInt64},
)
// adding 0.0001 to the largest number must overflow
test(
Currency{MaxCurrencyI64}, // 922337203685476.9999
arC(Currency{1}), // 0.0001
Currency{math.MaxInt64},
)
// adding two smallest numbers must overflow
test(
Currency{MinCurrencyI64}, // -922337203685476.9999
arC(Currency{MinCurrencyI64}), // -922337203685476.9999
Currency{math.MinInt64},
)
// adding two largest numbers must overflow
test(
Currency{MaxCurrencyI64}, // 922337203685476.9999
arC(Currency{MaxCurrencyI64}), // 922337203685476.9999
Currency{math.MaxInt64},
)
EnableErrors()
} // Test_crcy_Currency_Add_
// go test --run Test_crcy_Currency_AddFloat_
func Test_crcy_Currency_AddFloat_(t *testing.T) {
TBegin(t)
//
// (n Currency) AddFloat(nums...float64) Currency
//
test := func(n Currency, nums []float64, expect Currency) {
curFloatOpTest(t, "AddFloat", n.AddFloat, n, nums, expect, 0)
}
test(cur(0), arF(0), cur(0))
test(cur(1), arF(1), cur(2))
//
// test multiple arguments
test(cur(1), arF(2, 3, 4), cur(10))
//
// must overflow
DisableErrors()
test = func(n Currency, nums []float64, expect Currency) {
curFloatOpTest(t, "AddFloat", n.AddFloat, n, nums, expect, 1)
}
test(cur(1), arF(1e30), Currency{math.MaxInt64})
EnableErrors()
} // Test_crcy_Currency_AddFloat_
// go test --run Test_crcy_Currency_AddInt_
func Test_crcy_Currency_AddInt_(t *testing.T) {
// (n Currency) AddInt(nums...int) Currency
test := func(n Currency, nums []int, expect Currency) {
curIntOpTest(t, "AddInt", n.AddInt, n, nums, expect, 0)
}
test(cur(0), arI(0), cur(0))
} // Test_crcy_Currency_AddInt_
// -----------------------------------------------------------------------------
// # Subtraction:
// go test --run Test_crcy_Currency_Sub_
func Test_crcy_Currency_Sub_(t *testing.T) {
TBegin(t)
//
// (n Currency) Sub(nums... Currency) Currency
//
test := func(n Currency, nums []Currency, expect Currency) {
curOpTest(t, "Sub", n.Sub, n, nums, expect, 0)
}
//
// test subtractions within range of Currency:
//
test(cur(0), arC(0), cur(0))
test(cur(0.0001), arC(0.0001), cur(0))
test(cur(0), arC(0.0001), cur(-0.0001))
test(cur(1), arC(1), cur(0))
test(cur(10), arC(1, 2, 3), cur(4))
test(cur(int64(1234567891234)), arC(67891234), cur(int64(1234500000000)))
test(cur(2), arC(1), cur(1))
test(cur(2000), arC(1000), cur(1000))
test(cur(-2), arC(-1), cur(-1))
//
// multiple subtractions:
test(cur(11.1234), arC(4.0004, 3.003, 2.02, 1.1), cur(1))
//
// test subtraction close to the limits of Currency:
//
// largest - largest number: must be to zero
test(
Currency{MaxCurrencyI64}, // 9223372036854769999
arC(Currency{MaxCurrencyI64}), // 9223372036854769999
Currency{0},
)
// -922337203685476.9999 - -0.9999 = -922337203685476
test(
Currency{-9223372036854769999},
arC(Currency{-9999}),
Currency{-9223372036854760000},
)
test(
Currency{MinCurrencyI64},
arC(Currency{-9223372036854760000}),
Currency{-9999},
)
// -922337203685476.9999 - -922337203685476.9998 = -0.0001
test(
Currency{MinCurrencyI64},
arC(Currency{-9223372036854769998}),
Currency{-1},
)
// -922337203685476.9999 - 0.0001 = -922337203685476.9998
test(
Currency{MinCurrencyI64},
arC(Currency{-1}),
Currency{-9223372036854769998},
)
// 922337203685476.9999 - 922337203685476 = 0.9999
test(
Currency{MaxCurrencyI64},
arC(Currency{9223372036854760000}),
Currency{9999},
)
// 922337203685476.9999 - -0.9999 = 922337203685476
test(
Currency{9223372036854760000},
arC(Currency{-9999}),
Currency{MaxCurrencyI64},
)
// 922337203685476.9999 - 922337203685476.9998 = 0.0001
test(
Currency{MaxCurrencyI64},
arC(Currency{9223372036854769998}),
Currency{1},
)
// 922337203685476.9999 - 0.0001 = 922337203685476.9998
test(
Currency{MaxCurrencyI64},
arC(Currency{1}),
Currency{9223372036854769998},
)
// test subtraction that will overflow Currency:
DisableErrors()
test = func(n Currency, nums []Currency, expect Currency) {
curOpTest(t, "Sub", n.Sub, n, nums, expect, 1)
}
// subtracting 0.0001 from the smallest number must overflow
test(
Currency{MinCurrencyI64}, // -922337203685476.9999
arC(Currency{1}), // -0.0001
Currency{math.MinInt64},
)
// subtracting -0.0001 from the largest number must overflow
test(
Currency{MaxCurrencyI64}, // 922337203685476.9999
arC(Currency{-1}), // 0.0001
Currency{math.MaxInt64},
)
// subtracting largest from smallest number must overflow
test(
Currency{MinCurrencyI64}, // -922337203685476.9999
arC(Currency{MaxCurrencyI64}), // 922337203685476.9999
Currency{math.MinInt64},
)
// subtracting smallest from largest number must overflow
test(
Currency{MaxCurrencyI64}, // 922337203685476.9999
arC(Currency{MinCurrencyI64}), // -922337203685476.9999
Currency{math.MaxInt64},
)
EnableErrors()
} // Test_crcy_Currency_Sub_
// go test --run Test_crcy_Currency_SubFloat_
func Test_crcy_Currency_SubFloat_(t *testing.T) {
TBegin(t)
//
// (n Currency) SubFloat(nums...float64) Currency
//
test := func(n Currency, nums []float64, expect Currency) {
curFloatOpTest(t, "SubFloat", n.SubFloat, n, nums, expect, 0)
}
test(cur(0), arF(0), cur(0))
} // Test_crcy_Currency_SubFloat_
// go test --run Test_crcy_Currency_SubInt_
func Test_crcy_Currency_SubInt_(t *testing.T) {
TBegin(t)
//
// (n Currency) SubInt(nums...int) Currency
//
test := func(n Currency, nums []int, expect Currency) {