From db61b9a5490aea76ee64cad9ea3dbe765c33d8f5 Mon Sep 17 00:00:00 2001 From: ChaitanyaD48 Date: Tue, 16 Jan 2024 14:02:15 +0530 Subject: [PATCH] Added Tests for list Signed-off-by: ChaitanyaD48 --- go/list/list_test.go | 118 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 118 insertions(+) create mode 100644 go/list/list_test.go diff --git a/go/list/list_test.go b/go/list/list_test.go new file mode 100644 index 00000000000..29de9aff8fb --- /dev/null +++ b/go/list/list_test.go @@ -0,0 +1,118 @@ +package list + +import ( + "testing" +) + +// Can initialize an empty list +func TestInitEmptyList(t *testing.T) { + l := New[int]() + if l.Len() != 0 { + t.Errorf("Expected length of 0, got %d", l.Len()) + } + if l.Front() != nil { + t.Error("Expected Front() to be nil") + } + if l.Back() != nil { + t.Error("Expected Back() to be nil") + } +} + // Can insert an element at the front of the list +func TestInsertFront(t *testing.T) { + l := New[int]() + e := l.PushFront(1) + if l.Len() != 1 { + t.Errorf("Expected length of 1, got %d", l.Len()) + } + if l.Front() != e { + t.Error("Expected Front() to be the inserted element") + } + if l.Back() != e { + t.Error("Expected Back() to be the inserted element") + } +} + // Can insert an element at the back of the list +func TestInsertBack(t *testing.T) { + l := New[int]() + e := l.PushBack(1) + if l.Len() != 1 { + t.Errorf("Expected length of 1, got %d", l.Len()) + } + if l.Front() != e { + t.Error("Expected Front() to be the inserted element") + } + if l.Back() != e { + t.Error("Expected Back() to be the inserted element") + } +} + // Can insert an element at the front of an empty list +func TestInsertFrontEmptyList(t *testing.T) { + l := New[int]() + e := l.PushFront(1) + if l.Len() != 1 { + t.Errorf("Expected length of 1, got %d", l.Len()) + } + if l.Front() != e { + t.Error("Expected Front() to be the inserted element") + } + if l.Back() != e { + t.Error("Expected Back() to be the inserted element") + } +} + // Can insert an element at the back of an empty list +func TestInsertBackEmptyList(t *testing.T) { + l := New[int]() + e := l.PushBack(1) + if l.Len() != 1 { + t.Errorf("Expected length of 1, got %d", l.Len()) + } + if l.Front() != e { + t.Error("Expected Front() to be the inserted element") + } + if l.Back() != e { + t.Error("Expected Back() to be the inserted element") + } +} + // Can remove the only element from the list +func TestRemoveOnlyElement(t *testing.T) { + l := New[int]() + e := l.PushFront(1) + l.Remove(e) + if l.Len() != 0 { + t.Errorf("Expected length of 0, got %d", l.Len()) + } + if l.Front() != nil { + t.Error("Expected Front() to be nil") + } + if l.Back() != nil { + t.Error("Expected Back() to be nil") + } +} +func TestRemoveElement(t *testing.T) { + l := New[int]() + e := l.PushFront(1) + l.Remove(e) + if l.Len() != 0 { + t.Errorf("Expected length of 0, got %d", l.Len()) + } + if l.Front() != nil { + t.Error("Expected Front() to be nil") + } + if l.Back() != nil { + t.Error("Expected Back() to be nil") + } +} +func TestGetFirstElement(t *testing.T) { + l := New[int]() + e := l.PushFront(1) + if l.Front() != e { + t.Error("Expected Front() to be the first element") + } +} +func TestGetLastElement(t *testing.T) { + l := New[int]() + e := l.PushBack(1) + if l.Back() != e { + t.Error("Expected Back() to be the last element") + } +}