* range.c: Restrict bsearch to integers [#7728]
* test/ruby/test_range.rb: Test for above git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@38985 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
39048aca75
commit
00d6bb5108
@ -1,3 +1,9 @@
|
|||||||
|
Wed Jan 30 14:46:28 2013 Marc-Andre Lafortune <ruby-core@marc-andre.ca>
|
||||||
|
|
||||||
|
* range.c: Restrict bsearch to integers [#7728]
|
||||||
|
|
||||||
|
* test/ruby/test_range.rb: Test for above
|
||||||
|
|
||||||
Wed Jan 30 14:10:52 2013 Marc-Andre Lafortune <ruby-core@marc-andre.ca>
|
Wed Jan 30 14:10:52 2013 Marc-Andre Lafortune <ruby-core@marc-andre.ca>
|
||||||
|
|
||||||
* array.c (rb_ary_bsearch): Return enumerator if no block [#7725]
|
* array.c (rb_ary_bsearch): Return enumerator if no block [#7725]
|
||||||
|
16
range.c
16
range.c
@ -20,7 +20,7 @@
|
|||||||
#include <math.h>
|
#include <math.h>
|
||||||
|
|
||||||
VALUE rb_cRange;
|
VALUE rb_cRange;
|
||||||
static ID id_cmp, id_succ, id_beg, id_end, id_excl;
|
static ID id_cmp, id_succ, id_beg, id_end, id_excl, id_integer_p;
|
||||||
|
|
||||||
#define RANGE_BEG(r) (RSTRUCT(r)->as.ary[0])
|
#define RANGE_BEG(r) (RSTRUCT(r)->as.ary[0])
|
||||||
#define RANGE_END(r) (RSTRUCT(r)->as.ary[1])
|
#define RANGE_END(r) (RSTRUCT(r)->as.ary[1])
|
||||||
@ -497,6 +497,12 @@ double_as_int64(double d) {
|
|||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
static int
|
||||||
|
is_integer_p(VALUE v) {
|
||||||
|
VALUE is_int = rb_check_funcall(v, id_integer_p, 0, 0);
|
||||||
|
return RTEST(is_int) && is_int != Qundef;
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* call-seq:
|
* call-seq:
|
||||||
* rng.bsearch {|obj| block } -> value
|
* rng.bsearch {|obj| block } -> value
|
||||||
@ -635,10 +641,9 @@ range_bsearch(VALUE range)
|
|||||||
BSEARCH(int64_as_double_to_num);
|
BSEARCH(int64_as_double_to_num);
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
else if (!NIL_P(rb_check_to_integer(beg, "to_int")) &&
|
else if (is_integer_p(beg) && is_integer_p(end)) {
|
||||||
!NIL_P(rb_check_to_integer(end, "to_int"))) {
|
VALUE low = rb_to_int(beg);
|
||||||
VALUE low = beg;
|
VALUE high = rb_to_int(end);
|
||||||
VALUE high = end;
|
|
||||||
VALUE mid, org_high;
|
VALUE mid, org_high;
|
||||||
RETURN_ENUMERATOR(range, 0, 0);
|
RETURN_ENUMERATOR(range, 0, 0);
|
||||||
if (EXCL(range)) high = rb_funcall(high, '-', 1, INT2FIX(1));
|
if (EXCL(range)) high = rb_funcall(high, '-', 1, INT2FIX(1));
|
||||||
@ -1299,6 +1304,7 @@ Init_Range(void)
|
|||||||
id_beg = rb_intern("begin");
|
id_beg = rb_intern("begin");
|
||||||
id_end = rb_intern("end");
|
id_end = rb_intern("end");
|
||||||
id_excl = rb_intern("excl");
|
id_excl = rb_intern("excl");
|
||||||
|
id_integer_p = rb_intern("integer?");
|
||||||
|
|
||||||
rb_cRange = rb_struct_define_without_accessor(
|
rb_cRange = rb_struct_define_without_accessor(
|
||||||
"Range", rb_cObject, range_alloc,
|
"Range", rb_cObject, range_alloc,
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
require 'test/unit'
|
require 'test/unit'
|
||||||
require 'delegate'
|
require 'delegate'
|
||||||
require 'timeout'
|
require 'timeout'
|
||||||
|
require 'bigdecimal'
|
||||||
|
|
||||||
class TestRange < Test::Unit::TestCase
|
class TestRange < Test::Unit::TestCase
|
||||||
def test_range_string
|
def test_range_string
|
||||||
@ -362,6 +363,15 @@ class TestRange < Test::Unit::TestCase
|
|||||||
assert_equal 200, enum.each{|x| x >= 200 }
|
assert_equal 200, enum.each{|x| x >= 200 }
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_bsearch_for_other_numerics
|
||||||
|
assert_raise(TypeError) {
|
||||||
|
(Rational(-1,2)..Rational(9,4)).bsearch
|
||||||
|
}
|
||||||
|
assert_raise(TypeError) {
|
||||||
|
(BigDecimal('0.5')..BigDecimal('2.25')).bsearch
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
def test_bsearch_for_fixnum
|
def test_bsearch_for_fixnum
|
||||||
ary = [3, 4, 7, 9, 12]
|
ary = [3, 4, 7, 9, 12]
|
||||||
assert_equal(0, (0...ary.size).bsearch {|i| ary[i] >= 2 })
|
assert_equal(0, (0...ary.size).bsearch {|i| ary[i] >= 2 })
|
||||||
|
Loading…
x
Reference in New Issue
Block a user