-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathtask_test.go
More file actions
163 lines (147 loc) · 3.27 KB
/
Copy pathtask_test.go
File metadata and controls
163 lines (147 loc) · 3.27 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
package main
import (
"context"
"reflect"
"sort"
"sync"
"testing"
"time"
)
func TestQueue(t *testing.T) {
tests := []struct {
name string
size int
ops []string
args []int
expected []any
}{
{
name: "basic operations",
size: 3,
ops: []string{"push", "push", "push", "pop", "pop", "pop"},
args: []int{1, 2, 3, 0, 0, 0},
expected: []any{nil, nil, nil, 1, 2, 3},
},
{
name: "empty queue pop",
size: 1,
ops: []string{"pop", "push", "pop"},
args: []int{0, 5, 0},
expected: []any{-1, nil, 5},
},
{
name: "queue full error",
size: 2,
ops: []string{"push", "push", "push", "pop", "push", "pop"},
args: []int{5, 10, 15, 0, 20, 0},
expected: []any{nil, nil, ErrQueueFull, 5, nil, 10},
},
{
name: "interleaved operations",
size: 2,
ops: []string{"push", "pop", "push", "push", "pop", "pop"},
args: []int{1, 0, 2, 3, 0, 0},
expected: []any{nil, 1, nil, nil, 2, 3},
},
{
name: "peek operations",
size: 3,
ops: []string{"push", "peek", "push", "peek", "pop", "peek", "pop", "peek"},
args: []int{10, 0, 20, 0, 0, 0, 0, 0},
expected: []any{nil, 10, nil, 10, 10, 20, 20, -1},
},
{
name: "peek empty queue",
size: 2,
ops: []string{"peek", "push", "peek", "pop", "peek"},
args: []int{0, 42, 0, 0, 0},
expected: []any{-1, nil, 42, 42, -1},
},
{
name: "mixed operations",
size: 5,
ops: []string{"push", "push", "peek", "pop", "peek", "push", "push", "push", "peek"},
args: []int{5, 10, 0, 0, 0, 15, 20, 25, 0},
expected: []any{nil, nil, 5, 5, 10, nil, nil, nil, 10},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
q := NewQueue(tt.size)
results := make([]any, len(tt.ops))
for i, op := range tt.ops {
var result any
switch op {
case "push":
result = q.Push(tt.args[i])
case "pop":
result = q.Pop()
case "peek":
result = q.Peek()
}
results[i] = result
}
if !reflect.DeepEqual(results, tt.expected) {
t.Errorf("Expected: %v, got: %v", tt.expected, results)
}
})
}
}
func TestConcurrentQueue(t *testing.T) {
queue := NewQueue(1000)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
var wg sync.WaitGroup
wg.Add(1000)
for i := range 1000 {
go func() {
defer wg.Done()
queue.Push(i + 1)
}()
}
ch := make(chan int, 1000)
go func() {
defer close(ch)
for {
select {
case <-ctx.Done():
return
default:
if val := queue.Pop(); val > -1 {
ch <- val
}
}
}
}()
go func() {
wg.Wait()
time.Sleep(100 * time.Millisecond)
cancel()
}()
var result []int
for val := range ch {
result = append(result, val)
}
expected := make([]int, 1000)
for i := range 1000 {
expected[i] = i + 1
}
sort.Ints(result)
if !reflect.DeepEqual(result, expected) {
t.Errorf("Expected: 1..1000, got: %v", result)
}
}
func TestQueueAllocations(t *testing.T) {
res := testing.Benchmark(BenchmarkQueue)
if res.AllocsPerOp() != 0 {
t.Errorf("Expected 0 allocation, got: %d", res.AllocsPerOp())
}
}
func BenchmarkQueue(b *testing.B) {
queue := NewQueue(1)
for b.Loop() {
queue.Push(1)
queue.Peek()
queue.Pop()
}
}