-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlinked_list_test.go
252 lines (229 loc) · 6.57 KB
/
linked_list_test.go
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
package list
import "testing"
// TestPushBack checks if the PushBack method correctly adds a node at the end of the list
func TestPushBack(t *testing.T) {
list := Linked[int]{}
list.PushBack(1)
if list.Size() != 1 {
t.Errorf("Expected list size to be 1, got %d", list.Size())
}
if val, ok := list.PopBack(); val != 1 || !ok {
t.Errorf("Expected (1, true), got (%d, %v)", val, ok)
}
}
// TestPushFront checks if the PushFront method correctly adds a node at the beginning of the list
func TestPushFront(t *testing.T) {
list := Linked[int]{}
list.PushFront(1)
if list.Size() != 1 {
t.Errorf("Expected list size to be 1, got %d", list.Size())
}
if val, ok := list.PopFront(); val != 1 || !ok {
t.Errorf("Expected (1, true), got (%d, %v)", val, ok)
}
}
// TestPopBack checks if the PopBack method correctly removes a node from the end of the list
func TestPopBack(t *testing.T) {
list := Linked[int]{}
list.PushBack(1)
val, ok := list.PopBack()
if !ok {
t.Errorf("Expected true, got %v", ok)
}
if val != 1 {
t.Errorf("Expected 1, got %d", val)
}
if list.Size() != 0 {
t.Errorf("Expected list size to be 0, got %d", list.Size())
}
}
// TestPopFront checks if the PopFront method correctly removes a node from the beginning of the list
func TestPopFront(t *testing.T) {
list := Linked[int]{}
list.PushFront(1)
val, ok := list.PopFront()
if !ok {
t.Errorf("Expected true, got %v", ok)
}
if val != 1 {
t.Errorf("Expected 1, got %d", val)
}
if list.Size() != 0 {
t.Errorf("Expected list size to be 0, got %d", list.Size())
}
}
// TestSize checks if the Size method returns the correct number of nodes in the list
func TestSize(t *testing.T) {
list := Linked[int]{}
list.PushBack(1)
list.PushFront(2)
if list.Size() != 2 {
t.Errorf("Expected list size to be 2, got %d", list.Size())
}
}
// TestEmpty checks if the Empty method correctly checks whether the list is empty
func TestEmpty(t *testing.T) {
list := Linked[int]{}
if !list.Empty() {
t.Errorf("Expected list to be empty, but it's not")
}
list.PushBack(1)
if list.Empty() {
t.Errorf("Expected list not to be empty, but it is")
}
}
// TestBegin checks if the Begin method correctly iterates over the list from the head to the tail
func TestBegin(t *testing.T) {
list := Linked[int]{}
list.PushBack(1)
list.PushBack(2)
list.Begin(func(val int) bool {
t.Logf("Visited node with value %d", val)
return true
})
}
// TestEnd checks if the End method correctly iterates over the list from the tail to the head
func TestEnd(t *testing.T) {
list := Linked[int]{}
list.PushBack(1)
list.PushBack(2)
list.End(func(val int) bool {
t.Logf("Visited node with value %d", val)
return true
})
}
// TestVal checks if the Val method correctly returns the value of the node
func TestVal(t *testing.T) {
node := Node[int]{val: 1}
if node.Val() != 1 {
t.Errorf("Expected node value to be 1, got %d", node.Val())
}
}
// TestNext checks if the Next method correctly returns the next node
func TestNext(t *testing.T) {
nextNode := &Node[int]{val: 2}
node := Node[int]{val: 1, rg: nextNode}
if node.Next() != nextNode {
t.Errorf("Expected next node to be %+v, got %+v", nextNode, node.Next())
}
}
// TestPrev checks if the Prev method correctly returns the previous node
func TestPrev(t *testing.T) {
prevNode := &Node[int]{val: 1}
node := Node[int]{val: 2, lf: prevNode}
if node.Prev() != prevNode {
t.Errorf("Expected previous node to be %+v, got %+v", prevNode, node.Prev())
}
}
// TestPushBackMultiple checks if multiple nodes can be added to the back of the list
func TestPushBackMultiple(t *testing.T) {
list := Linked[int]{}
for i := 1; i <= 5; i++ {
list.PushBack(i)
}
if list.Size() != 5 {
t.Errorf("Expected list size to be 5, got %d", list.Size())
}
}
// TestPushFrontMultiple checks if multiple nodes can be added to the front of the list
func TestPushFrontMultiple(t *testing.T) {
list := Linked[int]{}
for i := 1; i <= 5; i++ {
list.PushFront(i)
}
if list.Size() != 5 {
t.Errorf("Expected list size to be 5, got %d", list.Size())
}
}
// TestPopBackEmpty checks if PopBack returns false when the list is empty
func TestPopBackEmpty(t *testing.T) {
list := Linked[int]{}
val, ok := list.PopBack()
if ok {
t.Errorf("Expected false, got %v", ok)
}
if val != 0 {
t.Errorf("Expected zero value, got %d", val)
}
}
// TestPopFrontEmpty checks if PopFront returns false when the list is empty
func TestPopFrontEmpty(t *testing.T) {
list := Linked[int]{}
val, ok := list.PopFront()
if ok {
t.Errorf("Expected false, got %v", ok)
}
if val != 0 {
t.Errorf("Expected zero value, got %d", val)
}
}
// TestBeginEarlyExit checks if the Begin method can exit early
func TestBeginEarlyExit(t *testing.T) {
list := Linked[int]{}
list.PushBack(1)
list.PushBack(2)
list.PushBack(3)
count := 0
list.Begin(func(val int) bool {
count++
return count < 2 // Exit after visiting the first two nodes
})
if count != 2 {
t.Errorf("Expected to visit 2 nodes, visited %d", count)
}
}
// TestEndEarlyExit checks if the End method can exit early
func TestEndEarlyExit(t *testing.T) {
list := Linked[int]{}
list.PushBack(1)
list.PushBack(2)
list.PushBack(3)
count := 0
list.End(func(val int) bool {
count++
return count < 2 // Exit after visiting the first two nodes
})
if count != 2 {
t.Errorf("Expected to visit 2 nodes, visited %d", count)
}
}
// TestMixedOperations checks if the list behaves correctly with mixed operations
func TestMixedOperations(t *testing.T) {
list := Linked[int]{}
list.PushBack(1)
list.PushFront(2)
list.PushBack(3)
if val, ok := list.PopFront(); val != 2 || !ok {
t.Errorf("Expected (2, true), got (%d, %v)", val, ok)
}
if val, ok := list.PopBack(); val != 3 || !ok {
t.Errorf("Expected (3, true), got (%d, %v)", val, ok)
}
if val, ok := list.PopBack(); val != 1 || !ok {
t.Errorf("Expected (1, true), got (%d, %v)", val, ok)
}
if !list.Empty() {
t.Errorf("Expected list to be empty, but it's not")
}
}
// TestValNil checks if Val method returns the correct value for a nil node
func TestValNil(t *testing.T) {
var node *Node[int]
if node != nil {
t.Errorf("Expected node to be nil, but it is not")
}
}
// TestNextNil checks if Next method returns nil for the last node
func TestNextNil(t *testing.T) {
lastNode := &Node[int]{val: 1}
if lastNode.Next() != nil {
t.Errorf("Expected next node to be nil, got %+v", lastNode.Next())
}
}
// TestPrevNil checks if Prev method returns nil for the first node
func TestPrevNil(t *testing.T) {
firstNode := &Node[int]{val: 1}
if firstNode.Prev() != nil {
t.Errorf("Expected previous node to be nil, got %+v", firstNode.Prev())
}
}