diff --git a/lib/rubella/weighting/per_count.rb b/lib/rubella/weighting/per_count.rb index fceb94a..0e00e35 100644 --- a/lib/rubella/weighting/per_count.rb +++ b/lib/rubella/weighting/per_count.rb @@ -40,6 +40,8 @@ module Rubella # Get the maximum of commits max = input.sort.pop + # Sets maximum to 1 if its 0 to avoid division by zero + max = 1 if max == 0 i = 0 input.each do |commits| diff --git a/spec/unit/rubella/weighting/per_count/parse_spec.rb b/spec/unit/rubella/weighting/per_count/parse_spec.rb index 50be0ee..253cb90 100644 --- a/spec/unit/rubella/weighting/per_count/parse_spec.rb +++ b/spec/unit/rubella/weighting/per_count/parse_spec.rb @@ -40,4 +40,22 @@ describe Rubella::Weighting::PerCount, '.parse' do ) end + it "writes 0 if value is 0" do + input = [0, 2, 1, 3, 1, 4, 5, 2, 1, 2, 3, 4, 2, 1] + weighting = Rubella::Weighting::PerCount.new 7 + + storage = weighting.parse(input) + + expect(storage.data[0][0]).to eq(0.0) + end + + it "sets 0 to 0 even if the maximum value is 0" do + input = [0, 0, 0, 0] + weighting = Rubella::Weighting::PerCount.new 2 + + storage = weighting.parse(input) + + expect(storage.data[0][0]).to eq(0.0) + end + end \ No newline at end of file