diff --git a/ext/date/date_core.c b/ext/date/date_core.c index b3040171..9e9337da 100644 --- a/ext/date/date_core.c +++ b/ext/date/date_core.c @@ -6872,7 +6872,7 @@ d_lite_step(int argc, VALUE *argv, VALUE self) date = self; c = f_cmp(step, INT2FIX(0)); if (c < 0) { - while (FIX2INT(d_lite_cmp(date, limit)) >= 0) { + while (rb_cmpint(d_lite_cmp(date, limit), date, limit) >= 0) { rb_yield(date); date = d_lite_plus(date, step); } @@ -6882,7 +6882,7 @@ d_lite_step(int argc, VALUE *argv, VALUE self) rb_yield(date); } else /* if (c > 0) */ { - while (FIX2INT(d_lite_cmp(date, limit)) <= 0) { + while (rb_cmpint(d_lite_cmp(date, limit), date, limit) <= 0) { rb_yield(date); date = d_lite_plus(date, step); } @@ -6904,7 +6904,7 @@ d_lite_upto(VALUE self, VALUE max) RETURN_ENUMERATOR(self, 1, &max); date = self; - while (FIX2INT(d_lite_cmp(date, max)) <= 0) { + while (rb_cmpint(d_lite_cmp(date, max), date, max) <= 0) { rb_yield(date); date = d_lite_plus(date, INT2FIX(1)); } @@ -6925,7 +6925,7 @@ d_lite_downto(VALUE self, VALUE min) RETURN_ENUMERATOR(self, 1, &min); date = self; - while (FIX2INT(d_lite_cmp(date, min)) >= 0) { + while (rb_cmpint(d_lite_cmp(date, min), date, min) >= 0) { rb_yield(date); date = d_lite_plus(date, INT2FIX(-1)); } @@ -6937,8 +6937,10 @@ cmp_gen(VALUE self, VALUE other) { get_d1(self); - if (k_numeric_p(other)) - return INT2FIX(f_cmp(m_ajd(dat), other)); + if (k_numeric_p(other)) { + VALUE c = rb_funcallv(m_ajd(dat), id_cmp, 1, &other); + return NIL_P(c) ? Qnil : INT2FIX(rb_cmpint(c, self, other)); + } else if (k_date_p(other)) return INT2FIX(f_cmp(m_ajd(dat), f_ajd(other))); return rb_num_coerce_cmp(self, other, id_cmp); diff --git a/test/date/test_date_arith.rb b/test/date/test_date_arith.rb index e148dabc..bdab5399 100644 --- a/test/date/test_date_arith.rb +++ b/test/date/test_date_arith.rb @@ -147,6 +147,15 @@ def test__compare DateTime.new(2001,2,3,0,0,0,'+00:00'))) assert_equal(-1, (DateTime.new(2001,2,3,0,0,0,'+09:00') <=> DateTime.new(2001,2,2,16,0,0,'+00:00'))) + n = Class.new(Numeric) {def coerce(x) [self, x] end; def <=>(x) 5 end}.new + assert_equal(1, (Date.new(2000,1,1) <=> n)) + end + + def test__compare_nan + [Date.new(2000,1,1), DateTime.new(2000,1,1,12)].each do |d| + assert_nil(d <=> Float::NAN) + assert_equal(false, d == Float::NAN) + end end def test_prev @@ -371,4 +380,15 @@ def o.<=>(*);2;end p.step(q, o) {|d| a << d} assert_empty(a) end + + def test_step__incomparable_limit + d = Date.new(2000,1,1) + [Float::NAN, Object.new].each do |limit| + no = proc {flunk("yielded for #{limit.inspect}")} + assert_raise(ArgumentError) { d.upto(limit, &no) } + assert_raise(ArgumentError) { d.downto(limit, &no) } + assert_raise(ArgumentError) { d.step(limit, 1, &no) } + assert_raise(ArgumentError) { d.step(limit, -1, &no) } + end + end end