summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authornobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2010-08-03 12:16:06 +0000
committernobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2010-08-03 12:16:06 +0000
commita4c0f7dfaefeb6d3fe83a9ac130926b7699c3186 (patch)
tree9f032e18635a1bda54a181a034c398ecf956871b
parentb80d5f14df01ac626a42668f40ede3724e4425ef (diff)
* random.c (Init_Random): add Random::DEFAULT.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@28846 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r--ChangeLog4
-rw-r--r--random.c9
-rw-r--r--test/ruby/test_rand.rb12
3 files changed, 24 insertions, 1 deletions
diff --git a/ChangeLog b/ChangeLog
index e1588da..aa6cfb0 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,7 @@
+Tue Aug 3 21:16:03 2010 Nobuyoshi Nakada <nobu@ruby-lang.org>
+
+ * random.c (Init_Random): add Random::DEFAULT.
+
Tue Aug 3 21:01:10 2010 Nobuyoshi Nakada <nobu@ruby-lang.org>
* hash.c (rb_check_hash_type): added.
diff --git a/random.c b/random.c
index 49d0a75..ef0de1f 100644
--- a/random.c
+++ b/random.c
@@ -329,7 +329,12 @@ random_mark(void *ptr)
rb_gc_mark(((rb_random_t *)ptr)->seed);
}
-#define random_free RUBY_TYPED_DEFAULT_FREE
+static void
+random_free(void *ptr)
+{
+ if (ptr != &default_rand)
+ xfree(ptr);
+}
static size_t
random_memsize(const void *ptr)
@@ -1232,6 +1237,8 @@ Init_Random(void)
rb_define_private_method(rb_cRandom, "state", random_state, 0);
rb_define_private_method(rb_cRandom, "left", random_left, 0);
rb_define_method(rb_cRandom, "==", random_equal, 1);
+ rb_define_const(rb_cRandom, "DEFAULT",
+ TypedData_Wrap_Struct(rb_cRandom, &random_data_type, &default_rand));
rb_define_singleton_method(rb_cRandom, "srand", rb_f_srand, -1);
rb_define_singleton_method(rb_cRandom, "rand", rb_f_rand, -1);
diff --git a/test/ruby/test_rand.rb b/test/ruby/test_rand.rb
index e22e532..8ef360b 100644
--- a/test/ruby/test_rand.rb
+++ b/test/ruby/test_rand.rb
@@ -415,4 +415,16 @@ END
assert_equal(r1, r2)
}
end
+
+ def test_default
+ r1 = Random::DEFAULT.dup
+ r2 = Random::DEFAULT.dup
+ 3.times do
+ x0 = rand
+ x1 = r1.rand
+ x2 = r2.rand
+ assert_equal(x0, x1)
+ assert_equal(x0, x2)
+ end
+ end
end