structure_old/priority_queue/priority_queue_test.go

123 lines
2.0 KiB
Go
Raw Normal View History

2019-01-26 10:45:30 +00:00
package plist
import (
"testing"
2019-01-26 10:45:30 +00:00
2019-02-15 10:51:21 +00:00
"github.com/emirpasic/gods/trees/avltree"
2019-01-31 10:39:17 +00:00
"github.com/emirpasic/gods/utils"
"github.com/Pallinder/go-randomdata"
"github.com/emirpasic/gods/trees/binaryheap"
)
2019-02-15 10:51:21 +00:00
func TestPList(t *testing.T) {
pl := &PriorityList{}
for i := 0; i < 10; i++ {
pl.nodeInsert(nil, randomdata.Number(0, 10))
}
t.Error(pl.String())
cur := pl.head
for i := 0; i < 5; i++ {
cur = cur.next
}
pl.nodeInsert(cur, 11)
t.Error(pl.String())
2019-02-08 22:32:58 +00:00
}
2019-02-15 10:51:21 +00:00
func TestPQ(t *testing.T) {
pq := NewWithIntComparator()
for i := 0; i < 1; i++ {
pq.Push(i)
}
iter := pq.avl.Iterator()
iter.Next()
pl := iter.Value().(*PriorityList)
t.Error(pq.size, pq.avl.Size(), pl.Size(), pl.head == nil, pl.head)
2019-02-08 22:32:58 +00:00
}
2019-02-15 10:51:21 +00:00
func BenchmarkPQ(b *testing.B) {
pq := NewWithIntComparator()
2019-02-08 22:32:58 +00:00
2019-02-15 10:51:21 +00:00
b.N = 1000000
for i := b.N; i > 0; i-- {
pq.Push(i)
2019-02-01 11:18:48 +00:00
}
2019-02-15 10:51:21 +00:00
iter := pq.avl.Iterator()
iter.Next()
2019-02-01 11:18:48 +00:00
2019-02-15 10:51:21 +00:00
pl := iter.Value().(*PriorityList)
b.Log(pq.size, pq.avl.Size(), pl.Size(), pl.head)
b.Log(pl.head.value)
if pl.head.next != nil {
b.Log(pl.head.next.value)
}
2019-02-08 22:32:58 +00:00
2019-02-15 10:51:21 +00:00
// b.Log("all:", pq.avl.Size(), pq.avl.Values())
2019-02-08 22:32:58 +00:00
}
2019-02-01 11:18:48 +00:00
2019-02-15 10:51:21 +00:00
func BenchmarkPList(b *testing.B) {
2019-01-26 10:45:30 +00:00
2019-02-15 10:51:21 +00:00
for i := 0; i < b.N; i++ {
pl := &PriorityList{}
for i2 := 0; i2 < 5; i2++ {
cur := pl.head
for n := 0; n < i2; n++ {
cur = cur.next
}
pl.nodeInsert(cur, i2)
}
2019-01-31 10:39:17 +00:00
}
}
2019-02-15 10:51:21 +00:00
func TestAvl(t *testing.T) {
2019-02-15 10:51:21 +00:00
comparator := func(v1, v2 interface{}) int {
if v1.(int) > v2.(int) {
return 1
} else if v1.(int) < v2.(int) {
return -1
}
return 0
}
2019-02-01 11:18:48 +00:00
2019-02-15 10:51:21 +00:00
avl := avltree.NewWith(comparator)
for _, v := range []int{9, 2, 3, 4, 5, 6, 8, 1} {
avl.Put(v, v)
}
2019-02-15 10:51:21 +00:00
iter := avl.Iterator()
iter.Next()
t.Error(avl.Values(), iter.Value())
f, ok := avl.Floor(10)
if ok {
t.Error("Floor", f)
}
f, ok = avl.Ceiling(10)
if ok {
t.Error("Ceiling", f)
}
}
2019-01-26 10:45:30 +00:00
2019-01-31 10:39:17 +00:00
func TestHeap(t *testing.T) {
heap := binaryheap.NewWithIntComparator()
for i := 0; i < 10; i++ {
heap.Push(randomdata.Number(0, 1000))
}
t.Error(heap.Peek())
t.Error(heap.Values())
utils.Sort(heap.Values(), utils.IntComparator)
}