From 3af9eb8a38882f71d9a7d8195b45224d62576168 Mon Sep 17 00:00:00 2001 From: Benjamin Fleischer Date: Wed, 18 Sep 2013 23:29:09 -0500 Subject: [PATCH] [#175][#140] Ensure we return Floats, not Fixnum or Rational; fixes segfaults with mathn --- lib/simplecov/file_list.rb | 8 +++++--- lib/simplecov/source_file.rb | 15 ++++++++------- 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/lib/simplecov/file_list.rb b/lib/simplecov/file_list.rb index 8cea9fd4..facee665 100644 --- a/lib/simplecov/file_list.rb +++ b/lib/simplecov/file_list.rb @@ -31,14 +31,16 @@ def lines_of_code end # Computes the coverage based upon lines covered and lines missed + # @return [Float] def covered_percent return 100.0 if empty? or lines_of_code == 0 - covered_lines * 100.0 / lines_of_code + Float(covered_lines * 100.0 / lines_of_code) end # Computes the strength (hits / line) based upon lines covered and lines missed + # @return [Float] def covered_strength - return 0 if empty? or lines_of_code == 0 - map {|f| f.covered_strength * f.lines_of_code }.inject(&:+) / lines_of_code + return 0.0 if empty? or lines_of_code == 0 + Float(map {|f| f.covered_strength * f.lines_of_code }.inject(&:+) / lines_of_code) end end diff --git a/lib/simplecov/source_file.rb b/lib/simplecov/source_file.rb index 9e578098..e091e2a7 100644 --- a/lib/simplecov/source_file.rb +++ b/lib/simplecov/source_file.rb @@ -111,24 +111,24 @@ def covered_percent return 100.0 if lines.length == 0 or lines.length == never_lines.count relevant_lines = lines.count - never_lines.count - skipped_lines.count if relevant_lines == 0 - 0 + 0.0 else - (covered_lines.count) * 100.0 / relevant_lines.to_f + Float((covered_lines.count) * 100.0 / relevant_lines.to_f) end end def covered_strength - return 0 if lines.length == 0 or lines.length == never_lines.count + return 0.0 if lines.length == 0 or lines.length == never_lines.count lines_strength = 0 lines.each do |c| lines_strength += c.coverage if c.coverage end - effective_lines_count = (lines.count - never_lines.count - skipped_lines.count).to_f + effective_lines_count = Float(lines.count - never_lines.count - skipped_lines.count) if effective_lines_count == 0 - 0 + 0.0 else strength = lines_strength / effective_lines_count round_float(strength, 1) @@ -178,9 +178,10 @@ def process_skipped_lines! private # ruby 1.9 could use Float#round(places) instead + # @return [Float] def round_float(float, places) - factor = (10 * places).to_f - (float * factor).round / factor + factor = Float(10 * places) + Float((float * factor).round / factor) end end end