Skip to content

Commit

Permalink
improve the code robustness of slist (#115)
Browse files Browse the repository at this point in the history
  • Loading branch information
wjiec authored Jan 15, 2023
1 parent f001ee3 commit 57cfac4
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 10 deletions.
26 changes: 17 additions & 9 deletions slist.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,17 @@ func (l *SList[T]) Clear() {

// Front returns the first element in the list.
func (l *SList[T]) Front() T {
if l.IsEmpty() {
panic("!IsEmpty")
}
return l.head.value
}

// Back returns the last element in the list.
func (l *SList[T]) Back() T {
if l.IsEmpty() {
panic("!IsEmpty")
}
return l.tail.value
}

Expand Down Expand Up @@ -74,14 +80,16 @@ func (l *SList[T]) PushBack(v T) {
// PopFront popups an element from the front of the list.
// The list must be non-empty!
func (l *SList[T]) PopFront() T {
if l.IsEmpty() {
panic("!IsEmpty")
}

node := l.head
if node != nil {
l.head = node.next
if l.head == nil {
l.tail = nil
}
l.length--
l.head = node.next
if l.head == nil {
l.tail = nil
}
l.length--
return node.value
}

Expand Down Expand Up @@ -166,19 +174,19 @@ type sListIterator[T any] struct {
node *sListNode[T]
}

func (it sListIterator[T]) IsNotEnd() bool {
func (it *sListIterator[T]) IsNotEnd() bool {
return it.node != nil
}

func (it *sListIterator[T]) MoveToNext() {
it.node = it.node.next
}

func (it sListIterator[T]) Value() T {
func (it *sListIterator[T]) Value() T {
return it.node.value
}

func (it sListIterator[T]) Pointer() *T {
func (it *sListIterator[T]) Pointer() *T {
return &it.node.value
}

Expand Down
42 changes: 41 additions & 1 deletion slist_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"testing"
)

func Test_DList_Interface(t *testing.T) {
func Test_SList_Interface(t *testing.T) {
sl := SList[int]{}
_ = Container(&sl)
}
Expand All @@ -17,6 +17,44 @@ func Test_SList_Clean(t *testing.T) {
expectEq(t, sl.Len(), 0)
}

func Test_SList_Front(t *testing.T) {
t.Run("empty", func(t *testing.T) {
sl := SList[int]{}
expectPanic(t, func() { sl.Front() })
})

t.Run("normal", func(t *testing.T) {
sl := SList[int]{}
sl.PushFront(1)
expectEq(t, sl.Front(), 1)

sl.PushBack(2)
expectEq(t, sl.Front(), 1)

sl.PushFront(3)
expectEq(t, sl.Front(), 3)
})
}

func Test_SList_Back(t *testing.T) {
t.Run("empty", func(t *testing.T) {
sl := SList[int]{}
expectPanic(t, func() { sl.Back() })
})

t.Run("normal", func(t *testing.T) {
sl := SList[int]{}
sl.PushBack(1)
expectEq(t, sl.Back(), 1)

sl.PushFront(2)
expectEq(t, sl.Back(), 1)

sl.PushBack(3)
expectEq(t, sl.Back(), 3)
})
}

func Test_SList_PushFront(t *testing.T) {
sl := SList[int]{}
for i := 1; i < 10; i++ {
Expand All @@ -38,6 +76,8 @@ func Test_SList_PushBack(t *testing.T) {

func Test_SList_PopFront(t *testing.T) {
sl := SList[int]{}
expectPanic(t, func() { sl.PopFront() })

sl.PushFront(1)
sl.PushFront(2)
expectEq(t, sl.PopFront(), 2)
Expand Down

0 comments on commit 57cfac4

Please sign in to comment.