structure_old/priority_queue/priority_queue_test.go

107 lines
1.6 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-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-13 08:47:11 +00:00
func TestGetPriorityQueue(t *testing.T) {
p := NewWithInt()
2019-02-08 22:32:58 +00:00
2019-02-13 08:47:11 +00:00
var l []int
2019-02-08 22:32:58 +00:00
for i := 0; i < 10; i++ {
2019-02-13 08:47:11 +00:00
l = append(l, randomdata.Number(0, 10000))
}
for _, v := range l {
p.Push(v)
t.Log(p.String())
2019-02-01 11:18:48 +00:00
}
2019-02-13 08:47:11 +00:00
t.Error(l)
t.Error(p.String())
2019-02-01 11:18:48 +00:00
2019-02-13 08:47:11 +00:00
for _, i := range []int{-1, 0, p.size / 2, p.size - 1, p.size} {
v, ok := p.Get(i)
t.Error(i, v, ok)
}
2019-02-08 22:32:58 +00:00
}
2019-02-01 11:18:48 +00:00
2019-02-13 08:47:11 +00:00
func TestPustPriorityQueue(t *testing.T) {
2019-02-08 22:32:58 +00:00
p := NewWithInt()
2019-01-26 10:45:30 +00:00
2019-02-13 02:46:30 +00:00
for i := 0; i < 100; i++ {
2019-02-08 22:32:58 +00:00
p.Push(randomdata.Number(0, 10000))
2019-02-10 17:27:47 +00:00
t.Log(p.String())
2019-01-31 10:39:17 +00:00
}
2019-02-08 22:32:58 +00:00
t.Error(p.String())
2019-01-31 10:39:17 +00:00
}
2019-02-01 11:18:48 +00:00
func BenchmarkPriorityQueue(b *testing.B) {
p := NewWithInt()
2019-02-13 02:46:30 +00:00
// for i := 0; i < 10000; i++ {
2019-02-01 11:18:48 +00:00
// p.Push(randomdata.Number(0, 100000))
// // p.Values()
// }
2019-02-13 02:46:30 +00:00
b.N = 100000
for i := 0; i < b.N; i++ {
2019-01-31 10:39:17 +00:00
p.Push(randomdata.Number(0, 100000))
}
}
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)
}
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()
2019-01-26 10:45:30 +00:00
}