|
|
|
|
@@ -10,9 +10,9 @@ func TestLinkedList_Empty(t *testing.T) {
|
|
|
|
|
list *LinkedList[int]
|
|
|
|
|
want bool
|
|
|
|
|
}{
|
|
|
|
|
{description: "no items in list", list: newList(), want: true},
|
|
|
|
|
{description: "one item in list", list: newList(1), want: false},
|
|
|
|
|
{description: "more items in list", list: newList(1, 2, 3), want: false},
|
|
|
|
|
{description: "no items in list", list: NewLinkedList[int](), want: true},
|
|
|
|
|
{description: "one item in list", list: NewLinkedList(1), want: false},
|
|
|
|
|
{description: "more items in list", list: NewLinkedList(1, 2, 3), want: false},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for _, tt := range useCases {
|
|
|
|
|
@@ -29,9 +29,9 @@ func TestLinkedList_Size(t *testing.T) {
|
|
|
|
|
list *LinkedList[int]
|
|
|
|
|
want int
|
|
|
|
|
}{
|
|
|
|
|
{description: "no items in list", list: newList(), want: 0},
|
|
|
|
|
{description: "one item in list", list: newList(1), want: 1},
|
|
|
|
|
{description: "more items in list", list: newList(1, 2, 3), want: 3},
|
|
|
|
|
{description: "no items in list", list: NewLinkedList[int](), want: 0},
|
|
|
|
|
{description: "one item in list", list: NewLinkedList(1), want: 1},
|
|
|
|
|
{description: "more items in list", list: NewLinkedList(1, 2, 3), want: 3},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for _, tt := range useCases {
|
|
|
|
|
@@ -50,13 +50,13 @@ func TestLinkedList_GetAt(t *testing.T) {
|
|
|
|
|
err error
|
|
|
|
|
want int
|
|
|
|
|
}{
|
|
|
|
|
{description: "get first item", list: newList(1, 2, 3), pos: 0, want: 1, err: nil},
|
|
|
|
|
{description: "get middle item", list: newList(1, 2, 3), pos: 1, want: 2, err: nil},
|
|
|
|
|
{description: "get last item", list: newList(1, 2, 3), pos: 2, want: 3, err: nil},
|
|
|
|
|
{description: "get last item", list: newList(1, 2, 3), pos: 2, want: 3, err: nil},
|
|
|
|
|
{description: "no items in list return zero value of the type", list: newList(), pos: 0, want: 0, err: ErrEmptyList},
|
|
|
|
|
{description: "get item with negative position", list: newList(1, 2, 3), pos: -1, want: 0, err: ErrPositionNegative},
|
|
|
|
|
{description: "get item with index out of bound", list: newList(1, 2, 3), pos: 4, want: 0, err: ErrIndexOutOfBound{4, 3}},
|
|
|
|
|
{description: "get first item", list: NewLinkedList(1, 2, 3), pos: 0, want: 1, err: nil},
|
|
|
|
|
{description: "get middle item", list: NewLinkedList(1, 2, 3), pos: 1, want: 2, err: nil},
|
|
|
|
|
{description: "get last item", list: NewLinkedList(1, 2, 3), pos: 2, want: 3, err: nil},
|
|
|
|
|
{description: "get last item", list: NewLinkedList(1, 2, 3), pos: 2, want: 3, err: nil},
|
|
|
|
|
{description: "no items in list return zero value of the type", list: NewLinkedList[int](), pos: 0, want: 0, err: ErrEmptyList},
|
|
|
|
|
{description: "get item with negative position", list: NewLinkedList(1, 2, 3), pos: -1, want: 0, err: ErrPositionNegative},
|
|
|
|
|
{description: "get item with index out of bound", list: NewLinkedList(1, 2, 3), pos: 4, want: 0, err: ErrIndexOutOfBound{4, 3}},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for _, tt := range useCases {
|
|
|
|
|
@@ -74,9 +74,9 @@ func TestLinkedList_Back(t *testing.T) {
|
|
|
|
|
err error
|
|
|
|
|
want int
|
|
|
|
|
}{
|
|
|
|
|
{description: "no items in list return zero value of the type", list: newList(), want: 0, err: ErrEmptyList},
|
|
|
|
|
{description: "singleton list return first element", list: newList(1), want: 1},
|
|
|
|
|
{description: "get last item", list: newList(1, 2, 3), want: 3},
|
|
|
|
|
{description: "no items in list return zero value of the type", list: NewLinkedList[int](), want: 0, err: ErrEmptyList},
|
|
|
|
|
{description: "singleton list return first element", list: NewLinkedList(1), want: 1},
|
|
|
|
|
{description: "get last item", list: NewLinkedList(1, 2, 3), want: 3},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for _, tt := range useCases {
|
|
|
|
|
@@ -94,9 +94,9 @@ func TestLinkedList_Front(t *testing.T) {
|
|
|
|
|
err error
|
|
|
|
|
want int
|
|
|
|
|
}{
|
|
|
|
|
{description: "no items in list return zero value of the type", list: newList(), want: 0, err: ErrEmptyList},
|
|
|
|
|
{description: "singleton list return first element", list: newList(1), want: 1},
|
|
|
|
|
{description: "get first item", list: newList(1, 2, 3), want: 1},
|
|
|
|
|
{description: "no items in list return zero value of the type", list: NewLinkedList[int](), want: 0, err: ErrEmptyList},
|
|
|
|
|
{description: "singleton list return first element", list: NewLinkedList(1), want: 1},
|
|
|
|
|
{description: "get first item", list: NewLinkedList(1, 2, 3), want: 1},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for _, tt := range useCases {
|
|
|
|
|
@@ -116,23 +116,23 @@ func TestLinkedList_PushAt(t *testing.T) {
|
|
|
|
|
item int
|
|
|
|
|
}{
|
|
|
|
|
{description: "add item in first position in empty list",
|
|
|
|
|
original: newList(),
|
|
|
|
|
modified: newList(1),
|
|
|
|
|
original: NewLinkedList[int](),
|
|
|
|
|
modified: NewLinkedList(1),
|
|
|
|
|
pos: 0,
|
|
|
|
|
item: 1},
|
|
|
|
|
{description: "add item in first position in full list",
|
|
|
|
|
original: newList(1, 2, 3),
|
|
|
|
|
modified: newList(0, 1, 2, 3),
|
|
|
|
|
original: NewLinkedList(1, 2, 3),
|
|
|
|
|
modified: NewLinkedList(0, 1, 2, 3),
|
|
|
|
|
pos: 0,
|
|
|
|
|
item: 0},
|
|
|
|
|
{description: "add item in middle position in full list",
|
|
|
|
|
original: newList(1, 2, 3),
|
|
|
|
|
modified: newList(1, 2, 0, 3),
|
|
|
|
|
original: NewLinkedList(1, 2, 3),
|
|
|
|
|
modified: NewLinkedList(1, 2, 0, 3),
|
|
|
|
|
pos: 2,
|
|
|
|
|
item: 0},
|
|
|
|
|
{description: "add item in last position in full list",
|
|
|
|
|
original: newList(1, 2, 3),
|
|
|
|
|
modified: newList(1, 2, 3, 0),
|
|
|
|
|
original: NewLinkedList(1, 2, 3),
|
|
|
|
|
modified: NewLinkedList(1, 2, 3, 0),
|
|
|
|
|
pos: 3,
|
|
|
|
|
item: 0},
|
|
|
|
|
}
|
|
|
|
|
@@ -154,16 +154,16 @@ func TestLinkedList_PushBack(t *testing.T) {
|
|
|
|
|
item int
|
|
|
|
|
}{
|
|
|
|
|
{description: "add item in first position in empty list",
|
|
|
|
|
original: newList(),
|
|
|
|
|
modified: newList(1),
|
|
|
|
|
original: NewLinkedList[int](),
|
|
|
|
|
modified: NewLinkedList(1),
|
|
|
|
|
item: 1},
|
|
|
|
|
{description: "add item in singleton list",
|
|
|
|
|
original: newList(1),
|
|
|
|
|
modified: newList(1, 0),
|
|
|
|
|
original: NewLinkedList(1),
|
|
|
|
|
modified: NewLinkedList(1, 0),
|
|
|
|
|
item: 0},
|
|
|
|
|
{description: "add item in full list",
|
|
|
|
|
original: newList(1, 2, 3),
|
|
|
|
|
modified: newList(1, 2, 3, 0),
|
|
|
|
|
original: NewLinkedList(1, 2, 3),
|
|
|
|
|
modified: NewLinkedList(1, 2, 3, 0),
|
|
|
|
|
item: 0},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@@ -184,16 +184,16 @@ func TestLinkedList_PushFront(t *testing.T) {
|
|
|
|
|
item int
|
|
|
|
|
}{
|
|
|
|
|
{description: "add item in first position in empty list",
|
|
|
|
|
original: newList(),
|
|
|
|
|
modified: newList(1),
|
|
|
|
|
original: NewLinkedList[int](),
|
|
|
|
|
modified: NewLinkedList(1),
|
|
|
|
|
item: 1},
|
|
|
|
|
{description: "add item in singleton list",
|
|
|
|
|
original: newList(1),
|
|
|
|
|
modified: newList(0, 1),
|
|
|
|
|
original: NewLinkedList(1),
|
|
|
|
|
modified: NewLinkedList(0, 1),
|
|
|
|
|
item: 0},
|
|
|
|
|
{description: "add item in full list",
|
|
|
|
|
original: newList(1, 2, 3),
|
|
|
|
|
modified: newList(0, 1, 2, 3),
|
|
|
|
|
original: NewLinkedList(1, 2, 3),
|
|
|
|
|
modified: NewLinkedList(0, 1, 2, 3),
|
|
|
|
|
item: 0},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@@ -214,33 +214,33 @@ func TestLinkedList_DeleteAt(t *testing.T) {
|
|
|
|
|
err error
|
|
|
|
|
}{
|
|
|
|
|
{description: "delete item with negative position",
|
|
|
|
|
original: newList(),
|
|
|
|
|
original: NewLinkedList[int](),
|
|
|
|
|
modified: nil,
|
|
|
|
|
pos: -1,
|
|
|
|
|
err: ErrPositionNegative},
|
|
|
|
|
{description: "delete item in empty list",
|
|
|
|
|
original: newList(),
|
|
|
|
|
original: NewLinkedList[int](),
|
|
|
|
|
modified: nil,
|
|
|
|
|
pos: 0,
|
|
|
|
|
err: ErrEmptyList},
|
|
|
|
|
{description: "delete item in position not found",
|
|
|
|
|
original: newList(1, 2, 3),
|
|
|
|
|
modified: newList(),
|
|
|
|
|
original: NewLinkedList(1, 2, 3),
|
|
|
|
|
modified: NewLinkedList[int](),
|
|
|
|
|
pos: 5,
|
|
|
|
|
err: ErrNodeNotFound},
|
|
|
|
|
{description: "delete item in first position",
|
|
|
|
|
original: newList(1, 2, 3),
|
|
|
|
|
modified: newList(2, 3),
|
|
|
|
|
original: NewLinkedList(1, 2, 3),
|
|
|
|
|
modified: NewLinkedList(2, 3),
|
|
|
|
|
pos: 0,
|
|
|
|
|
err: nil},
|
|
|
|
|
{description: "delete item in middle position",
|
|
|
|
|
original: newList(1, 2, 3),
|
|
|
|
|
modified: newList(1, 3),
|
|
|
|
|
original: NewLinkedList(1, 2, 3),
|
|
|
|
|
modified: NewLinkedList(1, 3),
|
|
|
|
|
pos: 1,
|
|
|
|
|
err: nil},
|
|
|
|
|
{description: "delete item in last position",
|
|
|
|
|
original: newList(1, 2, 3),
|
|
|
|
|
modified: newList(1, 2),
|
|
|
|
|
original: NewLinkedList(1, 2, 3),
|
|
|
|
|
modified: NewLinkedList(1, 2),
|
|
|
|
|
pos: 2,
|
|
|
|
|
err: nil},
|
|
|
|
|
}
|
|
|
|
|
@@ -253,14 +253,6 @@ func TestLinkedList_DeleteAt(t *testing.T) {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func newList(items ...int) *LinkedList[int] {
|
|
|
|
|
lst := &LinkedList[int]{}
|
|
|
|
|
for _, item := range items {
|
|
|
|
|
lst.PushBack(item)
|
|
|
|
|
}
|
|
|
|
|
return lst
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func compareLists(lst1, lst2 *LinkedList[int]) bool {
|
|
|
|
|
for it := lst1.Iterator(); it.HasNext(); {
|
|
|
|
|
i, item1 := it.NextWithIndex()
|
|
|
|
|
|