Debian Codegraph @

vendor/mruby-source/mrbgems/mruby-random/test/random.rb

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