casty 0.3.1

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