fix circular_linked_test
This commit is contained in:
parent
4b5b902e65
commit
979b01e251
|
@ -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 {
|
||||
|
|
|
@ -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")
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user