mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
* test/ruby/test_rational.rb: add tests to achieve over 90% test
coverage of rational.c. * test/ruby/test_complex.rb: ditto for complex.c. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@15840 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
2d656b7d28
commit
43a2aaea2f
4 changed files with 240 additions and 4 deletions
|
@ -686,6 +686,8 @@ class Rational_Test < Test::Unit::TestCase
|
|||
assert_equal(true, Rational(2,1) != Rational(1))
|
||||
assert_equal(false, Rational(1) == nil)
|
||||
assert_equal(false, Rational(1) == '')
|
||||
|
||||
assert_equal(false, Rational(1,2**100) == 1)
|
||||
end
|
||||
|
||||
def test_unify
|
||||
|
@ -955,6 +957,108 @@ class Rational_Test < Test::Unit::TestCase
|
|||
assert_equal(0.25, Rational(1,2).fdiv(2))
|
||||
end
|
||||
|
||||
def test_zero_div
|
||||
assert_raise(ZeroDivisionError) { Rational(1, 0) }
|
||||
assert_raise(ZeroDivisionError) { Rational(1, 1) / 0 }
|
||||
end
|
||||
|
||||
def test_gcd
|
||||
assert_equal(0, Rational(0, 2**100))
|
||||
end
|
||||
|
||||
def test_unify
|
||||
f = defined?(Rational::Unify)
|
||||
Rational.const_set(:Unify, true) unless f
|
||||
|
||||
assert_same(42, Rational(84, 2))
|
||||
assert_same(1, Rational(1, 2) + Rational(1, 2))
|
||||
|
||||
Rational.instance_eval { remove_const(:Unify) } unless f
|
||||
end
|
||||
|
||||
def test_coerce
|
||||
r = Rational(7, 3)
|
||||
assert_equal(Rational(42, 1), r.coerce(42).first)
|
||||
assert_raise(TypeError) { r.coerce(Object.new) }
|
||||
|
||||
o = Object.new
|
||||
def o.coerce(x); [x.numerator, x.denominator]; end
|
||||
assert_equal(10, r + o)
|
||||
assert_equal(4, r - o)
|
||||
assert_equal(21, r * o)
|
||||
assert_equal(2, r / o)
|
||||
assert_equal(343, r ** o)
|
||||
assert_equal(1, r <=> o)
|
||||
|
||||
b = 2**100
|
||||
def b.<=>(x); 0; end rescue nil
|
||||
assert_equal(1, r ** b)
|
||||
b = 2**100
|
||||
def b.**(x); -1; end rescue nil
|
||||
assert_equal(-1, Rational(1, b)**3)
|
||||
end
|
||||
|
||||
def test_modulo_remainder
|
||||
assert_equal(Rational(1, 2), Rational(5, 2).modulo(1))
|
||||
assert_equal(Rational(1, 2), Rational(5, 2).modulo(2))
|
||||
assert_equal(Rational(5, 2), Rational(5, 2).modulo(3))
|
||||
assert_equal(Rational(5, 6), Rational(5, 2).modulo(Rational(5, 3)))
|
||||
assert_equal(Rational(1, 2), Rational(-5, 2).modulo(1))
|
||||
assert_equal(Rational(-1, 2), Rational(5, 2).modulo(-1))
|
||||
assert_equal(Rational(-1, 2), Rational(-5, 2).modulo(-1))
|
||||
|
||||
assert_equal(Rational(1, 2), Rational(5, 2).remainder(1))
|
||||
assert_equal(Rational(1, 2), Rational(5, 2).remainder(2))
|
||||
assert_equal(Rational(5, 2), Rational(5, 2).remainder(3))
|
||||
assert_equal(Rational(5, 6), Rational(5, 2).remainder(Rational(5, 3)))
|
||||
assert_equal(Rational(-1, 2), Rational(-5, 2).remainder(1))
|
||||
assert_equal(Rational(1, 2), Rational(5, 2).remainder(-1))
|
||||
assert_equal(Rational(-1, 2), Rational(-5, 2).remainder(-1))
|
||||
end
|
||||
|
||||
def test_abs
|
||||
assert_equal(Rational(1, 2), Rational(1, 2).abs)
|
||||
assert_equal(Rational(1, 2), Rational(-1, 2).abs)
|
||||
end
|
||||
|
||||
def test_floor_ceil_truncate_round
|
||||
assert_equal( 2, Rational( 5, 2).floor)
|
||||
assert_equal(-3, Rational(-5, 2).floor)
|
||||
assert_equal( 3, Rational( 5, 2).ceil)
|
||||
assert_equal(-2, Rational(-5, 2).ceil)
|
||||
assert_equal( 2, Rational( 5, 2).truncate)
|
||||
assert_equal(-2, Rational(-5, 2).truncate)
|
||||
assert_equal( 3, Rational( 5, 2).round)
|
||||
assert_equal(-3, Rational(-5, 2).round)
|
||||
assert_equal( 1, Rational( 4, 3).round)
|
||||
assert_equal(-1, Rational(-4, 3).round)
|
||||
assert_equal( 2, Rational( 5, 3).round)
|
||||
assert_equal(-2, Rational(-5, 3).round)
|
||||
end
|
||||
|
||||
def test_convert
|
||||
assert_equal(Rational(1, 2), Rational(Complex(1, 0), 2))
|
||||
assert_raise(RangeError) { Rational(Complex(1, 1), 1) }
|
||||
assert_equal(Rational(1, 2), Rational(1, Complex(2, 0)))
|
||||
assert_raise(RangeError) { Rational(1, Complex(2, 1)) }
|
||||
assert_equal(Rational(1, 2), Rational(0.25, 0.5))
|
||||
assert_equal(Rational(1, 2), Rational('1', '2'))
|
||||
end
|
||||
|
||||
def test_add2
|
||||
assert_equal(Rational(2**100, 3), Rational(0, 1) + Rational(2**100, 3))
|
||||
assert_equal(Rational(2, 3**100), Rational(0, 1) + Rational(2, 3**100))
|
||||
end
|
||||
|
||||
def test_div2
|
||||
assert_raise(ZeroDivisionError) { Rational(1, 1) / Rational(0, 1) }
|
||||
end
|
||||
|
||||
def test_to_f2
|
||||
assert_equal(1, Rational(2**5000,3).to_f.infinite?)
|
||||
assert_equal(0, Rational(1, 2**5000).to_f)
|
||||
end
|
||||
|
||||
def test_fixed_bug
|
||||
if defined?(Rational::Unify)
|
||||
assert_instance_of(Fixnum, Rational(1,2) ** 0) # mathn's bug
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue