structure_old/priority_queue/priority_queue_test.go
2019-02-01 03:57:40 +08:00

88 lines
1.3 KiB
Go

package plist
import (
"testing"
"github.com/emirpasic/gods/utils"
"github.com/Pallinder/go-randomdata"
"github.com/emirpasic/gods/trees/binaryheap"
)
func TestPriorityQueue(t *testing.T) {
p := NewWithInt()
for i := 0; i < 20; i++ {
p.Push(randomdata.Number(0, 10000))
}
t.Error(p.Values())
t.Error(p.datas, p.size, len(p.Values()))
t.Error(p.search(1000000))
for _, idx := range []int{5, 0, 21, 19} {
t.Error(p.Get(idx))
}
}
func BenchmarkPriorityQueue10(b *testing.B) {
p := NewWithInt()
for i := 0; i < b.N; i++ {
p.Push(randomdata.Number(0, 100000))
if i%100 == 0 {
p.Values()
}
}
// t.Error(p.String())
}
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)
}
func BenchmarkList_InsertValues123(b *testing.B) {
a := func(v1, v2 interface{}) int {
if v1.(int) > v2.(int) {
return -1
}
return 1
}
h := binaryheap.NewWith(a)
TOPK := 50
for i := 0; i < TOPK*1000; i++ {
h.Push(i)
}
b.StartTimer()
for i := 0; i < b.N; i++ {
h.Push(i)
l := []interface{}{}
for n := 0; n < TOPK; n++ {
v, _ := h.Pop()
l = append(l, v)
}
for _, v := range l {
h.Push(v)
}
}
b.StopTimer()
}