Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

67 lines (56 sloc) 1.815 kb
require File.dirname(__FILE__) + '/../../spec_helper'
require File.dirname(__FILE__) + '/fixtures/classes'
describe "Array#sample" do
ruby_version_is "" ... "1.9" do
it "is not defined" do
lambda { [].sample }.should raise_error(NoMethodError)
end
end
ruby_version_is "1.9" do
it "selects a random value from the array" do
a = [1, 2, 3, 4]
10.times {
a.include?(a.sample).should be_true
}
end
it "returns nil for empty arrays" do
[].sample.should be_nil
end
describe "passed a number n as an argument" do
it "raises ArgumentError for a negative n" do
lambda { [1, 2].sample(-1) }.should raise_error(ArgumentError)
end
it "returns different random values from the array" do
a = [1, 2, 3, 4]
sum = []
42.times {
pair = a.sample(2)
sum.concat(pair)
(pair - a).should == []
pair[0].should_not == pair[1]
}
a.should == [1, 2, 3, 4]
(a - sum).should == [] # Might fail once every 2^40 times ...
end
it "tries to convert n to an Integer using #to_int" do
a = [1, 2, 3, 4]
a.sample(2.3).size.should == 2
obj = mock('to_int')
obj.should_receive(:to_int).and_return(2)
a.sample(obj).size.should == 2
end
it "returns all values with n big enough" do
a = [1, 2, 3, 4]
a.sample(4).sort.should == a
a.sample(5).sort.should == a
end
it "returns [] for empty arrays or if n <= 0" do
[].sample(1).should == []
[1, 2, 3].sample(0).should == []
end
it "does not return subclass instances with Array subclass" do
ArraySpecs::MyArray[1, 2, 3].sample(2).class.should == Array
end
end
end
end
Jump to Line
Something went wrong with that request. Please try again.