@@ -113,3 +113,56 @@ func Test_priorityQueue_Swap(t *testing.T) {
113
113
})
114
114
}
115
115
}
116
+
117
+ func TestPriorityQueue_Pop (t * testing.T ) {
118
+ t .Run ("Pop from empty queue" , func (t * testing.T ) {
119
+ pq := newPriorityQueue [int , string ](0 )
120
+ if elem := heap .Pop (pq ); elem != nil {
121
+ t .Errorf ("Expected nil from empty queue, got %v" , elem )
122
+ }
123
+ })
124
+
125
+ t .Run ("Pop from queue with single element" , func (t * testing.T ) {
126
+ pq := newPriorityQueue [int , string ](10 )
127
+ heap .Push (pq , newEntry (1 , "one" ))
128
+ if pq .Len () != 1 {
129
+ t .Fatalf ("Expected queue length of 1, got %d" , pq .Len ())
130
+ }
131
+ elem := heap .Pop (pq ).(* entry [int , string ])
132
+ if elem .key != 1 || elem .val != "one" {
133
+ t .Errorf ("Expected to pop element with key=1 and val='one', got key=%d and val='%s'" , elem .key , elem .val )
134
+ }
135
+ if pq .Len () != 0 {
136
+ t .Errorf ("Expected empty queue after pop, got length %d" , pq .Len ())
137
+ }
138
+ })
139
+
140
+ t .Run ("Pop from queue with multiple elements" , func (t * testing.T ) {
141
+ pq := newPriorityQueue [int , string ](10 )
142
+ heap .Push (pq , newEntry (1 , "one" ))
143
+ heap .Push (pq , newEntry (2 , "two" ))
144
+ heap .Push (pq , newEntry (3 , "three" ))
145
+
146
+ // Pop the first element
147
+ elem := heap .Pop (pq ).(* entry [int , string ])
148
+ if elem .key != 1 || elem .val != "one" {
149
+ t .Errorf ("Expected to pop element with key=1 and val='one', got key=%d and val='%s'" , elem .key , elem .val )
150
+ }
151
+
152
+ // Pop the second element
153
+ elem = heap .Pop (pq ).(* entry [int , string ])
154
+ if elem .key != 2 || elem .val != "two" {
155
+ t .Errorf ("Expected to pop element with key=2 and val='two', got key=%d and val='%s'" , elem .key , elem .val )
156
+ }
157
+
158
+ // Pop the third element
159
+ elem = heap .Pop (pq ).(* entry [int , string ])
160
+ if elem .key != 3 || elem .val != "three" {
161
+ t .Errorf ("Expected to pop element with key=3 and val='three', got key=%d and val='%s'" , elem .key , elem .val )
162
+ }
163
+
164
+ if pq .Len () != 0 {
165
+ t .Errorf ("Expected empty queue after all pops, got length %d" , pq .Len ())
166
+ }
167
+ })
168
+ }
0 commit comments