2019-04-15 14:57:44 +00:00
|
|
|
package heap
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/gob"
|
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/Pallinder/go-randomdata"
|
|
|
|
|
|
|
|
"github.com/davecgh/go-spew/spew"
|
|
|
|
|
|
|
|
"github.com/emirpasic/gods/trees/binaryheap"
|
|
|
|
)
|
|
|
|
|
|
|
|
func Int(k1, k2 interface{}) int {
|
|
|
|
c1 := k1.(int)
|
|
|
|
c2 := k2.(int)
|
|
|
|
switch {
|
|
|
|
case c1 > c2:
|
|
|
|
return -1
|
|
|
|
case c1 < c2:
|
|
|
|
return 1
|
|
|
|
default:
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPush(t *testing.T) {
|
|
|
|
|
|
|
|
for i := 0; i < 1000000; i++ {
|
|
|
|
h := New(Int)
|
|
|
|
|
|
|
|
gods := binaryheap.NewWithIntComparator()
|
|
|
|
for c := 0; c < 20; c++ {
|
|
|
|
v := randomdata.Number(0, 100)
|
|
|
|
h.Push(v)
|
|
|
|
gods.Push(v)
|
|
|
|
}
|
|
|
|
|
|
|
|
r1 := spew.Sprint(h.Values())
|
|
|
|
r2 := spew.Sprint(gods.Values())
|
|
|
|
if r1 != r2 {
|
|
|
|
t.Error(r1)
|
|
|
|
t.Error(r2)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPop(t *testing.T) {
|
|
|
|
|
2019-04-15 23:09:32 +00:00
|
|
|
for i := 0; i < 200000; i++ {
|
2019-04-15 14:57:44 +00:00
|
|
|
h := New(Int)
|
|
|
|
|
|
|
|
// m := make(map[int]int)
|
|
|
|
gods := binaryheap.NewWithIntComparator()
|
2019-04-15 23:09:32 +00:00
|
|
|
for c := 0; c < 40; c++ {
|
2019-04-15 14:57:44 +00:00
|
|
|
v := randomdata.Number(0, 100)
|
|
|
|
// if _, ok := m[v]; !ok {
|
|
|
|
h.Push(v)
|
|
|
|
gods.Push(v)
|
|
|
|
// m[v] = v
|
|
|
|
// }
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// t.Error(h.Values())
|
|
|
|
// t.Error(gods.Values())
|
|
|
|
for c := 0; c < randomdata.Number(5, 10); c++ {
|
|
|
|
v1, _ := h.Pop()
|
|
|
|
v2, _ := gods.Pop()
|
|
|
|
|
|
|
|
if v1 != v2 {
|
|
|
|
t.Error(h.Values(), v1)
|
|
|
|
t.Error(gods.Values(), v2)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
r1 := spew.Sprint(h.Values())
|
|
|
|
r2 := spew.Sprint(gods.Values())
|
|
|
|
if r1 != r2 {
|
|
|
|
t.Error(r1)
|
|
|
|
t.Error(r2)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkPush(b *testing.B) {
|
|
|
|
|
2019-04-15 23:09:32 +00:00
|
|
|
l := loadTestData()
|
2019-04-15 14:57:44 +00:00
|
|
|
|
2019-04-15 23:09:32 +00:00
|
|
|
b.ResetTimer()
|
|
|
|
execCount := 50
|
|
|
|
b.N = len(l) * execCount
|
|
|
|
|
|
|
|
for c := 0; c < execCount; c++ {
|
|
|
|
b.StopTimer()
|
2019-04-15 14:57:44 +00:00
|
|
|
h := New(Int)
|
2019-04-15 23:09:32 +00:00
|
|
|
b.StartTimer()
|
|
|
|
for _, v := range l {
|
|
|
|
h.Push(v)
|
2019-04-15 14:57:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkPop(b *testing.B) {
|
|
|
|
|
|
|
|
h := New(Int)
|
|
|
|
|
|
|
|
l := loadTestData()
|
|
|
|
|
|
|
|
b.ResetTimer()
|
2019-04-15 23:09:32 +00:00
|
|
|
execCount := 20
|
2019-04-15 14:57:44 +00:00
|
|
|
b.N = len(l) * execCount
|
|
|
|
|
|
|
|
for c := 0; c < execCount; c++ {
|
|
|
|
b.StopTimer()
|
|
|
|
for _, v := range l {
|
|
|
|
h.Push(v)
|
|
|
|
}
|
|
|
|
b.StartTimer()
|
|
|
|
for h.size != 0 {
|
|
|
|
h.Pop()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkGodsPop(b *testing.B) {
|
|
|
|
|
|
|
|
h := binaryheap.NewWithIntComparator()
|
|
|
|
|
|
|
|
l := loadTestData()
|
|
|
|
|
|
|
|
b.ResetTimer()
|
2019-04-15 23:09:32 +00:00
|
|
|
execCount := 20
|
2019-04-15 14:57:44 +00:00
|
|
|
b.N = len(l) * execCount
|
|
|
|
|
|
|
|
for c := 0; c < execCount; c++ {
|
|
|
|
b.StopTimer()
|
|
|
|
for _, v := range l {
|
|
|
|
h.Push(v)
|
|
|
|
}
|
|
|
|
b.StartTimer()
|
|
|
|
for h.Size() != 0 {
|
|
|
|
h.Pop()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-04-15 23:09:32 +00:00
|
|
|
func BenchmarkGodsPush(b *testing.B) {
|
|
|
|
l := loadTestData()
|
2019-04-15 14:57:44 +00:00
|
|
|
|
2019-04-15 23:09:32 +00:00
|
|
|
b.ResetTimer()
|
|
|
|
execCount := 50
|
|
|
|
b.N = len(l) * execCount
|
|
|
|
|
|
|
|
for c := 0; c < execCount; c++ {
|
|
|
|
b.StopTimer()
|
|
|
|
h := binaryheap.NewWith(Int)
|
|
|
|
b.StartTimer()
|
|
|
|
for _, v := range l {
|
|
|
|
h.Push(v)
|
2019-04-15 14:57:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func loadTestData() []int {
|
|
|
|
data, err := ioutil.ReadFile("../l.log")
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
}
|
|
|
|
var l []int
|
|
|
|
decoder := gob.NewDecoder(bytes.NewReader(data))
|
|
|
|
decoder.Decode(&l)
|
|
|
|
return l
|
|
|
|
}
|