cast 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,2485 @@
1
+ require 'common'
2
+ require 'stringio'
3
+
4
+ ###
5
+ ### ##################################################################
6
+ ###
7
+ ### Tests for NodeList classes.
8
+ ###
9
+ ### ##################################################################
10
+ ###
11
+
12
+ ###
13
+ ### NodeListTest classes are abstract, inherited by classes which
14
+ ### define List() to return a NodeList class. The tests defined in
15
+ ### NodeListTest subclasses are therefore used to test all NodeList
16
+ ### classes.
17
+ ###
18
+ module NodeListTest
19
+ @@submodules = []
20
+ def self.submodules
21
+ @@submodules
22
+ end
23
+ def self.included m
24
+ @@submodules << m
25
+ end
26
+ def setup
27
+ ## []
28
+ @empty = _List[]
29
+
30
+ ## [a]
31
+ a = C::Int.new
32
+ @one = _List[a]
33
+ @one_els = [a]
34
+
35
+ ## [a, b]
36
+ a, b = 2.of{C::Int.new}
37
+ @two = _List[a, b]
38
+ @two_els = [a, b]
39
+
40
+ ## [a, b, c]
41
+ a, b, c = 3.of{C::Int.new}
42
+ @three = _List[a, b, c]
43
+ @three_els = [a, b, c]
44
+
45
+ ## [a, b, c, d]
46
+ a, b, c, d = 4.of{C::Int.new}
47
+ @four = _List[a, b, c, d]
48
+ @four_els = [a, b, c, d]
49
+
50
+ ## [[a,b], [c,d], [e,f], [g,h]]
51
+ a, b, c, d, e, f, g, h = 8.of{C::Int.new}
52
+ l0 = _List[a,b]
53
+ l1 = _List[c,d]
54
+ l2 = _List[e,f]
55
+ l3 = _List[g,h]
56
+ @two_by_four = _List[l0, l1, l2, l3]
57
+ @two_by_four_els = [l0, l1, l2, l3, a, b, c, d, e, f, g, h]
58
+
59
+ ## [a, [b,c], [d, [e], [], [[]]]]
60
+ a, b, c, d, e = 5.of{C::Int.new}
61
+ l1 = _List[b,c]
62
+ l21 = _List[e]
63
+ l22 = _List[]
64
+ l230 = _List[]
65
+ l23 = _List[l230]
66
+ l2 = _List[d, l21, l22, l23]
67
+ @big = _List[a, l1, l2]
68
+ @big_els = [l1, l2, l21, l22, l23, l230, a, b, c, d, e]
69
+ end
70
+
71
+ attr_accessor *%w[empty empty_els
72
+ one one_els
73
+ two two_els
74
+ three three_els
75
+ four four_els
76
+ two_by_four two_by_four_els
77
+ big big_els
78
+ ]
79
+ end
80
+
81
+ module NodeListInitializeTest
82
+ include NodeListTest
83
+ def test_initialize
84
+ list = _List.new
85
+ assert_equal(0, list.length)
86
+ end
87
+
88
+ def test_from_attached
89
+ a1, a2, a3, a4 = 4.of{C::Int.new}
90
+ a = _List[a1, a2, a3, a4]
91
+
92
+ b = _List[a1, a2, a3, a4]
93
+ assert_same_list([a1, a2, a3, a4], a)
94
+ assert_equal(4, b.length)
95
+ assert_copy(a1, b[0])
96
+ assert_copy(a2, b[1])
97
+ assert_copy(a3, b[2])
98
+ assert_copy(a4, b[3])
99
+ end
100
+ end
101
+
102
+ ###
103
+ ### Tests dup, clone.
104
+ ###
105
+ module NodeListCopyTest
106
+ include NodeListTest
107
+ def test_copy
108
+ ## empty
109
+ a = empty
110
+ b = empty.dup
111
+ c = empty.clone
112
+ ##
113
+ assert_copy a, b
114
+ ##
115
+ assert_copy a, c
116
+
117
+ ## one
118
+ a = one
119
+ b = one.dup
120
+ c = one.clone
121
+ ##
122
+ assert_copy a, b
123
+ assert_copy a[0], b[0]
124
+ ##
125
+ assert_copy a, c
126
+ assert_copy a[0], c[0]
127
+
128
+ ## two
129
+ a = two
130
+ b = two.dup
131
+ c = two.clone
132
+ ##
133
+ assert_copy a, b
134
+ assert_copy a[0], b[0]
135
+ assert_copy a[1], b[1]
136
+ ##
137
+ assert_copy a, c
138
+ assert_copy a[0], c[0]
139
+ assert_copy a[1], c[1]
140
+
141
+ ## three
142
+ a = three
143
+ b = three.dup
144
+ c = three.clone
145
+ ##
146
+ assert_copy a, b
147
+ assert_copy a[0], b[0]
148
+ assert_copy a[1], b[1]
149
+ assert_copy a[2], b[2]
150
+ ##
151
+ assert_copy a, c
152
+ assert_copy a[0], c[0]
153
+ assert_copy a[1], c[1]
154
+ assert_copy a[2], c[2]
155
+
156
+ ## four
157
+ a = four
158
+ b = four.dup
159
+ c = four.clone
160
+ ##
161
+ assert_copy a, b
162
+ assert_copy a[0], b[0]
163
+ assert_copy a[1], b[1]
164
+ assert_copy a[2], b[2]
165
+ assert_copy a[3], b[3]
166
+ ##
167
+ assert_copy a, c
168
+ assert_copy a[0], c[0]
169
+ assert_copy a[1], c[1]
170
+ assert_copy a[2], c[2]
171
+ assert_copy a[3], c[3]
172
+
173
+ ## two_by_four
174
+ a = two_by_four
175
+ b = two_by_four.dup
176
+ c = two_by_four.clone
177
+ ##
178
+ assert_copy a, b
179
+ assert_copy a[0], b[0]
180
+ assert_copy a[1], b[1]
181
+ assert_copy a[2], b[2]
182
+ assert_copy a[3], b[3]
183
+ assert_copy a[0][0], b[0][0]
184
+ assert_copy a[0][1], b[0][1]
185
+ assert_copy a[1][0], b[1][0]
186
+ assert_copy a[1][1], b[1][1]
187
+ assert_copy a[2][0], b[2][0]
188
+ assert_copy a[2][1], b[2][1]
189
+ assert_copy a[3][0], b[3][0]
190
+ assert_copy a[3][1], b[3][1]
191
+ ##
192
+ assert_copy a, c
193
+ assert_copy a[0], c[0]
194
+ assert_copy a[1], c[1]
195
+ assert_copy a[2], c[2]
196
+ assert_copy a[3], c[3]
197
+ assert_copy a[0][0], c[0][0]
198
+ assert_copy a[0][1], c[0][1]
199
+ assert_copy a[1][0], c[1][0]
200
+ assert_copy a[1][1], c[1][1]
201
+ assert_copy a[2][0], c[2][0]
202
+ assert_copy a[2][1], c[2][1]
203
+ assert_copy a[3][0], c[3][0]
204
+ assert_copy a[3][1], c[3][1]
205
+
206
+ ## big -- [a, [b,c], [d, [e], [], [[]]]]
207
+ a = big
208
+ b = big.dup
209
+ c = big.clone
210
+ ##
211
+ assert_copy a, b
212
+ assert_copy a[0], b[0]
213
+ assert_copy a[1], b[1]
214
+ assert_copy a[2], b[2]
215
+ assert_copy a[1][0], b[1][0]
216
+ assert_copy a[1][1], b[1][1]
217
+ assert_copy a[2][0], b[2][0]
218
+ assert_copy a[2][1], b[2][1]
219
+ assert_copy a[2][1][0], b[2][1][0]
220
+ assert_copy a[2][2], b[2][2]
221
+ assert_copy a[2][3], b[2][3]
222
+ assert_copy a[2][3][0], b[2][3][0]
223
+ ##
224
+ assert_copy a, c
225
+ assert_copy a[0], c[0]
226
+ assert_copy a[1], c[1]
227
+ assert_copy a[2], c[2]
228
+ assert_copy a[1][0], c[1][0]
229
+ assert_copy a[1][1], c[1][1]
230
+ assert_copy a[2][0], c[2][0]
231
+ assert_copy a[2][1], c[2][1]
232
+ assert_copy a[2][1][0], c[2][1][0]
233
+ assert_copy a[2][2], c[2][2]
234
+ assert_copy a[2][3], c[2][3]
235
+ assert_copy a[2][3][0], c[2][3][0]
236
+ end
237
+ end
238
+
239
+ ###
240
+ ### Tests ==, eql?, hash.
241
+ ###
242
+ module NodeListEqualTest
243
+ include NodeListTest
244
+ def assert_eq a, b
245
+ assert(a == b)
246
+ assert(a.eql?(b))
247
+ assert(a.hash == b.hash)
248
+ end
249
+ def assert_not_eq a, b
250
+ assert(!(a == b))
251
+ assert(!a.eql?(b))
252
+ end
253
+ def test_eq
254
+ assert_eq(empty, empty)
255
+ assert_eq(one, one)
256
+ assert_eq(two, two)
257
+ assert_eq(three, three)
258
+ assert_eq(four, four)
259
+ assert_eq(two_by_four, two_by_four)
260
+ assert_eq(big, big)
261
+
262
+ assert_not_eq(empty, one)
263
+ assert_not_eq(one, empty)
264
+ assert_not_eq(one, two)
265
+ assert_not_eq(two, one)
266
+ assert_not_eq(two, three)
267
+ assert_not_eq(three, two)
268
+
269
+ ## []
270
+ empty2 = _List[]
271
+ assert_eq(empty, empty2)
272
+
273
+ ## [a]
274
+ a = C::Int.new
275
+ one2 = _List[a]
276
+ assert_eq(one, one2)
277
+
278
+ ## [a, b]
279
+ a, b = 2.of{C::Int.new}
280
+ two2 = _List[a, b]
281
+ assert_eq(two, two2)
282
+
283
+ ## [a, b, c]
284
+ a, b, c = 3.of{C::Int.new}
285
+ three2 = _List[a, b, c]
286
+ assert_eq(three, three2)
287
+
288
+ ## [a, b, c, d]
289
+ a, b, c, d = 4.of{C::Int.new}
290
+ four2 = _List[a, b, c, d]
291
+ assert_eq(four, four2)
292
+
293
+ ## [[a,b], [c,d], [e,f], [g,h]]
294
+ a, b, c, d, e, f, g, h = 8.of{C::Int.new}
295
+ l0 = _List[a,b]
296
+ l1 = _List[c,d]
297
+ l2 = _List[e,f]
298
+ l3 = _List[g,h]
299
+ two_by_four2 = _List[l0, l1, l2, l3]
300
+ assert_eq(two_by_four, two_by_four2)
301
+
302
+ ## [a, [b,c], [d, [e], [], [[]]]]
303
+ a, b, c, d, e = 5.of{C::Int.new}
304
+ l1 = _List[b,c]
305
+ l21 = _List[e]
306
+ l22 = _List[]
307
+ l230 = _List[]
308
+ l23 = _List[l230]
309
+ l2 = _List[d, l21, l22, l23]
310
+ big2 = _List[a, l1, l2]
311
+ assert_eq(big, big2)
312
+ end
313
+ end
314
+
315
+ module NodeListWalkTest
316
+ include NodeListTest
317
+ ###
318
+ ### Collect and return the args yielded to `node.send(method)' as an
319
+ ### Array, each element of which is an array of args yielded.
320
+ ###
321
+ def yields method, node, exp
322
+ ret = []
323
+ out = node.send(method) do |*args|
324
+ ret << args
325
+ end
326
+ assert_same(exp, out)
327
+ return ret
328
+ end
329
+
330
+ ###
331
+ ### Assert exp and out are equal, where elements are compared with
332
+ ### Array#same_list?. That is, exp[i].same_list?(out[i]) for all i.
333
+ ###
334
+ def assert_equal_yields exp, out
335
+ if exp.zip(out).all?{|a,b| a.same_list?(b)}
336
+ assert(true)
337
+ else
338
+ flunk("walk not equal: #{walk_str(out)} (expected #{walk_str(exp)})")
339
+ end
340
+ end
341
+ def walk_str walk
342
+ walk.is_a? ::Array or
343
+ raise "walk_str: expected ::Array"
344
+ if walk.empty?
345
+ return '[]'
346
+ else
347
+ s = StringIO.new
348
+ s.puts '['
349
+ walk.each do |(ev, node)|
350
+ nodestr = node.class.name << '(' << node.object_id.to_s << "): " << node.to_s
351
+ if nodestr.length > 70
352
+ nodestr[68..-1] = '...'
353
+ end
354
+ s.puts " [#{ev.to_s.rjust(10)}, #{nodestr}]"
355
+ end
356
+ s.puts ']'
357
+ return s.string
358
+ end
359
+ end
360
+
361
+ ###
362
+ ### ----------------------------------------------------------------
363
+ ### each, reverse_each
364
+ ### ----------------------------------------------------------------
365
+ ###
366
+ def iter_str iter
367
+ iter.is_a? ::Array or
368
+ raise "iter_str: expected ::Array"
369
+ if iter.empty?
370
+ return '[]'
371
+ else
372
+ s = StringIO.new
373
+ s.puts '['
374
+ iter.each do |node|
375
+ nodestr = node.class.name << '(' << node.object_id.to_s << "): " << node.to_s
376
+ if nodestr.length > 70
377
+ nodestr[68..-1] = '...'
378
+ end
379
+ s.puts " #{nodestr}"
380
+ end
381
+ s.puts ']'
382
+ return s.string
383
+ end
384
+ end
385
+ def check_iter node, exp
386
+ exp.map!{|n| [n]}
387
+
388
+ out = yields(:each, node, node)
389
+ assert_equal_yields exp, out
390
+
391
+ out = yields(:reverse_each, node, node)
392
+ exp.reverse!
393
+ assert_equal_yields exp, out
394
+ end
395
+
396
+ def test_each
397
+ ## empty
398
+ check_iter(empty, [])
399
+
400
+ ## one
401
+ a = *one_els
402
+ check_iter(one, [a])
403
+
404
+ ## two
405
+ a, b = *two_els
406
+ check_iter(two, [a, b])
407
+
408
+ ## three
409
+ a, b, c = *three_els
410
+ check_iter(three, [a, b, c])
411
+
412
+ ## four
413
+ a, b, c, d = *four_els
414
+ check_iter(four, [a, b, c, d])
415
+
416
+ ## two_by_four
417
+ l0, l1, l2, l3, a, b, c, d, e, f, g, h = *two_by_four_els
418
+ check_iter(two_by_four, [l0, l1, l2, l3])
419
+
420
+ ## big
421
+ l1, l2, l21, l22, l23, l230, a, b, c, d, e = *big_els
422
+ check_iter(big, [a, l1, l2])
423
+ end
424
+
425
+ ###
426
+ ### ----------------------------------------------------------------
427
+ ### each_index
428
+ ### ----------------------------------------------------------------
429
+ ###
430
+ def test_each_index
431
+ ## empty
432
+ assert_equal([], yields(:each_index, empty, empty))
433
+
434
+ ## one
435
+ assert_equal([[0]], yields(:each_index, one, one))
436
+
437
+ ## two
438
+ assert_equal([[0], [1]], yields(:each_index, two, two))
439
+
440
+ ## two_by_four
441
+ assert_equal([[0], [1], [2], [3]], yields(:each_index, two_by_four, two_by_four))
442
+
443
+ ## big
444
+ assert_equal([[0], [1], [2]], yields(:each_index, big, big))
445
+ end
446
+ end
447
+
448
+ ###
449
+ ### Tests:
450
+ ### -- node_before
451
+ ### -- node_after
452
+ ### -- remove_node
453
+ ### -- insert_before
454
+ ### -- insert_after
455
+ ### -- replace_node
456
+ ###
457
+ module NodeListChildManagementTests
458
+ include NodeListTest
459
+ def check_not_child list, node
460
+ n = C::Int.new
461
+ assert_raise(ArgumentError, list.node_before(node))
462
+ assert_raise(ArgumentError, list.node_after(node))
463
+ assert_raise(ArgumentError, list.remove_node(node))
464
+ assert_raise(ArgumentError, list.insert_after(node, n))
465
+ assert_raise(ArgumentError, list.insert_before(node, n))
466
+ assert_raise(ArgumentError, list.replace_node(node, n))
467
+ end
468
+
469
+ def check_list list, *nodes
470
+ afters = nodes.dup
471
+ afters.shift
472
+ afters.push(nil)
473
+
474
+ befores = nodes.dup
475
+ befores.pop
476
+ befores.unshift(nil)
477
+
478
+ assert_equal(list.length, nodes.length)
479
+ nodes.each_index do |i|
480
+ assert_same(nodes[i], list[i], "at index #{i}")
481
+ assert_same(list, nodes[i].parent, "at index #{i}")
482
+ ## node_after
483
+ assert_same(afters[i], list.node_after(nodes[i]), "at index #{i} (expected id=#{afters[i].object_id}, got id=#{list.node_after(nodes[i]).object_id})")
484
+ ## node_before
485
+ assert_same(befores[i], list.node_before(nodes[i]), "at index #{i}")
486
+ end
487
+ end
488
+
489
+ ###
490
+ ### ----------------------------------------------------------------
491
+ ### insert_before, insert_after
492
+ ### ----------------------------------------------------------------
493
+ ###
494
+
495
+ def test_insert_one_into_one
496
+ a1, a2 = 2.of{C::Int.new}
497
+ b1, b2 = 2.of{C::Int.new}
498
+ a = _List[a1]
499
+ b = _List[b1]
500
+
501
+ ## beginning
502
+ assert_same(b, b.insert_before(b1, b2))
503
+ check_list(b, b2, b1)
504
+
505
+ ## end
506
+ assert_same(a, a.insert_after(a1, a2))
507
+ check_list(a, a1, a2)
508
+ end
509
+
510
+ def test_insert_two_into_one
511
+ a1, a2, a3 = 3.of{C::Int.new}
512
+ b1, b2, b3 = 3.of{C::Int.new}
513
+ a = _List[a1]
514
+ b = _List[b1]
515
+
516
+ ## beginning
517
+ assert_same(a, a.insert_before(a1, a2, a3))
518
+ check_list(a, a2, a3, a1)
519
+
520
+ ## end
521
+ assert_same(b, b.insert_after(b1, b2, b3))
522
+ check_list(b, b1, b2, b3)
523
+ end
524
+
525
+ def test_insert_three_into_one
526
+ a1, a2, a3, a4 = 4.of{C::Int.new}
527
+ b1, b2, b3, b4 = 4.of{C::Int.new}
528
+ a = _List[a1]
529
+ b = _List[b1]
530
+
531
+ ## beginning
532
+ assert_same(a, a.insert_before(a1, a2, a3, a4))
533
+ check_list(a, a2, a3, a4, a1)
534
+
535
+ ## end
536
+ assert_same(b, b.insert_after(b1, b2, b3, b4))
537
+ check_list(b, b1, b2, b3, b4)
538
+ end
539
+
540
+ def test_insert_many_into_one
541
+ a1, a2, a3, a4, a5 = 5.of{C::Int.new}
542
+ b1, b2, b3, b4, b5 = 5.of{C::Int.new}
543
+ a = _List[a1]
544
+ b = _List[b1]
545
+
546
+ ## beginning
547
+ assert_same(a, a.insert_before(a1, a2, a3, a4, a5))
548
+ check_list(a, a2, a3, a4, a5, a1)
549
+
550
+ ## end
551
+ assert_same(b, b.insert_after(b1, b2, b3, b4, b5))
552
+ check_list(b, b1, b2, b3, b4, b5)
553
+ end
554
+
555
+ def insert_one_into_two
556
+ a1, a2, a3 = 3.of{C::Int.new}
557
+ b1, b2, b3 = 3.of{C::Int.new}
558
+ c1, c2, c3 = 3.of{C::Int.new}
559
+ d1, d2, d3 = 3.of{C::Int.new}
560
+ a = _List[a1, a2]
561
+ b = _List[b1, b2]
562
+ c = _List[c1, c2]
563
+ d = _List[d1, d2]
564
+
565
+ ## beginning
566
+ assert_same(a, a.insert_before(a1, a3))
567
+ check_list(a, a3, a1, a2)
568
+
569
+ ## end
570
+ assert_same(b, b.insert_after(b2, b3))
571
+ check_list(b, b1, b2, b3)
572
+
573
+ ## middle (after)
574
+ assert_same(c, c.insert_after(c1, c3))
575
+ check_list(c, c1, c3, c2)
576
+
577
+ ## middle (before)
578
+ assert_same(d, d.insert_before(d2, d3))
579
+ check_list(d, d1, d3, d2)
580
+ end
581
+
582
+ def test_insert_two_into_two
583
+ a1, a2, a3, a4 = 4.of{C::Int.new}
584
+ b1, b2, b3, b4 = 4.of{C::Int.new}
585
+ c1, c2, c3, c4 = 4.of{C::Int.new}
586
+ d1, d2, d3, d4 = 4.of{C::Int.new}
587
+ a = _List[a1, a2]
588
+ b = _List[b1, b2]
589
+ c = _List[c1, c2]
590
+ d = _List[d1, d2]
591
+
592
+ ## beginning
593
+ assert_same(a, a.insert_before(a1, a3, a4))
594
+ check_list(a, a3, a4, a1, a2)
595
+
596
+ ## end
597
+ assert_same(b, b.insert_after(b2, b3, b4))
598
+ check_list(b, b1, b2, b3, b4)
599
+
600
+ ## middle (after)
601
+ assert_same(c, c.insert_after(c1, c3, c4))
602
+ check_list(c, c1, c3, c4, c2)
603
+
604
+ ## middle (before)
605
+ assert_same(d, d.insert_before(d2, d3, d4))
606
+ check_list(d, d1, d3, d4, d2)
607
+ end
608
+
609
+ def test_insert_three_into_two
610
+ a1, a2, a3, a4, a5 = 5.of{C::Int.new}
611
+ b1, b2, b3, b4, b5 = 5.of{C::Int.new}
612
+ c1, c2, c3, c4, c5 = 5.of{C::Int.new}
613
+ d1, d2, d3, d4, d5 = 5.of{C::Int.new}
614
+ a = _List[a1, a2]
615
+ b = _List[b1, b2]
616
+ c = _List[c1, c2]
617
+ d = _List[d1, d2]
618
+
619
+ ## beginning
620
+ assert_same(a, a.insert_before(a1, a3, a4, a5))
621
+ check_list(a, a3, a4, a5, a1, a2)
622
+
623
+ ## end
624
+ assert_same(b, b.insert_after(b2, b3, b4, b5))
625
+ check_list(b, b1, b2, b3, b4, b5)
626
+
627
+ ## middle (after)
628
+ assert_same(c, c.insert_after(c1, c3, c4, c5))
629
+ check_list(c, c1, c3, c4, c5, c2)
630
+
631
+ ## middle (before)
632
+ assert_same(d, d.insert_before(d2, d3, d4, d5))
633
+ check_list(d, d1, d3, d4, d5, d2)
634
+ end
635
+
636
+ def test_insert_many_into_two
637
+ a1, a2, a3, a4, a5, a6 = 6.of{C::Int.new}
638
+ b1, b2, b3, b4, b5, b6 = 6.of{C::Int.new}
639
+ c1, c2, c3, c4, c5, c6 = 6.of{C::Int.new}
640
+ d1, d2, d3, d4, d5, d6 = 6.of{C::Int.new}
641
+ a = _List[a1, a2]
642
+ b = _List[b1, b2]
643
+ c = _List[c1, c2]
644
+ d = _List[d1, d2]
645
+
646
+ ## beginning
647
+ assert_same(a, a.insert_before(a1, a3, a4, a5, a6))
648
+ check_list(a, a3, a4, a5, a6, a1, a2)
649
+
650
+ ## end
651
+ assert_same(b, b.insert_after(b2, b3, b4, b5, b6))
652
+ check_list(b, b1, b2, b3, b4, b5, b6)
653
+
654
+ ## middle (after)
655
+ assert_same(c, c.insert_after(c1, c3, c4, c5, c6))
656
+ check_list(c, c1, c3, c4, c5, c6, c2)
657
+
658
+ ## middle (before)
659
+ assert_same(d, d.insert_before(d2, d3, d4, d5, d6))
660
+ check_list(d, d1, d3, d4, d5, d6, d2)
661
+ end
662
+
663
+ def test_insert_attached
664
+ ## one (before)
665
+ a1 = C::Int.new
666
+ b1 = C::Int.new
667
+ a = _List[a1]
668
+ b = _List[b1]
669
+ assert_same(a, a.insert_before(a1, b1))
670
+ assert_same_list([b1], b)
671
+ assert_equal(2, a.length)
672
+ assert_copy(b1, a[0])
673
+ assert_same(a1, a[1])
674
+
675
+ ## one (after)
676
+ a1 = C::Int.new
677
+ b1 = C::Int.new
678
+ a = _List[a1]
679
+ b = _List[b1]
680
+ assert_same(a, a.insert_after(a1, b1))
681
+ assert_same_list([b1], b)
682
+ assert_equal(2, a.length)
683
+ assert_same(a1, a[0])
684
+ assert_copy(b1, a[1])
685
+
686
+ ## many (before)
687
+ a1 = C::Int.new
688
+ b1, b2, b3, b4 = 4.of{C::Int.new}
689
+ a = _List[a1]
690
+ b = _List[b1, b2, b3, b4]
691
+ assert_same(a, a.insert_before(a1, b1, b2, b3, b4))
692
+ assert_same_list([b1, b2, b3, b4], b)
693
+ assert_equal(5, a.length)
694
+ assert_copy(b1, a[0])
695
+ assert_copy(b2, a[1])
696
+ assert_copy(b3, a[2])
697
+ assert_copy(b4, a[3])
698
+ assert_same(a1, a[4])
699
+
700
+ ## many (after)
701
+ a1 = C::Int.new
702
+ b1, b2, b3, b4 = 4.of{C::Int.new}
703
+ a = _List[a1]
704
+ b = _List[b1, b2, b3, b4]
705
+ assert_same(a, a.insert_after(a1, b1, b2, b3, b4))
706
+ assert_same_list([b1, b2, b3, b4], b)
707
+ assert_equal(5, a.length)
708
+ assert_same(a1, a[0])
709
+ assert_copy(b1, a[1])
710
+ assert_copy(b2, a[2])
711
+ assert_copy(b3, a[3])
712
+ assert_copy(b4, a[4])
713
+ end
714
+
715
+ ###
716
+ ### ----------------------------------------------------------------
717
+ ### remove_node
718
+ ### ----------------------------------------------------------------
719
+ ###
720
+
721
+ def test_remove_one_from_one
722
+ a1 = C::Int.new
723
+ a = _List[a1]
724
+
725
+ assert_same(a, a.remove_node(a1))
726
+ check_list(a)
727
+ assert_nil(a1.parent)
728
+ end
729
+
730
+ def test_remove_one_from_two
731
+ a1, a2 = 2.of{C::Int.new}
732
+ b1, b2 = 2.of{C::Int.new}
733
+ a = _List[a1, a2]
734
+ b = _List[b1, b2]
735
+
736
+ ## beginning
737
+ assert_same(a, a.remove_node(a1))
738
+ check_list(a, a2)
739
+ assert_nil(a1.parent)
740
+
741
+ ## end
742
+ assert_same(b, b.remove_node(b2))
743
+ check_list(b, b1)
744
+ assert_nil(b2.parent)
745
+ end
746
+
747
+ def test_remove_one_from_three
748
+ a1, a2, a3 = 3.of{C::Int.new}
749
+ b1, b2, b3 = 3.of{C::Int.new}
750
+ c1, c2, c3 = 3.of{C::Int.new}
751
+ a = _List[a1, a2, a3]
752
+ b = _List[b1, b2, b3]
753
+ c = _List[c1, c2, c3]
754
+
755
+ ## beginning
756
+ assert_same(a, a.remove_node(a1))
757
+ check_list(a, a2, a3)
758
+ assert_nil(a1.parent)
759
+
760
+ ## end
761
+ assert_same(b, b.remove_node(b3))
762
+ check_list(b, b1, b2)
763
+ assert_nil(b3.parent)
764
+
765
+ ## middle
766
+ assert_same(c, c.remove_node(c2))
767
+ check_list(c, c1, c3)
768
+ assert_nil(c2.parent)
769
+ end
770
+
771
+ def test_remove_one_from_many
772
+ a1, a2, a3, a4 = 4.of{C::Int.new}
773
+ b1, b2, b3, b4 = 4.of{C::Int.new}
774
+ c1, c2, c3, c4 = 4.of{C::Int.new}
775
+
776
+ a = _List[a1, a2, a3, a4]
777
+ b = _List[b1, b2, b3, b4]
778
+ c = _List[c1, c2, c3, c4]
779
+
780
+ ## beginning
781
+ assert_same(a, a.remove_node(a1))
782
+ check_list(a, a2, a3, a4)
783
+ assert_nil(a1.parent)
784
+
785
+ ## end
786
+ assert_same(b, b.remove_node(b4))
787
+ check_list(b, b1, b2, b3)
788
+ assert_nil(b4.parent)
789
+
790
+ ## middle
791
+ assert_same(c, c.remove_node(c2))
792
+ check_list(c, c1, c3, c4)
793
+ assert_nil(c2.parent)
794
+ end
795
+
796
+ ###
797
+ ### ----------------------------------------------------------------
798
+ ### replace_node
799
+ ### ----------------------------------------------------------------
800
+ ###
801
+
802
+ def test_replace_with_none_in_one
803
+ a1 = C::Int.new
804
+ a = _List[a1]
805
+ assert_same(a, a.replace_node(a1))
806
+ check_list(a)
807
+ assert_nil(a1.parent)
808
+ end
809
+
810
+ def test_replace_with_none_in_two
811
+ a1, a2 = 2.of{C::Int.new}
812
+ b1, b2 = 2.of{C::Int.new}
813
+ a = _List[a1, a2]
814
+ b = _List[b1, b2]
815
+
816
+ ## beginning
817
+ assert_same(a, a.replace_node(a1))
818
+ check_list(a, a2)
819
+ assert_nil(a1.parent)
820
+
821
+ ## end
822
+ assert_same(b, b.replace_node(b2))
823
+ check_list(b, b1)
824
+ assert_nil(b2.parent)
825
+ end
826
+
827
+ def test_replace_with_none_in_three
828
+ a1, a2, a3 = 3.of{C::Int.new}
829
+ b1, b2, b3 = 3.of{C::Int.new}
830
+ c1, c2, c3 = 3.of{C::Int.new}
831
+ a = _List[a1, a2, a3]
832
+ b = _List[b1, b2, b3]
833
+ c = _List[c1, c2, c3]
834
+
835
+ ## beginning
836
+ assert_same(a, a.replace_node(a1))
837
+ check_list(a, a2, a3)
838
+ assert_nil(a1.parent)
839
+
840
+ ## end
841
+ assert_same(b, b.replace_node(b3))
842
+ check_list(b, b1, b2)
843
+ assert_nil(b3.parent)
844
+
845
+ ## middle
846
+ assert_same(c, c.replace_node(c2))
847
+ check_list(c, c1, c3)
848
+ assert_nil(c2.parent)
849
+ end
850
+
851
+ def test_replace_with_one_in_one
852
+ a1, a2 = 2.of{C::Int.new}
853
+ a = _List[a1]
854
+
855
+ assert_same(a, a.replace_node(a1, a2))
856
+ check_list(a, a2)
857
+ assert_nil(a1.parent)
858
+ end
859
+
860
+ def test_replace_with_one_in_two
861
+ a1, a2, a3 = 3.of{C::Int.new}
862
+ b1, b2, b3 = 3.of{C::Int.new}
863
+ a = _List[a1, a2]
864
+ b = _List[b1, b2]
865
+
866
+ ## beginning
867
+ assert_same(a, a.replace_node(a1, a3))
868
+ check_list(a, a3, a2)
869
+ assert_nil(a1.parent)
870
+
871
+ ## end
872
+ assert_same(b, b.replace_node(b2, b3))
873
+ check_list(b, b1, b3)
874
+ assert_nil(b2.parent)
875
+ end
876
+
877
+ def test_replace_with_one_in_three
878
+ a1, a2, a3, a4 = 4.of{C::Int.new}
879
+ b1, b2, b3, b4 = 4.of{C::Int.new}
880
+ c1, c2, c3, c4 = 4.of{C::Int.new}
881
+ a = _List[a1, a2, a3]
882
+ b = _List[b1, b2, b3]
883
+ c = _List[c1, c2, c3]
884
+
885
+ ## beginning
886
+ assert_same(a, a.replace_node(a1, a4))
887
+ check_list(a, a4, a2, a3)
888
+ assert_nil(a1.parent)
889
+
890
+ ## end
891
+ assert_same(b, b.replace_node(b3, b4))
892
+ check_list(b, b1, b2, b4)
893
+ assert_nil(b3.parent)
894
+
895
+ ## middle
896
+ assert_same(c, c.replace_node(c2, c4))
897
+ check_list(c, c1, c4, c3)
898
+ assert_nil(c2.parent)
899
+ end
900
+
901
+ def test_replace_with_two_in_one
902
+ a1, a2, a3 = 3.of{C::Int.new}
903
+ a = _List[a1]
904
+
905
+ assert_same(a, a.replace_node(a1, a2, a3))
906
+ check_list(a, a2, a3)
907
+ assert_nil(a1.parent)
908
+ end
909
+
910
+ def test_replace_with_two_in_two
911
+ a1, a2, a3, a4 = 4.of{C::Int.new}
912
+ b1, b2, b3, b4 = 4.of{C::Int.new}
913
+ a = _List[a1, a2]
914
+ b = _List[b1, b2]
915
+
916
+ ## beginning
917
+ assert_same(a, a.replace_node(a1, a3, a4))
918
+ check_list(a, a3, a4, a2)
919
+ assert_nil(a1.parent)
920
+
921
+ ## end
922
+ assert_same(b, b.replace_node(b2, b3, b4))
923
+ check_list(b, b1, b3, b4)
924
+ assert_nil(b2.parent)
925
+ end
926
+
927
+ def test_replace_with_two_in_three
928
+ a1, a2, a3, a4, a5 = 5.of{C::Int.new}
929
+ b1, b2, b3, b4, b5 = 5.of{C::Int.new}
930
+ c1, c2, c3, c4, c5 = 5.of{C::Int.new}
931
+ a = _List[a1, a2, a3]
932
+ b = _List[b1, b2, b3]
933
+ c = _List[c1, c2, c3]
934
+
935
+ ## beginning
936
+ assert_same(a, a.replace_node(a1, a4, a5))
937
+ check_list(a, a4, a5, a2, a3)
938
+ assert_nil(a1.parent)
939
+
940
+ ## end
941
+ assert_same(b, b.replace_node(b3, b4, b5))
942
+ check_list(b, b1, b2, b4, b5)
943
+ assert_nil(b3.parent)
944
+
945
+ ## middle
946
+ assert_same(c, c.replace_node(c2, c4, c5))
947
+ check_list(c, c1, c4, c5, c3)
948
+ assert_nil(c2.parent)
949
+ end
950
+
951
+ def test_replace_with_three_in_one
952
+ a1, a2, a3, a4 = 4.of{C::Int.new}
953
+ a = _List[a1]
954
+
955
+ assert_same(a, a.replace_node(a1, a2, a3, a4))
956
+ check_list(a, a2, a3, a4)
957
+ assert_nil(a1.parent)
958
+ end
959
+
960
+ def test_replace_with_three_in_two
961
+ a1, a2, a3, a4, a5 = 5.of{C::Int.new}
962
+ b1, b2, b3, b4, b5 = 5.of{C::Int.new}
963
+ a = _List[a1, a2]
964
+ b = _List[b1, b2]
965
+
966
+ ## beginning
967
+ assert_same(a, a.replace_node(a1, a3, a4, a5))
968
+ check_list(a, a3, a4, a5, a2)
969
+ assert_nil(a1.parent)
970
+
971
+ ## end
972
+ assert_same(b, b.replace_node(b2, b3, b4, b5))
973
+ check_list(b, b1, b3, b4, b5)
974
+ assert_nil(b2.parent)
975
+ end
976
+
977
+ def test_replace_with_three_in_three
978
+ a1, a2, a3, a4, a5, a6 = 6.of{C::Int.new}
979
+ b1, b2, b3, b4, b5, b6 = 6.of{C::Int.new}
980
+ c1, c2, c3, c4, c5, c6 = 6.of{C::Int.new}
981
+ a = _List[a1, a2, a3]
982
+ b = _List[b1, b2, b3]
983
+ c = _List[c1, c2, c3]
984
+
985
+ ## beginning
986
+ assert_same(a, a.replace_node(a1, a4, a5, a6))
987
+ check_list(a, a4, a5, a6, a2, a3)
988
+ assert_nil(a1.parent)
989
+
990
+ ## end
991
+ assert_same(b, b.replace_node(b3, b4, b5, b6))
992
+ check_list(b, b1, b2, b4, b5, b6)
993
+ assert_nil(b3.parent)
994
+
995
+ ## middle
996
+ assert_same(c, c.replace_node(c2, c4, c5, c6))
997
+ check_list(c, c1, c4, c5, c6, c3)
998
+ assert_nil(c2.parent)
999
+ end
1000
+
1001
+ def test_replace_with_many_in_one
1002
+ a1, a2, a3, a4, a5 = 5.of{C::Int.new}
1003
+ a = _List[a1]
1004
+
1005
+ assert_same(a, a.replace_node(a1, a2, a3, a4, a5))
1006
+ check_list(a, a2, a3, a4, a5)
1007
+ assert_nil(a1.parent)
1008
+ end
1009
+
1010
+ def test_replace_with_many_in_two
1011
+ a1, a2, a3, a4, a5, a6 = 6.of{C::Int.new}
1012
+ b1, b2, b3, b4, b5, b6 = 6.of{C::Int.new}
1013
+ a = _List[a1, a2]
1014
+ b = _List[b1, b2]
1015
+
1016
+ ## beginning
1017
+ assert_same(a, a.replace_node(a1, a3, a4, a5, a6))
1018
+ check_list(a, a3, a4, a5, a6, a2)
1019
+ assert_nil(a1.parent)
1020
+
1021
+ ## end
1022
+ assert_same(b, b.replace_node(b2, b3, b4, b5, b6))
1023
+ check_list(b, b1, b3, b4, b5, b6)
1024
+ assert_nil(b2.parent)
1025
+ end
1026
+
1027
+ def test_replace_with_many_in_three
1028
+ a1, a2, a3, a4, a5, a6, a7 = 7.of{C::Int.new}
1029
+ b1, b2, b3, b4, b5, b6, b7 = 7.of{C::Int.new}
1030
+ c1, c2, c3, c4, c5, c6, c7 = 7.of{C::Int.new}
1031
+ a = _List[a1, a2, a3]
1032
+ b = _List[b1, b2, b3]
1033
+ c = _List[c1, c2, c3]
1034
+
1035
+ ## beginning
1036
+ assert_same(a, a.replace_node(a1, a4, a5, a6, a7))
1037
+ check_list(a, a4, a5, a6, a7, a2, a3)
1038
+ assert_nil(a1.parent)
1039
+
1040
+ ## end
1041
+ assert_same(b, b.replace_node(b3, b4, b5, b6, b7))
1042
+ check_list(b, b1, b2, b4, b5, b6, b7)
1043
+ assert_nil(b3.parent)
1044
+
1045
+ ## middle
1046
+ assert_same(c, c.replace_node(c2, c4, c5, c6, c7))
1047
+ check_list(c, c1, c4, c5, c6, c7, c3)
1048
+ assert_nil(c2.parent)
1049
+ end
1050
+
1051
+ def test_replace_with_attached
1052
+ ## one
1053
+ a1 = C::Int.new
1054
+ a = _List[a1]
1055
+ b1 = C::Int.new
1056
+ b = _List[b1]
1057
+ assert_same(a, a.replace_node(a1, b1))
1058
+ assert_copy(b1, a[0])
1059
+ assert_nil(a1.parent)
1060
+
1061
+ ## many
1062
+ a1 = C::Int.new
1063
+ a = _List[a1]
1064
+ b1, b2, b3, b4 = 4.of{C::Int.new}
1065
+ b = _List[b1, b2, b3, b4]
1066
+ assert_same(a, a.replace_node(a1, b1, b2, b3, b4))
1067
+ assert_copy(b1, a[0])
1068
+ assert_copy(b2, a[1])
1069
+ assert_copy(b3, a[2])
1070
+ assert_copy(b4, a[3])
1071
+ assert_nil(a1.parent)
1072
+ end
1073
+
1074
+ def test_replace_with_duplicated
1075
+ ## one
1076
+ a1, a2 = 2.of{C::Int.new}
1077
+ a = _List[a1]
1078
+ assert_same(a, a.replace_node(a1, a2, a2))
1079
+ assert_same(2, a.length)
1080
+ assert_same(a2, a[0])
1081
+ assert_copy(a2, a[1])
1082
+
1083
+ ## many
1084
+ a1, a2, a3, a4 = 4.of{C::Int.new}
1085
+ a = _List[a1, a2, a3]
1086
+ assert_same(a, a.replace_node(a1, a2, a4, a2, a4))
1087
+ assert_same(6, a.length)
1088
+ assert_copy(a2, a[0])
1089
+ assert_same(a4, a[1])
1090
+ assert_copy(a2, a[2])
1091
+ assert_copy(a4, a[3])
1092
+ assert_same(a2, a[4])
1093
+ assert_same(a3, a[5])
1094
+ end
1095
+
1096
+ def test_replace_with_replaced
1097
+ ## one
1098
+ a1 = C::Int.new
1099
+ a = _List[a1]
1100
+ assert_same(a, a.replace_node(a1, a1))
1101
+ assert_same_list([a1], a)
1102
+
1103
+ ## many -- some are the replaced node
1104
+ a1, a2, a3 = 3.of{C::Int.new}
1105
+ a = _List[a1]
1106
+ assert_same(a, a.replace_node(a1, a1, a2, a3, a1))
1107
+ assert_same(4, a.length)
1108
+ assert_same(a1, a[0])
1109
+ assert_same(a2, a[1])
1110
+ assert_same(a3, a[2])
1111
+ assert_copy(a1, a[3])
1112
+
1113
+ ## many -- all are the replaced node
1114
+ a1 = C::Int.new
1115
+ a = _List[a1]
1116
+ assert_same(a, a.replace_node(a1, a1, a1, a1))
1117
+ assert_same(3, a.length)
1118
+ assert_same(a1, a[0])
1119
+ assert_copy(a1, a[1])
1120
+ assert_copy(a1, a[2])
1121
+ end
1122
+ end
1123
+
1124
+ module NodeListArrayQueryTests
1125
+ include NodeListTest
1126
+ ###
1127
+ ### ----------------------------------------------------------------
1128
+ ### first, last
1129
+ ### ----------------------------------------------------------------
1130
+ ###
1131
+
1132
+ def test_first
1133
+ ## empty
1134
+ a = _List[]
1135
+ assert_nil(a.first)
1136
+
1137
+ ## one
1138
+ a1 = C::Int.new
1139
+ a = _List[a1]
1140
+ assert_same(a1, a.first)
1141
+ assert_same_list([ ], a.first(0))
1142
+ assert_same_list([a1], a.first(1))
1143
+ assert_same_list([a1], a.first(2))
1144
+
1145
+ ## two
1146
+ a1, a2 = 2.of{C::Int.new}
1147
+ a = _List[a1, a2]
1148
+ assert_same(a1, a.first)
1149
+ assert_same_list([ ], a.first(0))
1150
+ assert_same_list([a1 ], a.first(1))
1151
+ assert_same_list([a1, a2], a.first(2))
1152
+ assert_same_list([a1, a2], a.first(3))
1153
+
1154
+ ## three
1155
+ ret = a.first(3)
1156
+ a1, a2, a3 = 3.of{C::Int.new}
1157
+ a = _List[a1, a2, a3]
1158
+ assert_same(a1, a.first)
1159
+ assert_same_list([ ], a.first(0))
1160
+ assert_same_list([a1 ], a.first(1))
1161
+ assert_same_list([a1, a2 ], a.first(2))
1162
+ assert_same_list([a1, a2, a3], a.first(3))
1163
+ assert_same_list([a1, a2, a3], a.first(4))
1164
+
1165
+ ## negative array size
1166
+ assert_raise(ArgumentError){a.first(-1)}
1167
+ end
1168
+
1169
+ def test_last
1170
+ ## empty
1171
+ a = _List[]
1172
+ assert_nil(a.last)
1173
+
1174
+ ## one
1175
+ a1 = C::Int.new
1176
+ a = _List[a1]
1177
+ assert_same(a1, a.last)
1178
+ assert_same_list([ ], a.last(0))
1179
+ assert_same_list([a1], a.last(1))
1180
+ assert_same_list([a1], a.last(2))
1181
+
1182
+ ## two
1183
+ a1, a2 = 2.of{C::Int.new}
1184
+ a = _List[a1, a2]
1185
+ assert_same(a2, a.last)
1186
+ assert_same_list([ ], a.last(0))
1187
+ assert_same_list([ a2], a.last(1))
1188
+ assert_same_list([a1, a2], a.last(2))
1189
+ assert_same_list([a1, a2], a.last(3))
1190
+
1191
+ ## three
1192
+ ret = a.last(3)
1193
+ a1, a2, a3 = 3.of{C::Int.new}
1194
+ a = _List[a1, a2, a3]
1195
+ assert_same(a3, a.last)
1196
+ assert_same_list([ ], a.last(0))
1197
+ assert_same_list([ a3], a.last(1))
1198
+ assert_same_list([ a2, a3], a.last(2))
1199
+ assert_same_list([a1, a2, a3], a.last(3))
1200
+ assert_same_list([a1, a2, a3], a.last(4))
1201
+
1202
+ ## negative array size
1203
+ assert_raise(ArgumentError){a.last(-1)}
1204
+ end
1205
+
1206
+ ###
1207
+ ### ----------------------------------------------------------------
1208
+ ### empty?
1209
+ ### ----------------------------------------------------------------
1210
+ ###
1211
+
1212
+ def test_empty
1213
+ assert(_List[].empty?)
1214
+ assert(!_List[C::Int.new].empty?)
1215
+ assert(!_List[_List[]].empty?)
1216
+ end
1217
+
1218
+ ###
1219
+ ### ----------------------------------------------------------------
1220
+ ### to_a
1221
+ ### ----------------------------------------------------------------
1222
+ ###
1223
+
1224
+ def test_to_a
1225
+ ## empty
1226
+ r = empty.to_a
1227
+ assert_same(::Array, r.class)
1228
+ assert_same_list([], r)
1229
+
1230
+ ## one
1231
+ a = *one_els
1232
+ r = one.to_a
1233
+ assert_same(::Array, r.class)
1234
+ assert_same_list([a], r)
1235
+
1236
+ ## two
1237
+ a, b = *two_els
1238
+ r = two.to_a
1239
+ assert_same(::Array, r.class)
1240
+ assert_same_list([a, b], r)
1241
+
1242
+ ## three
1243
+ a, b, c = *three_els
1244
+ r = three.to_a
1245
+ assert_same(::Array, r.class)
1246
+ assert_same_list([a, b, c], r)
1247
+
1248
+ ## four
1249
+ a, b, c, d = *four_els
1250
+ r = four.to_a
1251
+ assert_same(::Array, r.class)
1252
+ assert_same_list([a, b, c, d], r)
1253
+
1254
+ ## two_by_four
1255
+ l0, l1, l2, l3, a, b, c, d, e, f, g, h = *two_by_four_els
1256
+ r = two_by_four.to_a
1257
+ assert_same(::Array, r.class)
1258
+ assert_same_list([l0, l1, l2, l3], r)
1259
+
1260
+ ## big
1261
+ l1, l2, l21, l22, l23, l230, a, b, c, d, e = *big_els
1262
+ r = big.to_a
1263
+ assert_same(::Array, r.class)
1264
+ assert_same_list([a, l1, l2], r)
1265
+ end
1266
+
1267
+ ###
1268
+ ### ----------------------------------------------------------------
1269
+ ### index, rindex
1270
+ ### ----------------------------------------------------------------
1271
+ ###
1272
+
1273
+ def test_index
1274
+ ## empty
1275
+ empty = _List[]
1276
+ assert_nil(empty.index(C::Int.new))
1277
+ assert_nil(empty.index(nil))
1278
+ ##
1279
+ assert_nil(empty.rindex(C::Int.new))
1280
+ assert_nil(empty.rindex(nil))
1281
+
1282
+ ## one
1283
+ a = C::Int.new(1)
1284
+ list = _List[a]
1285
+ ##
1286
+ assert_equal(0, list.index(a))
1287
+ assert_equal(0, list.index(C::Int.new(1)))
1288
+ assert_nil(list.index(C::Int.new))
1289
+ assert_nil(list.index(nil))
1290
+ ##
1291
+ assert_equal(0, list.rindex(a))
1292
+ assert_nil(list.rindex(C::Int.new))
1293
+ assert_nil(list.rindex(nil))
1294
+
1295
+ ## two
1296
+ a = C::Int.new(1)
1297
+ b = C::Int.new(2)
1298
+ list = _List[a, b]
1299
+ ##
1300
+ assert_equal(0, list.index(a))
1301
+ assert_equal(1, list.index(b))
1302
+ assert_nil(list.index(C::Int.new))
1303
+ assert_nil(list.index(nil))
1304
+ ##
1305
+ assert_equal(0, list.rindex(a))
1306
+ assert_equal(1, list.rindex(b))
1307
+ assert_nil(list.rindex(C::Int.new))
1308
+ assert_nil(list.rindex(nil))
1309
+
1310
+ ## nested -- [a, [b]]
1311
+ a = C::Int.new(1)
1312
+ b = C::Int.new(2)
1313
+ l1 = _List[b]
1314
+ list = _List[a, l1]
1315
+ ##
1316
+ assert_equal(0, list.index(a))
1317
+ assert_equal(1, list.index(l1))
1318
+ assert_equal(1, list.index(_List[C::Int.new(2)]))
1319
+ assert_equal(nil, list.index([b]))
1320
+ assert_nil(list.index([a]))
1321
+ assert_nil(list.index(C::Int.new))
1322
+ assert_nil(list.index(nil))
1323
+ ##
1324
+ assert_equal(0, list.rindex(a))
1325
+ assert_equal(1, list.rindex(l1))
1326
+ assert_equal(1, list.rindex(_List[b]))
1327
+ assert_nil(list.rindex([a]))
1328
+ assert_nil(list.rindex(C::Int.new))
1329
+ assert_nil(list.rindex(nil))
1330
+
1331
+ ## repeated
1332
+ a1, a2, a3 = 3.of{C::Int.new(-1)}
1333
+ b1, b2, b3 = 3.of{C::Int.new(1)}
1334
+ list = _List[a1, b1, a2, b2, a3, b3]
1335
+ ##
1336
+ assert_equal(0, list.index(a1))
1337
+ assert_equal(0, list.index(a2))
1338
+ assert_equal(0, list.index(a3))
1339
+ assert_equal(1, list.index(b1))
1340
+ assert_equal(1, list.index(b2))
1341
+ assert_equal(1, list.index(b3))
1342
+ assert_nil(list.index(C::Int.new))
1343
+ assert_nil(list.index(nil))
1344
+ ##
1345
+ assert_equal(4, list.rindex(a1))
1346
+ assert_equal(4, list.rindex(a2))
1347
+ assert_equal(4, list.rindex(a3))
1348
+ assert_equal(5, list.rindex(b1))
1349
+ assert_equal(5, list.rindex(b2))
1350
+ assert_equal(5, list.rindex(b3))
1351
+ assert_nil(list.rindex(C::Int.new))
1352
+ assert_nil(list.rindex(nil))
1353
+ end
1354
+
1355
+ ###
1356
+ ### ----------------------------------------------------------------
1357
+ ### values_at
1358
+ ### ----------------------------------------------------------------
1359
+ ###
1360
+
1361
+ def test_values_at
1362
+ ## empty
1363
+ assert_same_list([], empty.values_at())
1364
+ assert_same_list([nil], empty.values_at(1))
1365
+ assert_same_list([nil], empty.values_at(-1))
1366
+
1367
+ ## one
1368
+ a = *one_els
1369
+ assert_same_list([], one.values_at())
1370
+ assert_same_list([a], one.values_at(0))
1371
+ assert_same_list([a], one.values_at(-1))
1372
+ assert_same_list([nil, a], one.values_at(1, -1))
1373
+
1374
+ ## big -- [a, [b,c], [d, [e], [], [[]]]]
1375
+ l1, l2, l21, l22, l23, l230, a, b, c, d, e = *big_els
1376
+ assert_same_list([], big.values_at())
1377
+ assert_same_list([l2], big.values_at(-1))
1378
+ assert_same_list([a, nil], big.values_at(-3, 3))
1379
+ assert_same_list([a, l1, l2], big.values_at(0, -2, -1))
1380
+ end
1381
+
1382
+ ###
1383
+ ### ----------------------------------------------------------------
1384
+ ### join
1385
+ ### ----------------------------------------------------------------
1386
+ ###
1387
+
1388
+ class N < C::Node
1389
+ def initialize s
1390
+ @s = s
1391
+ end
1392
+ def to_s
1393
+ @s.dup
1394
+ end
1395
+ end
1396
+ def test_join
1397
+ ## empty
1398
+ list = _List[]
1399
+ assert_equal('', list.join)
1400
+ assert_equal('', list.join('.'))
1401
+
1402
+ ## one
1403
+ a = N.new('a')
1404
+ list = _List[a]
1405
+ assert_equal('a', list.join)
1406
+ assert_equal('a', list.join('.'))
1407
+
1408
+ ## two
1409
+ a = N.new('a')
1410
+ b = N.new('b')
1411
+ list = _List[a, b]
1412
+ assert_equal('ab', list.join)
1413
+ assert_equal('a.b', list.join('.'))
1414
+
1415
+ ## two_by_two
1416
+ a = N.new('a')
1417
+ b = N.new('b')
1418
+ c = N.new('c')
1419
+ d = N.new('d')
1420
+ l0 = _List[a, b]
1421
+ l1 = _List[c, d]
1422
+ list = _List[l0, l1]
1423
+ assert_equal('a, bc, d', list.join)
1424
+ assert_equal('a, b|c, d', list.join('|'))
1425
+ end
1426
+ end
1427
+
1428
+ module NodeListModifierTests
1429
+ include NodeListTest
1430
+ def test_push_none
1431
+ ## empty
1432
+ list = _List[]
1433
+ assert_same(list, list.push)
1434
+ assert_same_list([], list)
1435
+
1436
+ ## one
1437
+ a = C::Int.new
1438
+ list = _List[a]
1439
+ assert_same(list, list.push)
1440
+ assert_same_list([a], list)
1441
+
1442
+ ## two
1443
+ a, b = 2.of{C::Int.new}
1444
+ list = _List[a, b]
1445
+ assert_same(list, list.push)
1446
+ assert_same_list([a, b], list)
1447
+
1448
+ ## three
1449
+ a, b, c = 3.of{C::Int.new}
1450
+ list = _List[a, b, c]
1451
+ assert_same(list, list.push)
1452
+ assert_same_list([a, b, c], list)
1453
+ end
1454
+
1455
+ def test_push_one
1456
+ ## empty
1457
+ a = C::Int.new
1458
+ list = _List[]
1459
+ assert_same(list, list.push(a))
1460
+ assert_same_list([a], list)
1461
+
1462
+ ## one
1463
+ a, b = 2.of{C::Int.new}
1464
+ list = _List[a]
1465
+ assert_same(list, list.push(b))
1466
+ assert_same_list([a, b], list)
1467
+
1468
+ ## two
1469
+ a, b, c = 3.of{C::Int.new}
1470
+ list = _List[a, b]
1471
+ assert_same(list, list.push(c))
1472
+ assert_same_list([a, b, c], list)
1473
+
1474
+ ## three
1475
+ a, b, c, d = 4.of{C::Int.new}
1476
+ list = _List[a, b, c]
1477
+ assert_same(list, list.push(d))
1478
+ assert_same_list([a, b, c, d], list)
1479
+ end
1480
+
1481
+ def test_push_two
1482
+ ## empty
1483
+ a, b = 2.of{C::Int.new}
1484
+ list = _List[]
1485
+ assert_same(list, list.push(a, b))
1486
+ assert_same_list([a, b], list)
1487
+
1488
+ ## one
1489
+ a, b, c = 3.of{C::Int.new}
1490
+ list = _List[a]
1491
+ assert_same(list, list.push(b, c))
1492
+ assert_same_list([a, b, c], list)
1493
+
1494
+ ## two
1495
+ a, b, c, d = 4.of{C::Int.new}
1496
+ list = _List[a, b]
1497
+ assert_same(list, list.push(c, d))
1498
+ assert_same_list([a, b, c, d], list)
1499
+
1500
+ ## three
1501
+ a, b, c, d, e = 5.of{C::Int.new}
1502
+ list = _List[a, b, c]
1503
+ assert_same(list, list.push(d, e))
1504
+ assert_same_list([a, b, c, d, e], list)
1505
+ end
1506
+
1507
+ def test_push_three
1508
+ ## empty
1509
+ a, b, c = 3.of{C::Int.new}
1510
+ list = _List[]
1511
+ assert_same(list, list.push(a, b, c))
1512
+ assert_same_list([a, b, c], list)
1513
+
1514
+ ## one
1515
+ a, b, c, d = 4.of{C::Int.new}
1516
+ list = _List[a]
1517
+ assert_same(list, list.push(b, c, d))
1518
+ assert_same_list([a, b, c, d], list)
1519
+
1520
+ ## two
1521
+ a, b, c, d, e = 5.of{C::Int.new}
1522
+ list = _List[a, b]
1523
+ assert_same(list, list.push(c, d, e))
1524
+ assert_same_list([a, b, c, d, e], list)
1525
+
1526
+ ## three
1527
+ a, b, c, d, e, f = 6.of{C::Int.new}
1528
+ list = _List[a, b, c]
1529
+ assert_same(list, list.push(d, e, f))
1530
+ assert_same_list([a, b, c, d, e, f], list)
1531
+ end
1532
+
1533
+ def test_push_attached
1534
+ ## one
1535
+ a1 = C::Int.new
1536
+ b1 = C::Int.new
1537
+ a = _List[a1]
1538
+ b = _List[b1]
1539
+ assert_same(a, a.push(b1))
1540
+ assert_same_list([b1], b)
1541
+ assert_equal(2, a.length)
1542
+ assert_same(a1, a[0])
1543
+ assert_copy(b1, a[1])
1544
+
1545
+ ## many
1546
+ a1 = C::Int.new
1547
+ b1, b2, b3, b4 = 4.of{C::Int.new}
1548
+ a = _List[a1]
1549
+ b = _List[b1, b2, b3, b4]
1550
+ assert_same(a, a.push(b1, b2, b3, b4))
1551
+ assert_same_list([b1, b2, b3, b4], b)
1552
+ assert_equal(5, a.length)
1553
+ assert_same(a1, a[0])
1554
+ assert_copy(b1, a[1])
1555
+ assert_copy(b2, a[2])
1556
+ assert_copy(b3, a[3])
1557
+ assert_copy(b4, a[4])
1558
+ end
1559
+
1560
+ def test_unshift_none
1561
+ ## empty
1562
+ list = _List[]
1563
+ assert_same(list, list.unshift)
1564
+ assert_same_list([], list)
1565
+
1566
+ ## one
1567
+ a = C::Int.new
1568
+ list = _List[a]
1569
+ assert_same(list, list.unshift)
1570
+ assert_same_list([a], list)
1571
+
1572
+ ## two
1573
+ a, b = 2.of{C::Int.new}
1574
+ list = _List[a, b]
1575
+ assert_same(list, list.unshift)
1576
+ assert_same_list([a, b], list)
1577
+
1578
+ ## three
1579
+ a, b, c = 3.of{C::Int.new}
1580
+ list = _List[a, b, c]
1581
+ assert_same(list, list.unshift)
1582
+ assert_same_list([a, b, c], list)
1583
+ end
1584
+
1585
+ def test_unshift_one
1586
+ ## empty
1587
+ a = C::Int.new
1588
+ list = _List[]
1589
+ assert_same(list, list.unshift(a))
1590
+ assert_same_list([a], list)
1591
+
1592
+ ## one
1593
+ a, b = 2.of{C::Int.new}
1594
+ list = _List[a]
1595
+ assert_same(list, list.unshift(b))
1596
+ assert_same_list([b, a], list)
1597
+
1598
+ ## two
1599
+ a, b, c = 3.of{C::Int.new}
1600
+ list = _List[a, b]
1601
+ assert_same(list, list.unshift(c))
1602
+ assert_same_list([c, a, b], list)
1603
+
1604
+ ## three
1605
+ a, b, c, d = 4.of{C::Int.new}
1606
+ list = _List[a, b, c]
1607
+ assert_same(list, list.unshift(d))
1608
+ assert_same_list([d, a, b, c], list)
1609
+ end
1610
+
1611
+ def test_unshift_two
1612
+ ## empty
1613
+ a, b = 2.of{C::Int.new}
1614
+ list = _List[]
1615
+ assert_same(list, list.unshift(a, b))
1616
+ assert_same_list([a, b], list)
1617
+
1618
+ ## one
1619
+ a, b, c = 3.of{C::Int.new}
1620
+ list = _List[a]
1621
+ assert_same(list, list.unshift(b, c))
1622
+ assert_same_list([b, c, a], list)
1623
+
1624
+ ## two
1625
+ a, b, c, d = 4.of{C::Int.new}
1626
+ list = _List[a, b]
1627
+ assert_same(list, list.unshift(c, d))
1628
+ assert_same_list([c, d, a, b], list)
1629
+
1630
+ ## three
1631
+ a, b, c, d, e = 5.of{C::Int.new}
1632
+ list = _List[a, b, c]
1633
+ assert_same(list, list.unshift(d, e))
1634
+ assert_same_list([d, e, a, b, c], list)
1635
+ end
1636
+
1637
+ def test_unshift_three
1638
+ ## empty
1639
+ a, b, c = 3.of{C::Int.new}
1640
+ list = _List[]
1641
+ assert_same(list, list.unshift(a, b, c))
1642
+ assert_same_list([a, b, c], list)
1643
+
1644
+ ## one
1645
+ a, b, c, d = 4.of{C::Int.new}
1646
+ list = _List[a]
1647
+ assert_same(list, list.unshift(b, c, d))
1648
+ assert_same_list([b, c, d, a], list)
1649
+
1650
+ ## two
1651
+ a, b, c, d, e = 5.of{C::Int.new}
1652
+ list = _List[a, b]
1653
+ assert_same(list, list.unshift(c, d, e))
1654
+ assert_same_list([c, d, e, a, b], list)
1655
+
1656
+ ## three
1657
+ a, b, c, d, e, f = 6.of{C::Int.new}
1658
+ list = _List[a, b, c]
1659
+ assert_same(list, list.unshift(d, e, f))
1660
+ assert_same_list([d, e, f, a, b, c], list)
1661
+ end
1662
+
1663
+ def test_unshift_attached
1664
+ ## one
1665
+ a1 = C::Int.new
1666
+ b1 = C::Int.new
1667
+ a = _List[a1]
1668
+ b = _List[b1]
1669
+ assert_same(a, a.unshift(b1))
1670
+ assert_same_list([b1], b)
1671
+ assert_equal(2, a.length)
1672
+ assert_copy(b1, a[0])
1673
+ assert_same(a1, a[1])
1674
+
1675
+ ## many
1676
+ a1 = C::Int.new
1677
+ b1, b2, b3, b4 = 4.of{C::Int.new}
1678
+ a = _List[a1]
1679
+ b = _List[b1, b2, b3, b4]
1680
+ assert_same(a, a.unshift(b1, b2, b3, b4))
1681
+ assert_same_list([b1, b2, b3, b4], b)
1682
+ assert_equal(5, a.length)
1683
+ assert_copy(b1, a[0])
1684
+ assert_copy(b2, a[1])
1685
+ assert_copy(b3, a[2])
1686
+ assert_copy(b4, a[3])
1687
+ assert_same(a1, a[4])
1688
+ end
1689
+
1690
+ def test_pop
1691
+ ## empty
1692
+ list = _List[]
1693
+ assert_same(nil, list.pop)
1694
+ assert_same_list([], list)
1695
+
1696
+ ## one
1697
+ a = C::Int.new
1698
+ list = _List[a]
1699
+ assert_same(a, list.pop)
1700
+ assert_same_list([], list)
1701
+ assert_nil(a.parent)
1702
+
1703
+ ## two
1704
+ a, b = 2.of{C::Int.new}
1705
+ list = _List[a, b]
1706
+ assert_same(b, list.pop)
1707
+ assert_same_list([a], list)
1708
+ assert_nil(b.parent)
1709
+
1710
+ ## three
1711
+ a, b, c = 3.of{C::Int.new}
1712
+ list = _List[a, b, c]
1713
+ assert_same(c, list.pop)
1714
+ assert_same_list([a, b], list)
1715
+ assert_nil(c.parent)
1716
+ end
1717
+
1718
+ def test_pop_none
1719
+ ## empty
1720
+ list = _List[]
1721
+ ret = list.pop(0)
1722
+ assert_same_list([], ret)
1723
+ assert_same_list([], list)
1724
+
1725
+ ## one
1726
+ a = C::Int.new
1727
+ list = _List[a]
1728
+ ret = list.pop(0)
1729
+ assert_same_list([], ret)
1730
+ assert_same_list([a], list)
1731
+
1732
+ ## two
1733
+ a, b = 2.of{C::Int.new}
1734
+ list = _List[a, b]
1735
+ ret = list.pop(0)
1736
+ assert_same_list([], ret)
1737
+ assert_same_list([a, b], list)
1738
+
1739
+ ## three
1740
+ a, b, c = 3.of{C::Int.new}
1741
+ list = _List[a, b, c]
1742
+ ret = list.pop(0)
1743
+ assert_same_list([], ret)
1744
+ assert_same_list([a, b, c], list)
1745
+ end
1746
+
1747
+ def test_pop_one
1748
+ ## empty
1749
+ list = _List[]
1750
+ ret = list.pop(1)
1751
+ assert_same_list([], ret)
1752
+ assert_same_list([], list)
1753
+
1754
+ ## one
1755
+ a = C::Int.new
1756
+ list = _List[a]
1757
+ ret = list.pop(1)
1758
+ assert_same_list([a], ret)
1759
+ assert_same_list([], list)
1760
+ assert_nil(a.parent)
1761
+
1762
+ ## two
1763
+ a, b = 2.of{C::Int.new}
1764
+ list = _List[a, b]
1765
+ ret = list.pop(1)
1766
+ assert_same_list([b], ret)
1767
+ assert_same_list([a], list)
1768
+ assert_nil(b.parent)
1769
+
1770
+ ## three
1771
+ a, b, c = 3.of{C::Int.new}
1772
+ list = _List[a, b, c]
1773
+ ret = list.pop(1)
1774
+ assert_same_list([c], ret)
1775
+ assert_same_list([a, b], list)
1776
+ assert_nil(c.parent)
1777
+ end
1778
+
1779
+ def test_pop_two
1780
+ ## empty
1781
+ list = _List[]
1782
+ ret = list.pop(2)
1783
+ assert_same_list([], ret)
1784
+ assert_same_list([], list)
1785
+
1786
+ ## one
1787
+ a = C::Int.new
1788
+ list = _List[a]
1789
+ ret = list.pop(2)
1790
+ assert_same_list([a], ret)
1791
+ assert_same_list([], list)
1792
+ assert_nil(a.parent)
1793
+
1794
+ ## two
1795
+ a, b = 2.of{C::Int.new}
1796
+ list = _List[a, b]
1797
+ ret = list.pop(2)
1798
+ assert_same_list([a, b], ret)
1799
+ assert_same_list([], list)
1800
+ assert_nil(a.parent)
1801
+ assert_nil(b.parent)
1802
+
1803
+ ## three
1804
+ a, b, c = 3.of{C::Int.new}
1805
+ list = _List[a, b, c]
1806
+ ret = list.pop(2)
1807
+ assert_same_list([b, c], ret)
1808
+ assert_same_list([a], list)
1809
+ assert_nil(b.parent)
1810
+ assert_nil(c.parent)
1811
+ end
1812
+
1813
+ def test_shift
1814
+ ## empty
1815
+ list = _List[]
1816
+ ret = list.shift
1817
+ assert_nil(ret)
1818
+ assert_same_list([], list)
1819
+
1820
+ ## one
1821
+ a = C::Int.new
1822
+ list = _List[a]
1823
+ ret = list.shift
1824
+ assert_same(a, ret)
1825
+ assert_same_list([], list)
1826
+ assert_nil(a.parent)
1827
+
1828
+ ## two
1829
+ a, b = 2.of{C::Int.new}
1830
+ list = _List[a, b]
1831
+ ret = list.shift
1832
+ assert_same(a, ret)
1833
+ assert_same_list([b], list)
1834
+ assert_nil(a.parent)
1835
+
1836
+ ## three
1837
+ a, b, c = 3.of{C::Int.new}
1838
+ list = _List[a, b, c]
1839
+ ret = list.shift
1840
+ assert_same(a, ret)
1841
+ assert_same_list([b, c], list)
1842
+ assert_nil(a.parent)
1843
+ end
1844
+
1845
+ def test_shift_none
1846
+ ## empty
1847
+ list = _List[]
1848
+ ret = list.shift(0)
1849
+ assert_same_list([], ret)
1850
+ assert_same_list([], list)
1851
+
1852
+ ## one
1853
+ a = C::Int.new
1854
+ list = _List[a]
1855
+ ret = list.shift(0)
1856
+ assert_same_list([], ret)
1857
+ assert_same_list([a], list)
1858
+
1859
+ ## two
1860
+ a, b = 2.of{C::Int.new}
1861
+ list = _List[a, b]
1862
+ ret = list.shift(0)
1863
+ assert_same_list([], ret)
1864
+ assert_same_list([a, b], list)
1865
+
1866
+ ## three
1867
+ a, b, c = 3.of{C::Int.new}
1868
+ list = _List[a, b, c]
1869
+ ret = list.shift(0)
1870
+ assert_same_list([], ret)
1871
+ assert_same_list([a, b, c], list)
1872
+ end
1873
+
1874
+ def test_shift_one
1875
+ ## empty
1876
+ list = _List[]
1877
+ ret = list.shift(1)
1878
+ assert_same_list([], ret)
1879
+ assert_same_list([], list)
1880
+
1881
+ ## one
1882
+ a = C::Int.new
1883
+ list = _List[a]
1884
+ ret = list.shift(1)
1885
+ assert_same_list([a], ret)
1886
+ assert_same_list([], list)
1887
+ assert_nil(a.parent)
1888
+
1889
+ ## two
1890
+ a, b = 2.of{C::Int.new}
1891
+ list = _List[a, b]
1892
+ ret = list.shift(1)
1893
+ assert_same_list([a], ret)
1894
+ assert_same_list([b], list)
1895
+ assert_nil(a.parent)
1896
+
1897
+ ## three
1898
+ a, b, c = 3.of{C::Int.new}
1899
+ list = _List[a, b, c]
1900
+ ret = list.shift(1)
1901
+ assert_same_list([a], ret)
1902
+ assert_same_list([b, c], list)
1903
+ assert_nil(a.parent)
1904
+ end
1905
+
1906
+ def test_shift_two
1907
+ ## empty
1908
+ list = _List[]
1909
+ ret = list.shift(2)
1910
+ assert_same_list([], ret)
1911
+ assert_same_list([], list)
1912
+
1913
+ ## one
1914
+ a = C::Int.new
1915
+ list = _List[a]
1916
+ ret = list.shift(2)
1917
+ assert_same_list([a], ret)
1918
+ assert_same_list([], list)
1919
+ assert_nil(a.parent)
1920
+
1921
+ ## two
1922
+ a, b = 2.of{C::Int.new}
1923
+ list = _List[a, b]
1924
+ ret = list.shift(2)
1925
+ assert_same_list([a, b], ret)
1926
+ assert_same_list([], list)
1927
+ assert_nil(a.parent)
1928
+ assert_nil(b.parent)
1929
+
1930
+ ## three
1931
+ a, b, c = 3.of{C::Int.new}
1932
+ list = _List[a, b, c]
1933
+ ret = list.shift(2)
1934
+ assert_same_list([a, b], ret)
1935
+ assert_same_list([c], list)
1936
+ assert_nil(a.parent)
1937
+ assert_nil(b.parent)
1938
+ end
1939
+
1940
+ ###
1941
+ ### ----------------------------------------------------------------
1942
+ ### insert
1943
+ ### ----------------------------------------------------------------
1944
+ ###
1945
+
1946
+ def test_insert_one_into_one
1947
+ a1, a2 = 2.of{C::Int.new}
1948
+ b1, b2 = 2.of{C::Int.new}
1949
+ a = _List[a1]
1950
+ b = _List[b1]
1951
+
1952
+ ## beginning
1953
+ a.insert(0, a2)
1954
+ assert_same_list([a2, a1], a)
1955
+
1956
+ ## end
1957
+ b.insert(1, b2)
1958
+ assert_same_list([b1, b2], b)
1959
+ end
1960
+
1961
+ def test_insert_two_into_one
1962
+ a1, a2, a3 = 3.of{C::Int.new}
1963
+ b1, b2, b3 = 3.of{C::Int.new}
1964
+ a = _List[a1]
1965
+ b = _List[b1]
1966
+
1967
+ ## beginning
1968
+ a.insert(0, a2, a3)
1969
+ assert_same_list([a2, a3, a1], a)
1970
+
1971
+ ## end
1972
+ b.insert(1, b2, b3)
1973
+ assert_same_list([b1, b2, b3], b)
1974
+ end
1975
+
1976
+ def test_insert_three_into_one
1977
+ a1, a2, a3, a4 = 4.of{C::Int.new}
1978
+ b1, b2, b3, b4 = 4.of{C::Int.new}
1979
+ a = _List[a1]
1980
+ b = _List[b1]
1981
+
1982
+ ## beginning
1983
+ a.insert(0, a2, a3, a4)
1984
+ assert_same_list([a2, a3, a4, a1], a)
1985
+
1986
+ ## end
1987
+ b.insert(1, b2, b3, b4)
1988
+ assert_same_list([b1, b2, b3, b4], b)
1989
+ end
1990
+
1991
+ def test_insert_many_into_one
1992
+ a1, a2, a3, a4, a5 = 5.of{C::Int.new}
1993
+ b1, b2, b3, b4, b5 = 5.of{C::Int.new}
1994
+ a = _List[a1]
1995
+ b = _List[b1]
1996
+
1997
+ ## beginning
1998
+ a.insert(0, a2, a3, a4, a5)
1999
+ assert_same_list([a2, a3, a4, a5, a1], a)
2000
+
2001
+ ## end
2002
+ b.insert(1, b2, b3, b4, b5)
2003
+ assert_same_list([b1, b2, b3, b4, b5], b)
2004
+ end
2005
+
2006
+ def insert_one_into_two
2007
+ a1, a2, a3 = 3.of{C::Int.new}
2008
+ b1, b2, b3 = 3.of{C::Int.new}
2009
+ c1, c2, c3 = 3.of{C::Int.new}
2010
+ a = _List[a1, a2]
2011
+ b = _List[b1, b2]
2012
+ c = _List[c1, c2]
2013
+
2014
+ ## beginning
2015
+ a.insert(0, a3)
2016
+ assert_same_list([a3, a1, a2], a)
2017
+
2018
+ ## end
2019
+ b.insert(2, b3)
2020
+ assert_same_list([b1, b2, b3], b)
2021
+
2022
+ ## middle
2023
+ c.insert1(c1, c3)
2024
+ assert_same_list([c1, c3, c2], c)
2025
+ end
2026
+
2027
+ def test_insert_two_into_two
2028
+ a1, a2, a3, a4 = 4.of{C::Int.new}
2029
+ b1, b2, b3, b4 = 4.of{C::Int.new}
2030
+ c1, c2, c3, c4 = 4.of{C::Int.new}
2031
+ d1, d2, d3, d4 = 4.of{C::Int.new}
2032
+ a = _List[a1, a2]
2033
+ b = _List[b1, b2]
2034
+ c = _List[c1, c2]
2035
+
2036
+ ## beginning
2037
+ a.insert(0, a3, a4)
2038
+ assert_same_list([a3, a4, a1, a2], a)
2039
+
2040
+ ## end
2041
+ b.insert(2, b3, b4)
2042
+ assert_same_list([b1, b2, b3, b4], b)
2043
+
2044
+ ## middle
2045
+ c.insert(1, c3, c4)
2046
+ assert_same_list([c1, c3, c4, c2], c)
2047
+ end
2048
+
2049
+ def test_insert_three_into_two
2050
+ a1, a2, a3, a4, a5 = 5.of{C::Int.new}
2051
+ b1, b2, b3, b4, b5 = 5.of{C::Int.new}
2052
+ c1, c2, c3, c4, c5 = 5.of{C::Int.new}
2053
+ a = _List[a1, a2]
2054
+ b = _List[b1, b2]
2055
+ c = _List[c1, c2]
2056
+
2057
+ ## beginning
2058
+ a.insert(0, a3, a4, a5)
2059
+ assert_same_list([a3, a4, a5, a1, a2], a)
2060
+
2061
+ ## end
2062
+ b.insert(2, b3, b4, b5)
2063
+ assert_same_list([b1, b2, b3, b4, b5], b)
2064
+
2065
+ ## middle
2066
+ c.insert(1, c3, c4, c5)
2067
+ assert_same_list([c1, c3, c4, c5, c2], c)
2068
+ end
2069
+
2070
+ def test_insert_many_into_two
2071
+ a1, a2, a3, a4, a5, a6 = 6.of{C::Int.new}
2072
+ b1, b2, b3, b4, b5, b6 = 6.of{C::Int.new}
2073
+ c1, c2, c3, c4, c5, c6 = 6.of{C::Int.new}
2074
+ a = _List[a1, a2]
2075
+ b = _List[b1, b2]
2076
+ c = _List[c1, c2]
2077
+
2078
+ ## beginning
2079
+ a.insert(0, a3, a4, a5, a6)
2080
+ assert_same_list([a3, a4, a5, a6, a1, a2], a)
2081
+
2082
+ ## end
2083
+ b.insert(2, b3, b4, b5, b6)
2084
+ assert_same_list([b1, b2, b3, b4, b5, b6], b)
2085
+
2086
+ ## middle (after)
2087
+ c.insert(1, c3, c4, c5, c6)
2088
+ assert_same_list([c1, c3, c4, c5, c6, c2], c)
2089
+ end
2090
+
2091
+ def test_insert_attached
2092
+ ## one
2093
+ a1 = C::Int.new
2094
+ b1 = C::Int.new
2095
+ a = _List[a1]
2096
+ b = _List[b1]
2097
+ assert_same(a, a.insert(1, b1))
2098
+ assert_same_list([b1], b)
2099
+ assert_same(a1, a[0])
2100
+ assert_copy(b1, a[1])
2101
+
2102
+ ## many
2103
+ a1 = C::Int.new
2104
+ b1, b2, b3, b4 = 4.of{C::Int.new}
2105
+ a = _List[a1]
2106
+ b = _List[b1, b2, b3, b4]
2107
+ assert_same(a, a.insert(0, b1, b2, b3, b4))
2108
+ assert_same_list([b1, b2, b3, b4], b)
2109
+ assert_copy(b1, a[0])
2110
+ assert_copy(b2, a[1])
2111
+ assert_copy(b3, a[2])
2112
+ assert_copy(b4, a[3])
2113
+ assert_same(a1, a[4])
2114
+ end
2115
+
2116
+ ###
2117
+ ### ----------------------------------------------------------------
2118
+ ### concat
2119
+ ### ----------------------------------------------------------------
2120
+ ###
2121
+
2122
+ def test_concat_zero_on_zero
2123
+ a = _List[]
2124
+ b = _List[]
2125
+
2126
+ assert_same(a, a.concat(b))
2127
+ assert_same_list([], b)
2128
+ assert_same_list([], a)
2129
+ end
2130
+
2131
+ def test_concat_zero_on_one
2132
+ a1 = C::Int.new
2133
+ a = _List[a1]
2134
+ b = _List[]
2135
+
2136
+ assert_same(a, a.concat(b))
2137
+ assert_same_list([], b)
2138
+ assert_same_list([a1], a)
2139
+ end
2140
+
2141
+ def test_concat_zero_on_two
2142
+ a1, a2 = 2.of{C::Int.new}
2143
+ a = _List[a1, a2]
2144
+ b = _List[]
2145
+
2146
+ assert_same(a, a.concat(b))
2147
+ assert_same_list([], b)
2148
+ assert_equal(2, a.length)
2149
+ assert_same_list([a1, a2], a)
2150
+ end
2151
+
2152
+ def test_concat_one_on_zero
2153
+ b1 = C::Int.new
2154
+ a = _List[]
2155
+ b = _List[b1]
2156
+
2157
+ assert_same(a, a.concat(b))
2158
+ assert_same_list([b1], b)
2159
+ ##
2160
+ assert_equal(1, a.length)
2161
+ assert_copy(b1, a[0])
2162
+ end
2163
+
2164
+ def test_concat_one_on_one
2165
+ a1 = C::Int.new
2166
+ b1 = C::Int.new
2167
+ a = _List[a1]
2168
+ b = _List[b1]
2169
+
2170
+ assert_same(a, a.concat(b))
2171
+ assert_same_list([b1], b)
2172
+ ##
2173
+ assert_equal(2, a.length)
2174
+ assert_same(a1, a[0])
2175
+ assert_copy(b1, a[1])
2176
+ end
2177
+
2178
+ def test_concat_one_on_two
2179
+ a1, a2 = 2.of{C::Int.new}
2180
+ b1 = C::Int.new
2181
+ a = _List[a1, a2]
2182
+ b = _List[b1]
2183
+
2184
+ assert_same(a, a.concat(b))
2185
+ assert_same_list([b1], b)
2186
+ ##
2187
+ assert_equal(3, a.length)
2188
+ assert_same(a1, a[0])
2189
+ assert_same(a2, a[1])
2190
+ assert_copy(b1, a[2])
2191
+ end
2192
+
2193
+ def test_concat_two_on_zero
2194
+ b1, b2 = 2.of{C::Int.new}
2195
+ a = _List[]
2196
+ b = _List[b1, b2]
2197
+
2198
+ assert_same(a, a.concat(b))
2199
+ assert_same_list([b1, b2], b)
2200
+ ##
2201
+ assert_equal(2, a.length)
2202
+ assert_copy(b1, a[0])
2203
+ assert_copy(b2, a[1])
2204
+ end
2205
+
2206
+ def test_concat_two_on_one
2207
+ a1 = C::Int.new
2208
+ b1, b2 = 2.of{C::Int.new}
2209
+ a = _List[a1]
2210
+ b = _List[b1, b2]
2211
+
2212
+ assert_same(a, a.concat(b))
2213
+ assert_same_list([b1, b2], b)
2214
+ ##
2215
+ assert_equal(3, a.length)
2216
+ assert_same(a1, a[0])
2217
+ assert_copy(b1, a[1])
2218
+ assert_copy(b2, a[2])
2219
+ end
2220
+
2221
+ def test_concat_two_on_two
2222
+ a1, a2 = 2.of{C::Int.new}
2223
+ b1, b2 = 2.of{C::Int.new}
2224
+ a = _List[a1, a2]
2225
+ b = _List[b1, b2]
2226
+
2227
+ assert_same(a, a.concat(b))
2228
+ assert_same_list([b1, b2], b)
2229
+ ##
2230
+ assert_equal(4, a.length)
2231
+ assert_same(a1, a[0])
2232
+ assert_same(a2, a[1])
2233
+ assert_copy(b1, a[2])
2234
+ assert_copy(b2, a[3])
2235
+ end
2236
+
2237
+ ###
2238
+ ### ----------------------------------------------------------------
2239
+ ### delete_at
2240
+ ### ----------------------------------------------------------------
2241
+ ###
2242
+
2243
+ def test_delete_at_one
2244
+ a1 = C::Int.new
2245
+ a = _List[a1]
2246
+
2247
+ assert_same(a1, a.delete_at(0))
2248
+ assert_nil(a1.parent)
2249
+ assert_same_list([], a)
2250
+ end
2251
+
2252
+ def test_delete_at_two
2253
+ ## delete_at 0
2254
+ a1, a2 = 2.of{C::Int.new}
2255
+ a = _List[a1, a2]
2256
+ assert_same(a1, a.delete_at(0))
2257
+ assert_nil(a1.parent)
2258
+ assert_same_list([a2], a)
2259
+
2260
+ ## delete_at 1
2261
+ a1, a2 = 2.of{C::Int.new}
2262
+ a = _List[a1, a2]
2263
+ assert_same(a2, a.delete_at(1))
2264
+ assert_nil(a2.parent)
2265
+ assert_same_list([a1], a)
2266
+ end
2267
+
2268
+ def test_delete_at_three
2269
+ ## delete at 0
2270
+ a1, a2, a3 = 3.of{C::Int.new}
2271
+ a = _List[a1, a2, a3]
2272
+ assert_same(a1, a.delete_at(0))
2273
+ assert_nil(a1.parent)
2274
+ assert_same_list([a2, a3], a)
2275
+
2276
+ ## delete at 1
2277
+ a1, a2, a3 = 3.of{C::Int.new}
2278
+ a = _List[a1, a2, a3]
2279
+ assert_same(a2, a.delete_at(1))
2280
+ assert_nil(a2.parent)
2281
+ assert_same_list([a1, a3], a)
2282
+
2283
+ ## delete at 2
2284
+ a1, a2, a3 = 3.of{C::Int.new}
2285
+ a = _List[a1, a2, a3]
2286
+ assert_same(a3, a.delete_at(2))
2287
+ assert_nil(a3.parent)
2288
+ assert_same_list([a1, a2], a)
2289
+ end
2290
+
2291
+ def test_delete_at_four
2292
+ ## delete at 1
2293
+ a1, a2, a3, a4 = 4.of{C::Int.new}
2294
+ a = _List[a1, a2, a3, a4]
2295
+ assert_same(a2, a.delete_at(1))
2296
+ assert_nil(a2.parent)
2297
+ assert_same_list([a1, a3, a4], a)
2298
+ end
2299
+
2300
+ ###
2301
+ ### ----------------------------------------------------------------
2302
+ ### clear
2303
+ ### ----------------------------------------------------------------
2304
+ ###
2305
+
2306
+ def test_clear_empty
2307
+ assert_same(empty, empty.clear)
2308
+ assert_same_list([], empty)
2309
+ end
2310
+
2311
+ def test_clear_one
2312
+ a = one_els
2313
+ assert_same(one, one.clear)
2314
+ assert_same_list([], one)
2315
+ assert_nil(one.parent)
2316
+ end
2317
+
2318
+ def test_clear_two
2319
+ a, b = *two_els
2320
+ assert_same(two, two.clear)
2321
+ assert_same_list([], two)
2322
+ assert_nil(a.parent)
2323
+ assert_nil(b.parent)
2324
+ end
2325
+
2326
+ def test_clear_three
2327
+ a, b, c = *three_els
2328
+ assert_same(three, three.clear)
2329
+ assert_same_list([], three)
2330
+ assert_nil(a.parent)
2331
+ assert_nil(b.parent)
2332
+ assert_nil(c.parent)
2333
+ end
2334
+
2335
+ def test_clear_big
2336
+ l1, l2, l21, l22, l23, l230, a, b, c, d, e = *big_els
2337
+ assert_same(big, big.clear)
2338
+ assert_same_list([], big)
2339
+ assert_nil(a.parent)
2340
+ assert_nil(l1.parent)
2341
+ assert_nil(l2.parent)
2342
+ end
2343
+
2344
+ ###
2345
+ ### ----------------------------------------------------------------
2346
+ ### replace
2347
+ ### ----------------------------------------------------------------
2348
+ ###
2349
+
2350
+ def test_replace_none_with_none
2351
+ a = _List[]
2352
+ b = []
2353
+
2354
+ assert_same(a, a.replace(b))
2355
+ assert_same_list([], a)
2356
+ end
2357
+
2358
+ def test_replace_none_with_one
2359
+ b1 = C::Int.new
2360
+ a = _List[]
2361
+ b = [b1]
2362
+
2363
+ assert_same(a, a.replace(b))
2364
+ assert_same_list([b1], a)
2365
+ end
2366
+
2367
+ def test_replace_none_with_two
2368
+ b1, b2 = 2.of{C::Int.new}
2369
+ a = _List[]
2370
+ b = [b1, b2]
2371
+
2372
+ assert_same(a, a.replace(b))
2373
+ assert_same_list([b1, b2], a)
2374
+ end
2375
+
2376
+ def test_replace_one_with_none
2377
+ a1 = C::Int.new
2378
+ a = _List[a1]
2379
+ b = _List[]
2380
+
2381
+ assert_same(a, a.replace(b))
2382
+ assert_same_list([], a)
2383
+ assert_nil(a1.parent)
2384
+ end
2385
+
2386
+ def test_replace_one_with_one
2387
+ a1 = C::Int.new
2388
+ b1 = C::Int.new
2389
+ a = _List[a1]
2390
+ b = [b1]
2391
+
2392
+ assert_same(a, a.replace(b))
2393
+ assert_same_list([b1], a)
2394
+ assert_nil(a1.parent)
2395
+ end
2396
+
2397
+ def test_replace_one_with_two
2398
+ a1 = C::Int.new
2399
+ b1, b2 = 2.of{C::Int.new}
2400
+ a = _List[a1]
2401
+ b = [b1, b2]
2402
+
2403
+ assert_same(a, a.replace(b))
2404
+ assert_same_list([b1, b2], a)
2405
+ assert_nil(a1.parent)
2406
+ end
2407
+
2408
+ def test_replace_two_with_none
2409
+ a1, a2 = 2.of{C::Int.new}
2410
+ a = _List[a1, a2]
2411
+ b = _List[]
2412
+
2413
+ assert_same(a, a.replace(b))
2414
+ assert_same_list([], a)
2415
+ assert_nil(a1.parent)
2416
+ assert_nil(a2.parent)
2417
+ end
2418
+
2419
+ def test_replace_two_with_one
2420
+ a1, a2 = 2.of{C::Int.new}
2421
+ b1 = C::Int.new
2422
+ a = _List[a1, a2]
2423
+ b = [b1]
2424
+
2425
+ assert_same(a, a.replace(b))
2426
+ assert_same_list([b1], a)
2427
+ assert_nil(a1.parent)
2428
+ assert_nil(a2.parent)
2429
+ end
2430
+
2431
+ def test_replace_two_with_two
2432
+ a1, a2 = 2.of{C::Int.new}
2433
+ b1, b2 = 2.of{C::Int.new}
2434
+ a = _List[a1, a2]
2435
+ b = [b1, b2]
2436
+
2437
+ assert_same(a, a.replace(b))
2438
+ assert_same_list([b1, b2], a)
2439
+ assert_nil(a1.parent)
2440
+ assert_nil(a2.parent)
2441
+ end
2442
+
2443
+ def test_replace_with_attached
2444
+ ## one
2445
+ a1 = C::Int.new
2446
+ a = _List[a1]
2447
+ b1 = C::Int.new
2448
+ b = _List[b1]
2449
+ assert_same(a, a.replace(b))
2450
+ assert_same_list([b1], b)
2451
+ assert_equal(1, a.length)
2452
+ assert_copy(b1, a[0])
2453
+
2454
+ ## many
2455
+ a1 = C::Int.new
2456
+ a = _List[a1]
2457
+ b1, b2, b3, b4 = 4.of{C::Int.new}
2458
+ b = _List[b1, b2, b3, b4]
2459
+ assert_same(a, a.replace(b))
2460
+ assert_same_list([b1, b2, b3, b4], b)
2461
+ assert_equal(4, a.length)
2462
+ assert_copy(b1, a[0])
2463
+ assert_copy(b2, a[1])
2464
+ assert_copy(b3, a[2])
2465
+ assert_copy(b4, a[3])
2466
+ end
2467
+ end
2468
+
2469
+ ## Make concrete test classes.
2470
+ NodeListTest.submodules.each do |test_module|
2471
+ test_module.name =~ /^NodeList/ or
2472
+ raise "NodeListTest submodule name does not start with 'NodeList': #{test_module.name}"
2473
+
2474
+ %w[NodeArray NodeChain].each do |list_class|
2475
+ test_class = test_module.name.sub(/^NodeList/, list_class)
2476
+ eval "
2477
+ class #{test_class} < Test::Unit::TestCase
2478
+ def _List
2479
+ C::#{list_class}
2480
+ end
2481
+ include #{test_module}
2482
+ end
2483
+ "
2484
+ end
2485
+ end