1 | ##
|
---|
2 | # Random Test
|
---|
3 |
|
---|
4 | assert("Random#srand") do
|
---|
5 | r1 = Random.new(123)
|
---|
6 | r2 = Random.new(123)
|
---|
7 | r1.rand == r2.rand
|
---|
8 | end
|
---|
9 |
|
---|
10 | assert("Kernel::srand") do
|
---|
11 | srand(234)
|
---|
12 | r1 = rand
|
---|
13 | srand(234)
|
---|
14 | r2 = rand
|
---|
15 | r1 == r2
|
---|
16 | end
|
---|
17 |
|
---|
18 | assert("Random::srand") do
|
---|
19 | Random.srand(345)
|
---|
20 | r1 = rand
|
---|
21 | srand(345)
|
---|
22 | r2 = Random.rand
|
---|
23 | r1 == r2
|
---|
24 | end
|
---|
25 |
|
---|
26 | assert("fixnum") do
|
---|
27 | rand(3).class == Fixnum
|
---|
28 | end
|
---|
29 |
|
---|
30 | assert("float") do
|
---|
31 | rand.class == Float
|
---|
32 | end
|
---|
33 |
|
---|
34 | assert("Array#shuffle") do
|
---|
35 | ary = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
|
---|
36 | shuffled = ary.shuffle
|
---|
37 |
|
---|
38 | ary == [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] and shuffled != ary and 10.times { |x| ary.include? x }
|
---|
39 | end
|
---|
40 |
|
---|
41 | assert('Array#shuffle!') do
|
---|
42 | ary = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
|
---|
43 | ary.shuffle!
|
---|
44 |
|
---|
45 | ary != [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] and 10.times { |x| ary.include? x }
|
---|
46 | end
|
---|
47 |
|
---|
48 | assert("Array#shuffle(random)") do
|
---|
49 | assert_raise(TypeError) do
|
---|
50 | # this will cause an exception due to the wrong argument
|
---|
51 | [1, 2].shuffle "Not a Random instance"
|
---|
52 | end
|
---|
53 |
|
---|
54 | # verify that the same seed causes the same results
|
---|
55 | ary1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
|
---|
56 | shuffle1 = ary1.shuffle Random.new 345
|
---|
57 | ary2 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
|
---|
58 | shuffle2 = ary2.shuffle Random.new 345
|
---|
59 |
|
---|
60 | ary1 != shuffle1 and 10.times { |x| shuffle1.include? x } and shuffle1 == shuffle2
|
---|
61 | end
|
---|
62 |
|
---|
63 | assert('Array#shuffle!(random)') do
|
---|
64 | assert_raise(TypeError) do
|
---|
65 | # this will cause an exception due to the wrong argument
|
---|
66 | [1, 2].shuffle! "Not a Random instance"
|
---|
67 | end
|
---|
68 |
|
---|
69 | # verify that the same seed causes the same results
|
---|
70 | ary1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
|
---|
71 | ary1.shuffle! Random.new 345
|
---|
72 | ary2 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
|
---|
73 | ary2.shuffle! Random.new 345
|
---|
74 |
|
---|
75 | ary1 != [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] and 10.times { |x| ary1.include? x } and ary1 == ary2
|
---|
76 | end
|
---|
77 |
|
---|
78 | assert('Array#sample checks input length after reading arguments') do
|
---|
79 | $ary = [1, 2, 3]
|
---|
80 | class ArrayChange
|
---|
81 | def to_i
|
---|
82 | $ary << 4
|
---|
83 | 4
|
---|
84 | end
|
---|
85 | end
|
---|
86 |
|
---|
87 | assert_equal [1, 2, 3, 4], $ary.sample(ArrayChange.new).sort
|
---|
88 | end
|
---|