mirror of
https://github.com/mruby/mruby
synced 2026-06-08 16:11:16 +00:00
mruby-random/random.c: use random: keyword argument.
For Array#sample and Array#shuffle. They used to take optional ordinal argument for Random object but CRuby uses `random:` keyword argument. Now mruby is compatible with CRuby here.
This commit is contained in:
@@ -238,7 +238,10 @@ check_random_arg(mrb_state *mrb, mrb_value r)
|
||||
struct RClass *c = mrb_class_get_id(mrb, ID_RANDOM_STRICT);
|
||||
rand_state *random;
|
||||
|
||||
if (mrb_istruct_p(r) && mrb_obj_is_kind_of(mrb, r, c)){
|
||||
if (mrb_undef_p(r)) {
|
||||
random = random_default_state(mrb);
|
||||
}
|
||||
else if (mrb_istruct_p(r) && mrb_obj_is_kind_of(mrb, r, c)){
|
||||
random = (rand_state*)mrb_istruct_ptr(r);
|
||||
}
|
||||
else {
|
||||
@@ -258,15 +261,13 @@ mrb_ary_shuffle_bang(mrb_state *mrb, mrb_value ary)
|
||||
{
|
||||
if (RARRAY_LEN(ary) > 1) {
|
||||
mrb_int i, max;
|
||||
mrb_value r;
|
||||
rand_state *random;
|
||||
mrb_sym knames[3] = {MRB_SYM(random)};
|
||||
mrb_value r;
|
||||
const mrb_kwargs kw = {1, 0, knames, &r, NULL};
|
||||
|
||||
if (mrb_get_args(mrb, "|o", &r) == 0) {
|
||||
random = random_default_state(mrb);
|
||||
}
|
||||
else {
|
||||
random = check_random_arg(mrb, r);
|
||||
}
|
||||
mrb_get_args(mrb, ":", &kw);
|
||||
random = check_random_arg(mrb, r);
|
||||
mrb_ary_modify(mrb, mrb_ary_ptr(ary));
|
||||
max = RARRAY_LEN(ary);
|
||||
for (i = RARRAY_LEN(ary) - 1; i > 0; i--) {
|
||||
@@ -321,16 +322,14 @@ mrb_ary_sample(mrb_state *mrb, mrb_value ary)
|
||||
{
|
||||
mrb_int n = 0;
|
||||
mrb_bool given;
|
||||
mrb_value r;
|
||||
rand_state *random;
|
||||
mrb_int len;
|
||||
mrb_sym knames[3] = {MRB_SYM(random)};
|
||||
mrb_value r;
|
||||
const mrb_kwargs kw = {1, 0, knames, &r, NULL};
|
||||
|
||||
if (mrb_get_args(mrb, "|i?o", &n, &given, &r) < 2) {
|
||||
random = random_default_state(mrb);
|
||||
}
|
||||
else {
|
||||
random = check_random_arg(mrb, r);
|
||||
}
|
||||
mrb_get_args(mrb, "|i?:", &n, &given, &kw);
|
||||
random = check_random_arg(mrb, r);
|
||||
len = RARRAY_LEN(ary);
|
||||
if (!given) { /* pick one element */
|
||||
switch (len) {
|
||||
|
||||
@@ -73,14 +73,14 @@ end
|
||||
assert("Array#shuffle(random)") do
|
||||
assert_raise(TypeError) do
|
||||
# this will cause an exception due to the wrong argument
|
||||
[1, 2].shuffle "Not a Random instance"
|
||||
[1, 2].shuffle(random: "Not a Random instance")
|
||||
end
|
||||
|
||||
# verify that the same seed causes the same results
|
||||
ary = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
|
||||
shuffled1 = ary.shuffle Random.new 345
|
||||
shuffled2 = ary.shuffle Random.new 345
|
||||
shuffled3 = ary.shuffle Random.new 346
|
||||
shuffled1 = ary.shuffle(random: Random.new(345))
|
||||
shuffled2 = ary.shuffle(random: Random.new(345))
|
||||
shuffled3 = ary.shuffle(random: Random.new(346))
|
||||
assert_equal(shuffled1, shuffled2)
|
||||
assert_not_equal(shuffled1, shuffled3)
|
||||
end
|
||||
@@ -88,16 +88,16 @@ end
|
||||
assert('Array#shuffle!(random)') do
|
||||
assert_raise(TypeError) do
|
||||
# this will cause an exception due to the wrong argument
|
||||
[1, 2].shuffle! "Not a Random instance"
|
||||
[1, 2].shuffle!(random: "Not a Random instance")
|
||||
end
|
||||
|
||||
# verify that the same seed causes the same results
|
||||
ary1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
|
||||
ary1.shuffle! Random.new 345
|
||||
ary1.shuffle!(random: Random.new(345))
|
||||
ary2 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
|
||||
ary2.shuffle! Random.new 345
|
||||
ary2.shuffle!(random: Random.new(345))
|
||||
ary3 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
|
||||
ary3.shuffle! Random.new 346
|
||||
ary3.shuffle!(random: Random.new(346))
|
||||
assert_equal(ary1, ary2)
|
||||
assert_not_equal(ary1, ary3)
|
||||
end
|
||||
@@ -123,15 +123,15 @@ end
|
||||
assert('Array#sample(random)') do
|
||||
assert_raise(TypeError) do
|
||||
# this will cause an exception due to the wrong argument
|
||||
[1, 2].sample(2, "Not a Random instance")
|
||||
[1, 2].sample(2, random: "Not a Random instance")
|
||||
end
|
||||
|
||||
# verify that the same seed causes the same results
|
||||
ary = (1..10).to_a
|
||||
srand(15)
|
||||
samples1 = ary.sample(4)
|
||||
samples2 = ary.sample(4, Random.new(15))
|
||||
samples3 = ary.sample(4, Random.new(16))
|
||||
samples2 = ary.sample(4, random: Random.new(15))
|
||||
samples3 = ary.sample(4, random: Random.new(16))
|
||||
assert_equal(samples1, samples2)
|
||||
assert_not_equal(samples1, samples3)
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user