-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathsample_spec.rb
More file actions
201 lines (159 loc) · 5.37 KB
/
sample_spec.rb
File metadata and controls
201 lines (159 loc) · 5.37 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
require 'spec_helper'
require 'enumerable/statistics'
RSpec.describe Enumerable, '#sample' do
let(:random) { Random.new }
let(:n) { 20 }
let(:replace) { nil }
let(:weights) { nil }
let(:opts) { {} }
before do
opts[:replace] = replace if replace
opts[:weights] = weights if weights
end
context 'when the receiver has 1 item' do
let(:enum) { 1.upto(1) }
shared_examples_for '1-item enumerable' do
context 'without replacement' do
specify { expect(opts).not_to include(:replace) }
specify do
expect(enum.sample(**opts)).to eq(1)
expect(enum.sample(10, **opts)).to eq([1])
expect(enum.sample(20, **opts)).to eq([1])
end
end
context 'with replacement' do
let(:replace) { true }
specify { expect(opts).to include(replace: true) }
specify do
expect(enum.sample(10, **opts)).to eq(Array.new(10, 1))
expect(enum.sample(20, **opts)).to eq(Array.new(20, 1))
end
end
end
context 'without weights' do
specify { expect(opts).not_to include(:weights) }
include_examples '1-item enumerable'
end
# TODO: weights
xcontext 'with weights' do
let(:weights) do
{ 1 => 1.0 }
end
specify { expect(opts).to include(weights: weights) }
include_examples '1-item enumerable'
end
end
context 'when the receiver has 2 item' do
let(:enum) { 1.upto(2) }
shared_examples_for 'sample from 2-item enumerable without replacement' do
specify { expect(opts).not_to include(:replace) }
specify do
expect(Array.new(100) { enum.sample(**opts) }).to all(eq(1).or eq(2))
expect(enum.sample(10, **opts)).to contain_exactly(1, 2)
expect(enum.sample(20, **opts)).to contain_exactly(1, 2)
end
end
context 'without weights' do
context 'without replacement' do
it_behaves_like 'sample from 2-item enumerable without replacement'
end
context 'with replacement' do
let(:replace) { true }
specify { expect(opts).to include(replace: true) }
specify do
expect(enum.sample(10, **opts)).to have_attributes(length: 10).and all(eq(1).or eq(2))
expect(enum.sample(20, **opts)).to have_attributes(length: 20).and all(eq(1).or eq(2))
end
end
end
# TODO: weights
xcontext 'with weights' do
specify { expect(opts).to include(weights: weights) }
context 'without replacement' do
it_behaves_like 'sample from 2-item enumerable without replacement'
end
context 'with replacement' do
let(:replace) { true }
specify { expect(opts).to include(replace: true) }
end
end
end
context 'without weight' do
let(:enum) { 1.upto(100000) }
specify { expect(opts).not_to include(:weights) }
context 'without replacement' do
specify { expect(opts).not_to include(:replace) }
context 'without size' do
context 'without rng' do
specify do
result = enum.sample
expect(result).to be_an(Integer)
other_results = Array.new(100) { enum.sample }
expect(other_results).not_to be_all {|i| i == result }
end
end
context 'with rng' do
specify do
save_random = random.dup
result = enum.sample(random: random)
expect(result).to be_an(Integer)
other_results = Array.new(100) { enum.sample(random: save_random.dup) }
expect(other_results).to be_all {|i| i == result }
end
end
end
context 'with size (== 1)' do
context 'without rng' do
specify do
result = enum.sample(1)
expect(result).to be_an(Integer)
other_results = Array.new(100) { enum.sample(1) }
expect(other_results).not_to be_all {|i| i == result }
end
end
context 'with rng' do
specify do
save_random = random.dup
result = enum.sample(1, random: random)
expect(result).to be_an(Integer)
other_results = Array.new(100) { enum.sample(1, random: save_random.dup) }
expect(other_results).to be_all {|i| i == result }
end
end
end
context 'with size (> 1)' do
context 'without rng' do
subject(:result) { enum.sample(n) }
specify do
result = enum.sample(n)
expect(result).to be_an(Array)
expect(result.length).to eq(n)
expect(result.uniq.length).to eq(n)
other_results = Array.new(100) { enum.sample(n) }
expect(other_results).not_to be_all {|i| i == result }
end
end
context 'with rng' do
subject(:result) { enum.sample(n, random: random) }
specify do
save_random = random.dup
result = enum.sample(n, random: random)
expect(result).to be_an(Array)
expect(result.length).to eq(n)
expect(result.uniq.length).to eq(n)
other_results = Array.new(100) { enum.sample(n, random: save_random.dup) }
expect(other_results).to be_all {|i| i == result }
end
end
end
end
context 'with replacement' do
let(:replace) { true }
specify { expect(opts).to include(replace: true) }
pending
end
end
context 'with weight' do
pending
end
end