summaryrefslogtreecommitdiff
path: root/spec/ruby/core/random/rand_spec.rb
diff options
context:
space:
mode:
Diffstat (limited to 'spec/ruby/core/random/rand_spec.rb')
-rw-r--r--spec/ruby/core/random/rand_spec.rb64
1 files changed, 36 insertions, 28 deletions
diff --git a/spec/ruby/core/random/rand_spec.rb b/spec/ruby/core/random/rand_spec.rb
index e0cb133abd..c882db6381 100644
--- a/spec/ruby/core/random/rand_spec.rb
+++ b/spec/ruby/core/random/rand_spec.rb
@@ -1,9 +1,10 @@
-require File.expand_path('../../../spec_helper', __FILE__)
+require_relative '../../spec_helper'
+require_relative 'fixtures/classes'
+require_relative 'shared/rand'
describe "Random.rand" do
- it "returns a Float if no max argument is passed" do
- Random.rand.should be_kind_of(Float)
- end
+ it_behaves_like :random_number, :rand, Random.new
+ it_behaves_like :random_number, :rand, Random
it "returns a Float >= 0 if no max argument is passed" do
floats = 200.times.map { Random.rand }
@@ -23,10 +24,6 @@ describe "Random.rand" do
floats_a.should == floats_b
end
- it "returns an 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
@@ -48,13 +45,13 @@ describe "Random.rand" do
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)
+ Random.rand(obj).should.is_a?(Integer)
end
end
describe "Random#rand with Fixnum" do
it "returns an Integer" do
- Random.new.rand(20).should be_an_instance_of(Fixnum)
+ Random.new.rand(20).should.instance_of?(Integer)
end
it "returns a Fixnum greater than or equal to 0" do
@@ -83,22 +80,22 @@ describe "Random#rand with Fixnum" do
end
it "raises an ArgumentError when the argument is 0" do
- lambda do
+ -> do
Random.new.rand(0)
- end.should raise_error(ArgumentError)
+ end.should.raise(ArgumentError)
end
it "raises an ArgumentError when the argument is negative" do
- lambda do
+ -> do
Random.new.rand(-12)
- end.should raise_error(ArgumentError)
+ end.should.raise(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*2) }.max.should be_an_instance_of(Bignum)
+ 10.times.map{ rnd.rand(bignum_value*2) }.max.should.instance_of?(Integer)
end
it "returns a Bignum greater than or equal to 0" do
@@ -122,15 +119,15 @@ describe "Random#rand with Bignum" do
end
it "raises an ArgumentError when the argument is negative" do
- lambda do
+ -> do
Random.new.rand(-bignum_value)
- end.should raise_error(ArgumentError)
+ end.should.raise(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)
+ Random.new.rand(20.43).should.instance_of?(Float)
end
it "returns a Float greater than or equal to 0.0" do
@@ -154,21 +151,27 @@ describe "Random#rand with Float" do
end
it "raises an ArgumentError when the argument is negative" do
- lambda do
+ -> do
Random.new.rand(-1.234567)
- end.should raise_error(ArgumentError)
+ end.should.raise(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)
+ Random.new.rand(20..43).should.instance_of?(Integer)
+ end
+
+ it "supports custom object types" do
+ rand(RandomSpecs::CustomRangeInteger.new(1)..RandomSpecs::CustomRangeInteger.new(42)).should.instance_of?(RandomSpecs::CustomRangeInteger)
+ rand(RandomSpecs::CustomRangeFloat.new(1.0)..RandomSpecs::CustomRangeFloat.new(42.0)).should.instance_of?(RandomSpecs::CustomRangeFloat)
+ rand(Time.now..Time.now).should.instance_of?(Time)
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 }
+ 20.times { r.member?(prng.rand(r)).should == true }
end
it "works with inclusive ranges" do
@@ -198,19 +201,24 @@ describe "Random#rand with Range" do
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)
+ Random.new(42).rand(0.0..1).should.is_a?(Float)
+ Random.new(42).rand(0..1.0).should.is_a?(Float)
+ end
+
+ it "returns a float within a given float range" do
+ Random.new(42).rand(0.0...100.0).should == 37.454011884736246
+ Random.new(42).rand(-100.0...0.0).should == -62.545988115263754
end
it "raises an ArgumentError when the startpoint lacks #+ and #- methods" do
- lambda do
+ -> do
Random.new.rand(Object.new..67)
- end.should raise_error(ArgumentError)
+ end.should.raise(ArgumentError)
end
it "raises an ArgumentError when the endpoint lacks #+ and #- methods" do
- lambda do
+ -> do
Random.new.rand(68..Object.new)
- end.should raise_error(ArgumentError)
+ end.should.raise(ArgumentError)
end
end