diff options
author | nobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2013-02-22 03:46:47 +0000 |
---|---|---|
committer | nobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2013-02-22 03:46:47 +0000 |
commit | 3f2ce6373f7902e02c559876fb7255b5e36aaa64 (patch) | |
tree | 685108cad30bda565d1bd67f803546686f077691 | |
parent | e51a9b49f1ded288ee00732f0d7d4af01764a793 (diff) |
random.c: fix error message
* random.c (rb_random_ulong_limited): fix error message for negative
value. [ruby-dev:47061] [Bug #7903]
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@39376 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-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 |
5 files changed, 31 insertions, 0 deletions
@@ -1,3 +1,8 @@ +Fri Feb 22 12:46:41 2013 Nobuyoshi Nakada <nobu@ruby-lang.org> + + * random.c (rb_random_ulong_limited): fix error message for negative + value. [ruby-dev:47061] [Bug #7903] + Fri Feb 22 11:36:45 2013 Nobuyoshi Nakada <nobu@ruby-lang.org> * test/test_rbconfig.rb (TestRbConfig): skip user defined values by 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 |