[270] | 1 | ##
|
---|
| 2 | # Random Test
|
---|
| 3 |
|
---|
[439] | 4 | assert("Random.new") do
|
---|
[270] | 5 | r1 = Random.new(123)
|
---|
| 6 | r2 = Random.new(123)
|
---|
[439] | 7 | r3 = Random.new(124)
|
---|
| 8 | assert_equal(r1.rand, r2.rand)
|
---|
| 9 | assert_not_equal(r1.rand, r3.rand)
|
---|
[270] | 10 | end
|
---|
| 11 |
|
---|
[439] | 12 | assert("Kernel.srand") do
|
---|
[270] | 13 | srand(234)
|
---|
| 14 | r1 = rand
|
---|
| 15 | srand(234)
|
---|
| 16 | r2 = rand
|
---|
[439] | 17 | srand(235)
|
---|
| 18 | r3 = rand
|
---|
| 19 | assert_equal(r1, r2)
|
---|
| 20 | assert_not_equal(r1, r3)
|
---|
[270] | 21 | end
|
---|
| 22 |
|
---|
[439] | 23 | assert("Random.srand") do
|
---|
[270] | 24 | Random.srand(345)
|
---|
| 25 | r1 = rand
|
---|
| 26 | srand(345)
|
---|
| 27 | r2 = Random.rand
|
---|
[439] | 28 | Random.srand(346)
|
---|
| 29 | r3 = rand
|
---|
| 30 | assert_equal(r1, r2)
|
---|
| 31 | assert_not_equal(r1, r3)
|
---|
[270] | 32 | end
|
---|
| 33 |
|
---|
[439] | 34 | assert("return class of Kernel.rand") do
|
---|
| 35 | assert_kind_of(Fixnum, rand(3))
|
---|
| 36 | assert_kind_of(Fixnum, rand(1.5))
|
---|
| 37 | assert_kind_of(Float, rand)
|
---|
| 38 | assert_kind_of(Float, rand(0.5))
|
---|
[270] | 39 | end
|
---|
| 40 |
|
---|
| 41 | assert("Array#shuffle") do
|
---|
[439] | 42 | orig = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
|
---|
| 43 | ary = orig.dup
|
---|
[270] | 44 | shuffled = ary.shuffle
|
---|
[439] | 45 | assert_equal(orig, ary)
|
---|
| 46 | assert_not_equal(ary, shuffled)
|
---|
| 47 | assert_equal(orig, shuffled.sort)
|
---|
[270] | 48 | end
|
---|
| 49 |
|
---|
| 50 | assert('Array#shuffle!') do
|
---|
[439] | 51 | orig = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
|
---|
| 52 | ary = orig.dup
|
---|
| 53 | assert_same(ary, ary.shuffle!)
|
---|
| 54 | assert_not_equal(orig, ary)
|
---|
| 55 | assert_equal(orig, ary.sort)
|
---|
[270] | 56 | end
|
---|
| 57 |
|
---|
| 58 | assert("Array#shuffle(random)") do
|
---|
| 59 | assert_raise(TypeError) do
|
---|
| 60 | # this will cause an exception due to the wrong argument
|
---|
| 61 | [1, 2].shuffle "Not a Random instance"
|
---|
| 62 | end
|
---|
| 63 |
|
---|
| 64 | # verify that the same seed causes the same results
|
---|
[439] | 65 | ary = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
|
---|
| 66 | shuffled1 = ary.shuffle Random.new 345
|
---|
| 67 | shuffled2 = ary.shuffle Random.new 345
|
---|
| 68 | shuffled3 = ary.shuffle Random.new 346
|
---|
| 69 | assert_equal(shuffled1, shuffled2)
|
---|
| 70 | assert_not_equal(shuffled1, shuffled3)
|
---|
[270] | 71 | end
|
---|
| 72 |
|
---|
| 73 | assert('Array#shuffle!(random)') do
|
---|
| 74 | assert_raise(TypeError) do
|
---|
| 75 | # this will cause an exception due to the wrong argument
|
---|
| 76 | [1, 2].shuffle! "Not a Random instance"
|
---|
| 77 | end
|
---|
| 78 |
|
---|
| 79 | # verify that the same seed causes the same results
|
---|
| 80 | ary1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
|
---|
| 81 | ary1.shuffle! Random.new 345
|
---|
| 82 | ary2 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
|
---|
| 83 | ary2.shuffle! Random.new 345
|
---|
[439] | 84 | ary3 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
|
---|
| 85 | ary3.shuffle! Random.new 346
|
---|
| 86 | assert_equal(ary1, ary2)
|
---|
| 87 | assert_not_equal(ary1, ary3)
|
---|
[270] | 88 | end
|
---|
[331] | 89 |
|
---|
[439] | 90 | assert('Array#sample') do
|
---|
| 91 | 100.times do
|
---|
| 92 | assert_include([0, 1, 2], [2, 1, 0].sample)
|
---|
| 93 | [2, 1, 0].sample(2).each { |sample| assert_include([0, 1, 2], sample) }
|
---|
| 94 | h = {}
|
---|
| 95 | (1..10).to_a.sample(7).each do |sample|
|
---|
| 96 | assert_not_include(h, sample)
|
---|
| 97 | h[sample] = true
|
---|
[331] | 98 | end
|
---|
| 99 | end
|
---|
| 100 |
|
---|
[439] | 101 | assert_nil([].sample)
|
---|
| 102 | assert_equal([], [].sample(1))
|
---|
| 103 | assert_equal([], [2, 1].sample(0))
|
---|
| 104 | assert_raise(TypeError) { [2, 1].sample(true) }
|
---|
| 105 | assert_raise(ArgumentError) { [2, 1].sample(-1) }
|
---|
[331] | 106 | end
|
---|
[439] | 107 |
|
---|
| 108 | assert('Array#sample(random)') do
|
---|
| 109 | assert_raise(TypeError) do
|
---|
| 110 | # this will cause an exception due to the wrong argument
|
---|
| 111 | [1, 2].sample(2, "Not a Random instance")
|
---|
| 112 | end
|
---|
| 113 |
|
---|
| 114 | # verify that the same seed causes the same results
|
---|
| 115 | ary = (1..10).to_a
|
---|
| 116 | srand(15)
|
---|
| 117 | samples1 = ary.sample(4)
|
---|
| 118 | samples2 = ary.sample(4, Random.new(15))
|
---|
| 119 | samples3 = ary.sample(4, Random.new(16))
|
---|
| 120 | assert_equal(samples1, samples2)
|
---|
| 121 | assert_not_equal(samples1, samples3)
|
---|
| 122 | end
|
---|