1
+ package problems .impl ;
2
+
3
+ import org .junit .Test ;
4
+
5
+ import static org .junit .Assert .*;
6
+ import static problems .impl .DoublyLinkedList .*;
7
+
8
+ public class DoublyLinkedListTest {
9
+
10
+ @ Test
11
+ public void insertAtHead () {
12
+ DoublyLinkedList list = new DoublyLinkedList ();
13
+ list .insertAtHead (1 );
14
+ assertEquals (1 , list .getLength ());
15
+ Node head = list .getHead ();
16
+ assertEquals (1 , head .getValue ());
17
+ assertNull (head .getNext ());
18
+ assertNull (head .getPrevious ());
19
+ assertEquals (list .getHead (), list .getTail ());
20
+ }
21
+
22
+ @ Test
23
+ public void insertAtHeadTwice () {
24
+ DoublyLinkedList list = new DoublyLinkedList ();
25
+ list .insertAtHead (2 );
26
+ list .insertAtHead (1 );
27
+ assertEquals (2 , list .getLength ());
28
+ assertEquals (1 , list .getHead ().getValue ());
29
+ assertEquals (2 , list .getTail ().getValue ());
30
+ }
31
+
32
+ @ Test
33
+ public void insertAtTail () {
34
+ DoublyLinkedList list = new DoublyLinkedList ();
35
+ list .insertAtTail (1 );
36
+ assertEquals (1 , list .getLength ());
37
+ Node head = list .getHead ();
38
+ assertEquals (1 , head .getValue ());
39
+ assertNull (head .getNext ());
40
+ assertNull (head .getPrevious ());
41
+ assertEquals (list .getHead (), list .getTail ());
42
+ }
43
+
44
+ @ Test
45
+ public void insertAtTailTwice () {
46
+ DoublyLinkedList list = new DoublyLinkedList ();
47
+ list .insertAtTail (1 );
48
+ list .insertAtTail (2 );
49
+ assertEquals (2 , list .getLength ());
50
+ assertEquals (1 , list .getHead ().getValue ());
51
+ assertEquals (2 , list .getTail ().getValue ());
52
+ }
53
+
54
+ @ Test
55
+ public void insertAtZero () {
56
+ DoublyLinkedList list = new DoublyLinkedList ();
57
+ list .insertAt (1 , 0 );
58
+ assertEquals (1 , list .getLength ());
59
+ Node head = list .getHead ();
60
+ assertEquals (1 , head .getValue ());
61
+ assertNull (head .getNext ());
62
+ assertNull (head .getPrevious ());
63
+ assertEquals (list .getHead (), list .getTail ());
64
+ }
65
+
66
+ @ Test
67
+ public void insertAtIndexAtEndOfList () {
68
+ DoublyLinkedList list = new DoublyLinkedList ();
69
+ list .insertAt (1 , 0 );
70
+ assertEquals (1 , list .getLength ());
71
+ Node head = list .getHead ();
72
+ assertEquals (1 , head .getValue ());
73
+ assertNull (head .getNext ());
74
+ assertNull (head .getPrevious ());
75
+ assertEquals (list .getHead (), list .getTail ());
76
+ list .insertAt (2 , 1 );
77
+ assertEquals (2 , list .getLength ());
78
+ assertEquals (1 , list .getHead ().getValue ());
79
+ assertEquals (2 , list .getTail ().getValue ());
80
+ }
81
+
82
+ @ Test
83
+ public void insertAtOneThrows () {
84
+ DoublyLinkedList list = new DoublyLinkedList ();
85
+ try {
86
+ list .insertAt (1 , 1 );
87
+ } catch (Exception e ) {
88
+ // expected
89
+ return ;
90
+ }
91
+ fail ();
92
+ }
93
+
94
+ @ Test
95
+ public void insertAtNegativeIndexThrows () {
96
+ DoublyLinkedList list = new DoublyLinkedList ();
97
+ try {
98
+ list .insertAt (1 , -1 );
99
+ } catch (Exception e ) {
100
+ // expected
101
+ return ;
102
+ }
103
+ fail ();
104
+ }
105
+
106
+ @ Test
107
+ public void isEmpty () {
108
+ DoublyLinkedList list = new DoublyLinkedList ();
109
+ assertTrue (list .isEmpty ());
110
+ }
111
+
112
+ @ Test
113
+ public void isNotEmpty () {
114
+ DoublyLinkedList list = new DoublyLinkedList ();
115
+ list .insertAtHead (1 );
116
+ assertFalse (list .isEmpty ());
117
+ }
118
+
119
+ @ Test
120
+ public void removeFirstOccurrenceEmptyList () {
121
+ DoublyLinkedList list = new DoublyLinkedList ();
122
+ assertTrue (list .isEmpty ());
123
+ list .removeFirstOccurrence (1 );
124
+ assertTrue (list .isEmpty ());
125
+ }
126
+
127
+ @ Test
128
+ public void removeFirstOccurrenceRemovesElement () {
129
+ DoublyLinkedList list = new DoublyLinkedList ();
130
+ list .insertAtHead (1 );
131
+ assertFalse (list .isEmpty ());
132
+ list .removeFirstOccurrence (1 );
133
+ assertTrue (list .isEmpty ());
134
+ assertNull (list .getHead ());
135
+ assertEquals (list .getHead (), list .getTail ());
136
+ }
137
+
138
+ @ Test
139
+ public void removesOnlyFirstOccurrenceLeavesOthers () {
140
+ DoublyLinkedList list = new DoublyLinkedList ();
141
+ list .insertAtHead (1 );
142
+ list .insertAtHead (2 );
143
+ list .insertAtHead (1 );
144
+ assertFalse (list .isEmpty ());
145
+ list .removeFirstOccurrence (1 );
146
+ assertFalse (list .isEmpty ());
147
+ assertEquals (2 , list .getLength ());
148
+ assertEquals (2 , list .getHead ().getValue ());
149
+ assertEquals (1 , list .getTail ().getValue ());
150
+ }
151
+
152
+ @ Test
153
+ public void removeAtForEmptyListThrows () {
154
+ DoublyLinkedList list = new DoublyLinkedList ();
155
+ try {
156
+ list .removeAt (0 );
157
+ } catch (Exception e ) {
158
+ // expected
159
+ return ;
160
+ }
161
+
162
+ fail ();
163
+ }
164
+
165
+ @ Test
166
+ public void removeAtNegativeIndexThrows () {
167
+ DoublyLinkedList list = new DoublyLinkedList ();
168
+ list .insertAtHead (1 );
169
+ try {
170
+ list .removeAt (-1 );
171
+ } catch (Exception e ) {
172
+ // expected
173
+ return ;
174
+ }
175
+
176
+ fail ();
177
+ }
178
+
179
+ @ Test
180
+ public void removeAtZeroForSingleElementList () {
181
+ DoublyLinkedList list = new DoublyLinkedList ();
182
+ list .insertAtHead (1 );
183
+ assertEquals (1 , list .getLength ());
184
+ assertEquals (1 , list .getHead ().getValue ());
185
+ assertEquals (list .getHead (), list .getTail ());
186
+ list .removeAt (0 );
187
+ assertEquals (0 , list .getLength ());
188
+ assertNull (list .getHead ());
189
+ assertEquals (list .getHead (), list .getTail ());
190
+ }
191
+ }
0 commit comments