From 7ced5439b4fea1ca080473c82320dc81d20d5cce Mon Sep 17 00:00:00 2001 From: mame Date: Sat, 9 Feb 2008 12:21:30 +0000 Subject: * test/ruby/test_math.rb: add tests for Math#gamma, Math#lgamma and Math#cbrt, and use assert_in_delta instead of assert. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@15418 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 7 +++++- test/ruby/test_math.rb | 63 +++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 68 insertions(+), 2 deletions(-) diff --git a/ChangeLog b/ChangeLog index 2d2375f762..c50473b2ed 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,8 @@ +Sat Feb 9 21:20:28 2008 Yusuke Endoh + + * test/ruby/test_math.rb: add tests for Math#gamma, Math#lgamma and + Math#cbrt, and use assert_in_delta instead of assert. + Sat Feb 9 18:34:45 2008 Tanaka Akira * math.c (math_cbrt): new method Math.cbrt. @@ -69,7 +74,7 @@ Fri Feb 8 09:27:57 2008 NARUSE, Yui * lib/rdoc/ri/driver.rb (read_yaml): remove SM* for compatibility. -Fri Feb 08 00:07:24 2008 Yusuke Endoh +Fri Feb 8 00:07:24 2008 Yusuke Endoh * test/ruby/test_hash.rb: follow the change of Hash#flatten. diff --git a/test/ruby/test_math.rb b/test/ruby/test_math.rb index 342e81f410..d5856f3bd6 100644 --- a/test/ruby/test_math.rb +++ b/test/ruby/test_math.rb @@ -2,7 +2,7 @@ require 'test/unit' class TestMath < Test::Unit::TestCase def check(a, b) - assert(a - b < Float::EPSILON * 3) + assert_in_delta(a, b, Float::EPSILON * 4) end def test_atan2 @@ -170,4 +170,65 @@ class TestMath < Test::Unit::TestCase check(1, Math.erfc(0)) check(0, Math.erfc(1.0 / 0.0)) end + + def test_gamma + sqrt_pi = Math.sqrt(Math::PI) + check(4 * sqrt_pi / 3, Math.gamma(-1.5)) + check(-2 * sqrt_pi, Math.gamma(-0.5)) + check(sqrt_pi, Math.gamma(0.5)) + check(1, Math.gamma(1)) + check(sqrt_pi / 2, Math.gamma(1.5)) + check(1, Math.gamma(2)) + check(3 * sqrt_pi / 4, Math.gamma(2.5)) + check(2, Math.gamma(3)) + check(15 * sqrt_pi / 8, Math.gamma(3.5)) + check(6, Math.gamma(4)) + end + + def test_lgamma + sqrt_pi = Math.sqrt(Math::PI) + + g, s = Math.lgamma(-1.5) + check(Math.log(4 * sqrt_pi / 3), g) + assert_equal(s, 1) + + g, s = Math.lgamma(-0.5) + check(Math.log(2 * sqrt_pi), g) + assert_equal(s, -1) + + g, s = Math.lgamma(0.5) + check(Math.log(sqrt_pi), g) + assert_equal(s, 1) + + assert_equal([0, 1], Math.lgamma(1)) + + g, s = Math.lgamma(1.5) + check(Math.log(sqrt_pi / 2), g) + assert_equal(s, 1) + + assert_equal([0, 1], Math.lgamma(2)) + + g, s = Math.lgamma(2.5) + check(Math.log(3 * sqrt_pi / 4), g) + assert_equal(s, 1) + + g, s = Math.lgamma(3) + check(Math.log(2), g) + assert_equal(s, 1) + + g, s = Math.lgamma(3.5) + check(Math.log(15 * sqrt_pi / 8), g) + assert_equal(s, 1) + + g, s = Math.lgamma(4) + check(Math.log(6), g) + assert_equal(s, 1) + end + + def test_cbrt + check(1, Math.cbrt(1)) + check(-2, Math.cbrt(-8)) + check(3, Math.cbrt(27)) + check(-0.1, Math.cbrt(-0.001)) + end end -- cgit v1.2.3