@@ -3032,41 +3032,29 @@ def test_shuffle
30323032 end
30333033 end
30343034
3035- def test_shuffle_random
3036- gen = proc do
3037- 10000000
3038- end
3039- class << gen
3040- alias rand call
3041- end
3035+ def test_shuffle_random_out_of_range
3036+ gen = random_generator { 10000000 }
3037+ assert_raise ( RangeError ) {
3038+ [ *0 ..2 ] . shuffle ( random : gen )
3039+ }
3040+ gen = random_generator { -1 }
30423041 assert_raise ( RangeError ) {
30433042 [ *0 ..2 ] . shuffle ( random : gen )
30443043 }
30453044 end
30463045
30473046 def test_shuffle_random_clobbering
30483047 ary = ( 0 ...10000 ) . to_a
3049- gen = proc do
3048+ gen = random_generator do
30503049 ary . replace ( [ ] )
30513050 0.5
30523051 end
3053- class << gen
3054- alias rand call
3055- end
30563052 assert_raise ( RuntimeError ) { ary . shuffle! ( random : gen ) }
30573053 end
30583054
30593055 def test_shuffle_random_zero
3060- zero = Object . new
3061- def zero . to_int
3062- 0
3063- end
3064- gen_to_int = proc do |max |
3065- zero
3066- end
3067- class << gen_to_int
3068- alias rand call
3069- end
3056+ zero = Struct . new ( :to_int ) . new ( 0 )
3057+ gen_to_int = random_generator { |max | zero }
30703058 ary = ( 0 ...10000 ) . to_a
30713059 assert_equal ( ary . rotate , ary . shuffle ( random : gen_to_int ) )
30723060 end
@@ -3134,19 +3122,11 @@ def test_sample_unknown_keyword
31343122 def test_sample_random_generator
31353123 ary = ( 0 ...10000 ) . to_a
31363124 assert_raise ( ArgumentError ) { ary . sample ( 1 , 2 , random : nil ) }
3137- gen0 = proc do |max |
3138- max /2
3139- end
3140- class << gen0
3141- alias rand call
3142- end
3143- gen1 = proc do |max |
3125+ gen0 = random_generator { |max | max /2 }
3126+ gen1 = random_generator do |max |
31443127 ary . replace ( [ ] )
31453128 max /2
31463129 end
3147- class << gen1
3148- alias rand call
3149- end
31503130 assert_equal ( 5000 , ary . sample ( random : gen0 ) )
31513131 assert_nil ( ary . sample ( random : gen1 ) )
31523132 assert_equal ( [ ] , ary )
@@ -3177,20 +3157,23 @@ class << gen1
31773157 end
31783158
31793159 def test_sample_random_generator_half
3180- half = Object . new
3181- def half . to_int
3182- 5000
3183- end
3184- gen_to_int = proc do |max |
3185- half
3186- end
3187- class << gen_to_int
3188- alias rand call
3189- end
3160+ half = Struct . new ( :to_int ) . new ( 5000 )
3161+ gen_to_int = random_generator { |max | half }
31903162 ary = ( 0 ...10000 ) . to_a
31913163 assert_equal ( 5000 , ary . sample ( random : gen_to_int ) )
31923164 end
31933165
3166+ def test_sample_random_out_of_range
3167+ gen = random_generator { 10000000 }
3168+ assert_raise ( RangeError ) {
3169+ [ *0 ..2 ] . sample ( random : gen )
3170+ }
3171+ gen = random_generator { -1 }
3172+ assert_raise ( RangeError ) {
3173+ [ *0 ..2 ] . sample ( random : gen )
3174+ }
3175+ end
3176+
31943177 def test_sample_random_invalid_generator
31953178 ary = ( 0 ..10 ) . to_a
31963179 assert_raise ( NoMethodError ) {
@@ -3621,6 +3604,13 @@ def need_continuation
36213604 end
36223605 omit 'requires callcc support' unless respond_to? ( :callcc , true )
36233606 end
3607+
3608+ def random_generator ( &block )
3609+ class << block
3610+ alias rand call
3611+ end
3612+ block
3613+ end
36243614end
36253615
36263616class TestArraySubclass < TestArray
0 commit comments