From 432cb4bcfee62abaf99cff7b7406a80a85cff89d Mon Sep 17 00:00:00 2001 From: Oleg Strokachuk Date: Thu, 3 Dec 2020 19:13:04 +0300 Subject: [PATCH] Fix case with 1 choice which have weight equals 1 --- weightedrand.go | 2 +- weightedrand_test.go | 5 +++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/weightedrand.go b/weightedrand.go index 90ea2aa..9c6c943 100644 --- a/weightedrand.go +++ b/weightedrand.go @@ -52,7 +52,7 @@ func NewChooser(choices ...Choice) (*Chooser, error) { totals[i] = runningTotal } - if runningTotal <= 1 { + if runningTotal < 1 { return nil, errNoValidChoices } diff --git a/weightedrand_test.go b/weightedrand_test.go index 7c3656c..1949da0 100644 --- a/weightedrand_test.go +++ b/weightedrand_test.go @@ -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}},