-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathlist_test.go
195 lines (162 loc) · 4.41 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
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
package mlink_test
import (
"testing"
"github.com/creachadair/mds/internal/mdtest"
"github.com/creachadair/mds/mlink"
"github.com/creachadair/mds/mtest"
)
func eq(z int) func(int) bool {
return func(n int) bool { return n == z }
}
func TestList(t *testing.T) {
lst := mlink.NewList[int]()
checkList := func(want ...int) { t.Helper(); mdtest.CheckContents(t, lst, want) }
advance := func(c *mlink.Cursor[int], wants ...int) {
t.Helper()
for _, want := range wants {
c.Next()
if got := c.Get(); got != want {
t.Errorf("Get: got %v, want %v", got, want)
}
}
}
checkAt := func(c *mlink.Cursor[int], want int) {
t.Helper()
if got := c.Get(); got != want {
t.Errorf("Get: got %v, want %v", got, want)
}
}
// A new list is initially empty.
checkList()
if !lst.At(0).AtEnd() {
t.Error("At(0): should be at end for empty list")
}
if !lst.Last().AtEnd() {
t.Error("Last: should be at end for empty list")
}
// Add advances after insertion.
first := lst.At(0)
first.Add(1, 2)
checkList(1, 2)
// Push does not advance after insertion.
first.Push(3)
first.Push(4)
checkList(1, 2, 4, 3)
if got, want := first.Remove(), 4; got != want {
t.Errorf("Remove: got %v, want %v", got, want)
}
checkList(1, 2, 3)
// Check adding at the end.
lst.End().Add(4)
checkList(1, 2, 3, 4)
// At and Last should work.
checkAt(lst.At(0), 1)
checkAt(lst.At(1), 2)
checkAt(lst.At(2), 3)
checkAt(lst.At(3), 4)
checkAt(lst.Last(), 4)
// At past the end of the list should pin to the end.
if e := lst.At(1000); !e.AtEnd() {
t.Error("At(big) should go to the end of the list")
}
// Peek past the end should report failure and a zero.
if v, ok := lst.Peek(1000); ok || v != 0 {
t.Errorf("Peek(big): got (%v, %v), want (0, false)", v, ok)
}
// Exercise navigation with a cursor.
c := lst.At(0)
checkAt(c, 1)
advance(c, 2)
checkAt(c, 2)
if got, want := c.Remove(), 2; got != want {
t.Errorf("Remove: got %v, want %v", got, want)
}
checkList(1, 3, 4)
checkAt(c, 3)
// Add at the ends of the list.
lst.End().Add(5)
lst.At(0).Add(6)
checkList(6, 1, 3, 4, 5)
// The cursor should still be valid, and see the changes.
advance(c, 4)
// Add in the middle of the list.
c.Push(7)
checkList(6, 1, 3, 7, 4, 5)
// Exercise moving in a list.
c = lst.At(0)
checkAt(c, 6)
advance(c, 1, 3, 7)
cn := lst.Find(eq(4)) // grab a cursor after where we are about to truncate
checkAt(cn, 4)
c.Truncate()
checkList(6, 1, 3)
if !c.AtEnd() {
t.Error("Cursor should be at the end")
}
// Truncation invalidates a cursor after the cut point.
mtest.MustPanic(t, func() { cn.Get() })
// Push at the end does the needful.
lst.End().Push(9)
checkList(6, 1, 3, 9)
checkAt(c, 9) // c sees the new value
// Setting the last element doesn't add any mass.
lst.Last().Set(10)
checkList(6, 1, 3, 10)
checkAt(c, 10) // c sees the new value
// Setting elsewhere works too.
lst.At(0).Set(11)
checkList(11, 1, 3, 10)
// Setting at the end pushes a new item.
tail := lst.End()
tail.Set(12)
checkAt(tail, 12) // tail is no longer at the end
checkList(11, 1, 3, 10, 12)
checkAt(c, 10) // c hasn't moved
// Finding things.
checkAt(lst.Find(eq(11)), 11) // first
checkAt(lst.Find(eq(3)), 3) // middle
checkAt(lst.Find(eq(12)), 12) // last
if q := lst.Find(eq(-999)); !q.AtEnd() { // missing
t.Errorf("Find: got %v, wanted no result", q.Get())
}
// Remove an item, and verify that a cursor to the following item is
// correctly invalidated.
d := lst.Find(eq(12))
checkAt(d, 12)
if got, want := c.Remove(), 10; got != want {
t.Errorf("Remove: got %v, want %v", got, want)
}
checkList(11, 1, 3, 12)
// Removing invalidates a cursor to the next item.
mtest.MustPanic(t, func() { d.Get() })
// We can remove the first and last elements.
if got, want := lst.At(0).Remove(), 11; got != want {
t.Errorf("Remove: got %v, want %v", got, want)
}
checkList(1, 3, 12)
if got, want := lst.Last().Remove(), 12; got != want {
t.Errorf("Remove: got %v, want %v", got, want)
}
checkList(1, 3)
lst.Clear()
checkList()
}
func mustPanic(f func()) func(*testing.T) {
return func(t *testing.T) {
t.Helper()
mtest.MustPanic(t, f)
}
}
func TestPanics(t *testing.T) {
var lst mlink.List[bool]
t.Run("At(-1)", mustPanic(func() {
lst.At(-1)
}))
t.Run("Peek(-1)", mustPanic(func() {
lst.Peek(-1)
}))
t.Run("NilCursor", mustPanic(func() {
var nc *mlink.Cursor[bool]
nc.Get()
}))
}