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.

119 lines
2.4 KiB
Go

package container
import (
"reflect"
"testing"
)
func TestVectorPushBack(t *testing.T) {
v := Vector[int]{1, 2, 3}
e := 4
v.PushBack(e)
expected := Vector[int]{1, 2, 3, 4}
if !reflect.DeepEqual(v, expected) {
t.Errorf("PushBack() failed, expected %v, got %v", expected, v)
}
if v.Len() != 4 {
t.Errorf("expected a length of 4, got %v", v.Len())
}
}
func TestVectorPopBack(t *testing.T) {
v := Vector[int]{1, 2, 3, 4}
expectedValue := 4
value := v.PopBack()
if value != expectedValue {
t.Errorf("PopBack() failed, expected %v, got %v", expectedValue, value)
}
expectedVector := Vector[int]{1, 2, 3}
if !reflect.DeepEqual(v, expectedVector) {
t.Errorf("PopBack() failed, expected vector %v, got %v", expectedVector, v)
}
if v.Len() != 3 {
t.Errorf("expected a length of 3, got %v", v.Len())
}
// Test panic on empty vector
emptyVector := Vector[int]{}
defer func() {
if r := recover(); r == nil {
t.Errorf("PopBack() did not panic on empty vector")
}
}()
emptyVector.PopBack()
}
func TestVectorLen(t *testing.T) {
v := Vector[int]{1, 2, 3}
expected := 3
result := v.Len()
if result != expected {
t.Errorf("Len() failed, expected %d, got %d", expected, result)
}
}
func TestVectorHasElement(t *testing.T) {
v := Vector[int]{1, 2, 3}
hasElement := v.HasElement()
expected := true
if hasElement != expected {
t.Errorf("HasElement() failed, expected %v, got %v", expected, hasElement)
}
v = Vector[int]{}
hasElement = v.HasElement()
expected = false
if hasElement != expected {
t.Errorf("HasElement() failed, expected %v, got %v", expected, hasElement)
}
}
func TestVectorPushFront(t *testing.T) {
v := Vector[int]{1, 2, 3}
e := 4
v.PushFront(e)
expected := Vector[int]{4, 1, 2, 3}
if !reflect.DeepEqual(v, expected) {
t.Errorf("PushFront() failed, expected %v, got %v", expected, v)
}
if v.Len() != 4 {
t.Errorf("expected a length of 4, got %v", v.Len())
}
}
func TestVectorPopFront(t *testing.T) {
v := Vector[int]{1, 2, 3, 4}
expectedValue := 1
expectedVector := Vector[int]{2, 3, 4}
value := v.PopFront()
if value != expectedValue {
t.Errorf("PopFront() failed, expected value %v, got %v", expectedValue, value)
}
if !reflect.DeepEqual(v, expectedVector) {
t.Errorf("PopFront() failed, expected vector %v, got %v", expectedVector, v)
}
if v.Len() != 3 {
t.Errorf("expected a length of 3, got %v", v.Len())
}
}