Skip to content

Commit

Permalink
[simplecov-ruby#175][simplecov-ruby#140] Ensure we return Floats, not…
Browse files Browse the repository at this point in the history
… Fixnum or Rational; fixes segfaults with mathn
  • Loading branch information
bf4 committed May 2, 2014
1 parent 5e2b5b3 commit 3af9eb8
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 10 deletions.
8 changes: 5 additions & 3 deletions lib/simplecov/file_list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
15 changes: 8 additions & 7 deletions lib/simplecov/source_file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 3af9eb8

Please sign in to comment.