1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

cdhash_cmp: can also take complex

There are complex literals `123i`, which can also be a case condition.
This commit is contained in:
卜部昌平 2021-05-07 12:49:32 +09:00
parent d0e6c6e682
commit cc0dc67bbb
Notes: git 2021-05-12 10:31:14 +09:00
4 changed files with 25 additions and 7 deletions

View file

@ -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);
}

View file

@ -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: */

View file

@ -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 */

View file

@ -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