flex_array 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -3,118 +3,491 @@ require 'minitest/autorun'
3
3
 
4
4
  class FlexArrayEachTester < MiniTest::Unit::TestCase
5
5
  $do_this_only_one_time = "" unless defined? $do_this_only_one_time
6
-
6
+
7
7
  def initialize(*all)
8
8
  if $do_this_only_one_time != __FILE__
9
9
  puts
10
- puts "Running test file: #{File.split(__FILE__)[1]}"
10
+ puts "Running test file: #{File.split(__FILE__)[1]}"
11
11
  $do_this_only_one_time = __FILE__
12
12
  end
13
-
13
+
14
14
  super(*all)
15
15
  end
16
16
 
17
- def test_that_the_each_verbs_work
18
- idx = []
19
- q = FlexArray.new([3, 3]) {|i| idx << i.clone; i[0]*i[1]}
20
- it = q.array_data.each
21
-
22
- q.each do |v|
17
+ def test_each
18
+ d = [0,1,2,3,4,5,6,7,8]
19
+ q = FlexArray.new([3, 3]) {|i| i[0] * 3 + i[1]}
20
+ it = d.each
21
+
22
+ result = q.each do |v|
23
23
  assert_equal(it.next, v)
24
24
  end
25
-
25
+
26
+ assert_equal(result, q)
27
+
26
28
  it = q.each
27
- q.array_data.each do |v|
29
+
30
+ result = d.each do |v|
28
31
  assert_equal(it.next, v)
29
32
  end
33
+ end
30
34
 
31
- it = q.array_data.each
32
- it2 = idx.each
33
- q.each_with_index do |v, i|
35
+ def test_select_each
36
+ d = [0,1,2,3,4,5,6,7,8]
37
+ q = FlexArray.new([3, 3]) {|i| i[0] * 3 + i[1]}
38
+ it = d.each
39
+
40
+ result = q.select_each([:all]) do |v|
41
+ assert_equal(it.next, v)
42
+ end
43
+
44
+ assert_equal(result, q)
45
+
46
+ it = q.select_each([:all])
47
+ d.each do |v|
34
48
  assert_equal(it.next, v)
35
- assert_equal(it2.next, i)
36
- end
37
-
38
- it = q.each_with_index
39
- it2 = idx.each
40
- q.array_data.each do |v|
41
- value, index = it.next
42
- assert_equal(value, v)
43
- assert_equal(it2.next, index)
44
- end
45
-
46
- it = (0...9).each
47
- it2 = idx.each
48
- q._each_raw do |d, index, p|
49
- assert_equal(d.object_id, q.array_data.object_id)
50
- assert_equal(it2.next, index)
51
- assert_equal(it.next, p)
52
- end
53
-
54
- it = q._each_raw
55
- it2 = idx.each
56
- (0...9).each do |i|
57
- d, index, p = it.next
58
- assert_equal(d.object_id, q.array_data.object_id)
59
- assert_equal(it2.next, index)
60
- assert_equal(i, p)
61
- end
62
- end
63
-
64
- def test_that_the_indexed_each_verbs_work
65
- q = FlexArray.new([3, 3]) {|i| i[0]*i[1]}
66
- a = [0, 1, 2]
67
- b = [[1,0], [1,1], [1,2]]
68
- it = a.each
69
-
70
- q.each([1, :all]) do |v|
49
+ end
50
+
51
+ it = d.each
52
+
53
+ result = q.select_each([:all, :all]) do |v|
71
54
  assert_equal(it.next, v)
72
55
  end
73
-
74
- it = q.each([1, :all])
75
- a.each do |v|
56
+
57
+ d = [0,1,2]
58
+ it = d.each
59
+
60
+ result = q.select_each([0, :all]) do |v|
76
61
  assert_equal(it.next, v)
77
62
  end
78
-
79
- it = a.each
80
- it2 = b.each
81
- q.each_with_index([1, :all]) do |v, i|
63
+
64
+ d = [0,3,6]
65
+ it = d.each
66
+
67
+ result = q.select_each([:all, 0]) do |v|
82
68
  assert_equal(it.next, v)
