summaryrefslogtreecommitdiff
path: root/test/ruby/test_complex.rb
diff options
context:
space:
mode:
Diffstat (limited to 'test/ruby/test_complex.rb')
-rw-r--r--test/ruby/test_complex.rb204
1 files changed, 196 insertions, 8 deletions
diff --git a/test/ruby/test_complex.rb b/test/ruby/test_complex.rb
index 4ebc69e9b1..a3a7546575 100644
--- a/test/ruby/test_complex.rb
+++ b/test/ruby/test_complex.rb
@@ -79,7 +79,6 @@ class Complex_Test < Test::Unit::TestCase
def test_freeze
c = Complex(1)
- c.freeze
assert_predicate(c, :frozen?)
assert_instance_of(String, c.to_s)
end
@@ -124,6 +123,10 @@ class Complex_Test < Test::Unit::TestCase
assert_raise(TypeError){Complex(Object.new)}
assert_raise(ArgumentError){Complex()}
assert_raise(ArgumentError){Complex(1,2,3)}
+ c = Complex(1,0)
+ assert_same(c, Complex(c))
+ assert_same(c, Complex(c, exception: false))
+ assert_raise(ArgumentError){Complex(c, bad_keyword: true)}
if (0.0/0).nan?
assert_nothing_raised{Complex(0.0/0)}
@@ -217,6 +220,11 @@ class Complex_Test < Test::Unit::TestCase
def test_polar
assert_equal([1,2], Complex.polar(1,2).polar)
assert_equal(Complex.polar(1.0, Math::PI * 2 / 3), Complex.polar(1, Math::PI * 2 / 3))
+
+ assert_in_out_err([], <<-'end;', ['OK'], [])
+ Complex.polar(1, Complex(1, 0))
+ puts :OK
+ end;
end
def test_uplus
@@ -270,6 +278,39 @@ class Complex_Test < Test::Unit::TestCase
assert_equal(Complex(Rational(5,3),Rational(2)), c + Rational(2,3))
end
+ def test_add_with_redefining_int_plus
+ assert_in_out_err([], <<-'end;', ['true'], [])
+ class Integer
+ remove_method :+
+ def +(other); 42; end
+ end
+ a = Complex(1, 2) + Complex(0, 1)
+ puts a == Complex(42, 42)
+ end;
+ end
+
+ def test_add_with_redefining_float_plus
+ assert_in_out_err([], <<-'end;', ['true'], [])
+ class Float
+ remove_method :+
+ def +(other); 42.0; end
+ end
+ a = Complex(1.0, 2.0) + Complex(0, 1)
+ puts a == Complex(42.0, 42.0)
+ end;
+ end
+
+ def test_add_with_redefining_rational_plus
+ assert_in_out_err([], <<-'end;', ['true'], [])
+ class Rational
+ remove_method :+
+ def +(other); 355/113r; end
+ end
+ a = Complex(1r, 2r) + Complex(0, 1)
+ puts a == Complex(355/113r, 355/113r)
+ end;
+ end
+
def test_sub
c = Complex(1,2)
c2 = Complex(2,3)
@@ -283,6 +324,39 @@ class Complex_Test < Test::Unit::TestCase
assert_equal(Complex(Rational(1,3),Rational(2)), c - Rational(2,3))
end
+ def test_sub_with_redefining_int_minus
+ assert_in_out_err([], <<-'end;', ['true'], [])
+ class Integer
+ remove_method :-
+ def -(other); 42; end
+ end
+ a = Complex(1, 2) - Complex(0, 1)
+ puts a == Complex(42, 42)
+ end;
+ end
+
+ def test_sub_with_redefining_float_minus
+ assert_in_out_err([], <<-'end;', ['true'], [])
+ class Float
+ remove_method :-
+ def -(other); 42.0; end
+ end
+ a = Complex(1.0, 2.0) - Complex(0, 1)
+ puts a == Complex(42.0, 42.0)
+ end;
+ end
+
+ def test_sub_with_redefining_rational_minus
+ assert_in_out_err([], <<-'end;', ['true'], [])
+ class Rational
+ remove_method :-
+ def -(other); 355/113r; end
+ end
+ a = Complex(1r, 2r) - Complex(0, 1)
+ puts a == Complex(355/113r, 355/113r)
+ end;
+ end
+
def test_mul
c = Complex(1,2)
c2 = Complex(2,3)
@@ -301,6 +375,42 @@ class Complex_Test < Test::Unit::TestCase
c = Complex(0, Float::INFINITY)
assert_equal(Complex(0, Float::INFINITY), c * Complex(1, 0))
assert_equal(Complex(-Float::INFINITY, 0), c * Complex(0, 1))
+
+ assert_equal(Complex(-0.0, -0.0), Complex(-0.0, 0) * Complex(0, 0))
+ end
+
+ def test_mul_with_redefining_int_mult
+ assert_in_out_err([], <<-'end;', ['true'], [])
+ class Integer
+ remove_method :*
+ def *(other); 42; end
+ end
+ a = Complex(2, 0) * Complex(1, 2)
+ puts a == Complex(0, 84)
+ end;
+ end
+
+ def test_mul_with_redefining_float_mult
+ assert_in_out_err([], <<-'end;', ['true'], [])
+ class Float
+ remove_method :*
+ def *(other); 42.0; end
+ end
+ a = Complex(2.0, 0.0) * Complex(1, 2)
+ puts a == Complex(0.0, 84.0)
+ end;
+ end
+
+
+ def test_mul_with_redefining_rational_mult
+ assert_in_out_err([], <<-'end;', ['true'], [])
+ class Rational
+ remove_method :*
+ def *(other); 355/113r; end
+ end
+ a = Complex(2r, 0r) * Complex(1, 2)
+ puts a == Complex(0r, 2*355/113r)
+ end;
end
def test_div
@@ -324,6 +434,15 @@ class Complex_Test < Test::Unit::TestCase
assert_equal(Complex(Rational(1,2),Rational(1)), c / Rational(2))
assert_equal(Complex(Rational(3,2),Rational(3)), c / Rational(2,3))
+
+ c = Complex(1)
+ [ 1, Rational(1), c ].each do |d|
+ r = c / d
+ assert_instance_of(Complex, r)
+ assert_equal(1, r)
+ assert_predicate(r.real, :integer?)
+ assert_predicate(r.imag, :integer?)
+ end
end
def test_quo
@@ -401,12 +520,23 @@ class Complex_Test < Test::Unit::TestCase
r = c ** Rational(-2,3)
assert_in_delta(0.432, r.real, 0.001)
assert_in_delta(-0.393, r.imag, 0.001)
+
+ c = Complex(0.0, -888888888888888.0)**8888
+ assert_not_predicate(c.real, :nan?)
+ assert_not_predicate(c.imag, :nan?)
end
def test_cmp
- assert_raise(NoMethodError){1 <=> Complex(1,1)}
- assert_raise(NoMethodError){Complex(1,1) <=> 1}
- assert_raise(NoMethodError){Complex(1,1) <=> Complex(1,1)}
+ assert_nil(Complex(5, 1) <=> Complex(2))
+ assert_nil(5 <=> Complex(2, 1))
+
+ assert_equal(1, Complex(5) <=> Complex(2))
+ assert_equal(-1, Complex(2) <=> Complex(3))
+ assert_equal(0, Complex(2) <=> Complex(2))
+
+ assert_equal(1, Complex(5) <=> 2)
+ assert_equal(-1, Complex(2) <=> 3)
+ assert_equal(0, Complex(2) <=> 2)
end
def test_eqeq
@@ -538,12 +668,10 @@ class Complex_Test < Test::Unit::TestCase
def test_marshal
c = Complex(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(Complex, c2)
c = Complex(Rational(1,2),Rational(2,3))
@@ -555,7 +683,6 @@ class Complex_Test < Test::Unit::TestCase
bug3656 = '[ruby-core:31622]'
c = Complex(1,2)
- c.freeze
assert_predicate(c, :frozen?)
result = c.marshal_load([2,3]) rescue :fail
assert_equal(:fail, result, bug3656)
@@ -750,10 +877,42 @@ class Complex_Test < Test::Unit::TestCase
end
+ def test_Complex_with_invalid_exception
+ assert_raise(ArgumentError) {
+ Complex("0", exception: 1)
+ }
+ end
+
+ def test_Complex_without_exception
+ assert_nothing_raised(ArgumentError){
+ assert_equal(nil, Complex('5x', exception: false))
+ }
+ assert_nothing_raised(ArgumentError){
+ assert_equal(nil, Complex(nil, exception: false))
+ }
+ assert_nothing_raised(ArgumentError){
+ assert_equal(nil, Complex(Object.new, exception: false))
+ }
+ assert_nothing_raised(ArgumentError){
+ assert_equal(nil, Complex(1, nil, exception: false))
+ }
+ assert_nothing_raised(ArgumentError){
+ assert_equal(nil, Complex(1, Object.new, exception: false))
+ }
+
+ o = Object.new
+ def o.to_c; raise; end
+ assert_nothing_raised(ArgumentError){
+ assert_equal(nil, Complex(o, exception: false))
+ }
+ assert_nothing_raised(ArgumentError){
+ assert_equal(nil, Complex(1, o, exception: false))
+ }
+ end
+
def test_respond
c = Complex(1,1)
assert_not_respond_to(c, :%)
- assert_not_respond_to(c, :<=>)
assert_not_respond_to(c, :div)
assert_not_respond_to(c, :divmod)
assert_not_respond_to(c, :floor)
@@ -857,6 +1016,9 @@ class Complex_Test < Test::Unit::TestCase
assert_equal(1, Complex(-1, Float::INFINITY).infinite?)
assert_equal(1, Complex(1, -Float::INFINITY).infinite?)
assert_equal(1, Complex(-1, -Float::INFINITY).infinite?)
+ assert_nil(Complex(Float::MAX, 0.0).infinite?)
+ assert_nil(Complex(0.0, Float::MAX).infinite?)
+ assert_nil(Complex(Float::MAX, Float::MAX).infinite?)
assert_nil(Complex(Float::NAN, 0).infinite?)
assert_nil(Complex(0, Float::NAN).infinite?)
assert_nil(Complex(Float::NAN, Float::NAN).infinite?)
@@ -959,4 +1121,30 @@ class Complex_Test < Test::Unit::TestCase
def test_known_bug
end
+ def test_canonicalize_internal
+ obj = Class.new(Numeric) do
+ attr_accessor :real
+ alias real? real
+ end.new
+ obj.real = true
+ c = Complex.rect(obj, 1);
+ obj.real = false
+ c = c.conj
+ assert_equal(obj, c.real)
+ assert_equal(-1, c.imag)
+ end
+
+ def test_canonicalize_polar
+ obj = Class.new(Numeric) do
+ def initialize
+ @x = 2
+ end
+ def real?
+ (@x -= 1) > 0
+ end
+ end.new
+ assert_raise(TypeError) do
+ Complex.polar(1, obj)
+ end
+ end
end