diff options
Diffstat (limited to 'spec/ruby/core/random')
| -rw-r--r-- | spec/ruby/core/random/bytes_spec.rb | 11 | ||||
| -rw-r--r-- | spec/ruby/core/random/default_spec.rb | 4 | ||||
| -rw-r--r-- | spec/ruby/core/random/fixtures/classes.rb | 15 | ||||
| -rw-r--r-- | spec/ruby/core/random/new_seed_spec.rb | 4 | ||||
| -rw-r--r-- | spec/ruby/core/random/new_spec.rb | 7 | ||||
| -rw-r--r-- | spec/ruby/core/random/rand_spec.rb | 40 | ||||
| -rw-r--r-- | spec/ruby/core/random/random_number_spec.rb | 8 | ||||
| -rw-r--r-- | spec/ruby/core/random/raw_seed_spec.rb | 9 | ||||
| -rw-r--r-- | spec/ruby/core/random/shared/bytes.rb | 8 | ||||
| -rw-r--r-- | spec/ruby/core/random/shared/rand.rb | 9 | ||||
| -rw-r--r-- | spec/ruby/core/random/shared/urandom.rb | 23 | ||||
| -rw-r--r-- | spec/ruby/core/random/urandom_spec.rb | 26 |
12 files changed, 93 insertions, 71 deletions
diff --git a/spec/ruby/core/random/bytes_spec.rb b/spec/ruby/core/random/bytes_spec.rb index 2caf18fbd0..c9be07cd3f 100644 --- a/spec/ruby/core/random/bytes_spec.rb +++ b/spec/ruby/core/random/bytes_spec.rb @@ -1,4 +1,4 @@ -# -*- encoding: binary -*- +# encoding: binary require_relative '../../spec_helper' require_relative 'shared/bytes' @@ -9,7 +9,6 @@ describe "Random#bytes" do Random.new(33).bytes(2).should == Random.new(33).bytes(2) end - # Should double check this is official spec it "returns the same numeric output for a given seed across all implementations and platforms" do rnd = Random.new(33) rnd.bytes(2).should == "\x14\\" @@ -18,15 +17,13 @@ describe "Random#bytes" do end it "returns the same numeric output for a given huge seed across all implementations and platforms" do - rnd = Random.new(bignum_value ** 4) + rnd = Random.new(2 ** (63 * 4)) rnd.bytes(2).should == "_\x91" rnd.bytes(1000) # skip some rnd.bytes(2).should == "\x17\x12" end end -ruby_version_is "2.6" do - describe "Random.bytes" do - it_behaves_like :random_bytes, :bytes, Random - end +describe "Random.bytes" do + it_behaves_like :random_bytes, :bytes, Random end diff --git a/spec/ruby/core/random/default_spec.rb b/spec/ruby/core/random/default_spec.rb index 1d8b1ce5ee..9e4845986d 100644 --- a/spec/ruby/core/random/default_spec.rb +++ b/spec/ruby/core/random/default_spec.rb @@ -1,7 +1,7 @@ require_relative '../../spec_helper' describe "Random::DEFAULT" do - it "returns a Random instance" do - Random::DEFAULT.should be_an_instance_of(Random) + it "is no longer defined" do + Random.should_not.const_defined?(:DEFAULT) end end diff --git a/spec/ruby/core/random/fixtures/classes.rb b/spec/ruby/core/random/fixtures/classes.rb new file mode 100644 index 0000000000..9c8d113e24 --- /dev/null +++ b/spec/ruby/core/random/fixtures/classes.rb @@ -0,0 +1,15 @@ +module RandomSpecs + CustomRangeInteger = Struct.new(:value) do + def to_int; value; end + def <=>(other); to_int <=> other.to_int; end + def -(other); self.class.new(to_int - other.to_int); end + def +(other); self.class.new(to_int + other.to_int); end + end + + CustomRangeFloat = Struct.new(:value) do + def to_f; value; end + def <=>(other); to_f <=> other.to_f; end + def -(other); to_f - other.to_f; end + def +(other); self.class.new(to_f + other.to_f); end + end +end diff --git a/spec/ruby/core/random/new_seed_spec.rb b/spec/ruby/core/random/new_seed_spec.rb index 4b34e871a2..7a93da99aa 100644 --- a/spec/ruby/core/random/new_seed_spec.rb +++ b/spec/ruby/core/random/new_seed_spec.rb @@ -1,8 +1,8 @@ require_relative '../../spec_helper' describe "Random.new_seed" do - it "returns a Bignum" do - Random.new_seed.should be_an_instance_of(Bignum) + it "returns an Integer" do + Random.new_seed.should be_an_instance_of(Integer) end it "returns an arbitrary seed value each time" do diff --git a/spec/ruby/core/random/new_spec.rb b/spec/ruby/core/random/new_spec.rb index 8160f44d79..69210cef03 100644 --- a/spec/ruby/core/random/new_spec.rb +++ b/spec/ruby/core/random/new_spec.rb @@ -1,16 +1,17 @@ +require_relative "../../spec_helper" describe "Random.new" do it "returns a new instance of Random" do Random.new.should be_an_instance_of(Random) end it "uses a random seed value if none is supplied" do - Random.new.seed.should be_an_instance_of(Bignum) + Random.new.seed.should be_an_instance_of(Integer) end it "returns Random instances initialized with different seeds" do first = Random.new second = Random.new - (0..20).map { first.rand } .should_not == (0..20).map { second.rand } + (0..20).map { first.rand }.should_not == (0..20).map { second.rand } end it "accepts an Integer seed value as an argument" do @@ -30,7 +31,7 @@ describe "Random.new" do end it "raises a RangeError if passed a Complex (with imaginary part) seed value as an argument" do - lambda do + -> do Random.new(Complex(20,2)) end.should raise_error(RangeError) end diff --git a/spec/ruby/core/random/rand_spec.rb b/spec/ruby/core/random/rand_spec.rb index 395e89dc75..9244177ab5 100644 --- a/spec/ruby/core/random/rand_spec.rb +++ b/spec/ruby/core/random/rand_spec.rb @@ -1,9 +1,10 @@ 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 @@ -54,7 +51,7 @@ 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 be_an_instance_of(Integer) end it "returns a Fixnum greater than or equal to 0" do @@ -83,13 +80,13 @@ 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 it "raises an ArgumentError when the argument is negative" do - lambda do + -> do Random.new.rand(-12) end.should raise_error(ArgumentError) end @@ -98,7 +95,7 @@ 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 be_an_instance_of(Integer) end it "returns a Bignum greater than or equal to 0" do @@ -122,7 +119,7 @@ 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 @@ -154,7 +151,7 @@ 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 @@ -162,7 +159,13 @@ 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 be_an_instance_of(Integer) + end + + it "supports custom object types" do + rand(RandomSpecs::CustomRangeInteger.new(1)..RandomSpecs::CustomRangeInteger.new(42)).should be_an_instance_of(RandomSpecs::CustomRangeInteger) + rand(RandomSpecs::CustomRangeFloat.new(1.0)..RandomSpecs::CustomRangeFloat.new(42.0)).should be_an_instance_of(RandomSpecs::CustomRangeFloat) + rand(Time.now..Time.now).should be_an_instance_of(Time) end it "returns an object that is a member of the Range" do @@ -202,14 +205,19 @@ describe "Random#rand with Range" do Random.new(42).rand(0..1.0).should be_kind_of(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 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 diff --git a/spec/ruby/core/random/random_number_spec.rb b/spec/ruby/core/random/random_number_spec.rb new file mode 100644 index 0000000000..bad81aeff6 --- /dev/null +++ b/spec/ruby/core/random/random_number_spec.rb @@ -0,0 +1,8 @@ +require_relative '../../spec_helper' +require_relative 'shared/rand' + +describe "Random.random_number" do + it_behaves_like :random_number, :random_number, Random.new + + it_behaves_like :random_number, :random_number, Random +end diff --git a/spec/ruby/core/random/raw_seed_spec.rb b/spec/ruby/core/random/raw_seed_spec.rb deleted file mode 100644 index c1a1eb1f42..0000000000 --- a/spec/ruby/core/random/raw_seed_spec.rb +++ /dev/null @@ -1,9 +0,0 @@ -# -*- encoding: binary -*- -require_relative '../../spec_helper' -require_relative 'shared/urandom' - -ruby_version_is "2.5" do - describe "Random.urandom" do - it_behaves_like :random_urandom, :urandom - end -end diff --git a/spec/ruby/core/random/shared/bytes.rb b/spec/ruby/core/random/shared/bytes.rb index fe8cd8d683..9afad3b7f8 100644 --- a/spec/ruby/core/random/shared/bytes.rb +++ b/spec/ruby/core/random/shared/bytes.rb @@ -1,17 +1,17 @@ describe :random_bytes, shared: true do it "returns a String" do - @object.bytes(1).should be_an_instance_of(String) + @object.send(@method, 1).should be_an_instance_of(String) end it "returns a String of the length given as argument" do - @object.bytes(15).length.should == 15 + @object.send(@method, 15).length.should == 15 end it "returns a binary String" do - @object.bytes(15).encoding.should == Encoding::BINARY + @object.send(@method, 15).encoding.should == Encoding::BINARY end it "returns a random binary String" do - @object.bytes(12).should_not == @object.bytes(12) + @object.send(@method, 12).should_not == @object.send(@method, 12) end end diff --git a/spec/ruby/core/random/shared/rand.rb b/spec/ruby/core/random/shared/rand.rb new file mode 100644 index 0000000000..d3b24b8851 --- /dev/null +++ b/spec/ruby/core/random/shared/rand.rb @@ -0,0 +1,9 @@ +describe :random_number, shared: true do + it "returns a Float if no max argument is passed" do + @object.send(@method).should be_kind_of(Float) + end + + it "returns an Integer if an Integer argument is passed" do + @object.send(@method, 20).should be_kind_of(Integer) + end +end diff --git a/spec/ruby/core/random/shared/urandom.rb b/spec/ruby/core/random/shared/urandom.rb deleted file mode 100644 index 316167b63f..0000000000 --- a/spec/ruby/core/random/shared/urandom.rb +++ /dev/null @@ -1,23 +0,0 @@ -describe :random_urandom, shared: true do - it "returns a String" do - Random.send(@method, 1).should be_an_instance_of(String) - end - - it "returns a String of the length given as argument" do - Random.send(@method, 15).length.should == 15 - end - - it "raises an ArgumentError on a negative size" do - lambda { - Random.send(@method, -1) - }.should raise_error(ArgumentError) - end - - it "returns a binary String" do - Random.send(@method, 15).encoding.should == Encoding::BINARY - end - - it "returns a random binary String" do - Random.send(@method, 12).should_not == Random.send(@method, 12) - end -end diff --git a/spec/ruby/core/random/urandom_spec.rb b/spec/ruby/core/random/urandom_spec.rb index e27f83cdcd..6f180e54ac 100644 --- a/spec/ruby/core/random/urandom_spec.rb +++ b/spec/ruby/core/random/urandom_spec.rb @@ -1,9 +1,25 @@ -# -*- encoding: binary -*- require_relative '../../spec_helper' -require_relative 'shared/urandom' -ruby_version_is ""..."2.5" do - describe "Random.raw_seed" do - it_behaves_like :random_urandom, :raw_seed +describe "Random.urandom" do + it "returns a String" do + Random.urandom(1).should be_an_instance_of(String) + end + + it "returns a String of the length given as argument" do + Random.urandom(15).length.should == 15 + end + + it "raises an ArgumentError on a negative size" do + -> { + Random.urandom(-1) + }.should raise_error(ArgumentError) + end + + it "returns a binary String" do + Random.urandom(15).encoding.should == Encoding::BINARY + end + + it "returns a random binary String" do + Random.urandom(12).should_not == Random.urandom(12) end end |
