1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
|
require 'test/unit'
class TestRand < Test::Unit::TestCase
def test_srand
srand(123)
a = [rand, rand, rand]
srand(123)
assert_equal(a, [rand, rand, rand])
end
def test_return_float_for_no_arg
r = rand
assert(r.kind_of?(Float))
assert(r < 1.0)
assert(r >= 0.0)
end
def test_return_float_for_zero_arg
r = rand(0)
assert(r.kind_of?(Float))
assert(r < 1.0)
assert(r >= 0.0)
end
def test_one_arg
100.times {
r = rand(1)
assert_equal(0, r)
}
end
def test_rand_int_in_range
do_range_test(2**31 - 1)
end
def test_rand_long_in_range
do_range_test(2**31)
do_range_test(2**64 - 1)
end
def test_rand_bignum_in_range
do_range_test(2**64)
do_range_test(2**100)
end
def test_negative_arg
do_abs_value_arg_test(0)
do_abs_value_arg_test(1)
do_abs_value_arg_test(2**31 - 1)
do_abs_value_arg_test(2**31)
do_abs_value_arg_test(2**64 - 1)
do_abs_value_arg_test(2**64)
do_abs_value_arg_test(2**100)
end
def do_range_test(max)
100.times {
r = rand(max)
assert(r < max)
assert(r >= 0)
}
end
def do_abs_value_arg_test(max)
srand(42)
r1 = rand(max)
srand(42)
r2 = rand(-max)
assert(r1 == r2)
end
end
|