Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix allocate panic #123

Merged
merged 6 commits into from
Nov 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion calculator.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@ func (c *calculator) modulus(a Amount, d int64) Amount {
return a % d
}

func (c *calculator) allocate(a Amount, r, s int) Amount {
func (c *calculator) allocate(a Amount, r, s uint) Amount {
if a == 0 || s == 0 {
return 0
}

return a * int64(r) / int64(s)
}

Expand Down
15 changes: 12 additions & 3 deletions money.go
Original file line number Diff line number Diff line change
Expand Up @@ -265,23 +265,32 @@ func (m *Money) Allocate(rs ...int) ([]*Money, error) {
}

// Calculate sum of ratios.
var sum int
var sum uint
for _, r := range rs {
sum += r
if r < 0 {
return nil, errors.New("negative ratios not allowed")
}
sum += uint(r)
}

var total int64
ms := make([]*Money, 0, len(rs))
for _, r := range rs {
party := &Money{
amount: mutate.calc.allocate(m.amount, r, sum),
amount: mutate.calc.allocate(m.amount, uint(r), sum),
currency: m.currency,
}

ms = append(ms, party)
total += party.amount
}

// if the sum of all ratios is zero, then we just returns zeros and don't do anything
// with the leftover
if sum == 0 {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would it work with the case when ratios are equal to {-50, 50}? The sum is 0, how this works? To be honest with you, I'm not really sure what negative ratio means and I expect to always get unsigned integer, is there a use case I'm missing?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I couldn't come up with a use case for negative ratios. I've added a check, that returns a new error if any of the ratios are negative.

return ms, nil
}

// Calculate leftover value and divide to first parties.
lo := m.amount - total
sub := int64(1)
Expand Down
4 changes: 4 additions & 0 deletions money_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,10 @@ func TestMoney_Allocate(t *testing.T) {
{100, []int{30, 30, 30}, []int64{34, 33, 33}},
{200, []int{25, 25, 50}, []int64{50, 50, 100}},
{5, []int{50, 25, 25}, []int64{3, 1, 1}},
{0, []int{0, 0, 0, 0}, []int64{0, 0, 0, 0}},
{0, []int{50, 10}, []int64{0, 0}},
{10, []int{0, 100}, []int64{0, 10}},
{10, []int{0, 0}, []int64{0, 0}},
}

for _, tc := range tcs {
Expand Down