From 7e6dd6142e30e09976307e2626fd1ae9a36ab83e Mon Sep 17 00:00:00 2001 From: mame Date: Tue, 25 Mar 2008 15:56:36 +0000 Subject: * test/ruby/test_rand.rb: add tests to achieve over 95% test coverage of random.c. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@15841 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 5 +++++ test/ruby/test_rand.rb | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+) diff --git a/ChangeLog b/ChangeLog index 143d624ba8..8eeeace69a 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,8 @@ +Wed Mar 26 00:55:28 2008 Yusuke Endoh + + * test/ruby/test_rand.rb: add tests to achieve over 95% test coverage + of random.c. + Wed Mar 26 00:28:55 2008 Yusuke Endoh * test/ruby/test_rational.rb: add tests to achieve over 90% test diff --git a/test/ruby/test_rand.rb b/test/ruby/test_rand.rb index e2fd127f9c..b7d841dbba 100644 --- a/test/ruby/test_rand.rb +++ b/test/ruby/test_rand.rb @@ -128,4 +128,40 @@ class TestRand < Test::Unit::TestCase ws.each {|w| assert_equal(w.to_i, rand(-0x10000)) } end + def test_types + srand(0) + assert_equal(44, rand(100.0)) + assert_equal(1245085576965981900420779258691, rand((2**100).to_f)) + assert_equal(914679880601515615685077935113, rand(-(2**100).to_f)) + + srand(0) + assert_equal(997707939797331598305742933184, rand(2**100)) + assert_in_delta(0.602763376071644, rand((2**100).coerce(0).first), + 0.000000000000001) + + srand(0) + assert_in_delta(0.548813503927325, rand(nil), + 0.000000000000001) + srand(0) + o = Object.new + def o.to_i; 100; end + assert_equal(44, rand(o)) + assert_equal(47, rand(o)) + assert_equal(64, rand(o)) + end + + def test_srand + srand + assert_kind_of(Integer, rand(2)) + + srand(2**100) + %w(3258412053).each {|w| + assert_equal(w.to_i, rand(0x100000000)) + } + end + + def test_shuffle + srand(0) + assert_equal([1,4,2,5,3], [1,2,3,4,5].shuffle) + end end -- cgit v1.2.3