summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorKenta Murata <3959+mrkn@users.noreply.github.com>2024-02-27 09:37:03 +0900
committerGitHub <noreply@github.com>2024-02-27 09:37:03 +0900
commit54a5b829442eb7ed1f4a2794ebb26696cf784e64 (patch)
tree1bdb2b9212fc19faf9e7babd3e6324d6fb10aaee /test
parent89f0c0ceb5e5e20c56e63498521966c499b361a5 (diff)
Handle zero-like imaginary part as zero in to_r (#9581)
Fixes [Bug #5179]
Diffstat (limited to 'test')
-rw-r--r--test/ruby/test_complex.rb23
1 files changed, 23 insertions, 0 deletions
diff --git a/test/ruby/test_complex.rb b/test/ruby/test_complex.rb
index 8817a62873..c0cfb73235 100644
--- a/test/ruby/test_complex.rb
+++ b/test/ruby/test_complex.rb
@@ -1054,6 +1054,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])