From 979b01e2510b16e6b21336207f03943d7637d6a6 Mon Sep 17 00:00:00 2001 From: huangsimin Date: Thu, 6 Dec 2018 11:30:44 +0800 Subject: [PATCH] fix circular_linked_test --- circular_linked/circular_linked.go | 12 ++++- circular_linked/circular_linked_test.go | 66 ++++++++++++++++++++++++- 2 files changed, 76 insertions(+), 2 deletions(-) diff --git a/circular_linked/circular_linked.go b/circular_linked/circular_linked.go index 714442e..a80eb55 100644 --- a/circular_linked/circular_linked.go +++ b/circular_linked/circular_linked.go @@ -1,4 +1,4 @@ -package circular_linked +package clinked import ( "log" @@ -41,6 +41,16 @@ func NewCircularLinked(values ...interface{}) *CircularLinked { return list } +// Head 链表的首 +func (list *CircularLinked) Head() *CNode { + return list.head +} + +// Tail 链表的首 +func (list *CircularLinked) Tail() *CNode { + return list.tail +} + // Cursor get current Cursor func (list *CircularLinked) Cursor() *CNode { if list.cursor == nil { diff --git a/circular_linked/circular_linked_test.go b/circular_linked/circular_linked_test.go index 980014b..467e22e 100644 --- a/circular_linked/circular_linked_test.go +++ b/circular_linked/circular_linked_test.go @@ -1 +1,65 @@ -package circular_linked +package clinked + +import "testing" + +func TestCircularLinkedLookUp(t *testing.T) { + + cl := NewCircularLinked(1, 2, 3, 4, 5, 6) + if !(cl.head.value.(int) == 1 && cl.tail.value.(int) == 6) { + t.Error(cl.LookCursor()) + } + + cl = NewCircularLinked(6, 2, 3, 4, 5, 1) + if !(cl.head.value.(int) == 6 && cl.tail.value.(int) == 1) { + t.Error("New List is error:", cl.LookCursor()) + } + + if cl.MoveNext().value.(int) != 2 { + t.Error("CursorNext error:", cl.LookCursor()) + } + + cl = NewCircularLinked(0, 1, 2, 3, 4, 5) + for i := 0; i < 6; i++ { + if cl.Cursor().value.(int) != i { + t.Error("CursorNext error:", cl.LookCursor()) + } + cl.MoveNext() + } + + for i := 0; i < 6; i++ { + if cl.Cursor().value.(int) != i { + t.Error("CursorNext loop error:", cl.LookCursor()) + } + cl.MoveNext() + } + + cl = NewCircularLinked(6, 2, 3, 4, 5, 1) + cl.Remove(cl.Cursor()) + if cl.Cursor().value != 2 { + t.Error("Remove Head is error", cl.LookCursor()) + } + cl.Remove(cl.CursorToTail()) + if cl.Cursor().value != 5 { + t.Error("Remove CursorToTail is error", cl.LookCursor()) + } + + cl.Remove(cl.CursorToHead()) + if cl.Cursor().value != 3 { + t.Error("Remove CursorToHead is error", cl.LookCursor()) + } + + limitCount := 0 + for cl.Size() > 0 { + cl.Remove(cl.Cursor()) + limitCount++ + if limitCount >= 10 { + t.Error("Not Clear", cl.LookCursor()) + break + } + } + + cl.Remove(cl.CursorToHead()) // nil is not in list is success! + if cl.head != nil || cl.tail != nil || cl.cursor != nil { + t.Error("Remove Boundary error") + } +}