Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure we return Floats, not Fixnum or Rational; fixes segfaults with mathn #266

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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