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.rb326
1 files changed, 275 insertions, 51 deletions
diff --git a/test/ruby/test_complex.rb b/test/ruby/test_complex.rb
index 6d189f46dd..a3a7546575 100644
--- a/test/ruby/test_complex.rb
+++ b/test/ruby/test_complex.rb
@@ -41,15 +41,19 @@ class Complex_Test < Test::Unit::TestCase
c2 = Complex(0)
c3 = Complex(1)
- assert_equal(true, c.eql?(c2))
- assert_equal(false, c.eql?(c3))
+ assert_operator(c, :eql?, c2)
+ assert_not_operator(c, :eql?, c3)
- assert_equal(false, c.eql?(0))
+ assert_not_operator(c, :eql?, 0)
end
def test_hash
- assert_kind_of(Integer, Complex(1,2).hash)
- assert_kind_of(Integer, Complex(1.0,2.0).hash)
+ h = Complex(1,2).hash
+ assert_kind_of(Integer, h)
+ assert_nothing_raised {h.to_s}
+ h = Complex(1.0,2.0).hash
+ assert_kind_of(Integer, h)
+ assert_nothing_raised {h.to_s}
h = {}
h[Complex(0)] = 0
@@ -75,8 +79,7 @@ class Complex_Test < Test::Unit::TestCase
def test_freeze
c = Complex(1)
- c.freeze
- assert_equal(true, c.frozen?)
+ assert_predicate(c, :frozen?)
assert_instance_of(String, c.to_s)
end
@@ -120,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)}
@@ -189,14 +196,14 @@ class Complex_Test < Test::Unit::TestCase
def test_attr2
c = Complex(1)
- assert_equal(false, c.integer?)
- assert_equal(false, c.real?)
+ assert_not_predicate(c, :integer?)
+ assert_not_predicate(c, :real?)
- assert_equal(true, Complex(0).zero?)
- assert_equal(true, Complex(0,0).zero?)
- assert_equal(false, Complex(1,0).zero?)
- assert_equal(false, Complex(0,1).zero?)
- assert_equal(false, Complex(1,1).zero?)
+ assert_predicate(Complex(0), :zero?)
+ assert_predicate(Complex(0,0), :zero?)
+ assert_not_predicate(Complex(1,0), :zero?)
+ assert_not_predicate(Complex(0,1), :zero?)
+ assert_not_predicate(Complex(1,1), :zero?)
assert_equal(nil, Complex(0).nonzero?)
assert_equal(nil, Complex(0,0).nonzero?)
@@ -213,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
@@ -266,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)
@@ -279,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)
@@ -297,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
@@ -320,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
@@ -397,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
@@ -434,7 +568,7 @@ class Complex_Test < Test::Unit::TestCase
end
class ObjectX
- def + (x) Rational(1) end
+ def +(x) Rational(1) end
alias - +
alias * +
alias / +
@@ -534,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))
@@ -551,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)
@@ -746,36 +877,67 @@ 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_equal(false, c.respond_to?(:%))
- assert_equal(false, c.respond_to?(:<))
- assert_equal(false, c.respond_to?(:<=))
- assert_equal(false, c.respond_to?(:<=>))
- assert_equal(false, c.respond_to?(:>))
- assert_equal(false, c.respond_to?(:>=))
- assert_equal(false, c.respond_to?(:between?))
- assert_equal(false, c.respond_to?(:div))
- assert_equal(false, c.respond_to?(:divmod))
- assert_equal(false, c.respond_to?(:floor))
- assert_equal(false, c.respond_to?(:ceil))
- assert_equal(false, c.respond_to?(:modulo))
- assert_equal(false, c.respond_to?(:remainder))
- assert_equal(false, c.respond_to?(:round))
- assert_equal(false, c.respond_to?(:step))
- assert_equal(false, c.respond_to?(:tunrcate))
-
- assert_equal(false, c.respond_to?(:positive?))
- assert_equal(false, c.respond_to?(:negative?))
- assert_equal(false, c.respond_to?(:sign))
-
- assert_equal(false, c.respond_to?(:quotient))
- assert_equal(false, c.respond_to?(:quot))
- assert_equal(false, c.respond_to?(:quotrem))
-
- assert_equal(false, c.respond_to?(:gcd))
- assert_equal(false, c.respond_to?(:lcm))
- assert_equal(false, c.respond_to?(:gcdlcm))
+ assert_not_respond_to(c, :%)
+ assert_not_respond_to(c, :div)
+ assert_not_respond_to(c, :divmod)
+ assert_not_respond_to(c, :floor)
+ assert_not_respond_to(c, :ceil)
+ assert_not_respond_to(c, :modulo)
+ assert_not_respond_to(c, :remainder)
+ assert_not_respond_to(c, :round)
+ assert_not_respond_to(c, :step)
+ assert_not_respond_to(c, :tunrcate)
+
+ assert_not_respond_to(c, :positive?)
+ assert_not_respond_to(c, :negative?)
+ assert_not_respond_to(c, :sign)
+
+ assert_not_respond_to(c, :quotient)
+ assert_not_respond_to(c, :quot)
+ assert_not_respond_to(c, :quotrem)
+
+ assert_not_respond_to(c, :gcd)
+ assert_not_respond_to(c, :lcm)
+ assert_not_respond_to(c, :gcdlcm)
+
+ (Comparable.instance_methods(false) - Complex.instance_methods(false)).each do |n|
+ assert_not_respond_to(c, n, "Complex##{n}")
+ end
end
def test_to_i
@@ -826,9 +988,45 @@ class Complex_Test < Test::Unit::TestCase
end
end
+ def test_finite_p
+ assert_predicate(1+1i, :finite?)
+ assert_predicate(1-1i, :finite?)
+ assert_predicate(-1+1i, :finite?)
+ assert_predicate(-1-1i, :finite?)
+ assert_not_predicate(Float::INFINITY + 1i, :finite?)
+ assert_not_predicate(Complex(1, Float::INFINITY), :finite?)
+ assert_predicate(Complex(Float::MAX, 0.0), :finite?)
+ assert_predicate(Complex(0.0, Float::MAX), :finite?)
+ assert_predicate(Complex(Float::MAX, Float::MAX), :finite?)
+ assert_not_predicate(Complex(Float::NAN, 0), :finite?)
+ assert_not_predicate(Complex(0, Float::NAN), :finite?)
+ assert_not_predicate(Complex(Float::NAN, Float::NAN), :finite?)
+ end
+
+ def test_infinite_p
+ assert_nil((1+1i).infinite?)
+ assert_nil((1-1i).infinite?)
+ assert_nil((-1+1i).infinite?)
+ assert_nil((-1-1i).infinite?)
+ assert_equal(1, (Float::INFINITY + 1i).infinite?)
+ assert_equal(1, (Float::INFINITY - 1i).infinite?)
+ assert_equal(1, (-Float::INFINITY + 1i).infinite?)
+ assert_equal(1, (-Float::INFINITY - 1i).infinite?)
+ 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_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?)
+ end
+
def test_supp
- assert_equal(true, 1.real?)
- assert_equal(true, 1.1.real?)
+ assert_predicate(1, :real?)
+ assert_predicate(1.1, :real?)
assert_equal(1, 1.real)
assert_equal(0, 1.imag)
@@ -923,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