[前][次][番号順一覧][スレッド一覧]

ruby-changes:27324

From: nobu <ko1@a...>
Date: Fri, 22 Feb 2013 12:46:58 +0900 (JST)
Subject: [ruby-changes:27324] nobu:r39376 (trunk): random.c: fix error message

nobu	2013-02-22 12:46:47 +0900 (Fri, 22 Feb 2013)

  New Revision: 39376

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=39376

  Log:
    random.c: fix error message
    
    * random.c (rb_random_ulong_limited): fix error message for negative
      value.  [ruby-dev:47061] [Bug #7903]

  Modified files:
    trunk/ChangeLog
    trunk/internal.h
    trunk/numeric.c
    trunk/random.c
    trunk/test/ruby/test_rand.rb

Index: ChangeLog
===================================================================
--- ChangeLog	(revision 39375)
+++ ChangeLog	(revision 39376)
@@ -1,3 +1,8 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Fri Feb 22 12:46:41 2013  Nobuyoshi Nakada  <nobu@r...>
+
+	* 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@r...>
 
 	* test/test_rbconfig.rb (TestRbConfig): skip user defined values by
Index: numeric.c
===================================================================
--- numeric.c	(revision 39375)
+++ numeric.c	(revision 39376)
@@ -185,6 +185,12 @@ negative_int_p(VALUE num) https://github.com/ruby/ruby/blob/trunk/numeric.c#L185
     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
Index: internal.h
===================================================================
--- internal.h	(revision 39375)
+++ internal.h	(revision 39376)
@@ -168,6 +168,7 @@ int rb_num_to_uint(VALUE val, unsigned i https://github.com/ruby/ruby/blob/trunk/internal.h#L168
 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);
Index: test/ruby/test_rand.rb
===================================================================
--- test/ruby/test_rand.rb	(revision 39375)
+++ test/ruby/test_rand.rb	(revision 39376)
@@ -1,4 +1,5 @@ https://github.com/ruby/ruby/blob/trunk/test/ruby/test_rand.rb#L1
 require 'test/unit'
+require_relative 'envutil'
 
 class TestRand < Test::Unit::TestCase
   def assert_random_int(ws, m, init = 0)
@@ -514,4 +515,18 @@ END https://github.com/ruby/ruby/blob/trunk/test/ruby/test_rand.rb#L515
       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
Index: random.c
===================================================================
--- random.c	(revision 39375)
+++ random.c	(revision 39376)
@@ -947,9 +947,13 @@ rb_random_ulong_limited(VALUE obj, unsig https://github.com/ruby/ruby/blob/trunk/random.c#L947
 {
     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);
 	}

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]