diff options
author | nobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2010-08-26 22:57:39 +0000 |
---|---|---|
committer | nobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2010-08-26 22:57:39 +0000 |
commit | 5b7ccc0629baa7cd2c7ab92802ee1bf62e3ec0f4 (patch) | |
tree | 145ed61a619f6c58d4df760ea8c727527456f841 /test/ruby/test_array.rb | |
parent | 6e08cd5ec2e4133bbb02ccd196206c60f9cb6795 (diff) |
* array.c (rb_ary_shuffle_bang): bail out from modification during
shuffle.
* array.c (rb_ary_sample): ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@29108 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'test/ruby/test_array.rb')
-rw-r--r-- | test/ruby/test_array.rb | 56 |
1 files changed, 55 insertions, 1 deletions
diff --git a/test/ruby/test_array.rb b/test/ruby/test_array.rb index 4c3aba0589..44f71d3495 100644 --- a/test/ruby/test_array.rb +++ b/test/ruby/test_array.rb @@ -1901,7 +1901,6 @@ class TestArray < Test::Unit::TestCase end def test_shuffle_random - cc = nil gen = proc do 10000000 end @@ -1911,6 +1910,16 @@ class TestArray < Test::Unit::TestCase assert_raise(RangeError) { [*0..2].shuffle(random: gen) } + + 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_sample @@ -1951,6 +1960,51 @@ class TestArray < Test::Unit::TestCase end end + def test_sample_random + ary = (0...10000).to_a + assert_raise(ArgumentError) {ary.sample(1, 2, random: nil)} + gen0 = proc do + 0.5 + end + class << gen0 + alias rand call + end + gen1 = proc do + ary.replace([]) + 0.5 + end + class << gen1 + alias rand call + end + assert_equal(5000, ary.sample(random: gen0)) + assert_nil(ary.sample(random: gen1)) + assert_equal([], ary) + ary = (0...10000).to_a + assert_equal([5000], ary.sample(1, random: gen0)) + assert_equal([], ary.sample(1, random: gen1)) + assert_equal([], ary) + ary = (0...10000).to_a + assert_equal([5000, 4999], ary.sample(2, random: gen0)) + assert_equal([], ary.sample(2, random: gen1)) + assert_equal([], ary) + ary = (0...10000).to_a + assert_equal([5000, 4999, 5001], ary.sample(3, random: gen0)) + assert_equal([], ary.sample(3, random: gen1)) + assert_equal([], ary) + ary = (0...10000).to_a + assert_equal([5000, 4999, 5001, 4998], ary.sample(4, random: gen0)) + assert_equal([], ary.sample(4, random: gen1)) + assert_equal([], ary) + ary = (0...10000).to_a + assert_equal([5000, 4999, 5001, 4998, 5002, 4997, 5003, 4996, 5004, 4995], ary.sample(10, random: gen0)) + assert_equal([], ary.sample(10, random: gen1)) + assert_equal([], ary) + ary = (0...10000).to_a + assert_equal([5000, 0, 5001, 2, 5002, 4, 5003, 6, 5004, 8, 5005], ary.sample(11, random: gen0)) + ary.sample(11, random: gen1) # implementation detail, may change in the future + assert_equal([], ary) + end + def test_cycle a = [] [0, 1, 2].cycle do |i| |