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.rb97
1 files changed, 55 insertions, 42 deletions
diff --git a/test/ruby/test_rational.rb b/test/ruby/test_rational.rb
index 1d280e4361..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,8 +267,7 @@ class Rational_Test < Test::Unit::TestCase
def test_attr2
c = Rational(1)
- if defined?(Rational::Unify)
- assert_equal(true, c.scalar?)
+ if @unify
=begin
assert_equal(true, c.finite?)
assert_equal(false, c.infinite?)
@@ -257,13 +275,14 @@ class Rational_Test < Test::Unit::TestCase
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
- assert_equal(true, c.scalar?)
=begin
assert_equal(true, c.finite?)
assert_equal(false, c.infinite?)
@@ -271,7 +290,9 @@ class Rational_Test < Test::Unit::TestCase
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?)
@@ -406,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)
@@ -441,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)
@@ -476,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)
@@ -512,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)
@@ -548,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)
@@ -584,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)
@@ -641,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)
@@ -649,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)
@@ -657,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)
@@ -666,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)
@@ -674,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)
@@ -682,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)
@@ -726,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
@@ -787,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)
@@ -801,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
@@ -831,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
@@ -854,10 +875,12 @@ 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(ZeroDivisionError){
@@ -987,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
@@ -1015,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}
@@ -1024,16 +1047,6 @@ class Rational_Test < Test::Unit::TestCase
end
end
- def test_prec
- assert_equal(true, Rational < Precision)
-
- c = Rational(3,2)
-
- assert_eql(1, c.prec(Integer))
- assert_eql(1.5, c.prec(Float))
- assert_eql(c, c.prec(Rational))
- end
-
def test_gcdlcm
assert_equal(7, 91.gcd(-49))
assert_equal(5, 5.gcd(0))
@@ -1052,8 +1065,8 @@ class Rational_Test < Test::Unit::TestCase
end
def test_supp
- assert_equal(true, 1.scalar?)
- assert_equal(true, 1.1.scalar?)
+ assert_equal(true, 1.real?)
+ assert_equal(true, 1.1.real?)
assert_equal(1, 1.numerator)
assert_equal(9, 9.numerator)
@@ -1089,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