mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
Fix Range#max for beginless Integer ranges [Bug #17034]
* Fix Range#max for beginless Integer ranges * Update test/ruby/test_range.rb * Fix formatting https://github.com/ruby/ruby/pull/3328 Co-authored-by: Nobuyoshi Nakada <nobu@ruby-lang.org>
This commit is contained in:
parent
b4e784434c
commit
8a5ad2b77d
Notes:
git
2020-07-19 01:19:15 +09:00
Merged-By: nobu <nobu@ruby-lang.org>
2 changed files with 8 additions and 3 deletions
7
range.c
7
range.c
|
@ -1220,16 +1220,17 @@ range_max(int argc, VALUE *argv, VALUE range)
|
|||
rb_raise(rb_eRangeError, "cannot get the maximum of endless range");
|
||||
}
|
||||
|
||||
VALUE b = RANGE_BEG(range);
|
||||
|
||||
if (rb_block_given_p() || (EXCL(range) && !nm) || argc) {
|
||||
if (NIL_P(RANGE_BEG(range))) {
|
||||
if (NIL_P(b)) {
|
||||
rb_raise(rb_eRangeError, "cannot get the maximum of beginless range with custom comparison method");
|
||||
}
|
||||
return rb_call_super(argc, argv);
|
||||
}
|
||||
else {
|
||||
struct cmp_opt_data cmp_opt = { 0, 0 };
|
||||
VALUE b = RANGE_BEG(range);
|
||||
int c = OPTIMIZED_CMP(b, e, cmp_opt);
|
||||
int c = NIL_P(b) ? -1 : OPTIMIZED_CMP(b, e, cmp_opt);
|
||||
|
||||
if (c > 0)
|
||||
return Qnil;
|
||||
|
|
|
@ -127,6 +127,10 @@ class TestRange < Test::Unit::TestCase
|
|||
assert_raise(RangeError) { (1...).max(3) }
|
||||
|
||||
assert_raise(RangeError) { (..0).min {|a, b| a <=> b } }
|
||||
|
||||
assert_equal(2, (..2).max)
|
||||
assert_raise(TypeError) { (...2).max }
|
||||
assert_raise(TypeError) { (...2.0).max }
|
||||
end
|
||||
|
||||
def test_minmax
|
||||
|
|
Loading…
Add table
Reference in a new issue