summaryrefslogtreecommitdiff
path: root/test/ruby/test_math.rb
diff options
context:
space:
mode:
authormuraken <muraken@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2010-02-28 10:08:22 +0000
committermuraken <muraken@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2010-02-28 10:08:22 +0000
commit5073155a178a9f478950afef4f148e44fd14b5d6 (patch)
tree83edec3cce4786cea435ac932fb503fdaea17bc6 /test/ruby/test_math.rb
parent58e8688f69776eb891abf9980d204015c3fddb1d (diff)
downloadruby-5073155a178a9f478950afef4f148e44fd14b5d6.tar.gz
* math.c (rb_eMathDomainError): new exception class for representing mathematical domain error instead of Errno::EDOM.
* math.c (domain_check, infinity_check): removed, no longer needed. * math.c (math_atan2, math_acos, math_asin, math_acosh, math_atanh, math_log, math_log2, math_log10, math_sqrt, math_gamma, math_lgamma): mathematical domain errors are checked and raised before calling libm's functions. * test/ruby/test_math.rb: updated for changes of maht.c. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@26785 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'test/ruby/test_math.rb')
-rw-r--r--test/ruby/test_math.rb51
1 files changed, 39 insertions, 12 deletions
diff --git a/test/ruby/test_math.rb b/test/ruby/test_math.rb
index 5b49291df0..f7055d7b5b 100644
--- a/test/ruby/test_math.rb
+++ b/test/ruby/test_math.rb
@@ -2,16 +2,26 @@ require 'test/unit'
class TestMath < Test::Unit::TestCase
def assert_infinity(a, *rest)
- rest = ["not infinity"] if rest.empty?
+ rest = ["not infinity: #{a.inspect}"] if rest.empty?
assert(!a.finite?, *rest)
end
+ def assert_nan(a, *rest)
+ rest = ["not nan: #{a.inspect}"] if rest.empty?
+ assert(a.nan?, *rest)
+ end
+
def check(a, b)
err = [Float::EPSILON * 4, [a.abs, b.abs].max * Float::EPSILON * 256].max
assert_in_delta(a, b, err)
end
def test_atan2
+ assert_raise(Math::DomainError) { Math.atan2(0, 0) }
+ assert_raise(Math::DomainError) { Math.atan2(Float::INFINITY, Float::INFINITY) }
+ assert_raise(Math::DomainError) { Math.atan2(Float::INFINITY, -Float::INFINITY) }
+ assert_raise(Math::DomainError) { Math.atan2(-Float::INFINITY, Float::INFINITY) }
+ assert_raise(Math::DomainError) { Math.atan2(-Float::INFINITY, -Float::INFINITY) }
check(0, Math.atan2(0, 1))
check(Math::PI / 4, Math.atan2(1, 1))
check(Math::PI / 2, Math.atan2(1, 0))
@@ -46,7 +56,9 @@ class TestMath < Test::Unit::TestCase
check(1 * Math::PI / 4, Math.acos( 1.0 / Math.sqrt(2)))
check(2 * Math::PI / 4, Math.acos( 0.0))
check(4 * Math::PI / 4, Math.acos(-1.0))
- assert_raise(Errno::EDOM, Errno::ERANGE) { Math.acos(2.0) }
+ assert_raise(Math::DomainError) { Math.acos(+1.0 + Float::EPSILON) }
+ assert_raise(Math::DomainError) { Math.acos(-1.0 - Float::EPSILON) }
+ assert_raise(Math::DomainError) { Math.acos(2.0) }
end
def test_asin
@@ -54,7 +66,9 @@ class TestMath < Test::Unit::TestCase
check( 1 * Math::PI / 4, Math.asin( 1.0 / Math.sqrt(2)))
check( 2 * Math::PI / 4, Math.asin( 1.0))
check(-2 * Math::PI / 4, Math.asin(-1.0))
- assert_raise(Errno::EDOM, Errno::ERANGE) { Math.asin(2.0) }
+ assert_raise(Math::DomainError) { Math.asin(+1.0 + Float::EPSILON) }
+ assert_raise(Math::DomainError) { Math.asin(-1.0 - Float::EPSILON) }
+ assert_raise(Math::DomainError) { Math.asin(2.0) }
end
def test_atan
@@ -86,7 +100,8 @@ class TestMath < Test::Unit::TestCase
check(0, Math.acosh(1))
check(1, Math.acosh((Math::E ** 1 + Math::E ** -1) / 2))
check(2, Math.acosh((Math::E ** 2 + Math::E ** -2) / 2))
- assert_raise(Errno::EDOM, Errno::ERANGE) { Math.acosh(0) }
+ assert_raise(Math::DomainError) { Math.acosh(1.0 - Float::EPSILON) }
+ assert_raise(Math::DomainError) { Math.acosh(0) }
end
def test_asinh
@@ -99,7 +114,10 @@ class TestMath < Test::Unit::TestCase
check(0, Math.atanh(Math.sinh(0) / Math.cosh(0)))
check(1, Math.atanh(Math.sinh(1) / Math.cosh(1)))
check(2, Math.atanh(Math.sinh(2) / Math.cosh(2)))
- assert_raise(Errno::EDOM, Errno::ERANGE) { Math.atanh(-1) }
+ assert_nothing_raised { assert_infinity Math.atanh(1) }
+ assert_nothing_raised { assert_infinity -Math.atanh(-1) }
+ assert_raise(Math::DomainError) { Math.atanh(+1.0 + Float::EPSILON) }
+ assert_raise(Math::DomainError) { Math.atanh(-1.0 - Float::EPSILON) }
end
def test_exp
@@ -116,8 +134,9 @@ class TestMath < Test::Unit::TestCase
check(1, Math.log(10, 10))
check(2, Math.log(100, 10))
assert_equal(1.0/0, Math.log(1.0/0))
- assert_raise(Errno::EDOM, Errno::ERANGE) { Math.log(0) }
- assert_raise(Errno::EDOM, Errno::ERANGE) { Math.log(-1) }
+ assert_nothing_raised { assert_infinity -Math.log(+0.0) }
+ assert_nothing_raised { assert_infinity -Math.log(-0.0) }
+ assert_raise(Math::DomainError) { Math.log(-1.0) }
assert_raise(TypeError) { Math.log(1,nil) }
end
@@ -126,8 +145,9 @@ class TestMath < Test::Unit::TestCase
check(1, Math.log2(2))
check(2, Math.log2(4))
assert_equal(1.0/0, Math.log2(1.0/0))
- assert_raise(Errno::EDOM, Errno::ERANGE) { Math.log2(0) }
- assert_raise(Errno::EDOM, Errno::ERANGE) { Math.log2(-1) }
+ assert_nothing_raised { assert_infinity -Math.log2(+0.0) }
+ assert_nothing_raised { assert_infinity -Math.log2(-0.0) }
+ assert_raise(Math::DomainError) { Math.log2(-1.0) }
end
def test_log10
@@ -135,8 +155,9 @@ class TestMath < Test::Unit::TestCase
check(1, Math.log10(10))
check(2, Math.log10(100))
assert_equal(1.0/0, Math.log10(1.0/0))
- assert_raise(Errno::EDOM, Errno::ERANGE) { Math.log10(0) }
- assert_raise(Errno::EDOM, Errno::ERANGE) { Math.log10(-1) }
+ assert_nothing_raised { assert_infinity -Math.log10(+0.0) }
+ assert_nothing_raised { assert_infinity -Math.log10(-0.0) }
+ assert_raise(Math::DomainError) { Math.log10(-1.0) }
end
def test_sqrt
@@ -144,7 +165,8 @@ class TestMath < Test::Unit::TestCase
check(1, Math.sqrt(1))
check(2, Math.sqrt(4))
assert_equal(1.0/0, Math.sqrt(1.0/0))
- assert_raise(Errno::EDOM, Errno::ERANGE) { Math.sqrt(-1) }
+ assert_equal("0.0", Math.sqrt(-0.0).to_s) # insure it is +0.0, not -0.0
+ assert_raise(Math::DomainError) { Math.sqrt(-1.0) }
end
def test_frexp
@@ -201,6 +223,8 @@ class TestMath < Test::Unit::TestCase
assert_infinity(Math.gamma(i), "Math.gamma(#{i}) should be INF")
assert_infinity(Math.gamma(i-1), "Math.gamma(#{i-1}) should be INF")
end
+
+ assert_raise(Math::DomainError) { Math.gamma(-Float::INFINITY) }
end
def test_lgamma
@@ -241,6 +265,9 @@ class TestMath < Test::Unit::TestCase
g, s = Math.lgamma(4)
check(Math.log(6), g)
assert_equal(s, 1)
+
+ assert_infinity Math.lgamma(-Float::INFINITY)
+ assert_raise(Math::DomainError) { Math.lgamma(-Float::INFINITY) }
end
def test_cbrt