summaryrefslogtreecommitdiff
path: root/test/ruby/test_array.rb
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 (patch)
tree7feb648c8657f4f67d355b51ce9c3087a538b2ec /test/ruby/test_array.rb
parent571f3394f2b8ac312e0e6213c639a44cf7e29fe4 (diff)
Add out of range tests of random number generator
Notes
Notes: Merged: https://github.com/ruby/ruby/pull/12690
Diffstat (limited to 'test/ruby/test_array.rb')
-rw-r--r--test/ruby/test_array.rb72
1 files changed, 31 insertions, 41 deletions
diff --git a/test/ruby/test_array.rb b/test/ruby/test_array.rb
index 797ae95e97..e2f07ba115 100644
--- a/test/ruby/test_array.rb
+++ b/test/ruby/test_array.rb
@@ -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