-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqueue_test.go
137 lines (107 loc) · 2.79 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package main
import (
"testing"
)
func TestQueueAdd(t *testing.T) {
queue := NewQueue[int]()
// Add elements to the queue
queue.Add(1)
queue.Add(2)
queue.Add(3)
// Check if the queue length is updated correctly
if queue.GetLength() != 3 {
t.Error("Queue length should be 3, but got", queue.GetLength())
}
}
func TestQueuePeek(t *testing.T) {
queue := NewQueue[int]()
// Add elements to the queue
queue.Add(1)
queue.Add(2)
queue.Add(3)
// Peek at the head value
peekedValue := queue.Peek()
// Check if the peeked value is correct
if peekedValue != 1 {
t.Error("Queue peeked value should be 1, but got", peekedValue)
}
// Check if the queue remains unchanged after peeking
if queue.GetLength() != 3 {
t.Error("Queue length should remain unchanged after peeking")
}
}
func TestQueueIsEmpty(t *testing.T) {
queue := NewQueue[int]()
// Check if the empty queue is empty
if !queue.IsEmpty() {
t.Error("Empty queue should be empty")
}
// Add an element to the queue
queue.Add(1)
// Check if the non-empty queue is not empty
if queue.IsEmpty() {
t.Error("Non-empty queue should not be empty")
}
}
func TestQueueGetLength(t *testing.T) {
queue := NewQueue[int]()
// Check the length of the empty queue
if queue.GetLength() != 0 {
t.Error("Empty queue length should be 0, but got", queue.GetLength())
}
// Add elements to the queue
queue.Add(1)
queue.Add(2)
queue.Add(3)
// Check the length of the non-empty queue
if queue.GetLength() != 3 {
t.Error("Queue length should be 3, but got", queue.GetLength())
}
}
func TestQueueContains(t *testing.T) {
queue := NewQueue[int]()
// Add elements to the queue
queue.Add(1)
queue.Add(2)
queue.Add(3)
// Check if the queue contains element 2
contains, index := queue.Contains(2)
if !contains || index != 1 {
t.Error("Queue should contain element 2 at index 1")
}
// Check if the queue does not contain element 4
contains, index = queue.Contains(4)
if contains || index != -1 {
t.Error("Queue should not contain element 4")
}
}
func TestQueuePop(t *testing.T) {
queue := NewQueue[int]()
// Add elements to the queue
queue.Add(1)
queue.Add(2)
queue.Add(3)
// Pop an element from the queue
poppedNode := queue.Pop()
// Check if the popped element is correct
if poppedNode.val != 1 {
t.Error("Popped element value should be 1, but got", poppedNode.val)
}
// Check if the queue length is updated correctly
if queue.GetLength() != 2 {
t.Error("Queue length should be 2, but got", queue.GetLength())
}
}
func TestQueuePrintData(t *testing.T) {
queue := NewQueue[int]()
// Add elements to the queue
queue.Add(1)
queue.Add(2)
queue.Add(3)
// Print the contents of the queue
t.Log("Queue contents:")
queue.PrintData()
// The expected output should be:
// 1>>2>>3
t.Log("Expected output: 1 -> 2 -> 3")
}