-
Notifications
You must be signed in to change notification settings - Fork 1
/
size_spec.rb
296 lines (258 loc) · 8.68 KB
/
size_spec.rb
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
# encoding: utf-8
require File.expand_path('../../../spec_helper', __FILE__)
require File.expand_path('../fixtures/classes', __FILE__)
# Note: this describes the generic #size and #size=
# Specialized #size methods are found in subfolders, e.g. loop/size_spec.rb
ruby_version_is "1.9.3" do
describe "Enumerator#size" do
before :each do
@e = Enumerator.new{|y| y << :foo << :bar}
end
describe "when called without a block" do
it "returns nil when size was not specified" do
@e.size.should == nil
end
it "returns the specified size" do
@e.size = 42
@e.size.should == 42
end
it "returns the result of the given size block or method" do
values = [42, 1, 2, 3]
@e.size = Proc.new{ values.pop }
@e.size.should == 3
@e.size.should == 2
def values.shift(x)
super()
end
@e.size = values.method(:shift)
@e.size.should == 42
@e.size.should == 1
end
it "yields the receiver and the arguments to the given size block or method" do
values = [42, 1, 2, 3]
def values.foo(*args)
each{|x| yield x}
end
s = "hello"
@e = values.to_enum(:foo, s)
receiver = nil
arg = nil
@e.size = lambda{|r, a| receiver = r; arg = a; r.size }
@e.size.should == 4
receiver.should equal(values)
arg.should equal(s)
end
it "is lazy and doesn't iterate" do
lambda{
Enumerator.new{ raise "be lazy!" }.size
}.should_not raise_error
end
end
describe "when called with a block" do
it "returns the actual size when size was not specified" do
@e.size{}.should == 2
end
it "ignores the specified size" do
@e.size = 42
@e.size{}.should == 2
end
it "ignores the given size block or method" do
@e.size = Proc.new{ raise "ignore me" }
lambda{
@e.size{}
}.should_not raise_error
end
end
end
describe "Array#permutation.size" do
it "returns the number of permutations" do
[].permutation.size.should == 1
[1].permutation.size.should == 1
[1, 2].permutation(3).size.should == 0
(1..5).to_a.permutation(2).size.should == 20
(1..100).to_a.permutation.size.should == 93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000
end
end
describe "Array#combination.size" do
it "returns the number of combinations" do
[].combination(0).size.should == 1
[1].combination(0).size.should == 1
[1].combination(1).size.should == 1
[1, 2].combination(3).size.should == 0
(1..5).to_a.combination(2).size.should == 10
(1..5).to_a.combination(3).size.should == 10
(1..100).to_a.combination(40).size.should == 13746234145802811501267369720
end
end
describe "Array#repeated_permutation.size" do
it "returns the number of repeated permutations" do
[ ].repeated_permutation(0).size.should == 1
[ ].repeated_permutation(2).size.should == 0
[1].repeated_permutation(0).size.should == 1
[1].repeated_permutation(5).size.should == 1
[1].repeated_permutation(-1).size.should == 0
(1..100).to_a.repeated_permutation(40).size.should == 100**40
end
end
describe "Array#repeated_combination.size" do
it "returns the number of repeated combinations" do
[ ].repeated_combination(0).size.should == 1
[ ].repeated_combination(2).size.should == 0
[1].repeated_combination(0).size.should == 1
[1].repeated_combination(5).size.should == 1
[1].repeated_combination(-1).size.should == 0
(1..5).to_a.repeated_combination(3).size.should == 35
(1..100).to_a.repeated_combination(40).size.should == 126279609474586092455213621207360700
end
end
describe "Array#cycle.size" do
it "returns the size" do
[ ].cycle.size.should == 0
[1].cycle.size.should == Float::INFINITY
[1,2].cycle(3).size.should == 6
end
end
describe "Kernel#loop.size" do
it "returns Infinity" do
loop.size.should == Float::INFINITY
end
end
describe "Enumerator#size when created from an enumerable" do
before :each do
@methods = [:each_with_index, :each_entry, :reverse_each,
:find_all, :reject, :map, :flat_map, :partition, :group_by, :sort_by,
:min_by, :max_by, :minmax_by ]
end
it "returns nil if the Enumerable has no size method" do
e = EnumeratorSpecs::Numerous.new
@methods.each do |method|
e.send(method).size.should == nil
end
e.each_with_object(:foo).size.should == nil
end
it "uses the size method if there is one" do
e = EnumeratorSpecs::SizedNumerous.new
@methods.each do |method|
e.send(method).size.should == 6
end
e.each_with_object(:foo).size.should == 6
end
end
describe "Enumerable#each_slize.size" do
it "returns the right size" do
e = EnumeratorSpecs::SizedNumerous.new(*1..42)
e.each_slice(3).size.should == 14
e.each_slice(4).size.should == 11
end
end
describe "Enumerable#each_cons.size" do
it "returns the right size" do
e = EnumeratorSpecs::SizedNumerous.new(*1..42)
e.each_cons(3).size.should == 40
e.each_cons(50).size.should == 0
end
end
describe "Enumerable#cycle.size" do
it "returns the right size" do
e = EnumeratorSpecs::SizedNumerous.new
e.cycle(3).size.should == 18
e.cycle(0).size.should == 0
e.cycle(-1).size.should == 0
e.cycle.size.should == Float::INFINITY
end
end
describe "Enumerator#size when created from a Hash" do
it "returns the hash's size" do
h = {"hello" => "world", :foo => :bar, 1 => 2}
[ :each, :each_value, :each_key, :each_pair,
:keep_if, :delete_if, :reject!, :select, :select!
].each do |method|
h.send(method).size.should == 3
end
end
end
describe "Enumerator#size when created from ENV" do
it "returns the number of environment variables" do
s = ENV.to_a.size
[ :each, :each_value, :each_key, :each_pair,
:keep_if, :delete_if, :reject!, :select, :select!
].each do |method|
ENV.send(method).size.should == s
end
end
end
describe "Enumerator#size when created from a Struct" do
it "returns the number of fields" do
klass = Struct.new(:foo, :bar, :baz)
s = klass.new
[ :each, :each_pair, :select
].each do |method|
s.send(method).size.should == 3
end
end
end
describe "Numeric#step.size" do
it "returns the right size for fixnums" do
42.step(99, 3).size.should == 20
42.step(100, 3).size.should == 20
42.step(20, 3).size.should == 0
42.step(20, -3).size.should == 8
10.step(20).size.should == 11
end
it "returns the right size for floats" do
0.5.step(4.2, 0.3).size.should == 13
0.5.step(4.2, -0.3).size.should == 0
-0.5.step(-4.2, -0.3).size.should == 13
end
it "returns the right size for bignums" do
(2 ** 100).step(2 ** 100 + 42, 2).size.should == 22
(2 ** 100).step(2 ** 100 - 41, 2).size.should == 0
(2 ** 100).step(2 ** 100 - 41, -2).size.should == 21
end
end
describe "Numeric#upto.size" do
it "returns the right size for fixnums" do
42.upto(99).size.should == 58
42.upto(20).size.should == 0
end
it "returns the right size for bignum arguments" do
(2 ** 100).upto(2 ** 100 + 42).size.should == 43
(2 ** 100).upto(2 ** 100 - 42).size.should == 0
end
end
describe "Numeric#downto.size" do
it "returns the right size for fixnums" do
42.downto(99).size.should == 0
42.downto(20).size.should == 23
end
it "returns the right size for bignum arguments" do
(2 ** 100).downto(2 ** 100 + 42).size.should == 0
(2 ** 100).downto(2 ** 100 - 42).size.should == 43
end
end
describe "Numeric#times.size" do
it "returns the right size" do
5.times.size.should == 5
(2 ** 99).times.size.should == 2 ** 99
end
it "returns 0 for negative integers" do
(-5).times.size.should == 0
(-2 ** 99).times.size.should == 0
end
end
describe "String#each_byte.size" do
it "returns the size" do
"Môntréål".each_byte.size.should == 11
end
end
describe "String#each_char.size" do
it "returns the number of characters" do
"Môntréål".each_char.size.should == 8
end
end
describe "String#each_codepoint.size" do
it "returns the number of characters" do
"Môntréål".each_codepoint.size.should == 8
end
end
end