summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNobuyoshi Nakada <[email protected]>2025-02-03 00:27:43 +0900
committerNobuyoshi Nakada <[email protected]>2025-02-03 00:27:43 +0900
commit581d85058cf638f2f8ad87391dccc5c7708d597b ()
tree7feb648c8657f4f67d355b51ce9c3087a538b2ec
parent571f3394f2b8ac312e0e6213c639a44cf7e29fe4 (diff)
Add out of range tests of random number generator
Notes: Merged: https://.com/ruby/ruby/pull/12690
-rw-r--r--test/ruby/test_array.rb72
1 files changed, 31 insertions, 41 deletions
@@ -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
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
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
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|
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
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) {
@@ -3621,6 +3604,13 @@ class TestArray < Test::Unit::TestCase
end
omit 'requires callcc support' unless respond_to?(:callcc, true)
end
end
class TestArraySubclass < TestArray