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 Sep 19, 2013
1 parent 5703690 commit 6629c66
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 12 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 }.inject(&:+).to_f / size
return 0.0 if empty? or lines_of_code == 0
Float(map {|f| f.covered_strength }.inject(&:+).to_f / size)
end
end
4 changes: 2 additions & 2 deletions lib/simplecov/result.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def groups
# The overall percentual coverage for this result
def covered_percent
# Make sure that weird rounding error from #15, #23 and #24 does not occur again!
total_lines.zero? ? 0 : 100.0 * covered_lines / total_lines
total_lines.zero? ? 0 : Float(100.0 * covered_lines / total_lines)
end

# The multiple of coverage for this result
Expand All @@ -62,7 +62,7 @@ def covered_strength
end
end
end
@covered_strength = m.to_f / total_lines
@covered_strength = Float(m.to_f / total_lines)
end

# Returns the count of lines that are covered
Expand Down
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 6629c66

Please sign in to comment.