summaryrefslogtreecommitdiff
path: root/test/ruby/test_rational.rb
diff options
context:
space:
mode:
Diffstat (limited to 'test/ruby/test_rational.rb')
-rw-r--r--test/ruby/test_rational.rb808
1 files changed, 370 insertions, 438 deletions
diff --git a/test/ruby/test_rational.rb b/test/ruby/test_rational.rb
index 0496c0afe3..e0edbde463 100644
--- a/test/ruby/test_rational.rb
+++ b/test/ruby/test_rational.rb
@@ -1,41 +1,29 @@
+# frozen_string_literal: false
require 'test/unit'
class RationalSub < Rational; end
class Rational_Test < Test::Unit::TestCase
- def setup
- @complex = defined?(Complex)
- if @complex
- @keiju = Complex.instance_variables.include?(:@RCS_ID)
- end
- seps = [File::SEPARATOR, File::ALT_SEPARATOR].compact.map{|x| Regexp.escape(x)}.join("|")
- @unify = $".grep(/(?:^|#{seps})mathn(?:\.(?:rb|so))?/).size != 0
- end
-
def test_ratsub
c = RationalSub.__send__(:convert, 1)
assert_kind_of(Numeric, c)
- if @unify
- assert_instance_of(Fixnum, c)
- else
- assert_instance_of(RationalSub, c)
+ assert_instance_of(RationalSub, c)
- c2 = c + 1
- assert_instance_of(RationalSub, c2)
- c2 = c - 1
- assert_instance_of(RationalSub, c2)
+ c2 = c + 1
+ assert_instance_of(RationalSub, c2)
+ c2 = c - 1
+ assert_instance_of(RationalSub, c2)
- c3 = c - c2
- assert_instance_of(RationalSub, c3)
+ c3 = c - c2
+ assert_instance_of(RationalSub, c3)
- s = Marshal.dump(c)
- c5 = Marshal.load(s)
- assert_equal(c, c5)
- assert_instance_of(RationalSub, c5)
- end
+ s = Marshal.dump(c)
+ c5 = Marshal.load(s)
+ assert_equal(c, c5)
+ assert_instance_of(RationalSub, c5)
c1 = Rational(1)
assert_equal(c1.hash, c.hash, '[ruby-dev:38850]')
@@ -47,18 +35,16 @@ class Rational_Test < Test::Unit::TestCase
c2 = Rational(0)
c3 = Rational(1)
- assert_equal(true, c.eql?(c2))
- assert_equal(false, c.eql?(c3))
+ assert_operator(c, :eql?, c2)
+ assert_not_operator(c, :eql?, c3)
- if @unify
- assert_equal(true, c.eql?(0))
- else
- assert_equal(false, c.eql?(0))
- end
+ assert_not_operator(c, :eql?, 0)
end
def test_hash
- assert_instance_of(Fixnum, Rational(1,2).hash)
+ h = Rational(1,2).hash
+ assert_kind_of(Integer, h)
+ assert_nothing_raised {h.to_s}
h = {}
h[Rational(0)] = 0
@@ -75,10 +61,7 @@ class Rational_Test < Test::Unit::TestCase
def test_freeze
c = Rational(1)
- c.freeze
- unless @unify
- assert_equal(true, c.frozen?)
- end
+ assert_predicate(c, :frozen?)
assert_instance_of(String, c.to_s)
end
@@ -111,16 +94,14 @@ class Rational_Test < Test::Unit::TestCase
c = Rational(Rational(1,2),Rational(1,2))
assert_equal(Rational(1), c)
- if @complex && !@keiju
- c = Rational(Complex(1,2),2)
- assert_equal(Complex(Rational(1,2),1), c)
+ c = Rational(Complex(1,2),2)
+ assert_equal(Complex(Rational(1,2),1), c)
- c = Rational(2,Complex(1,2))
- assert_equal(Complex(Rational(2,5),Rational(-4,5)), c)
+ c = Rational(2,Complex(1,2))
+ assert_equal(Complex(Rational(2,5),Rational(-4,5)), c)
- c = Rational(Complex(1,2),Complex(1,2))
- assert_equal(Rational(1), c)
- end
+ c = Rational(Complex(1,2),Complex(1,2))
+ assert_equal(Rational(1), c)
assert_equal(Rational(3),Rational(3))
assert_equal(Rational(1),Rational(3,3))
@@ -129,9 +110,56 @@ class Rational_Test < Test::Unit::TestCase
assert_equal(Rational(3),Rational('3'))
assert_equal(Rational(1),Rational('3.0','3.0'))
assert_equal(Rational(1),Rational('3/3','3/3'))
+ assert_equal(Rational(111, 1), Rational('1.11e+2'))
+ assert_equal(Rational(111, 10), Rational('1.11e+1'))
+ assert_equal(Rational(111, 10), Rational('1.11e1'))
+ assert_equal(Rational(111, 100), Rational('1.11e0'))
+ assert_equal(Rational(111, 1000), Rational('1.11e-1'))
assert_raise(TypeError){Rational(nil)}
assert_raise(ArgumentError){Rational('')}
+
+ EnvUtil.with_default_internal(Encoding::UTF_8) do
+ assert_raise_with_message(ArgumentError, /\u{221a 2668}/) {
+ Rational("\u{221a 2668}")
+ }
+ end
+
+ assert_warning('') {
+ assert_predicate(Rational('1e-99999999999999999999'), :zero?)
+ }
+
assert_raise(TypeError){Rational(Object.new)}
+ assert_raise(TypeError){Rational(Object.new, Object.new)}
+ assert_raise(TypeError){Rational(1, Object.new)}
+
+ bug12485 = '[ruby-core:75995] [Bug #12485]'
+ o = Object.new
+ def o.to_int; 1; end
+ assert_equal(1, Rational(o, 1), bug12485)
+ assert_equal(1, Rational(1, o), bug12485)
+ assert_equal(1, Rational(o, o), bug12485)
+
+ o = Object.new
+ def o.to_r; 1/42r; end
+ assert_equal(1/42r, Rational(o))
+ assert_equal(1/84r, Rational(o, 2))
+ assert_equal(42, Rational(1, o))
+ assert_equal(1, Rational(o, o))
+
+ o = Object.new
+ def o.to_r; nil; end
+ assert_raise(TypeError) { Rational(o) }
+ assert_raise(TypeError) { Rational(o, 2) }
+ assert_raise(TypeError) { Rational(1, o) }
+ assert_raise(TypeError) { Rational(o, o) }
+
+ o = Object.new
+ def o.to_r; raise; end
+ assert_raise(RuntimeError) { Rational(o) }
+ assert_raise(RuntimeError) { Rational(o, 2) }
+ assert_raise(RuntimeError) { Rational(1, o) }
+ assert_raise(RuntimeError) { Rational(o, o) }
+
assert_raise(ArgumentError){Rational()}
assert_raise(ArgumentError){Rational(1,2,3)}
@@ -141,6 +169,14 @@ class Rational_Test < Test::Unit::TestCase
if (1.0/0).infinite?
assert_raise(FloatDomainError){Rational(1.0/0)}
end
+
+ bug16518 = "[ruby-core:96942] [Bug #16518]"
+ cls = Class.new(Numeric) do
+ def /(y); 42; end
+ def to_r; 1r; end
+ def to_int; 1; end
+ end
+ assert_equal(1/2r, Rational(cls.new, 2), bug16518)
end
def test_attr
@@ -178,57 +214,15 @@ class Rational_Test < Test::Unit::TestCase
def test_attr2
c = Rational(1)
- if @unify
-=begin
- assert_equal(true, c.finite?)
- assert_equal(false, c.infinite?)
- assert_equal(false, c.nan?)
- assert_equal(true, c.integer?)
- assert_equal(false, c.float?)
- assert_equal(true, c.rational?)
-=end
- assert_equal(true, c.real?)
-=begin
- assert_equal(false, c.complex?)
- assert_equal(true, c.exact?)
- assert_equal(false, c.inexact?)
-=end
- else
-=begin
- assert_equal(true, c.finite?)
- assert_equal(false, c.infinite?)
- assert_equal(false, c.nan?)
- assert_equal(false, c.integer?)
- assert_equal(false, c.float?)
- assert_equal(true, c.rational?)
-=end
- assert_equal(true, c.real?)
-=begin
- assert_equal(false, c.complex?)
- assert_equal(true, c.exact?)
- assert_equal(false, c.inexact?)
-=end
- end
+ assert_not_predicate(c, :integer?)
+ assert_predicate(c, :real?)
+
+ assert_predicate(Rational(0), :zero?)
+ assert_predicate(Rational(0,1), :zero?)
+ assert_not_predicate(Rational(1,1), :zero?)
-=begin
- assert_equal(true, Rational(0).positive?)
- assert_equal(true, Rational(1).positive?)
- assert_equal(false, Rational(-1).positive?)
- assert_equal(false, Rational(0).negative?)
- assert_equal(false, Rational(1).negative?)
- assert_equal(true, Rational(-1).negative?)
-
- assert_equal(0, Rational(0).sign)
- assert_equal(1, Rational(2).sign)
- assert_equal(-1, Rational(-2).sign)
-=end
-
- assert_equal(true, Rational(0).zero?)
- assert_equal(true, Rational(0,1).zero?)
- assert_equal(false, Rational(1,1).zero?)
-
- assert_equal(nil, Rational(0).nonzero?)
- assert_equal(nil, Rational(0,1).nonzero?)
+ assert_nil(Rational(0).nonzero?)
+ assert_nil(Rational(0,1).nonzero?)
assert_equal(Rational(1,1), Rational(1,1).nonzero?)
end
@@ -248,12 +242,6 @@ class Rational_Test < Test::Unit::TestCase
assert_equal(Rational(1,1), -Rational(-1,1))
assert_equal(Rational(1,1), -Rational(1,-1))
assert_equal(Rational(-1,1), -Rational(-1,-1))
-
-=begin
- assert_equal(0, Rational(0).negate)
- assert_equal(-2, Rational(2).negate)
- assert_equal(2, Rational(-2).negate)
-=end
end
def test_add
@@ -299,7 +287,7 @@ class Rational_Test < Test::Unit::TestCase
assert_raise(ZeroDivisionError){Rational(1, 3) / Rational(0)}
assert_equal(0, Rational(1, 3) / Float::INFINITY)
- assert((Rational(1, 3) / 0.0).infinite?, '[ruby-core:31626]')
+ assert_predicate(Rational(1, 3) / 0.0, :infinite?, '[ruby-core:31626]')
end
def assert_eql(exp, act, *args)
@@ -341,15 +329,13 @@ class Rational_Test < Test::Unit::TestCase
assert_equal(-2, (-c).div(c2))
assert_equal(1, (-c).div(-c2))
- unless @unify
- c = Rational(11)
- c2 = Rational(3)
+ c = Rational(11)
+ c2 = Rational(3)
- assert_equal(3, c.div(c2))
- assert_equal(-4, c.div(-c2))
- assert_equal(-4, (-c).div(c2))
- assert_equal(3, (-c).div(-c2))
- end
+ assert_equal(3, c.div(c2))
+ assert_equal(-4, c.div(-c2))
+ assert_equal(-4, (-c).div(c2))
+ assert_equal(3, (-c).div(-c2))
end
def test_modulo
@@ -376,15 +362,13 @@ class Rational_Test < Test::Unit::TestCase
assert_equal(Rational(99,100), (-c).modulo(c2))
assert_equal(Rational(-101,100), (-c).modulo(-c2))
- unless @unify
- c = Rational(11)
- c2 = Rational(3)
+ c = Rational(11)
+ c2 = Rational(3)
- assert_equal(2, c.modulo(c2))
- assert_equal(-1, c.modulo(-c2))
- assert_equal(1, (-c).modulo(c2))
- assert_equal(-2, (-c).modulo(-c2))
- end
+ assert_equal(2, c.modulo(c2))
+ assert_equal(-1, c.modulo(-c2))
+ assert_equal(1, (-c).modulo(c2))
+ assert_equal(-2, (-c).modulo(-c2))
end
def test_divmod
@@ -411,53 +395,14 @@ class Rational_Test < Test::Unit::TestCase
assert_equal([-2, Rational(99,100)], (-c).divmod(c2))
assert_equal([1, Rational(-101,100)], (-c).divmod(-c2))
- unless @unify
- c = Rational(11)
- c2 = Rational(3)
-
- assert_equal([3,2], c.divmod(c2))
- assert_equal([-4,-1], c.divmod(-c2))
- assert_equal([-4,1], (-c).divmod(c2))
- assert_equal([3,-2], (-c).divmod(-c2))
- end
- end
-
-=begin
- def test_quot
- c = Rational(1,2)
- c2 = Rational(2,3)
-
- assert_eql(0, c.quot(c2))
- assert_eql(0, c.quot(2))
- assert_eql(0, c.quot(2.0))
-
- c = Rational(301,100)
- c2 = Rational(7,5)
-
- assert_equal(2, c.quot(c2))
- assert_equal(-2, c.quot(-c2))
- assert_equal(-2, (-c).quot(c2))
- assert_equal(2, (-c).quot(-c2))
-
- c = Rational(301,100)
- c2 = Rational(2)
-
- assert_equal(1, c.quot(c2))
- assert_equal(-1, c.quot(-c2))
- assert_equal(-1, (-c).quot(c2))
- assert_equal(1, (-c).quot(-c2))
-
- unless @unify
- c = Rational(11)
- c2 = Rational(3)
+ c = Rational(11)
+ c2 = Rational(3)
- assert_equal(3, c.quot(c2))
- assert_equal(-3, c.quot(-c2))
- assert_equal(-3, (-c).quot(c2))
- assert_equal(3, (-c).quot(-c2))
- end
+ assert_equal([3,2], c.divmod(c2))
+ assert_equal([-4,-1], c.divmod(-c2))
+ assert_equal([-4,1], (-c).divmod(c2))
+ assert_equal([3,-2], (-c).divmod(-c2))
end
-=end
def test_remainder
c = Rational(1,2)
@@ -483,53 +428,14 @@ class Rational_Test < Test::Unit::TestCase
assert_equal(Rational(-101,100), (-c).remainder(c2))
assert_equal(Rational(-101,100), (-c).remainder(-c2))
- unless @unify
- c = Rational(11)
- c2 = Rational(3)
-
- assert_equal(2, c.remainder(c2))
- assert_equal(2, c.remainder(-c2))
- assert_equal(-2, (-c).remainder(c2))
- assert_equal(-2, (-c).remainder(-c2))
- end
- end
-
-=begin
- def test_quotrem
- c = Rational(1,2)
- c2 = Rational(2,3)
-
- assert_eql([0, Rational(1,2)], c.quotrem(c2))
- assert_eql([0, Rational(1,2)], c.quotrem(2))
- assert_eql([0, 0.5], c.quotrem(2.0))
-
- c = Rational(301,100)
- c2 = Rational(7,5)
-
- assert_equal([2, Rational(21,100)], c.quotrem(c2))
- assert_equal([-2, Rational(21,100)], c.quotrem(-c2))
- assert_equal([-2, Rational(-21,100)], (-c).quotrem(c2))
- assert_equal([2, Rational(-21,100)], (-c).quotrem(-c2))
-
- c = Rational(301,100)
- c2 = Rational(2)
+ c = Rational(11)
+ c2 = Rational(3)
- assert_equal([1, Rational(101,100)], c.quotrem(c2))
- assert_equal([-1, Rational(101,100)], c.quotrem(-c2))
- assert_equal([-1, Rational(-101,100)], (-c).quotrem(c2))
- assert_equal([1, Rational(-101,100)], (-c).quotrem(-c2))
-
- unless @unify
- c = Rational(11)
- c2 = Rational(3)
-
- assert_equal([3,2], c.quotrem(c2))
- assert_equal([-3,2], c.quotrem(-c2))
- assert_equal([-3,-2], (-c).quotrem(c2))
- assert_equal([3,-2], (-c).quotrem(-c2))
- end
+ assert_equal(2, c.remainder(c2))
+ assert_equal(2, c.remainder(-c2))
+ assert_equal(-2, (-c).remainder(c2))
+ assert_equal(-2, (-c).remainder(-c2))
end
-=end
def test_quo
c = Rational(1,2)
@@ -550,7 +456,7 @@ class Rational_Test < Test::Unit::TestCase
assert_equal(0.25, c.fdiv(2))
assert_equal(0.25, c.fdiv(2.0))
assert_equal(0, c.fdiv(Float::INFINITY))
- assert(c.fdiv(0).infinite?, '[ruby-core:31626]')
+ assert_predicate(c.fdiv(0), :infinite?, '[ruby-core:31626]')
end
def test_expt
@@ -578,50 +484,38 @@ class Rational_Test < Test::Unit::TestCase
# p ** p
x = 2 ** Rational(2)
assert_equal(Rational(4), x)
- unless @unify
- assert_instance_of(Rational, x)
- end
+ assert_instance_of(Rational, x)
assert_equal(4, x.numerator)
assert_equal(1, x.denominator)
x = Rational(2) ** 2
assert_equal(Rational(4), x)
- unless @unify
- assert_instance_of(Rational, x)
- end
+ assert_instance_of(Rational, x)
assert_equal(4, x.numerator)
assert_equal(1, x.denominator)
x = Rational(2) ** Rational(2)
assert_equal(Rational(4), x)
- unless @unify
- assert_instance_of(Rational, x)
- end
+ assert_instance_of(Rational, x)
assert_equal(4, x.numerator)
assert_equal(1, x.denominator)
# -p ** p
x = (-2) ** Rational(2)
assert_equal(Rational(4), x)
- unless @unify
- assert_instance_of(Rational, x)
- end
+ assert_instance_of(Rational, x)
assert_equal(4, x.numerator)
assert_equal(1, x.denominator)
x = Rational(-2) ** 2
assert_equal(Rational(4), x)
- unless @unify
- assert_instance_of(Rational, x)
- end
+ assert_instance_of(Rational, x)
assert_equal(4, x.numerator)
assert_equal(1, x.denominator)
x = Rational(-2) ** Rational(2)
assert_equal(Rational(4), x)
- unless @unify
- assert_instance_of(Rational, x)
- end
+ assert_instance_of(Rational, x)
assert_equal(4, x.numerator)
assert_equal(1, x.denominator)
@@ -663,9 +557,7 @@ class Rational_Test < Test::Unit::TestCase
assert_equal(1, x.numerator)
assert_equal(4, x.denominator)
- unless @unify # maybe bug mathn
- assert_raise(ZeroDivisionError){0 ** -1}
- end
+ assert_raise(ZeroDivisionError){0 ** -1}
end
def test_cmp
@@ -698,23 +590,23 @@ class Rational_Test < Test::Unit::TestCase
assert_equal(-1, Rational(b-1) <=> Rational(b))
assert_equal(+1, Rational(b) <=> Rational(b-1))
- assert_equal(false, Rational(0) < Rational(0))
- assert_equal(true, Rational(0) <= Rational(0))
- assert_equal(true, Rational(0) >= Rational(0))
- assert_equal(false, Rational(0) > Rational(0))
+ assert_not_operator(Rational(0), :<, Rational(0))
+ assert_operator(Rational(0), :<=, Rational(0))
+ assert_operator(Rational(0), :>=, Rational(0))
+ assert_not_operator(Rational(0), :>, Rational(0))
- assert_equal(nil, Rational(0) <=> nil)
- assert_equal(nil, Rational(0) <=> 'foo')
+ assert_nil(Rational(0) <=> nil)
+ assert_nil(Rational(0) <=> 'foo')
end
def test_eqeq
- assert(Rational(1,1) == Rational(1))
- assert(Rational(-1,1) == Rational(-1))
+ assert_equal(Rational(1,1), Rational(1))
+ assert_equal(Rational(-1,1), Rational(-1))
- assert_equal(false, Rational(2,1) == Rational(1))
- assert_equal(true, Rational(2,1) != Rational(1))
- assert_equal(false, Rational(1) == nil)
- assert_equal(false, Rational(1) == '')
+ assert_not_operator(Rational(2,1), :==, Rational(1))
+ assert_operator(Rational(2,1), :!=, Rational(1))
+ assert_not_operator(Rational(1), :==, nil)
+ assert_not_operator(Rational(1), :==, '')
end
def test_coerce
@@ -722,13 +614,20 @@ class Rational_Test < Test::Unit::TestCase
assert_equal([Rational(2.2),Rational(1)], Rational(1).coerce(2.2))
assert_equal([Rational(2),Rational(1)], Rational(1).coerce(Rational(2)))
- assert_nothing_raised(TypeError, '[Bug #5020] [ruby-devl:44088]') do
+ assert_nothing_raised(TypeError, '[Bug #5020] [ruby-dev:44088]') do
Rational(1,2).coerce(Complex(1,1))
end
+
+ assert_raise(ZeroDivisionError) do
+ 1 / 0r.coerce(0+0i)[0]
+ end
+ assert_raise(ZeroDivisionError) do
+ 1 / 0r.coerce(0.0+0i)[0]
+ end
end
class ObjectX
- def + (x) Rational(1) end
+ def +(x) Rational(1) end
alias - +
alias * +
alias / +
@@ -747,42 +646,32 @@ class Rational_Test < Test::Unit::TestCase
end
end
- def test_unify
- if @unify
- assert_instance_of(Fixnum, Rational(1,2) + Rational(1,2))
- assert_instance_of(Fixnum, Rational(1,2) - Rational(1,2))
- assert_instance_of(Fixnum, Rational(1,2) * 2)
- assert_instance_of(Fixnum, Rational(1,2) / Rational(1,2))
- assert_instance_of(Fixnum, Rational(1,2).div(Rational(1,2)))
- assert_instance_of(Fixnum, Rational(1,2).quo(Rational(1,2)))
- assert_instance_of(Fixnum, Rational(1,2) ** -2)
- end
- end
-
def test_math
assert_equal(Rational(1,2), Rational(1,2).abs)
assert_equal(Rational(1,2), Rational(-1,2).abs)
- if @complex && !@keiju
- assert_equal(Rational(1,2), Rational(1,2).magnitude)
- assert_equal(Rational(1,2), Rational(-1,2).magnitude)
- end
+ assert_equal(Rational(1,2), Rational(1,2).magnitude)
+ assert_equal(Rational(1,2), Rational(-1,2).magnitude)
assert_equal(1, Rational(1,2).numerator)
assert_equal(2, Rational(1,2).denominator)
end
def test_trunc
- [[Rational(13, 5), [ 2, 3, 2, 3]], # 2.6
- [Rational(5, 2), [ 2, 3, 2, 3]], # 2.5
- [Rational(12, 5), [ 2, 3, 2, 2]], # 2.4
- [Rational(-12,5), [-3, -2, -2, -2]], # -2.4
- [Rational(-5, 2), [-3, -2, -2, -3]], # -2.5
- [Rational(-13, 5), [-3, -2, -2, -3]], # -2.6
+ [[Rational(13, 5), [ 2, 3, 2, 3, 3, 3, 3]], # 2.6
+ [Rational(5, 2), [ 2, 3, 2, 3, 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, -3, -2, -3, -2]], # -2.5
+ [Rational(-13, 5), [-3, -2, -2, -3, -3, -3, -3]], # -2.6
].each do |i, a|
- assert_equal(a[0], i.floor)
- assert_equal(a[1], i.ceil)
- assert_equal(a[2], i.truncate)
- assert_equal(a[3], i.round)
+ s = proc {i.inspect}
+ assert_equal(a[0], i.floor, s)
+ assert_equal(a[1], i.ceil, s)
+ assert_equal(a[2], i.truncate, s)
+ 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
@@ -792,13 +681,8 @@ class Rational_Test < Test::Unit::TestCase
assert_instance_of(String, c.to_s)
assert_equal('1/2', c.to_s)
- if @unify
- assert_equal('0', Rational(0,2).to_s)
- assert_equal('0', Rational(0,-2).to_s)
- else
- assert_equal('0/1', Rational(0,2).to_s)
- assert_equal('0/1', Rational(0,-2).to_s)
- end
+ assert_equal('0/1', Rational(0,2).to_s)
+ assert_equal('0/1', Rational(0,-2).to_s)
assert_equal('1/2', Rational(1,2).to_s)
assert_equal('-1/2', Rational(-1,2).to_s)
assert_equal('1/2', Rational(-1,-2).to_s)
@@ -815,22 +699,23 @@ class Rational_Test < Test::Unit::TestCase
def test_marshal
c = Rational(1,2)
- c.instance_eval{@ivar = 9}
s = Marshal.dump(c)
c2 = Marshal.load(s)
assert_equal(c, c2)
- assert_equal(9, c2.instance_variable_get(:@ivar))
assert_instance_of(Rational, c2)
+ assert_raise(TypeError){
+ Marshal.load("\x04\bU:\rRational[\ai\x060")
+ }
+
assert_raise(ZeroDivisionError){
Marshal.load("\x04\bU:\rRational[\ai\x06i\x05")
}
bug3656 = '[ruby-core:31622]'
c = Rational(1,2)
- c.freeze
- assert(c.frozen?)
+ assert_predicate(c, :frozen?)
result = c.marshal_load([2,3]) rescue :fail
assert_equal(:fail, result, bug3656)
end
@@ -841,132 +726,163 @@ class Rational_Test < Test::Unit::TestCase
assert_nothing_raised(bug6625) do
assert_equal(Rational(1, 2), Marshal.load(dump), bug6625)
end
+ dump = "\x04\x08o:\x0dRational\x07:\x11@denominatori\x07:\x0f@numerator0"
+ assert_raise(TypeError) do
+ Marshal.load(dump)
+ end
+ end
+
+ def assert_valid_rational(n, d, r)
+ x = Rational(n, d)
+ assert_equal(x, r.to_r, "#{r.dump}.to_r")
+ assert_equal(x, Rational(r), "Rational(#{r.dump})")
+ end
+
+ def assert_invalid_rational(n, d, r)
+ x = Rational(n, d)
+ assert_equal(x, r.to_r, "#{r.dump}.to_r")
+ assert_raise(ArgumentError, "Rational(#{r.dump})") {Rational(r)}
end
def test_parse
- assert_equal(Rational(5), '5'.to_r)
- assert_equal(Rational(-5), '-5'.to_r)
- assert_equal(Rational(5,3), '5/3'.to_r)
- assert_equal(Rational(-5,3), '-5/3'.to_r)
-# assert_equal(Rational(5,-3), '5/-3'.to_r)
-# assert_equal(Rational(-5,-3), '-5/-3'.to_r)
-
- assert_equal(Rational(5), '5.0'.to_r)
- assert_equal(Rational(-5), '-5.0'.to_r)
- assert_equal(Rational(5,3), '5.0/3'.to_r)
- assert_equal(Rational(-5,3), '-5.0/3'.to_r)
-# assert_equal(Rational(5,-3), '5.0/-3'.to_r)
-# assert_equal(Rational(-5,-3), '-5.0/-3'.to_r)
-
- assert_equal(Rational(5), '5e0'.to_r)
- assert_equal(Rational(-5), '-5e0'.to_r)
- assert_equal(Rational(5,3), '5e0/3'.to_r)
- assert_equal(Rational(-5,3), '-5e0/3'.to_r)
-# assert_equal(Rational(5,-3), '5e0/-3'.to_r)
-# assert_equal(Rational(-5,-3), '-5e0/-3'.to_r)
-
- assert_equal(Rational(5e1), '5e1'.to_r)
- assert_equal(Rational(-5e2), '-5e2'.to_r)
- assert_equal(Rational(5e3,3), '5e003/3'.to_r)
- assert_equal(Rational(-5e4,3), '-5e004/3'.to_r)
-# assert_equal(Rational(5e1,-3), '5e1/-3'.to_r)
-# assert_equal(Rational(-5e2,-3), '-5e2/-3'.to_r)
-
- assert_equal(Rational(33,100), '.33'.to_r)
- assert_equal(Rational(33,100), '0.33'.to_r)
- assert_equal(Rational(-33,100), '-.33'.to_r)
- assert_equal(Rational(-33,100), '-0.33'.to_r)
- assert_equal(Rational(-33,100), '-0.3_3'.to_r)
-
- assert_equal(Rational(1,2), '5e-1'.to_r)
- assert_equal(Rational(50), '5e+1'.to_r)
- assert_equal(Rational(1,2), '5.0e-1'.to_r)
- assert_equal(Rational(50), '5.0e+1'.to_r)
- assert_equal(Rational(50), '5e1'.to_r)
- assert_equal(Rational(50), '5E1'.to_r)
- assert_equal(Rational(500), '5e2'.to_r)
- assert_equal(Rational(5000), '5e3'.to_r)
- assert_equal(Rational(500000000000), '5e1_1'.to_r)
-
- assert_equal(Rational(5), Rational('5'))
- assert_equal(Rational(-5), Rational('-5'))
- assert_equal(Rational(5,3), Rational('5/3'))
- assert_equal(Rational(-5,3), Rational('-5/3'))
-# assert_equal(Rational(5,-3), Rational('5/-3'))
-# assert_equal(Rational(-5,-3), Rational('-5/-3'))
-
- assert_equal(Rational(5), Rational('5.0'))
- assert_equal(Rational(-5), Rational('-5.0'))
- assert_equal(Rational(5,3), Rational('5.0/3'))
- assert_equal(Rational(-5,3), Rational('-5.0/3'))
-# assert_equal(Rational(5,-3), Rational('5.0/-3'))
-# assert_equal(Rational(-5,-3), Rational('-5.0/-3'))
-
- assert_equal(Rational(5), Rational('5e0'))
- assert_equal(Rational(-5), Rational('-5e0'))
- assert_equal(Rational(5,3), Rational('5e0/3'))
- assert_equal(Rational(-5,3), Rational('-5e0/3'))
-# assert_equal(Rational(5,-3), Rational('5e0/-3'))
-# assert_equal(Rational(-5,-3), Rational('-5e0/-3'))
-
- assert_equal(Rational(5e1), Rational('5e1'))
- assert_equal(Rational(-5e2), Rational('-5e2'))
- assert_equal(Rational(5e3,3), Rational('5e003/3'))
- assert_equal(Rational(-5e4,3), Rational('-5e004/3'))
-# assert_equal(Rational(5e1,-3), Rational('5e1/-3'))
-# assert_equal(Rational(-5e2,-3), Rational('-5e2/-3'))
-
- assert_equal(Rational(33,100), Rational('.33'))
- assert_equal(Rational(33,100), Rational('0.33'))
- assert_equal(Rational(-33,100), Rational('-.33'))
- assert_equal(Rational(-33,100), Rational('-0.33'))
- assert_equal(Rational(-33,100), Rational('-0.3_3'))
-
- assert_equal(Rational(1,2), Rational('5e-1'))
- assert_equal(Rational(50), Rational('5e+1'))
- assert_equal(Rational(1,2), Rational('5.0e-1'))
- assert_equal(Rational(50), Rational('5.0e+1'))
- assert_equal(Rational(50), Rational('5e1'))
- assert_equal(Rational(50), Rational('5E1'))
- assert_equal(Rational(500), Rational('5e2'))
- assert_equal(Rational(5000), Rational('5e3'))
- assert_equal(Rational(500000000000), Rational('5e1_1'))
-
- assert_equal(Rational(0), ''.to_r)
- assert_equal(Rational(0), ' '.to_r)
- assert_equal(Rational(5), "\f\n\r\t\v5\0".to_r)
- assert_equal(Rational(0), '_'.to_r)
- assert_equal(Rational(0), '_5'.to_r)
- assert_equal(Rational(5), '5_'.to_r)
- assert_equal(Rational(5), '5x'.to_r)
- assert_equal(Rational(5), '5/_3'.to_r)
- assert_equal(Rational(5,3), '5/3_'.to_r)
- assert_equal(Rational(5,3), '5/3.3'.to_r)
- assert_equal(Rational(5,3), '5/3x'.to_r)
- assert_raise(ArgumentError){ Rational('')}
- assert_raise(ArgumentError){ Rational('_')}
- assert_raise(ArgumentError){ Rational("\f\n\r\t\v5\0")}
- assert_raise(ArgumentError){ Rational('_5')}
- assert_raise(ArgumentError){ Rational('5_')}
- assert_raise(ArgumentError){ Rational('5x')}
- assert_raise(ArgumentError){ Rational('5/_3')}
- assert_raise(ArgumentError){ Rational('5/3_')}
- assert_raise(ArgumentError){ Rational('5/3.3')}
- assert_raise(ArgumentError){ Rational('5/3x')}
+ ok = method(:assert_valid_rational)
+ ng = method(:assert_invalid_rational)
+
+ ok[ 5, 1, '5']
+ ok[-5, 1, '-5']
+ ok[ 5, 3, '5/3']
+ ok[-5, 3, '-5/3']
+ ok[ 5, 3, '5_5/33']
+ ok[ 5,33, '5/3_3']
+ ng[ 5, 1, '5__5/33']
+ ng[ 5, 3, '5/3__3']
+
+ ok[ 5, 1, '5.0']
+ ok[-5, 1, '-5.0']
+ ok[ 5, 3, '5.0/3']
+ ok[-5, 3, '-5.0/3']
+ ok[ 501,100, '5.0_1']
+ ok[ 501,300, '5.0_1/3']
+ ok[ 5,33, '5.0/3_3']
+ ng[ 5, 1, '5.0__1/3']
+ ng[ 5, 3, '5.0/3__3']
+
+ ok[ 5, 1, '5e0']
+ ok[-5, 1, '-5e0']
+ ok[ 5, 3, '5e0/3']
+ ok[-5, 3, '-5e0/3']
+ ok[550, 1, '5_5e1']
+ ng[ 5, 1, '5_e1']
+
+ ok[ 5e1, 1, '5e1']
+ ok[-5e2, 1, '-5e2']
+ ok[ 5e3, 3, '5e003/3']
+ ok[-5e4, 3, '-5e004/3']
+ ok[ 5e3, 1, '5e0_3']
+ ok[ 5e1,33, '5e1/3_3']
+ ng[ 5e0, 1, '5e0__3/3']
+ ng[ 5e1, 3, '5e1/3__3']
+
+ ok[ 33, 100, '.33']
+ ok[ 33, 100, '0.33']
+ ok[-33, 100, '-.33']
+ ok[-33, 100, '-0.33']
+ ok[-33, 100, '-0.3_3']
+ ng[ -3, 10, '-0.3__3']
+
+ ok[ 1, 2, '5e-1']
+ ok[50, 1, '5e+1']
+ ok[ 1, 2, '5.0e-1']
+ ok[50, 1, '5.0e+1']
+ ok[50, 1, '5e1']
+ ok[50, 1, '5E1']
+ ok[500, 1, '5e2']
+ ok[5000, 1, '5e3']
+ ok[500000000000, 1, '5e1_1']
+ ng[ 5, 1, '5e']
+ ng[ 5, 1, '5e_']
+ ng[ 5, 1, '5e_1']
+ ng[50, 1, '5e1_']
+
+ ok[ 50, 33, '5/3.3']
+ ok[ 5, 3, '5/3e0']
+ ok[ 5, 30, '5/3e1']
+ ng[ 5, 3, '5/3._3']
+ ng[ 50, 33, '5/3.3_']
+ ok[500,333, '5/3.3_3']
+ ng[ 5, 3, '5/3e']
+ ng[ 5, 3, '5/3_e']
+ ng[ 5, 3, '5/3e_']
+ ng[ 5, 3, '5/3e_1']
+ ng[ 5, 30, '5/3e1_']
+ ok[ 5, 300000000000, '5/3e1_1']
+
+ ng[0, 1, '']
+ ng[0, 1, ' ']
+ ng[5, 1, "\f\n\r\t\v5\0"]
+ ng[0, 1, '_']
+ ng[0, 1, '_5']
+ ng[5, 1, '5_']
+ ng[5, 1, '5x']
+ ng[5, 1, '5/_3']
+ ng[5, 3, '5/3_']
+ ng[5, 3, '5/3x']
+
+ ng[5, 1, '5/-3']
+ end
+
+ def test_parse_zero_denominator
+ assert_raise(ZeroDivisionError) {"1/0".to_r}
+ assert_raise(ZeroDivisionError) {Rational("1/0")}
end
-=begin
- def test_reciprocal
- assert_equal(Rational(1,9), Rational(9,1).reciprocal)
- assert_equal(Rational(9,1), Rational(1,9).reciprocal)
- assert_equal(Rational(-1,9), Rational(-9,1).reciprocal)
- assert_equal(Rational(-9,1), Rational(-1,9).reciprocal)
- assert_equal(Rational(1,9), Rational(9,1).inverse)
- assert_equal(Rational(9,1), Rational(1,9).inverse)
- assert_equal(Rational(-1,9), Rational(-9,1).inverse)
- assert_equal(Rational(-9,1), Rational(-1,9).inverse)
+ def test_Rational_with_invalid_exception
+ assert_raise(ArgumentError) {
+ Rational("1/1", exception: 1)
+ }
+ end
+
+ def test_Rational_without_exception
+ assert_nothing_raised(ArgumentError) {
+ assert_equal(nil, Rational("5/3x", exception: false))
+ }
+ assert_nothing_raised(ZeroDivisionError) {
+ assert_equal(nil, Rational("1/0", exception: false))
+ }
+ assert_nothing_raised(TypeError) {
+ assert_equal(nil, Rational(nil, exception: false))
+ }
+ assert_nothing_raised(TypeError) {
+ assert_equal(nil, Rational(Object.new, exception: false))
+ }
+ assert_nothing_raised(TypeError) {
+ assert_equal(nil, Rational(1, nil, exception: false))
+ }
+ assert_nothing_raised(TypeError) {
+ assert_equal(nil, Rational(1, Object.new, exception: false))
+ }
+
+ bug12485 = '[ruby-core:75995] [Bug #12485]'
+ assert_nothing_raised(RuntimeError, bug12485) {
+ o = Object.new
+ def o.to_int; raise; end
+ assert_equal(nil, Rational(o, exception: false))
+ }
+ assert_nothing_raised(RuntimeError, bug12485) {
+ o = Object.new
+ def o.to_int; raise; end
+ assert_equal(nil, Rational(1, o, exception: false))
+ }
+
+ o = Object.new;
+ def o.to_r; raise; end
+ assert_nothing_raised(RuntimeError) {
+ assert_equal(nil, Rational(o, exception: false))
+ }
+ assert_nothing_raised(TypeError) {
+ assert_equal(nil, Rational(1, o, exception: false))
+ }
end
-=end
def test_to_i
assert_equal(1, Rational(3,2).to_i)
@@ -976,18 +892,12 @@ class Rational_Test < Test::Unit::TestCase
def test_to_f
assert_equal(1.5, Rational(3,2).to_f)
assert_equal(1.5, Float(Rational(3,2)))
+ assert_equal(1e-23, Rational(1, 10**23).to_f, "Bug #14637")
end
def test_to_c
- if @complex && !@keiju
- if @unify
- assert_equal(Rational(3,2), Rational(3,2).to_c)
- assert_equal(Rational(3,2), Complex(Rational(3,2)))
- else
- assert_equal(Complex(Rational(3,2)), Rational(3,2).to_c)
- assert_equal(Complex(Rational(3,2)), Complex(Rational(3,2)))
- end
- end
+ assert_equal(Complex(Rational(3,2)), Rational(3,2).to_c)
+ assert_equal(Complex(Rational(3,2)), Complex(Rational(3,2)))
end
def test_to_r
@@ -1007,13 +917,7 @@ class Rational_Test < Test::Unit::TestCase
c = Rational(1,2).to_r
assert_equal([1,2], [c.numerator, c.denominator])
- if @complex
- if @keiju
- assert_raise(NoMethodError){Complex(1,2).to_r}
- else
- assert_raise(RangeError){Complex(1,2).to_r}
- end
- end
+ assert_raise(RangeError){Complex(1,2).to_r}
if (0.0/0).nan?
assert_raise(FloatDomainError){(0.0/0).to_r}
@@ -1063,12 +967,7 @@ class Rational_Test < Test::Unit::TestCase
assert_equal(r.rationalize(Rational(1,10)), Rational(-1,3))
assert_equal(r.rationalize(Rational(-1,10)), Rational(-1,3))
- if @complex
- if @keiju
- else
- assert_raise(RangeError){Complex(1,2).rationalize}
- end
- end
+ assert_raise(RangeError){Complex(1,2).rationalize}
if (0.0/0).nan?
assert_raise(FloatDomainError){(0.0/0).rationalize}
@@ -1095,9 +994,19 @@ class Rational_Test < Test::Unit::TestCase
assert_equal(1152921470247108503, 1073741789.lcm(1073741827))
end
+ def test_gcd_no_memory_leak
+ assert_no_memory_leak([], "#{<<-"begin;"}", "#{<<-"end;"}", limit: 1.2, rss: true)
+ x = (1<<121) + 1
+ y = (1<<99) + 1
+ 1000.times{x.gcd(y)}
+ begin;
+ 100.times {1000.times{x.gcd(y)}}
+ end;
+ end
+
def test_supp
- assert_equal(true, 1.real?)
- assert_equal(true, 1.1.real?)
+ assert_predicate(1, :real?)
+ assert_predicate(1.1, :real?)
assert_equal(1, 1.numerator)
assert_equal(9, 9.numerator)
@@ -1109,24 +1018,24 @@ class Rational_Test < Test::Unit::TestCase
assert_equal(1.0, 1.0.denominator)
assert_equal(1.0, 9.0.denominator)
-=begin
- assert_equal(Rational(1,9), 9.reciprocal)
- assert_in_delta(0.1111, 9.0.reciprocal, 0.001)
- assert_equal(Rational(1,9), 9.inverse)
- assert_in_delta(0.1111, 9.0.inverse, 0.001)
-=end
-
assert_equal(Rational(1,2), 1.quo(2))
assert_equal(Rational(5000000000), 10000000000.quo(2))
assert_equal(0.5, 1.0.quo(2))
assert_equal(Rational(1,4), Rational(1,2).quo(2))
assert_equal(0, Rational(1,2).quo(Float::INFINITY))
- assert(Rational(1,2).quo(0.0).infinite?, '[ruby-core:31626]')
+ assert_predicate(Rational(1,2).quo(0.0), :infinite?, '[ruby-core:31626]')
assert_equal(0.5, 1.fdiv(2))
assert_equal(5000000000.0, 10000000000.fdiv(2))
assert_equal(0.5, 1.0.fdiv(2))
assert_equal(0.25, Rational(1,2).fdiv(2))
+
+ a = 0xa42fcabf_c51ce400_00001000_00000000_00000000_00000000_00000000_00000000
+ b = 1<<1074
+ assert_equal(Rational(a, b).to_f, a.fdiv(b))
+ a = 3
+ b = 0x20_0000_0000_0001
+ assert_equal(Rational(a, b).to_f, a.fdiv(b))
end
def test_ruby19
@@ -1135,12 +1044,9 @@ class Rational_Test < Test::Unit::TestCase
end
def test_fixed_bug
- if @unify
- assert_instance_of(Fixnum, Rational(1,2) ** 0) # mathn's bug
- end
-
n = Float::MAX.to_i * 2
- assert_equal(1.0, Rational(n + 2, n + 1).to_f, '[ruby-dev:33852]')
+ x = EnvUtil.suppress_warning {Rational(n + 2, n + 1).to_f}
+ assert_equal(1.0, x, '[ruby-dev:33852]')
end
def test_power_of_1_and_minus_1
@@ -1149,7 +1055,7 @@ class Rational_Test < Test::Unit::TestCase
one = Rational( 1, 1)
assert_eql one, one ** -big , bug5715
assert_eql one, (-one) ** -big , bug5715
- assert_eql -one, (-one) ** -(big+1) , bug5715
+ assert_eql (-one), (-one) ** -(big+1) , bug5715
assert_equal Complex, ((-one) ** Rational(1,3)).class
end
@@ -1163,7 +1069,33 @@ class Rational_Test < Test::Unit::TestCase
assert_raise(ZeroDivisionError, bug5713) { Rational(0, 1) ** Rational(-2,3) }
end
+ def test_power_overflow
+ assert_raise(ArgumentError) { 4r**400000000000000000000 }
+ exp = 4**40000000
+ assert_equal exp, 4r**40000000
+ assert_equal 1r/exp, (1/4r)**40000000
+ end
+
+ def test_positive_p
+ assert_predicate(1/2r, :positive?)
+ assert_not_predicate(-1/2r, :positive?)
+ end
+
+ def test_negative_p
+ assert_predicate(-1/2r, :negative?)
+ assert_not_predicate(1/2r, :negative?)
+ end
+
def test_known_bug
end
+ def test_finite_p
+ assert_predicate(1/2r, :finite?)
+ assert_predicate(-1/2r, :finite?)
+ end
+
+ def test_infinite_p
+ assert_nil((1/2r).infinite?)
+ assert_nil((-1/2r).infinite?)
+ end
end