From 72565402d9d0662a2fbef2e30f76b7a6621eefef Mon Sep 17 00:00:00 2001 From: tadf Date: Sun, 21 Sep 2008 01:30:25 +0000 Subject: * 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 --- test/ruby/test_complex.rb | 80 ++++++++++++++++++++++++++-------------------- test/ruby/test_rational.rb | 75 +++++++++++++++++++++++++++---------------- 2 files changed, 93 insertions(+), 62 deletions(-) (limited to 'test') diff --git a/test/ruby/test_complex.rb b/test/ruby/test_complex.rb index e6248a15cf..e2417fe14d 100644 --- a/test/ruby/test_complex.rb +++ b/test/ruby/test_complex.rb @@ -4,6 +4,14 @@ class ComplexSub < Complex; end class Complex_Test < Test::Unit::TestCase + def setup + @rational = defined?(Rational) + if @rational + @keiju = Rational.instance_variable_get('@RCS_ID') + end + @unify = defined?(Complex::Unify) + end + def test_compsub c = ComplexSub.__send__(:new, 1) cc = ComplexSub.__send__(:convert, 1) @@ -11,7 +19,7 @@ class Complex_Test < Test::Unit::TestCase assert_kind_of(Numeric, c) assert_kind_of(Numeric, cc) - if defined?(ComplexSub::Unify) + if @unify assert_instance_of(Fixnum, c) assert_instance_of(Fixnum, cc) else @@ -42,7 +50,7 @@ class Complex_Test < Test::Unit::TestCase assert_equal(true, c.eql?(c2)) assert_equal(false, c.eql?(c3)) - if defined?(Complex::Unify) + if @unify assert_equal(true, c.eql?(0)) else assert_equal(false, c.eql?(0)) @@ -72,7 +80,7 @@ class Complex_Test < Test::Unit::TestCase def test_freeze c = Complex(1) c.freeze - unless defined?(Complex::Unify) + unless @unify assert_equal(true, c.frozen?) end assert_instance_of(String, c.to_s) @@ -110,7 +118,7 @@ class Complex_Test < Test::Unit::TestCase def test_new assert_instance_of(Complex, Complex.__send__(:new, 2,0.0)) - if defined?(Complex::Unify) + if @unify assert_instance_of(Fixnum, Complex.__send__(:new, 2,0)) else assert_instance_of(Complex, Complex.__send__(:new, 2,0)) @@ -152,20 +160,24 @@ class Complex_Test < Test::Unit::TestCase assert_equal(Complex.__send__(:new, -2**32,-2**32), c) assert_equal([-2**32,-2**32], [c.real,c.imag]) - c = Complex(Complex(1),0) - assert_equal(Complex.__send__(:new, 1,0), c) + c = Complex(Complex(1,2),2) + assert_equal(Complex.__send__(:new, 1,4), c) - c = Complex(0,Complex(1)) + c = Complex(2,Complex(1,2)) assert_equal(Complex.__send__(:new, 0,1), c) - c = Complex(Complex(1,1),Complex(1)) - assert_equal(Complex.__send__(:new, 1,2), c) + c = Complex(Complex(1,2),Complex(1,2)) + assert_equal(Complex.__send__(:new, -1,3), c) c = Complex::I assert_equal(Complex.__send__(:new, 0,1), c) assert_equal(Complex.__send__(:new, 1),Complex(1)) assert_equal(Complex.__send__(:new, 1),Complex('1')) + assert_equal(Complex.__send__(:new, 3.0,3.0),Complex('3.0','3.0')) + if @rational && !@keiju + assert_equal(Complex.__send__(:new, 1,1),Complex('3/3','3/3')) + end assert_raise(ArgumentError){Complex(nil)} assert_raise(ArgumentError){Complex(Object.new)} assert_raise(ArgumentError){Complex()} @@ -232,7 +244,7 @@ class Complex_Test < Test::Unit::TestCase def test_attr2 c = Complex(1) - if defined?(Complex::Unify) + if @unify =begin assert_equal(true, c.finite?) assert_equal(false, c.infinite?) @@ -345,7 +357,7 @@ class Complex_Test < Test::Unit::TestCase assert_equal(Complex(3,2), c + 2) assert_equal(Complex(3.0,2), c + 2.0) - if defined?(Rational) + if @rational assert_equal(Complex(Rational(3,1),Rational(2)), c + Rational(2)) assert_equal(Complex(Rational(5,3),Rational(2)), c + Rational(2,3)) end @@ -360,7 +372,7 @@ class Complex_Test < Test::Unit::TestCase assert_equal(Complex(-1,2), c - 2) assert_equal(Complex(-1.0,2), c - 2.0) - if defined?(Rational) + if @rational assert_equal(Complex(Rational(-1,1),Rational(2)), c - Rational(2)) assert_equal(Complex(Rational(1,3),Rational(2)), c - Rational(2,3)) end @@ -375,7 +387,7 @@ class Complex_Test < Test::Unit::TestCase assert_equal(Complex(2,4), c * 2) assert_equal(Complex(2.0,4.0), c * 2.0) - if defined?(Rational) + if @rational assert_equal(Complex(Rational(2,1),Rational(4)), c * Rational(2)) assert_equal(Complex(Rational(2,3),Rational(4,3)), c * Rational(2,3)) end @@ -386,7 +398,7 @@ class Complex_Test < Test::Unit::TestCase c = Complex(1,2) c2 = Complex(2,3) - if defined?(Rational) + if @rational assert_equal(Complex(Rational(8,13),Rational(1,13)), c / c2) else r = c / c2 @@ -404,14 +416,14 @@ class Complex_Test < Test::Unit::TestCase c = Complex(1,2) c2 = Complex(2,3) - if defined?(Rational) + if @rational assert_equal(Complex(Rational(1,2),1), c / 2) else assert_equal(Complex(0.5,1.0), c / 2) end assert_equal(Complex(0.5,1.0), c / 2.0) - if defined?(Rational) + if @rational assert_equal(Complex(Rational(1,2),Rational(1)), c / Rational(2)) assert_equal(Complex(Rational(3,2),Rational(3)), c / Rational(2,3)) end @@ -421,7 +433,7 @@ class Complex_Test < Test::Unit::TestCase c = Complex(1,2) c2 = Complex(2,3) - if defined?(Rational) + if @rational assert_equal(Complex(Rational(8,13),Rational(1,13)), c.quo(c2)) else r = c.quo(c2) @@ -439,14 +451,14 @@ class Complex_Test < Test::Unit::TestCase c = Complex(1,2) c2 = Complex(2,3) - if defined?(Rational) + if @rational assert_equal(Complex(Rational(1,2),1), c.quo(2)) else assert_equal(Complex(0.5,1.0), c.quo(2)) end assert_equal(Complex(0.5,1.0), c.quo(2.0)) - if defined?(Rational) + if @rational assert_equal(Complex(Rational(1,2),Rational(1)), c / Rational(2)) assert_equal(Complex(Rational(3,2),Rational(3)), c / Rational(2,3)) end @@ -483,7 +495,7 @@ class Complex_Test < Test::Unit::TestCase assert_in_delta(-0.179, r.imag, 0.001) assert_equal(Complex(-3,4), c ** 2) - if defined?(Rational) && !Rational.instance_variable_get('@RCS_ID') + if @rational && !@keiju assert_equal(Complex(Rational(-3,25),Rational(-4,25)), c ** -2) else r = c ** -2 @@ -498,7 +510,7 @@ class Complex_Test < Test::Unit::TestCase assert_in_delta(-0.12, r.real, 0.001) assert_in_delta(-0.16, r.imag, 0.001) - if defined?(Rational) && !Rational.instance_variable_get('@RCS_ID') + if @rational && !@keiju assert_equal(Complex(-3,4), c ** Rational(2)) #=begin assert_equal(Complex(Rational(-3,25),Rational(-4,25)), @@ -541,7 +553,7 @@ class Complex_Test < Test::Unit::TestCase end def test_unify - if defined?(Complex::Unify) + if @unify assert_instance_of(Fixnum, Complex(1,2) + Complex(-1,-2)) assert_instance_of(Fixnum, Complex(1,2) - Complex(1,2)) assert_instance_of(Fixnum, Complex(1,2) * 0) @@ -602,7 +614,7 @@ class Complex_Test < Test::Unit::TestCase assert_equal('1.0-2.0i', Complex(1.0,-2.0).to_s) assert_equal('-1.0-2.0i', Complex(-1.0,-2.0).to_s) - if defined?(Rational) && !defined?(Complex::Unify) && !Rational.instance_variable_get('@RCS_ID') + if @rational && !@unify && !@keiju assert_equal('0+2/1i', Complex(0,Rational(2)).to_s) assert_equal('0-2/1i', Complex(0,Rational(-2)).to_s) assert_equal('1+2/1i', Complex(1,Rational(2)).to_s) @@ -638,7 +650,7 @@ class Complex_Test < Test::Unit::TestCase assert_equal(9, c2.instance_variable_get(:@ivar)) assert_instance_of(Complex, c2) - if defined?(Rational) + if @rational c = Complex(Rational(1,2),Rational(2,3)) s = Marshal.dump(c) @@ -773,7 +785,7 @@ class Complex_Test < Test::Unit::TestCase assert_raise(ArgumentError){ Complex('5+3i_')} assert_raise(ArgumentError){ Complex('5+3ix')} - if defined?(Rational) && defined?(''.to_r) + if @rational && defined?(''.to_r) assert_equal(Complex(Rational(1,5)), '1/5'.to_c) assert_equal(Complex(Rational(-1,5)), '-1/5'.to_c) assert_equal(Complex(Rational(1,5),3), '1/5+3i'.to_c) @@ -839,11 +851,11 @@ class Complex_Test < Test::Unit::TestCase end def test_to_r - if defined?(Rational) && !Rational.instance_variable_get('@RCS_ID') + if @rational && !@keiju assert_equal(Rational(3), Complex(3).to_r) assert_equal(Rational(3), Rational(Complex(3))) assert_raise(RangeError){Complex(3,2).to_r} - assert_raise(RangeError){Rational(Complex(3,2))} +# assert_raise(RangeError){Rational(Complex(3,2))} end end @@ -860,7 +872,7 @@ class Complex_Test < Test::Unit::TestCase c = 1.1.to_c assert_equal([1.1, 0], [c.real, c.imag]) - if defined?(Rational) + if @rational c = Rational(1,2).to_c assert_equal([Rational(1,2), 0], [c.real, c.imag]) end @@ -933,14 +945,14 @@ class Complex_Test < Test::Unit::TestCase assert_equal(1.1, 1.1.conj) assert_equal(-1.1, -1.1.conj) - if defined?(Rational) + if @rational assert_equal(Complex(Rational(1,2),Rational(1)), Complex(1,2).quo(2)) else assert_equal(Complex(0.5,1.0), Complex(1,2).quo(2)) end =begin - if defined?(Rational) && !Rational.instance_variable_get('@RCS_ID') + if @rational && !@keiju assert_equal(Complex(Rational(1,2),Rational(1)), Complex(1,2).quo(2)) end =end @@ -948,7 +960,7 @@ class Complex_Test < Test::Unit::TestCase assert_equal(0.5, 1.fdiv(2)) assert_equal(5000000000.0, 10000000000.fdiv(2)) assert_equal(0.5, 1.0.fdiv(2)) - if defined?(Rational) + if @rational assert_equal(0.25, Rational(1,2).fdiv(2)) end assert_equal(Complex(0.5,1.0), Complex(1,2).quo(2)) @@ -958,7 +970,7 @@ class Complex_Test < Test::Unit::TestCase # assert_equal(true, Math.sqrt(-4.0).inexact?) assert_equal(Complex(0,2), Math.sqrt(-4)) # assert_equal(true, Math.sqrt(-4).exact?) - if defined?(Rational) + if @rational assert_equal(Complex(0,2), Math.sqrt(Rational(-4))) # assert_equal(true, Math.sqrt(Rational(-4)).exact?) end @@ -967,7 +979,7 @@ class Complex_Test < Test::Unit::TestCase # assert_equal(true, Math.sqrt(-9.0).inexact?) assert_equal(Complex(0,3), Math.sqrt(-9)) # assert_equal(true, Math.sqrt(-9).exact?) - if defined?(Rational) + if @rational assert_equal(Complex(0,3), Math.sqrt(Rational(-9))) # assert_equal(true, Math.sqrt(Rational(-9)).exact?) end @@ -1070,7 +1082,7 @@ class Complex_Test < Test::Unit::TestCase end def test_fixed_bug - if defined?(Rational) && !Rational.instance_variable_get('@RCS_ID') + if @rational && !@keiju assert_equal(Complex(1), 1 ** Complex(1)) end assert_equal('-1.0-0.0i', Complex(-1.0, -0.0).to_s) 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 -- cgit v1.2.3