1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

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
This commit is contained in:
nobu 2013-02-22 03:46:47 +00:00
parent e51a9b49f1
commit 3f2ce6373f
5 changed files with 31 additions and 0 deletions

View file

@ -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

View file

@ -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);

View file

@ -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

View file

@ -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);
}

View file

@ -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