File: rand_spec.rb

package info (click to toggle)
jruby 1.7.26-1%2Bdeb9u1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 84,572 kB
  • sloc: ruby: 669,910; java: 253,056; xml: 35,152; ansic: 9,187; yacc: 7,267; cpp: 5,244; sh: 1,036; makefile: 345; jsp: 48; tcl: 40
file content (218 lines) | stat: -rw-r--r-- 6,384 bytes parent folder | download
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
require File.expand_path('../../../spec_helper', __FILE__)

ruby_version_is "1.9" do
  describe "Random.rand" do
    it "returns a Float if no max argument is passed" do
      Random.rand.should be_kind_of(Float)
    end

    it "returns a Float >= 0 if no max argument is passed" do
      floats = 200.times.map { Random.rand }
      floats.min.should >= 0
    end

    it "returns a Float < 1 if no max argument is passed" do
      floats = 200.times.map { Random.rand }
      floats.max.should < 1
    end

    it "returns the same sequence for a given seed if no max argument is passed" do
      Random.srand 33
      floats_a = 20.times.map { Random.rand }
      Random.srand 33
      floats_b = 20.times.map { Random.rand }
      floats_a.should == floats_b
    end

    it "returns a Integer if an Integer argument is passed" do
      Random.rand(20).should be_kind_of(Integer)
    end

    it "returns an Integer >= 0 if an Integer argument is passed" do
      ints = 200.times.map { Random.rand(34) }
      ints.min.should >= 0
    end

    it "returns an Integer < the max argument if an Integer argument is passed" do
      ints = 200.times.map { Random.rand(55) }
      ints.max.should < 55
    end

    it "returns the same sequence for a given seed if an Integer argument is passed" do
      Random.srand 33
      floats_a = 20.times.map { Random.rand(90) }
      Random.srand 33
      floats_b = 20.times.map { Random.rand(90) }
      floats_a.should == floats_b
    end

    it "coerces arguments to Integers with #to_int" do
      obj = mock_numeric('int')
      obj.should_receive(:to_int).and_return(99)
      Random.rand(obj).should be_kind_of(Integer)
    end
  end

  describe "Random#rand with Fixnum" do
    it "returns an Integer" do
      Random.new.rand(20).should be_an_instance_of(Fixnum)
    end

    it "returns a Fixnum greater than or equal to 0" do
      prng = Random.new
      ints = 20.times.map { prng.rand(5) }
      ints.min.should >= 0
    end

    it "returns a Fixnum less than the argument" do
      prng = Random.new
      ints = 20.times.map { prng.rand(5) }
      ints.max.should <= 4
    end

    it "returns the same sequence for a given seed" do
      prng = Random.new 33
      a = 20.times.map { prng.rand(90) }
      prng = Random.new 33
      b = 20.times.map { prng.rand(90) }
      a.should == b
    end

    it "eventually returns all possible values" do
      prng = Random.new 33
      100.times.map{ prng.rand(10) }.uniq.sort.should == (0...10).to_a
    end

    it "raises an ArgumentError when the argument is 0" do
      lambda do
        Random.new.rand(0)
      end.should raise_error(ArgumentError)
    end

    it "raises an ArgumentError when the argument is negative" do
      lambda do
        Random.new.rand(-12)
      end.should raise_error(ArgumentError)
    end
  end

  describe "Random#rand with Bignum" do
    it "typically returns a Bignum" do
      rnd = Random.new(1)
      10.times.map{ rnd.rand(bignum_value) }.max.should be_an_instance_of(Bignum)
    end

    it "returns a Bignum greater than or equal to 0" do
      prng = Random.new
      bigs = 20.times.map { prng.rand(bignum_value) }
      bigs.min.should >= 0
    end

    it "returns a Bignum less than the argument" do
      prng = Random.new
      bigs = 20.times.map { prng.rand(bignum_value) }
      bigs.max.should < bignum_value
    end

    it "returns the same sequence for a given seed" do
      prng = Random.new 33
      a = 20.times.map { prng.rand(bignum_value) }
      prng = Random.new 33
      b = 20.times.map { prng.rand(bignum_value) }
      a.should == b
    end

    it "raises an ArgumentError when the argument is negative" do
      lambda do
        Random.new.rand(-bignum_value)
      end.should raise_error(ArgumentError)
    end
  end

  describe "Random#rand with Float" do
    it "returns a Float" do
      Random.new.rand(20.43).should be_an_instance_of(Float)
    end

    it "returns a Float greater than or equal to 0.0" do
      prng = Random.new
      floats = 20.times.map { prng.rand(5.2) }
      floats.min.should >= 0.0
    end

    it "returns a Float less than the argument" do
      prng = Random.new
      floats = 20.times.map { prng.rand(4.30) }
      floats.max.should < 4.30
    end

    it "returns the same sequence for a given seed" do
      prng = Random.new 33
      a = 20.times.map { prng.rand(89.2928) }
      prng = Random.new 33
      b = 20.times.map { prng.rand(89.2928) }
      a.should == b
    end

    it "raises an ArgumentError when the argument is negative" do
      lambda do
        Random.new.rand(-1.234567)
      end.should raise_error(ArgumentError)
    end
  end

  describe "Random#rand with Range" do
    it "returns an element from the Range" do
      Random.new.rand(20..43).should be_an_instance_of(Fixnum)
    end

    it "returns an object that is a member of the Range" do
      prng = Random.new
      r = 20..30
      20.times { r.member?(prng.rand(r)).should be_true }
    end

    it "works with inclusive ranges" do
      prng = Random.new
      r = 3..5
      40.times.map { prng.rand(r) }.uniq.sort.should == [3,4,5]
    end

    it "works with exclusive ranges" do
      prng = Random.new
      r = 3...5
      20.times.map { prng.rand(r) }.uniq.sort.should == [3,4]
    end

    it "returns the same sequence for a given seed" do
      prng = Random.new 33
      a = 20.times.map { prng.rand(76890.028..800000.00) }
      prng = Random.new 33
      b = 20.times.map { prng.rand(76890.028..800000.00) }
      a.should == b
    end

    it "eventually returns all possible values" do
      prng = Random.new 33
      100.times.map{ prng.rand(10..20) }.uniq.sort.should == (10..20).to_a
      100.times.map{ prng.rand(10...20) }.uniq.sort.should == (10...20).to_a
    end

    it "considers Integers as Floats if one end point is a float" do
      Random.new(42).rand(0.0..1).should be_kind_of(Float)
      Random.new(42).rand(0..1.0).should be_kind_of(Float)
    end

    it "raises an ArgumentError when the startpoint lacks #+ and #- methods" do
      lambda do
        Random.new.rand(Object.new..67)
      end.should raise_error(ArgumentError)
    end

    it "raises an ArgumentError when the endpoint lacks #+ and #- methods" do
      lambda do
        Random.new.rand(68..Object.new)
      end.should raise_error(ArgumentError)
    end
  end
end