diff options
author | nagachika <nagachika@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2013-03-09 14:57:48 +0000 |
---|---|---|
committer | nagachika <nagachika@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2013-03-09 14:57:48 +0000 |
commit | 0c2b3f7f4f1628b340d2e08614db608f0e54f96e (patch) | |
tree | 22ef91343761b4dbac662eaec94913eb0d8cb669 | |
parent | 8767705eb238d7c2391c5cce3c2945db99fc0c24 (diff) |
merge revision(s) 39466,39470: [Backport #7935]
* random.c (rb_random_ulong_limited): limit is inclusive, but generic
rand method should return a number less than it, so increase for the
difference. [ruby-core:52779] [Bug #7935]
* test/ruby/test_array.rb (test_sample_random): remove adjustment for
the bug fixed by r39466. [ruby-core:52779] [Bug #7935]
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_2_0_0@39675 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r-- | ChangeLog | 6 | ||||
-rw-r--r-- | random.c | 15 | ||||
-rw-r--r-- | test/ruby/test_array.rb | 4 | ||||
-rw-r--r-- | test/ruby/test_rand.rb | 8 | ||||
-rw-r--r-- | version.h | 2 |
5 files changed, 31 insertions, 4 deletions
@@ -1,3 +1,9 @@ +Sat Mar 9 23:55:42 2013 Nobuyoshi Nakada <nobu@ruby-lang.org> + + * random.c (rb_random_ulong_limited): limit is inclusive, but generic + rand method should return a number less than it, so increase for the + difference. [ruby-core:52779] [Bug #7935] + Sat Mar 9 23:51:58 2013 Nobuyoshi Nakada <nobu@ruby-lang.org> * random.c (rb_random_ulong_limited): fix error message for negative @@ -942,13 +942,26 @@ rb_random_real(VALUE obj) return genrand_real(&rnd->mt); } +static inline VALUE +ulong_to_num_plus_1(unsigned long n) +{ +#if HAVE_LONG_LONG + return ULL2NUM((LONG_LONG)n+1); +#else + if (n >= ULONG_MAX) { + return rb_big_plus(ULONG2NUM(n), INT2FIX(1)); + } + return ULONG2NUM(n+1); +#endif +} + unsigned long 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 lim = ulong_to_num_plus_1(limit); VALUE v = rb_funcall2(obj, id_rand, 1, &lim); unsigned long r = NUM2ULONG(v); if (rb_num_negative_p(v)) { diff --git a/test/ruby/test_array.rb b/test/ruby/test_array.rb index 3ab0895f8c..17f2ef66c4 100644 --- a/test/ruby/test_array.rb +++ b/test/ruby/test_array.rb @@ -2055,14 +2055,14 @@ class TestArray < Test::Unit::TestCase ary = (0...10000).to_a assert_raise(ArgumentError) {ary.sample(1, 2, random: nil)} gen0 = proc do |max| - (max+1)/2 + max/2 end class << gen0 alias rand call end gen1 = proc do |max| ary.replace([]) - (max+1)/2 + max/2 end class << gen1 alias rand call diff --git a/test/ruby/test_rand.rb b/test/ruby/test_rand.rb index 9fabfc607f..611e9f57a0 100644 --- a/test/ruby/test_rand.rb +++ b/test/ruby/test_rand.rb @@ -528,5 +528,13 @@ END 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) + + bug7935 = '[ruby-core:52779] [Bug #7935]' + class << (gen = Object.new) + def rand(limit) @limit = limit; 0 end + attr_reader :limit + end + [1, 2].sample(1, random: gen) + assert_equal(2, gen.limit, bug7935) end end @@ -1,6 +1,6 @@ #define RUBY_VERSION "2.0.0" #define RUBY_RELEASE_DATE "2013-03-09" -#define RUBY_PATCHLEVEL 44 +#define RUBY_PATCHLEVEL 45 #define RUBY_RELEASE_YEAR 2013 #define RUBY_RELEASE_MONTH 3 |