forked from zyedidia/generic
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueue_test.go
More file actions
215 lines (170 loc) · 3.37 KB
/
queue_test.go
File metadata and controls
215 lines (170 loc) · 3.37 KB
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
package queue
import (
"fmt"
"testing"
"github.com/zyedidia/generic/list"
)
func TestQueueEmpty(t *testing.T) {
cases := []struct {
name string
queue *Queue[int]
want bool
}{
{
name: "empty queue",
queue: emptyQueue(),
want: true,
},
{
name: "non-empty queue",
queue: nonEmptyQueue(),
want: false,
},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
got := c.queue.Empty()
if got != c.want {
t.Errorf("got %v, want %v", got, c.want)
}
})
}
}
func TestQueuePeek(t *testing.T) {
t.Run("panics on empty queue", func(t *testing.T) {
defer func() {
if err := recover(); err == nil {
t.Error("peeking on empty queue did not panic")
}
}()
emptyQueue().Peek()
})
t.Run("non-empty queue", func(t *testing.T) {
got := nonEmptyQueue().Peek()
want := 1
if got != want {
t.Errorf("got %v, want %v", got, want)
}
})
}
func TestQueueEnqueue(t *testing.T) {
t.Run("empty queue", func(t *testing.T) {
q := emptyQueue()
q.Enqueue(1)
if q.list.Front == nil {
t.Error("front is nil")
}
if q.list.Front.Value != 1 {
t.Errorf("got %v, want %v for front value", q.list.Front.Value, 1)
}
if q.list.Back == nil {
t.Error("back is nil")
}
if q.list.Front != q.list.Back {
t.Error(("front and back are not the same"))
}
})
t.Run("non-empty queue", func(t *testing.T) {
q := nonEmptyQueue()
q.Enqueue(3)
if q.list.Front.Value != 1 {
t.Errorf("got %v, want %v for front value", q.list.Front.Value, 1)
}
if q.list.Back == nil {
t.Error("back is nil")
}
if q.list.Back.Value != 3 {
t.Errorf("got %v, want %v for back value", q.list.Back.Value, 3)
}
})
}
func TestQueueDequeue(t *testing.T) {
t.Run("panics on empty queue", func(t *testing.T) {
defer func() {
if err := recover(); err == nil {
t.Error("dequeue on empty queue did not panic")
}
}()
emptyQueue().Dequeue()
})
t.Run("non-empty queue", func(t *testing.T) {
q := nonEmptyQueue()
// Non-empty after dequeue
got := q.Dequeue()
if got != 1 {
t.Errorf("got %v, want %v", got, 1)
}
if q.list.Front.Value == 1 {
t.Error("front of queue is still 1 after dequeue")
}
if q.Empty() {
t.Error("queue is empty")
}
// Empty after dequeue
got = q.Dequeue()
if got != 2 {
t.Errorf("got %v, want %v", got, 2)
}
if !q.Empty() {
t.Error("queue is not empty")
}
})
}
func TestQueueEach(t *testing.T) {
q := nonEmptyQueue()
i := 1
q.Each(func(item int) {
if item != i {
t.Errorf("got %v, want %v", item, i)
}
i++
})
}
func Example_Enqueue() {
q := New[int]()
q.Enqueue(1)
}
func Example_Peek() {
q := New[int]()
q.Enqueue(1)
fmt.Println(q.Peek())
// Output: 1
}
func Example_Dequeue() {
q := New[int]()
q.Enqueue(1)
fmt.Println(q.Dequeue())
// Output: 1
}
func Example() {
q := New[int]()
q.Enqueue(1)
q.Enqueue(2)
q.Each(func(i int) {
fmt.Println(i)
})
// Output:
// 1
// 2
}
func Example_Empty_empty() {
q := New[int]()
fmt.Println(q.Empty())
// Output: true
}
func Example_Empty_nonempty() {
q := New[int]()
q.Enqueue(1)
fmt.Println(q.Empty())
// Output: false
}
func emptyQueue() *Queue[int] {
return New[int]()
}
func nonEmptyQueue() *Queue[int] {
q := New[int]()
q.list.Front = &list.Node[int]{Value: 1}
q.list.Front.Next = &list.Node[int]{Value: 2, Prev: q.list.Front}
q.list.Back = q.list.Front.Next
return q
}