Add out of range tests of random number generator
This commit is contained in:
parent
571f3394f2
commit
581d85058c
Notes:
git
2025-02-02 16:19:08 +00:00
@ -3032,13 +3032,12 @@ class TestArray < Test::Unit::TestCase
|
||||
end
|
||||
end
|
||||
|
||||
def test_shuffle_random
|
||||
gen = proc do
|
||||
10000000
|
||||
end
|
||||
class << gen
|
||||
alias rand call
|
||||
end
|
||||
def test_shuffle_random_out_of_range
|
||||
gen = random_generator {10000000}
|
||||
assert_raise(RangeError) {
|
||||
[*0..2].shuffle(random: gen)
|
||||
}
|
||||
gen = random_generator {-1}
|
||||
assert_raise(RangeError) {
|
||||
[*0..2].shuffle(random: gen)
|
||||
}
|
||||
@ -3046,27 +3045,16 @@ class TestArray < Test::Unit::TestCase
|
||||
|
||||
def test_shuffle_random_clobbering
|
||||
ary = (0...10000).to_a
|
||||
gen = proc do
|
||||
gen = random_generator do
|
||||
ary.replace([])
|
||||
0.5
|
||||
end
|
||||
class << gen
|
||||
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
|
||||
end
|
||||
gen_to_int = proc do |max|
|
||||
zero
|
||||
end
|
||||
class << gen_to_int
|
||||
alias rand call
|
||||
end
|
||||
zero = Struct.new(:to_int).new(0)
|
||||
gen_to_int = random_generator {|max| zero}
|
||||
ary = (0...10000).to_a
|
||||
assert_equal(ary.rotate, ary.shuffle(random: gen_to_int))
|
||||
end
|
||||
@ -3134,19 +3122,11 @@ class TestArray < Test::Unit::TestCase
|
||||
def test_sample_random_generator
|
||||
ary = (0...10000).to_a
|
||||
assert_raise(ArgumentError) {ary.sample(1, 2, random: nil)}
|
||||
gen0 = proc do |max|
|
||||
max/2
|
||||
end
|
||||
class << gen0
|
||||
alias rand call
|
||||
end
|
||||
gen1 = proc do |max|
|
||||
gen0 = random_generator {|max| max/2}
|
||||
gen1 = random_generator do |max|
|
||||
ary.replace([])
|
||||
max/2
|
||||
end
|
||||
class << gen1
|
||||
alias rand call
|
||||
end
|
||||
assert_equal(5000, ary.sample(random: gen0))
|
||||
assert_nil(ary.sample(random: gen1))
|
||||
assert_equal([], ary)
|
||||
@ -3177,20 +3157,23 @@ class TestArray < Test::Unit::TestCase
|
||||
end
|
||||
|
||||
def test_sample_random_generator_half
|
||||
half = Object.new
|
||||
def half.to_int
|
||||
5000
|
||||
end
|
||||
gen_to_int = proc do |max|
|
||||
half
|
||||
end
|
||||
class << gen_to_int
|
||||
alias rand call
|
||||
end
|
||||
half = Struct.new(:to_int).new(5000)
|
||||
gen_to_int = random_generator {|max| half}
|
||||
ary = (0...10000).to_a
|
||||
assert_equal(5000, ary.sample(random: gen_to_int))
|
||||
end
|
||||
|
||||
def test_sample_random_out_of_range
|
||||
gen = random_generator {10000000}
|
||||
assert_raise(RangeError) {
|
||||
[*0..2].sample(random: gen)
|
||||
}
|
||||
gen = random_generator {-1}
|
||||
assert_raise(RangeError) {
|
||||
[*0..2].sample(random: gen)
|
||||
}
|
||||
end
|
||||
|
||||
def test_sample_random_invalid_generator
|
||||
ary = (0..10).to_a
|
||||
assert_raise(NoMethodError) {
|
||||
@ -3621,6 +3604,13 @@ class TestArray < Test::Unit::TestCase
|
||||
end
|
||||
omit 'requires callcc support' unless respond_to?(:callcc, true)
|
||||
end
|
||||
|
||||
def random_generator(&block)
|
||||
class << block
|
||||
alias rand call
|
||||
end
|
||||
block
|
||||
end
|
||||
end
|
||||
|
||||
class TestArraySubclass < TestArray
|
||||
|
Loading…
x
Reference in New Issue
Block a user