83
- assert_equal(it2.next, i)
84
- end
85
-
86
- it = q.each_with_index([1, :all])
87
- it2 = b.each
88
- a.each do |v|
89
- value, index = it.next
90
- assert_equal(value, v)
91
- assert_equal(it2.next, index)
92
- end
93
-
94
- c = [3, 4, 5]
95
- it = c.each
96
- it2 = b.each
97
- q._each_raw([1, :all]) do |d, index, p|
98
- assert_equal(d.object_id, q.array_data.object_id)
99
- assert_equal(it2.next, index)
100
- assert_equal(it.next, p)
101
- end
102
-
103
- it = q._each_raw([1, :all])
104
- it2 = b.each
105
- c.each do |i|
106
- d, index, p = it.next
107
- assert_equal(d.object_id, q.array_data.object_id)
108
- assert_equal(it2.next, index)
109
- assert_equal(i, p)
110
- end
111
- end
112
-
113
- def test_that_flex_array_cycle_works
114
- a = FlexArray.new_from(3, [1,2,3])
115
- r = []
116
- it = a.cycle
117
- 10.times {r << it.next}
118
- assert_equal([1,2,3,1,2,3,1,2,3,1], r)
69
+ end
70
+ end
71
+
72
+ def test_each_with_index
73
+ d1 = [0,1,2,3,4,5,6,7,8]
74
+ d2 = [[0,0], [0,1], [0,2],
75
+ [1,0], [1,1], [1,2],
76
+ [2,0], [2,1], [2,2]]
77
+ q = FlexArray.new([3, 3]) {|i| i[0] * 3 + i[1]}
78
+ i1 = d1.each
79
+ i2 = d2.each
80
+
81
+ q.each_with_index do |v, i|
82
+ assert_equal(v, i1.next)
83
+ assert_equal(i, i2.next)
84
+ end
85
+
86
+ iq = q.each_with_index
87
+ i2 = d2.each
88
+
89
+ d1.each do |v|
90
+ a,b = iq.next
91
+ assert_equal(a, v)
92
+ assert_equal(b, i2.next)
93
+ end
94
+ end
95
+
96
+ def test_select_each_with_index
97
+ d1 = [0,1,2,3,4,5,6,7,8]
98
+ d2 = [[0,0], [0,1], [0,2],
99
+ [1,0], [1,1], [1,2],
100
+ [2,0], [2,1], [2,2]]
101
+ q = FlexArray.new([3, 3]) {|i| i[0] * 3 + i[1]}
102
+ i1 = d1.each
103
+ i2 = d2.each
104
+
105
+ q.select_each_with_index([:all]) do |v, i|
106
+ assert_equal(v, i1.next)
107
+ assert_equal(i, i2.next)
108
+ end
109
+
110
+ iq = q.select_each_with_index([:all])
111
+ i2 = d2.each
112
+
113
+ d1.each do |v|
114
+ a,b = iq.next
115
+ assert_equal(a, v)
116
+ assert_equal(b, i2.next)
117
+ end
118
+
119
+ i1 = d1.each
120
+ i2 = d2.each
121
+
122
+ q.select_each_with_index([:all, :all]) do |v, i|
123
+ assert_equal(v, i1.next)
124
+ assert_equal(i, i2.next)
125
+ end
126
+
127
+ iq = q.select_each_with_index([:all, :all])
128
+ i2 = d2.each
129
+
130
+ d1.each do |v|
131
+ a,b = iq.next
132
+ assert_equal(a, v)
133
+ assert_equal(b, i2.next)
134
+ end
135
+
136
+ d1 = [0,1,2]
137
+ d2 = [[0,0], [0,1], [0,2]]
138
+
139
+ i1 = d1.each
140
+ i2 = d2.each
141
+
142
+ q.select_each_with_index([0, :all]) do |v, i|
143
+ assert_equal(v, i1.next)
144
+ assert_equal(i, i2.next)
145
+ end
146
+
147
+ d1 = [0,3,6]
148
+ d2 = [[0,0], [1,0], [2,0]]
149
+
150
+ i1 = d1.each
151
+ i2 = d2.each
152
+
153
+ q.select_each_with_index([:all, 0]) do |v, i|
154
+ assert_equal(v, i1.next)
155
+ assert_equal(i, i2.next)
156
+ end
157
+ end
158
+
159
+ def test_each_raw
160
+ d0 = [0, 1, 2, 3, 4, 5, 6, 7, 8]
161
+ d1 = [100, 101, 102, 103, 104, 105, 106, 107, 108]
162
+ d2 = [[0,0], [0,1], [0,2],
163
+ [1,0], [1,1], [1,2],
164
+ [2,0], [2,1], [2,2]]
165
+ q = FlexArray.new([3, 3]) {|i| i[0] * 3 + i[1] + 100}
166
+ i0 = d0.each
167
+ i1 = d1.each
168
+ i2 = d2.each
169
+
170
+ q._each_raw do |i, posn|
171
+ assert_equal(posn, i0.next)
172
+ assert_equal(q.array_data[posn], i1.next)
173
+ assert_equal(i, i2.next)
174
+ end
175
+
176
+ iq = q._each_raw
177
+ i1 = d1.each
178
+ i2 = d2.each
179
+
180
+ d0.each do |v|
181
+ i, posn = iq.next
182
+ assert_equal(posn, v)
183
+ assert_equal(q.array_data[posn], i1.next)
184
+ assert_equal(i, i2.next)
185
+ end
186
+ end
187
+
188
+ def test_select_each_raw
189
+ d0 = [0, 1, 2, 3, 4, 5, 6, 7, 8]
190
+ d1 = [100, 101, 102, 103, 104, 105, 106, 107, 108]
191
+ d2 = [[0,0], [0,1], [0,2],
192
+ [1,0], [1,1], [1,2],
193
+ [2,0], [2,1], [2,2]]
194
+ q = FlexArray.new([3, 3]) {|i| i[0] * 3 + i[1] + 100}
195
+ i0 = d0.each
196
+ i1 = d1.each
197
+ i2 = d2.each
198
+
199
+ q._select_each_raw([:all]) do |i, posn|
200
+ assert_equal(posn, i0.next)
201
+ assert_equal(q.array_data[posn], i1.next)
202
+ assert_equal(i, i2.next)
203
+ end
204
+
205
+ i0 = d0.each
206
+ i1 = d1.each
207
+ i2 = d2.each
208
+
209
+ q._select_each_raw([:all, :all]) do |i, posn|
210
+ assert_equal(posn, i0.next)
211
+ assert_equal(q.array_data[posn], i1.next)
212
+ assert_equal(i, i2.next)
213
+ end
214
+
215
+ d0 = [0, 1, 2]
216
+ d1 = [100, 101, 102]
217
+ d2 = [[0,0], [0,1], [0,2]]
218
+
219
+ i0 = d0.each
220
+ i1 = d1.each
221
+ i2 = d2.each
222
+
223
+ q._select_each_raw([0, :all]) do |i, posn|
224
+ assert_equal(posn, i0.next)
225
+ assert_equal(q.array_data[posn], i1.next)
226
+ assert_equal(i, i2.next)
227
+ end
228
+
229
+ d0 = [0, 3, 6]
230
+ d1 = [100, 103, 106]
231
+ d2 = [[0,0], [1,0], [2,0]]
232
+
233
+ i0 = d0.each
234
+ i1 = d1.each
235
+ i2 = d2.each
236
+
237
+ q._select_each_raw([:all, 0]) do |i, posn|
238
+ assert_equal(posn, i0.next)
239
+ assert_equal(q.array_data[posn], i1.next)
240
+ assert_equal(i, i2.next)
241
+ end
242
+ end
243
+
244
+ def test_cycle
245
+ d = [0,1,2,3,4,5,6,7,8,0,1,2]
246
+ q = FlexArray.new([3, 3]) {|i| i[0] * 3 + i[1]}
247
+ i = 0
248
+
249
+ q.cycle do |v|
250
+ assert_equal(d[i], v)
251
+ i += 1
252
+ break if i == 12
253
+ end
254
+
255
+ iq = q.cycle
256
+
257
+ d.each do |v|
258
+ assert_equal(iq.next, v)
259
+ end
260
+ end
261
+
262
+ def test_select_cycle
263
+ d = [0,1,2,3,4,5,6,7,8,0,1,2]
264
+ q = FlexArray.new([3, 3]) {|i| i[0] * 3 + i[1]}
265
+ i = 0
266
+
267
+ q.select_cycle([:all]) do |v|
268
+ assert_equal(d[i], v)
269
+ i += 1
270
+ break if i == 12
271
+ end
272
+
273
+ iq = q.select_cycle([:all])
274
+
275
+ d.each do |v|
276
+ assert_equal(iq.next, v)
277
+ end
278
+
279
+ i = 0
280
+
281
+ q.select_cycle([:all, :all]) do |v|
282
+ assert_equal(d[i], v)
283
+ i += 1
284
+ break if i == 12
285
+ end
286
+
287
+ iq = q.select_cycle([:all, :all])
288
+
289
+ d.each do |v|
290
+ assert_equal(iq.next, v)
291
+ end
292
+
293
+ d = [0,1,2,0,1,2,0,1,2,0,1,2]
294
+ i = 0
295
+
296
+ q.select_cycle([0, :all]) do |v|
297
+ assert_equal(d[i], v)
298
+ i += 1
299
+ break if i == 12
300
+ end
301
+
302
+ iq = q.select_cycle([0, :all])
303
+
304
+ d.each do |v|
305
+ assert_equal(iq.next, v)
306
+ end
307
+
308
+ d = [0,3,6,0,3,6,0,3,6,0,3,6]
309
+ i = 0
310
+
311
+ q.select_cycle([:all, 0]) do |v|
312
+ assert_equal(d[i], v)
313
+ i += 1
314
+ break if i == 12
315
+ end
316
+
317
+ iq = q.select_cycle([:all, 0])
318
+
319
+ d.each do |v|
320
+ assert_equal(iq.next, v)
321
+ end
322
+ end
323
+
324
+ def test_flatten_collect
325
+ d = [0,1,4,9,16,25,36,49,64]
326
+ q = FlexArray.new([3, 3]) {|i| i[0] * 3 + i[1]}
327
+
328
+ b = q.flatten_collect {|v| v * v }
329
+ assert_equal(d, b)
330
+ end
331
+
332
+ def test_select_flatten_collect
333
+ d = [0,1,4,9,16,25,36,49,64]
334
+ q = FlexArray.new([3, 3]) {|i| i[0] * 3 + i[1]}
335
+
336
+ b = q.select_flatten_collect([:all]) {|v| v * v }
337
+ assert_equal(d, b)
338
+
339
+ b = q.select_flatten_collect([:all, :all]) {|v| v * v }
340
+ assert_equal(d, b)
341
+
342
+ d = [0,1,4]
343
+ b = q.select_flatten_collect([0, :all]) {|v| v * v }
344
+ assert_equal(d, b)
345
+
346
+ d = [0,9,36]
347
+ b = q.select_flatten_collect([:all, 0]) {|v| v * v }
348
+ assert_equal(d, b)
349
+ end
350
+
351
+
352
+
353
+
354
+
355
+ def test_collect
356
+ d = [0,1,4,9,16,25,36,49,64]
357
+ q = FlexArray.new([3, 3]) {|i| i[0] * 3 + i[1]}
358
+ a = q.collect {|v| v * v }
359
+ assert_equal(d, a.array_data)
360
+ end
361
+
362
+ def test_select_collect
363
+ d = [0,1,4,9,16,25,36,49,64]
364
+ q = FlexArray.new([3, 3]) {|i| i[0] * 3 + i[1]}
365
+ a = q.select_collect([:all]) {|v| v * v }
366
+ assert_equal(d, a.array_data)
367
+
368
+ d = [0,1,16,81,256,625,1296,2401,4096]
369
+ b = a.select_collect([:all, :all]) {|v| v * v }
370
+ assert_equal(d, b.array_data)
371
+
372
+ d = [0,1,256,81,256,625,1296,2401,4096]
373
+ c = b.select_collect([0, :all]) {|v| v * v }
374
+ assert_equal(d, c.array_data)
375
+
376
+ d = [0,1,256,6561,256,625,1679616,2401,4096]
377
+ e = c.select_collect([:all, 0]) {|v| v * v }
378
+ assert_equal(d, e.array_data)
379
+ end
380
+
381
+ def test_collect_em
382
+ d = [0,1,4,9,16,25,36,49,64]
383
+ q = FlexArray.new([3, 3]) {|i| i[0] * 3 + i[1]}
384
+ q.collect! {|v| v * v }
385
+ assert_equal(d, q.array_data)
386
+ end
387
+
388
+ def test_select_collect_em
389
+ d = [0,1,4,9,16,25,36,49,64]
390
+ q = FlexArray.new([3, 3]) {|i| i[0] * 3 + i[1]}
391
+ q.select_collect!([:all]) {|v| v * v }
392
+ assert_equal(d, q.array_data)
393
+
394
+ d = [0,1,16,81,256,625,1296,2401,4096]
395
+ q.select_collect!([:all, :all]) {|v| v * v }
396
+ assert_equal(d, q.array_data)
397
+
398
+ d = [0,1,256,81,256,625,1296,2401,4096]
399
+ q.select_collect!([0, :all]) {|v| v * v }
400
+ assert_equal(d, q.array_data)
401
+
402
+ d = [0,1,256,6561,256,625,1679616,2401,4096]
403
+ q.select_collect!([:all, 0]) {|v| v * v }
404
+ assert_equal(d, q.array_data)
405
+ end
406
+
407
+ def test_find_index
408
+ q = FlexArray.new([3, 3]) {|i| i[0] * 3 + i[1] + 100}
409
+ i = q.find_index(107)
410
+ assert_equal([2, 1], i)
411
+
412
+ i = q.find_index { |v| v > 106 }
413
+ assert_equal([2, 1], i)
414
+
415
+ i = q.find_index(300)
416
+ assert_equal(nil, i)
417
+
418
+ i = q.find_index { |v| v > 206 }
419
+ assert_equal(nil, i)
420
+ end
421
+
422
+ def test_select_find_index
423
+ q = FlexArray.new([3, 3]) {|i| i[0] * 3 + i[1] + 100}
424
+ i = q.select_find_index([:all], 107)
425
+ assert_equal([2, 1], i)
426
+
427
+ i = q.select_find_index([:all, :all], 107)
428
+ assert_equal([2, 1], i)
429
+
430
+ i = q.select_find_index([0, :all], 107)
431
+ assert_equal(nil, i)
432
+
433
+ i = q.select_find_index([1, :all], 107)
434
+ assert_equal(nil, i)
435
+
436
+ i = q.select_find_index([2, :all], 107)
437
+ assert_equal([2, 1], i)
438
+
439
+ i = q.select_find_index([:all, 0], 107)
440
+ assert_equal(nil, i)
441
+
442
+ i = q.select_find_index([:all, 1], 107)
443
+ assert_equal([2, 1], i)
444
+
445
+ i = q.select_find_index([:all, 2], 107)
446
+ assert_equal(nil, i)
447
+ end
448
+
449
+ def test_find_indexes
450
+ q = FlexArray.new([3, 3]) {|i| i[0] * 3 + i[1] + 100}
451
+ i = q.find_indexes(107)
452
+ assert_equal([[2, 1]], i)
453
+
454
+ i = q.find_indexes { |v| v == 107 }
455
+ assert_equal([[2, 1]], i)
456
+
457
+ i = q.find_indexes { |v| v > 106 }
458
+ assert_equal([[2, 1], [2, 2]], i)
459
+
460
+ i = q.find_indexes(307)
461
+ assert_equal([], i)
462
+
463
+ i = q.find_indexes { |v| v > 406 }
464
+ assert_equal([], i)
465
+ end
466
+
467
+ def test_select_find_indexes
468
+ q = FlexArray.new([3, 3]) {|i| i[0] * 3 + i[1] + 100}
469
+ i = q.select_find_indexes([:all], 107)
470
+ assert_equal([[2, 1]], i)
471
+
472
+ i = q.select_find_indexes([:all, :all], 107)
473
+ assert_equal([[2, 1]], i)
474
+
475
+ i = q.select_find_indexes([0, :all], 107)
476
+ assert_equal([], i)
477
+
478
+ i = q.select_find_indexes([1, :all], 107)
479
+ assert_equal([], i)
480
+
481
+ i = q.select_find_indexes([2, :all], 107)
482
+ assert_equal([[2, 1]], i)
483
+
484
+ i = q.select_find_indexes([:all, 0], 107)
485
+ assert_equal([], i)
486
+
487
+ i = q.select_find_indexes([:all, 1], 107)
488
+ assert_equal([[2, 1]], i)
489
+
490
+ i = q.select_find_indexes([:all, 2], 107)
491
+ assert_equal([], i)
119
492
  end
120
493
  end