Skip to content

Commit e095eea

Browse files
committed
Add examples
1 parent d2c8304 commit e095eea

File tree

1 file changed

+103
-2
lines changed

1 file changed

+103
-2
lines changed

spec/enum/sample_spec.rb

Lines changed: 103 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,107 @@
55
let(:random) { Random.new }
66
let(:n) { 20 }
77

8+
let(:replace) { nil }
9+
let(:weights) { nil }
10+
let(:opts) { {} }
11+
12+
before do
13+
opts[:replace] = replace if replace
14+
opts[:weights] = weights if weights
15+
end
16+
17+
context 'when the receiver has 1 item' do
18+
let(:enum) { 1.upto(1) }
19+
20+
shared_examples_for '1-item enumerable' do
21+
context 'without replacement' do
22+
specify { expect(opts).not_to include(:replace) }
23+
24+
specify do
25+
expect(enum.sample(**opts)).to eq(1)
26+
27+
expect(enum.sample(10, **opts)).to eq([1])
28+
expect(enum.sample(20, **opts)).to eq([1])
29+
end
30+
end
31+
32+
context 'with replacement' do
33+
let(:replace) { true }
34+
35+
specify { expect(opts).to include(replace: true) }
36+
37+
specify do
38+
expect(enum.sample(10, **opts)).to eq(Array.new(10, 1))
39+
expect(enum.sample(20, **opts)).to eq(Array.new(20, 1))
40+
end
41+
end
42+
end
43+
44+
context 'without weights' do
45+
specify { expect(opts).not_to include(:weights) }
46+
47+
include_examples '1-item enumerable'
48+
end
49+
50+
# TODO: weights
51+
xcontext 'with weights' do
52+
let(:weights) do
53+
{ 1 => 1.0 }
54+
end
55+
56+
specify { expect(opts).to include(weights: weights) }
57+
58+
include_examples '1-item enumerable'
59+
end
60+
end
61+
62+
context 'when the receiver has 2 item' do
63+
let(:enum) { 1.upto(2) }
64+
65+
shared_examples_for 'sample from 2-item enumerable without replacement' do
66+
specify { expect(opts).not_to include(:replace) }
67+
68+
specify do
69+
expect(Array.new(100) { enum.sample(**opts) }).to all(eq(1).or eq(2))
70+
71+
expect(enum.sample(10, **opts)).to contain_exactly(1, 2)
72+
expect(enum.sample(20, **opts)).to contain_exactly(1, 2)
73+
end
74+
end
75+
76+
context 'without weights' do
77+
context 'without replacement' do
78+
it_behaves_like 'sample from 2-item enumerable without replacement'
79+
end
80+
81+
context 'with replacement' do
82+
let(:replace) { true }
83+
84+
specify { expect(opts).to include(replace: true) }
85+
86+
specify do
87+
expect(enum.sample(10, **opts)).to have_attributes(length: 10).and all(eq(1).or eq(2))
88+
expect(enum.sample(20, **opts)).to have_attributes(length: 20).and all(eq(1).or eq(2))
89+
end
90+
end
91+
end
92+
93+
# TODO: weights
94+
xcontext 'with weights' do
95+
specify { expect(opts).to include(weights: weights) }
96+
97+
context 'without replacement' do
98+
it_behaves_like 'sample from 2-item enumerable without replacement'
99+
end
100+
101+
context 'with replacement' do
102+
let(:replace) { true }
103+
104+
specify { expect(opts).to include(replace: true) }
105+
end
106+
end
107+
end
108+
8109
context 'without weight' do
9110
let(:enum) { 1.upto(100000) }
10111

@@ -65,7 +166,7 @@
65166
expect(result.length).to eq(n)
66167
expect(result.uniq.length).to eq(n)
67168
other_results = Array.new(100) { enum.sample(n) }
68-
expect(other_results).not_to be_all {|i| i == result }
169+
expect(other_results).not_to be_all {|i| i == result }
69170
end
70171
end
71172

@@ -79,7 +180,7 @@
79180
expect(result.length).to eq(n)
80181
expect(result.uniq.length).to eq(n)
81182
other_results = Array.new(100) { enum.sample(n, random: save_random.dup) }
82-
expect(other_results).to be_all {|i| i == result }
183+
expect(other_results).to be_all {|i| i == result }
83184
end
84185
end
85186
end

0 commit comments

Comments
 (0)