87 lines
1.3 KiB
Go
87 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 < 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))
|
|
if i%100 == 0 {
|
|
p.Values()
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
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()
|
|
}
|