Fix Array#bsearch when block returns a non-integer numeric value

This commit is contained in:
Kouhei Yanagita 2023-08-29 14:49:57 +09:00 committed by Nobuyoshi Nakada
parent f16c50772c
commit a28c5151f5
Notes: git 2023-08-29 09:01:06 +00:00
2 changed files with 6 additions and 2 deletions

View File

@ -3522,8 +3522,8 @@ rb_ary_bsearch_index(VALUE ary)
const VALUE zero = INT2FIX(0);
switch (rb_cmpint(rb_funcallv(v, id_cmp, 1, &zero), v, zero)) {
case 0: return INT2FIX(mid);
case 1: smaller = 1; break;
case -1: smaller = 0;
case 1: smaller = 0; break;
case -1: smaller = 1;
}
}
else {

View File

@ -3336,6 +3336,8 @@ class TestArray < Test::Unit::TestCase
assert_equal(nil, a.bsearch {|x| 1 * (2**100) })
assert_equal(nil, a.bsearch {|x| (-1) * (2**100) })
assert_equal(4, a.bsearch {|x| (4 - x).to_r })
assert_include([4, 7], a.bsearch {|x| (2**100).coerce((1 - x / 4) * (2**100)).first })
end
@ -3371,6 +3373,8 @@ class TestArray < Test::Unit::TestCase
assert_equal(nil, a.bsearch_index {|x| 1 * (2**100) })
assert_equal(nil, a.bsearch_index {|x| (-1) * (2**100) })
assert_equal(1, a.bsearch_index {|x| (4 - x).to_r })
assert_include([1, 2], a.bsearch_index {|x| (2**100).coerce((1 - x / 4) * (2**100)).first })
end