-
Notifications
You must be signed in to change notification settings - Fork 2
/
circular_queue_test.go
76 lines (66 loc) · 1.44 KB
/
circular_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
package go_circular_queue
import (
"fmt"
"testing"
)
func TestSimple(t *testing.T) {
queue := NewCircularQueue(3)
queue.Enqueue(1)
queue.Enqueue(2)
queue.Enqueue(3)
if queue.IsFull() {
fmt.Println("Queue is full.")
}
v1, ok := queue.Dequeue()
v2, ok := queue.Dequeue()
v3, ok := queue.Dequeue()
if ok {
fmt.Println("Values %d, %d, %d", v1, v2, v3)
}
if queue.IsEmpty() {
fmt.Println("Queue is empty.")
}
}
func TestNewCircularQueue(t *testing.T) {
size := 100
queue := NewCircularQueue(size)
if queue == nil {
t.Errorf("NewCircularQueue returned nil")
}
if queue.Size() != size {
t.Errorf("Expected queue size of %d but got %d", size, queue.Size())
}
for i := 0; i < size; i++ {
queue.Enqueue(i)
}
if !queue.IsFull() {
t.Errorf("Expected the queue to be full.")
}
if queue.Enqueue(101) {
t.Errorf("Expected not enqueue extra value.")
}
for i := 0; i < size; i++ {
if v, ok := queue.Dequeue(); v != i || !ok {
t.Errorf("Expected return value of %d got %d with return status %t.", i, v, ok)
}
}
if !queue.IsEmpty() {
t.Errorf("Expected queue to be empty.")
}
if _, ok := queue.Dequeue(); ok {
t.Errorf("Expected to return status false but received true")
}
}
func BenchmarkNewCircularQueue(b *testing.B) {
queue := NewCircularQueue(b.N)
for i := 0; i < b.N; i++ {
queue.Enqueue(i)
queue.IsFull()
queue.IsEmpty()
}
for i:= 0; i < b.N; i++ {
queue.Dequeue()
queue.IsEmpty()
queue.IsFull()
}
}