summaryrefslogtreecommitdiff
path: root/spec/ruby/core/kernel/rand_spec.rb
blob: 355e425792eca5eb2d12509ec5e225d5674f4a9c (plain)
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
require_relative '../../spec_helper'
require_relative 'fixtures/classes'

describe "Kernel#rand" do
  it "is a private method" do
    Kernel.should have_private_instance_method(:rand)
  end

  it "returns a float if no argument is passed" do
    rand.should be_kind_of(Float)
  end

  it "returns an integer for an integer argument" do
    rand(77).should be_kind_of(Integer)
  end

  it "returns an integer for a float argument greater than 1" do
    rand(1.3).should be_kind_of(Integer)
  end

  it "returns a float for an argument between -1 and 1" do
    rand(-0.999).should be_kind_of(Float)
    rand(-0.01).should be_kind_of(Float)
    rand(0).should be_kind_of(Float)
    rand(0.01).should be_kind_of(Float)
    rand(0.999).should be_kind_of(Float)
  end

  it "ignores the sign of the argument" do
    [0, 1, 2, 3].should include(rand(-4))
  end

  it "never returns a value greater or equal to 1.0 with no arguments" do
    1000.times do
      (0...1.0).should include(rand)
    end
  end

  it "never returns a value greater or equal to any passed in max argument" do
    1000.times do
      (0...100).to_a.should include(rand(100))
    end
  end

  it "calls to_int on its argument" do
    l = mock('limit')
    l.should_receive(:to_int).and_return 7

    rand l
  end

  context "given an exclusive range" do
    it "returns an Integer between the two Integers" do
      1000.times do
        x = rand(4...6)
        x.should be_kind_of(Integer)
        (4...6).should include(x)
      end
    end

    it "returns a Float between the given Integer and Float" do
      1000.times do
        x = rand(4...6.5)
        x.should be_kind_of(Float)
        (4...6.5).should include(x)
      end
    end

    it "returns a Float between the given Float and Integer" do
      1000.times do
        x = rand(3.5...6)
        x.should be_kind_of(Float)
        (3.5...6).should include(x)
      end
    end

    it "returns a Float between the two given Floats" do
      1000.times do
        x = rand(3.5...6.5)
        x.should be_kind_of(Float)
        (3.5...6.5).should include(x)
      end
    end
  end

  context "given an inclusive range" do
    it "returns an Integer between the two Integers" do
      1000.times do
        x = rand(4..6)
        x.should be_kind_of(Integer)
        (4..6).should include(x)
      end
    end

    it "returns a Float between the given Integer and Float" do
      1000.times do
        x = rand(4..6.5)
        x.should be_kind_of(Float)
        (4..6.5).should include(x)
      end
    end

    it "returns a Float between the given Float and Integer" do
      1000.times do
        x = rand(3.5..6)
        x.should be_kind_of(Float)
        (3.5..6).should include(x)
      end
    end

    it "returns a Float between the two given Floats" do
      1000.times do
        x = rand(3.5..6.5)
        x.should be_kind_of(Float)
        (3.5..6.5).should include(x)
      end
    end
  end

  context "given an inclusive range between 0 and 1" do
    it "returns an Integer between the two Integers" do
      x = rand(0..1)
      x.should be_kind_of(Integer)
      (0..1).should include(x)
    end

    it "returns a Float if at least one side is Float" do
      seed = 42
      x1 = Random.new(seed).rand(0..1.0)
      x2 = Random.new(seed).rand(0.0..1.0)
      x3 = Random.new(seed).rand(0.0..1)

      x3.should be_kind_of(Float)
      x1.should eql(x3)
      x2.should eql(x3)

      (0.0..1.0).should include(x3)
    end
  end

  context "given an exclusive range between 0 and 1" do
    it "returns zero as an Integer" do
      x = rand(0...1)
      x.should be_kind_of(Integer)
      x.should eql(0)
    end

    it "returns a Float if at least one side is Float" do
      seed = 42
      x1 = Random.new(seed).rand(0...1.0)
      x2 = Random.new(seed).rand(0.0...1.0)
      x3 = Random.new(seed).rand(0.0...1)

      x3.should be_kind_of(Float)
      x1.should eql(x3)
      x2.should eql(x3)

      (0.0...1.0).should include(x3)
    end
  end

  it "returns a numeric for an range argument where max is < 1" do
    rand(0.25..0.75).should be_kind_of(Numeric)
  end

  it "returns nil when range is backwards" do
    rand(1..0).should be_nil
  end

  it "returns the range start/end when Float range is 0" do
    rand(1.0..1.0).should eql(1.0)
  end

  it "returns the range start/end when Integer range is 0" do
    rand(42..42).should eql(42)
  end

  it "supports custom object types" do
    rand(KernelSpecs::CustomRangeInteger.new(1)..KernelSpecs::CustomRangeInteger.new(42)).should be_an_instance_of(KernelSpecs::CustomRangeInteger)
    rand(KernelSpecs::CustomRangeFloat.new(1.0)..KernelSpecs::CustomRangeFloat.new(42.0)).should be_an_instance_of(KernelSpecs::CustomRangeFloat)
    rand(Time.now..Time.now).should be_an_instance_of(Time)
  end

  it "is random on boot" do
    results = 2.times.map {
      out = ruby_exe('p rand', options: '--disable-gems')
      Float(out)
    }
    results.size.should == 2
    # this is technically flaky, but very unlikely in a good distribution
    results[0].should_not == results[1]
  end
end

describe "Kernel.rand" do
  it "needs to be reviewed for spec completeness"
end