diff options
author | Jeremy Evans <code@jeremyevans.net> | 2019-06-04 21:41:02 -0700 |
---|---|---|
committer | Jeremy Evans <code@jeremyevans.net> | 2019-06-19 10:50:58 -0700 |
commit | b9ef35e4c6325864e013ab6e45df6fe00f759a47 (patch) | |
tree | 0e3814c316dd16034139a4b2802a69aca469da7c /test/ruby/test_complex.rb | |
parent | 65944e96d39a8ae3602751f49cb337335b2c6c45 (diff) |
Implement Complex#<=>
Implement Complex#<=> so that it is usable as an argument when
calling <=> on objects of other classes (since #coerce will coerce
such numbers to Complex). If the complex number has a zero imaginary
part, and the other argument is a real number (or complex number with
zero imaginary part), return -1, 0, or 1. Otherwise, return nil,
indicating the objects are not comparable.
Fixes [Bug #15857]
Diffstat (limited to 'test/ruby/test_complex.rb')
-rw-r--r-- | test/ruby/test_complex.rb | 14 |
1 files changed, 10 insertions, 4 deletions
diff --git a/test/ruby/test_complex.rb b/test/ruby/test_complex.rb index 3da66890c1..60a46d737d 100644 --- a/test/ruby/test_complex.rb +++ b/test/ruby/test_complex.rb @@ -518,9 +518,16 @@ class Complex_Test < Test::Unit::TestCase 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 @@ -891,7 +898,6 @@ class Complex_Test < Test::Unit::TestCase def test_respond c = Complex(1,1) assert_not_respond_to(c, :%) - assert_not_respond_to(c, :<=>) assert_not_respond_to(c, :div) assert_not_respond_to(c, :divmod) assert_not_respond_to(c, :floor) |