-
Notifications
You must be signed in to change notification settings - Fork 292
/
Copy pathqueue_test.go
65 lines (52 loc) · 1.08 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
package queue
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestQueue(t *testing.T) {
size := 100
q := NewCircularBuffer(size)
for i := 0; i < size; i++ {
assert.NoError(t, q.Enqueue(i))
}
// can't insert new data.
assert.Error(t, q.Enqueue(0))
assert.Equal(t, errFull, q.Enqueue(0))
for i := 0; i < size; i++ {
v, err := q.Dequeue()
assert.Equal(t, i, v.(int))
assert.NoError(t, err)
}
// no task
_, err := q.Dequeue()
assert.Error(t, err)
assert.Equal(t, errNoTask, err)
}
func BenchmarkCircularBufferEnqueueDequeue(b *testing.B) {
q := NewCircularBuffer(b.N)
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = q.Enqueue(i)
_, _ = q.Dequeue()
}
}
func BenchmarkCircularBufferEnqueue(b *testing.B) {
q := NewCircularBuffer(b.N)
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = q.Enqueue(i)
}
}
func BenchmarkCircularBufferDequeue(b *testing.B) {
q := NewCircularBuffer(b.N)
for i := 0; i < b.N; i++ {
_ = q.Enqueue(i)
}
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, _ = q.Dequeue()
}
}