-
Notifications
You must be signed in to change notification settings - Fork 0
/
queue_test.go
100 lines (82 loc) · 1.87 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
package gods
import (
"errors"
"testing"
)
func checkQueue[T comparable](q *Queue[T], ch []T) error {
return checkList(&q.List, ch)
}
func TestQueue(t *testing.T) {
queue := NewQueue[int]()
t.Run("empty", func(t *testing.T) {
check := []int{}
if err := checkQueue(queue, check); err != nil {
t.Fatal(err)
}
})
t.Run("push one", func(t *testing.T) {
queue.Push(1)
check := []int{1}
if err := checkQueue(queue, check); err != nil {
t.Fatal(err)
}
})
t.Run("peek one", func(t *testing.T) {
v, err := queue.Peek()
check := []int{1}
if err != nil {
t.Fatalf("unexpected error %v", err)
}
if v != 1 {
t.Fatalf("got %d expected 1", v)
}
if err := checkQueue(queue, check); err != nil {
t.Fatal(err)
}
})
t.Run("push two", func(t *testing.T) {
queue.Push(2)
check := []int{1, 2}
if err := checkQueue(queue, check); err != nil {
t.Fatal(err)
}
})
t.Run("pop one", func(t *testing.T) {
val, err := queue.Pop()
if err != nil {
t.Fatalf("unexpected error popping populated queue: %v", err)
}
if val != 1 {
t.Fatalf("expected value popped, expected 1, got %d", val)
}
check := []int{2}
if err := checkQueue(queue, check); err != nil {
t.Fatal(err)
}
})
t.Run("pop two", func(t *testing.T) {
val, err := queue.Pop()
if err != nil {
t.Fatalf("unexpected error popping populated queue: %v", err)
}
if val != 2 {
t.Fatalf("expected value popped, expected 2, got %d", val)
}
check := []int{}
if err := checkQueue(queue, check); err != nil {
t.Fatal(err)
}
})
t.Run("peek empty", func(t *testing.T) {
_, err := queue.Peek()
if !errors.Is(err, ErrQueueEmpty) {
t.Fatalf("expected empty queue error, got %v", err)
}
})
t.Run("pop empty", func(t *testing.T) {
_, err := queue.Pop()
if !errors.Is(err, ErrQueueEmpty) {
t.Fatalf("expected empty queue error, got %v", err)
}
})
}