File: list_test.rb

package info (click to toggle)
ruby-linked-list 0.0.14-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 156 kB
  • sloc: ruby: 957; sh: 4; makefile: 4
file content (756 lines) | stat: -rw-r--r-- 19,487 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
require 'test_helper'

describe LinkedList::List do
  let(:list)   { create_list }
  let(:node_1) { create_node('foo') }
  let(:node_2) { create_node('bar') }
  let(:node_3) { create_node('baz') }

  describe 'instantiation' do
    it 'assigns first to nil' do
      assert_nil list.first
    end

    it 'assigns last to nil' do
      assert_nil list.last
    end

    it 'has zero length' do
      assert_equal 0, list.length
    end
  end

  describe '#push' do
    it 'last pushed node can be accessed with #last' do
      list.push(node_1)
      assert_equal node_1.data, list.last
    end

    it 'last pushed node data can be accessed with #first' do
      list.push(node_1)
      assert_equal node_1.data, list.first
    end

    it 'maintains first pushed node as first' do
      list.push(node_1)
      list.push(node_2)
      assert_equal node_1.data, list.first
    end

    it 'sets reference to the next node' do
      list.push(node_1)
      list.push(node_2)
      assert_equal node_1.next, node_2
    end

    it 'sets reference to the prev node' do
      list.push(node_1)
      list.push(node_2)
      assert_equal node_2.prev, node_1
    end

    it 'increases list length by 1' do
      list.push(node_1)
      assert_equal 1, list.length
    end

    it 'returns self' do
      assert_equal list.push(node_1), list
    end
  end

  describe '#unshift' do
    it 'last pushed node can be accessed with #last' do
      list.unshift(node_1)
      assert_equal node_1.data, list.last
    end

    it 'last pushed node can be accessed with #first' do
      list.unshift(node_1)
      assert_equal node_1.data, list.first
    end

    it 'maintains first pushed node as last' do
      list.unshift(node_1)
      list.unshift(node_2)
      assert_equal node_1.data, list.last
    end

    it 'sets reference to the next node' do
      list.unshift(node_1)
      list.unshift(node_2)
      assert_equal node_2.next, node_1
    end

    it 'sets reference to the prev node' do
      list.unshift(node_1)
      list.unshift(node_2)
      assert_equal node_1.prev, node_2
    end

    it 'increases list length by 1' do
      list.unshift(node_1)
      assert_equal 1, list.length
    end

    it 'returns self' do
      assert_equal list.unshift(node_1), list
    end
  end

  describe '#pop' do
    it 'returns nil if list is empty' do
      assert_nil list.pop
    end

    it 'returns node from the end of the list' do
      list.push(node_1)
      list.push(node_2)
      assert_equal node_2.data, list.pop
    end

    it 'sets #last to nil when all nodes are removed' do
      list.push(node_1)
      list.pop
      assert_nil list.last
    end

    it 'sets #first to nil when all nodes are removed' do
      list.push(node_1)
      list.pop
      assert_nil list.first
    end

    it 'reduces list length by 1' do
      list.push(node_1)
      list.pop
      assert_equal 0, list.length
    end

    it 'maintains list when multiple nodes are removed' do
      list.push(node_1)
      list.push(node_2)
      list.pop
      assert_equal [node_1.data], list.to_a
    end
  end

  describe '#insert' do
    it 'raises error if after and before are passed' do
      err = assert_raises ArgumentError do
        assert_nil list.insert(1, after: 'x', before: 'y')
      end
      assert_equal err.message, 'either :after or :before keys should be passed'
    end

    it 'raises error if no after nore before are passed' do
      err = assert_raises ArgumentError do
        assert_nil list.insert(1)
      end
      assert_equal err.message, 'either :after or :before keys should be passed'
    end

    describe 'after:' do
      describe 'by block' do
        it 'does not add value if insert after not found' do
          list.push('foo')
          assert_nil list.insert(1, after: ->(d) { d == 'foo1' })
          assert_equal ['foo'], list.to_a
        end

        it 'inserts value after first matching node by block' do
          list.push('foo')
          list.push('bar')
          assert_equal 1, list.insert(1, after: ->(d) { d == 'foo' })
          assert_equal ['foo', 1, 'bar'], list.to_a
          assert_equal 3, list.length
        end

        describe 'position edge cases' do
          before do
            list.push(0)
            list.push(1)
            list.push(2)
          end

          it 'inserts after in the middle' do
            list.insert('foo', after: ->(d) { d == 0 })
            assert_equal [0, 'foo', 1, 2], list.to_a
          end

          it 'inserts after the tail' do
            list.insert('foo', after: ->(d) { d == 2 })
            assert_equal [0, 1, 2, 'foo'], list.to_a
            assert_equal 'foo', list.last
          end
        end
      end

      describe 'by value' do
        it 'does not add value if insert after not found' do
          list.push('foo')
          assert_nil list.insert(1, after: 'foo1')
          assert_equal ['foo'], list.to_a
        end

        it 'inserts value after first matching node by block' do
          list.push('foo')
          list.push('bar')
          assert_equal 1, list.insert(1, after: 'foo')
          assert_equal ['foo', 1, 'bar'], list.to_a
          assert_equal 3, list.length
        end

        describe 'position edge cases' do
          before do
            list.push(0)
            list.push(1)
            list.push(2)
          end

          it 'inserts after in the middle' do
            list.insert('foo', after: 0)
            assert_equal [0, 'foo', 1, 2], list.to_a
          end

          it 'inserts after the tail' do
            list.insert('foo', after: 2)
            assert_equal [0, 1, 2, 'foo'], list.to_a
            assert_equal 'foo', list.last
          end
        end
      end
    end

    describe ':before' do
      describe 'by block' do
        it 'does not add value if insert before not found' do
          list.push('foo')
          assert_nil list.insert(1, before: ->(d) { d == 'foo1' })
          assert_equal ['foo'], list.to_a
        end

        it 'inserts value before first matching node by block' do
          list.push('foo')
          list.push('bar')
          assert_equal 1, list.insert(1, before: ->(d) { d == 'foo' })
          assert_equal [1, 'foo', 'bar'], list.to_a
          assert_equal 3, list.length
        end

        describe 'position edge cases' do
          before do
            list.push(0)
            list.push(1)
            list.push(2)
          end

          it 'inserts before head' do
            list.insert('foo', before: ->(d) { d == 0 })
            assert_equal ['foo', 0, 1, 2], list.to_a
            assert_equal 'foo', list.first
          end

          it 'inserts before in the middle' do
            list.insert('foo', before: ->(d) { d == 2 })
            assert_equal [0, 1, 'foo', 2], list.to_a
          end
        end
      end

      describe 'by value' do
        it 'does not add value if insert before not found' do
          list.push('foo')
          assert_nil list.insert(1, before: 'foo1')
          assert_equal ['foo'], list.to_a
        end

        it 'inserts value before first' do
          list.push('foo')
          list.push('bar')
          assert_equal 1, list.insert(1, before: 'foo')
          assert_equal [1, 'foo', 'bar'], list.to_a
          assert_equal 3, list.length
        end

        describe 'position edge cases' do
          before do
            list.push(0)
            list.push(1)
            list.push(2)
          end

          it 'inserts before head' do
            list.insert('foo', before: 0)
            assert_equal ['foo', 0, 1, 2], list.to_a
            assert_equal 'foo', list.first
          end

          it 'inserts before in the middle' do
            list.insert('foo', before: 2)
            assert_equal [0, 1, 'foo', 2], list.to_a
          end
        end
      end
    end
  end

  describe '#insert_after_node' do
    it 'inserts value after passed node' do
      list.push('foo')
      list.push('bar')
      node = list.each_node.find { |n| n.data == 'foo' }
      assert_equal 1, list.insert_after_node(1, node).data
      assert_equal ['foo', 1, 'bar'], list.to_a
      assert_equal 3, list.length
    end

    describe 'position edge cases' do
      before do
        list.push(0)
        list.push(1)
        list.push(2)
      end

      it 'inserts after in the middle' do
        node = list.each_node.find { |n| n.data == 0 }
        list.insert_after_node('foo', node)
        assert_equal [0, 'foo', 1, 2], list.to_a
      end

      it 'inserts after the tail' do
        node = list.each_node.find { |n| n.data == 2 }
        list.insert_after_node('foo', node)
        assert_equal [0, 1, 2, 'foo'], list.to_a
        assert_equal 'foo', list.last
      end
    end
  end

  describe '#insert_after_node' do
    it 'inserts value before first' do
      list.push('foo')
      list.push('bar')
      node = list.each_node.find { |n| n.data == 'foo' }
      assert_equal 1, list.insert_before_node(1, node).data
      assert_equal [1, 'foo', 'bar'], list.to_a
      assert_equal 3, list.length
    end

    describe 'position edge cases' do
      before do
        list.push(0)
        list.push(1)
        list.push(2)
      end

      it 'inserts before head' do
        node = list.each_node.find { |n| n.data == 0 }
        list.insert_before_node('foo', node)
        assert_equal ['foo', 0, 1, 2], list.to_a
        assert_equal 'foo', list.first
      end

      it 'inserts before in the middle' do
        node = list.each_node.find { |n| n.data == 2 }
        list.insert_before_node('foo', node)
        assert_equal [0, 1, 'foo', 2], list.to_a
      end
    end
  end

  describe '#delete' do
    it 'raises error if block and value are passed' do
      err = assert_raises ArgumentError do
        assert_nil list.delete('x') { |d| d == 'x' }
      end
      assert_equal err.message, 'either value or block should be passed'
    end

    describe 'by block' do
      it 'returns nil if list is empty' do
        assert_nil list.delete { |d| d == 'x' }
      end

      it 'deletes value in first matching node' do
        calls_count = 0
        list.push('foo')
        list.push('foo')
        list.push('bar')
        list.push('foo')
        list.delete { |d| calls_count += 1;d == 'bar' }
        assert_equal ['foo', 'foo', 'foo'], list.to_a
        assert_equal 3, calls_count
      end

      it 'returns deleted value' do
        list.push('foo')
        assert_equal 'foo', list.delete { |d| d == 'foo' }
      end

      it 'decreases length of list' do
        list.push('foo')
        list.push('bar')
        list.push('foo')
        list.delete { |d| d == 'foo' }
        assert_equal 2, list.length
        assert_equal ['bar', 'foo'], list.to_a
      end

      describe 'position edge cases' do
        before do
          list.push(0)
          list.push(1)
          list.push(2)
        end

        it 'deletes value from head' do
          list.delete { |d| d == 0 }
          assert_equal [1, 2], list.to_a
          assert_equal 1, list.first
        end

        it 'deletes value from middle' do
          list.delete { |d| d == 1 }
          assert_equal [0, 2], list.to_a
        end

        it 'deletes value from tail' do
          list.delete { |d| d == 2 }
          assert_equal [0, 1], list.to_a
          assert_equal 1, list.last
        end
      end
    end

    describe 'by data equality' do
      it 'returns nil if list is empty' do
        assert_nil list.delete('x')
      end

      it 'deletes value in first node' do
        list.push('foo')
        list.push('foo')
        list.push('bar')
        list.push('foo')
        list.delete('foo')
        assert_equal ['foo', 'bar', 'foo'], list.to_a
      end

      it 'returns deleted value' do
        list.push('foo')
        assert_equal 'foo', list.delete('foo')
      end

      it 'decreases length of list' do
        list.push('foo')
        list.push('bar')
        list.push('foo')
        list.delete('foo')
        assert_equal 2, list.length
        assert_equal ['bar', 'foo'], list.to_a
      end

      describe 'position edge cases' do
        before do
          list.push(0)
          list.push(1)
          list.push(2)
        end

        it 'deletes value from head' do
          list.delete(0)
          assert_equal [1, 2], list.to_a
          assert_equal 1, list.first
        end

        it 'deletes value from middle' do
          list.delete(1)
          assert_equal [0, 2], list.to_a
        end


        it 'deletes value from tail' do
          list.delete(2)
          assert_equal [0, 1], list.to_a
          assert_equal 1, list.last
        end
      end
    end

    describe 'by node' do
      it 'deletes first node' do
        list.push(node_1)
        list.push(node_2)
        list.push(node_3)
        list.delete(node_1)
        assert_equal ['bar', 'baz'], list.to_a
      end

      it 'returns deleted value' do
        list.push(node_1)
        list.delete(node_1)
        assert_equal node_1.data, list.delete(node_1)
      end

      it 'decreases length of list' do
        list.push('foo')
        list.push('bar')
        list.push('baz')
        list.delete('foo')
        assert_equal 2, list.length
      end

      describe 'position edge cases' do
        before do
          list.push(node_1)
          list.push(node_2)
          list.push(node_3)
        end

        it 'deletes value from head' do
          list.delete(node_1)
          assert_equal ['bar', 'baz'], list.to_a
          assert_equal 'bar', list.first
        end

        it 'deletes value from middle' do
          list.delete(node_2)
          assert_equal ['foo', 'baz'], list.to_a
        end


        it 'deletes value from tail' do
          list.delete(node_3)
          assert_equal ['foo', 'bar'], list.to_a
          assert_equal 'bar', list.last
        end
      end
    end
  end

  describe '#delete_all' do
    it 'raises error if block and value are passed' do
      err = assert_raises ArgumentError do
        assert_nil list.delete_all('x') { |d| d == 'x' }
      end
      assert_equal err.message, 'either value or block should be passed'
    end

    describe 'by block' do
      it 'returns nil if list is empty' do
        assert_equal list.delete_all { |d| d == 'x' }, []
      end

      it 'deletes value in first matching node' do
        list.push('foo')
        list.push('foo')
        list.push('bar')
        list.push('foo')
        list.delete_all { |d| d == 'foo' }
        assert_equal ['bar'], list.to_a
      end

      it 'returns deleted value' do
        list.push('foo')
        assert_equal ['foo'], list.delete_all { |d| d == 'foo' }
      end

      it 'decreases length of list' do
        list.push('foo')
        list.push('bar')
        list.push('foo')
        list.delete_all { |d| d == 'foo' }
        assert_equal 1, list.length
        assert_equal ['bar'], list.to_a
      end
    end

    describe 'by data equality' do
      it 'returns nil if list is empty' do
        assert_nil list.delete('x')
      end

      it 'deletes all matched values' do
        list.push('foo')
        list.push('foo')
        list.push('bar')
        list.push('foo')
        list.delete_all('foo')
        assert_equal ['bar'], list.to_a
      end

      it 'returns deleted value' do
        list.push('foo')
        assert_equal ['foo'], list.delete_all('foo')
      end

      it 'decreases length of list' do
        list.push('foo')
        list.push('bar')
        list.push('foo')
        list.delete_all('foo')
        assert_equal 1, list.length
        assert_equal ['bar'], list.to_a
      end
    end
  end

  describe '#shift' do
    it 'returns nil if list is empty' do
      assert_nil list.shift
    end

    it 'returns node from the top of the list' do
      list.push(node_1)
      list.push(node_2)
      assert_equal node_1.data, list.shift
    end

    it 'sets #last to nil when all nodes are removed' do
      list.push(node_1)
      list.shift
      assert_nil list.last
    end

    it 'sets #first to nil when all nodes are removed' do
      list.push(node_1)
      list.shift
      assert_nil list.first
    end

    it 'reduces list length by 1' do
      list.push(node_1)
      list.shift
      assert_equal 0, list.length
    end
  end

  describe '#reverse' do
    it 'returns new empty list when receiver list is empty' do
      refute_equal list, list.reverse
    end

    it 'returns new list in reverse order' do
      list.push(node_1)
      refute_equal list.reverse, list.reverse!
    end

    it 'reverses order of nodes' do
      list.push(node_1)
      list.push(node_2)
      new_list = list.reverse
      assert_equal %w(bar foo), [new_list.first, new_list.last]
    end
  end

  describe '#reverse!' do
    it 'returns self when list is empty' do
      assert_equal list, list.reverse!
    end

    it 'reverses order of nodes' do
      list.push(node_1)
      list.push(node_2)
      list.reverse!
      assert_equal [node_2.data, node_1.data], [list.first, list.last]
    end

    it 'returns same object' do
      list.push(node_1)
      assert_equal list, list.reverse!
    end
  end

  describe '#each_node' do
    it 'returns enumerator if no block given' do
      assert_instance_of Enumerator, list.each_node
    end

    it 'pass each node data to the block' do
      list.push(node_1)
      list.push(node_2)
      nodes = []
      list.each_node { |e| nodes << e }
      assert_equal %w(foo bar), nodes.map(&:data)
      assert_equal true, nodes.all? { |n| n.is_a?(LinkedList::Node) }
    end
  end

  describe '#each' do
    it 'returns enumerator if no block given' do
      assert_instance_of Enumerator, list.each
    end

    it 'pass each node data to the block' do
      list.push(node_1)
      list.push(node_2)
      nodes = []
      list.each { |e| nodes << e }
      assert_equal %w(foo bar), nodes
    end
  end

  describe '#each_node' do
    it 'returns enumerator if no block given' do
      assert_instance_of Enumerator, list.each
    end

    it 'pass each node data to the block' do
      list.push(node_1)
      list.push(node_2)
      nodes = []
      list.each_node { |e| nodes << e }
      assert_equal %w(foo bar), nodes.map(&:data)
    end
  end

  describe '#reverse_each' do
    it 'returns enumerator if no block given' do
      assert_instance_of Enumerator, list.each
    end

    it 'pass each node data to the block' do
      list.push(node_1)
      list.push(node_2)
      nodes = []
      list.reverse_each { |e| nodes << e }
      assert_equal %w(bar foo), nodes
    end
  end

  describe '#reverse_each_node' do
    it 'returns enumerator if no block given' do
      assert_instance_of Enumerator, list.each
    end

    it 'pass each node data to the block' do
      list.push(node_1)
      list.push(node_2)
      nodes = []
      list.reverse_each_node { |e| nodes << e }
      assert_equal %w(bar foo), nodes.map(&:data)
    end
  end

  describe '#inspect' do
    it 'includes class name' do
      assert_match(/LinkedList::List/, list.inspect)
    end

    it 'includes object id' do
      assert_match(/#{list.object_id.to_s(16)}/, list.inspect)
    end

    it 'includes node values' do
      list.push(node_1)
      assert_match(/foo/, list.inspect)
    end
  end

  describe 'conversion' do
    it '#to_list returns self' do
      assert_equal list, list.to_list
    end
  end
end