-
Notifications
You must be signed in to change notification settings - Fork 0
/
queue_test.go
118 lines (98 loc) · 2.02 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
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package laney
import (
"strconv"
"testing"
)
func TestQueueEnqueue(t *testing.T) {
queue := NewQueue[string]()
queueSize := 100
// Populate test queue and assert Enqueue
// function does not fail
for i := 0; i < queueSize; i++ {
var value string = strconv.Itoa(i)
queue.Enqueue(value)
}
assert(
t,
queue.Size() == queueSize,
"queue.Size() = %d; want %d", queue.Size(), 3,
)
assert(
t,
queue.First() == "99",
"queue.Size() = %s; want %s", queue.First(), "99")
assert(
t,
queue.Last() == "0",
"queue.Last() = %s; want %s", queue.Last(), "0",
)
}
func TestQueueDequeue_fulfilled(t *testing.T) {
queue := NewQueue[string]()
queueSize := 100
// Populate test queue and assert Enqueue
// function does not fail
for i := 0; i < queueSize; i++ {
var value string = strconv.Itoa(i)
queue.Enqueue(value)
}
// Check that while deuqueing, elements come out in
// their insertion order
for i := 0; i < queueSize; i++ {
item := queue.Dequeue()
expectedValue := strconv.Itoa(i)
assert(
t,
item == expectedValue,
"queue.Dequeue() = %s; want %s", item, expectedValue,
)
assert(
t,
queue.Size() == queueSize-(i+1),
"queue.Size() = %d; want %d", queue.Size(), queueSize-(i+1),
)
}
}
func TestQueueDequeue_empty(t *testing.T) {
queue := NewQueue[*string]()
item := queue.Dequeue()
assert(
t,
item == nil,
"queue.Dequeue() = %v; want %v", item, nil,
)
assert(
t,
queue.Size() == 0,
"queue.Size() = %d; want %d", queue.Size(), 0,
)
}
func TestQueueHead_fulfilled(t *testing.T) {
queue := NewQueue[string]()
queue.Enqueue("1")
item := queue.Head()
assert(
t,
item == "1",
"queue.Enqueue() = %s; want %s", item, "1",
)
assert(
t,
queue.Size() == 1,
"queue.Size() = %d; want %d", queue.Size(), 1,
)
}
func TestQueueHead_empty(t *testing.T) {
queue := NewQueue[*string]()
item := queue.Head()
assert(
t,
item == nil,
"queue.Head() = %v; want %v", item, nil,
)
assert(
t,
queue.Size() == 0,
"queue.Size() = %d; want %d", queue.Size(), 0,
)
}