structure/priority_list/priority_list_test.go
2019-03-12 10:54:52 +08:00

149 lines
2.5 KiB
Go

package plist
import (
"testing"
"github.com/emirpasic/gods/utils"
)
func TestInsert(t *testing.T) {
pl := New(utils.IntComparator)
for i := 0; i < 10; i++ {
pl.Push(i)
}
if pl.size != 10 {
t.Error(pl.size)
}
if pl.String() != "9 8 7 6 5 4 3 2 1 0" {
t.Error(pl.String())
}
if pl.RString() != "0 1 2 3 4 5 6 7 8 9" {
t.Error(pl.RString())
}
for i := 0; i < 10; i++ {
pl.Push(i)
}
if pl.String() != "9 9 8 8 7 7 6 6 5 5 4 4 3 3 2 2 1 1 0 0" {
t.Error(pl.String())
}
if pl.RString() != "0 0 1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9" {
t.Error(pl.RString())
}
}
func TestIterator(t *testing.T) {
pl := New(utils.IntComparator)
for i := 0; i < 10; i++ {
pl.Push(i)
}
iter := pl.Iterator()
for i := 0; iter.Next(); i++ {
if iter.Value() != 9-i {
t.Error("iter.Next() ", iter.Value(), "is not equal ", 9-i)
}
}
if iter.cur != iter.pl.tail {
t.Error("current point is not equal tail ", iter.pl.tail)
}
for i := 0; iter.Prev(); i++ {
if iter.Value() != i {
t.Error("iter.Prev() ", iter.Value(), "is not equal ", i)
}
}
}
func TestCircularIterator(t *testing.T) {
pl := New(utils.IntComparator)
for i := 0; i < 10; i++ {
pl.Push(i)
}
iter := pl.CircularIterator()
for i := 0; i != 10; i++ {
iter.Next()
if iter.Value() != 9-i {
t.Error("iter.Next() ", iter.Value(), "is not equal ", 9-i)
}
}
if iter.cur != iter.pl.tail.prev {
t.Error("current point is not equal tail ", iter.pl.tail.prev)
}
if iter.Next() {
if iter.Value() != 9 {
t.Error("iter.Value() != ", iter.Value())
}
}
iter.MoveToTail()
for i := 0; i != 10; i++ {
iter.Prev()
if iter.Value() != i {
t.Error("iter.Prev() ", iter.Value(), "is not equal ", i)
}
}
if iter.cur != iter.pl.head.next {
t.Error("current point is not equal tail ", iter.pl.tail.prev)
}
if iter.Prev() {
if iter.Value() != 0 {
t.Error("iter.Value() != ", iter.Value())
}
}
}
func TestGet(t *testing.T) {
pl := New(utils.IntComparator)
for i := 0; i < 10; i++ {
pl.Push(i)
}
for _, v := range []int{0, 9, 5, 7} {
if pl.Get(v) != (9 - v) {
t.Error(v, "Get == ", pl.Get(v))
}
}
func() {
defer func() {
if err := recover(); err == nil {
t.Error("out index, but is not error")
}
}()
pl.Get(10)
}()
}
func TestRemove(t *testing.T) {
pl := New(utils.IntComparator)
for i := 0; i < 10; i++ {
pl.Push(i)
}
pl.RemoveWithIndex(0)
if pl.Get(0).(int) != 8 {
t.Error(pl.Get(0))
}
pl.RemoveWithIndex(-1)
if pl.Get(-1).(int) != 1 {
t.Error(pl.Get(-1))
}
}