aboutsummaryrefslogtreecommitdiffstats
path: root/test/ruby/test_rational.rb
diff options
context:
space:
mode:
authortadf <tadf@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2008-09-21 01:30:25 +0000
committertadf <tadf@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2008-09-21 01:30:25 +0000
commit72565402d9d0662a2fbef2e30f76b7a6621eefef (patch)
treef4a7af81718c24690caeeb7e87571c2d83f494eb /test/ruby/test_rational.rb
parent280cbe0b1fb807ee0de9ea3e862d556763e08811 (diff)
downloadruby-72565402d9d0662a2fbef2e30f76b7a6621eefef.tar.gz
* complex.c (nucomp_s_canonicalize_internal): checks exactness of
imag only. * rational.c (nurat_s_convert): accepts non real value (Rational(a,b) as a/b). * complex.c (nucomp_s_convert): refined. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@19442 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'test/ruby/test_rational.rb')
-rw-r--r--test/ruby/test_rational.rb75
1 files changed, 47 insertions, 28 deletions
diff --git a/test/ruby/test_rational.rb b/test/ruby/test_rational.rb
index 949098e5d7..7f75584e0e 100644
--- a/test/ruby/test_rational.rb
+++ b/test/ruby/test_rational.rb
@@ -4,6 +4,14 @@ class RationalSub < Rational; end
class Rational_Test < Test::Unit::TestCase
+ def setup
+ @complex = defined?(Complex)
+ if @complex
+ @keiju = Complex.instance_variable_get('@RCS_ID')
+ end
+ @unify = defined?(Rational::Unify)
+ end
+
def test_ratsub
c = RationalSub.__send__(:new, 1)
cc = RationalSub.__send__(:convert, 1)
@@ -11,7 +19,7 @@ class Rational_Test < Test::Unit::TestCase
assert_kind_of(Numeric, c)
assert_kind_of(Numeric, cc)
- if defined?(RationalSub::Unify)
+ if @unify
assert_instance_of(Fixnum, c)
assert_instance_of(Fixnum, cc)
else
@@ -41,7 +49,7 @@ class Rational_Test < Test::Unit::TestCase
assert_equal(true, c.eql?(c2))
assert_equal(false, c.eql?(c3))
- if defined?(Rational::Unify)
+ if @unify
assert_equal(true, c.eql?(0))
else
assert_equal(false, c.eql?(0))
@@ -67,7 +75,7 @@ class Rational_Test < Test::Unit::TestCase
def test_freeze
c = Rational(1)
c.freeze
- unless defined?(Rational::Unify)
+ unless @unify
assert_equal(true, c.frozen?)
end
assert_instance_of(String, c.to_s)
@@ -108,7 +116,7 @@ class Rational_Test < Test::Unit::TestCase
=begin
def test_reduce
- if defined?(Rational::Unify)
+ if @unify
assert_instance_of(Fixnum, Rational.__send__(:reduce, 2,1))
else
assert_instance_of(Rational, Rational.__send__(:reduce, 2,1))
@@ -138,7 +146,7 @@ class Rational_Test < Test::Unit::TestCase
=end
def test_new
- if defined?(Rational::Unify)
+ if @unify
assert_instance_of(Fixnum, Rational.__send__(:new, 2,1))
else
assert_instance_of(Rational, Rational.__send__(:new, 2,1))
@@ -164,7 +172,7 @@ class Rational_Test < Test::Unit::TestCase
assert_raise(ArgumentError){Rational.__send__(:new, nil)}
=begin
assert_raise(ArgumentError){Rational.__send__(:new, Rational(1))}
- if defined?(Complex)
+ if @complex
assert_raise(ArgumentError){Rational.__send__(:new, Complex(1))}
end
=end
@@ -199,6 +207,17 @@ class Rational_Test < Test::Unit::TestCase
c = Rational(Rational(1,2),Rational(1,2))
assert_equal(Rational.__send__(:new, 1), c)
+ if @complex && !@keiju
+ c = Rational(Complex(1,2),2)
+ assert_equal(Complex.__send__(:new, Rational(1,2),1), c)
+
+ c = Rational(2,Complex(1,2))
+ assert_equal(Complex.__send__(:new, Rational(2,5),Rational(-4,5)), c)
+
+ c = Rational(Complex(1,2),Complex(1,2))
+ assert_equal(Rational.__send__(:new, 1), c)
+ end
+
assert_equal(Rational.__send__(:new, 3),Rational(3))
assert_equal(Rational.__send__(:new, 1),Rational(3,3))
assert_equal(3.3.to_r,Rational(3.3))
@@ -248,7 +267,7 @@ class Rational_Test < Test::Unit::TestCase
def test_attr2
c = Rational(1)
- if defined?(Rational::Unify)
+ if @unify
=begin
assert_equal(true, c.finite?)
assert_equal(false, c.infinite?)
@@ -408,7 +427,7 @@ class Rational_Test < Test::Unit::TestCase
assert_equal(-2, (-c).div(c2))
assert_equal(1, (-c).div(-c2))
- unless defined?(Rational::Unify)
+ unless @unify
c = Rational(11)
c2 = Rational(3)
@@ -443,7 +462,7 @@ class Rational_Test < Test::Unit::TestCase
assert_equal(Rational(99,100), (-c).modulo(c2))
assert_equal(Rational(-101,100), (-c).modulo(-c2))
- unless defined?(Rational::Unify)
+ unless @unify
c = Rational(11)
c2 = Rational(3)
@@ -478,7 +497,7 @@ 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 defined?(Rational::Unify)
+ unless @unify
c = Rational(11)
c2 = Rational(3)
@@ -514,7 +533,7 @@ class Rational_Test < Test::Unit::TestCase
assert_equal(-1, (-c).quot(c2))
assert_equal(1, (-c).quot(-c2))
- unless defined?(Rational::Unify)
+ unless @unify
c = Rational(11)
c2 = Rational(3)
@@ -550,7 +569,7 @@ class Rational_Test < Test::Unit::TestCase
assert_equal(Rational(-101,100), (-c).remainder(c2))
assert_equal(Rational(-101,100), (-c).remainder(-c2))
- unless defined?(Rational::Unify)
+ unless @unify
c = Rational(11)
c2 = Rational(3)
@@ -586,7 +605,7 @@ class Rational_Test < Test::Unit::TestCase
assert_equal([-1, Rational(-101,100)], (-c).quotrem(c2))
assert_equal([1, Rational(-101,100)], (-c).quotrem(-c2))
- unless defined?(Rational::Unify)
+ unless @unify
c = Rational(11)
c2 = Rational(3)
@@ -643,7 +662,7 @@ class Rational_Test < Test::Unit::TestCase
# p ** p
x = 2 ** Rational(2)
assert_equal(Rational(4), x)
- unless defined?(Rational::Unify)
+ unless @unify
assert_instance_of(Rational, x)
end
assert_equal(4, x.numerator)
@@ -651,7 +670,7 @@ class Rational_Test < Test::Unit::TestCase
x = Rational(2) ** 2
assert_equal(Rational(4), x)
- unless defined?(Rational::Unify)
+ unless @unify
assert_instance_of(Rational, x)
end
assert_equal(4, x.numerator)
@@ -659,7 +678,7 @@ class Rational_Test < Test::Unit::TestCase
x = Rational(2) ** Rational(2)
assert_equal(Rational(4), x)
- unless defined?(Rational::Unify)
+ unless @unify
assert_instance_of(Rational, x)
end
assert_equal(4, x.numerator)
@@ -668,7 +687,7 @@ class Rational_Test < Test::Unit::TestCase
# -p ** p
x = (-2) ** Rational(2)
assert_equal(Rational(4), x)
- unless defined?(Rational::Unify)
+ unless @unify
assert_instance_of(Rational, x)
end
assert_equal(4, x.numerator)
@@ -676,7 +695,7 @@ class Rational_Test < Test::Unit::TestCase
x = Rational(-2) ** 2
assert_equal(Rational(4), x)
- unless defined?(Rational::Unify)
+ unless @unify
assert_instance_of(Rational, x)
end
assert_equal(4, x.numerator)
@@ -684,7 +703,7 @@ class Rational_Test < Test::Unit::TestCase
x = Rational(-2) ** Rational(2)
assert_equal(Rational(4), x)
- unless defined?(Rational::Unify)
+ unless @unify
assert_instance_of(Rational, x)
end
assert_equal(4, x.numerator)
@@ -728,7 +747,7 @@ class Rational_Test < Test::Unit::TestCase
assert_equal(1, x.numerator)
assert_equal(4, x.denominator)
- unless defined?(Rational::Unify) # maybe bug mathn
+ unless @unify # maybe bug mathn
assert_raise(ZeroDivisionError){0 ** -1}
end
end
@@ -789,7 +808,7 @@ class Rational_Test < Test::Unit::TestCase
end
def test_unify
- if defined?(Rational::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)
@@ -803,7 +822,7 @@ class Rational_Test < Test::Unit::TestCase
def test_math
assert_equal(Rational(1,2), Rational(1,2).abs)
assert_equal(Rational(1,2), Rational(-1,2).abs)
- if defined?(Complex) && !Complex.instance_variable_get('@RCS_ID')
+ if @complex && !@keiju
assert_equal(Rational(1,2), Rational(1,2).magnitude)
assert_equal(Rational(1,2), Rational(-1,2).magnitude)
end
@@ -833,7 +852,7 @@ class Rational_Test < Test::Unit::TestCase
assert_instance_of(String, c.to_s)
assert_equal('1/2', c.to_s)
- if defined?(Rational::Unify)
+ if @unify
assert_equal('0', Rational(0,2).to_s)
assert_equal('0', Rational(0,-2).to_s)
else
@@ -991,8 +1010,8 @@ class Rational_Test < Test::Unit::TestCase
end
def test_to_c
- if defined?(Complex) && !Complex.instance_variable_get('@RCS_ID')
- if defined?(Rational::Unify)
+ 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
@@ -1019,8 +1038,8 @@ class Rational_Test < Test::Unit::TestCase
c = Rational(1,2).to_r
assert_equal([1,2] , [c.numerator, c.denominator])
- if defined?(Complex)
- if Complex.instance_variable_get('@RCS_ID')
+ if @complex
+ if @keiju
assert_raise(NoMethodError){Complex(1,2).to_r}
else
assert_raise(RangeError){Complex(1,2).to_r}
@@ -1083,7 +1102,7 @@ class Rational_Test < Test::Unit::TestCase
end
def test_fixed_bug
- if defined?(Rational::Unify)
+ if @unify
assert_instance_of(Fixnum, Rational(1,2) ** 0) # mathn's bug
end