-
Notifications
You must be signed in to change notification settings - Fork 122
/
Copy pathlist_test.go
79 lines (61 loc) · 1.33 KB
/
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
package ccache
import (
"testing"
"github.com/karlseguin/ccache/v3/assert"
)
func Test_List_Insert(t *testing.T) {
l := NewList[int]()
assertList(t, l)
l.Insert(newItem("a", 1, 0, false))
assertList(t, l, 1)
l.Insert(newItem("b", 2, 0, false))
assertList(t, l, 2, 1)
l.Insert(newItem("c", 3, 0, false))
assertList(t, l, 3, 2, 1)
}
func Test_List_Remove(t *testing.T) {
l := NewList[int]()
assertList(t, l)
item := newItem("a", 1, 0, false)
l.Insert(item)
l.Remove(item)
assertList(t, l)
n5 := newItem("e", 5, 0, false)
l.Insert(n5)
n4 := newItem("d", 4, 0, false)
l.Insert(n4)
n3 := newItem("c", 3, 0, false)
l.Insert(n3)
n2 := newItem("b", 2, 0, false)
l.Insert(n2)
n1 := newItem("a", 1, 0, false)
l.Insert(n1)
l.Remove(n5)
assertList(t, l, 1, 2, 3, 4)
l.Remove(n1)
assertList(t, l, 2, 3, 4)
l.Remove(n3)
assertList(t, l, 2, 4)
l.Remove(n2)
assertList(t, l, 4)
l.Remove(n4)
assertList(t, l)
}
func assertList(t *testing.T, list *List[int], expected ...int) {
t.Helper()
if len(expected) == 0 {
assert.Nil(t, list.Head)
assert.Nil(t, list.Tail)
return
}
node := list.Head
for _, expected := range expected {
assert.Equal(t, node.value, expected)
node = node.next
}
node = list.Tail
for i := len(expected) - 1; i >= 0; i-- {
assert.Equal(t, node.value, expected[i])
node = node.prev
}
}