Skip to content

Commit 2571168

Browse files
committed
Fix regression when testing inclusion in unbounded ranges
Caused by 04a92a6. This treats unbounded ranges of arbitrary objects the same as how unbounded string ranges are treated: (..x) === y # (y <=> x) <= 0 (...x) === y # (y <=> x) < 0 (x..) === y # (x <=> y) <= 0 Fixes [Bug #19864]
1 parent 8835ca2 commit 2571168

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

range.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1820,6 +1820,7 @@ range_string_cover_internal(VALUE range, VALUE val)
18201820
return r_cover_p(range, beg, end, val);
18211821
}
18221822
if (NIL_P(beg)) {
1823+
unbounded_begin:;
18231824
VALUE r = rb_funcall(val, id_cmp, 1, end);
18241825
if (NIL_P(r)) return Qfalse;
18251826
if (RANGE_EXCL(range)) {
@@ -1828,12 +1829,20 @@ range_string_cover_internal(VALUE range, VALUE val)
18281829
return RBOOL(rb_cmpint(r, val, end) <= 0);
18291830
}
18301831
else if (NIL_P(end)) {
1832+
unbounded_end:;
18311833
VALUE r = rb_funcall(beg, id_cmp, 1, val);
18321834
if (NIL_P(r)) return Qfalse;
18331835
return RBOOL(rb_cmpint(r, beg, val) <= 0);
18341836
}
18351837
}
18361838

1839+
if (!NIL_P(beg) && NIL_P(end)) {
1840+
goto unbounded_end;
1841+
}
1842+
if (NIL_P(beg) && !NIL_P(end)) {
1843+
goto unbounded_begin;
1844+
}
1845+
18371846
return range_include_fallback(beg, end, val);
18381847
}
18391848

test/ruby/test_range.rb

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
require 'test/unit'
33
require 'delegate'
44
require 'timeout'
5+
require 'date'
56
require 'rbconfig/sizeof'
67

78
class TestRange < Test::Unit::TestCase
@@ -624,6 +625,28 @@ def <=>(other)
624625
assert_operator(c.new(0)..c.new(10), :===, c.new(5), bug12003)
625626
end
626627

628+
def test_eqq_unbounded_ruby_bug_19864
629+
t1 = Date.today
630+
t2 = t1 + 1
631+
assert_equal(true, (..t1) === t1)
632+
assert_equal(false, (..t1) === t2)
633+
assert_equal(true, (..t2) === t1)
634+
assert_equal(true, (..t2) === t2)
635+
assert_equal(false, (...t1) === t1)
636+
assert_equal(false, (...t1) === t2)
637+
assert_equal(true, (...t2) === t1)
638+
assert_equal(false, (...t2) === t2)
639+
640+
assert_equal(true, (t1..) === t1)
641+
assert_equal(true, (t1..) === t2)
642+
assert_equal(false, (t2..) === t1)
643+
assert_equal(true, (t2..) === t2)
644+
assert_equal(true, (t1...) === t1)
645+
assert_equal(true, (t1...) === t2)
646+
assert_equal(false, (t2...) === t1)
647+
assert_equal(true, (t2...) === t2)
648+
end
649+
627650
def test_eqq_non_iteratable
628651
k = Class.new do
629652
include Comparable

0 commit comments

Comments
 (0)