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

range.c: Range#min with a beginless one now raise an explicit exception

[Bug #16450]
This commit is contained in:
Yusuke Endoh 2019-12-25 13:35:22 +09:00
parent cd6c013b07
commit 81e377023c
2 changed files with 13 additions and 0 deletions

View file

@ -1136,6 +1136,10 @@ range_last(int argc, VALUE *argv, VALUE range)
static VALUE
range_min(int argc, VALUE *argv, VALUE range)
{
if (NIL_P(RANGE_BEG(range))) {
rb_raise(rb_eRangeError, "cannot get the minimum of beginless range");
}
if (rb_block_given_p()) {
if (NIL_P(RANGE_END(range))) {
rb_raise(rb_eRangeError, "cannot get the minimum of endless range with custom comparison method");
@ -1185,6 +1189,9 @@ range_max(int argc, VALUE *argv, VALUE range)
}
if (rb_block_given_p() || (EXCL(range) && !nm) || argc) {
if (NIL_P(RANGE_BEG(range))) {
rb_raise(rb_eRangeError, "cannot get the maximum of beginless range with custom comparison method");
}
return rb_call_super(argc, argv);
}
else {

View file

@ -81,6 +81,8 @@ class TestRange < Test::Unit::TestCase
assert_equal(nil, (2..1).min)
assert_equal(1, (1...2).min)
assert_equal(1, (1..).min)
assert_raise(RangeError) { (..1).min }
assert_raise(RangeError) { (...1).min }
assert_equal(1.0, (1.0..2.0).min)
assert_equal(nil, (2.0..1.0).min)
@ -93,6 +95,8 @@ class TestRange < Test::Unit::TestCase
assert_equal([0,1,2], (0..10).min(3))
assert_equal([0,1], (0..1).min(3))
assert_equal([0,1,2], (0..).min(3))
assert_raise(RangeError) { (..1).min(3) }
assert_raise(RangeError) { (...1).min(3) }
assert_raise(RangeError) { (0..).min {|a, b| a <=> b } }
end
@ -119,6 +123,8 @@ class TestRange < Test::Unit::TestCase
assert_equal([9,8,7], (0...10).max(3))
assert_raise(RangeError) { (1..).max(3) }
assert_raise(RangeError) { (1...).max(3) }
assert_raise(RangeError) { (..0).min {|a, b| a <=> b } }
end
def test_minmax