diff options
Diffstat (limited to 'spec/ruby/core/array/sample_spec.rb')
| -rw-r--r-- | spec/ruby/core/array/sample_spec.rb | 61 |
1 files changed, 39 insertions, 22 deletions
diff --git a/spec/ruby/core/array/sample_spec.rb b/spec/ruby/core/array/sample_spec.rb index 565d7ff7f9..fd443b47de 100644 --- a/spec/ruby/core/array/sample_spec.rb +++ b/spec/ruby/core/array/sample_spec.rb @@ -3,24 +3,34 @@ require_relative 'fixtures/classes' describe "Array#sample" do it "samples evenly" do - ary = [0, 1, 2, 3] - 3.times do |i| - counts = [0, 0, 0, 0] - 4000.times do - counts[ary.sample(3)[i]] += 1 - end - counts.each do |count| - (800..1200).should include(count) - end - end + ArraySpecs.measure_sample_fairness(4, 1, 400) + ArraySpecs.measure_sample_fairness(4, 2, 400) + ArraySpecs.measure_sample_fairness(4, 3, 400) + ArraySpecs.measure_sample_fairness(40, 3, 400) + ArraySpecs.measure_sample_fairness(40, 4, 400) + ArraySpecs.measure_sample_fairness(40, 8, 400) + ArraySpecs.measure_sample_fairness(40, 16, 400) + ArraySpecs.measure_sample_fairness_large_sample_size(100, 80, 4000) end it "returns nil for an empty Array" do - [].sample.should be_nil + [].sample.should == nil + end + + it "returns nil for an empty array when called without n and a Random is given" do + [].sample(random: Random.new(42)).should == nil end it "returns a single value when not passed a count" do - [4].sample.should equal(4) + [4].sample.should.equal?(4) + end + + it "returns a single value when not passed a count and a Random is given" do + [4].sample(random: Random.new(42)).should.equal?(4) + end + + it "returns a single value when not passed a count and a Random class is given" do + [4].sample(random: Random).should.equal?(4) end it "returns an empty Array when passed zero" do @@ -28,12 +38,12 @@ describe "Array#sample" do end it "returns an Array of elements when passed a count" do - [1, 2, 3, 4].sample(3).should be_an_instance_of(Array) + [1, 2, 3, 4].sample(3).should.instance_of?(Array) end it "returns elements from the Array" do array = [1, 2, 3, 4] - array.sample(3).all? { |x| array.should include(x) } + array.sample(3).all? { |x| array.should.include?(x) } end it "returns at most the number of elements in the Array" do @@ -57,11 +67,11 @@ describe "Array#sample" do end it "raises ArgumentError when passed a negative count" do - -> { [1, 2].sample(-1) }.should raise_error(ArgumentError) + -> { [1, 2].sample(-1) }.should.raise(ArgumentError) end it "does not return subclass instances with Array subclass" do - ArraySpecs::MyArray[1, 2, 3].sample(2).should be_an_instance_of(Array) + ArraySpecs::MyArray[1, 2, 3].sample(2).should.instance_of?(Array) end describe "with options" do @@ -69,13 +79,13 @@ describe "Array#sample" do obj = mock("array_sample_random") obj.should_receive(:rand).and_return(0.5) - [1, 2].sample(random: obj).should be_an_instance_of(Integer) + [1, 2].sample(random: obj).should.instance_of?(Integer) end it "raises a NoMethodError if an object passed for the RNG does not define #rand" do obj = BasicObject.new - -> { [1, 2].sample(random: obj) }.should raise_error(NoMethodError) + -> { [1, 2].sample(random: obj) }.should.raise(NoMethodError) end describe "when the object returned by #rand is an Integer" do @@ -95,14 +105,21 @@ describe "Array#sample" do random = mock("array_sample_random") random.should_receive(:rand).and_return(-1) - -> { [1, 2].sample(random: random) }.should raise_error(RangeError) + -> { [1, 2].sample(random: random) }.should.raise(RangeError) end it "raises a RangeError if the value is equal to the Array size" do random = mock("array_sample_random") random.should_receive(:rand).and_return(2) - -> { [1, 2].sample(random: random) }.should raise_error(RangeError) + -> { [1, 2].sample(random: random) }.should.raise(RangeError) + end + + it "raises a RangeError if the value is greater than the Array size" do + random = mock("array_sample_random") + random.should_receive(:rand).and_return(3) + + -> { [1, 2].sample(random: random) }.should.raise(RangeError) end end end @@ -123,7 +140,7 @@ describe "Array#sample" do random = mock("array_sample_random") random.should_receive(:rand).and_return(value) - -> { [1, 2].sample(random: random) }.should raise_error(RangeError) + -> { [1, 2].sample(random: random) }.should.raise(RangeError) end it "raises a RangeError if the value is equal to the Array size" do @@ -132,7 +149,7 @@ describe "Array#sample" do random = mock("array_sample_random") random.should_receive(:rand).and_return(value) - -> { [1, 2].sample(random: random) }.should raise_error(RangeError) + -> { [1, 2].sample(random: random) }.should.raise(RangeError) end end end |
