diff options
Diffstat (limited to 'test/ruby/test_complex.rb')
-rw-r--r-- | test/ruby/test_complex.rb | 98 |
1 files changed, 59 insertions, 39 deletions
diff --git a/test/ruby/test_complex.rb b/test/ruby/test_complex.rb index b661864519..786451febc 100644 --- a/test/ruby/test_complex.rb +++ b/test/ruby/test_complex.rb @@ -7,6 +7,10 @@ class Complex_Test < Test::Unit::TestCase def test_compsub c = ComplexSub.__send__(:new, 1) cc = ComplexSub.__send__(:convert, 1) + + assert_kind_of(Numeric, c) + assert_kind_of(Numeric, cc) + if defined?(ComplexSub::Unify) assert_instance_of(Fixnum, c) assert_instance_of(Fixnum, cc) @@ -181,10 +185,12 @@ class Complex_Test < Test::Unit::TestCase assert_equal(4, c.real) assert_equal(5, c.image) - c = Complex(-0.0,-0.0) + if -0.0.to_s == '-0.0' + c = Complex(-0.0,-0.0) - assert_equal('-0.0', c.real.to_s) - assert_equal('-0.0', c.image.to_s) + assert_equal('-0.0', c.real.to_s) + assert_equal('-0.0', c.image.to_s) + end c = Complex.__send__(:new, 4) @@ -198,11 +204,13 @@ class Complex_Test < Test::Unit::TestCase assert_equal(5, c.image) assert_equal(c.imag, c.image) - c = Complex.__send__(:new, -0.0,-0.0) + if -0.0.to_s == '-0.0' + c = Complex.__send__(:new, -0.0,-0.0) - assert_equal('-0.0', c.real.to_s) - assert_equal('-0.0', c.image.to_s) - assert_equal(c.imag.to_s, c.image.to_s) + assert_equal('-0.0', c.real.to_s) + assert_equal('-0.0', c.image.to_s) + assert_equal(c.imag.to_s, c.image.to_s) + end c = Complex.__send__(:new!, 4) @@ -275,6 +283,15 @@ class Complex_Test < Test::Unit::TestCase assert_equal(Complex(1,1), Complex(1,1).nonzero?) end + def rect + assert_equal([1,2], Complex.rectangular(1,2).rectangular) + assert_equal([1,2], Complex.rect(1,2).rect) + end + + def polar + assert_equal([1,2], Complex.polar(1,2).polar) + end + def test_uplus assert_equal(Complex(1), +Complex(1)) assert_equal(Complex(-1), +Complex(-1)) @@ -503,10 +520,23 @@ class Complex_Test < Test::Unit::TestCase assert_equal(false, Complex(1) == '') end + def test_unify + if defined?(Complex::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) + assert_instance_of(Fixnum, Complex(1,2) / Complex(1,2)) + assert_instance_of(Fixnum, Complex(1,2).div(Complex(1,2))) + assert_instance_of(Fixnum, Complex(1,2).quo(Complex(1,2))) +# assert_instance_of(Fixnum, Complex(1,2) ** 0) # mathn's bug + end + end + def test_math c = Complex(1,2) assert_in_delta(2.236, c.abs, 0.001) + assert_in_delta(2.236, c.magnitude, 0.001) assert_equal(5, c.abs2) assert_equal(c.abs, Math.sqrt(c * c.conj)) @@ -516,6 +546,7 @@ class Complex_Test < Test::Unit::TestCase assert_in_delta(1.107, c.arg, 0.001) assert_in_delta(1.107, c.angle, 0.001) + assert_in_delta(1.107, c.phase, 0.001) r = c.polar assert_in_delta(2.236, r[0], 0.001) @@ -598,6 +629,7 @@ class Complex_Test < Test::Unit::TestCase def test_parse assert_equal(Complex(0), ''.to_c) assert_equal(Complex(0), ' '.to_c) + assert_equal(Complex(5), "\f\n\r\t\v5\0".to_c) assert_equal(Complex(5), '5'.to_c) assert_equal(Complex(-5), '-5'.to_c) assert_equal(Complex(5,3), '5+3i'.to_c) @@ -705,6 +737,7 @@ class Complex_Test < Test::Unit::TestCase assert_raise(ArgumentError){ Complex('5+3_i')} assert_raise(ArgumentError){ Complex('5+3i_')} assert_raise(ArgumentError){ Complex('5+3ix')} + assert_raise(ArgumentError){ Complex("5\0")} if defined?(Rational) && defined?(''.to_r) assert_equal(Complex(Rational(1,5)), '1/5'.to_c) @@ -818,17 +851,36 @@ class Complex_Test < Test::Unit::TestCase assert_equal(0, 1.1.image) assert_equal(0, 1.1.imag) + assert_equal(1, 1.magnitude) + assert_equal(1, -1.magnitude) + assert_equal(1, 1.0.magnitude) + assert_equal(1, -1.0.magnitude) + assert_equal(0, 1.arg) assert_equal(0, 1.angle) + assert_equal(0, 1.phase) assert_equal(0, 1.0.arg) assert_equal(0, 1.0.angle) + assert_equal(0, 1.0.phase) assert_equal(Math::PI, -1.arg) assert_equal(Math::PI, -1.angle) + assert_equal(Math::PI, -1.phase) assert_equal(Math::PI, -1.0.arg) assert_equal(Math::PI, -1.0.angle) + assert_equal(Math::PI, -1.0.phase) + + assert_equal([1,0], 1.rect) + assert_equal([-1,0], -1.rect) + assert_equal([1,0], 1.rectangular) + assert_equal([-1,0], -1.rectangular) + + assert_equal([1.0,0], 1.0.rect) + assert_equal([-1.0,0], -1.0.rect) + assert_equal([1.0,0], 1.0.rectangular) + assert_equal([-1.0,0], -1.0.rectangular) assert_equal([1,0], 1.polar) assert_equal([1, Math::PI], -1.polar) @@ -846,46 +898,14 @@ class Complex_Test < Test::Unit::TestCase assert_equal(1.1, 1.1.conj) assert_equal(-1.1, -1.1.conj) - assert_equal(1, 1.numerator) - assert_equal(9, 9.numerator) - assert_equal(1, 1.denominator) - assert_equal(1, 9.denominator) - - if defined?(Rational) && !Rational.instance_variable_get('@RCS_ID') - assert_equal(1.0, 1.0.numerator) - assert_equal(9.0, 9.0.numerator) - assert_equal(1.0, 1.0.denominator) - assert_equal(1.0, 9.0.denominator) - end - -=begin - if defined?(Rational) && !Rational.instance_variable_get('@RCS_ID') - assert_equal(Rational(1,9), 9.reciprocal) - assert_equal(Rational(1,9), 9.0.reciprocal) - assert_equal(Rational(1,9), 9.inverse) - assert_equal(Rational(1,9), 9.0.inverse) - end -=end - if defined?(Rational) - 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(Complex(Rational(1,2),Rational(1)), Complex(1,2).quo(2)) else - assert_equal(0.5, 1.quo(2)) - assert_equal(5000000000.0, 10000000000.quo(2)) - assert_equal(0.5, 1.0.quo(2)) assert_equal(Complex(0.5,1.0), Complex(1,2).quo(2)) end =begin if defined?(Rational) && !Rational.instance_variable_get('@RCS_ID') - assert_equal(Rational(1,2), 1.rdiv(2)) - assert_equal(Rational(5000000000), 10000000000.rdiv(2)) - assert_equal(Rational(1,2), 1.0.rdiv(2)) - assert_equal(Rational(1,4), Rational(1,2).rdiv(2)) assert_equal(Complex(Rational(1,2),Rational(1)), Complex(1,2).quo(2)) end =end |