-
Notifications
You must be signed in to change notification settings - Fork 0
/
linklist_test.go
286 lines (258 loc) · 5.75 KB
/
linklist_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
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
package datastruct
import (
"testing"
)
func TestLinkedListIndexOf(t *testing.T) {
var node1 = &ListNode{
Next: nil,
Value: 1,
}
var node2 = &ListNode{
Next: node1,
Value: 2,
}
var node3 = &ListNode{
Next: node2,
Value: 2,
}
ll := NewLinkedList()
ll.Insert(node1)
ll.Insert(node2)
ll.Insert(node3)
if r, err := ll.IndexOf(node1); err != nil || r != 2 {
t.Errorf("LinkedList: IndexOf error: %s, r should be 2 but got %d", err, r)
}
if r, err := ll.IndexOf(node2); err != nil || r != 1 {
t.Errorf("LinkedList: IndexOf error: %s, r should be 1 but got %d", err, r)
}
if r, err := ll.IndexOf(node3); err != nil || r != 0 {
t.Errorf("LinkedList: IndexOf error: %s, r should be 0 but got %d", err, r)
}
node4 := NewListNode(4)
if r, err := ll.IndexOf(node4); err == nil || r != -1 {
t.Errorf("LinkedList: IndexOf error: %s, r should be -1 but got %d", err, r)
}
}
func TestLinkedListInsert(t *testing.T) {
var node1 = &ListNode{
Next: nil,
Value: 1,
}
var node2 = &ListNode{
Next: node1,
Value: 2,
}
var node3 = &ListNode{
Next: node2,
Value: 3,
}
ll := NewLinkedList()
ll.Insert(node1)
ll.Insert(node2)
ll.Insert(node3)
}
func TestLinkedListInsertAt(t *testing.T) {
var node1 = &ListNode{
Next: nil,
Value: 1,
}
var node2 = &ListNode{
Next: node1,
Value: 2,
}
var node3 = &ListNode{
Next: node2,
Value: 3,
}
ll := NewLinkedList()
ll.Insert(node1)
ll.Insert(node2)
ll.Insert(node3)
var node4 = NewListNode(4)
err := ll.InsertAt(node4, 2)
if err != nil {
t.Errorf("LinkedList: InsertAt error: %s", err)
}
if r := ll.String(); r != "3->2->4->1" {
t.Errorf("LinkedList: String error: it should be 3->2->4->1 but get: %s", r)
}
if err := ll.InsertAt(NewListNode(5), 999); err == nil {
t.Errorf("LinkedList: InsertAt error: it should be error, but get: %s", err)
}
}
func TestLinkedListIndex(t *testing.T) {
var node1 = &ListNode{
Next: nil,
Value: 1,
}
var node2 = &ListNode{
Next: node1,
Value: 2,
}
var node3 = &ListNode{
Next: node2,
Value: 3,
}
ll := NewLinkedList()
ll.Insert(node1)
ll.Insert(node2)
ll.Insert(node3)
if r, err := ll.Index(1); r != node2 || err != nil {
t.Errorf("LinkedList: Index error: %s, it should be %v, but get %v", err, node2, r)
}
if r, err := ll.Index(999); r != nil || err == nil {
t.Errorf("LinkedList: Index error: %s, it should be %v, but get %v", err, nil, r)
}
}
//TestLinkedListPop
func TestLinkedListPop(t *testing.T) {
var node1 = &ListNode{
Next: nil,
Value: 1,
}
var node2 = &ListNode{
Next: node1,
Value: 2,
}
var node3 = &ListNode{
Next: node2,
Value: 3,
}
ll := NewLinkedList()
ll.Insert(node1)
ll.Insert(node2)
ll.Insert(node3)
tNode, err := ll.Pop()
if err != nil {
t.Errorf("LinkedList: Pop error: %s", err)
}
if tNode != node3 {
t.Errorf("LinkedList: Pop error: it should be node3 but get %v", tNode)
}
}
func TestLinkedListDeleteByIndex(t *testing.T) {
var node1 = &ListNode{
Next: nil,
Value: 1,
}
var node2 = &ListNode{
Next: node1,
Value: 2,
}
var node3 = &ListNode{
Next: node2,
Value: 3,
}
ll := NewLinkedList()
ll.Insert(node1)
ll.Insert(node2)
ll.Insert(node3)
if err := ll.DeleteByIndex(1); err != nil {
t.Errorf("LinkedList: DeleteByIndex error: %s", err)
}
if r := ll.String(); r != "3->1" {
t.Errorf("LinkedList: String error: it should be 3->1 but get: %s", r)
}
if err := ll.DeleteByIndex(999); err == nil {
t.Errorf("LinkedList: DeleteByIndex error: it should not be nil")
}
if err := ll.DeleteByIndex(0); err != nil {
t.Errorf("LinkedList: DeleteByIndex error: %s", err)
}
}
func TestLinkedListDeleteByNode(t *testing.T) {
var node1 = &ListNode{
Next: nil,
Value: 1,
}
var node2 = &ListNode{
Next: node1,
Value: 2,
}
var node3 = &ListNode{
Next: node2,
Value: 3,
}
ll := NewLinkedList()
ll.Insert(node1)
ll.Insert(node2)
ll.Insert(node3)
ll.DeleteByNode(node1)
if r := ll.String(); r != "3->2" {
t.Errorf("LinkedList: String error: it should be 3->2 but get: %s", r)
}
if err := ll.DeleteByNode(NewListNode(5)); err == nil {
t.Errorf("LinkedList: DeleteByNode error: it should not be nil")
}
}
func TestLinkedListGetFirst(t *testing.T) {
var node1 = &ListNode{
Next: nil,
Value: 1,
}
var node2 = &ListNode{
Next: node1,
Value: 2,
}
var node3 = &ListNode{
Next: node2,
Value: 3,
}
ll := NewLinkedList()
ll.Insert(node1)
ll.Insert(node2)
ll.Insert(node3)
if index, node := ll.GetFirst(1); index != 2 || node != node1 {
t.Errorf("LinkedList: GetFirst error: index and node should be %d and %v, but get %d and %v", 2, node1, index, node)
}
if index, node := ll.GetFirst(100); index != -1 || node != nil {
t.Errorf("LinkedList: GetFirst error: index and node should be %d and %v, but get %d and %v", -1, nil, index, node)
}
}
func TestLinkedListModify(t *testing.T) {
var node1 = &ListNode{
Next: nil,
Value: 1,
}
var node2 = &ListNode{
Next: node1,
Value: 2,
}
var node3 = &ListNode{
Next: node2,
Value: 3,
}
ll := NewLinkedList()
ll.Insert(node1)
ll.Insert(node2)
ll.Insert(node3)
ll.Modify(0, 100)
if r := ll.String(); r != "100->2->1" {
t.Errorf("LinkedList: Modify error: it should be 100->2->1 but get %s", r)
}
if err := ll.Modify(100, 0); err == nil {
t.Errorf("LinkedList: Modify error: it should not be nil")
}
}
func TestLinkedListClear(t *testing.T) {
var node1 = &ListNode{
Next: nil,
Value: 1,
}
var node2 = &ListNode{
Next: node1,
Value: 2,
}
var node3 = &ListNode{
Next: node2,
Value: 3,
}
ll := NewLinkedList()
ll.Insert(node1)
ll.Insert(node2)
ll.Insert(node3)
ll.Clear()
if ll.Head != nil || ll.GetLength() != 0 {
t.Errorf("LinkedList: Clear error: ll.Head should be nil but is %v, ll.length should be 0 but is %v", ll.Head, ll.GetLength())
}
}