random-accessible 0.1.2 → 0.1.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- 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,566 @@
|
|
|
1
|
+
require 'test/unit'
|
|
2
|
+
require 'random-writable'
|
|
3
|
+
|
|
4
|
+
class TestRandomWritable < Test::Unit::TestCase
|
|
5
|
+
|
|
6
|
+
class ErrorForTest < Exception
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
class Reference < Array
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
class WriExp
|
|
13
|
+
|
|
14
|
+
include RandomWritable
|
|
15
|
+
|
|
16
|
+
def initialize(ary = [])
|
|
17
|
+
@a = ary
|
|
18
|
+
@size = ary.size
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def to_ary
|
|
22
|
+
@a.clone
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def expand(n)
|
|
26
|
+
@size += n
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def shrink(n)
|
|
30
|
+
@a.pop(n)
|
|
31
|
+
@size -= n
|
|
32
|
+
@size = 0 if @size < 0
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def replace_access(pos, val)
|
|
36
|
+
if pos < 0
|
|
37
|
+
raise ErrorForTest, "size=#{@size} pos=#{pos}"
|
|
38
|
+
end
|
|
39
|
+
@a[pos.to_int] = val
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
class WriExpSiz < WriExp
|
|
45
|
+
|
|
46
|
+
def size
|
|
47
|
+
@size
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
def replace_access(pos, val)
|
|
51
|
+
if @size <= pos
|
|
52
|
+
raise ErrorForTest, "size=#{@size} pos=#{pos}"
|
|
53
|
+
end
|
|
54
|
+
super
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
class WriExpDelIns < WriExp
|
|
60
|
+
|
|
61
|
+
def delete_access(pos)
|
|
62
|
+
if pos < 0 || @a.size <= pos
|
|
63
|
+
raise ErrorForTest, "@a.size=#{@a.size} but pos=#{pos}"
|
|
64
|
+
end
|
|
65
|
+
@a.delete_at(pos)
|
|
66
|
+
@size -= 1
|
|
67
|
+
return "retval of delete_access"
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
def insert_access(pos, val)
|
|
71
|
+
raise ErrorForTest if pos < 0
|
|
72
|
+
@a.insert(pos, val)
|
|
73
|
+
@size += 1
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
class WriExpDelInsSiz < WriExpSiz
|
|
79
|
+
|
|
80
|
+
def delete_access(pos)
|
|
81
|
+
raise ErrorForTest if pos < 0 || @size <= pos
|
|
82
|
+
@a.delete_at(pos)
|
|
83
|
+
@size -= 1
|
|
84
|
+
return "retval of delete_access"
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
def insert_access(pos, val)
|
|
88
|
+
raise ErrorForTest if pos < 0 || @size <= pos
|
|
89
|
+
@a.insert(pos, val)
|
|
90
|
+
@size += 1
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
class FixedArray < Array
|
|
96
|
+
|
|
97
|
+
def delete_at(pos)
|
|
98
|
+
s = size
|
|
99
|
+
super
|
|
100
|
+
if pos < -s || s <= pos
|
|
101
|
+
return nil
|
|
102
|
+
else
|
|
103
|
+
return "retval of delete_access"
|
|
104
|
+
end
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
NOSIZE_IMPLS = [WriExpDelIns]
|
|
110
|
+
NODELETE_IMPLS = [WriExpSiz]
|
|
111
|
+
FULL_IMPLS = [FixedArray,
|
|
112
|
+
WriExpDelInsSiz]
|
|
113
|
+
|
|
114
|
+
|
|
115
|
+
def test_lshift
|
|
116
|
+
FULL_IMPLS.each do |klass|
|
|
117
|
+
impl = klass.new([1, 2])
|
|
118
|
+
impl << 3 << 4 << 5
|
|
119
|
+
assert_equal([1, 2, 3 ,4 ,5], impl.to_ary)
|
|
120
|
+
assert_equal(5, impl.size)
|
|
121
|
+
end
|
|
122
|
+
NOSIZE_IMPLS.each do |klass|
|
|
123
|
+
impl = klass.new([])
|
|
124
|
+
assert_raise NotImplementedError do
|
|
125
|
+
impl << 0
|
|
126
|
+
end
|
|
127
|
+
end
|
|
128
|
+
end
|
|
129
|
+
|
|
130
|
+
def test_bracket_pos
|
|
131
|
+
FULL_IMPLS.each do |klass|
|
|
132
|
+
impl = klass.new([0, 1, 2, 3, 4, 5])
|
|
133
|
+
impl[0] = "a"
|
|
134
|
+
assert_equal(["a", 1, 2, 3, 4, 5], impl.to_ary)
|
|
135
|
+
assert_equal(6, impl.size)
|
|
136
|
+
impl[3] = 3.5
|
|
137
|
+
assert_equal(["a", 1, 2, 3.5, 4, 5], impl.to_ary)
|
|
138
|
+
assert_equal(6, impl.size)
|
|
139
|
+
impl[10] = nil
|
|
140
|
+
assert_equal(["a", 1, 2, 3.5, 4, 5, nil, nil, nil, nil, nil],
|
|
141
|
+
impl.to_ary)
|
|
142
|
+
assert_equal(11, impl.size)
|
|
143
|
+
end
|
|
144
|
+
NOSIZE_IMPLS.each do |klass|
|
|
145
|
+
impl = klass.new([0, 1, 2, 3, 4, 5])
|
|
146
|
+
impl[0] = "a"
|
|
147
|
+
assert_equal(["a", 1, 2, 3, 4, 5], impl.to_ary)
|
|
148
|
+
impl[3] = 3.5
|
|
149
|
+
assert_equal(["a", 1, 2, 3.5, 4, 5], impl.to_ary)
|
|
150
|
+
impl[10] = nil
|
|
151
|
+
assert_equal(["a", 1, 2, 3.5, 4, 5, nil, nil, nil, nil, nil],
|
|
152
|
+
impl.to_ary)
|
|
153
|
+
end
|
|
154
|
+
end
|
|
155
|
+
|
|
156
|
+
def test_bracket_range
|
|
157
|
+
FULL_IMPLS.each do |klass|
|
|
158
|
+
msg = "Error in #{klass.name}"
|
|
159
|
+
impl = klass.new([0, 1, 2, 3, 4, 5])
|
|
160
|
+
|
|
161
|
+
impl[0..2] = ["a", "b"]
|
|
162
|
+
assert_equal(["a", "b", 3, 4, 5], impl.to_ary, msg)
|
|
163
|
+
assert_equal(5, impl.size, msg)
|
|
164
|
+
|
|
165
|
+
impl[1..3] = 3.5
|
|
166
|
+
assert_equal(["a", 3.5, 5], impl.to_ary, msg)
|
|
167
|
+
assert_equal(3, impl.size, msg)
|
|
168
|
+
|
|
169
|
+
impl[5..10] = nil
|
|
170
|
+
assert_equal(["a", 3.5, 5, nil, nil, nil],
|
|
171
|
+
impl.to_ary, msg)
|
|
172
|
+
assert_equal(6, impl.size, msg)
|
|
173
|
+
|
|
174
|
+
impl[2..3] = [-1, -2, -3]
|
|
175
|
+
assert_equal(["a", 3.5, -1, -2, -3, nil, nil],
|
|
176
|
+
impl.to_ary, msg)
|
|
177
|
+
assert_equal(7, impl.size, msg)
|
|
178
|
+
end
|
|
179
|
+
NODELETE_IMPLS.each do |klass|
|
|
180
|
+
msg = "Error in #{klass.name}"
|
|
181
|
+
impl = klass.new([0, 1, 2, 3, 4, 5])
|
|
182
|
+
impl[0..1] = ["a", "b"]
|
|
183
|
+
assert_equal(["a", "b", 2, 3, 4, 5], impl.to_ary, msg)
|
|
184
|
+
assert_equal(6, impl.size, msg)
|
|
185
|
+
|
|
186
|
+
impl[1..1] = 3.5
|
|
187
|
+
assert_equal(["a", 3.5, 2, 3, 4, 5], impl.to_ary, msg)
|
|
188
|
+
assert_equal(6, impl.size, msg)
|
|
189
|
+
|
|
190
|
+
impl[7..10] = 7
|
|
191
|
+
assert_equal(["a", 3.5, 2, 3, 4, 5, nil, 7],
|
|
192
|
+
impl.to_ary, msg)
|
|
193
|
+
assert_equal(8, impl.size, msg)
|
|
194
|
+
|
|
195
|
+
assert_raise(NotImplementedError) { impl[0..1] = 0 }
|
|
196
|
+
assert_raise(NotImplementedError) { impl[1..3] = [1, 2] }
|
|
197
|
+
assert_equal(["a", 3.5, 2, 3, 4, 5, nil, 7],
|
|
198
|
+
impl.to_ary, msg)
|
|
199
|
+
assert_equal(8, impl.size, msg)
|
|
200
|
+
end
|
|
201
|
+
NOSIZE_IMPLS.each do |klass|
|
|
202
|
+
msg = "Error in #{klass.name}"
|
|
203
|
+
impl = klass.new([0, 1, 2, 3, 4, 5])
|
|
204
|
+
impl[0..2] = ["a", "b"]
|
|
205
|
+
assert_equal(["a", "b", 3, 4, 5], impl.to_ary, msg)
|
|
206
|
+
impl[1..3] = 3.5
|
|
207
|
+
assert_equal(["a", 3.5, 5], impl.to_ary, msg)
|
|
208
|
+
assert_raise(ErrorForTest, msg) { impl[5..10] = nil }
|
|
209
|
+
assert_equal(["a", 3.5, 5], impl.to_ary, msg)
|
|
210
|
+
end
|
|
211
|
+
end
|
|
212
|
+
|
|
213
|
+
def test_clear
|
|
214
|
+
FULL_IMPLS.each do |klass|
|
|
215
|
+
impl = klass.new((1..100).to_a)
|
|
216
|
+
impl.clear
|
|
217
|
+
assert_equal([], impl.to_ary)
|
|
218
|
+
assert_equal(0, impl.size)
|
|
219
|
+
end
|
|
220
|
+
NODELETE_IMPLS.each do |klass|
|
|
221
|
+
impl = klass.new((1..100).to_a)
|
|
222
|
+
impl.clear
|
|
223
|
+
assert_equal([], impl.to_ary)
|
|
224
|
+
assert_equal(0, impl.size)
|
|
225
|
+
end
|
|
226
|
+
NOSIZE_IMPLS.each do |klass|
|
|
227
|
+
impl = klass.new((1..100).to_a)
|
|
228
|
+
assert_raise(NotImplementedError) { impl.clear }
|
|
229
|
+
end
|
|
230
|
+
end
|
|
231
|
+
|
|
232
|
+
def test_concat
|
|
233
|
+
(FULL_IMPLS + NODELETE_IMPLS).each do |klass|
|
|
234
|
+
impl = klass.new([1, 2, 3])
|
|
235
|
+
impl.concat([4, 5])
|
|
236
|
+
assert_equal([1, 2, 3, 4, 5], impl.to_ary)
|
|
237
|
+
assert_equal(5, impl.size)
|
|
238
|
+
end
|
|
239
|
+
NOSIZE_IMPLS.each do |klass|
|
|
240
|
+
impl = klass.new([1, 2, 3])
|
|
241
|
+
assert_raise(NotImplementedError) { impl.concat([4, 5]) }
|
|
242
|
+
end
|
|
243
|
+
end
|
|
244
|
+
|
|
245
|
+
def test_delete_at
|
|
246
|
+
FULL_IMPLS.each do |klass|
|
|
247
|
+
msg = "Error in #{klass.name}"
|
|
248
|
+
|
|
249
|
+
impl = klass.new([1, 2, 3, 4, 5])
|
|
250
|
+
assert_equal("retval of delete_access", impl.delete_at(0), msg)
|
|
251
|
+
assert_equal([2, 3, 4, 5], impl.to_ary, msg)
|
|
252
|
+
assert_equal(4, impl.size, msg)
|
|
253
|
+
|
|
254
|
+
impl = klass.new([1, 2, 3, 4, 5])
|
|
255
|
+
assert_equal("retval of delete_access", impl.delete_at(4), msg)
|
|
256
|
+
assert_equal([1, 2, 3, 4], impl.to_ary, msg)
|
|
257
|
+
assert_equal(4, impl.size, msg)
|
|
258
|
+
|
|
259
|
+
impl = klass.new([1, 2, 3, 4, 5])
|
|
260
|
+
assert_equal("retval of delete_access", impl.delete_at(-1), msg)
|
|
261
|
+
assert_equal([1, 2, 3, 4], impl.to_ary, msg)
|
|
262
|
+
assert_equal(4, impl.size, msg)
|
|
263
|
+
|
|
264
|
+
impl = klass.new([1, 2, 3, 4, 5])
|
|
265
|
+
assert_equal("retval of delete_access", impl.delete_at(-5), msg)
|
|
266
|
+
assert_equal([2, 3, 4, 5], impl.to_ary, msg)
|
|
267
|
+
assert_equal(4, impl.size, msg)
|
|
268
|
+
|
|
269
|
+
impl = klass.new([1, 2, 3, 4, 5])
|
|
270
|
+
assert_equal(nil, impl.delete_at(-7), msg)
|
|
271
|
+
assert_equal(nil, impl.delete_at(-6), msg)
|
|
272
|
+
assert_equal(nil, impl.delete_at(5), msg)
|
|
273
|
+
assert_equal(nil, impl.delete_at(6), msg)
|
|
274
|
+
assert_equal([1, 2, 3, 4, 5], impl.to_ary, msg)
|
|
275
|
+
assert_equal(5, impl.size, msg)
|
|
276
|
+
end
|
|
277
|
+
NODELETE_IMPLS.each do |klass|
|
|
278
|
+
impl = klass.new([1, 2, 3, 4, 5])
|
|
279
|
+
assert_equal(nil, impl.delete_at(-7))
|
|
280
|
+
assert_equal(nil, impl.delete_at(-6))
|
|
281
|
+
assert_raise(NotImplementedError) { impl.delete_at(-5) }
|
|
282
|
+
assert_raise(NotImplementedError) { impl.delete_at(-1) }
|
|
283
|
+
assert_raise(NotImplementedError) { impl.delete_at(0) }
|
|
284
|
+
assert_raise(NotImplementedError) { impl.delete_at(4) }
|
|
285
|
+
assert_equal(nil, impl.delete_at(5))
|
|
286
|
+
assert_equal(nil, impl.delete_at(6))
|
|
287
|
+
end
|
|
288
|
+
NOSIZE_IMPLS.each do |klass|
|
|
289
|
+
impl = klass.new([1, 2, 3, 4, 5])
|
|
290
|
+
assert_equal("retval of delete_access", impl.delete_at(0))
|
|
291
|
+
assert_equal([2, 3, 4, 5], impl.to_ary)
|
|
292
|
+
|
|
293
|
+
impl = klass.new([1, 2, 3, 4, 5])
|
|
294
|
+
assert_equal("retval of delete_access", impl.delete_at(4))
|
|
295
|
+
assert_equal([1, 2, 3, 4], impl.to_ary)
|
|
296
|
+
|
|
297
|
+
impl = klass.new([1, 2, 3, 4, 5])
|
|
298
|
+
assert_raise(NotImplementedError) { impl.delete_at(-1) }
|
|
299
|
+
assert_equal([1, 2, 3, 4, 5], impl.to_ary)
|
|
300
|
+
|
|
301
|
+
assert_raise(NotImplementedError) { impl.delete_at(-5) }
|
|
302
|
+
assert_equal([1, 2, 3, 4, 5], impl.to_ary)
|
|
303
|
+
|
|
304
|
+
assert_raise(NotImplementedError) { impl.delete_at(-6) }
|
|
305
|
+
assert_equal([1, 2, 3, 4, 5], impl.to_ary)
|
|
306
|
+
|
|
307
|
+
impl = klass.new([1, 2, 3, 4, 5])
|
|
308
|
+
assert_raise(ErrorForTest) { impl.delete_at(5) }
|
|
309
|
+
assert_raise(ErrorForTest) { impl.delete_at(6) }
|
|
310
|
+
assert_equal([1, 2, 3, 4, 5], impl.to_ary)
|
|
311
|
+
end
|
|
312
|
+
end
|
|
313
|
+
|
|
314
|
+
def test_fill_val
|
|
315
|
+
(FULL_IMPLS + NODELETE_IMPLS).each do |klass|
|
|
316
|
+
impl = klass.new([1, 2, 3, 4])
|
|
317
|
+
impl.fill(10)
|
|
318
|
+
assert_equal([10, 10, 10, 10], impl.to_ary)
|
|
319
|
+
|
|
320
|
+
impl = klass.new([1, 2, 3])
|
|
321
|
+
impl.fill { |i| -i }
|
|
322
|
+
assert_equal([0, -1, -2], impl.to_ary)
|
|
323
|
+
end
|
|
324
|
+
NOSIZE_IMPLS.each do |klass|
|
|
325
|
+
impl = klass.new([1, 2])
|
|
326
|
+
assert_raise(NotImplementedError) { impl.fill(0) }
|
|
327
|
+
assert_equal([1, 2], impl.to_ary)
|
|
328
|
+
|
|
329
|
+
assert_raise(NotImplementedError) { impl.fill { |i| i } }
|
|
330
|
+
assert_equal([1, 2], impl.to_ary)
|
|
331
|
+
end
|
|
332
|
+
end
|
|
333
|
+
|
|
334
|
+
def test_fill_start_length
|
|
335
|
+
(FULL_IMPLS + NODELETE_IMPLS).each do |klass|
|
|
336
|
+
impl = klass.new([1, 2, 3, 4])
|
|
337
|
+
impl.fill(10, 0, 2)
|
|
338
|
+
assert_equal([10, 10, 3, 4], impl.to_ary)
|
|
339
|
+
assert_equal(4, impl.size)
|
|
340
|
+
|
|
341
|
+
impl.fill(-1, 1, 2)
|
|
342
|
+
assert_equal([10, -1, -1, 4], impl.to_ary)
|
|
343
|
+
assert_equal(4, impl.size)
|
|
344
|
+
|
|
345
|
+
impl.fill(0, 2, 2)
|
|
346
|
+
assert_equal([10, -1, 0, 0], impl.to_ary)
|
|
347
|
+
assert_equal(4, impl.size)
|
|
348
|
+
|
|
349
|
+
impl.fill(1, 3, 3)
|
|
350
|
+
assert_equal([10, -1, 0, 1, 1, 1], impl.to_ary)
|
|
351
|
+
assert_equal(6, impl.size)
|
|
352
|
+
|
|
353
|
+
impl.fill(2, 6, 1)
|
|
354
|
+
assert_equal([10, -1, 0, 1, 1, 1, 2], impl.to_ary)
|
|
355
|
+
assert_equal(7, impl.size)
|
|
356
|
+
|
|
357
|
+
impl.fill(3, 8, 3)
|
|
358
|
+
assert_equal([10, -1, 0, 1, 1, 1, 2, nil, 3, 3, 3], impl.to_ary)
|
|
359
|
+
assert_equal(11, impl.size)
|
|
360
|
+
|
|
361
|
+
impl = klass.new([1, 2, 3, 4])
|
|
362
|
+
impl.fill(0, 2) { |i| 10 }
|
|
363
|
+
assert_equal([10, 10, 3, 4], impl.to_ary)
|
|
364
|
+
assert_equal(4, impl.size)
|
|
365
|
+
|
|
366
|
+
impl.fill(1, 2) { |i| -1 }
|
|
367
|
+
assert_equal([10, -1, -1, 4], impl.to_ary)
|
|
368
|
+
assert_equal(4, impl.size)
|
|
369
|
+
|
|
370
|
+
impl.fill(2, 2) { |i| i * 0 }
|
|
371
|
+
assert_equal([10, -1, 0, 0], impl.to_ary)
|
|
372
|
+
assert_equal(4, impl.size)
|
|
373
|
+
|
|
374
|
+
impl.fill(3, 3) { |i| 1 }
|
|
375
|
+
assert_equal([10, -1, 0, 1, 1, 1], impl.to_ary)
|
|
376
|
+
assert_equal(6, impl.size)
|
|
377
|
+
|
|
378
|
+
impl.fill(6, 1) { |i| i - 4 }
|
|
379
|
+
assert_equal([10, -1, 0, 1, 1, 1, 2], impl.to_ary)
|
|
380
|
+
assert_equal(7, impl.size)
|
|
381
|
+
|
|
382
|
+
impl.fill(8, 3) { |i| i }
|
|
383
|
+
assert_equal([10, -1, 0, 1, 1, 1, 2, nil, 8, 9, 10], impl.to_ary)
|
|
384
|
+
assert_equal(11, impl.size)
|
|
385
|
+
end
|
|
386
|
+
NOSIZE_IMPLS.each do |klass|
|
|
387
|
+
impl = klass.new([1, 2, 3, 4])
|
|
388
|
+
impl.fill(10, 0, 2)
|
|
389
|
+
assert_equal([10, 10, 3, 4], impl.to_ary)
|
|
390
|
+
|
|
391
|
+
impl.fill(-1, 1, 2)
|
|
392
|
+
assert_equal([10, -1, -1, 4], impl.to_ary)
|
|
393
|
+
|
|
394
|
+
impl.fill(0, 2, 2)
|
|
395
|
+
assert_equal([10, -1, 0, 0], impl.to_ary)
|
|
396
|
+
|
|
397
|
+
impl.fill(1, 3, 3)
|
|
398
|
+
assert_equal([10, -1, 0, 1, 1, 1], impl.to_ary)
|
|
399
|
+
|
|
400
|
+
impl.fill(2, 6, 1)
|
|
401
|
+
assert_equal([10, -1, 0, 1, 1, 1, 2], impl.to_ary)
|
|
402
|
+
|
|
403
|
+
impl.fill(3, 8, 3)
|
|
404
|
+
assert_equal([10, -1, 0, 1, 1, 1, 2, nil, 3, 3, 3], impl.to_ary)
|
|
405
|
+
|
|
406
|
+
impl = klass.new([1, 2, 3, 4])
|
|
407
|
+
impl.fill(0, 2) { |i| 10 }
|
|
408
|
+
assert_equal([10, 10, 3, 4], impl.to_ary)
|
|
409
|
+
|
|
410
|
+
impl.fill(1, 2) { |i| -1 }
|
|
411
|
+
assert_equal([10, -1, -1, 4], impl.to_ary)
|
|
412
|
+
|
|
413
|
+
impl.fill(2, 2) { |i| i * 0 }
|
|
414
|
+
assert_equal([10, -1, 0, 0], impl.to_ary)
|
|
415
|
+
|
|
416
|
+
impl.fill(3, 3) { |i| 1 }
|
|
417
|
+
assert_equal([10, -1, 0, 1, 1, 1], impl.to_ary)
|
|
418
|
+
|
|
419
|
+
impl.fill(6, 1) { |i| i - 4 }
|
|
420
|
+
assert_equal([10, -1, 0, 1, 1, 1, 2], impl.to_ary)
|
|
421
|
+
|
|
422
|
+
impl.fill(8, 3) { |i| i }
|
|
423
|
+
assert_equal([10, -1, 0, 1, 1, 1, 2, nil, 8, 9, 10], impl.to_ary)
|
|
424
|
+
end
|
|
425
|
+
end
|
|
426
|
+
|
|
427
|
+
def test_insert
|
|
428
|
+
FULL_IMPLS.each do |klass|
|
|
429
|
+
msg = "Error in #{klass.name}"
|
|
430
|
+
impl = klass.new([1, 2, 3])
|
|
431
|
+
impl.insert(2, -1, -2)
|
|
432
|
+
assert_equal([1, 2, -1, -2, 3], impl.to_ary, msg)
|
|
433
|
+
assert_equal(5, impl.size, msg)
|
|
434
|
+
|
|
435
|
+
impl = klass.new([1, 2, 3])
|
|
436
|
+
impl.insert(-2, -1, -2, -3)
|
|
437
|
+
assert_equal([1, 2, -1, -2, -3, 3], impl.to_ary, msg)
|
|
438
|
+
assert_equal(6, impl.size, msg)
|
|
439
|
+
end
|
|
440
|
+
NODELETE_IMPLS.each do |klass|
|
|
441
|
+
impl = klass.new([1, 2, 3])
|
|
442
|
+
assert_raise(NotImplementedError) { impl.insert(2, -1, -2) }
|
|
443
|
+
assert_equal([1, 2, 3], impl.to_ary)
|
|
444
|
+
assert_equal(3, impl.size)
|
|
445
|
+
end
|
|
446
|
+
NOSIZE_IMPLS.each do |klass|
|
|
447
|
+
msg = "Error in #{klass.name}"
|
|
448
|
+
impl = klass.new([1, 2, 3])
|
|
449
|
+
impl.insert(2, -1, -2)
|
|
450
|
+
assert_equal([1, 2, -1, -2, 3], impl.to_ary, msg)
|
|
451
|
+
|
|
452
|
+
impl = klass.new([1, 2, 3])
|
|
453
|
+
assert_raise(NotImplementedError) { impl.insert(-2, -1, -2, -3) }
|
|
454
|
+
assert_equal([1, 2, 3], impl.to_ary, msg)
|
|
455
|
+
end
|
|
456
|
+
end
|
|
457
|
+
|
|
458
|
+
def test_pop
|
|
459
|
+
(FULL_IMPLS + NODELETE_IMPLS).each do |klass|
|
|
460
|
+
impl = klass.new([1, 2, 3, 4, 5])
|
|
461
|
+
impl.pop
|
|
462
|
+
assert_equal([1, 2, 3, 4], impl.to_ary)
|
|
463
|
+
assert_equal(4, impl.size)
|
|
464
|
+
|
|
465
|
+
impl = klass.new([1, 2, 3, 4, 5])
|
|
466
|
+
impl.pop(3)
|
|
467
|
+
assert_equal([1, 2], impl.to_ary)
|
|
468
|
+
assert_equal(2, impl.size)
|
|
469
|
+
|
|
470
|
+
impl = klass.new([1, 2, 3, 4, 5])
|
|
471
|
+
impl.pop(100)
|
|
472
|
+
assert_equal([], impl.to_ary)
|
|
473
|
+
assert_equal(0, impl.size)
|
|
474
|
+
end
|
|
475
|
+
NOSIZE_IMPLS.each do |klass|
|
|
476
|
+
impl = klass.new([1, 2, 3, 4, 5])
|
|
477
|
+
impl.pop
|
|
478
|
+
assert_equal([1, 2, 3, 4], impl.to_ary)
|
|
479
|
+
|
|
480
|
+
impl = klass.new([1, 2, 3, 4, 5])
|
|
481
|
+
impl.pop(3)
|
|
482
|
+
assert_equal([1, 2], impl.to_ary)
|
|
483
|
+
|
|
484
|
+
impl = klass.new([1, 2, 3, 4, 5])
|
|
485
|
+
impl.pop(100)
|
|
486
|
+
assert_equal([], impl.to_ary)
|
|
487
|
+
end
|
|
488
|
+
end
|
|
489
|
+
|
|
490
|
+
def test_push
|
|
491
|
+
(FULL_IMPLS + NODELETE_IMPLS).each do |klass|
|
|
492
|
+
impl = klass.new([1])
|
|
493
|
+
impl.push(2, 3)
|
|
494
|
+
assert_equal([1, 2, 3], impl.to_ary)
|
|
495
|
+
assert_equal(3, impl.size)
|
|
496
|
+
end
|
|
497
|
+
NOSIZE_IMPLS.each do |klass|
|
|
498
|
+
impl = klass.new([])
|
|
499
|
+
assert_raise(NotImplementedError) { impl.push(2, 3) }
|
|
500
|
+
end
|
|
501
|
+
end
|
|
502
|
+
|
|
503
|
+
def test_replace
|
|
504
|
+
(FULL_IMPLS + NODELETE_IMPLS).each do |klass|
|
|
505
|
+
impl = klass.new([1, 2, 3])
|
|
506
|
+
impl.replace([4, 5, 6])
|
|
507
|
+
assert_equal([4, 5, 6], impl.to_ary)
|
|
508
|
+
assert_equal(3, impl.size)
|
|
509
|
+
|
|
510
|
+
impl = klass.new([1, 2, 3])
|
|
511
|
+
impl.replace([4, 5])
|
|
512
|
+
assert_equal([4, 5], impl.to_ary)
|
|
513
|
+
assert_equal(2, impl.size)
|
|
514
|
+
|
|
515
|
+
impl = klass.new([1, 2, 3])
|
|
516
|
+
impl.replace([4, 5, 6, 7])
|
|
517
|
+
assert_equal([4, 5, 6, 7], impl.to_ary)
|
|
518
|
+
assert_equal(4, impl.size)
|
|
519
|
+
end
|
|
520
|
+
NOSIZE_IMPLS.each do |klass|
|
|
521
|
+
impl = klass.new([1, 2])
|
|
522
|
+
assert_raise(NotImplementedError) { impl.replace([]) }
|
|
523
|
+
assert_equal([1, 2], impl.to_ary)
|
|
524
|
+
end
|
|
525
|
+
end
|
|
526
|
+
|
|
527
|
+
def test_shift
|
|
528
|
+
FULL_IMPLS.each do |klass|
|
|
529
|
+
msg = "Error in #{klass.name}"
|
|
530
|
+
impl = klass.new([1, 2, 3])
|
|
531
|
+
impl.shift
|
|
532
|
+
assert_equal([2, 3], impl.to_ary, msg)
|
|
533
|
+
assert_equal(2, impl.size, msg)
|
|
534
|
+
|
|
535
|
+
impl = klass.new([1, 2, 3])
|
|
536
|
+
impl.shift(2)
|
|
537
|
+
assert_equal([3], impl.to_ary, msg)
|
|
538
|
+
assert_equal(1, impl.size, msg)
|
|
539
|
+
|
|
540
|
+
impl = klass.new([1, 2, 3])
|
|
541
|
+
impl.shift(5)
|
|
542
|
+
assert_equal([], impl.to_ary, msg)
|
|
543
|
+
assert_equal(0, impl.size, msg)
|
|
544
|
+
end
|
|
545
|
+
NODELETE_IMPLS.each do |klass|
|
|
546
|
+
impl = klass.new([1, 2, 3, 4])
|
|
547
|
+
assert_raise(NotImplementedError) { impl.shift }
|
|
548
|
+
assert_raise(NotImplementedError) { impl.shift(2) }
|
|
549
|
+
end
|
|
550
|
+
NOSIZE_IMPLS.each do |klass|
|
|
551
|
+
msg = "Error in #{klass.name}"
|
|
552
|
+
impl = klass.new([1, 2, 3])
|
|
553
|
+
impl.shift
|
|
554
|
+
assert_equal([2, 3], impl.to_ary, msg)
|
|
555
|
+
|
|
556
|
+
impl = klass.new([1, 2, 3])
|
|
557
|
+
impl.shift(2)
|
|
558
|
+
assert_equal([3], impl.to_ary, msg)
|
|
559
|
+
|
|
560
|
+
impl = klass.new([1, 2, 3])
|
|
561
|
+
assert_raise(ErrorForTest) { impl.shift(5) }
|
|
562
|
+
assert_equal([], impl.to_ary, msg)
|
|
563
|
+
end
|
|
564
|
+
end
|
|
565
|
+
|
|
566
|
+
end
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: random-accessible
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.1.
|
|
4
|
+
version: 0.1.3
|
|
5
5
|
prerelease:
|
|
6
6
|
platform: ruby
|
|
7
7
|
authors:
|
|
@@ -9,26 +9,15 @@ authors:
|
|
|
9
9
|
autorequire:
|
|
10
10
|
bindir: bin
|
|
11
11
|
cert_chain: []
|
|
12
|
-
date: 2011-12-
|
|
12
|
+
date: 2011-12-07 00:00:00.000000000 Z
|
|
13
13
|
dependencies: []
|
|
14
|
-
description: ! 'RandomAccessible mixin provides all methods of Array
|
|
15
|
-
|
|
14
|
+
description: ! 'RandomAccessible mixin provides all methods of Array (regard as high-functioning
|
|
15
|
+
edition of Enumerable).
|
|
16
16
|
|
|
17
|
-
As Enumerable
|
|
18
|
-
|
|
17
|
+
As a class includes Enumerable must provide "each" method, a class includes RandomAccessible
|
|
18
|
+
must have some methods. (please see the document for detail).
|
|
19
19
|
|
|
20
20
|
|
|
21
|
-
- size (same as Array#size)
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
- read_access (similar to Array#[])
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
- replace_access (similar to Array#[]=)
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
- shrink (similar to Array#pop)
|
|
31
|
-
|
|
32
21
|
'
|
|
33
22
|
email:
|
|
34
23
|
- natsuki.kawai@gmail.com
|
|
@@ -37,13 +26,16 @@ extensions: []
|
|
|
37
26
|
extra_rdoc_files:
|
|
38
27
|
- README.en
|
|
39
28
|
files:
|
|
40
|
-
- lib
|
|
29
|
+
- lib/random-writable.rb
|
|
41
30
|
- lib/random-accessible.rb
|
|
42
31
|
- lib/common-traits.rb
|
|
43
|
-
- lib/random-writable.rb
|
|
44
32
|
- lib/random-readable.rb
|
|
45
|
-
-
|
|
33
|
+
- test/test-random-readable.rb
|
|
34
|
+
- test/test-random-writable.rb
|
|
46
35
|
- test/test-suite.rb
|
|
36
|
+
- test/test-random-accessible.rb
|
|
37
|
+
- BSDL
|
|
38
|
+
- README.en
|
|
47
39
|
homepage: https://github.com/natsuki14/random-accessible
|
|
48
40
|
licenses:
|
|
49
41
|
- Ruby's
|
|
@@ -66,7 +58,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
66
58
|
version: '0'
|
|
67
59
|
requirements: []
|
|
68
60
|
rubyforge_project:
|
|
69
|
-
rubygems_version: 1.8.
|
|
61
|
+
rubygems_version: 1.8.11
|
|
70
62
|
signing_key:
|
|
71
63
|
specification_version: 3
|
|
72
64
|
summary: RandomAccessible mixin provides all methods of Array.
|