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 | 11 | ||||
| -rw-r--r-- | spec/ruby/core/random/rand_spec.rb | 62 | ||||
| -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/seed_spec.rb | 2 | ||||
| -rw-r--r-- | spec/ruby/core/random/shared/bytes.rb | 10 | ||||
| -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 |
13 files changed, 108 insertions, 86 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..b2741aaa31 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.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..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(Bignum) + 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 @@ -30,8 +31,8 @@ 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.should.raise(RangeError) end end diff --git a/spec/ruby/core/random/rand_spec.rb b/spec/ruby/core/random/rand_spec.rb index 395e89dc75..c882db6381 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 @@ -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 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/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 3485d0f221..86c64d5696 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.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 an ASCII-8BIT String" do - @object.bytes(15).encoding.should == Encoding::ASCII_8BIT + it "returns a binary String" do + @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..655c75c9f1 --- /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.is_a?(Float) + end + + it "returns an Integer if an Integer argument is passed" do + @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 f50d30c9de..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 an ASCII-8BIT String" do - Random.send(@method, 15).encoding.should == Encoding::ASCII_8BIT - 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..94e9423a06 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.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 |
