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
|
// Copyright 2021 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"./a"
"fmt"
"strconv"
)
func TestList() {
l := a.New[string]()
a.CheckListPointers(l, []*(a.Element[string]){})
// Single element list
e := l.PushFront("a")
a.CheckListPointers(l, []*(a.Element[string]){e})
l.MoveToFront(e)
a.CheckListPointers(l, []*(a.Element[string]){e})
l.MoveToBack(e)
a.CheckListPointers(l, []*(a.Element[string]){e})
l.Remove(e)
a.CheckListPointers(l, []*(a.Element[string]){})
// Bigger list
l2 := a.New[int]()
e2 := l2.PushFront(2)
e1 := l2.PushFront(1)
e3 := l2.PushBack(3)
e4 := l2.PushBack(600)
a.CheckListPointers(l2, []*(a.Element[int]){e1, e2, e3, e4})
l2.Remove(e2)
a.CheckListPointers(l2, []*(a.Element[int]){e1, e3, e4})
l2.MoveToFront(e3) // move from middle
a.CheckListPointers(l2, []*(a.Element[int]){e3, e1, e4})
l2.MoveToFront(e1)
l2.MoveToBack(e3) // move from middle
a.CheckListPointers(l2, []*(a.Element[int]){e1, e4, e3})
l2.MoveToFront(e3) // move from back
a.CheckListPointers(l2, []*(a.Element[int]){e3, e1, e4})
l2.MoveToFront(e3) // should be no-op
a.CheckListPointers(l2, []*(a.Element[int]){e3, e1, e4})
l2.MoveToBack(e3) // move from front
a.CheckListPointers(l2, []*(a.Element[int]){e1, e4, e3})
l2.MoveToBack(e3) // should be no-op
a.CheckListPointers(l2, []*(a.Element[int]){e1, e4, e3})
e2 = l2.InsertBefore(2, e1) // insert before front
a.CheckListPointers(l2, []*(a.Element[int]){e2, e1, e4, e3})
l2.Remove(e2)
e2 = l2.InsertBefore(2, e4) // insert before middle
a.CheckListPointers(l2, []*(a.Element[int]){e1, e2, e4, e3})
l2.Remove(e2)
e2 = l2.InsertBefore(2, e3) // insert before back
a.CheckListPointers(l2, []*(a.Element[int]){e1, e4, e2, e3})
l2.Remove(e2)
e2 = l2.InsertAfter(2, e1) // insert after front
a.CheckListPointers(l2, []*(a.Element[int]){e1, e2, e4, e3})
l2.Remove(e2)
e2 = l2.InsertAfter(2, e4) // insert after middle
a.CheckListPointers(l2, []*(a.Element[int]){e1, e4, e2, e3})
l2.Remove(e2)
e2 = l2.InsertAfter(2, e3) // insert after back
a.CheckListPointers(l2, []*(a.Element[int]){e1, e4, e3, e2})
l2.Remove(e2)
// Check standard iteration.
sum := 0
for e := l2.Front(); e != nil; e = e.Next() {
sum += e.Value
}
if sum != 604 {
panic(fmt.Sprintf("sum over l = %d, want 604", sum))
}
// Clear all elements by iterating
var next *a.Element[int]
for e := l2.Front(); e != nil; e = next {
next = e.Next()
l2.Remove(e)
}
a.CheckListPointers(l2, []*(a.Element[int]){})
}
func checkList[T comparable](l *a.List[T], es []interface{}) {
if !a.CheckListLen(l, len(es)) {
return
}
i := 0
for e := l.Front(); e != nil; e = e.Next() {
le := e.Value
// Comparison between a generically-typed variable le and an interface.
if le != es[i] {
panic(fmt.Sprintf("elt[%d].Value = %v, want %v", i, le, es[i]))
}
i++
}
}
func TestExtending() {
l1 := a.New[int]()
l2 := a.New[int]()
l1.PushBack(1)
l1.PushBack(2)
l1.PushBack(3)
l2.PushBack(4)
l2.PushBack(5)
l3 := a.New[int]()
l3.PushBackList(l1)
checkList(l3, []interface{}{1, 2, 3})
l3.PushBackList(l2)
checkList(l3, []interface{}{1, 2, 3, 4, 5})
l3 = a.New[int]()
l3.PushFrontList(l2)
checkList(l3, []interface{}{4, 5})
l3.PushFrontList(l1)
checkList(l3, []interface{}{1, 2, 3, 4, 5})
checkList(l1, []interface{}{1, 2, 3})
checkList(l2, []interface{}{4, 5})
l3 = a.New[int]()
l3.PushBackList(l1)
checkList(l3, []interface{}{1, 2, 3})
l3.PushBackList(l3)
checkList(l3, []interface{}{1, 2, 3, 1, 2, 3})
l3 = a.New[int]()
l3.PushFrontList(l1)
checkList(l3, []interface{}{1, 2, 3})
l3.PushFrontList(l3)
checkList(l3, []interface{}{1, 2, 3, 1, 2, 3})
l3 = a.New[int]()
l1.PushBackList(l3)
checkList(l1, []interface{}{1, 2, 3})
l1.PushFrontList(l3)
checkList(l1, []interface{}{1, 2, 3})
}
func TestRemove() {
l := a.New[int]()
e1 := l.PushBack(1)
e2 := l.PushBack(2)
a.CheckListPointers(l, []*(a.Element[int]){e1, e2})
e := l.Front()
l.Remove(e)
a.CheckListPointers(l, []*(a.Element[int]){e2})
l.Remove(e)
a.CheckListPointers(l, []*(a.Element[int]){e2})
}
func TestIssue4103() {
l1 := a.New[int]()
l1.PushBack(1)
l1.PushBack(2)
l2 := a.New[int]()
l2.PushBack(3)
l2.PushBack(4)
e := l1.Front()
l2.Remove(e) // l2 should not change because e is not an element of l2
if n := l2.Len(); n != 2 {
panic(fmt.Sprintf("l2.Len() = %d, want 2", n))
}
l1.InsertBefore(8, e)
if n := l1.Len(); n != 3 {
panic(fmt.Sprintf("l1.Len() = %d, want 3", n))
}
}
func TestIssue6349() {
l := a.New[int]()
l.PushBack(1)
l.PushBack(2)
e := l.Front()
l.Remove(e)
if e.Value != 1 {
panic(fmt.Sprintf("e.value = %d, want 1", e.Value))
}
if e.Next() != nil {
panic(fmt.Sprintf("e.Next() != nil"))
}
if e.Prev() != nil {
panic(fmt.Sprintf("e.Prev() != nil"))
}
}
func TestMove() {
l := a.New[int]()
e1 := l.PushBack(1)
e2 := l.PushBack(2)
e3 := l.PushBack(3)
e4 := l.PushBack(4)
l.MoveAfter(e3, e3)
a.CheckListPointers(l, []*(a.Element[int]){e1, e2, e3, e4})
l.MoveBefore(e2, e2)
a.CheckListPointers(l, []*(a.Element[int]){e1, e2, e3, e4})
l.MoveAfter(e3, e2)
a.CheckListPointers(l, []*(a.Element[int]){e1, e2, e3, e4})
l.MoveBefore(e2, e3)
a.CheckListPointers(l, []*(a.Element[int]){e1, e2, e3, e4})
l.MoveBefore(e2, e4)
a.CheckListPointers(l, []*(a.Element[int]){e1, e3, e2, e4})
e2, e3 = e3, e2
l.MoveBefore(e4, e1)
a.CheckListPointers(l, []*(a.Element[int]){e4, e1, e2, e3})
e1, e2, e3, e4 = e4, e1, e2, e3
l.MoveAfter(e4, e1)
a.CheckListPointers(l, []*(a.Element[int]){e1, e4, e2, e3})
e2, e3, e4 = e4, e2, e3
l.MoveAfter(e2, e3)
a.CheckListPointers(l, []*(a.Element[int]){e1, e3, e2, e4})
e2, e3 = e3, e2
}
// Test PushFront, PushBack, PushFrontList, PushBackList with uninitialized a.List
func TestZeroList() {
var l1 = new(a.List[int])
l1.PushFront(1)
checkList(l1, []interface{}{1})
var l2 = new(a.List[int])
l2.PushBack(1)
checkList(l2, []interface{}{1})
var l3 = new(a.List[int])
l3.PushFrontList(l1)
checkList(l3, []interface{}{1})
var l4 = new(a.List[int])
l4.PushBackList(l2)
checkList(l4, []interface{}{1})
}
// Test that a list l is not modified when calling InsertBefore with a mark that is not an element of l.
func TestInsertBeforeUnknownMark() {
var l a.List[int]
l.PushBack(1)
l.PushBack(2)
l.PushBack(3)
l.InsertBefore(1, new(a.Element[int]))
checkList(&l, []interface{}{1, 2, 3})
}
// Test that a list l is not modified when calling InsertAfter with a mark that is not an element of l.
func TestInsertAfterUnknownMark() {
var l a.List[int]
l.PushBack(1)
l.PushBack(2)
l.PushBack(3)
l.InsertAfter(1, new(a.Element[int]))
checkList(&l, []interface{}{1, 2, 3})
}
// Test that a list l is not modified when calling MoveAfter or MoveBefore with a mark that is not an element of l.
func TestMoveUnknownMark() {
var l1 a.List[int]
e1 := l1.PushBack(1)
var l2 a.List[int]
e2 := l2.PushBack(2)
l1.MoveAfter(e1, e2)
checkList(&l1, []interface{}{1})
checkList(&l2, []interface{}{2})
l1.MoveBefore(e1, e2)
checkList(&l1, []interface{}{1})
checkList(&l2, []interface{}{2})
}
// Test the Transform function.
func TestTransform() {
l1 := a.New[int]()
l1.PushBack(1)
l1.PushBack(2)
l2 := a.Transform(l1, strconv.Itoa)
checkList(l2, []interface{}{"1", "2"})
}
func main() {
TestList()
TestExtending()
TestRemove()
TestIssue4103()
TestIssue6349()
TestMove()
TestZeroList()
TestInsertBeforeUnknownMark()
TestInsertAfterUnknownMark()
TestTransform()
}
|