-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmoney_test.go
More file actions
112 lines (103 loc) · 3.5 KB
/
money_test.go
File metadata and controls
112 lines (103 loc) · 3.5 KB
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
package dough
import "testing"
// This package was largely taken from itsoneiota/dough-go.
// dough-go has a ton of tests for addition, subtraction, multiplication, and comparison.
// Since we've switch to an int, rather than a struct, there's not much point testing go's arithmetic.
func TestCanAllocate(t *testing.T) {
var cases = []struct {
a Money
ratios []uint
want []Money
}{
{0, []uint{1, 1, 1}, []Money{0, 0, 0}},
{1, []uint{1, 1, 1}, []Money{1, 0, 0}},
{2, []uint{1, 1, 1}, []Money{1, 1, 0}},
{3, []uint{1, 1, 1}, []Money{1, 1, 1}},
{4, []uint{1, 1, 1}, []Money{2, 1, 1}},
{5, []uint{1, 1, 1}, []Money{2, 2, 1}},
{100, []uint{0, 1, 0}, []Money{0, 100, 0}},
{3, []uint{0, 5, 0}, []Money{0, 3, 0}},
{300, []uint{1, 1, 1}, []Money{100, 100, 100}},
{100, []uint{1, 1, 1}, []Money{34, 33, 33}},
{3, []uint{0, 5, 0}, []Money{0, 3, 0}},
{3, []uint{0, 4, 2}, []Money{0, 2, 1}},
// Allocate spare pennies and skip zero weighting.
{7, []uint{0, 1, 1}, []Money{0, 4, 3}},
// Copied from MoneyTest.php
{105, []uint{3, 7}, []Money{32, 73}},
{5, []uint{1, 1}, []Money{3, 2}},
{30000, []uint{122, 878}, []Money{3660, 26340}},
{30000, []uint{122, 0, 878}, []Money{3660, 0, 26340}},
{12000, []uint{20, 100}, []Money{2000, 10000}},
// If weightings are equal, the amount will be shared.
{30000, []uint{0}, []Money{30000}},
{30000, []uint{0, 0, 0}, []Money{10000, 10000, 10000}},
// Repeat all of the above with negatives.
{-0, []uint{1, 1, 1}, []Money{0, 0, 0}},
{-1, []uint{1, 1, 1}, []Money{-1, 0, 0}},
{-2, []uint{1, 1, 1}, []Money{-1, -1, 0}},
{-3, []uint{1, 1, 1}, []Money{-1, -1, -1}},
{-4, []uint{1, 1, 1}, []Money{-2, -1, -1}},
{-5, []uint{1, 1, 1}, []Money{-2, -2, -1}},
{-100, []uint{0, 1, 0}, []Money{0, -100, 0}},
{-3, []uint{0, 5, 0}, []Money{0, -3, 0}},
{-300, []uint{1, 1, 1}, []Money{-100, -100, -100}},
{-100, []uint{1, 1, 1}, []Money{-34, -33, -33}},
{-3, []uint{0, 5, 0}, []Money{0, -3, 0}},
{-3, []uint{0, 4, 2}, []Money{0, -2, -1}},
{-105, []uint{3, 7}, []Money{-32, -73}},
{-5, []uint{1, 1}, []Money{-3, -2}},
{-30000, []uint{122, 878}, []Money{-3660, -26340}},
{-30000, []uint{122, 0, 878}, []Money{-3660, 0, -26340}},
{-12000, []uint{20, 100}, []Money{-2000, -10000}},
{-30000, []uint{0}, []Money{-30000}},
{-30000, []uint{0, 0, 0}, []Money{-10000, -10000, -10000}},
}
for ci, c := range cases {
res := c.a.Share(c.ratios)
if len(c.ratios) != len(res) {
t.Errorf("Case %d. Incorrect number of allocations returned. Expected %d, got %d: %v", ci, len(c.ratios), len(res), res)
return
}
for i := range c.want {
if c.want[i] != res[i] {
t.Errorf("Case %d: Sharing %d into (%v), portion %d: Expected %d, got %d", ci, c, c.ratios, i, c.want[i], res[i])
}
}
}
}
func TestCanDiscount(t *testing.T) {
var cases = []struct {
a Money
p uint
want Money
}{
{100, 0, 100},
{100, 100, 0},
{100, 1, 99},
{100, 50, 50},
}
for ci, c := range cases {
res, err := c.a.PercentageDiscount(c.p)
if err != nil {
t.Errorf("Case %d. Error discounting %d by %d%%: %s", ci, c.a, c.p, err)
return
}
if c.want != res {
t.Errorf("Case %d. Incorrect result. Discounted %d by %d%%, got %d.", ci, c.a, c.p, res)
}
}
}
func TestCanRejectBadPercentageDiscount(t *testing.T) {
var cases = []uint{
101,
11111,
}
var a Money = 100
for i, p := range cases {
_, err := a.PercentageDiscount(p)
if err == nil {
t.Errorf("Case %d. Expected percentage discount of %d to be rejected, was accepted.", i, p)
}
}
}