-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqueue_test.go
104 lines (96 loc) · 2.24 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
package queue
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestLen(t *testing.T) {
t.Parallel()
q := NewQueue(10)
assert.Equal(t, 0, q.Len())
}
func TestCap(t *testing.T) {
t.Parallel()
q := NewQueue(10)
assert.Equal(t, 10, q.Cap())
}
func TestEnlarge(t *testing.T) {
t.Parallel()
q := NewQueue(2)
for i := 0; i < 3; i++ {
q.Push(i)
}
assert.Equal(t, 4, q.Cap())
}
func TestPushPop1(t *testing.T) {
t.Parallel()
q := NewQueue(2)
q.Push(1)
q.Push(2)
v := q.Pop()
assert.Equal(t, 1, v.(int))
q.Push("ss")
assert.Equal(t, 2, q.Top())
q.Push("q")
assert.Equal(t, 2, q.Top())
v = q.Pop()
assert.Equal(t, 2, v.(int))
v = q.Pop()
assert.Equal(t, "ss", v.(string))
v = q.Pop()
assert.Equal(t, "q", v.(string))
v = q.Pop()
assert.Nil(t, v)
}
func TestPushPop2(t *testing.T) {
t.Parallel()
q := queue{cap: 10, head: -1,
data: make([]interface{}, 10),
limit: 1<<32 - 1}
for i := 0; i < 5; i++ {
q.Push(i)
}
for i := 5; i < 15; i++ {
v := q.Pop()
assert.Equal(t, i-5, v.(int))
q.Push(i)
if i == 7 {
assert.Equal(t, 7, q.head)
assert.Equal(t, 3, q.tail)
}
if i == 11 {
assert.Equal(t, 1, q.head)
assert.Equal(t, 7, q.tail)
}
}
assert.Equal(t, []interface{}{10, 11, 12, 13, 14, 5, 6, 7, 8, 9}, q.data)
assert.Equal(t, 4, q.head)
assert.Equal(t, 0, q.tail)
assert.Equal(t, true, q.Push(15))
assert.Equal(t, []interface{}{10, 11, 12, 13, 14, 15, 6, 7, 8, 9}, q.data)
for i := 16; i < 21; i++ {
q.Push(i)
}
assert.Equal(t, []interface{}{10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, interface{}(nil), interface{}(nil), interface{}(nil), interface{}(nil), interface{}(nil), interface{}(nil), interface{}(nil), interface{}(nil), interface{}(nil)}, q.data)
assert.Equal(t, 20, q.cap)
assert.Equal(t, 11, q.len)
assert.Equal(t, 10, q.head)
assert.Equal(t, 0, q.tail)
assert.Equal(t, 10, q.Top())
}
func TestError(t *testing.T) {
q := NewQueueWithLimit(1, 1)
q.Push(1)
assert.Equal(t, false, q.Push(2))
assert.Equal(t, ErrHitLimit, q.Err())
q = NewQueueWithLimit(2, 1)
q.Push(1)
assert.Equal(t, true, q.Push(2))
assert.Nil(t, q.Err())
q.Push(12)
assert.Equal(t, ErrHitLimit, q.Err())
q = NewQueueWithLimit(2, 3)
for i := 0; i < 4; i++ {
q.Push(i)
}
assert.Equal(t, ErrHitLimit, q.Err())
}