1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

Split tests for Array#shuffle and Array#sample

This commit is contained in:
Nobuyoshi Nakada 2022-10-23 18:33:03 +09:00
parent bcb72f503c
commit 21fa0135a4
No known key found for this signature in database
GPG key ID: 7CD2805BFA3770C6

View file

@ -2980,7 +2980,9 @@ class TestArray < Test::Unit::TestCase
assert_raise(RangeError) { assert_raise(RangeError) {
[*0..2].shuffle(random: gen) [*0..2].shuffle(random: gen)
} }
end
def test_shuffle_random_clobbering
ary = (0...10000).to_a ary = (0...10000).to_a
gen = proc do gen = proc do
ary.replace([]) ary.replace([])
@ -2990,7 +2992,9 @@ class TestArray < Test::Unit::TestCase
alias rand call alias rand call
end end
assert_raise(RuntimeError) {ary.shuffle!(random: gen)} assert_raise(RuntimeError) {ary.shuffle!(random: gen)}
end
def test_shuffle_random_zero
zero = Object.new zero = Object.new
def zero.to_int def zero.to_int
0 0
@ -3003,7 +3007,10 @@ class TestArray < Test::Unit::TestCase
end end
ary = (0...10000).to_a ary = (0...10000).to_a
assert_equal(ary.rotate, ary.shuffle(random: gen_to_int)) 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) { assert_raise(NoMethodError) {
ary.shuffle(random: Object.new) ary.shuffle(random: Object.new)
} }
@ -3020,7 +3027,9 @@ class TestArray < Test::Unit::TestCase
assert_include([0, 1, 2], sample) assert_include([0, 1, 2], sample)
} }
end end
end
def test_sample_statistics
srand(0) srand(0)
a = (1..18).to_a a = (1..18).to_a
(0..20).each do |n| (0..20).each do |n|
@ -3037,9 +3046,13 @@ class TestArray < Test::Unit::TestCase
end end
assert_operator(h.values.min * 2, :>=, h.values.max) if n != 0 assert_operator(h.values.min * 2, :>=, h.values.max) if n != 0
end end
end
def test_sample_invalid_argument
assert_raise(ArgumentError, '[ruby-core:23374]') {[1, 2].sample(-1)} assert_raise(ArgumentError, '[ruby-core:23374]') {[1, 2].sample(-1)}
end
def test_sample_random_srand0
gen = Random.new(0) gen = Random.new(0)
srand(0) srand(0)
a = (1..18).to_a 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}") assert_equal(a.sample(n), a.sample(n, random: gen), "#{i}/#{n}")
end end
end end
end
def test_sample_unknown_keyword
assert_raise_with_message(ArgumentError, /unknown keyword/) do assert_raise_with_message(ArgumentError, /unknown keyword/) do
[0, 1, 2].sample(xawqij: "a") [0, 1, 2].sample(xawqij: "a")
end end
end end
def test_sample_random def test_sample_random_generator
ary = (0...10000).to_a ary = (0...10000).to_a
assert_raise(ArgumentError) {ary.sample(1, 2, random: nil)} assert_raise(ArgumentError) {ary.sample(1, 2, random: nil)}
gen0 = proc do |max| 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)) 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 ary.sample(11, random: gen1) # implementation detail, may change in the future
assert_equal([], ary) assert_equal([], ary)
end
def test_sample_random_generator_half
half = Object.new half = Object.new
def half.to_int def half.to_int
5000 5000
@ -3110,7 +3127,10 @@ class TestArray < Test::Unit::TestCase
end end
ary = (0...10000).to_a ary = (0...10000).to_a
assert_equal(5000, ary.sample(random: gen_to_int)) assert_equal(5000, ary.sample(random: gen_to_int))
end
def test_sample_random_invalid_generator
ary = (0..10).to_a
assert_raise(NoMethodError) { assert_raise(NoMethodError) {
ary.sample(random: Object.new) ary.sample(random: Object.new)
} }