structure_old/priority_queue/priority_queue_test.go
2019-02-13 16:47:11 +08:00

107 lines
1.6 KiB
Go

package plist
import (
"testing"
"github.com/emirpasic/gods/utils"
"github.com/Pallinder/go-randomdata"
"github.com/emirpasic/gods/trees/binaryheap"
)
func TestGetPriorityQueue(t *testing.T) {
p := NewWithInt()
var l []int
for i := 0; i < 10; i++ {
l = append(l, randomdata.Number(0, 10000))
}
for _, v := range l {
p.Push(v)
t.Log(p.String())
}
t.Error(l)
t.Error(p.String())
for _, i := range []int{-1, 0, p.size / 2, p.size - 1, p.size} {
v, ok := p.Get(i)
t.Error(i, v, ok)
}
}
func TestPustPriorityQueue(t *testing.T) {
p := NewWithInt()
for i := 0; i < 100; i++ {
p.Push(randomdata.Number(0, 10000))
t.Log(p.String())
}
t.Error(p.String())
}
func BenchmarkPriorityQueue(b *testing.B) {
p := NewWithInt()
// for i := 0; i < 10000; i++ {
// p.Push(randomdata.Number(0, 100000))
// // p.Values()
// }
b.N = 100000
for i := 0; i < b.N; i++ {
p.Push(randomdata.Number(0, 100000))
}
}
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()
}