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.rb925
1 files changed, 529 insertions, 396 deletions
diff --git a/test/ruby/test_complex.rb b/test/ruby/test_complex.rb
index 0a7828b85f..bb131cee91 100644
--- a/test/ruby/test_complex.rb
+++ b/test/ruby/test_complex.rb
@@ -1,18 +1,10 @@
+# frozen_string_literal: false
require 'test/unit'
class ComplexSub < Complex; end
class Complex_Test < Test::Unit::TestCase
- def setup
- @rational = defined?(Rational)
- if @rational
- @keiju = Rational.instance_variables.include?(:@RCS_ID)
- end
- seps = [File::SEPARATOR, File::ALT_SEPARATOR].compact.map{|x| Regexp.escape(x)}.join("|")
- @unify = $".grep(/(?:^|#{seps})mathn(?:\.(?:rb|so))?/).size != 0
- end
-
def test_rationalize
assert_equal(1.quo(3), Complex(1/3.0, 0).rationalize, '[ruby-core:38885]')
assert_equal(1.quo(5), Complex(0.2, 0).rationalize, '[ruby-core:38885]')
@@ -24,24 +16,20 @@ class Complex_Test < Test::Unit::TestCase
assert_kind_of(Numeric, c)
- if @unify
- assert_instance_of(Fixnum, c)
- else
- assert_instance_of(ComplexSub, c)
+ assert_instance_of(ComplexSub, c)
- c2 = c + 1
- assert_instance_of(ComplexSub, c2)
- c2 = c - 1
- assert_instance_of(ComplexSub, c2)
+ c2 = c + 1
+ assert_instance_of(ComplexSub, c2)
+ c2 = c - 1
+ assert_instance_of(ComplexSub, c2)
- c3 = c - c2
- assert_instance_of(ComplexSub, c3)
+ c3 = c - c2
+ assert_instance_of(ComplexSub, c3)
- s = Marshal.dump(c)
- c5 = Marshal.load(s)
- assert_equal(c, c5)
- assert_instance_of(ComplexSub, c5)
- end
+ s = Marshal.dump(c)
+ c5 = Marshal.load(s)
+ assert_equal(c, c5)
+ assert_instance_of(ComplexSub, c5)
c1 = Complex(1)
assert_equal(c1.hash, c.hash, '[ruby-dev:38850]')
@@ -53,19 +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)
- if @unify
- assert_equal(true, c.eql?(0))
- else
- assert_equal(false, c.eql?(0))
- end
+ assert_not_operator(c, :eql?, 0)
end
def test_hash
- assert_instance_of(Fixnum, Complex(1,2).hash)
- assert_instance_of(Fixnum, 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
@@ -91,10 +79,7 @@ class Complex_Test < Test::Unit::TestCase
def test_freeze
c = Complex(1)
- c.freeze
- unless @unify
- assert_equal(true, c.frozen?)
- end
+ assert_predicate(c, :frozen?)
assert_instance_of(String, c.to_s)
end
@@ -133,13 +118,15 @@ class Complex_Test < Test::Unit::TestCase
assert_equal(Complex(1),Complex(1))
assert_equal(Complex(1),Complex('1'))
assert_equal(Complex(3.0,3.0),Complex('3.0','3.0'))
- if @rational && !@keiju
- assert_equal(Complex(1,1),Complex('3/3','3/3'))
- end
+ assert_equal(Complex(1,1),Complex('3/3','3/3'))
assert_raise(TypeError){Complex(nil)}
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)}
@@ -209,49 +196,14 @@ class Complex_Test < Test::Unit::TestCase
def test_attr2
c = Complex(1)
- if @unify
-=begin
- assert_equal(true, c.finite?)
- assert_equal(false, c.infinite?)
- assert_equal(false, c.nan?)
- assert_equal(true, c.integer?)
- assert_equal(false, c.float?)
- assert_equal(true, c.rational?)
-=end
- assert_equal(true, c.real?)
-=begin
- assert_equal(false, c.complex?)
- assert_equal(true, c.exact?)
- assert_equal(false, c.inexact?)
-=end
- else
-=begin
- assert_equal(true, c.finite?)
- assert_equal(false, c.infinite?)
- assert_equal(false, c.nan?)
- assert_equal(false, c.integer?)
- assert_equal(false, c.float?)
- assert_equal(false, c.rational?)
-=end
- assert_equal(false, c.real?)
-=begin
- assert_equal(true, c.complex?)
- assert_equal(true, c.exact?)
- assert_equal(false, c.inexact?)
-=end
- end
-
-=begin
- assert_equal(0, Complex(0).sign)
- assert_equal(1, Complex(2).sign)
- assert_equal(-1, Complex(-2).sign)
-=end
+ 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?)
@@ -267,6 +219,18 @@ 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))
+
+ one = 1+0i
+ c = Complex.polar(0, one)
+ assert_equal(0, c)
+ assert_predicate(c.real, :real?)
+ c = Complex.polar(one, 0)
+ assert_equal(1, c)
+ assert_predicate(c.real, :real?)
+ c = Complex.polar(one)
+ assert_equal(1, c)
+ assert_predicate(c.real, :real?)
end
def test_uplus
@@ -305,12 +269,6 @@ class Complex_Test < Test::Unit::TestCase
assert_equal('0.0', c.real.to_s)
assert_equal('0.0', c.imag.to_s)
end
-
-=begin
- assert_equal(0, Complex(0).negate)
- assert_equal(-2, Complex(2).negate)
- assert_equal(2, Complex(-2).negate)
-=end
end
def test_add
@@ -322,10 +280,41 @@ class Complex_Test < Test::Unit::TestCase
assert_equal(Complex(3,2), c + 2)
assert_equal(Complex(3.0,2), c + 2.0)
- 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
+ assert_equal(Complex(Rational(3,1),Rational(2)), c + Rational(2))
+ 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
@@ -337,10 +326,41 @@ class Complex_Test < Test::Unit::TestCase
assert_equal(Complex(-1,2), c - 2)
assert_equal(Complex(-1.0,2), c - 2.0)
- 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
+ assert_equal(Complex(Rational(-1,1),Rational(2)), c - Rational(2))
+ 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
@@ -352,24 +372,58 @@ class Complex_Test < Test::Unit::TestCase
assert_equal(Complex(2,4), c * 2)
assert_equal(Complex(2.0,4.0), c * 2.0)
- 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
+ assert_equal(Complex(Rational(2,1),Rational(4)), c * Rational(2))
+ assert_equal(Complex(Rational(2,3),Rational(4,3)), c * Rational(2,3))
+
+ c = Complex(Float::INFINITY, 0)
+ assert_equal(Complex(Float::INFINITY, 0), c * Complex(1, 0))
+ assert_equal(Complex(0, Float::INFINITY), c * Complex(0, 1))
+ 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
c = Complex(1,2)
c2 = Complex(2,3)
- if @rational
- assert_equal(Complex(Rational(8,13),Rational(1,13)), c / c2)
- else
- r = c / c2
- assert_in_delta(0.615, r.real, 0.001)
- assert_in_delta(0.076, r.imag, 0.001)
- end
+ assert_equal(Complex(Rational(8,13),Rational(1,13)), c / c2)
c = Complex(1.0,2.0)
c2 = Complex(2.0,3.0)
@@ -381,16 +435,19 @@ class Complex_Test < Test::Unit::TestCase
c = Complex(1,2)
c2 = Complex(2,3)
- 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(Rational(1,2),1), c / 2)
assert_equal(Complex(0.5,1.0), c / 2.0)
- 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))
+ 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
@@ -398,13 +455,7 @@ class Complex_Test < Test::Unit::TestCase
c = Complex(1,2)
c2 = Complex(2,3)
- if @rational
- assert_equal(Complex(Rational(8,13),Rational(1,13)), c.quo(c2))
- else
- r = c.quo(c2)
- assert_in_delta(0.615, r.real, 0.001)
- assert_in_delta(0.076, r.imag, 0.001)
- end
+ assert_equal(Complex(Rational(8,13),Rational(1,13)), c.quo(c2))
c = Complex(1.0,2.0)
c2 = Complex(2.0,3.0)
@@ -416,17 +467,11 @@ class Complex_Test < Test::Unit::TestCase
c = Complex(1,2)
c2 = Complex(2,3)
- 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(Rational(1,2),1), c.quo(2))
assert_equal(Complex(0.5,1.0), c.quo(2.0))
- 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
+ assert_equal(Complex(Rational(1,2),Rational(1)), c / Rational(2))
+ assert_equal(Complex(Rational(3,2),Rational(3)), c / Rational(2,3))
end
def test_fdiv
@@ -460,13 +505,8 @@ class Complex_Test < Test::Unit::TestCase
assert_in_delta(-0.179, r.imag, 0.001)
assert_equal(Complex(-3,4), c ** 2)
- if @rational && !@keiju
- assert_equal(Complex(Rational(-3,25),Rational(-4,25)), c ** -2)
- else
- r = c ** -2
- assert_in_delta(-0.12, r.real, 0.001)
- assert_in_delta(-0.16, r.imag, 0.001)
- end
+ assert_equal(Complex(Rational(-3,25),Rational(-4,25)), c ** -2)
+
r = c ** 2.0
assert_in_delta(-3.0, r.real, 0.001)
assert_in_delta(4.0, r.imag, 0.001)
@@ -475,43 +515,115 @@ 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 @rational && !@keiju
- assert_equal(Complex(-3,4), c ** Rational(2))
-#=begin
- assert_equal(Complex(Rational(-3,25),Rational(-4,25)),
- c ** Rational(-2)) # why failed?
-#=end
+ assert_equal(Complex(-3,4), c ** Rational(2))
+ assert_equal(Complex(Rational(-3,25),Rational(-4,25)),
+ c ** Rational(-2)) # why failed?
- r = c ** Rational(2,3)
- assert_in_delta(1.264, r.real, 0.001)
- assert_in_delta(1.150, r.imag, 0.001)
+ r = c ** Rational(2,3)
+ assert_in_delta(1.264, r.real, 0.001)
+ assert_in_delta(1.150, r.imag, 0.001)
- r = c ** Rational(-2,3)
- assert_in_delta(0.432, r.real, 0.001)
- assert_in_delta(-0.393, r.imag, 0.001)
- end
+ r = c ** Rational(-2,3)
+ assert_in_delta(0.432, r.real, 0.001)
+ assert_in_delta(-0.393, r.imag, 0.001)
+ end
+
+ def test_expt_for_special_angle
+ c = Complex(1, 0) ** 100000000000000000000000000000000
+ assert_equal(Complex(1, 0), c)
+
+ c = Complex(-1, 0) ** 10000000000000000000000000000000
+ assert_equal(Complex(1, 0), c)
+
+ c = Complex(-1, 0) ** 10000000000000000000000000000001
+ assert_equal(Complex(-1, 0), c)
+
+ c = Complex(0, 1) ** 100000000000000000000000000000000
+ assert_equal(Complex(1, 0), c)
+
+ c = Complex(0, 1) ** 100000000000000000000000000000001
+ assert_equal(Complex(0, 1), c)
+
+ c = Complex(0, 1) ** 100000000000000000000000000000002
+ assert_equal(Complex(-1, 0), c)
+
+ c = Complex(0, 1) ** 100000000000000000000000000000003
+ assert_equal(Complex(0, -1), c)
+
+ c = Complex(0, -1) ** 100000000000000000000000000000000
+ assert_equal(Complex(1, 0), c)
+
+ c = Complex(0, -1) ** 100000000000000000000000000000001
+ assert_equal(Complex(0, -1), c)
+
+ c = Complex(0, -1) ** 100000000000000000000000000000002
+ assert_equal(Complex(-1, 0), c)
+
+ c = Complex(0, -1) ** 100000000000000000000000000000003
+ assert_equal(Complex(0, 1), c)
+
+ c = Complex(1, 1) ** 1
+ assert_equal(Complex(1, 1), c)
+
+ c = Complex(1, 1) ** 2
+ assert_equal(Complex(0, 2), c)
+
+ c = Complex(1, 1) ** 3
+ assert_equal(Complex(-2, 2), c)
+
+ c = Complex(1, 1) ** 4
+ assert_equal(Complex(-4, 0), c)
+
+ c = Complex(1, 1) ** 5
+ assert_equal(Complex(-4, -4), c)
+
+ c = Complex(1, 1) ** 6
+ assert_equal(Complex(0, -8), c)
+
+ c = Complex(1, 1) ** 7
+ assert_equal(Complex(8, -8), c)
+
+ c = Complex(-2, -2) ** 3
+ assert_equal(Complex(16, -16), c)
+
+ c = Complex(2, -2) ** 3
+ assert_equal(Complex(-16, -16), c)
+
+ c = Complex(-2, 2) ** 3
+ assert_equal(Complex(16, 16), c)
+
+ 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
- assert(Complex(1,0) == Complex(1))
- assert(Complex(-1,0) == Complex(-1))
+ assert_equal(Complex(1), Complex(1,0))
+ assert_equal(Complex(-1), Complex(-1,0))
- assert_equal(false, Complex(2,1) == Complex(1))
- assert_equal(true, Complex(2,1) != Complex(1))
- assert_equal(false, Complex(1) == nil)
- assert_equal(false, Complex(1) == '')
+ assert_not_equal(Complex(1), Complex(2,1))
+ assert_operator(Complex(2,1), :!=, Complex(1))
+ assert_not_equal(nil, Complex(1))
+ assert_not_equal('', Complex(1))
nan = 0.0 / 0
if nan.nan? && nan != nan
- assert_equal(false, Complex(nan, 0) == Complex(nan, 0))
- assert_equal(false, Complex(0, nan) == Complex(0, nan))
- assert_equal(false, Complex(nan, nan) == Complex(nan, nan))
+ assert_not_equal(Complex(nan, 0), Complex(nan, 0))
+ assert_not_equal(Complex(0, nan), Complex(0, nan))
+ assert_not_equal(Complex(nan, nan), Complex(nan, nan))
end
end
@@ -521,34 +633,29 @@ class Complex_Test < Test::Unit::TestCase
assert_equal([Complex(Rational(2)),Complex(1)],
Complex(1).coerce(Rational(2)))
assert_equal([Complex(2),Complex(1)], Complex(1).coerce(Complex(2)))
+
+ obj = eval("class C\u{1f5ff}; self; end").new
+ assert_raise_with_message(TypeError, /C\u{1f5ff}/) { Complex(1).coerce(obj) }
end
- class ObjectX
- def + (x) Rational(1) end
+ class ObjectX < Numeric
+ def initialize(real = true, n = 1) @n = n; @real = real; end
+ def +(x) Rational(@n) end
alias - +
alias * +
alias / +
alias quo +
alias ** +
- def coerce(x) [x, Complex(1)] end
+ def coerce(x) [x, Complex(@n)] end
+ def real?; @real; end
end
def test_coerce2
x = ObjectX.new
- %w(+ - * / quo **).each do |op|
- assert_kind_of(Numeric, Complex(1).__send__(op, x))
- end
- end
-
- def test_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)
- 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
+ y = ObjectX.new(false)
+ %w(+ - * / quo ** <=>).each do |op|
+ assert_kind_of(Numeric, Complex(1).__send__(op, x), op)
+ assert_kind_of(Numeric, Complex(1).__send__(op, y), op)
end
end
@@ -573,8 +680,6 @@ class Complex_Test < Test::Unit::TestCase
assert_in_delta(1.107, r[1], 0.001)
assert_equal(Complex(1,-2), c.conjugate)
assert_equal(Complex(1,-2), c.conj)
-# assert_equal(Complex(1,-2), ~c)
-# assert_equal(5, c * ~c)
assert_equal(Complex(1,2), c.numerator)
assert_equal(1, c.denominator)
@@ -602,23 +707,21 @@ 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 @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)
- assert_equal('-1+2/1i', Complex(-1,Rational(2)).to_s)
- assert_equal('-1-2/1i', Complex(-1,Rational(-2)).to_s)
- assert_equal('1-2/1i', Complex(1,Rational(-2)).to_s)
- assert_equal('-1-2/1i', Complex(-1,Rational(-2)).to_s)
-
- assert_equal('0+2/3i', Complex(0,Rational(2,3)).to_s)
- assert_equal('0-2/3i', Complex(0,Rational(-2,3)).to_s)
- assert_equal('1+2/3i', Complex(1,Rational(2,3)).to_s)
- assert_equal('-1+2/3i', Complex(-1,Rational(2,3)).to_s)
- assert_equal('-1-2/3i', Complex(-1,Rational(-2,3)).to_s)
- assert_equal('1-2/3i', Complex(1,Rational(-2,3)).to_s)
- assert_equal('-1-2/3i', Complex(-1,Rational(-2,3)).to_s)
- end
+ 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)
+ assert_equal('-1+2/1i', Complex(-1,Rational(2)).to_s)
+ assert_equal('-1-2/1i', Complex(-1,Rational(-2)).to_s)
+ assert_equal('1-2/1i', Complex(1,Rational(-2)).to_s)
+ assert_equal('-1-2/1i', Complex(-1,Rational(-2)).to_s)
+
+ assert_equal('0+2/3i', Complex(0,Rational(2,3)).to_s)
+ assert_equal('0-2/3i', Complex(0,Rational(-2,3)).to_s)
+ assert_equal('1+2/3i', Complex(1,Rational(2,3)).to_s)
+ assert_equal('-1+2/3i', Complex(-1,Rational(2,3)).to_s)
+ assert_equal('-1-2/3i', Complex(-1,Rational(-2,3)).to_s)
+ assert_equal('1-2/3i', Complex(1,Rational(-2,3)).to_s)
+ assert_equal('-1-2/3i', Complex(-1,Rational(-2,3)).to_s)
nan = 0.0 / 0
inf = 1.0 / 0
@@ -638,29 +741,35 @@ class Complex_Test < Test::Unit::TestCase
assert_equal('(1+2i)', c.inspect)
end
+ def test_inspect_to_s_frozen_bug_20337
+ assert_separately([], <<~'RUBY')
+ class Numeric
+ def inspect = super.freeze
+ end
+ c = Complex(Numeric.new, 1)
+ assert_match(/\A\(#<Numeric:/, c.inspect)
+ assert_match(/\A#<Numeric:/, c.to_s)
+ RUBY
+ end
+
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)
- if @rational
- c = Complex(Rational(1,2),Rational(2,3))
+ c = Complex(Rational(1,2),Rational(2,3))
- s = Marshal.dump(c)
- c2 = Marshal.load(s)
- assert_equal(c, c2)
- assert_instance_of(Complex, c2)
- end
+ s = Marshal.dump(c)
+ c2 = Marshal.load(s)
+ assert_equal(c, c2)
+ assert_instance_of(Complex, c2)
bug3656 = '[ruby-core:31622]'
c = Complex(1,2)
- c.freeze
- assert(c.frozen?)
+ assert_predicate(c, :frozen?)
result = c.marshal_load([2,3]) rescue :fail
assert_equal(:fail, result, bug3656)
assert_equal(Complex(1,2), c)
@@ -820,72 +929,119 @@ class Complex_Test < Test::Unit::TestCase
assert_equal(Complex(0), '_5'.to_c)
assert_equal(Complex(5), '5_'.to_c)
assert_equal(Complex(5), '5x'.to_c)
+ assert_equal(Complex(51), '5_1'.to_c)
+ assert_equal(Complex(5), '5__1'.to_c)
assert_equal(Complex(5), '5+_3i'.to_c)
assert_equal(Complex(5), '5+3_i'.to_c)
assert_equal(Complex(5,3), '5+3i_'.to_c)
assert_equal(Complex(5,3), '5+3ix'.to_c)
+ assert_equal(Complex(5,31), '5+3_1i'.to_c)
+ assert_equal(Complex(5), '5+3__1i'.to_c)
+ assert_equal(Complex(51), Complex('5_1'))
+ assert_equal(Complex(5,31), Complex('5+3_1i'))
+ assert_equal(Complex(5,31), Complex('5+3_1I'))
+ assert_equal(Complex(5,31), Complex('5+3_1j'))
+ assert_equal(Complex(5,31), Complex('5+3_1J'))
+ assert_equal(Complex(0,31), Complex('3_1i'))
+ assert_equal(Complex(0,31), Complex('3_1I'))
+ assert_equal(Complex(0,31), Complex('3_1j'))
+ assert_equal(Complex(0,31), Complex('3_1J'))
assert_raise(ArgumentError){ Complex('')}
assert_raise(ArgumentError){ Complex('_')}
assert_raise(ArgumentError){ Complex("\f\n\r\t\v5\0")}
assert_raise(ArgumentError){ Complex('_5')}
assert_raise(ArgumentError){ Complex('5_')}
+ assert_raise(ArgumentError){ Complex('5__1')}
assert_raise(ArgumentError){ Complex('5x')}
assert_raise(ArgumentError){ Complex('5+_3i')}
assert_raise(ArgumentError){ Complex('5+3_i')}
assert_raise(ArgumentError){ Complex('5+3i_')}
assert_raise(ArgumentError){ Complex('5+3ix')}
+ assert_raise(ArgumentError){ Complex('5+3__1i')}
+ assert_raise(ArgumentError){ Complex('5+3__1I')}
+ assert_raise(ArgumentError){ Complex('5+3__1j')}
+ assert_raise(ArgumentError){ Complex('5+3__1J')}
+ assert_raise(ArgumentError){ Complex('3__1i')}
+ assert_raise(ArgumentError){ Complex('3__1I')}
+ assert_raise(ArgumentError){ Complex('3__1j')}
+ assert_raise(ArgumentError){ Complex('3__1J')}
+
+ 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)
+ assert_equal(Complex(Rational(1,5),-3), '1/5-3i'.to_c)
+ assert_equal(Complex(Rational(-1,5),3), '-1/5+3i'.to_c)
+ assert_equal(Complex(Rational(-1,5),-3), '-1/5-3i'.to_c)
+ assert_equal(Complex(Rational(1,5),Rational(3,2)), '1/5+3/2i'.to_c)
+ assert_equal(Complex(Rational(1,5),Rational(-3,2)), '1/5-3/2i'.to_c)
+ assert_equal(Complex(Rational(-1,5),Rational(3,2)), '-1/5+3/2i'.to_c)
+ assert_equal(Complex(Rational(-1,5),Rational(-3,2)), '-1/5-3/2i'.to_c)
+ assert_equal(Complex(Rational(1,5),Rational(3,2)), '1/5+3/2i'.to_c)
+ assert_equal(Complex(Rational(1,5),Rational(-3,2)), '1/5-3/2i'.to_c)
+ assert_equal(Complex(Rational(-1,5),Rational(3,2)), '-1/5+3/2i'.to_c)
+ assert_equal(Complex(Rational(-1,5),Rational(-3,2)), '-1/5-3/2i'.to_c)
+ assert_equal(Complex.polar(Rational(1,5),Rational(3,2)), Complex('1/5@3/2'))
+ assert_equal(Complex.polar(Rational(-1,5),Rational(-3,2)), Complex('-1/5@-3/2'))
+
+ end
+
+ def test_Complex_with_invalid_exception
+ assert_raise(ArgumentError) {
+ Complex("0", exception: 1)
+ }
+ end
- 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)
- assert_equal(Complex(Rational(1,5),-3), '1/5-3i'.to_c)
- assert_equal(Complex(Rational(-1,5),3), '-1/5+3i'.to_c)
- assert_equal(Complex(Rational(-1,5),-3), '-1/5-3i'.to_c)
- assert_equal(Complex(Rational(1,5),Rational(3,2)), '1/5+3/2i'.to_c)
- assert_equal(Complex(Rational(1,5),Rational(-3,2)), '1/5-3/2i'.to_c)
- assert_equal(Complex(Rational(-1,5),Rational(3,2)), '-1/5+3/2i'.to_c)
- assert_equal(Complex(Rational(-1,5),Rational(-3,2)), '-1/5-3/2i'.to_c)
- assert_equal(Complex(Rational(1,5),Rational(3,2)), '1/5+3/2i'.to_c)
- assert_equal(Complex(Rational(1,5),Rational(-3,2)), '1/5-3/2i'.to_c)
- assert_equal(Complex(Rational(-1,5),Rational(3,2)), '-1/5+3/2i'.to_c)
- assert_equal(Complex(Rational(-1,5),Rational(-3,2)), '-1/5-3/2i'.to_c)
- assert_equal(Complex.polar(Rational(1,5),Rational(3,2)), Complex('1/5@3/2'))
- assert_equal(Complex.polar(Rational(-1,5),Rational(-3,2)), Complex('-1/5@-3/2'))
+ def assert_complex_with_exception(error, *args, message: "")
+ assert_raise(error, message) do
+ Complex(*args, exception: true)
+ end
+ assert_nothing_raised(error, message) do
+ assert_nil(Complex(*args, exception: false))
+ assert_nil($!)
end
+ end
+ def test_Complex_with_exception
+ assert_complex_with_exception(ArgumentError, '5x')
+ assert_complex_with_exception(TypeError, nil)
+ assert_complex_with_exception(TypeError, Object.new)
+ assert_complex_with_exception(TypeError, 1, nil)
+ assert_complex_with_exception(TypeError, 1, Object.new)
+
+ o = Object.new
+ def o.to_c; raise; end
+ assert_complex_with_exception(RuntimeError, o)
+ assert_complex_with_exception(TypeError, 1, o)
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
@@ -903,12 +1059,33 @@ class Complex_Test < Test::Unit::TestCase
end
def test_to_r
- 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))}
- end
+ 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))}
+ end
+
+ def test_to_r_with_float
+ assert_equal(Rational(3), Complex(3, 0.0).to_r)
+ assert_raise(RangeError){Complex(3, 1.0).to_r}
+ end
+
+ def test_to_r_with_numeric_obj
+ c = Class.new(Numeric)
+
+ num = 0
+ c.define_method(:to_s) { num.to_s }
+ c.define_method(:==) { num == it }
+ c.define_method(:<) { num < it }
+
+ o = c.new
+ assert_equal(Rational(3), Complex(3, o).to_r)
+
+ num = 1
+ assert_raise(RangeError){Complex(3, o).to_r}
+
+ c.define_method(:to_r) { 0r }
+ assert_equal(Rational(3), Complex(3, o).to_r)
end
def test_to_c
@@ -924,10 +1101,8 @@ class Complex_Test < Test::Unit::TestCase
c = 1.1.to_c
assert_equal([1.1, 0], [c.real, c.imag])
- if @rational
- c = Rational(1,2).to_c
- assert_equal([Rational(1,2), 0], [c.real, c.imag])
- end
+ c = Rational(1,2).to_c
+ assert_equal([Rational(1,2), 0], [c.real, c.imag])
c = Complex(1,2).to_c
assert_equal([1, 2], [c.real, c.imag])
@@ -940,9 +1115,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)
@@ -972,9 +1183,9 @@ class Complex_Test < Test::Unit::TestCase
if (0.0/0).nan?
nan = 0.0/0
- assert(nan.arg.equal?(nan))
- assert(nan.angle.equal?(nan))
- assert(nan.phase.equal?(nan))
+ assert_same(nan, nan.arg)
+ assert_same(nan, nan.angle)
+ assert_same(nan, nan.phase)
end
assert_equal(Math::PI, -1.arg)
@@ -1011,134 +1222,13 @@ class Complex_Test < Test::Unit::TestCase
assert_equal(1.1, 1.1.conj)
assert_equal(-1.1, -1.1.conj)
- 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 @rational && !@keiju
- assert_equal(Complex(Rational(1,2),Rational(1)), Complex(1,2).quo(2))
- end
-=end
+ assert_equal(Complex(Rational(1,2),Rational(1)), Complex(1,2).quo(2))
assert_equal(0.5, 1.fdiv(2))
assert_equal(5000000000.0, 10000000000.fdiv(2))
assert_equal(0.5, 1.0.fdiv(2))
- if @rational
- assert_equal(0.25, Rational(1,2).fdiv(2))
- end
+ assert_equal(0.25, Rational(1,2).fdiv(2))
assert_equal(Complex(0.5,1.0), Complex(1,2).quo(2))
-
- unless $".grep(/(?:\A|(?<!add)\/)complex/).empty?
- assert_equal(Complex(0,2), Math.sqrt(-4.0))
-# assert_equal(true, Math.sqrt(-4.0).inexact?)
- assert_equal(Complex(0,2), Math.sqrt(-4))
-# assert_equal(true, Math.sqrt(-4).exact?)
- if @rational
- assert_equal(Complex(0,2), Math.sqrt(Rational(-4)))
-# assert_equal(true, Math.sqrt(Rational(-4)).exact?)
- end
-
- assert_equal(Complex(0,3), Math.sqrt(-9.0))
-# assert_equal(true, Math.sqrt(-9.0).inexact?)
- assert_equal(Complex(0,3), Math.sqrt(-9))
-# assert_equal(true, Math.sqrt(-9).exact?)
- if @rational
- assert_equal(Complex(0,3), Math.sqrt(Rational(-9)))
-# assert_equal(true, Math.sqrt(Rational(-9)).exact?)
- end
-
- c = Math.sqrt(Complex(1, 2))
- assert_in_delta(1.272, c.real, 0.001)
- assert_in_delta(0.786, c.imag, 0.001)
-
- c = Math.sqrt(-9)
- assert_in_delta(0.0, c.real, 0.001)
- assert_in_delta(3.0, c.imag, 0.001)
-
- c = Math.exp(Complex(1, 2))
- assert_in_delta(-1.131, c.real, 0.001)
- assert_in_delta(2.471, c.imag, 0.001)
-
- c = Math.sin(Complex(1, 2))
- assert_in_delta(3.165, c.real, 0.001)
- assert_in_delta(1.959, c.imag, 0.001)
-
- c = Math.cos(Complex(1, 2))
- assert_in_delta(2.032, c.real, 0.001)
- assert_in_delta(-3.051, c.imag, 0.001)
-
- c = Math.tan(Complex(1, 2))
- assert_in_delta(0.033, c.real, 0.001)
- assert_in_delta(1.014, c.imag, 0.001)
-
- c = Math.sinh(Complex(1, 2))
- assert_in_delta(-0.489, c.real, 0.001)
- assert_in_delta(1.403, c.imag, 0.001)
-
- c = Math.cosh(Complex(1, 2))
- assert_in_delta(-0.642, c.real, 0.001)
- assert_in_delta(1.068, c.imag, 0.001)
-
- c = Math.tanh(Complex(1, 2))
- assert_in_delta(1.166, c.real, 0.001)
- assert_in_delta(-0.243, c.imag, 0.001)
-
- c = Math.log(Complex(1, 2))
- assert_in_delta(0.804, c.real, 0.001)
- assert_in_delta(1.107, c.imag, 0.001)
-
- c = Math.log(Complex(1, 2), Math::E)
- assert_in_delta(0.804, c.real, 0.001)
- assert_in_delta(1.107, c.imag, 0.001)
-
- c = Math.log(-1)
- assert_in_delta(0.0, c.real, 0.001)
- assert_in_delta(Math::PI, c.imag, 0.001)
-
- c = Math.log(8, 2)
- assert_in_delta(3.0, c.real, 0.001)
- assert_in_delta(0.0, c.imag, 0.001)
-
- c = Math.log(-8, -2)
- assert_in_delta(1.092, c.real, 0.001)
- assert_in_delta(-0.420, c.imag, 0.001)
-
- c = Math.log10(Complex(1, 2))
- assert_in_delta(0.349, c.real, 0.001)
- assert_in_delta(0.480, c.imag, 0.001)
-
- c = Math.asin(Complex(1, 2))
- assert_in_delta(0.427, c.real, 0.001)
- assert_in_delta(1.528, c.imag, 0.001)
-
- c = Math.acos(Complex(1, 2))
- assert_in_delta(1.143, c.real, 0.001)
- assert_in_delta(-1.528, c.imag, 0.001)
-
- c = Math.atan(Complex(1, 2))
- assert_in_delta(1.338, c.real, 0.001)
- assert_in_delta(0.402, c.imag, 0.001)
-
- c = Math.atan2(Complex(1, 2), 1)
- assert_in_delta(1.338, c.real, 0.001)
- assert_in_delta(0.402, c.imag, 0.001)
-
- c = Math.asinh(Complex(1, 2))
- assert_in_delta(1.469, c.real, 0.001)
- assert_in_delta(1.063, c.imag, 0.001)
-
- c = Math.acosh(Complex(1, 2))
- assert_in_delta(1.528, c.real, 0.001)
- assert_in_delta(1.143, c.imag, 0.001)
-
- c = Math.atanh(Complex(1, 2))
- assert_in_delta(0.173, c.real, 0.001)
- assert_in_delta(1.178, c.imag, 0.001)
- end
-
end
def test_ruby19
@@ -1148,9 +1238,7 @@ class Complex_Test < Test::Unit::TestCase
end
def test_fixed_bug
- if @rational && !@keiju
- assert_equal(Complex(1), 1 ** Complex(1))
- end
+ assert_equal(Complex(1), 1 ** Complex(1))
assert_equal('-1.0-0.0i', Complex(-1.0, -0.0).to_s)
assert_in_delta(Math::PI, Complex(-0.0).arg, 0.001)
assert_equal(Complex(2e3, 2e4), '2e3+2e4i'.to_c)
@@ -1160,4 +1248,49 @@ 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
+ error = "not a real"
+ assert_raise_with_message(TypeError, error) do
+ Complex.polar(1i)
+ end
+ assert_raise_with_message(TypeError, error) do
+ Complex.polar(1i, 0)
+ end
+ assert_raise_with_message(TypeError, error) do
+ Complex.polar(0, 1i)
+ end
+ n = Class.new(Numeric) do
+ def initialize(x = 1)
+ @x = x
+ end
+ def real?
+ (@x -= 1) > 0
+ end
+ end
+ obj = n.new
+ assert_raise_with_message(TypeError, error) do
+ Complex.polar(obj)
+ end
+ obj = n.new
+ assert_raise_with_message(TypeError, error) do
+ Complex.polar(obj, 0)
+ end
+ obj = n.new
+ assert_raise_with_message(TypeError, error) do
+ Complex.polar(1, obj)
+ end
+ end
end