mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
srand() should not run in tests.
test_rand.rb calls srand() several times, however it change global rand-sequence (and --seeds doesn't have meaning). This patch makes such tests run in other processes.
This commit is contained in:
parent
471a969311
commit
4d132fa130
Notes:
git
2020-01-28 18:24:27 +09:00
1 changed files with 46 additions and 24 deletions
|
@ -2,28 +2,35 @@
|
||||||
require 'test/unit'
|
require 'test/unit'
|
||||||
|
|
||||||
class TestRand < Test::Unit::TestCase
|
class TestRand < Test::Unit::TestCase
|
||||||
|
def teardown
|
||||||
|
raise if srand == 0
|
||||||
|
end
|
||||||
def assert_random_int(ws, m, init = 0)
|
def assert_random_int(ws, m, init = 0)
|
||||||
srand(init)
|
# call srand in another process
|
||||||
rnds = [Random.new(init)]
|
assert_separately [], %Q{
|
||||||
rnds2 = [rnds[0].dup]
|
m = #{m}
|
||||||
rnds3 = [rnds[0].dup]
|
srand(#{init})
|
||||||
ws.each_with_index do |w, i|
|
rnds = [Random.new(#{init})]
|
||||||
w = w.to_i
|
rnds2 = [rnds[0].dup]
|
||||||
assert_equal(w, rand(m))
|
rnds3 = [rnds[0].dup]
|
||||||
rnds.each do |rnd|
|
#{ws.inspect}.each_with_index do |w, i|
|
||||||
assert_equal(w, rnd.rand(m))
|
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
|
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
|
end
|
||||||
|
|
||||||
def test_mt
|
def test_mt
|
||||||
|
@ -122,6 +129,7 @@ class TestRand < Test::Unit::TestCase
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_types
|
def test_types
|
||||||
|
assert_separately [], <<-EOT
|
||||||
srand(0)
|
srand(0)
|
||||||
rnd = Random.new(0)
|
rnd = Random.new(0)
|
||||||
assert_equal(44, rand(100.0))
|
assert_equal(44, rand(100.0))
|
||||||
|
@ -154,9 +162,11 @@ class TestRand < Test::Unit::TestCase
|
||||||
assert_equal(47, rnd.rand(o))
|
assert_equal(47, rnd.rand(o))
|
||||||
assert_equal(64, rand(o))
|
assert_equal(64, rand(o))
|
||||||
assert_equal(64, rnd.rand(o))
|
assert_equal(64, rnd.rand(o))
|
||||||
|
EOT
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_srand
|
def test_srand
|
||||||
|
assert_separately [], <<-EOT
|
||||||
srand
|
srand
|
||||||
assert_kind_of(Integer, rand(2))
|
assert_kind_of(Integer, rand(2))
|
||||||
assert_kind_of(Integer, Random.new.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, rand(0x100000000))
|
||||||
assert_equal(w.to_i, rnd.rand(0x100000000))
|
assert_equal(w.to_i, rnd.rand(0x100000000))
|
||||||
}
|
}
|
||||||
|
EOT
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_shuffle
|
def test_shuffle
|
||||||
|
assert_separately [], <<-EOT
|
||||||
srand(0)
|
srand(0)
|
||||||
result = [*1..5].shuffle
|
result = [*1..5].shuffle
|
||||||
assert_equal([*1..5], result.sort)
|
assert_equal([*1..5], result.sort)
|
||||||
assert_equal(result, [*1..5].shuffle(random: Random.new(0)))
|
assert_equal(result, [*1..5].shuffle(random: Random.new(0)))
|
||||||
|
EOT
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_big_seed
|
def test_big_seed
|
||||||
|
@ -239,6 +252,7 @@ class TestRand < Test::Unit::TestCase
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_random_state
|
def test_random_state
|
||||||
|
assert_separately [], <<-EOT
|
||||||
state = <<END
|
state = <<END
|
||||||
3877134065023083674777481835852171977222677629000095857864323111193832400974413
|
3877134065023083674777481835852171977222677629000095857864323111193832400974413
|
||||||
4782302161934463784850675209112299537259006497924090422596764895633625964527441
|
4782302161934463784850675209112299537259006497924090422596764895633625964527441
|
||||||
|
@ -325,9 +339,11 @@ END
|
||||||
assert_equal(state, Random.instance_eval { state })
|
assert_equal(state, Random.instance_eval { state })
|
||||||
r.rand(0x100)
|
r.rand(0x100)
|
||||||
assert_equal(state, r.instance_eval { state })
|
assert_equal(state, r.instance_eval { state })
|
||||||
|
EOT
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_random_left
|
def test_random_left
|
||||||
|
assert_separately [], <<-EOT
|
||||||
r = Random.new(0)
|
r = Random.new(0)
|
||||||
assert_equal(1, r.instance_eval { left })
|
assert_equal(1, r.instance_eval { left })
|
||||||
r.rand(0x100)
|
r.rand(0x100)
|
||||||
|
@ -340,25 +356,30 @@ END
|
||||||
assert_equal(624, Random.instance_eval { left })
|
assert_equal(624, Random.instance_eval { left })
|
||||||
rand(0x100)
|
rand(0x100)
|
||||||
assert_equal(623, Random.instance_eval { left })
|
assert_equal(623, Random.instance_eval { left })
|
||||||
|
EOT
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_random_bytes
|
def test_random_bytes
|
||||||
assert_random_bytes(Random.new(0))
|
assert_random_bytes
|
||||||
end
|
end
|
||||||
|
|
||||||
def assert_random_bytes(r)
|
def assert_random_bytes
|
||||||
|
assert_separately [], <<-EOT
|
||||||
|
r = Random.new(0)
|
||||||
srand(0)
|
srand(0)
|
||||||
assert_equal("", r.bytes(0))
|
assert_equal("", r.bytes(0))
|
||||||
assert_equal("", Random.bytes(0))
|
assert_equal("", Random.bytes(0))
|
||||||
x = "\xAC".force_encoding("ASCII-8BIT")
|
x = "\\xAC".force_encoding("ASCII-8BIT")
|
||||||
assert_equal(x, r.bytes(1))
|
assert_equal(x, r.bytes(1))
|
||||||
assert_equal(x, Random.bytes(1))
|
assert_equal(x, Random.bytes(1))
|
||||||
x = "/\xAA\xC4\x97u\xA6\x16\xB7\xC0\xCC".force_encoding("ASCII-8BIT")
|
x = "/\\xAA\\xC4\\x97u\\xA6\\x16\\xB7\\xC0\\xCC".force_encoding("ASCII-8BIT")
|
||||||
assert_equal(x, r.bytes(10))
|
assert_equal(x, r.bytes(10))
|
||||||
assert_equal(x, Random.bytes(10))
|
assert_equal(x, Random.bytes(10))
|
||||||
|
EOT
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_random_range
|
def test_random_range
|
||||||
|
assert_separately [], <<-EOT
|
||||||
srand(0)
|
srand(0)
|
||||||
r = Random.new(0)
|
r = Random.new(0)
|
||||||
%w(9 5 8).each {|w|
|
%w(9 5 8).each {|w|
|
||||||
|
@ -387,6 +408,7 @@ END
|
||||||
now = Time.now
|
now = Time.now
|
||||||
assert_equal(now, rand(now..now))
|
assert_equal(now, rand(now..now))
|
||||||
assert_equal(now, r.rand(now..now))
|
assert_equal(now, r.rand(now..now))
|
||||||
|
EOT
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_random_float
|
def test_random_float
|
||||||
|
|
Loading…
Reference in a new issue