Project

General

Profile

Bug #15807 ยป range-minmax.patch

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

View differences:

range.c
1211 1211
    }
1212 1212
}
1213 1213

  
1214
/*
1215
 *  call-seq:
1216
 *     rng.minmax                       -> [obj, obj]
1217
 *     rng.minmax {| a,b | block }      -> [obj, obj]
1218
 *
1219
 *  Returns a two element array which contains the minimum and the
1220
 *  maximum value in the range.
1221
 *
1222
 *  Can be given an optional block to override the default comparison
1223
 *  method <code>a <=> b</code>.
1224
 */
1225

  
1226
static VALUE
1227
range_minmax(VALUE range)
1228
{
1229
    if (rb_block_given_p()) {
1230
        return rb_call_super(0, NULL);
1231
    }
1232
    return rb_ary_new_from_args(2, range_min(0, NULL, range), range_max(0, NULL, range));
1233
}
1234

  
1214 1235
int
1215 1236
rb_range_values(VALUE range, VALUE *begp, VALUE *endp, int *exclp)
1216 1237
{
......
1694 1715
    rb_define_method(rb_cRange, "last", range_last, -1);
1695 1716
    rb_define_method(rb_cRange, "min", range_min, -1);
1696 1717
    rb_define_method(rb_cRange, "max", range_max, -1);
1718
    rb_define_method(rb_cRange, "minmax", range_minmax, 0);
1697 1719
    rb_define_method(rb_cRange, "size", range_size, 0);
1698 1720
    rb_define_method(rb_cRange, "to_a", range_to_a, 0);
1699 1721
    rb_define_method(rb_cRange, "entries", range_to_a, 0);
test/ruby/test_range.rb
121 121
    assert_raise(RangeError) { (1...).max(3) }
122 122
  end
123 123

  
124
  def test_minmax
125
    assert_equal([1, 2], (1..2).minmax)
126
    assert_equal([nil, nil], (2..1).minmax)
127
    assert_equal([1, 1], (1...2).minmax)
128
    assert_raise(RangeError) { (1..).minmax }
129
    assert_raise(RangeError) { (1...).minmax }
130

  
131
    assert_equal([1.0, 2.0], (1.0..2.0).minmax)
132
    assert_equal([nil, nil], (2.0..1.0).minmax)
133
    assert_raise(TypeError) { (1.0...2.0).minmax }
134
    assert_raise(TypeError) { (1...1.5).minmax }
135
    assert_raise(TypeError) { (1.5...2).minmax }
136

  
137
    assert_equal([-0x80000002, -0x80000002], ((-0x80000002)...(-0x80000001)).minmax)
138

  
139
    assert_equal([0, 0], (0..0).minmax)
140
    assert_equal([nil, nil], (0...0).minmax)
141

  
142
    assert_equal([2, 1], (1..2).minmax{|a, b| b <=> a})
143
  end
144

  
124 145
  def test_initialize_twice
125 146
    r = eval("1..2")
126 147
    assert_raise(NameError) { r.instance_eval { initialize 3, 4 } }
127
-