[前][次][番号順一覧][スレッド一覧]

ruby-changes:59861

From: Koichi <ko1@a...>
Date: Wed, 29 Jan 2020 01:01:13 +0900 (JST)
Subject: [ruby-changes:59861] 650f152d2f (master): Revert "srand() should not run in tests."

https://git.ruby-lang.org/ruby.git/commit/?id=650f152d2f

From 650f152d2f5ed929c76cd7e7cf17270e53ac346c Mon Sep 17 00:00:00 2001
From: Koichi Sasada <ko1@a...>
Date: Wed, 29 Jan 2020 00:55:27 +0900
Subject: Revert "srand() should not run in tests."

This reverts commit 4d132fa130e16eeb4af4177cfaccc00e05e2f864.

There are discussions about using srand() in tests.
I'll write a ticket about it and continue to discuss.

diff --git a/test/ruby/test_rand.rb b/test/ruby/test_rand.rb
index 0808c1e..939d17b 100644
--- a/test/ruby/test_rand.rb
+++ b/test/ruby/test_rand.rb
@@ -2,35 +2,28 @@ https://github.com/ruby/ruby/blob/trunk/test/ruby/test_rand.rb#L2
 require 'test/unit'
 
 class TestRand < Test::Unit::TestCase
-  def teardown
-    raise if srand == 0
-  end
   def assert_random_int(ws, m, init = 0)
-    # 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]))
+    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))
       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
@@ -129,7 +122,6 @@ class TestRand < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_rand.rb#L122
   end
 
   def test_types
-    assert_separately [], <<-EOT
     srand(0)
     rnd = Random.new(0)
     assert_equal(44, rand(100.0))
@@ -162,11 +154,9 @@ class TestRand < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_rand.rb#L154
     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))
@@ -177,16 +167,13 @@ class TestRand < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_rand.rb#L167
       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
@@ -252,7 +239,6 @@ class TestRand < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_rand.rb#L239
   end
 
   def test_random_state
-    assert_separately [], <<-EOT
     state = <<END
 3877134065023083674777481835852171977222677629000095857864323111193832400974413
 4782302161934463784850675209112299537259006497924090422596764895633625964527441
@@ -339,11 +325,9 @@ END https://github.com/ruby/ruby/blob/trunk/test/ruby/test_rand.rb#L325
     assert_equal(state, Random.instance_eval { state })
     r.rand(0x100)
     assert_equal(state, r.instance_eval { state })
-    EOT
   end
 
   def test_random_left
-    assert_separately [], <<-EOT
     r = Random.new(0)
     assert_equal(1, r.instance_eval { left })
     r.rand(0x100)
@@ -356,30 +340,25 @@ END https://github.com/ruby/ruby/blob/trunk/test/ruby/test_rand.rb#L340
     assert_equal(624, Random.instance_eval { left })
     rand(0x100)
     assert_equal(623, Random.instance_eval { left })
-    EOT
   end
 
   def test_random_bytes
-    assert_random_bytes
+    assert_random_bytes(Random.new(0))
   end
 
-  def assert_random_bytes
-    assert_separately [], <<-EOT
-    r = Random.new(0)
+  def assert_random_bytes(r)
     srand(0)
     assert_equal("", r.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, 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, Random.bytes(10))
-    EOT
   end
 
   def test_random_range
-    assert_separately [], <<-EOT
     srand(0)
     r = Random.new(0)
     %w(9 5 8).each {|w|
@@ -408,7 +387,6 @@ END https://github.com/ruby/ruby/blob/trunk/test/ruby/test_rand.rb#L387
     now = Time.now
     assert_equal(now, rand(now..now))
     assert_equal(now, r.rand(now..now))
-    EOT
   end
 
   def test_random_float
-- 
cgit v0.10.2


--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]