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
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_shuffle_random
|
def test_shuffle_random_out_of_range
|
||||||
gen = proc do
|
gen = random_generator {10000000}
|
||||||
10000000
|
assert_raise(RangeError) {
|
||||||
end
|
[*0..2].shuffle(random: gen)
|
||||||
class << gen
|
}
|
||||||
alias rand call
|
gen = random_generator {-1}
|
||||||
end
|
|
||||||
assert_raise(RangeError) {
|
assert_raise(RangeError) {
|
||||||
[*0..2].shuffle(random: gen)
|
[*0..2].shuffle(random: gen)
|
||||||
}
|
}
|
||||||
@ -3046,27 +3045,16 @@ class TestArray < Test::Unit::TestCase
|
|||||||
|
|
||||||
def test_shuffle_random_clobbering
|
def test_shuffle_random_clobbering
|
||||||
ary = (0...10000).to_a
|
ary = (0...10000).to_a
|
||||||
gen = proc do
|
gen = random_generator do
|
||||||
ary.replace([])
|
ary.replace([])
|
||||||
0.5
|
0.5
|
||||||
end
|
end
|
||||||
class << gen
|
|
||||||
alias rand call
|
|
||||||
end
|
|
||||||
assert_raise(RuntimeError) {ary.shuffle!(random: gen)}
|
assert_raise(RuntimeError) {ary.shuffle!(random: gen)}
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_shuffle_random_zero
|
def test_shuffle_random_zero
|
||||||
zero = Object.new
|
zero = Struct.new(:to_int).new(0)
|
||||||
def zero.to_int
|
gen_to_int = random_generator {|max| zero}
|
||||||
0
|
|
||||||
end
|
|
||||||
gen_to_int = proc do |max|
|
|
||||||
zero
|
|
||||||
end
|
|
||||||
class << gen_to_int
|
|
||||||
alias rand call
|
|
||||||
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
|
end
|
||||||
@ -3134,19 +3122,11 @@ class TestArray < Test::Unit::TestCase
|
|||||||
def test_sample_random_generator
|
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 = random_generator {|max| max/2}
|
||||||
max/2
|
gen1 = random_generator do |max|
|
||||||
end
|
|
||||||
class << gen0
|
|
||||||
alias rand call
|
|
||||||
end
|
|
||||||
gen1 = proc do |max|
|
|
||||||
ary.replace([])
|
ary.replace([])
|
||||||
max/2
|
max/2
|
||||||
end
|
end
|
||||||
class << gen1
|
|
||||||
alias rand call
|
|
||||||
end
|
|
||||||
assert_equal(5000, ary.sample(random: gen0))
|
assert_equal(5000, ary.sample(random: gen0))
|
||||||
assert_nil(ary.sample(random: gen1))
|
assert_nil(ary.sample(random: gen1))
|
||||||
assert_equal([], ary)
|
assert_equal([], ary)
|
||||||
@ -3177,20 +3157,23 @@ class TestArray < Test::Unit::TestCase
|
|||||||
end
|
end
|
||||||
|
|
||||||
def test_sample_random_generator_half
|
def test_sample_random_generator_half
|
||||||
half = Object.new
|
half = Struct.new(:to_int).new(5000)
|
||||||
def half.to_int
|
gen_to_int = random_generator {|max| half}
|
||||||
5000
|
|
||||||
end
|
|
||||||
gen_to_int = proc do |max|
|
|
||||||
half
|
|
||||||
end
|
|
||||||
class << gen_to_int
|
|
||||||
alias rand call
|
|
||||||
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
|
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
|
def test_sample_random_invalid_generator
|
||||||
ary = (0..10).to_a
|
ary = (0..10).to_a
|
||||||
assert_raise(NoMethodError) {
|
assert_raise(NoMethodError) {
|
||||||
@ -3621,6 +3604,13 @@ class TestArray < Test::Unit::TestCase
|
|||||||
end
|
end
|
||||||
omit 'requires callcc support' unless respond_to?(:callcc, true)
|
omit 'requires callcc support' unless respond_to?(:callcc, true)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def random_generator(&block)
|
||||||
|
class << block
|
||||||
|
alias rand call
|
||||||
|
end
|
||||||
|
block
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
class TestArraySubclass < TestArray
|
class TestArraySubclass < TestArray
|
||||||
|
Loading…
x
Reference in New Issue
Block a user