diff options
Diffstat (limited to 'test/ruby/test_complex.rb')
-rw-r--r-- | test/ruby/test_complex.rb | 218 |
1 files changed, 182 insertions, 36 deletions
diff --git a/test/ruby/test_complex.rb b/test/ruby/test_complex.rb index a3a7546575..bb131cee91 100644 --- a/test/ruby/test_complex.rb +++ b/test/ruby/test_complex.rb @@ -221,10 +221,16 @@ class Complex_Test < Test::Unit::TestCase 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; + 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 @@ -520,6 +526,71 @@ 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) + 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?) @@ -567,20 +638,24 @@ class Complex_Test < Test::Unit::TestCase 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)) + 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 @@ -666,6 +741,17 @@ 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) @@ -843,20 +929,42 @@ 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) @@ -883,31 +991,27 @@ class Complex_Test < Test::Unit::TestCase } 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)) - } + 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_nothing_raised(ArgumentError){ - assert_equal(nil, Complex(o, exception: false)) - } - assert_nothing_raised(ArgumentError){ - assert_equal(nil, Complex(1, o, exception: false)) - } + assert_complex_with_exception(RuntimeError, o) + assert_complex_with_exception(TypeError, 1, o) end def test_respond @@ -961,6 +1065,29 @@ class Complex_Test < Test::Unit::TestCase 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 c = nil.to_c assert_equal([0,0], [c.real, c.imag]) @@ -1135,15 +1262,34 @@ class Complex_Test < Test::Unit::TestCase end def test_canonicalize_polar - obj = Class.new(Numeric) do - def initialize - @x = 2 + 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.new - assert_raise(TypeError) do + 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 |