diff options
-rw-r--r-- | ChangeLog | 5 | ||||
-rw-r--r-- | internal.h | 1 | ||||
-rw-r--r-- | numeric.c | 6 | ||||
-rw-r--r-- | random.c | 4 | ||||
-rw-r--r-- | test/ruby/test_rand.rb | 15 | ||||
-rw-r--r-- | version.h | 2 |
6 files changed, 32 insertions, 1 deletions
@@ -1,3 +1,8 @@ +Sat Mar 9 23:51:58 2013 Nobuyoshi Nakada <nobu@ruby-lang.org> + + * random.c (rb_random_ulong_limited): fix error message for negative + value. [ruby-dev:47061] [Bug #7903] + Sat Mar 9 23:41:11 2013 Zachary Scott <zachary@zacharyscott.net> * thread.c: Document ThreadGroup::Default diff --git a/internal.h b/internal.h index b5871fcd55..e66b5fd038 100644 --- a/internal.h +++ b/internal.h @@ -168,6 +168,7 @@ int rb_num_to_uint(VALUE val, unsigned int *ret); VALUE num_interval_step_size(VALUE from, VALUE to, VALUE step, int excl); int ruby_float_step(VALUE from, VALUE to, VALUE step, int excl); double ruby_float_mod(double x, double y); +int rb_num_negative_p(VALUE); /* object.c */ VALUE rb_obj_equal(VALUE obj1, VALUE obj2); @@ -185,6 +185,12 @@ negative_int_p(VALUE num) return RTEST(rb_funcall(num, mid, 1, INT2FIX(0))); } +int +rb_num_negative_p(VALUE num) +{ + return negative_int_p(num); +} + /* * call-seq: * num.coerce(numeric) -> array @@ -947,9 +947,13 @@ rb_random_ulong_limited(VALUE obj, unsigned long limit) { rb_random_t *rnd = try_get_rnd(obj); if (!rnd) { + extern int rb_num_negative_p(VALUE); VALUE lim = ULONG2NUM(limit); VALUE v = rb_funcall2(obj, id_rand, 1, &lim); unsigned long r = NUM2ULONG(v); + if (rb_num_negative_p(v)) { + rb_raise(rb_eRangeError, "random number too small %ld", r); + } if (r > limit) { rb_raise(rb_eRangeError, "random number too big %ld", r); } diff --git a/test/ruby/test_rand.rb b/test/ruby/test_rand.rb index b482773c00..9fabfc607f 100644 --- a/test/ruby/test_rand.rb +++ b/test/ruby/test_rand.rb @@ -1,4 +1,5 @@ require 'test/unit' +require_relative 'envutil' class TestRand < Test::Unit::TestCase def assert_random_int(ws, m, init = 0) @@ -514,4 +515,18 @@ END l.call end end + + def test_random_ulong_limited + def (gen = Object.new).rand(*) 1 end + assert_equal([2], (1..100).map {[1,2,3].sample(random: gen)}.uniq) + + def (gen = Object.new).rand(*) 100 end + e = assert_raise(RangeError) {[1,2,3].sample(random: gen)} + assert_match(/big 100\z/, e.message) + + bug7903 = '[ruby-dev:47061] [Bug #7903]' + def (gen = Object.new).rand(*) -1 end + e = assert_raise(RangeError) {[1,2,3].sample(random: gen)} + assert_match(/small -1\z/, e.message, bug7903) + end end @@ -1,6 +1,6 @@ #define RUBY_VERSION "2.0.0" #define RUBY_RELEASE_DATE "2013-03-09" -#define RUBY_PATCHLEVEL 43 +#define RUBY_PATCHLEVEL 44 #define RUBY_RELEASE_YEAR 2013 #define RUBY_RELEASE_MONTH 3 |