Project

General

Profile

Bug #15807 ยป range-minmax.patch

jeremyevans0 (Jeremy Evans), 06/08/2019 05:16 AM

View differences:

range.c
}
}
/*
* call-seq:
* rng.minmax -> [obj, obj]
* rng.minmax {| a,b | block } -> [obj, obj]
*
* Returns a two element array which contains the minimum and the
* maximum value in the range.
*
* Can be given an optional block to override the default comparison
* method <code>a <=> b</code>.
*/
static VALUE
range_minmax(VALUE range)
{
if (rb_block_given_p()) {
return rb_call_super(0, NULL);
}
return rb_ary_new_from_args(2, range_min(0, NULL, range), range_max(0, NULL, range));
}
int
rb_range_values(VALUE range, VALUE *begp, VALUE *endp, int *exclp)
{
......
rb_define_method(rb_cRange, "last", range_last, -1);
rb_define_method(rb_cRange, "min", range_min, -1);
rb_define_method(rb_cRange, "max", range_max, -1);
rb_define_method(rb_cRange, "minmax", range_minmax, 0);
rb_define_method(rb_cRange, "size", range_size, 0);
rb_define_method(rb_cRange, "to_a", range_to_a, 0);
rb_define_method(rb_cRange, "entries", range_to_a, 0);
test/ruby/test_range.rb
assert_raise(RangeError) { (1...).max(3) }
end
def test_minmax
assert_equal([1, 2], (1..2).minmax)
assert_equal([nil, nil], (2..1).minmax)
assert_equal([1, 1], (1...2).minmax)
assert_raise(RangeError) { (1..).minmax }
assert_raise(RangeError) { (1...).minmax }
assert_equal([1.0, 2.0], (1.0..2.0).minmax)
assert_equal([nil, nil], (2.0..1.0).minmax)
assert_raise(TypeError) { (1.0...2.0).minmax }
assert_raise(TypeError) { (1...1.5).minmax }
assert_raise(TypeError) { (1.5...2).minmax }
assert_equal([-0x80000002, -0x80000002], ((-0x80000002)...(-0x80000001)).minmax)
assert_equal([0, 0], (0..0).minmax)
assert_equal([nil, nil], (0...0).minmax)
assert_equal([2, 1], (1..2).minmax{|a, b| b <=> a})
end
def test_initialize_twice
r = eval("1..2")
assert_raise(NameError) { r.instance_eval { initialize 3, 4 } }
    (1-1/1)