summaryrefslogtreecommitdiff
path: root/test/ruby/test_complex.rb
diff options
context:
space:
mode:
authortadf <tadf@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2008-08-29 13:41:41 +0000
committertadf <tadf@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2008-08-29 13:41:41 +0000
commit5185955f3f64d53f55e34bfe4eaf059b7b347fc4 (patch)
tree6ffaa4e868c32e6a179f7051fac15048a7f938b3 /test/ruby/test_complex.rb
parentfa4c17e71283e72100c12b8824aafe176372241d (diff)
* complex.c ({nucomp,numeric}_rect): new.
* complex.c: added some aliases (::rectangular, ::rect, #rectangular, #rect, #phase, #magnitude). * complex.c (string_to_c_internal): should not strip any null bytes. * rational.c (string_to_r_internal): ditto. * rational.c (i_gcd): reverted to nurat 0.0.2's one. * numeric.c: added an alias (#magnitude). * test/ruby/test_complex.rb: added assertions. * test/ruby/test_rational.rb: ditto. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@18925 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'test/ruby/test_complex.rb')
-rw-r--r--test/ruby/test_complex.rb98
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