Project

General

Profile

Bug #11471 ยป enum_nmin_filter_fix.patch

helfper (Helder Pereira), 08/21/2015 01:31 AM

View differences:

enum.c
1192 1192
    long numelts;
1193 1193

  
1194 1194
    long left, right;
1195
    long store_index;
1195 1196

  
1196 1197
    long i, j;
1197 1198

  
......
1217 1218

  
1218 1219
    while (1) {
1219 1220
	long pivot_index = left + (right-left)/2;
1220
	long store_index;
1221 1221
	long num_pivots = 1;
1222 1222

  
1223 1223
	SWAP(pivot_index, right);
......
1261 1261
#undef GETPTR
1262 1262
#undef SWAP
1263 1263

  
1264
    data->limit = RARRAY_PTR(data->buf)[store_index*eltsize];
1264 1265
    data->curlen = data->n;
1265 1266
    rb_ary_resize(data->buf, data->n * eltsize);
1266
    data->limit = RARRAY_PTR(data->buf)[(data->n-1)*eltsize];
1267 1267
}
1268 1268

  
1269 1269
static VALUE
......
1283 1283
        int c = data->cmpfunc(&cmpv, &data->limit, data);
1284 1284
        if (data->rev)
1285 1285
            c = -c;
1286
        if (c > 0)
1286
        if (c >= 0)
1287 1287
            return Qnil;
1288 1288
    }
1289 1289

  
test/ruby/test_enum.rb
289 289
    assert_equal(%w[albatross dog], ary.min(2))
290 290
    assert_equal(%w[dog horse],
291 291
                 ary.min(2) {|a,b| a.length <=> b.length })
292
    assert_equal([13, 14], [20, 32, 32, 21, 30, 25, 29, 13, 14].min(2))
293
    assert_equal([2, 4, 6, 7], [2, 4, 8, 6, 7].min(4))
292 294
  end
293 295

  
294 296
  def test_max
......
303 305
    assert_equal(%w[horse dog], ary.max(2))
304 306
    assert_equal(%w[albatross horse],
305 307
                 ary.max(2) {|a,b| a.length <=> b.length })
308
    assert_equal([3, 2], [0, 0, 0, 0, 0, 0, 1, 3, 2].max(2))
306 309
  end
307 310

  
308 311
  def test_minmax
......
325 328
    assert_equal("dog", a.min_by {|x| x.length })
326 329
    assert_equal(3, [2,3,1].min_by {|x| -x })
327 330
    assert_equal(%w[dog horse], a.min_by(2) {|x| x.length })
331
    assert_equal([13, 14], [20, 32, 32, 21, 30, 25, 29, 13, 14].min_by(2) {|x| x})
328 332
  end
329 333

  
330 334
  def test_max_by
......
335 339
    assert_equal("albatross", a.max_by {|x| x.length })
336 340
    assert_equal(1, [2,3,1].max_by {|x| -x })
337 341
    assert_equal(%w[albatross horse], a.max_by(2) {|x| x.length })
342
    assert_equal([3, 2], [0, 0, 0, 0, 0, 0, 1, 3, 2].max_by(2) {|x| x})
338 343
  end
339 344

  
340 345
  def test_minmax_by