You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
73 lines
1.7 KiB
Go
73 lines
1.7 KiB
Go
package container
|
|
|
|
import (
|
|
"testing"
|
|
)
|
|
|
|
// TestEnqueue tests the Enqueue method of the PriorityQueue.
|
|
func TestEnqueue(t *testing.T) {
|
|
pq := NewPriorityQueue[int, int](true) // Min-priority queue
|
|
pq.Enqueue(3, 3)
|
|
pq.Enqueue(1, 1)
|
|
pq.Enqueue(2, 2)
|
|
|
|
expected := []int{1, 3, 2} // Expected min-heap state after enqueuing elements
|
|
for i, v := range pq.heap.elements {
|
|
if v.Value != expected[i] {
|
|
t.Errorf("Enqueue() test failed: expected %v at index %d, got %v", expected[i], i, v)
|
|
}
|
|
}
|
|
}
|
|
|
|
// TestDequeue tests the Dequeue method of the PriorityQueue.
|
|
func TestDequeue(t *testing.T) {
|
|
pq := NewPriorityQueue[int, int](true)
|
|
pq.Enqueue(3, 3)
|
|
pq.Enqueue(1, 1)
|
|
pq.Enqueue(2, 2)
|
|
|
|
if val := pq.Dequeue(); val != 1 {
|
|
t.Errorf("Dequeue() test failed: expected 1, got %v", val)
|
|
}
|
|
|
|
if val := pq.Dequeue(); val != 2 {
|
|
t.Errorf("Dequeue() test failed: expected 2, got %v", val)
|
|
}
|
|
}
|
|
|
|
// TestPeek tests the Peek method of the PriorityQueue.
|
|
func TestPQueuePeek(t *testing.T) {
|
|
pq := NewPriorityQueue[int, int](true)
|
|
pq.Enqueue(3, 3)
|
|
pq.Enqueue(1, 1)
|
|
pq.Enqueue(2, 2)
|
|
|
|
if val := pq.Peek(); val != 1 {
|
|
t.Errorf("Peek() test failed: expected 1, got %v", val)
|
|
}
|
|
|
|
// Check if Peek() doesn't remove the element
|
|
if val := pq.Dequeue(); val != 1 {
|
|
t.Errorf("Peek() test failed: Peek() should not remove the element")
|
|
}
|
|
}
|
|
|
|
// TestIsEmpty tests the IsEmpty method of the PriorityQueue.
|
|
func TestIsEmpty(t *testing.T) {
|
|
pq := NewPriorityQueue[int, int](true)
|
|
|
|
if !pq.IsEmpty() {
|
|
t.Errorf("IsEmpty() test failed: expected true, got false")
|
|
}
|
|
|
|
pq.Enqueue(1, 1)
|
|
if pq.IsEmpty() {
|
|
t.Errorf("IsEmpty() test failed: expected false, got true")
|
|
}
|
|
|
|
pq.Dequeue()
|
|
if !pq.IsEmpty() {
|
|
t.Errorf("IsEmpty() test failed: expected true, got false")
|
|
}
|
|
}
|