42 | 42 |
q.Add(2)
|
43 | 43 |
q.Add(3)
|
44 | 44 |
|
45 | |
func() {
|
46 | |
defer func() {
|
47 | |
if r := recover(); r == nil {
|
48 | |
t.Errorf("should panic when negative index")
|
49 | |
} else {
|
50 | |
t.Logf("got panic as expected: %v", r)
|
51 | |
}
|
52 | |
}()
|
|
45 |
assertPanics(t, "should panic when negative index", func() {
|
|
46 |
q.Get(-1)
|
|
47 |
})
|
53 | 48 |
|
54 | |
func() {
|
55 | |
q.Get(-1)
|
56 | |
}()
|
|
49 |
assertPanics(t, "should panic when index greater than length", func() {
|
|
50 |
q.Get(4)
|
|
51 |
})
|
|
52 |
}
|
|
53 |
|
|
54 |
func TestQueuePeekOutOfRangePanics(t *testing.T) {
|
|
55 |
q := New()
|
|
56 |
|
|
57 |
assertPanics(t, "should panic when peeking empty queue", func() {
|
|
58 |
q.Peek()
|
|
59 |
})
|
|
60 |
|
|
61 |
q.Add(1)
|
|
62 |
q.Remove()
|
|
63 |
|
|
64 |
assertPanics(t, "should panic when peeking emptied queue", func() {
|
|
65 |
q.Peek()
|
|
66 |
})
|
|
67 |
}
|
|
68 |
|
|
69 |
func TestQueueRemoveOutOfRangePanics(t *testing.T) {
|
|
70 |
q := New()
|
|
71 |
|
|
72 |
assertPanics(t, "should panic when removing empty queue", func() {
|
|
73 |
q.Remove()
|
|
74 |
})
|
|
75 |
|
|
76 |
q.Add(1)
|
|
77 |
q.Remove()
|
|
78 |
|
|
79 |
assertPanics(t, "should panic when removing emptied queue", func() {
|
|
80 |
q.Remove()
|
|
81 |
})
|
|
82 |
}
|
|
83 |
|
|
84 |
func assertPanics(t *testing.T, name string, f func()) {
|
|
85 |
defer func() {
|
|
86 |
if r := recover(); r == nil {
|
|
87 |
t.Errorf("%s: didn't panic as expected", name)
|
|
88 |
} else {
|
|
89 |
t.Logf("%s: got panic as expected: %v", name, r)
|
|
90 |
}
|
57 | 91 |
}()
|
58 | 92 |
|
59 | |
func() {
|
60 | |
defer func() {
|
61 | |
if r := recover(); r == nil {
|
62 | |
t.Errorf("should panic when index greater than length")
|
63 | |
} else {
|
64 | |
t.Logf("got panic as expected: %v", r)
|
65 | |
}
|
66 | |
}()
|
67 | |
|
68 | |
func() {
|
69 | |
q.Get(4)
|
70 | |
}()
|
71 | |
}()
|
|
93 |
f()
|
72 | 94 |
}
|
73 | 95 |
|
74 | 96 |
// General warning: Go's benchmark utility (go test -bench .) increases the number of
|
|
86 | 108 |
}
|
87 | 109 |
}
|
88 | 110 |
|
|
111 |
func BenchmarkQueueGet(b *testing.B) {
|
|
112 |
q := New()
|
|
113 |
for i := 0; i < b.N; i++ {
|
|
114 |
q.Add(i)
|
|
115 |
}
|
|
116 |
b.ResetTimer()
|
|
117 |
for i := 0; i < b.N; i++ {
|
|
118 |
q.Get(i)
|
|
119 |
}
|
|
120 |
}
|
|
121 |
|
89 | 122 |
func BenchmarkQueueTickTock(b *testing.B) {
|
90 | 123 |
q := New()
|
91 | 124 |
for i := 0; i < b.N; i++ {
|