-
Notifications
You must be signed in to change notification settings - Fork 76
/
queue_test.go
179 lines (148 loc) · 3.27 KB
/
queue_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
package queue
import "testing"
func TestQueueSimple(t *testing.T) {
q := New()
for i := 0; i < minQueueLen; i++ {
q.Add(i)
}
for i := 0; i < minQueueLen; i++ {
if q.Peek().(int) != i {
t.Error("peek", i, "had value", q.Peek())
}
x := q.Remove()
if x != i {
t.Error("remove", i, "had value", x)
}
}
}
func TestQueueWrapping(t *testing.T) {
q := New()
for i := 0; i < minQueueLen; i++ {
q.Add(i)
}
for i := 0; i < 3; i++ {
q.Remove()
q.Add(minQueueLen + i)
}
for i := 0; i < minQueueLen; i++ {
if q.Peek().(int) != i+3 {
t.Error("peek", i, "had value", q.Peek())
}
q.Remove()
}
}
func TestQueueLength(t *testing.T) {
q := New()
if q.Length() != 0 {
t.Error("empty queue length not 0")
}
for i := 0; i < 1000; i++ {
q.Add(i)
if q.Length() != i+1 {
t.Error("adding: queue with", i, "elements has length", q.Length())
}
}
for i := 0; i < 1000; i++ {
q.Remove()
if q.Length() != 1000-i-1 {
t.Error("removing: queue with", 1000-i-i, "elements has length", q.Length())
}
}
}
func TestQueueGet(t *testing.T) {
q := New()
for i := 0; i < 1000; i++ {
q.Add(i)
for j := 0; j < q.Length(); j++ {
if q.Get(j).(int) != j {
t.Errorf("index %d doesn't contain %d", j, j)
}
}
}
}
func TestQueueGetNegative(t *testing.T) {
q := New()
for i := 0; i < 1000; i++ {
q.Add(i)
for j := 1; j <= q.Length(); j++ {
if q.Get(-j).(int) != q.Length()-j {
t.Errorf("index %d doesn't contain %d", -j, q.Length()-j)
}
}
}
}
func TestQueueGetOutOfRangePanics(t *testing.T) {
q := New()
q.Add(1)
q.Add(2)
q.Add(3)
assertPanics(t, "should panic when negative index", func() {
q.Get(-4)
})
assertPanics(t, "should panic when index greater than length", func() {
q.Get(4)
})
}
func TestQueuePeekOutOfRangePanics(t *testing.T) {
q := New()
assertPanics(t, "should panic when peeking empty queue", func() {
q.Peek()
})
q.Add(1)
q.Remove()
assertPanics(t, "should panic when peeking emptied queue", func() {
q.Peek()
})
}
func TestQueueRemoveOutOfRangePanics(t *testing.T) {
q := New()
assertPanics(t, "should panic when removing empty queue", func() {
q.Remove()
})
q.Add(1)
q.Remove()
assertPanics(t, "should panic when removing emptied queue", func() {
q.Remove()
})
}
func assertPanics(t *testing.T, name string, f func()) {
defer func() {
if r := recover(); r == nil {
t.Errorf("%s: didn't panic as expected", name)
}
}()
f()
}
// WARNING: Go's benchmark utility (go test -bench .) increases the number of
// iterations until the benchmarks take a reasonable amount of time to run; memory usage
// is *NOT* considered. On a fast CPU, these benchmarks can fill hundreds of GB of memory
// (and then hang when they start to swap). You can manually control the number of iterations
// with the `-benchtime` argument. Passing `-benchtime 1000000x` seems to be about right.
func BenchmarkQueueSerial(b *testing.B) {
q := New()
for i := 0; i < b.N; i++ {
q.Add(nil)
}
for i := 0; i < b.N; i++ {
q.Peek()
q.Remove()
}
}
func BenchmarkQueueGet(b *testing.B) {
q := New()
for i := 0; i < b.N; i++ {
q.Add(i)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
q.Get(i)
}
}
func BenchmarkQueueTickTock(b *testing.B) {
q := New()
for i := 0; i < b.N; i++ {
q.Add(nil)
q.Peek()
q.Remove()
}
}