2018-03-04 10:09:32 -05:00
|
|
|
require_relative '../../spec_helper'
|
|
|
|
require_relative 'fixtures/classes'
|
2017-05-07 08:04:49 -04:00
|
|
|
|
2021-12-16 01:55:37 -05:00
|
|
|
describe "Kernel#srand" do
|
2021-12-16 02:01:06 -05:00
|
|
|
before :each do
|
|
|
|
@seed = srand
|
|
|
|
end
|
|
|
|
|
|
|
|
after :each do
|
|
|
|
srand(@seed)
|
|
|
|
end
|
|
|
|
|
2017-05-07 08:04:49 -04:00
|
|
|
it "is a private method" do
|
|
|
|
Kernel.should have_private_instance_method(:srand)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "returns the previous seed value" do
|
|
|
|
srand(10)
|
|
|
|
srand(20).should == 10
|
|
|
|
end
|
|
|
|
|
2021-12-16 01:53:42 -05:00
|
|
|
it "returns the system-initialized seed value on the first call" do
|
2021-12-16 01:07:23 -05:00
|
|
|
ruby_exe('print srand(10)', options: '--disable-gems').should =~ /\A\d+\z/
|
2020-06-27 09:51:37 -04:00
|
|
|
end
|
|
|
|
|
2017-05-07 08:04:49 -04:00
|
|
|
it "seeds the RNG correctly and repeatably" do
|
|
|
|
srand(10)
|
|
|
|
x = rand
|
|
|
|
srand(10)
|
|
|
|
rand.should == x
|
|
|
|
end
|
|
|
|
|
|
|
|
it "defaults number to a random value" do
|
2019-07-27 06:40:09 -04:00
|
|
|
-> { srand }.should_not raise_error
|
2017-05-07 08:04:49 -04:00
|
|
|
srand.should_not == 0
|
|
|
|
end
|
|
|
|
|
|
|
|
it "accepts and uses a seed of 0" do
|
|
|
|
srand(0)
|
|
|
|
srand.should == 0
|
|
|
|
end
|
|
|
|
|
|
|
|
it "accepts a negative seed" do
|
|
|
|
srand(-17)
|
|
|
|
srand.should == -17
|
|
|
|
end
|
|
|
|
|
2020-12-20 11:16:26 -05:00
|
|
|
it "accepts an Integer as a seed" do
|
2017-05-07 08:04:49 -04:00
|
|
|
srand(0x12345678901234567890)
|
|
|
|
srand.should == 0x12345678901234567890
|
|
|
|
end
|
|
|
|
|
|
|
|
it "calls #to_int on seed" do
|
|
|
|
srand(3.8)
|
|
|
|
srand.should == 3
|
|
|
|
|
|
|
|
s = mock('seed')
|
|
|
|
s.should_receive(:to_int).and_return 0
|
|
|
|
srand(s)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "raises a TypeError when passed nil" do
|
2019-07-27 06:40:09 -04:00
|
|
|
-> { srand(nil) }.should raise_error(TypeError)
|
2017-05-07 08:04:49 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "raises a TypeError when passed a String" do
|
2019-07-27 06:40:09 -04:00
|
|
|
-> { srand("7") }.should raise_error(TypeError)
|
2017-05-07 08:04:49 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-12-16 01:55:37 -05:00
|
|
|
describe "Kernel.srand" do
|
2017-05-07 08:04:49 -04:00
|
|
|
it "needs to be reviewed for spec completeness"
|
|
|
|
end
|