diff --git a/test/ruby/test_rand.rb b/test/ruby/test_rand.rb index 939d17bdf7..0808c1ef2a 100644 --- a/test/ruby/test_rand.rb +++ b/test/ruby/test_rand.rb @@ -2,28 +2,35 @@ require 'test/unit' class TestRand < Test::Unit::TestCase + def teardown + raise if srand == 0 + end def assert_random_int(ws, m, init = 0) - srand(init) - rnds = [Random.new(init)] - rnds2 = [rnds[0].dup] - rnds3 = [rnds[0].dup] - ws.each_with_index do |w, i| - w = w.to_i - assert_equal(w, rand(m)) - rnds.each do |rnd| - assert_equal(w, rnd.rand(m)) + # call srand in another process + assert_separately [], %Q{ + m = #{m} + srand(#{init}) + rnds = [Random.new(#{init})] + rnds2 = [rnds[0].dup] + rnds3 = [rnds[0].dup] + #{ws.inspect}.each_with_index do |w, i| + w = w.to_i + assert_equal(w, rand(m)) + rnds.each do |rnd| + assert_equal(w, rnd.rand(m)) + end + rnds2.each do |rnd| + r=rnd.rand(i...(m+i)) + assert_equal(w+i, r) + end + rnds3.each do |rnd| + r=rnd.rand(i..(m+i-1)) + assert_equal(w+i, r) + end + rnds << Marshal.load(Marshal.dump(rnds[-1])) + rnds2 << Marshal.load(Marshal.dump(rnds2[-1])) end - rnds2.each do |rnd| - r=rnd.rand(i...(m+i)) - assert_equal(w+i, r) - end - rnds3.each do |rnd| - r=rnd.rand(i..(m+i-1)) - assert_equal(w+i, r) - end - rnds << Marshal.load(Marshal.dump(rnds[-1])) - rnds2 << Marshal.load(Marshal.dump(rnds2[-1])) - end + } end def test_mt @@ -122,6 +129,7 @@ class TestRand < Test::Unit::TestCase end def test_types + assert_separately [], <<-EOT srand(0) rnd = Random.new(0) assert_equal(44, rand(100.0)) @@ -154,9 +162,11 @@ class TestRand < Test::Unit::TestCase assert_equal(47, rnd.rand(o)) assert_equal(64, rand(o)) assert_equal(64, rnd.rand(o)) + EOT end def test_srand + assert_separately [], <<-EOT srand assert_kind_of(Integer, rand(2)) assert_kind_of(Integer, Random.new.rand(2)) @@ -167,13 +177,16 @@ class TestRand < Test::Unit::TestCase assert_equal(w.to_i, rand(0x100000000)) assert_equal(w.to_i, rnd.rand(0x100000000)) } + EOT end def test_shuffle + assert_separately [], <<-EOT srand(0) result = [*1..5].shuffle assert_equal([*1..5], result.sort) assert_equal(result, [*1..5].shuffle(random: Random.new(0))) + EOT end def test_big_seed @@ -239,6 +252,7 @@ class TestRand < Test::Unit::TestCase end def test_random_state + assert_separately [], <<-EOT state = <