forked from erikdubbelboer/ringqueue
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueues_test.go
More file actions
100 lines (81 loc) · 1.57 KB
/
Copy pathqueues_test.go
File metadata and controls
100 lines (81 loc) · 1.57 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
package main
import (
"testing"
)
type intqueue interface {
add(int)
remove() (int, bool)
len() int
cap() int
}
func testqueue(t *testing.T, q intqueue) {
for j := 0; j < 100; j++ {
if q.len() != 0 {
t.Fatal("expected no elements")
} else if _, ok := q.remove(); ok {
t.Fatal("expected no elements")
}
for i := 0; i < j; i++ {
q.add(i)
}
for i := 0; i < j; i++ {
if x, ok := q.remove(); !ok {
t.Fatal("expected an element")
} else if x != i {
t.Fatalf("expected %d got %d", i, x)
}
}
}
a := 0
r := 0
for j := 0; j < 100; j++ {
for i := 0; i < 4; i++ {
q.add(a)
a++
}
for i := 0; i < 2; i++ {
if x, ok := q.remove(); !ok {
t.Fatal("expected an element")
} else if x != r {
t.Fatalf("expected %d got %d", r, x)
}
r++
}
}
if q.len() != 200 {
t.Fatalf("expected 200 elements have %d", q.len())
}
}
func TestSlicequeue(t *testing.T) {
testqueue(t, newslicequeue())
}
func TestRingqueue(t *testing.T) {
testqueue(t, newringqueue())
}
func benchmarkAdd(b *testing.B, q intqueue) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
q.add(i)
}
}
func BenchmarkSliceAdd(b *testing.B) {
benchmarkAdd(b, newslicequeue())
}
func BenchmarkRingAdd(b *testing.B) {
benchmarkAdd(b, newringqueue())
}
func benchmarkRemove(b *testing.B, q intqueue) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
q.add(i)
if q.len() > 10 {
q.remove()
}
}
}
func BenchmarkSliceRemove(b *testing.B) {
benchmarkRemove(b, newslicequeue())
}
func BenchmarkRingRemove(b *testing.B) {
benchmarkRemove(b, newringqueue())
}