Skip to content

Commit

Permalink
Added Tests for list
Browse files Browse the repository at this point in the history
Signed-off-by: ChaitanyaD48 <[email protected]>
  • Loading branch information
ChaitanyaD48 committed Jan 16, 2024
1 parent 7f4c982 commit db61b9a
Showing 1 changed file with 118 additions and 0 deletions.
118 changes: 118 additions & 0 deletions go/list/list_test.go
Original file line number Diff line number Diff line change
@@ -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")
}
}

0 comments on commit db61b9a

Please sign in to comment.