-
Notifications
You must be signed in to change notification settings - Fork 0
/
queue_test.go
72 lines (59 loc) · 1.23 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
package qfifo
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestQueueInstantiation(t *testing.T) {
q := New(nil)
assert.NotNil(t, q)
assert.Equal(t, 0, len(q.list))
assert.Equal(t, defaultQueueSize, cap(q.list))
q = New(&QueueOptions{
InitialSize: 20,
})
assert.NotNil(t, q)
assert.Equal(t, 0, len(q.list))
assert.Equal(t, 20, cap(q.list))
}
func TestQueuePushAndPop(t *testing.T) {
q := New(nil)
assert.NotNil(t, q)
for i := 1; i <= 5; i++ {
q.Push(i)
}
assert.Equal(t, 5, len(q.list))
assert.Equal(t, defaultQueueSize, cap(q.list))
for i := 0; i < 5; i++ {
assert.Equal(t, i+1, q.list[i])
}
refMap := make(map[int]bool)
for i := 1; i <= 5; i++ {
refMap[i] = false
}
var v int
for {
if q.IsEmpty() {
break
}
v = q.Pop().(int)
refMap[v] = true
}
isVerified := true
for _, state := range refMap {
isVerified = isVerified && state
}
assert.True(t, isVerified)
}
func TestQueueClear(t *testing.T) {
q := New(nil)
assert.NotNil(t, q)
for i := 1; i <= 5; i++ {
q.Push(i)
}
assert.Equal(t, 5, len(q.list))
assert.Equal(t, defaultQueueSize, cap(q.list))
q.Clear()
assert.Equal(t, 0, len(q.list))
assert.Equal(t, defaultQueueSize, cap(q.list))
assert.True(t, q.IsEmpty())
}