This repository was archived by the owner on Jul 10, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsample_spec.rb
More file actions
60 lines (51 loc) · 1.65 KB
/
sample_spec.rb
File metadata and controls
60 lines (51 loc) · 1.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
require File.expand_path('../../../spec_helper', __FILE__)
require File.expand_path('../fixtures/classes', __FILE__)
describe "Array#sample" do
ruby_version_is "1.8.8" 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).should be_kind_of(Array)
end
end
end
end