From ee2ddf5411f51ea89a1569ea5d70b41cd4c6c7f2 Mon Sep 17 00:00:00 2001 From: mrkn Date: Wed, 2 Jan 2019 04:25:11 +0000 Subject: complex.c: fix against redefining component methods This fixes the incompatibility (maybe unintentionally) introduced by removal of `#ifndef PRESERVE_SIGNEDZERO` guards in f_add, f_mul, and f_sub functions in r62701. [Bug #15491] [ruby-core:90843] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@66688 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- test/ruby/test_complex.rb | 73 +++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 68 insertions(+), 5 deletions(-) (limited to 'test') diff --git a/test/ruby/test_complex.rb b/test/ruby/test_complex.rb index 7c1418b2bf..98ed42dae9 100644 --- a/test/ruby/test_complex.rb +++ b/test/ruby/test_complex.rb @@ -276,7 +276,7 @@ class Complex_Test < Test::Unit::TestCase def +(other); 42; end end a = Complex(1, 2) + Complex(0, 1) - puts a == Complex(1, 42) + puts a == Complex(42, 42) end; end @@ -286,8 +286,8 @@ class Complex_Test < Test::Unit::TestCase remove_method :+ def +(other); 42.0; end end - a = Complex(1, 2.0) + Complex(0, 1) - puts a == Complex(1, 42.0) + a = Complex(1.0, 2.0) + Complex(0, 1) + puts a == Complex(42.0, 42.0) end; end @@ -297,8 +297,8 @@ class Complex_Test < Test::Unit::TestCase remove_method :+ def +(other); 355/113r; end end - a = Complex(1, 2r) + Complex(0, 1) - puts a == Complex(1, 355/113r) + a = Complex(1r, 2r) + Complex(0, 1) + puts a == Complex(355/113r, 355/113r) end; end @@ -315,6 +315,36 @@ 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 + 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 + 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 + 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) @@ -333,6 +363,39 @@ 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 + 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 + 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 + 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 -- cgit v1.2.3