From 57cfac4d03c8d266bc95cd363f3bb6961cf4f876 Mon Sep 17 00:00:00 2001 From: Jayson Wang Date: Sun, 15 Jan 2023 19:39:03 +0800 Subject: [PATCH] improve the code robustness of slist (#115) --- slist.go | 26 +++++++++++++++++--------- slist_test.go | 42 +++++++++++++++++++++++++++++++++++++++++- 2 files changed, 58 insertions(+), 10 deletions(-) diff --git a/slist.go b/slist.go index 1069b01..a7b5dc6 100644 --- a/slist.go +++ b/slist.go @@ -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 } @@ -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 } @@ -166,7 +174,7 @@ type sListIterator[T any] struct { node *sListNode[T] } -func (it sListIterator[T]) IsNotEnd() bool { +func (it *sListIterator[T]) IsNotEnd() bool { return it.node != nil } @@ -174,11 +182,11 @@ 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 } diff --git a/slist_test.go b/slist_test.go index a4bbfc5..b72bc63 100644 --- a/slist_test.go +++ b/slist_test.go @@ -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) } @@ -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++ { @@ -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)