ruby-changes:27623
From: nagachika <ko1@a...>
Date: Sat, 9 Mar 2013 23:58:02 +0900 (JST)
Subject: [ruby-changes:27623] nagachika:r39675 (ruby_2_0_0): merge revision(s) 39466,39470: [Backport #7935]
nagachika 2013-03-09 23:57:48 +0900 (Sat, 09 Mar 2013) New Revision: 39675 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=39675 Log: 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] Modified directories: branches/ruby_2_0_0/ Modified files: branches/ruby_2_0_0/ChangeLog branches/ruby_2_0_0/random.c branches/ruby_2_0_0/test/ruby/test_array.rb branches/ruby_2_0_0/test/ruby/test_rand.rb branches/ruby_2_0_0/version.h Index: ruby_2_0_0/ChangeLog =================================================================== --- ruby_2_0_0/ChangeLog (revision 39674) +++ ruby_2_0_0/ChangeLog (revision 39675) @@ -1,3 +1,9 @@ https://github.com/ruby/ruby/blob/trunk/ruby_2_0_0/ChangeLog#L1 +Sat Mar 9 23:55:42 2013 Nobuyoshi Nakada <nobu@r...> + + * 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@r...> * random.c (rb_random_ulong_limited): fix error message for negative Index: ruby_2_0_0/version.h =================================================================== --- ruby_2_0_0/version.h (revision 39674) +++ ruby_2_0_0/version.h (revision 39675) @@ -1,6 +1,6 @@ https://github.com/ruby/ruby/blob/trunk/ruby_2_0_0/version.h#L1 #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 Index: ruby_2_0_0/test/ruby/test_array.rb =================================================================== --- ruby_2_0_0/test/ruby/test_array.rb (revision 39674) +++ ruby_2_0_0/test/ruby/test_array.rb (revision 39675) @@ -2055,14 +2055,14 @@ class TestArray < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/ruby_2_0_0/test/ruby/test_array.rb#L2055 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 Index: ruby_2_0_0/test/ruby/test_rand.rb =================================================================== --- ruby_2_0_0/test/ruby/test_rand.rb (revision 39674) +++ ruby_2_0_0/test/ruby/test_rand.rb (revision 39675) @@ -528,5 +528,13 @@ END https://github.com/ruby/ruby/blob/trunk/ruby_2_0_0/test/ruby/test_rand.rb#L528 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 Index: ruby_2_0_0/random.c =================================================================== --- ruby_2_0_0/random.c (revision 39674) +++ ruby_2_0_0/random.c (revision 39675) @@ -942,13 +942,26 @@ rb_random_real(VALUE obj) https://github.com/ruby/ruby/blob/trunk/ruby_2_0_0/random.c#L942 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)) { Property changes on: ruby_2_0_0 ___________________________________________________________________ Modified: svn:mergeinfo Merged /trunk:r39466,39470 -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/