2018-12-06 03:30:44 +00:00
|
|
|
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")
|
|
|
|
}
|
|
|
|
}
|