* random.c (rb_f_rand): type check simplified. strings are no

longer allowed for argument.  [ruby-dev:37655]

* test/ruby/test_rand.rb (TestRand::o.to_int): need override
  to_int.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@21178 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
matz 2008-12-29 13:29:12 +00:00
Родитель db86fef72f
Коммит 6bbd76a732
3 изменённых файлов: 26 добавлений и 36 удалений

Просмотреть файл

@ -1,3 +1,11 @@
Mon Dec 29 22:27:11 2008 Yukihiro Matsumoto <matz@ruby-lang.org>
* random.c (rb_f_rand): type check simplified. strings are no
longer allowed for argument. [ruby-dev:37655]
* test/ruby/test_rand.rb (TestRand::o.to_int): need override
to_int.
Mon Dec 29 21:22:31 2008 Tadayoshi Funaba <tadf@dotrb.org>
* numeric.c: Infinity.numerator returns self. [experimental]

Просмотреть файл

@ -489,49 +489,31 @@ rb_f_rand(int argc, VALUE *argv, VALUE obj)
long val, max;
struct MT *mt = &default_mt.mt;
rb_scan_args(argc, argv, "01", &vmax);
if (!genrand_initialized(mt)) {
rand_init(mt, random_seed());
}
switch (TYPE(vmax)) {
case T_FLOAT:
if (RFLOAT_VALUE(vmax) <= LONG_MAX && RFLOAT_VALUE(vmax) >= LONG_MIN) {
max = (long)RFLOAT_VALUE(vmax);
break;
if (argc == 0) goto zero_arg;
rb_scan_args(argc, argv, "01", &vmax);
if (NIL_P(vmax)) goto zero_arg;
vmax = rb_to_int(vmax);
if (TYPE(vmax) == T_BIGNUM) {
struct RBignum *limit = (struct RBignum *)vmax;
if (!RBIGNUM_SIGN(limit)) {
limit = (struct RBignum *)rb_big_clone(vmax);
RBIGNUM_SET_SIGN(limit, 1);
}
if (RFLOAT_VALUE(vmax) < 0)
vmax = rb_dbl2big(-RFLOAT_VALUE(vmax));
else
vmax = rb_dbl2big(RFLOAT_VALUE(vmax));
/* fall through */
case T_BIGNUM:
bignum:
{
struct RBignum *limit = (struct RBignum *)vmax;
if (!RBIGNUM_SIGN(limit)) {
limit = (struct RBignum *)rb_big_clone(vmax);
RBIGNUM_SET_SIGN(limit, 1);
}
limit = (struct RBignum *)rb_big_minus((VALUE)limit, INT2FIX(1));
if (FIXNUM_P((VALUE)limit)) {
if (FIX2LONG((VALUE)limit) == -1)
return DBL2NUM(genrand_real(mt));
return LONG2NUM(limited_rand(mt, FIX2LONG((VALUE)limit)));
}
return limited_big_rand(mt, limit);
limit = (struct RBignum *)rb_big_minus((VALUE)limit, INT2FIX(1));
if (FIXNUM_P((VALUE)limit)) {
if (FIX2LONG((VALUE)limit) == -1)
return DBL2NUM(genrand_real(mt));
return LONG2NUM(limited_rand(mt, FIX2LONG((VALUE)limit)));
}
case T_NIL:
max = 0;
break;
default:
vmax = rb_Integer(vmax);
if (TYPE(vmax) == T_BIGNUM) goto bignum;
case T_FIXNUM:
max = FIX2LONG(vmax);
break;
return limited_big_rand(mt, limit);
}
max = NUM2LONG(vmax);
if (max == 0) {
zero_arg:
return DBL2NUM(genrand_real(mt));
}
if (max < 0) max = -max;

Просмотреть файл

@ -144,7 +144,7 @@ class TestRand < Test::Unit::TestCase
0.000000000000001)
srand(0)
o = Object.new
def o.to_i; 100; end
def o.to_int; 100; end
assert_equal(44, rand(o))
assert_equal(47, rand(o))
assert_equal(64, rand(o))