diff options
author | Nobuyoshi Nakada <nobu@ruby-lang.org> | 2022-10-23 18:33:03 +0900 |
---|---|---|
committer | Nobuyoshi Nakada <nobu@ruby-lang.org> | 2022-10-24 23:45:45 +0900 |
commit | 21fa0135a4f66e101d6225a9de1e02fade22a38c (patch) | |
tree | de5e5957d9dee52f1ff717a75b19bedbd319fefd /test | |
parent | bcb72f503c311e214545e77906efbbadd8313068 (diff) |
Split tests for `Array#shuffle` and `Array#sample`
Diffstat (limited to 'test')
-rw-r--r-- | test/ruby/test_array.rb | 22 |
1 files changed, 21 insertions, 1 deletions
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 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 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 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 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 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 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 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 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) } |