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.

67 lines
1.3 KiB
Go

package container
import (
"testing"
)
func TestQueueDequeue(t *testing.T) {
// Create a new queue
q := NewQueue[int]()
// Enqueue some elements
q.Enqueue(1)
q.Enqueue(2)
q.Enqueue(3)
// Dequeue the elements and check if they match the expected values
value := q.Dequeue()
if value != 1 {
t.Errorf("Expected dequeued value to be 1, but got %v", value)
}
value = q.Dequeue()
if value != 2 {
t.Errorf("Expected dequeued value to be 2, but got %v", value)
}
value = q.Dequeue()
if value != 3 {
t.Errorf("Expected dequeued value to be 3, but got %v", value)
}
// Try to dequeue from an empty queue and expect a panic
defer func() {
if r := recover(); r == nil {
t.Errorf("Expected Dequeue() to panic, but it didn't")
}
}()
q.Dequeue()
}
func TestQueueHasElement(t *testing.T) {
// Create a new queue
q := NewQueue[int]()
// Check if the queue has elements, expect false
if q.HasElement() {
t.Errorf("Expected HasElement() to return false, but got true")
}
// Enqueue an element
q.Enqueue(1)
// Check if the queue has elements, expect true
if !q.HasElement() {
t.Errorf("Expected HasElement() to return true, but got false")
}
// Dequeue the element
q.Dequeue()
// Check if the queue has elements, expect false
if q.HasElement() {
t.Errorf("Expected HasElement() to return false, but got true")
}
}