diff options
author | nobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2016-11-25 06:28:00 +0000 |
---|---|---|
committer | nobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2016-11-25 06:28:00 +0000 |
commit | cc8782ccc86563b5aae5713863a72806d2a1400d (patch) | |
tree | d85aad8b66468b69730c9a85362879b56f5aa41a /test | |
parent | 7e49e3a40acfb155afc41f49f82e19dd0336fb3f (diff) | |
download | ruby-cc8782ccc86563b5aae5713863a72806d2a1400d.tar.gz |
round-down
* numeric.c (round_half_down, int_round_half_down): support
round-down mode.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@56897 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'test')
-rw-r--r-- | test/ruby/test_float.rb | 21 | ||||
-rw-r--r-- | test/ruby/test_integer.rb | 16 | ||||
-rw-r--r-- | test/ruby/test_rational.rb | 13 | ||||
-rw-r--r-- | test/test_mathn.rb | 21 |
4 files changed, 65 insertions, 6 deletions
diff --git a/test/ruby/test_float.rb b/test/ruby/test_float.rb index 74744bfaa2..aed387029e 100644 --- a/test/ruby/test_float.rb +++ b/test/ruby/test_float.rb @@ -701,6 +701,27 @@ class TestFloat < Test::Unit::TestCase assert_equal(-7.1364, -7.1364499.round(4, half: :up)) end + def test_round_half_down + assert_equal(12.0, 12.5.round(half: :down)) + assert_equal(13.0, 13.5.round(half: :down)) + + assert_equal(2.1, 2.15.round(1, half: :down)) + assert_equal(2.2, 2.25.round(1, half: :down)) + assert_equal(2.3, 2.35.round(1, half: :down)) + + assert_equal(-2.1, -2.15.round(1, half: :down)) + assert_equal(-2.2, -2.25.round(1, half: :down)) + assert_equal(-2.3, -2.35.round(1, half: :down)) + + assert_equal(7.1364, 7.13645.round(4, half: :down)) + assert_equal(7.1365, 7.1364501.round(4, half: :down)) + assert_equal(7.1364, 7.1364499.round(4, half: :down)) + + assert_equal(-7.1364, -7.13645.round(4, half: :down)) + assert_equal(-7.1365, -7.1364501.round(4, half: :down)) + assert_equal(-7.1364, -7.1364499.round(4, half: :down)) + end + def test_round_half_invalid assert_raise_with_message(ArgumentError, /xxx/) { 1.0.round(half: "\0xxx") diff --git a/test/ruby/test_integer.rb b/test/ruby/test_integer.rb index d9dd754ca6..18346e95f9 100644 --- a/test/ruby/test_integer.rb +++ b/test/ruby/test_integer.rb @@ -197,6 +197,10 @@ class TestInteger < Test::Unit::TestCase assert_int_equal(+300, +250.round(-2, half: :up)) assert_int_equal(+300, +349.round(-2, half: :up)) assert_int_equal(+400, +350.round(-2, half: :up)) + assert_int_equal(+200, +249.round(-2, half: :down)) + assert_int_equal(+200, +250.round(-2, half: :down)) + assert_int_equal(+300, +349.round(-2, half: :down)) + assert_int_equal(+300, +350.round(-2, half: :down)) assert_int_equal(-200, -250.round(-2)) assert_int_equal(-200, -249.round(-2, half: :even)) assert_int_equal(-200, -250.round(-2, half: :even)) @@ -206,6 +210,10 @@ class TestInteger < Test::Unit::TestCase assert_int_equal(-300, -250.round(-2, half: :up)) assert_int_equal(-300, -349.round(-2, half: :up)) assert_int_equal(-400, -350.round(-2, half: :up)) + assert_int_equal(-200, -249.round(-2, half: :down)) + assert_int_equal(-200, -250.round(-2, half: :down)) + assert_int_equal(-300, -349.round(-2, half: :down)) + assert_int_equal(-300, -350.round(-2, half: :down)) assert_int_equal(+20 * 10**70, (+25 * 10**70).round(-71)) assert_int_equal(-20 * 10**70, (-25 * 10**70).round(-71)) assert_int_equal(+20 * 10**70, (+25 * 10**70 - 1).round(-71)) @@ -230,6 +238,14 @@ class TestInteger < Test::Unit::TestCase assert_int_equal(-40 * 10**70, (-35 * 10**70).round(-71, half: :up)) assert_int_equal(+30 * 10**70, (+35 * 10**70 - 1).round(-71, half: :up)) assert_int_equal(-30 * 10**70, (-35 * 10**70 + 1).round(-71, half: :up)) + assert_int_equal(+20 * 10**70, (+25 * 10**70).round(-71, half: :down)) + assert_int_equal(-20 * 10**70, (-25 * 10**70).round(-71, half: :down)) + assert_int_equal(+20 * 10**70, (+25 * 10**70 - 1).round(-71, half: :down)) + assert_int_equal(-20 * 10**70, (-25 * 10**70 + 1).round(-71, half: :down)) + assert_int_equal(+30 * 10**70, (+35 * 10**70).round(-71, half: :down)) + assert_int_equal(-30 * 10**70, (-35 * 10**70).round(-71, half: :down)) + assert_int_equal(+30 * 10**70, (+35 * 10**70 - 1).round(-71, half: :down)) + assert_int_equal(-30 * 10**70, (-35 * 10**70 + 1).round(-71, half: :down)) assert_int_equal(1111_1111_1111_1111_1111_1111_1111_1110, 1111_1111_1111_1111_1111_1111_1111_1111.round(-1)) assert_int_equal(-1111_1111_1111_1111_1111_1111_1111_1110, (-1111_1111_1111_1111_1111_1111_1111_1111).round(-1)) diff --git a/test/ruby/test_rational.rb b/test/ruby/test_rational.rb index 834188e4d8..cf870ad3a2 100644 --- a/test/ruby/test_rational.rb +++ b/test/ruby/test_rational.rb @@ -597,12 +597,12 @@ class Rational_Test < Test::Unit::TestCase end def test_trunc - [[Rational(13, 5), [ 2, 3, 2, 3, 3, 3]], # 2.6 - [Rational(5, 2), [ 2, 3, 2, 2, 2, 3]], # 2.5 - [Rational(12, 5), [ 2, 3, 2, 2, 2, 2]], # 2.4 - [Rational(-12,5), [-3, -2, -2, -2, -2, -2]], # -2.4 - [Rational(-5, 2), [-3, -2, -2, -2, -2, -3]], # -2.5 - [Rational(-13, 5), [-3, -2, -2, -3, -3, -3]], # -2.6 + [[Rational(13, 5), [ 2, 3, 2, 3, 3, 3, 3]], # 2.6 + [Rational(5, 2), [ 2, 3, 2, 2, 2, 3, 2]], # 2.5 + [Rational(12, 5), [ 2, 3, 2, 2, 2, 2, 2]], # 2.4 + [Rational(-12,5), [-3, -2, -2, -2, -2, -2, -2]], # -2.4 + [Rational(-5, 2), [-3, -2, -2, -2, -2, -3, -2]], # -2.5 + [Rational(-13, 5), [-3, -2, -2, -3, -3, -3, -3]], # -2.6 ].each do |i, a| s = proc {i.inspect} assert_equal(a[0], i.floor, s) @@ -611,6 +611,7 @@ class Rational_Test < Test::Unit::TestCase assert_equal(a[3], i.round, s) assert_equal(a[4], i.round(half: :even), s) assert_equal(a[5], i.round(half: :up), s) + assert_equal(a[6], i.round(half: :down), s) end end diff --git a/test/test_mathn.rb b/test/test_mathn.rb index 2ea049502c..bafc4c8dbc 100644 --- a/test/test_mathn.rb +++ b/test/test_mathn.rb @@ -157,6 +157,27 @@ class TestMathn < Test::Unit::TestCase assert_equal((-12/5), (-12/5).round(2, half: :up)) assert_equal(( -5/2), ( -5/2).round(2, half: :up)) assert_equal((-13/5), (-13/5).round(2, half: :up)) + + assert_equal( 3, ( 13/5).round(half: :down)) + assert_equal( 2, ( 5/2).round(half: :down)) + assert_equal( 2, ( 12/5).round(half: :down)) + assert_equal(-2, (-12/5).round(half: :down)) + assert_equal(-2, ( -5/2).round(half: :down)) + assert_equal(-3, (-13/5).round(half: :down)) + + assert_equal( 3, ( 13/5).round(0, half: :down)) + assert_equal( 2, ( 5/2).round(0, half: :down)) + assert_equal( 2, ( 12/5).round(0, half: :down)) + assert_equal(-2, (-12/5).round(0, half: :down)) + assert_equal(-2, ( -5/2).round(0, half: :down)) + assert_equal(-3, (-13/5).round(0, half: :down)) + + assert_equal(( 13/5), ( 13/5).round(2, half: :down)) + assert_equal(( 5/2), ( 5/2).round(2, half: :down)) + assert_equal(( 12/5), ( 12/5).round(2, half: :down)) + assert_equal((-12/5), (-12/5).round(2, half: :down)) + assert_equal(( -5/2), ( -5/2).round(2, half: :down)) + assert_equal((-13/5), (-13/5).round(2, half: :down)) EOS end end |