summaryrefslogtreecommitdiffhomepage
path: root/test
diff options
context:
space:
mode:
authorKOBAYASHI Shuji <[email protected]>2019-03-26 22:12:09 +0900
committerKOBAYASHI Shuji <[email protected]>2019-03-26 22:12:09 +0900
commit0b6696cc2872c9b33efa48c7dcd45dc4a17f3686 (patch)
tree3718e369517fd3dca7be81364e0b4fdf72a4c18f /test
parentc2660b8111cd6cd98a41aa257c503cbd3a7cf881 (diff)
downloadmruby-0b6696cc2872c9b33efa48c7dcd45dc4a17f3686.tar.gz
mruby-0b6696cc2872c9b33efa48c7dcd45dc4a17f3686.zip
Fix dealing with infinity and NaN in `test/assert.rb:assert_float`
`assert_float` is always passed when expected value and/or actual value are infinity or NaN. This behavior seems unintentional. Before this patch: assert_float(Float::INFINITY, 1.0) #=> pass assert_float(-Float::INFINITY, 1) #=> pass assert_float(1, 1/0) #=> pass assert_float(1, -1/0) #=> pass assert_float(1.0, Float::NAN) #=> pass assert_float(Float::NAN, 1) #=> pass After this patch: assert_float(Float::INFINITY, 1.0) #=> fail: Expected 1.0 to be Infinity. assert_float(-Float::INFINITY, 1) #=> fail: Expected 1 to be -Infinity. assert_float(1, 1/0) #=> fail: Expected Infinity to be 1. assert_float(1, -1/0) #=> fail: Expected -Infinity to be 1. assert_float(1.0, Float::NAN) #=> fail: Expected NaN to be 1.0. assert_float(Float::NAN, 1) #=> fail: Expected 1 to be NaN.
Diffstat (limited to 'test')
-rw-r--r--test/assert.rb24
1 files changed, 8 insertions, 16 deletions
diff --git a/test/assert.rb b/test/assert.rb
index f6914cf81..33d9285ce 100644
--- a/test/assert.rb
+++ b/test/assert.rb
@@ -148,10 +148,15 @@ end
##
# Fails unless +exp+ is equal to +act+ in terms of a Float
def assert_float(exp, act, msg = nil)
- unless ret = check_float(exp, act)
- diff = " Expected |#{exp} - #{act}| (#{(exp-act).abs}) to be <= #{Mrbtest::FLOAT_TOLERANCE}."
+ e, a = exp.to_f, act.to_f
+ if (e.infinite? || a.infinite?) && e != a ||
+ e.nan? && !a.nan? || !e.nan? && a.nan?
+ assert_true(false, msg, " Expected #{act} to be #{exp}.")
+ elsif (n = (e - a).abs) > Mrbtest::FLOAT_TOLERANCE
+ assert_true(false, msg, " Expected |#{exp} - #{act}| (#{n}) to be <= #{Mrbtest::FLOAT_TOLERANCE}.")
+ else
+ assert_true(true)
end
- assert_true(ret, msg, diff)
end
def assert_raise(*exc)
@@ -211,19 +216,6 @@ def report
$ko_test == 0 && $kill_test == 0
end
-##
-# Performs fuzzy check for equality on methods returning floats
-def check_float(a, b)
- tolerance = Mrbtest::FLOAT_TOLERANCE
- a = a.to_f
- b = b.to_f
- if a.finite? and b.finite?
- (a-b).abs < tolerance
- else
- true
- end
-end
-
def _eval_assertion(meth, exp, act_or_msg, msg, block)
if block
exp, act, msg = exp, block.call, act_or_msg