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:
parent
bcb72f503c
commit
21fa0135a4
1 changed files with 21 additions and 1 deletions
|
@ -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)
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue