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 | 35 | ||||
| -rw-r--r-- | spec/ruby/core/random/new_seed_spec.rb | 2 | ||||
| -rw-r--r-- | spec/ruby/core/random/new_spec.rb | 9 | ||||
| -rw-r--r-- | spec/ruby/core/random/rand_spec.rb | 39 | ||||
| -rw-r--r-- | spec/ruby/core/random/random_number_spec.rb | 4 | ||||
| -rw-r--r-- | spec/ruby/core/random/raw_seed_spec.rb | 6 | ||||
| -rw-r--r-- | spec/ruby/core/random/seed_spec.rb | 2 | ||||
| -rw-r--r-- | spec/ruby/core/random/shared/bytes.rb | 2 | ||||
| -rw-r--r-- | spec/ruby/core/random/shared/rand.rb | 4 | ||||
| -rw-r--r-- | spec/ruby/core/random/shared/urandom.rb | 23 | ||||
| -rw-r--r-- | spec/ruby/core/random/urandom_spec.rb | 25 |
12 files changed, 64 insertions, 98 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 a709eddd53..9e4845986d 100644 --- a/spec/ruby/core/random/default_spec.rb +++ b/spec/ruby/core/random/default_spec.rb @@ -1,38 +1,7 @@ require_relative '../../spec_helper' describe "Random::DEFAULT" do - - it "returns a random number generator" do - suppress_warning do - Random::DEFAULT.should respond_to(:rand) - end - end - - ruby_version_is ''...'3.0' do - it "returns a Random instance" do - suppress_warning do - Random::DEFAULT.should be_an_instance_of(Random) - end - end - end - - ruby_version_is '3.0' do - it "refers to the Random class" do - suppress_warning do - Random::DEFAULT.should.equal?(Random) - end - end - - it "is deprecated" do - -> { - Random::DEFAULT.should.equal?(Random) - }.should complain(/constant Random::DEFAULT is deprecated/) - end - end - - it "changes seed on reboot" do - seed1 = ruby_exe('p Random::DEFAULT.seed', options: '--disable-gems') - seed2 = ruby_exe('p Random::DEFAULT.seed', options: '--disable-gems') - seed1.should != seed2 + it "is no longer defined" do + Random.should_not.const_defined?(:DEFAULT) end end diff --git a/spec/ruby/core/random/new_seed_spec.rb b/spec/ruby/core/random/new_seed_spec.rb index 7a93da99aa..b2741aaa31 100644 --- a/spec/ruby/core/random/new_seed_spec.rb +++ b/spec/ruby/core/random/new_seed_spec.rb @@ -2,7 +2,7 @@ require_relative '../../spec_helper' describe "Random.new_seed" do it "returns an Integer" do - Random.new_seed.should be_an_instance_of(Integer) + Random.new_seed.should.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 4280b5b9c3..e20d487137 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) + Random.new.should.instance_of?(Random) end it "uses a random seed value if none is supplied" do - Random.new.seed.should be_an_instance_of(Integer) + Random.new.seed.should.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 @@ -32,6 +33,6 @@ describe "Random.new" do it "raises a RangeError if passed a Complex (with imaginary part) seed value as an argument" do -> do Random.new(Complex(20,2)) - end.should raise_error(RangeError) + end.should.raise(RangeError) end end diff --git a/spec/ruby/core/random/rand_spec.rb b/spec/ruby/core/random/rand_spec.rb index 6ea7eece5f..c882db6381 100644 --- a/spec/ruby/core/random/rand_spec.rb +++ b/spec/ruby/core/random/rand_spec.rb @@ -45,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(Integer) + Random.new.rand(20).should.instance_of?(Integer) end it "returns a Fixnum greater than or equal to 0" do @@ -82,20 +82,20 @@ describe "Random#rand with Fixnum" do it "raises an ArgumentError when the argument is 0" 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 -> 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(Integer) + 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 @@ -121,13 +121,13 @@ describe "Random#rand with Bignum" do it "raises an ArgumentError when the argument is negative" 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 @@ -153,25 +153,25 @@ describe "Random#rand with Float" do it "raises an ArgumentError when the argument is negative" 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(Integer) + 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 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) + 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 @@ -201,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 -> 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 -> do Random.new.rand(68..Object.new) - end.should raise_error(ArgumentError) + end.should.raise(ArgumentError) end end diff --git a/spec/ruby/core/random/random_number_spec.rb b/spec/ruby/core/random/random_number_spec.rb index 60a80ae1b9..bad81aeff6 100644 --- a/spec/ruby/core/random/random_number_spec.rb +++ b/spec/ruby/core/random/random_number_spec.rb @@ -4,7 +4,5 @@ require_relative 'shared/rand' describe "Random.random_number" do it_behaves_like :random_number, :random_number, Random.new - ruby_version_is "2.6" do - it_behaves_like :random_number, :random_number, Random - end + 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 0e40ed0796..0000000000 --- a/spec/ruby/core/random/raw_seed_spec.rb +++ /dev/null @@ -1,6 +0,0 @@ -require_relative '../../spec_helper' -require_relative 'shared/urandom' - -describe "Random.urandom" do - it_behaves_like :random_urandom, :urandom -end diff --git a/spec/ruby/core/random/seed_spec.rb b/spec/ruby/core/random/seed_spec.rb index bf4524fdd9..6529b2c873 100644 --- a/spec/ruby/core/random/seed_spec.rb +++ b/spec/ruby/core/random/seed_spec.rb @@ -2,7 +2,7 @@ require_relative '../../spec_helper' describe "Random#seed" do it "returns an Integer" do - Random.new.seed.should be_kind_of(Integer) + Random.new.seed.should.is_a?(Integer) end it "returns an arbitrary seed if the constructor was called without arguments" do diff --git a/spec/ruby/core/random/shared/bytes.rb b/spec/ruby/core/random/shared/bytes.rb index 9afad3b7f8..86c64d5696 100644 --- a/spec/ruby/core/random/shared/bytes.rb +++ b/spec/ruby/core/random/shared/bytes.rb @@ -1,6 +1,6 @@ describe :random_bytes, shared: true do it "returns a String" do - @object.send(@method, 1).should be_an_instance_of(String) + @object.send(@method, 1).should.instance_of?(String) end it "returns a String of the length given as argument" do diff --git a/spec/ruby/core/random/shared/rand.rb b/spec/ruby/core/random/shared/rand.rb index d3b24b8851..655c75c9f1 100644 --- a/spec/ruby/core/random/shared/rand.rb +++ b/spec/ruby/core/random/shared/rand.rb @@ -1,9 +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) + @object.send(@method).should.is_a?(Float) end it "returns an Integer if an Integer argument is passed" do - @object.send(@method, 20).should be_kind_of(Integer) + @object.send(@method, 20).should.is_a?(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 159716075c..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 - -> { - 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 new file mode 100644 index 0000000000..94e9423a06 --- /dev/null +++ b/spec/ruby/core/random/urandom_spec.rb @@ -0,0 +1,25 @@ +require_relative '../../spec_helper' + +describe "Random.urandom" do + it "returns a String" do + Random.urandom(1).should.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(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 |
