-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqueue_test.go
169 lines (139 loc) · 3.56 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
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 r, _ := q.Peek(); r.(int) != i {
t.Error("peek", i, "had value", r)
}
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 r, _ := q.Peek(); r.(int) != i+3 {
t.Error("peek", i, "had value", r)
}
_, _ = 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 r, _ := q.Get(j); r.(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 r, _ := q.Get(-j); r.(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)
_, err := q.Get(-4)
assertError(t, "should get index out of range when negative index", err, ErrIndexOutOfRange)
_, err = q.Get(4)
assertError(t, "should panic when index greater than length", err, ErrIndexOutOfRange)
}
func TestQueuePeekOutOfRangePanics(t *testing.T) {
q := New()
_, err := q.Peek()
assertError(t, "should return empty queue error when peeking empty queue", err, ErrQueueEmpty)
q.Add(1)
_, _ = q.Remove()
_, err = q.Peek()
assertError(t, "should return empty queue error when peeking emptied queue", err, ErrQueueEmpty)
}
func TestQueueRemoveOutOfRangePanics(t *testing.T) {
q := New()
_, err := q.Remove()
assertError(t, "should return empty queue error when removing empty queue", err, ErrQueueEmpty)
q.Add(1)
_, _ = q.Remove()
_, err = q.Remove()
assertError(t, "should return empty queue error when removing emptied queue", err, ErrQueueEmpty)
}
func assertError(t *testing.T, name string, actualErr error, expectedErr error) {
if actualErr != expectedErr {
t.Errorf("%s: didn't get error as expected", name)
}
}
// 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()
}
}