Skip to content

Commit

Permalink
Merge pull request #6 from zifter/fix-corner-case-with-1-choose
Browse files Browse the repository at this point in the history
Fix case with 1 choice which have weight equals 1
  • Loading branch information
mroth committed Dec 4, 2020
2 parents 953da99 + 432cb4b commit 0d64275
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 1 deletion.
2 changes: 1 addition & 1 deletion weightedrand.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func NewChooser(choices ...Choice) (*Chooser, error) {
totals[i] = runningTotal
}

if runningTotal <= 1 {
if runningTotal < 1 {
return nil, errNoValidChoices
}

Expand Down
5 changes: 5 additions & 0 deletions weightedrand_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ func TestNewChooser(t *testing.T) {
cs: []Choice{{Item: 'a', Weight: 0}, {Item: 'b', Weight: 0}},
wantErr: errNoValidChoices,
},
{
name: "choice with weight equals 1",
cs: []Choice{{Item: 'a', Weight: 1}},
wantErr: nil,
},
{
name: "weight overflow",
cs: []Choice{{Item: 'a', Weight: maxInt/2 + 1}, {Item: 'b', Weight: maxInt/2 + 1}},
Expand Down

0 comments on commit 0d64275

Please sign in to comment.