112 lines
1.8 KiB
Go
112 lines
1.8 KiB
Go
package plist
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/davecgh/go-spew/spew"
|
|
|
|
"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))
|
|
}
|
|
|
|
data := p.Values()
|
|
t.Error(data)
|
|
|
|
content := ""
|
|
last := 0
|
|
for i := 0; i < len(data); i++ {
|
|
if i == 0 {
|
|
content += "\n"
|
|
} else if i%5 == 0 {
|
|
content += spew.Sprintln(data[last:i])
|
|
last = i
|
|
}
|
|
}
|
|
content += spew.Sprintln(data[last:])
|
|
t.Error(content)
|
|
|
|
lleft := p.search(0)
|
|
lmid := p.search(5000)
|
|
lright := p.search(10000)
|
|
|
|
t.Error(lleft, lmid, lright)
|
|
t.Error(p.left+lleft, p.left+lmid, p.left+lright)
|
|
|
|
t.Error(p.datas, p.size, len(p.Values()))
|
|
|
|
for _, idx := range []int{5, 0, 21, 19} {
|
|
t.Error(p.Get(idx))
|
|
}
|
|
|
|
}
|
|
|
|
func BenchmarkPriorityQueue(b *testing.B) {
|
|
p := NewWithInt()
|
|
|
|
// for i := 0; i < 100000; i++ {
|
|
// p.Push(randomdata.Number(0, 100000))
|
|
// // p.Values()
|
|
// }
|
|
|
|
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()
|
|
}
|