random-accessible 0.1.2 → 0.1.3
Sign up to get free protection for your applications and to get access to all the features.
- data/BSDL +9 -0
- data/lib/random-accessible.rb +4 -35
- data/lib/random-readable.rb +44 -54
- data/test/test-random-accessible.rb +495 -0
- data/test/test-random-readable.rb +1271 -0
- data/test/test-random-writable.rb +566 -0
- metadata +13 -21
- data/lib/#random-readable.rb# +0 -598
@@ -0,0 +1,1271 @@
|
|
1
|
+
require 'rational'
|
2
|
+
require 'test/unit'
|
3
|
+
require 'random-readable'
|
4
|
+
|
5
|
+
|
6
|
+
class TestReadAccessable < Test::Unit::TestCase
|
7
|
+
|
8
|
+
class ErrorForTest < Exception
|
9
|
+
end
|
10
|
+
|
11
|
+
class ReadAccess
|
12
|
+
|
13
|
+
include RandomReadable
|
14
|
+
|
15
|
+
def initialize(ary)
|
16
|
+
@a = ary
|
17
|
+
end
|
18
|
+
|
19
|
+
def read_access(pos)
|
20
|
+
raise ErrorForTest if pos < 0 || @a.size <= pos
|
21
|
+
@a[pos.to_int]
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
25
|
+
|
26
|
+
class ReadAccessAndSize
|
27
|
+
|
28
|
+
include RandomReadable
|
29
|
+
|
30
|
+
def initialize(ary)
|
31
|
+
@a = ary
|
32
|
+
end
|
33
|
+
|
34
|
+
def read_access(pos)
|
35
|
+
raise ErrorForTest if pos < 0 || @a.size <= pos
|
36
|
+
@a[pos.to_int]
|
37
|
+
end
|
38
|
+
|
39
|
+
def size
|
40
|
+
@a.size
|
41
|
+
end
|
42
|
+
|
43
|
+
end
|
44
|
+
|
45
|
+
class ReadAccessAndLength
|
46
|
+
|
47
|
+
include RandomReadable
|
48
|
+
|
49
|
+
def initialize(ary)
|
50
|
+
@a = ary
|
51
|
+
end
|
52
|
+
|
53
|
+
def read_access(pos)
|
54
|
+
raise ErrorForTest if pos < 0 || @a.size <= pos
|
55
|
+
@a[pos.to_int]
|
56
|
+
end
|
57
|
+
|
58
|
+
def length
|
59
|
+
@a.length
|
60
|
+
end
|
61
|
+
|
62
|
+
end
|
63
|
+
|
64
|
+
class HashWrapper
|
65
|
+
|
66
|
+
include RandomReadable
|
67
|
+
|
68
|
+
def initialize(ary = nil)
|
69
|
+
@h = Hash.new
|
70
|
+
if ary.nil?
|
71
|
+
@size = 0
|
72
|
+
else
|
73
|
+
ary.each_with_index do |el, i|
|
74
|
+
@h[i] = el
|
75
|
+
end
|
76
|
+
@size = ary.size
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
def read_access(pos)
|
81
|
+
raise if pos < 0 && @size <= pos
|
82
|
+
return @h[pos]
|
83
|
+
end
|
84
|
+
|
85
|
+
attr_reader :size
|
86
|
+
|
87
|
+
end
|
88
|
+
|
89
|
+
NOSIZE_IMPLS = [ReadAccess]
|
90
|
+
FULL_IMPLS = [Array, # To test test cases.
|
91
|
+
ReadAccessAndSize,
|
92
|
+
ReadAccessAndLength,
|
93
|
+
HashWrapper]
|
94
|
+
|
95
|
+
def test_size_not_implemented
|
96
|
+
NOSIZE_IMPLS.each do |klass|
|
97
|
+
impl = klass.new([])
|
98
|
+
assert_raise NotImplementedError do
|
99
|
+
impl.size
|
100
|
+
end
|
101
|
+
assert_raise NotImplementedError do
|
102
|
+
impl.length
|
103
|
+
end
|
104
|
+
end
|
105
|
+
end
|
106
|
+
|
107
|
+
def test_ampersand
|
108
|
+
FULL_IMPLS.each do |klass|
|
109
|
+
impl1 = klass.new([1, 1, 2, 3])
|
110
|
+
impl2 = klass.new([1, 3, 4])
|
111
|
+
|
112
|
+
assert_equal([1, 3], impl1 & impl2)
|
113
|
+
assert_equal([1, 3], impl1 & [1, 3, 4])
|
114
|
+
end
|
115
|
+
NOSIZE_IMPLS.each do |klass|
|
116
|
+
assert_raise NotImplementedError do
|
117
|
+
klass.new([]) & []
|
118
|
+
end
|
119
|
+
end
|
120
|
+
end
|
121
|
+
|
122
|
+
def test_asterisk_times
|
123
|
+
FULL_IMPLS.each do |klass|
|
124
|
+
impl = klass.new([1, 2, 3])
|
125
|
+
assert_equal([1, 2, 3] * 3, impl * 3)
|
126
|
+
end
|
127
|
+
NOSIZE_IMPLS.each do |klass|
|
128
|
+
assert_raise NotImplementedError do
|
129
|
+
klass.new([]) * []
|
130
|
+
end
|
131
|
+
end
|
132
|
+
end
|
133
|
+
|
134
|
+
def test_asterisk_separator
|
135
|
+
FULL_IMPLS.each do |klass|
|
136
|
+
impl = klass.new([1, 2, 3])
|
137
|
+
assert_equal([1, 2, 3] * 'foo', impl * 'foo')
|
138
|
+
end
|
139
|
+
NOSIZE_IMPLS.each do |klass|
|
140
|
+
assert_raise NotImplementedError do
|
141
|
+
klass.new([]) * 'bar'
|
142
|
+
end
|
143
|
+
end
|
144
|
+
end
|
145
|
+
|
146
|
+
def test_plus
|
147
|
+
FULL_IMPLS.each do |klass|
|
148
|
+
impl1 = klass.new([1, 2])
|
149
|
+
impl2 = klass.new([8, 9])
|
150
|
+
assert_equal([1, 2, 8, 9], impl1 + impl2)
|
151
|
+
assert_equal([1, 2], impl1)
|
152
|
+
assert_equal([8, 9], impl2)
|
153
|
+
end
|
154
|
+
NOSIZE_IMPLS.each do |klass|
|
155
|
+
impl = klass.new([])
|
156
|
+
assert_raise NotImplementedError do
|
157
|
+
impl + impl
|
158
|
+
end
|
159
|
+
end
|
160
|
+
end
|
161
|
+
|
162
|
+
def test_minus
|
163
|
+
FULL_IMPLS.each do |klass|
|
164
|
+
impl1 = klass.new([1, 2, 1, 3, 1, 4, 1, 5])
|
165
|
+
impl2 = klass.new([2, 3, 4, 5])
|
166
|
+
assert_equal([1, 1, 1, 1], impl1 - impl2)
|
167
|
+
end
|
168
|
+
NOSIZE_IMPLS.each do |klass|
|
169
|
+
impl = klass.new([])
|
170
|
+
assert_raise NotImplementedError do
|
171
|
+
impl - impl
|
172
|
+
end
|
173
|
+
end
|
174
|
+
end
|
175
|
+
|
176
|
+
def test_compare
|
177
|
+
FULL_IMPLS.each do |klass|
|
178
|
+
impl = klass.new([1, 2, 3])
|
179
|
+
assert_equal(-1, impl <=> [1, 3, 2])
|
180
|
+
assert_equal(1, impl <=> [1, 0, 3])
|
181
|
+
assert_equal(0, impl <=> [1, 2, 3])
|
182
|
+
assert_equal(1, [1, 3, 2] <=> impl)
|
183
|
+
assert_equal(-1, [1, 0, 3] <=> impl)
|
184
|
+
assert_equal(0, [1, 2, 3] <=> impl)
|
185
|
+
assert_equal(-1, impl <=> [1, 2, 3, 4])
|
186
|
+
assert_equal(1, [1, 2, 3, 4] <=> impl)
|
187
|
+
assert_equal(1, impl <=> [1, 2])
|
188
|
+
assert_equal(-1, [1, 2] <=> impl)
|
189
|
+
end
|
190
|
+
NOSIZE_IMPLS.each do |klass|
|
191
|
+
impl = klass.new([])
|
192
|
+
assert_raise NotImplementedError do
|
193
|
+
impl <=> []
|
194
|
+
end
|
195
|
+
assert_raise NotImplementedError do
|
196
|
+
[] <=> impl
|
197
|
+
end
|
198
|
+
end
|
199
|
+
end
|
200
|
+
|
201
|
+
def test_equal
|
202
|
+
FULL_IMPLS.each do |klass|
|
203
|
+
impl = klass.new([1, 2, 3])
|
204
|
+
assert(impl == [1, 2, 3])
|
205
|
+
assert(impl == [1.0, 2.0, 3.0])
|
206
|
+
assert(impl != [1, 2, 3, 4])
|
207
|
+
assert(impl != [1, 2])
|
208
|
+
assert(impl != [1, 2, 3.01])
|
209
|
+
assert(impl != [1.01, 2, 3])
|
210
|
+
end
|
211
|
+
NOSIZE_IMPLS.each do |klass|
|
212
|
+
impl1 = klass.new([1, 2])
|
213
|
+
impl2 = klass.new([1, 2])
|
214
|
+
assert(impl1 == impl1)
|
215
|
+
assert(impl1 != impl2)
|
216
|
+
assert(impl1 != [1, 2])
|
217
|
+
assert([1, 2] != impl1)
|
218
|
+
end
|
219
|
+
end
|
220
|
+
|
221
|
+
def test_bracket_position
|
222
|
+
FULL_IMPLS.each do |klass|
|
223
|
+
impl = klass.new((0...10).to_a)
|
224
|
+
assert_nil(impl[-11])
|
225
|
+
assert_equal(0, impl[-10])
|
226
|
+
assert_equal(9, impl[-1])
|
227
|
+
assert_equal(0, impl[0])
|
228
|
+
assert_equal(9, impl[9])
|
229
|
+
assert_nil(impl[10])
|
230
|
+
end
|
231
|
+
NOSIZE_IMPLS.each do |klass|
|
232
|
+
impl = klass.new((0...10).to_a)
|
233
|
+
[-11, -10, -1].each do |pos|
|
234
|
+
assert_raise NotImplementedError do
|
235
|
+
impl[pos]
|
236
|
+
end
|
237
|
+
end
|
238
|
+
|
239
|
+
assert_equal(0, impl[0])
|
240
|
+
assert_equal(9, impl[9])
|
241
|
+
|
242
|
+
assert_raise ErrorForTest do
|
243
|
+
impl[10]
|
244
|
+
end
|
245
|
+
end
|
246
|
+
end
|
247
|
+
|
248
|
+
def test_bracket_range
|
249
|
+
FULL_IMPLS.each do |klass|
|
250
|
+
impl = klass.new((0...10).to_a)
|
251
|
+
msg = "Error in #{klass.name}"
|
252
|
+
|
253
|
+
assert_equal(nil, impl[-12..-11], msg)
|
254
|
+
assert_equal(nil, impl[-14..-10], msg)
|
255
|
+
assert_equal([0, 1, 2], impl[-10..-8], msg)
|
256
|
+
assert_equal([8, 9], impl[-2..-1], msg)
|
257
|
+
assert_equal([1, 2, 3], impl[-9..3], msg)
|
258
|
+
assert_equal([0, 1], impl[0..1], msg)
|
259
|
+
assert_equal([8, 9], impl[8..9], msg)
|
260
|
+
assert_equal([8, 9], impl[8..100], msg)
|
261
|
+
assert_equal([], impl[10..15], msg)
|
262
|
+
assert_equal(nil, impl[11..15], msg)
|
263
|
+
assert_equal([], impl[-1..-2], msg)
|
264
|
+
assert_equal([], impl[3..0], msg)
|
265
|
+
assert_equal([], impl[-1..1], msg)
|
266
|
+
assert_equal(nil, impl[15..14], msg)
|
267
|
+
assert_equal(nil, impl[-11..-20], msg)
|
268
|
+
assert_equal(nil, impl[15..5], msg)
|
269
|
+
assert_equal([], impl[-10..-20], msg)
|
270
|
+
assert_equal([], impl[1..-20], msg)
|
271
|
+
|
272
|
+
assert_equal(nil, impl[-12...-10], msg)
|
273
|
+
assert_equal(nil, impl[-14...-9], msg)
|
274
|
+
assert_equal([0, 1, 2], impl[-10...-7], msg)
|
275
|
+
assert_equal([], impl[-2...0], msg)
|
276
|
+
assert_equal([1, 2, 3], impl[-9...4], msg)
|
277
|
+
assert_equal([0, 1], impl[0...2], msg)
|
278
|
+
assert_equal([8, 9], impl[8...10], msg)
|
279
|
+
assert_equal([8, 9], impl[8...100], msg)
|
280
|
+
assert_equal([], impl[10...15], msg)
|
281
|
+
assert_equal(nil, impl[11...15], msg)
|
282
|
+
assert_equal([], impl[-1...-1], msg)
|
283
|
+
assert_equal([], impl[0...0], msg)
|
284
|
+
assert_equal([3, 4, 5, 6, 7, 8], impl[3...-1], msg)
|
285
|
+
assert_equal([], impl[-1...2], msg)
|
286
|
+
assert_equal(nil, impl[16...15], msg)
|
287
|
+
assert_equal(nil, impl[-11...-20], msg)
|
288
|
+
assert_equal(nil, impl[15...5], msg)
|
289
|
+
assert_equal([], impl[-10...-20], msg)
|
290
|
+
assert_equal([], impl[1...-20], msg)
|
291
|
+
end
|
292
|
+
NOSIZE_IMPLS.each do |klass|
|
293
|
+
impl = klass.new((0...10).to_a)
|
294
|
+
[-12..-11, -14..-10, -10..-8, -2..-1, -1..1].each do |range|
|
295
|
+
assert_raise NotImplementedError do
|
296
|
+
impl[range]
|
297
|
+
end
|
298
|
+
end
|
299
|
+
|
300
|
+
assert_equal([0, 1], impl[0..1])
|
301
|
+
assert_equal([8, 9], impl[8..9])
|
302
|
+
assert_equal([], impl[15..10])
|
303
|
+
assert_equal([], impl[-1..-2])
|
304
|
+
|
305
|
+
[8..11, 10..15].each do |range|
|
306
|
+
assert_raise ErrorForTest do
|
307
|
+
impl[range]
|
308
|
+
end
|
309
|
+
end
|
310
|
+
end
|
311
|
+
end
|
312
|
+
|
313
|
+
def test_bracket_start_and_length
|
314
|
+
FULL_IMPLS.each do |klass|
|
315
|
+
impl = klass.new((0...10).to_a)
|
316
|
+
assert_equal(nil, impl[-12, 2])
|
317
|
+
assert_equal(nil, impl[-14, 5])
|
318
|
+
assert_equal([0, 1, 2], impl[-10, 3])
|
319
|
+
assert_equal([8, 9], impl[-2, 2])
|
320
|
+
assert_equal([1, 2, 3], impl[-9, 3])
|
321
|
+
assert_equal([0, 1], impl[0, 2])
|
322
|
+
assert_equal([5], impl[5, 1])
|
323
|
+
assert_equal([8, 9], impl[8, 2])
|
324
|
+
assert_equal([8, 9], impl[8, 100])
|
325
|
+
assert_equal([], impl[10, 5])
|
326
|
+
assert_equal(nil, impl[11, 15])
|
327
|
+
assert_equal(nil, impl[-1, -2], "Error in #{klass.name}")
|
328
|
+
assert_equal(nil, impl[3, -3])
|
329
|
+
assert_equal(nil, impl[15, -1])
|
330
|
+
assert_equal(nil, impl[-11, -20])
|
331
|
+
assert_equal(nil, impl[15, -10])
|
332
|
+
assert_equal(nil, impl[-10, -20])
|
333
|
+
end
|
334
|
+
NOSIZE_IMPLS.each do |klass|
|
335
|
+
impl = klass.new((0...10).to_a)
|
336
|
+
[[-12, 2], [-14, 5], [-10, 3], [-2, 2], [-1, 3]].each do |range|
|
337
|
+
assert_raise NotImplementedError do
|
338
|
+
impl[*range]
|
339
|
+
end
|
340
|
+
end
|
341
|
+
|
342
|
+
assert_equal([0, 1], impl[0, 2])
|
343
|
+
assert_equal([8, 9], impl[8, 2])
|
344
|
+
assert_equal([], impl[15, -6])
|
345
|
+
assert_equal([], impl[-1, -2])
|
346
|
+
|
347
|
+
[[8, 4], [10, 6]].each do |range|
|
348
|
+
assert_raise ErrorForTest do
|
349
|
+
impl[*range]
|
350
|
+
end
|
351
|
+
end
|
352
|
+
end
|
353
|
+
end
|
354
|
+
|
355
|
+
def test_assoc
|
356
|
+
FULL_IMPLS.each do |klass|
|
357
|
+
impl = klass.new([[1,15], [2,25], [3,35]])
|
358
|
+
assert_equal([1, 15], impl.assoc(1))
|
359
|
+
assert_equal([2, 25], impl.assoc(2))
|
360
|
+
assert_equal([3, 35], impl.assoc(3))
|
361
|
+
assert_nil(impl.assoc(100))
|
362
|
+
assert_nil(impl.assoc(15))
|
363
|
+
end
|
364
|
+
NOSIZE_IMPLS.each do |klass|
|
365
|
+
impl = klass.new([[1,15], [2,25], [3,35]])
|
366
|
+
assert_equal([1, 15], impl.assoc(1))
|
367
|
+
assert_equal([2, 25], impl.assoc(2))
|
368
|
+
assert_equal([3, 35], impl.assoc(3))
|
369
|
+
[100, 15].each do |n|
|
370
|
+
assert_raise ErrorForTest do
|
371
|
+
impl.assoc(n)
|
372
|
+
end
|
373
|
+
end
|
374
|
+
end
|
375
|
+
end
|
376
|
+
|
377
|
+
def test_at
|
378
|
+
FULL_IMPLS.each do |klass|
|
379
|
+
impl = klass.new((0...10).to_a)
|
380
|
+
assert_nil(impl.at(-11))
|
381
|
+
assert_equal(0, impl.at(-10))
|
382
|
+
assert_equal(9, impl.at(-1))
|
383
|
+
assert_equal(0, impl.at(0))
|
384
|
+
assert_equal(9, impl.at(9))
|
385
|
+
assert_nil(impl.at(10))
|
386
|
+
end
|
387
|
+
NOSIZE_IMPLS.each do |klass|
|
388
|
+
impl = klass.new((0...10).to_a)
|
389
|
+
[-11, -10, -1].each do |pos|
|
390
|
+
assert_raise NotImplementedError do
|
391
|
+
impl.at(pos)
|
392
|
+
end
|
393
|
+
end
|
394
|
+
|
395
|
+
assert_equal(0, impl.at(0))
|
396
|
+
assert_equal(9, impl.at(9))
|
397
|
+
|
398
|
+
assert_raise ErrorForTest do
|
399
|
+
impl.at(10)
|
400
|
+
end
|
401
|
+
end
|
402
|
+
end
|
403
|
+
|
404
|
+
def test_combination
|
405
|
+
FULL_IMPLS.each do |klass|
|
406
|
+
impl = klass.new([1, 2, 3, 4])
|
407
|
+
assert_equal([], impl.combination(-1).to_a)
|
408
|
+
assert_equal([[]], impl.combination(0).to_a)
|
409
|
+
assert_equal([[1],[2],[3],[4]], impl.combination(1).to_a)
|
410
|
+
assert_equal([[1,2],[1,3],[1,4],[2,3],[2,4],[3,4]],
|
411
|
+
impl.combination(2).to_a)
|
412
|
+
assert_equal([[1,2,3],[1,2,4],[1,3,4],[2,3,4]],
|
413
|
+
impl.combination(3).to_a)
|
414
|
+
assert_equal([[1,2,3,4]], impl.combination(4).to_a)
|
415
|
+
assert_equal([], impl.combination(5).to_a)
|
416
|
+
end
|
417
|
+
NOSIZE_IMPLS.each do |klass|
|
418
|
+
impl = klass.new([1, 2, 3, 4])
|
419
|
+
(-1..5).each do |i|
|
420
|
+
assert_raise NotImplementedError do
|
421
|
+
impl.combination(i)
|
422
|
+
end
|
423
|
+
end
|
424
|
+
end
|
425
|
+
end
|
426
|
+
|
427
|
+
def test_compact
|
428
|
+
FULL_IMPLS.each do |klass|
|
429
|
+
impl = klass.new([1, nil, 2, nil, 3, nil])
|
430
|
+
assert_equal([1, 2, 3], impl.compact)
|
431
|
+
end
|
432
|
+
NOSIZE_IMPLS.each do |klass|
|
433
|
+
impl = klass.new([1, nil, 2, nil, 3, nil])
|
434
|
+
assert_raise NotImplementedError do
|
435
|
+
impl.compact
|
436
|
+
end
|
437
|
+
end
|
438
|
+
end
|
439
|
+
|
440
|
+
def test_cycle
|
441
|
+
FULL_IMPLS.each do |klass|
|
442
|
+
impl = klass.new([0, 1, 2])
|
443
|
+
i = 0
|
444
|
+
impl.cycle do |el|
|
445
|
+
assert_equal(i % 3, el)
|
446
|
+
i += 1
|
447
|
+
break if i > 100
|
448
|
+
end
|
449
|
+
end
|
450
|
+
NOSIZE_IMPLS.each do |klass|
|
451
|
+
impl = klass.new([0, 1, 2])
|
452
|
+
i = 0
|
453
|
+
assert_raise ErrorForTest do
|
454
|
+
impl.cycle do |el|
|
455
|
+
if i < 3
|
456
|
+
assert_equal(i, el)
|
457
|
+
end
|
458
|
+
i += 1
|
459
|
+
end
|
460
|
+
end
|
461
|
+
end
|
462
|
+
end
|
463
|
+
|
464
|
+
def test_each
|
465
|
+
FULL_IMPLS.each do |klass|
|
466
|
+
impl = klass.new([1, 4, 9, 16])
|
467
|
+
i = 1
|
468
|
+
impl.each do |el|
|
469
|
+
assert_equal(i * i, el)
|
470
|
+
i += 1
|
471
|
+
end
|
472
|
+
assert_equal(4, i - 1)
|
473
|
+
assert_equal([1, 4, 9, 16], impl.each.to_a)
|
474
|
+
end
|
475
|
+
NOSIZE_IMPLS.each do |klass|
|
476
|
+
impl = klass.new([])
|
477
|
+
assert_raise NotImplementedError do
|
478
|
+
impl.each.next
|
479
|
+
end
|
480
|
+
assert_raise NotImplementedError do
|
481
|
+
impl.each do
|
482
|
+
assert_fail
|
483
|
+
end
|
484
|
+
end
|
485
|
+
end
|
486
|
+
end
|
487
|
+
|
488
|
+
def test_each_index
|
489
|
+
FULL_IMPLS.each do |klass|
|
490
|
+
impl = klass.new([1, 2, 3, 4, 5])
|
491
|
+
n = 0
|
492
|
+
impl.each_index do |i|
|
493
|
+
assert_equal(n, i)
|
494
|
+
n += 1
|
495
|
+
end
|
496
|
+
end
|
497
|
+
NOSIZE_IMPLS.each do |klass|
|
498
|
+
impl = klass.new([])
|
499
|
+
assert_raise NotImplementedError do
|
500
|
+
impl.each_index do
|
501
|
+
assert_fail
|
502
|
+
end
|
503
|
+
end
|
504
|
+
end
|
505
|
+
end
|
506
|
+
|
507
|
+
def test_empty
|
508
|
+
FULL_IMPLS.each do |klass|
|
509
|
+
impl = klass.new([])
|
510
|
+
assert_equal(true, impl.empty?)
|
511
|
+
|
512
|
+
impl = klass.new([1])
|
513
|
+
assert_equal(false, impl.empty?)
|
514
|
+
end
|
515
|
+
NOSIZE_IMPLS.each do |klass|
|
516
|
+
impl = klass.new([])
|
517
|
+
assert_equal(false, impl.empty?)
|
518
|
+
end
|
519
|
+
end
|
520
|
+
|
521
|
+
def test_eql?
|
522
|
+
FULL_IMPLS.each do |klass|
|
523
|
+
next if klass == Array
|
524
|
+
|
525
|
+
impl1 = klass.new(["a", "b", "c"])
|
526
|
+
impl2 = klass.new(["a", "b", "c"])
|
527
|
+
assert_equal(true, impl1.eql?(impl2))
|
528
|
+
|
529
|
+
assert_equal(false, impl1.eql?(["a", "b", "c"]))
|
530
|
+
assert_equal(false, ["a", "b", "c"].eql?(impl1))
|
531
|
+
|
532
|
+
impl1 = klass.new(["a", "b", "c"])
|
533
|
+
impl2 = klass.new(["a", "b", "d"])
|
534
|
+
assert_equal(false, impl1.eql?(impl2))
|
535
|
+
|
536
|
+
impl1 = klass.new(["a", "b", 1])
|
537
|
+
impl2 = klass.new(["a", "b", 1.0])
|
538
|
+
assert_equal(false, impl1.eql?(impl2))
|
539
|
+
end
|
540
|
+
NOSIZE_IMPLS.each do |klass|
|
541
|
+
impl1 = klass.new(["a", "b", "c"])
|
542
|
+
impl2 = klass.new(["a", "b", "c"])
|
543
|
+
assert_equal(false, impl1.eql?(impl2))
|
544
|
+
assert_equal(true, impl1.eql?(impl1))
|
545
|
+
end
|
546
|
+
end
|
547
|
+
|
548
|
+
def test_fetch
|
549
|
+
FULL_IMPLS.each do |klass|
|
550
|
+
impl = klass.new([1, 2, 3])
|
551
|
+
|
552
|
+
assert_equal(1, impl.fetch(-3))
|
553
|
+
assert_equal(3, impl.fetch(-1))
|
554
|
+
assert_equal(1, impl.fetch(0))
|
555
|
+
assert_equal(3, impl.fetch(2))
|
556
|
+
|
557
|
+
[-4, 3].each do |i|
|
558
|
+
assert_raise IndexError do
|
559
|
+
impl.fetch(i)
|
560
|
+
end
|
561
|
+
end
|
562
|
+
|
563
|
+
assert_equal(-1, impl.fetch(-4, -1))
|
564
|
+
assert_equal(1, impl.fetch(-3, -1))
|
565
|
+
assert_equal(3, impl.fetch(-1, -1))
|
566
|
+
assert_equal(1, impl.fetch(0, -1))
|
567
|
+
assert_equal(3, impl.fetch(2, -1))
|
568
|
+
assert_equal(-1, impl.fetch(3, -1))
|
569
|
+
|
570
|
+
assert_equal(-1, impl.fetch(-4) { -1 })
|
571
|
+
assert_equal(1, impl.fetch(-3) { -1 })
|
572
|
+
assert_equal(3, impl.fetch(-1) { -1 })
|
573
|
+
assert_equal(1, impl.fetch(0) { -1 })
|
574
|
+
assert_equal(3, impl.fetch(2) { -1 })
|
575
|
+
assert_equal(-1, impl.fetch(3) { -1 })
|
576
|
+
end
|
577
|
+
NOSIZE_IMPLS.each do |klass|
|
578
|
+
impl = klass.new([1, 2, 3])
|
579
|
+
|
580
|
+
assert_equal(1, impl.fetch(0))
|
581
|
+
assert_equal(3, impl.fetch(2))
|
582
|
+
|
583
|
+
assert_equal(1, impl.fetch(0, -1))
|
584
|
+
assert_equal(3, impl.fetch(2, -1))
|
585
|
+
|
586
|
+
assert_equal(1, impl.fetch(0) { -1 })
|
587
|
+
assert_equal(3, impl.fetch(2) { -1 })
|
588
|
+
|
589
|
+
[-4, -3, -1].each do |i|
|
590
|
+
assert_raise NotImplementedError do
|
591
|
+
impl.fetch(i)
|
592
|
+
end
|
593
|
+
assert_raise NotImplementedError do
|
594
|
+
impl.fetch(i, -1)
|
595
|
+
end
|
596
|
+
assert_raise NotImplementedError do
|
597
|
+
impl.fetch(i) { -1 }
|
598
|
+
end
|
599
|
+
end
|
600
|
+
|
601
|
+
assert_raise ErrorForTest do
|
602
|
+
impl.fetch(3)
|
603
|
+
end
|
604
|
+
assert_raise ErrorForTest do
|
605
|
+
impl.fetch(3, -1)
|
606
|
+
end
|
607
|
+
assert_raise ErrorForTest do
|
608
|
+
impl.fetch(3) { -1 }
|
609
|
+
end
|
610
|
+
end
|
611
|
+
end
|
612
|
+
|
613
|
+
def test_first
|
614
|
+
FULL_IMPLS.each do |klass|
|
615
|
+
impl = klass.new([1, 2, 3])
|
616
|
+
assert_equal(1, impl.first)
|
617
|
+
assert_equal([], impl.first(0))
|
618
|
+
assert_equal([1], impl.first(1))
|
619
|
+
assert_equal([1, 2], impl.first(2))
|
620
|
+
assert_equal([1, 2, 3], impl.first(3))
|
621
|
+
assert_equal([1, 2, 3], impl.first(4))
|
622
|
+
|
623
|
+
impl = klass.new([])
|
624
|
+
assert_equal(nil, impl.first)
|
625
|
+
assert_equal([], impl.first(0))
|
626
|
+
assert_equal([], impl.first(1))
|
627
|
+
end
|
628
|
+
NOSIZE_IMPLS.each do |klass|
|
629
|
+
impl = klass.new([1, 2, 3])
|
630
|
+
assert_equal(1, impl.first)
|
631
|
+
assert_equal([], impl.first(0))
|
632
|
+
assert_equal([1], impl.first(1))
|
633
|
+
assert_equal([1, 2], impl.first(2))
|
634
|
+
assert_equal([1, 2, 3], impl.first(3))
|
635
|
+
assert_raise ErrorForTest do
|
636
|
+
impl.first(4)
|
637
|
+
end
|
638
|
+
|
639
|
+
impl = klass.new([])
|
640
|
+
assert_raise(ErrorForTest) { impl.first }
|
641
|
+
assert_equal([], impl.first(0))
|
642
|
+
assert_raise(ErrorForTest) { impl.first(1) }
|
643
|
+
end
|
644
|
+
end
|
645
|
+
|
646
|
+
def test_flatten
|
647
|
+
FULL_IMPLS.each do |klass|
|
648
|
+
impl = klass.new([1, [2, 3, [4], 5]])
|
649
|
+
assert_equal([1, 2, 3, 4, 5], impl.flatten)
|
650
|
+
assert_equal([1, 2, 3, [4], 5], impl.flatten(1))
|
651
|
+
end
|
652
|
+
NOSIZE_IMPLS.each do |klass|
|
653
|
+
impl = klass.new([1, [2, 3, [4], 5]])
|
654
|
+
assert_raise(NotImplementedError) { impl.flatten }
|
655
|
+
assert_raise(NotImplementedError) { impl.flatten(1) }
|
656
|
+
end
|
657
|
+
end
|
658
|
+
|
659
|
+
def test_hash
|
660
|
+
FULL_IMPLS.each do |klass|
|
661
|
+
impl1 = klass.new([1, 2, 3, 4])
|
662
|
+
impl2 = klass.new([1, 2, 3, 4])
|
663
|
+
assert(impl1.hash == impl2.hash)
|
664
|
+
|
665
|
+
# Assuming there is no collision.
|
666
|
+
impl2 = klass.new([1, 2, 3, 3])
|
667
|
+
assert(impl1.hash != impl2.hash)
|
668
|
+
end
|
669
|
+
NOSIZE_IMPLS.each do |klass|
|
670
|
+
# Assuming there is no collision.
|
671
|
+
impl1 = klass.new([1, 2, 3, 4])
|
672
|
+
impl2 = klass.new([1, 2, 3, 4])
|
673
|
+
assert(impl1.hash != impl2.hash)
|
674
|
+
end
|
675
|
+
end
|
676
|
+
|
677
|
+
def test_include
|
678
|
+
FULL_IMPLS.each do |klass|
|
679
|
+
impl = klass.new([1, 2, 3, 4.0])
|
680
|
+
assert_equal(true, impl.include?(2))
|
681
|
+
assert_equal(true, impl.include?(2.0))
|
682
|
+
assert_equal(true, impl.include?(4))
|
683
|
+
assert_equal(false, impl.include?(0))
|
684
|
+
assert_equal(false, impl.include?(4.01))
|
685
|
+
end
|
686
|
+
NOSIZE_IMPLS.each do |klass|
|
687
|
+
impl = klass.new([1, 2, 3, 4.0])
|
688
|
+
assert_raise(NotImplementedError) { impl.include?(0) }
|
689
|
+
assert_raise(NotImplementedError) { impl.include?(3) }
|
690
|
+
end
|
691
|
+
end
|
692
|
+
|
693
|
+
def test_index
|
694
|
+
FULL_IMPLS.each do |klass|
|
695
|
+
impl = klass.new([1, 2, 3, 4.0])
|
696
|
+
assert_equal(1, impl.index(2))
|
697
|
+
assert_equal(1, impl.index(2.0))
|
698
|
+
assert_equal(3, impl.index(4))
|
699
|
+
assert_equal(nil, impl.index(0))
|
700
|
+
assert_equal(nil, impl.index(4.01))
|
701
|
+
end
|
702
|
+
NOSIZE_IMPLS.each do |klass|
|
703
|
+
impl = klass.new([1, 2, 3, 4.0])
|
704
|
+
assert_raise(NotImplementedError) { impl.index(0) }
|
705
|
+
assert_raise(NotImplementedError) { impl.index(3) }
|
706
|
+
end
|
707
|
+
end
|
708
|
+
|
709
|
+
def test_to_s_inspect
|
710
|
+
FULL_IMPLS.each do |klass|
|
711
|
+
ary = [1, "a", 2.0, [3], Hash.new([4])]
|
712
|
+
impl = klass.new(ary)
|
713
|
+
assert_equal(ary.to_s, impl.to_s)
|
714
|
+
assert_equal(ary.inspect, impl.inspect)
|
715
|
+
end
|
716
|
+
NOSIZE_IMPLS.each do |klass|
|
717
|
+
impl = klass.new([1, "a", 2.0, [3], Hash.new([4])])
|
718
|
+
assert_nothing_raised do
|
719
|
+
impl.to_s
|
720
|
+
impl.inspect
|
721
|
+
end
|
722
|
+
end
|
723
|
+
end
|
724
|
+
|
725
|
+
def test_join
|
726
|
+
FULL_IMPLS.each do |klass|
|
727
|
+
ary = [1, "a", 2.0, [3], Hash.new([4])]
|
728
|
+
impl = klass.new(ary)
|
729
|
+
assert_equal(ary.join, impl.join)
|
730
|
+
assert_equal(ary.join('foobar'), impl.join('foobar'))
|
731
|
+
temp = $,
|
732
|
+
$, = 'bazbaz'
|
733
|
+
assert_equal(ary.join, impl.join)
|
734
|
+
$, = temp
|
735
|
+
end
|
736
|
+
NOSIZE_IMPLS.each do |klass|
|
737
|
+
impl = klass.new([1, "a", 2.0, [3], Hash.new([4])])
|
738
|
+
assert_raise(NotImplementedError) { impl.join }
|
739
|
+
assert_raise(NotImplementedError) { impl.join('foobar') }
|
740
|
+
temp = $,
|
741
|
+
$, = 'bazbaz'
|
742
|
+
assert_raise(NotImplementedError) { impl.join }
|
743
|
+
$, = temp
|
744
|
+
end
|
745
|
+
end
|
746
|
+
|
747
|
+
def test_last
|
748
|
+
FULL_IMPLS.each do |klass|
|
749
|
+
impl = klass.new([1, 2, 3])
|
750
|
+
assert_equal(3, impl.last)
|
751
|
+
assert_equal([], impl.last(0))
|
752
|
+
assert_equal([3], impl.last(1))
|
753
|
+
assert_equal([2, 3], impl.last(2))
|
754
|
+
assert_equal([1, 2, 3], impl.last(3))
|
755
|
+
assert_equal([1, 2, 3], impl.last(4))
|
756
|
+
|
757
|
+
impl = klass.new([])
|
758
|
+
assert_equal(nil, impl.last)
|
759
|
+
assert_equal([], impl.last(0))
|
760
|
+
assert_equal([], impl.last(1))
|
761
|
+
end
|
762
|
+
NOSIZE_IMPLS.each do |klass|
|
763
|
+
impl = klass.new([1, 2, 3])
|
764
|
+
assert_raise(NotImplementedError) { impl.last }
|
765
|
+
assert_raise(NotImplementedError) { impl.last(0) }
|
766
|
+
assert_raise(NotImplementedError) { impl.last(1) }
|
767
|
+
|
768
|
+
impl = klass.new([])
|
769
|
+
assert_raise(NotImplementedError) { impl.last }
|
770
|
+
assert_raise(NotImplementedError) { impl.last(0) }
|
771
|
+
assert_raise(NotImplementedError) { impl.last(1) }
|
772
|
+
end
|
773
|
+
end
|
774
|
+
|
775
|
+
def test_size_length
|
776
|
+
FULL_IMPLS.each do |klass|
|
777
|
+
impl = klass.new([1, 2, 3])
|
778
|
+
assert_equal(3, impl.size)
|
779
|
+
assert_equal(3, impl.length)
|
780
|
+
|
781
|
+
impl = klass.new([])
|
782
|
+
assert_equal(0, impl.size)
|
783
|
+
assert_equal(0, impl.length)
|
784
|
+
end
|
785
|
+
NOSIZE_IMPLS.each do |klass|
|
786
|
+
impl = klass.new([1, 2, 3])
|
787
|
+
assert_raise(NotImplementedError) { impl.size }
|
788
|
+
assert_raise(NotImplementedError) { impl.length }
|
789
|
+
|
790
|
+
impl = klass.new([])
|
791
|
+
assert_raise(NotImplementedError) { impl.size }
|
792
|
+
assert_raise(NotImplementedError) { impl.length }
|
793
|
+
end
|
794
|
+
end
|
795
|
+
|
796
|
+
def test_pack
|
797
|
+
# TODO
|
798
|
+
end
|
799
|
+
|
800
|
+
def test_permutation
|
801
|
+
FULL_IMPLS.each do |klass|
|
802
|
+
ary = [1, 2, 3, 4]
|
803
|
+
impl = klass.new([1, 2, 3, 4])
|
804
|
+
(-1..5).each do |i|
|
805
|
+
ary_p = []
|
806
|
+
impl_p = []
|
807
|
+
ary.permutation(i) do |p|
|
808
|
+
ary_p << p
|
809
|
+
end
|
810
|
+
impl.permutation(i) do |p|
|
811
|
+
impl_p << p
|
812
|
+
end
|
813
|
+
assert_equal(ary_p.sort, impl_p.sort,
|
814
|
+
"Failed #{klass.name}\#permutation(#{i})")
|
815
|
+
|
816
|
+
assert_equal(ary.permutation(i).to_a.sort,
|
817
|
+
impl.permutation(i).to_a.sort)
|
818
|
+
end
|
819
|
+
end
|
820
|
+
NOSIZE_IMPLS.each do |klass|
|
821
|
+
impl = klass.new([1, 2, 3, 4])
|
822
|
+
(-1..5).each do |i|
|
823
|
+
assert_raise(NotImplementedError) { impl.permutation(i) }
|
824
|
+
end
|
825
|
+
end
|
826
|
+
end
|
827
|
+
|
828
|
+
def test_product
|
829
|
+
FULL_IMPLS.each do |klass|
|
830
|
+
ary = [1, 2, 3, 4]
|
831
|
+
impl = klass.new([1, 2, 3, 4])
|
832
|
+
[[[4, 5]], [[1, 2]], [[4, 5], [1, 2]]].each do |lists|
|
833
|
+
ary_p = []
|
834
|
+
impl_p = []
|
835
|
+
ary.product(*lists) do |p|
|
836
|
+
ary_p << p
|
837
|
+
end
|
838
|
+
impl.product(*lists) do |p|
|
839
|
+
impl_p << p
|
840
|
+
end
|
841
|
+
assert_equal(ary_p.sort, impl_p.sort,
|
842
|
+
"Failed #{klass.name}\#product(#{lists.join(', ')}")
|
843
|
+
|
844
|
+
assert_equal(ary.product(*lists).to_a.sort,
|
845
|
+
impl.product(*lists).to_a.sort)
|
846
|
+
end
|
847
|
+
end
|
848
|
+
NOSIZE_IMPLS.each do |klass|
|
849
|
+
impl = klass.new([1, 2, 3, 4])
|
850
|
+
[[[4, 5]], [[1, 2]], [[4, 5], [1, 2]]].each do |lists|
|
851
|
+
assert_raise(NotImplementedError) { impl.product(*lists) }
|
852
|
+
end
|
853
|
+
end
|
854
|
+
end
|
855
|
+
|
856
|
+
def test_rassoc
|
857
|
+
FULL_IMPLS.each do |klass|
|
858
|
+
impl = klass.new([[15, 1], [25, 2], [35, 3]])
|
859
|
+
msg = "Failed in #{klass.name}"
|
860
|
+
assert_equal([15, 1], impl.rassoc(1), msg)
|
861
|
+
assert_equal([25, 2], impl.rassoc(2), msg)
|
862
|
+
assert_equal([25, 2], impl.rassoc(2.0), msg)
|
863
|
+
assert_equal([35, 3], impl.rassoc(3), msg)
|
864
|
+
|
865
|
+
assert_equal(nil, impl.rassoc(0), msg)
|
866
|
+
assert_equal(nil, impl.rassoc(4), msg)
|
867
|
+
end
|
868
|
+
NOSIZE_IMPLS.each do |klass|
|
869
|
+
impl = klass.new([])
|
870
|
+
(0..4).each do |i|
|
871
|
+
assert_raise(NotImplementedError) { impl.rassoc(i) }
|
872
|
+
end
|
873
|
+
end
|
874
|
+
end
|
875
|
+
|
876
|
+
def test_repeated_combination
|
877
|
+
FULL_IMPLS.each do |klass|
|
878
|
+
ary = [1, 2, 3, 4]
|
879
|
+
impl = klass.new([1, 2, 3, 4])
|
880
|
+
(-1..5).each do |i|
|
881
|
+
ary_p = []
|
882
|
+
impl_p = []
|
883
|
+
ary.repeated_combination(i) do |p|
|
884
|
+
ary_p << p
|
885
|
+
end
|
886
|
+
impl.repeated_combination(i) do |p|
|
887
|
+
impl_p << p
|
888
|
+
end
|
889
|
+
assert_equal(ary_p.sort, impl_p.sort,
|
890
|
+
"Failed #{klass.name}\#repeated_combination(#{i})")
|
891
|
+
|
892
|
+
assert_equal(ary.repeated_combination(i).to_a.sort,
|
893
|
+
impl.repeated_combination(i).to_a.sort)
|
894
|
+
end
|
895
|
+
end
|
896
|
+
NOSIZE_IMPLS.each do |klass|
|
897
|
+
impl = klass.new([1, 2, 3, 4])
|
898
|
+
(-1..5).each do |i|
|
899
|
+
assert_raise(NotImplementedError) { impl.repeated_combination(i) }
|
900
|
+
end
|
901
|
+
end
|
902
|
+
end
|
903
|
+
|
904
|
+
def test_repeated_permutation
|
905
|
+
FULL_IMPLS.each do |klass|
|
906
|
+
ary = [1, 2, 3, 4]
|
907
|
+
impl = klass.new([1, 2, 3, 4])
|
908
|
+
(-1..5).each do |i|
|
909
|
+
ary_p = []
|
910
|
+
impl_p = []
|
911
|
+
ary.repeated_permutation(i) do |p|
|
912
|
+
ary_p << p
|
913
|
+
end
|
914
|
+
impl.repeated_permutation(i) do |p|
|
915
|
+
impl_p << p
|
916
|
+
end
|
917
|
+
assert_equal(ary_p.sort, impl_p.sort,
|
918
|
+
"Failed #{klass.name}\#repeated_permutation(#{i})")
|
919
|
+
|
920
|
+
assert_equal(ary.repeated_permutation(i).to_a.sort,
|
921
|
+
impl.repeated_permutation(i).to_a.sort)
|
922
|
+
end
|
923
|
+
end
|
924
|
+
NOSIZE_IMPLS.each do |klass|
|
925
|
+
impl = klass.new([1, 2, 3, 4])
|
926
|
+
(-1..5).each do |i|
|
927
|
+
assert_raise(NotImplementedError) { impl.repeated_permutation(i) }
|
928
|
+
end
|
929
|
+
end
|
930
|
+
end
|
931
|
+
|
932
|
+
def test_reverse
|
933
|
+
FULL_IMPLS.each do |klass|
|
934
|
+
impl = klass.new([1, 2, 3, 4])
|
935
|
+
assert_equal([4, 3, 2, 1], impl.reverse)
|
936
|
+
|
937
|
+
impl = klass.new([])
|
938
|
+
assert_equal([], impl.reverse)
|
939
|
+
end
|
940
|
+
NOSIZE_IMPLS.each do |klass|
|
941
|
+
impl = klass.new([])
|
942
|
+
assert_raise(NotImplementedError) { impl.reverse }
|
943
|
+
end
|
944
|
+
end
|
945
|
+
|
946
|
+
def test_reverse_each
|
947
|
+
FULL_IMPLS.each do |klass|
|
948
|
+
impl = klass.new([1, 2, 3, 4])
|
949
|
+
assert_equal([4, 3, 2, 1], impl.reverse)
|
950
|
+
ary = []
|
951
|
+
impl.reverse_each { |el| ary << el }
|
952
|
+
assert_equal([4, 3, 2, 1], ary)
|
953
|
+
|
954
|
+
impl = klass.new([])
|
955
|
+
assert_equal([], impl.reverse)
|
956
|
+
impl.reverse_each { |el| assert_fail }
|
957
|
+
end
|
958
|
+
NOSIZE_IMPLS.each do |klass|
|
959
|
+
impl = klass.new([])
|
960
|
+
assert_raise(NotImplementedError) do
|
961
|
+
impl.reverse_each { |el| } # Nothing to do in this block.
|
962
|
+
end
|
963
|
+
assert_raise(NotImplementedError) { impl.reverse_each.next }
|
964
|
+
end
|
965
|
+
end
|
966
|
+
|
967
|
+
def test_rindex
|
968
|
+
FULL_IMPLS.each do |klass|
|
969
|
+
msg = "Failed in #{klass.name}"
|
970
|
+
|
971
|
+
impl = klass.new([1, 0, 0, 1, 0])
|
972
|
+
assert_equal(3, impl.rindex(1), msg)
|
973
|
+
|
974
|
+
impl = klass.new([1, 0, 0, 0, 0])
|
975
|
+
assert_equal(0, impl.rindex(1), msg)
|
976
|
+
|
977
|
+
impl = klass.new([0, 0, 0, 0, 0])
|
978
|
+
assert_equal(nil, impl.rindex(1), msg)
|
979
|
+
|
980
|
+
impl = klass.new([3, 0, 2, 0, 0])
|
981
|
+
rindex = impl.rindex do |el|
|
982
|
+
el > 1
|
983
|
+
end
|
984
|
+
assert_equal(2, rindex, msg)
|
985
|
+
end
|
986
|
+
NOSIZE_IMPLS.each do |klass|
|
987
|
+
impl = klass.new([])
|
988
|
+
assert_raise(NotImplementedError) { impl.rindex }
|
989
|
+
assert_raise(NotImplementedError) do
|
990
|
+
impl.rindex { |el| } # Nothing to do in this block.
|
991
|
+
end
|
992
|
+
end
|
993
|
+
end
|
994
|
+
|
995
|
+
def test_rotate
|
996
|
+
FULL_IMPLS.each do |klass|
|
997
|
+
ary = ["a" ,"b", "c", "d"]
|
998
|
+
impl = klass.new(ary)
|
999
|
+
assert_equal(ary.rotate, impl.rotate)
|
1000
|
+
(-9..9).each do |i|
|
1001
|
+
assert_equal(ary.rotate(i), impl.rotate(i))
|
1002
|
+
end
|
1003
|
+
end
|
1004
|
+
NOSIZE_IMPLS.each do |klass|
|
1005
|
+
impl = klass.new(["a" ,"b", "c", "d"])
|
1006
|
+
assert_raise(NotImplementedError) { impl.rotate }
|
1007
|
+
(-9..9).each do |i|
|
1008
|
+
assert_raise(NotImplementedError) { impl.rotate(i) }
|
1009
|
+
end
|
1010
|
+
end
|
1011
|
+
end
|
1012
|
+
|
1013
|
+
def test_sample
|
1014
|
+
FULL_IMPLS.each do |klass|
|
1015
|
+
impl = klass.new([1, 2, 3])
|
1016
|
+
(0..4).each do |i|
|
1017
|
+
sample = impl.sample(i)
|
1018
|
+
assert_equal(i > impl.size ? impl.size : i, sample.size,
|
1019
|
+
"Failed #{klass.name}#sample(#{i})")
|
1020
|
+
read = []
|
1021
|
+
sample.each do |el|
|
1022
|
+
assert(impl.include?(el))
|
1023
|
+
assert(!read.include?(el))
|
1024
|
+
read << el
|
1025
|
+
end
|
1026
|
+
|
1027
|
+
if i > 0
|
1028
|
+
# res will be Integer if sample always returns a same array
|
1029
|
+
# if not, res will be false.
|
1030
|
+
res = 10000.times do
|
1031
|
+
if sample != impl.sample(i)
|
1032
|
+
break false
|
1033
|
+
end
|
1034
|
+
end
|
1035
|
+
assert(!res)
|
1036
|
+
end
|
1037
|
+
end
|
1038
|
+
sample = impl.sample
|
1039
|
+
assert(impl.include?(sample))
|
1040
|
+
|
1041
|
+
impl = klass.new([])
|
1042
|
+
assert_equal(nil, impl.sample)
|
1043
|
+
assert_equal([], impl.sample(1))
|
1044
|
+
end
|
1045
|
+
NOSIZE_IMPLS.each do |klass|
|
1046
|
+
impl = klass.new([])
|
1047
|
+
assert_raise(NotImplementedError) { impl.sample(0) }
|
1048
|
+
assert_raise(NotImplementedError) { impl.sample }
|
1049
|
+
end
|
1050
|
+
end
|
1051
|
+
|
1052
|
+
def test_shuffle
|
1053
|
+
FULL_IMPLS.each do |klass|
|
1054
|
+
impl = klass.new([1, 2, 3, 4, 5])
|
1055
|
+
shuffle = impl.shuffle
|
1056
|
+
assert_equal(impl, shuffle.sort)
|
1057
|
+
|
1058
|
+
# res will be Integer if sample always returns a same array
|
1059
|
+
# if not, res will be false.
|
1060
|
+
res = 10000.times do
|
1061
|
+
break false if shuffle != impl.shuffle
|
1062
|
+
end
|
1063
|
+
assert(!res)
|
1064
|
+
|
1065
|
+
impl = klass.new([])
|
1066
|
+
assert_equal(impl, impl.shuffle)
|
1067
|
+
end
|
1068
|
+
NOSIZE_IMPLS.each do |klass|
|
1069
|
+
impl = klass.new([1, 2, 3, 4, 5])
|
1070
|
+
assert_raise(NotImplementedError) { impl.shuffle }
|
1071
|
+
end
|
1072
|
+
end
|
1073
|
+
|
1074
|
+
def test_slice_position
|
1075
|
+
FULL_IMPLS.each do |klass|
|
1076
|
+
impl = klass.new((0...10).to_a)
|
1077
|
+
assert_nil(impl.slice(-11))
|
1078
|
+
assert_equal(0, impl.slice(-10))
|
1079
|
+
assert_equal(9, impl.slice(-1))
|
1080
|
+
assert_equal(0, impl.slice(0))
|
1081
|
+
assert_equal(9, impl.slice(9))
|
1082
|
+
assert_nil(impl.slice(10))
|
1083
|
+
end
|
1084
|
+
NOSIZE_IMPLS.each do |klass|
|
1085
|
+
impl = klass.new((0...10).to_a)
|
1086
|
+
[-11, -10, -1].each do |pos|
|
1087
|
+
assert_raise NotImplementedError do
|
1088
|
+
impl.slice(pos)
|
1089
|
+
end
|
1090
|
+
end
|
1091
|
+
|
1092
|
+
assert_equal(0, impl.slice(0))
|
1093
|
+
assert_equal(9, impl.slice(9))
|
1094
|
+
|
1095
|
+
assert_raise ErrorForTest do
|
1096
|
+
impl.slice(10)
|
1097
|
+
end
|
1098
|
+
end
|
1099
|
+
end
|
1100
|
+
|
1101
|
+
def test_slice_range
|
1102
|
+
FULL_IMPLS.each do |klass|
|
1103
|
+
impl = klass.new((0...10).to_a)
|
1104
|
+
msg = "Error in #{klass.name}"
|
1105
|
+
|
1106
|
+
assert_equal(nil, impl.slice(-12..-11), msg)
|
1107
|
+
assert_equal(nil, impl.slice(-14..-10), msg)
|
1108
|
+
assert_equal([0, 1, 2], impl.slice(-10..-8), msg)
|
1109
|
+
assert_equal([8, 9], impl.slice(-2..-1), msg)
|
1110
|
+
assert_equal([1, 2, 3], impl.slice(-9..3), msg)
|
1111
|
+
assert_equal([0, 1], impl.slice(0..1), msg)
|
1112
|
+
assert_equal([8, 9], impl.slice(8..9), msg)
|
1113
|
+
assert_equal([8, 9], impl.slice(8..100), msg)
|
1114
|
+
assert_equal([], impl.slice(10..15), msg)
|
1115
|
+
assert_equal(nil, impl.slice(11..15), msg)
|
1116
|
+
assert_equal([], impl.slice(-1..-2), msg)
|
1117
|
+
assert_equal([], impl.slice(3..0), msg)
|
1118
|
+
assert_equal([], impl.slice(-1..1), msg)
|
1119
|
+
assert_equal(nil, impl.slice(15..14), msg)
|
1120
|
+
assert_equal(nil, impl.slice(-11..-20), msg)
|
1121
|
+
assert_equal(nil, impl.slice(15..5), msg)
|
1122
|
+
assert_equal([], impl.slice(-10..-20), msg)
|
1123
|
+
assert_equal([], impl.slice(1..-20), msg)
|
1124
|
+
|
1125
|
+
assert_equal(nil, impl.slice(-12...-10), msg)
|
1126
|
+
assert_equal(nil, impl.slice(-14...-9), msg)
|
1127
|
+
assert_equal([0, 1, 2], impl.slice(-10...-7), msg)
|
1128
|
+
assert_equal([], impl.slice(-2...0), msg)
|
1129
|
+
assert_equal([1, 2, 3], impl.slice(-9...4), msg)
|
1130
|
+
assert_equal([0, 1], impl.slice(0...2), msg)
|
1131
|
+
assert_equal([8, 9], impl.slice(8...10), msg)
|
1132
|
+
assert_equal([8, 9], impl.slice(8...100), msg)
|
1133
|
+
assert_equal([], impl.slice(10...15), msg)
|
1134
|
+
assert_equal(nil, impl.slice(11...15), msg)
|
1135
|
+
assert_equal([], impl.slice(-1...-1), msg)
|
1136
|
+
assert_equal([], impl.slice(0...0), msg)
|
1137
|
+
assert_equal([3, 4, 5, 6, 7, 8], impl.slice(3...-1), msg)
|
1138
|
+
assert_equal([], impl.slice(-1...2), msg)
|
1139
|
+
assert_equal(nil, impl.slice(16...15), msg)
|
1140
|
+
assert_equal(nil, impl.slice(-11...-20), msg)
|
1141
|
+
assert_equal(nil, impl.slice(15...5), msg)
|
1142
|
+
assert_equal([], impl.slice(-10...-20), msg)
|
1143
|
+
assert_equal([], impl.slice(1...-20), msg)
|
1144
|
+
end
|
1145
|
+
NOSIZE_IMPLS.each do |klass|
|
1146
|
+
impl = klass.new((0...10).to_a)
|
1147
|
+
[-12..-11, -14..-10, -10..-8, -2..-1, -1..1].each do |range|
|
1148
|
+
assert_raise NotImplementedError do
|
1149
|
+
impl.slice(range)
|
1150
|
+
end
|
1151
|
+
end
|
1152
|
+
|
1153
|
+
assert_equal([0, 1], impl.slice(0..1))
|
1154
|
+
assert_equal([8, 9], impl.slice(8..9))
|
1155
|
+
assert_equal([], impl.slice(15..10))
|
1156
|
+
assert_equal([], impl.slice(-1..-2))
|
1157
|
+
|
1158
|
+
[8..11, 10..15].each do |range|
|
1159
|
+
assert_raise ErrorForTest do
|
1160
|
+
impl.slice(range)
|
1161
|
+
end
|
1162
|
+
end
|
1163
|
+
end
|
1164
|
+
end
|
1165
|
+
|
1166
|
+
def test_to_a_and_to_ary
|
1167
|
+
FULL_IMPLS.each do |klass|
|
1168
|
+
ary = [1, 2, 3, 4, 5]
|
1169
|
+
impl = klass.new(ary)
|
1170
|
+
assert(ary.eql?(impl.to_a))
|
1171
|
+
assert(ary.eql?(impl.to_ary))
|
1172
|
+
end
|
1173
|
+
NOSIZE_IMPLS.each do |klass|
|
1174
|
+
impl = klass.new([])
|
1175
|
+
assert_raise(NotImplementedError) { impl.to_a }
|
1176
|
+
assert_raise(NotImplementedError) { impl.to_ary }
|
1177
|
+
end
|
1178
|
+
end
|
1179
|
+
|
1180
|
+
def test_transpose
|
1181
|
+
FULL_IMPLS.each do |klass|
|
1182
|
+
impl = klass.new([[1, 2], [3, 4], [5, 6]])
|
1183
|
+
assert_equal([[1, 3, 5], [2, 4, 6]], impl.transpose)
|
1184
|
+
|
1185
|
+
impl = klass.new([])
|
1186
|
+
assert_equal([], impl.transpose)
|
1187
|
+
|
1188
|
+
impl = klass.new([0])
|
1189
|
+
assert_raise(TypeError) { impl.transpose }
|
1190
|
+
|
1191
|
+
impl = klass.new([[1, 2], [3, 4, 5]])
|
1192
|
+
assert_raise(IndexError) { impl.transpose }
|
1193
|
+
end
|
1194
|
+
NOSIZE_IMPLS.each do |klass|
|
1195
|
+
impl = klass.new([])
|
1196
|
+
assert_raise(NotImplementedError) { impl.transpose }
|
1197
|
+
end
|
1198
|
+
end
|
1199
|
+
|
1200
|
+
def test_uniq
|
1201
|
+
FULL_IMPLS.each do |klass|
|
1202
|
+
impl = klass.new([1, 1, 1, 2, 1])
|
1203
|
+
assert_equal([1, 2], impl.uniq)
|
1204
|
+
|
1205
|
+
impl = klass.new([1, 1.0, Rational(1, 1)])
|
1206
|
+
assert_equal([1, 1.0, Rational(1, 1)], impl.uniq)
|
1207
|
+
uniq = impl.uniq { |n| n.to_i }
|
1208
|
+
assert_equal([1], uniq)
|
1209
|
+
end
|
1210
|
+
NOSIZE_IMPLS.each do |klass|
|
1211
|
+
impl = klass.new([])
|
1212
|
+
assert_raise(NotImplementedError) { impl.uniq }
|
1213
|
+
end
|
1214
|
+
end
|
1215
|
+
|
1216
|
+
def test_values_at
|
1217
|
+
FULL_IMPLS.each do |klass|
|
1218
|
+
msg = "Error in #{klass.name}"
|
1219
|
+
impl = klass.new([1, 2, 3, 4, 5])
|
1220
|
+
assert_equal([1, 3, 5], impl.values_at(0, 2, 4), msg)
|
1221
|
+
assert_equal([nil, 1, 5, 1, 5, nil, nil],
|
1222
|
+
impl.values_at(-6, -5, -1, 0, 4, 5, 100),
|
1223
|
+
msg)
|
1224
|
+
assert_equal([2, 3], impl.values_at(1..2), msg)
|
1225
|
+
assert_equal([4, 5, nil], impl.values_at(3..10), msg)
|
1226
|
+
assert_equal([], impl.values_at(6..7), msg)
|
1227
|
+
assert_equal([5, 4, 5, 1],
|
1228
|
+
impl.values_at(-1, 3..4, 0), msg)
|
1229
|
+
assert_equal([2, 3], impl.values_at(1...3), msg)
|
1230
|
+
assert_equal([4, 5], impl.values_at(3...11), msg)
|
1231
|
+
assert_equal([], impl.values_at(6...8), msg)
|
1232
|
+
assert_equal([5, 4, 5, 1],
|
1233
|
+
impl.values_at(-1, 3...5, 0), msg)
|
1234
|
+
end
|
1235
|
+
NOSIZE_IMPLS.each do |klass|
|
1236
|
+
impl = klass.new([1, 2, 3, 4, 5])
|
1237
|
+
assert_equal([1, 3, 5], impl.values_at(0, 2, 4))
|
1238
|
+
assert_raise(NotImplementedError) { impl.values_at(-6, 0) }
|
1239
|
+
assert_raise(ErrorForTest) { impl.values_at(0, 5) }
|
1240
|
+
assert_equal([2, 3], impl.values_at(1..2))
|
1241
|
+
assert_raise(ErrorForTest) { impl.values_at(3..10) }
|
1242
|
+
assert_raise(ErrorForTest) { impl.values_at(6..7) }
|
1243
|
+
assert_raise(NotImplementedError) { impl.values_at(-1, 3..4, 0) }
|
1244
|
+
end
|
1245
|
+
end
|
1246
|
+
|
1247
|
+
def test_zip
|
1248
|
+
FULL_IMPLS.each do |klass|
|
1249
|
+
impl = klass.new([1, 2, 3])
|
1250
|
+
assert_equal([[1, 4, 7], [2, 5, 8], [3, 6, 9]],
|
1251
|
+
impl.zip([4, 5, 6], [7, 8, 9]))
|
1252
|
+
end
|
1253
|
+
NOSIZE_IMPLS.each do |klass|
|
1254
|
+
impl = klass.new([])
|
1255
|
+
assert_raise(NotImplementedError) { impl.zip([]) }
|
1256
|
+
end
|
1257
|
+
end
|
1258
|
+
|
1259
|
+
def test_or
|
1260
|
+
FULL_IMPLS.each do |klass|
|
1261
|
+
impl = klass.new([1, 1, 4, 2, 3])
|
1262
|
+
assert_equal([1, 4, 2, 3, 5], impl | [4, 5, 5])
|
1263
|
+
end
|
1264
|
+
NOSIZE_IMPLS.each do |klass|
|
1265
|
+
impl = klass.new([])
|
1266
|
+
assert_raise(NotImplementedError) { impl | [] }
|
1267
|
+
end
|
1268
|
+
end
|
1269
|
+
|
1270
|
+
end
|
1271
|
+
|