diff options
author | 卜部昌平 <shyouhei@ruby-lang.org> | 2021-05-07 12:49:32 +0900 |
---|---|---|
committer | 卜部昌平 <shyouhei@ruby-lang.org> | 2021-05-12 10:30:46 +0900 |
commit | cc0dc67bbbe1951ff90004bc987f78545625d772 (patch) | |
tree | b6bf1c68d335875409ecd96bf56ef6e5dec64f92 | |
parent | d0e6c6e682b9ba2b0309a5177933a0628e8ef316 (diff) |
cdhash_cmp: can also take complex
There are complex literals `123i`, which can also be a case condition.
Notes
Notes:
Merged: https://github.com/ruby/ruby/pull/4469
-rw-r--r-- | compile.c | 15 | ||||
-rw-r--r-- | complex.c | 12 | ||||
-rw-r--r-- | internal/complex.h | 1 | ||||
-rw-r--r-- | test/ruby/test_rational.rb | 4 |
4 files changed, 25 insertions, 7 deletions
@@ -2005,10 +2005,15 @@ cdhash_cmp(VALUE val, VALUE lit) else if (tlit == T_FLOAT) { return rb_float_cmp(lit, val); } - else if (tlit == T_RATIONAL) { - const struct RRational *dat1 = RRATIONAL(val); - const struct RRational *dat2 = RRATIONAL(lit); - return (dat1->num == dat2->num) && (dat1->den == dat2->den); + else if (tlit == T_RATIONAL) { + const struct RRational *rat1 = RRATIONAL(val); + const struct RRational *rat2 = RRATIONAL(lit); + return (rat1->num == rat2->num) && (rat1->den == rat2->den); + } + else if (tlit == T_COMPLEX) { + const struct RComplex *comp1 = RCOMPLEX(val); + const struct RComplex *comp2 = RCOMPLEX(lit); + return (comp1->real == comp2->real) && (comp1->imag == comp2->imag); } else { UNREACHABLE_RETURN(-1); @@ -2030,6 +2035,8 @@ cdhash_hash(VALUE a) return rb_dbl_long_hash(RFLOAT_VALUE(a)); case T_RATIONAL: return rb_rational_hash(a); + case T_COMPLEX: + return rb_complex_hash(a); default: UNREACHABLE_RETURN(0); } @@ -1326,8 +1326,8 @@ nucomp_numerator(VALUE self) } /* :nodoc: */ -static VALUE -nucomp_hash(VALUE self) +st_index_t +rb_complex_hash(VALUE self) { st_index_t v, h[2]; VALUE n; @@ -1338,7 +1338,13 @@ nucomp_hash(VALUE self) n = rb_hash(dat->imag); h[1] = NUM2LONG(n); v = rb_memhash(h, sizeof(h)); - return ST2FIX(v); + return v; +} + +static VALUE +nucomp_hash(VALUE self) +{ + return ST2FIX(rb_complex_hash(self)); } /* :nodoc: */ diff --git a/internal/complex.h b/internal/complex.h index 6f63a9ebeb..9eae804ddb 100644 --- a/internal/complex.h +++ b/internal/complex.h @@ -25,5 +25,6 @@ struct RComplex { /* complex.c */ VALUE rb_dbl_complex_new_polar_pi(double abs, double ang); +st_index_t rb_complex_hash(VALUE comp); #endif /* INTERNAL_COMPLEX_H */ diff --git a/test/ruby/test_rational.rb b/test/ruby/test_rational.rb index 5262b6edad..27d0527d42 100644 --- a/test/ruby/test_rational.rb +++ b/test/ruby/test_rational.rb @@ -839,6 +839,10 @@ class Rational_Test < Test::Unit::TestCase n = case 3/2r when 1.5r then true else false end assert_equal(n, true, '[ruby-core:103759] [Bug #17854]') RUBY + assert_separately([], <<-RUBY) + n = case 1i when 1i then true else false end + assert_equal(n, true, '[ruby-core:103759] [Bug #17854]') + RUBY end def test_Rational_with_invalid_exception |