@@ -65,117 +65,6 @@ describe('DoublyLinkedList', () => {
65
65
} ) ;
66
66
} ) ;
67
67
68
- describe ( 'isEmpty' , ( ) => {
69
- it ( 'works correctly' , ( ) => {
70
- // Arrange
71
- expect ( doublyLinkedList . isEmpty ) . toBeTruthy ( ) ;
72
- doublyLinkedList . append ( 1 ) ;
73
-
74
- // Act and Assert
75
- expect ( doublyLinkedList . isEmpty ) . toBeFalsy ( ) ;
76
- } ) ;
77
- } ) ;
78
-
79
- describe ( 'toString' , ( ) => {
80
- it ( 'converts a list to string' , ( ) => {
81
- doublyLinkedList . append ( 1 ) . append ( 2 ) ;
82
-
83
- expect ( doublyLinkedList . toString ( ) ) . toBe ( '1,2' ) ;
84
- } ) ;
85
- it ( 'converts a list to string with custom callback' , ( ) => {
86
- // Arrange
87
- type NodeValue = {
88
- key : string ;
89
- value : number ;
90
- } ;
91
-
92
- const list = new DoublyLinkedList < NodeValue > ( ) ;
93
-
94
- list
95
- . append ( {
96
- key : 'one' ,
97
- value : 1 ,
98
- } )
99
- . append ( {
100
- key : 'two' ,
101
- value : 2 ,
102
- } ) ;
103
-
104
- // Act
105
- const received = list . toString ( ( node ) => `${ node . value } ` ) ;
106
-
107
- // Assert
108
- expect ( received ) . toBe ( '1,2' ) ;
109
- } ) ;
110
- } ) ;
111
-
112
- describe ( 'fromArray' , ( ) => {
113
- it ( 'creates an empty list when an empty array is passed' , ( ) => {
114
- // Act
115
- doublyLinkedList . fromArray ( [ ] ) ;
116
-
117
- // Assert
118
- expect ( doublyLinkedList . head ) . toBeNull ( ) ;
119
- expect ( doublyLinkedList . tail ) . toBeNull ( ) ;
120
- expect ( doublyLinkedList . size ) . toBe ( 0 ) ;
121
- } ) ;
122
-
123
- it ( 'creates a list with the same nodes as the input array' , ( ) => {
124
- // Act
125
- doublyLinkedList . fromArray ( [ 1 , 2 ] ) ;
126
-
127
- // Assert
128
- expect ( doublyLinkedList . toString ( ) ) . toBe ( '1,2' ) ;
129
- } ) ;
130
- } ) ;
131
-
132
- describe ( 'Iterator' , ( ) => {
133
- it ( 'handles an empty list' , ( ) => {
134
- // Act
135
- const received = Array . from ( doublyLinkedList ) ;
136
-
137
- // Assert
138
- expect ( received ) . toEqual ( [ ] ) ;
139
- } ) ;
140
- it ( 'iterates through the elements of the list' , ( ) => {
141
- // Arrange
142
- const expected = [ 1 , 2 , 3 ] ;
143
-
144
- doublyLinkedList . fromArray ( expected ) ;
145
-
146
- // Act
147
- const received = Array . from ( doublyLinkedList , ( node ) => node . data ) ;
148
-
149
- // Assert
150
- expect ( received ) . toEqual ( expected ) ;
151
- } ) ;
152
- } ) ;
153
-
154
- describe ( 'toArray' , ( ) => {
155
- it ( 'converts an empty list to an array' , ( ) => {
156
- // Act
157
- const received = doublyLinkedList . toArray ( ) ;
158
-
159
- // Assert
160
- expect ( received ) . toEqual ( [ ] ) ;
161
- expect ( doublyLinkedList . head ) . toBeNull ( ) ;
162
- expect ( doublyLinkedList . tail ) . toBeNull ( ) ;
163
- expect ( doublyLinkedList . size ) . toBe ( 0 ) ;
164
- } ) ;
165
-
166
- it ( 'converts a list to an array' , ( ) => {
167
- // Arrange
168
- const expected = [ 1 , 2 ] ;
169
- doublyLinkedList . fromArray ( expected ) ;
170
-
171
- // Act
172
- const received = doublyLinkedList . toArray ( ) ;
173
-
174
- // Assert
175
- expect ( received ) . toEqual ( expected ) ;
176
- } ) ;
177
- } ) ;
178
-
179
68
describe ( 'prepend' , ( ) => {
180
69
it ( 'prepends a new node to the beginning of the empty list' , ( ) => {
181
70
// Arrange
@@ -424,6 +313,7 @@ describe('DoublyLinkedList', () => {
424
313
const received = ( ) => doublyLinkedList . insertAt ( - 1 , 1 ) ;
425
314
426
315
// Assert
316
+ expect ( received ) . toThrow ( RangeError ) ;
427
317
expect ( received ) . toThrow (
428
318
'Index should be greater than or equal to 0 and less than or equal to the list length.' ,
429
319
) ;
@@ -434,6 +324,7 @@ describe('DoublyLinkedList', () => {
434
324
const received = ( ) => doublyLinkedList . insertAt ( 10 , 1 ) ;
435
325
436
326
// Assert
327
+ expect ( received ) . toThrow ( RangeError ) ;
437
328
expect ( received ) . toThrow (
438
329
'Index should be greater than or equal to 0 and less than or equal to the list length.' ,
439
330
) ;
@@ -592,123 +483,4 @@ describe('DoublyLinkedList', () => {
592
483
expect ( doublyLinkedList . size ) . toBe ( 1 ) ;
593
484
} ) ;
594
485
} ) ;
595
-
596
- describe ( 'indexOf' , ( ) => {
597
- it ( 'returns -1 for an empty list' , ( ) => {
598
- // Act and Assert
599
- expect ( doublyLinkedList . indexOf ( 42 ) ) . toBe ( - 1 ) ;
600
- } ) ;
601
-
602
- beforeEach ( ( ) => {
603
- // Arrange
604
- doublyLinkedList . fromArray ( [ 1 , 2 , 3 , 3 , 4 ] ) ;
605
- } ) ;
606
-
607
- it ( 'returns -1 for a value not present in the list' , ( ) => {
608
- // Act and Assert
609
- expect ( doublyLinkedList . indexOf ( 42 ) ) . toBe ( - 1 ) ;
610
- } ) ;
611
-
612
- it ( 'returns the correct index for a value present in the list' , ( ) => {
613
- // Act and Assert
614
- expect ( doublyLinkedList . indexOf ( 2 ) ) . toBe ( 1 ) ;
615
- } ) ;
616
-
617
- it ( 'returns the index of the first occurrence of the value' , ( ) => {
618
- // Act and Assert
619
- expect ( doublyLinkedList . indexOf ( 3 ) ) . toBe ( 2 ) ;
620
- } ) ;
621
-
622
- it ( 'returns the correct index for the head value' , ( ) => {
623
- // Act and Assert
624
- expect ( doublyLinkedList . indexOf ( 1 ) ) . toBe ( 0 ) ;
625
- } ) ;
626
-
627
- it ( 'returns the correct index for the tail value' , ( ) => {
628
- // Act and Assert
629
- expect ( doublyLinkedList . indexOf ( 4 ) ) . toBe ( 4 ) ;
630
- } ) ;
631
-
632
- it ( 'handles custom objects and comparison correctly' , ( ) => {
633
- type Item = {
634
- key : string ;
635
- } ;
636
- // Arrange
637
- const list = new DoublyLinkedList < Item > ( ) . fromArray ( [
638
- { key : 'value1' } ,
639
- { key : 'value2' } ,
640
- ] ) ;
641
-
642
- // Act and Assert
643
- expect ( list . indexOf ( { key : 'value1' } ) ) . toBe ( 0 ) ;
644
- } ) ;
645
- } ) ;
646
-
647
- describe ( 'find' , ( ) => {
648
- it ( 'returns null for a not-found node' , ( ) => {
649
- // Act and Assert
650
- expect ( doublyLinkedList . find ( 1 ) ) . toBeNull ( ) ;
651
- expect ( doublyLinkedList . find ( ( value ) => value === 100 ) ) . toBeNull ( ) ;
652
- } ) ;
653
-
654
- it ( 'finds a node by value' , ( ) => {
655
- // Arrange
656
- doublyLinkedList . fromArray ( [ 1 , 2 ] ) ;
657
-
658
- // Act
659
- const foundedNode = doublyLinkedList . find ( 2 ) ;
660
-
661
- // Assert
662
- expect ( foundedNode ?. data ) . toBe ( 2 ) ;
663
- } ) ;
664
-
665
- it ( 'finds a node by predicate' , ( ) => {
666
- // Arrange
667
- doublyLinkedList . fromArray ( [ 1 , 2 , 3 ] ) ;
668
-
669
- // Act
670
- const foundedNode = doublyLinkedList . find ( ( value ) => value > 2 ) ;
671
-
672
- // Assert
673
- expect ( foundedNode ?. data ) . toBe ( 3 ) ;
674
- } ) ;
675
-
676
- it ( 'prioritizes predicate over value' , ( ) => {
677
- // Arrange
678
- doublyLinkedList . fromArray ( [ 1 , 2 ] ) ;
679
-
680
- // Act
681
- const foundedNode = doublyLinkedList . find ( ( value ) => value > 1 ) ;
682
-
683
- // Assert
684
- expect ( foundedNode ?. data ) . toBe ( 2 ) ;
685
- } ) ;
686
-
687
- it ( 'returns the first node if multiple nodes match the predicate' , ( ) => {
688
- // Arrange
689
- doublyLinkedList . fromArray ( [ 1 , 2 , 3 , 4 ] ) ;
690
-
691
- // Act
692
- const foundedNode = doublyLinkedList . find ( ( value ) => value > 1 ) ;
693
-
694
- // Assert
695
- expect ( foundedNode ?. data ) . toBe ( 2 ) ;
696
- } ) ;
697
- } ) ;
698
-
699
- describe ( 'clear' , ( ) => {
700
- it ( 'removes all nodes from the linked list' , ( ) => {
701
- // Arrange
702
- doublyLinkedList . fromArray ( [ 1 , 2 , 3 ] ) ;
703
-
704
- // Act
705
- doublyLinkedList . clear ( ) ;
706
-
707
- // Assert
708
- expect ( doublyLinkedList . head ) . toBeNull ( ) ;
709
- expect ( doublyLinkedList . tail ) . toBeNull ( ) ;
710
- expect ( doublyLinkedList . size ) . toBe ( 0 ) ;
711
- expect ( doublyLinkedList . isEmpty ) . toBeTruthy ( ) ;
712
- } ) ;
713
- } ) ;
714
486
} ) ;
0 commit comments