-
Notifications
You must be signed in to change notification settings - Fork 1
/
queue_test.go
107 lines (82 loc) · 1.48 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
package jdeque
import (
"testing"
"github.com/stretchr/testify/require"
)
func TestQueueFront(t *testing.T) {
q := New[int](10)
for i := 0; i < 100; i++ {
q.PushFront(i)
}
for i := 100 - 1; i >= 0; i-- {
v, ok := q.PopFront()
require.True(t, ok)
require.Equal(t, i, v)
}
v, ok := q.PopFront()
require.False(t, ok)
require.Equal(t, 0, v)
}
func TestQueueBack(t *testing.T) {
q := New[int](10)
for i := 0; i < 100; i++ {
q.PushBack(i)
}
for i := 100 - 1; i >= 0; i-- {
v, ok := q.PopBack()
require.True(t, ok)
require.Equal(t, i, v)
}
v, ok := q.PopBack()
require.False(t, ok)
require.Equal(t, 0, v)
}
func TestQueueMixed(t *testing.T) {
q := New[int](10)
q.PushFront(0)
q.PushFront(-1)
v, ok := q.PopBack()
require.True(t, ok)
require.Equal(t, 0, v)
for i := 0; i < 10; i++ {
q.PushFront(i)
}
for i := 0; i < 10; i++ {
q.PushBack(i)
}
var a []int
for {
v, ok := q.PopFront()
if !ok {
break
}
a = append(a, v)
}
expected := []int{
9, 8, 7, 6, 5, 4, 3, 2, 1, 0,
-1,
0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
}
require.Equal(t, expected, a)
}
func TestLen(t *testing.T) {
q := New[int](10)
for i := 0; i < 68; i++ {
q.PushFront(0)
}
size := q.Len()
require.Equal(t, 68, size)
}
func TestPeekFront(t *testing.T) {
q := New[int](10)
for i := 0; i < 20; i++ {
q.PushFront(i)
}
for i := 19; i >= 0; i-- {
v, ok := q.PeekFront()
require.True(t, ok)
v2, ok := q.PopFront()
require.True(t, ok)
require.Equal(t, v, v2)
}
}