-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueue_test.go
More file actions
363 lines (310 loc) · 7.46 KB
/
queue_test.go
File metadata and controls
363 lines (310 loc) · 7.46 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
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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
package bfq
import (
"fmt"
"testing"
)
func BenchmarkQueueCreation(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
q := NewQueue[int]()
// Push enough elements to trigger a resize and allocation
for j := 0; j < 1000; j++ {
q.PushBack(j)
}
}
}
func BenchmarkQueuePushBack(b *testing.B) {
b.ReportAllocs()
q := NewQueue[int]()
for i := 0; i < b.N; i++ {
q.PushBack(i)
}
}
func BenchmarkQueuePushFront(b *testing.B) {
b.ReportAllocs()
q := NewQueue[int]()
for i := 0; i < b.N; i++ {
q.PushFront(i)
}
}
func BenchmarkQueueFromSlice(b *testing.B) {
arr := make([]int, b.N)
for i:=0; i<b.N; i++ {
arr[i]=i
}
b.ResetTimer()
b.ReportAllocs()
_ = FromSlice(arr)
}
func BenchmarkQueuePopBack(b *testing.B) {
q := NewQueue[int]()
for i := 0; i < b.N; i++ {
q.PushBack(i)
}
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
q.PopBack()
}
}
func BenchmarkQueuePopFront(b *testing.B) {
q := NewQueue[int]()
for i := 0; i < b.N; i++ {
q.PushBack(i)
}
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
q.PopFront()
}
}
func BenchmarkQueueFront(b *testing.B) {
q := NewQueue[int]()
q.PushBack(42)
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
q.Front()
}
}
func BenchmarkQueueBack(b *testing.B) {
q := NewQueue[int]()
q.PushBack(42)
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
q.Back()
}
}
func BenchmarkQueueIsEmpty(b *testing.B) {
q := NewQueue[int]()
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
q.IsEmpty()
}
}
func BenchmarkQueueMixedOperations(b *testing.B) {
b.ReportAllocs()
q := NewQueue[int]()
for i := 0; i < b.N; i++ {
q.PushBack(i)
if i%2 == 0 {
q.PopFront()
}
}
}
// Different data types
func BenchmarkQueueWithInts(b *testing.B) {
b.ReportAllocs()
q := NewQueue[int]()
for i := 0; i < b.N; i++ {
q.PushBack(42)
q.PopFront()
}
}
type Data struct {
ID int
Name string
}
func BenchmarkQueueWithStruct(b *testing.B) {
b.ReportAllocs()
q := NewQueue[Data]()
for i := 0; i < b.N; i++ {
q.PushBack(Data{ID: i, Name: fmt.Sprintf("Item %d", i)})
q.PopFront()
}
}
func BenchmarkQueueWithStructPointers(b *testing.B) {
b.ReportAllocs()
q := NewQueue[*Data]()
for i := 0; i < b.N; i++ {
item := &Data{ID: i, Name: fmt.Sprintf("Item %d", i)}
q.PushBack(item)
q.PopFront()
}
}
func BenchmarkQueueWithSlices(b *testing.B) {
b.ReportAllocs()
q := NewQueue[[]int]()
for i := 0; i < b.N; i++ {
q.PushBack([]int{i, i + 1, i + 2})
q.PopFront()
}
}
func TestNewQueue(t *testing.T) {
q := NewQueue[int]()
if q.Len() != 0 {
t.Errorf("Expected queue length 0, got %d", q.Len())
}
if !q.IsEmpty() {
t.Errorf("Expected queue to be empty, but it's not")
}
}
func TestPushFrontAndPopFront(t *testing.T) {
q := NewQueue[int]()
q.PushFront(10)
q.PushFront(20)
// Ensure the front is correct
if front, ok := q.Front(); !ok || front != 20 {
t.Errorf("Expected front element 20, got %v", front)
}
// Pop the front and check the result
if val, ok := q.PopFront(); !ok || val != 20 {
t.Errorf("Expected popped value 20, got %v", val)
}
// Ensure the front is now the next element
if front, ok := q.Front(); !ok || front != 10 {
t.Errorf("Expected front element 10, got %v", front)
}
// Test popping last element
if val, ok := q.PopFront(); !ok || val != 10 {
t.Errorf("Expected popped value 10, got %v", val)
}
// Ensure the queue is empty after popping
if !q.IsEmpty() {
t.Errorf("Expected queue to be empty, but it's not")
}
}
func TestPushBackAndPopBack(t *testing.T) {
q := NewQueue[int]()
q.PushBack(10)
q.PushBack(20)
// Ensure the back is correct
if back, ok := q.Back(); !ok || back != 20 {
t.Errorf("Expected back element 20, got %v", back)
}
// Pop the back and check the result
if val, ok := q.PopBack(); !ok || val != 20 {
t.Errorf("Expected popped value 20, got %v", val)
}
// Ensure the back is now the next element
if back, ok := q.Back(); !ok || back != 10 {
t.Errorf("Expected back element 10, got %v", back)
}
// Test popping last element
if val, ok := q.PopBack(); !ok || val != 10 {
t.Errorf("Expected popped value 10, got %v", val)
}
// Ensure the queue is empty after popping
if !q.IsEmpty() {
t.Errorf("Expected queue to be empty, but it's not")
}
}
func TestFrontAndBackOnEmptyQueue(t *testing.T) {
q := NewQueue[int]()
// Test Front on empty queue
if _, ok := q.Front(); ok {
t.Errorf("Expected Front to return false on empty queue")
}
// Test Back on empty queue
if _, ok := q.Back(); ok {
t.Errorf("Expected Back to return false on empty queue")
}
}
func TestPushPopMix(t *testing.T) {
q := NewQueue[int]()
q.PushBack(10)
q.PushFront(20)
// Test mixed operations: PushFront, PushBack, PopFront, PopBack
if front, ok := q.Front(); !ok || front != 20 {
t.Errorf("Expected front element 20, got %v", front)
}
if back, ok := q.Back(); !ok || back != 10 {
t.Errorf("Expected back element 10, got %v", back)
}
// Pop front
if val, ok := q.PopFront(); !ok || val != 20 {
t.Errorf("Expected popped value 20, got %v", val)
}
// Pop back
if val, ok := q.PopBack(); !ok || val != 10 {
t.Errorf("Expected popped value 10, got %v", val)
}
// Ensure the queue is empty
if !q.IsEmpty() {
t.Errorf("Expected queue to be empty, but it's not")
}
}
func TestResize(t *testing.T) {
q := NewQueue[int]()
for i := 0; i < 100; i++ {
q.PushBack(i)
}
// Test the queue length after inserting 100 elements
if q.Len() != 100 {
t.Errorf("Expected queue length 100, got %d", q.Len())
}
// Pop all elements to check if everything is in order
for i := 0; i < 100; i++ {
if val, ok := q.PopFront(); !ok || val != i {
t.Errorf("Expected popped value %d, got %v", i, val)
}
}
// Ensure the queue is empty
if !q.IsEmpty() {
t.Errorf("Expected queue to be empty, but it's not")
}
}
func TestPushFrontOverflow(t *testing.T) {
q := NewQueue[int]()
for i := 0; i < 1000; i++ {
q.PushFront(i)
}
// Test the queue length after inserting 1000 elements
if q.Len() != 1000 {
t.Errorf("Expected queue length 1000, got %d", q.Len())
}
// Pop all elements to check if everything is in order (LIFO)
for i := 999; i >= 0; i-- {
if val, ok := q.PopFront(); !ok || val != i {
t.Errorf("Expected popped value %d, got %v", i, val)
}
}
// Ensure the queue is empty
if !q.IsEmpty() {
t.Errorf("Expected queue to be empty, but it's not")
}
}
func TestPopFromEmptyQueue(t *testing.T) {
q := NewQueue[int]()
if val, ok := q.PopFront(); ok {
t.Errorf("Expected PopFront to return false, got %v", val)
}
if val, ok := q.PopBack(); ok {
t.Errorf("Expected PopBack to return false, got %v", val)
}
}
func TestQueueWithNilPointers(t *testing.T) {
q := NewQueue[*Data]()
q.PushBack(nil)
// Test popping a nil value
if val, ok := q.PopFront(); !ok || val != nil {
t.Errorf("Expected nil value, got %v", val)
}
// Ensure the queue is empty after popping
if !q.IsEmpty() {
t.Errorf("Expected queue to be empty, but it's not")
}
}
func TestLargeQueueOperations(t *testing.T) {
q := NewQueue[int]()
n := 100000
for i := 0; i < n; i++ {
q.PushBack(i)
}
// Ensure the queue size is correct
if q.Len() != n {
t.Errorf("Expected queue size %d, got %d", n, q.Len())
}
// Pop all elements and check if it's in order
for i := 0; i < n; i++ {
if val, ok := q.PopFront(); !ok || val != i {
t.Errorf("Expected popped value %d, got %v", i, val)
}
}
// Ensure the queue is empty
if !q.IsEmpty() {
t.Errorf("Expected queue to be empty, but it's not")
}
}