ruby-changes:74237
From: Nobuyoshi <ko1@a...>
Date: Mon, 24 Oct 2022 23:46:07 +0900 (JST)
Subject: [ruby-changes:74237] 21fa0135a4 (master): Split tests for `Array#shuffle` and `Array#sample`
https://git.ruby-lang.org/ruby.git/commit/?id=21fa0135a4 From 21fa0135a4f66e101d6225a9de1e02fade22a38c Mon Sep 17 00:00:00 2001 From: Nobuyoshi Nakada <nobu@r...> Date: Sun, 23 Oct 2022 18:33:03 +0900 Subject: Split tests for `Array#shuffle` and `Array#sample` --- test/ruby/test_array.rb | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/test/ruby/test_array.rb b/test/ruby/test_array.rb index 6ee468eaef..20e6ee7917 100644 --- a/test/ruby/test_array.rb +++ b/test/ruby/test_array.rb @@ -2980,7 +2980,9 @@ class TestArray < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_array.rb#L2980 assert_raise(RangeError) { [*0..2].shuffle(random: gen) } + end + def test_shuffle_random_clobbering ary = (0...10000).to_a gen = proc do ary.replace([]) @@ -2990,7 +2992,9 @@ class TestArray < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_array.rb#L2992 alias rand call end assert_raise(RuntimeError) {ary.shuffle!(random: gen)} + end + def test_shuffle_random_zero zero = Object.new def zero.to_int 0 @@ -3003,7 +3007,10 @@ class TestArray < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_array.rb#L3007 end ary = (0...10000).to_a assert_equal(ary.rotate, ary.shuffle(random: gen_to_int)) + end + def test_shuffle_random_invalid_generator + ary = (0...10).to_a assert_raise(NoMethodError) { ary.shuffle(random: Object.new) } @@ -3020,7 +3027,9 @@ class TestArray < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_array.rb#L3027 assert_include([0, 1, 2], sample) } end + end + def test_sample_statistics srand(0) a = (1..18).to_a (0..20).each do |n| @@ -3037,9 +3046,13 @@ class TestArray < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_array.rb#L3046 end assert_operator(h.values.min * 2, :>=, h.values.max) if n != 0 end + end + def test_sample_invalid_argument assert_raise(ArgumentError, '[ruby-core:23374]') {[1, 2].sample(-1)} + end + def test_sample_random_srand0 gen = Random.new(0) srand(0) a = (1..18).to_a @@ -3048,13 +3061,15 @@ class TestArray < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_array.rb#L3061 assert_equal(a.sample(n), a.sample(n, random: gen), "#{i}/#{n}") end end + end + def test_sample_unknown_keyword assert_raise_with_message(ArgumentError, /unknown keyword/) do [0, 1, 2].sample(xawqij: "a") end end - def test_sample_random + def test_sample_random_generator ary = (0...10000).to_a assert_raise(ArgumentError) {ary.sample(1, 2, random: nil)} gen0 = proc do |max| @@ -3097,7 +3112,9 @@ class TestArray < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_array.rb#L3112 assert_equal([5000, 0, 5001, 2, 5002, 4, 5003, 6, 5004, 8, 5005], ary.sample(11, random: gen0)) ary.sample(11, random: gen1) # implementation detail, may change in the future assert_equal([], ary) + end + def test_sample_random_generator_half half = Object.new def half.to_int 5000 @@ -3110,7 +3127,10 @@ class TestArray < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_array.rb#L3127 end ary = (0...10000).to_a assert_equal(5000, ary.sample(random: gen_to_int)) + end + def test_sample_random_invalid_generator + ary = (0..10).to_a assert_raise(NoMethodError) { ary.sample(random: Object.new) } -- cgit v1.2.3 -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/