Skip to content

Commit a15448c

Browse files
author
Theo
committed
ArrayBlockingQueue: More tests
1 parent 273509a commit a15448c

File tree

5 files changed

+62
-18
lines changed

5 files changed

+62
-18
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ test:
1919

2020
.PHONY: bench
2121
bench:
22-
GOPATH=$(GOPATH) go test -bench=. -check.b
22+
GOPATH=$(GOPATH) go test -bench=. -check.b -benchmem
2323

2424
.PHONY: docs
2525
docs:

README.md

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,12 @@ Using:
3333

3434
Simple operations - no goroutines
3535

36-
```go
37-
PASS: ArrayBlockingQueueSuite.BenchmarkPop 50000000 49.1 ns/op
38-
PASS: ArrayBlockingQueueSuite.BenchmarkPopOverflow 50000000 51.3 ns/op
39-
PASS: ArrayBlockingQueueSuite.BenchmarkPush 20000000 84.7 ns/op
40-
PASS: ArrayBlockingQueueSuite.BenchmarkPushOverflow 20000000 75.3 ns/op
36+
```text
37+
ArrayBlockingQueueSuite.BenchmarkPeek 50000000 56.5 ns/op
38+
ArrayBlockingQueueSuite.BenchmarkPop 50000000 49.1 ns/op
39+
ArrayBlockingQueueSuite.BenchmarkPopOverflow 50000000 51.3 ns/op
40+
ArrayBlockingQueueSuite.BenchmarkPush 20000000 84.7 ns/op
41+
ArrayBlockingQueueSuite.BenchmarkPushOverflow 20000000 75.3 ns/op
4142
```
4243

4344
## LICENCE

blockingQueue.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ func (q *BlockingQueue) Pop() (interface{}, error) {
119119

120120
if q.count == 0 {
121121
// Case empty
122-
return false, nil
122+
return false, ErrorEmpty
123123
} else {
124124
var item = q.pop()
125125
return item, nil

blockingQueue_test.go

Lines changed: 53 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,15 @@ func (s *ArrayBlockingQueueSuite) TestIncrement(c *C) {
5151
}
5252

5353
func (s *ArrayBlockingQueueSuite) TestPush(c *C) {
54-
for i:= 0;i < 10; i+=1 {
54+
for i:= 0;i < 16; i+=1 {
5555
s.queue.Push(i)
5656
}
5757

58-
c.Assert(s.queue.Size(), Equals, uint64(10))
58+
c.Assert(s.queue.Size(), Equals, uint64(16))
59+
60+
res, err := s.queue.Push(17)
61+
c.Assert(res, Equals, false)
62+
c.Assert(err, ErrorMatches, "ERROR_FULL: attempt to Put while Queue is Full")
5963
}
6064

6165

@@ -85,6 +89,10 @@ func (s *ArrayBlockingQueueSuite) TestPop(c *C) {
8589
}
8690

8791
c.Assert(s.queue.Size(), Equals, uint64(0))
92+
93+
res, err := s.queue.Pop()
94+
c.Assert(res, Equals, false)
95+
c.Assert(err, ErrorMatches, "ERROR_EMPTY: attempt to Get while Queue is Empty")
8896
}
8997

9098

@@ -102,9 +110,50 @@ func (s *ArrayBlockingQueueSuite) BenchmarkPop(c *C) {
102110
}
103111

104112
c.ResetTimer()
105-
c.StartTimer()
106113

107114
for i := 0; i < c.N; i++ {
108115
q.Pop()
109116
}
110-
}
117+
}
118+
119+
120+
func (s *ArrayBlockingQueueSuite) TestClear(c *C) {
121+
for i:= 0;i < 10; i+=1 {
122+
s.queue.Push(i)
123+
}
124+
125+
s.queue.Clear()
126+
127+
c.Assert(s.queue.Size(), Equals, uint64(0))
128+
}
129+
130+
131+
func (s *ArrayBlockingQueueSuite) TestPeek(c *C) {
132+
for i:= 0;i < 10; i+=1 {
133+
s.queue.Push(i)
134+
}
135+
136+
c.Assert(s.queue.Peek(), Equals, 0)
137+
138+
s.queue.Pop()
139+
140+
c.Assert(s.queue.Peek(), Equals, 1)
141+
}
142+
143+
144+
func (s *ArrayBlockingQueueSuite) BenchmarkPeek(c *C) {
145+
for i := 0; i < c.N; i++ {
146+
s.queue.Peek()
147+
}
148+
149+
q, _ := NewArrayBlockingQueue(math.MaxUint16)
150+
151+
q.Push(1);q.Push(1);q.Push(1)
152+
153+
c.ResetTimer()
154+
155+
for i := 0; i < c.N; i++ {
156+
s.queue.Peek()
157+
}
158+
}
159+

errors.go

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,4 @@ import "errors"
88

99
var ErrorCapacity = errors.New("ERROR_CAPACITY: attempt to Create Queue with invalid Capacity")
1010
var ErrorFull = errors.New("ERROR_FULL: attempt to Put while Queue is Full")
11-
12-
//
13-
//var (
14-
//
15-
// //ErrorEmpty = errors.New("ERROR_EMPTY: attempt to Get while Queue is Empty")
16-
// //
17-
//)
11+
var ErrorEmpty = errors.New("ERROR_EMPTY: attempt to Get while Queue is Empty")

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy