From 12d5858ff8afa845578a5232122dd1287bd6fc33 Mon Sep 17 00:00:00 2001 From: Stormwind Date: Wed, 21 Oct 2015 09:00:33 +0200 Subject: [PATCH] Fix division by zero If the maximum value is zero, the maximum value will be set to one. This avoids a division by zero. --- lib/rubella/weighting/per_count.rb | 2 ++ .../rubella/weighting/per_count/parse_spec.rb | 18 ++++++++++++++++++ 2 files changed, 20 insertions(+) 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