2019-03-28 18:44:24 +00:00
|
|
|
package pqueue
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"474420502.top/eson/structure/compare"
|
|
|
|
)
|
|
|
|
|
2019-04-07 18:11:37 +00:00
|
|
|
func TestQueuePush(t *testing.T) {
|
|
|
|
pq := New(compare.Int)
|
|
|
|
for _, v := range []int{32, 10, 53, 78, 90, 1, 4} {
|
|
|
|
pq.Push(v)
|
|
|
|
if v, ok := pq.Top(); ok {
|
|
|
|
} else {
|
|
|
|
t.Error(v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if v, ok := pq.Top(); ok {
|
|
|
|
if v != 90 {
|
|
|
|
t.Error(v)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
t.Error(v)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestQueuePop(t *testing.T) {
|
2019-03-28 18:44:24 +00:00
|
|
|
pq := New(compare.Int)
|
2019-04-07 18:11:37 +00:00
|
|
|
for _, v := range []int{32, 10, 53, 78, 90, 1, 4} {
|
2019-03-28 18:44:24 +00:00
|
|
|
pq.Push(v)
|
2019-04-07 18:11:37 +00:00
|
|
|
if v, ok := pq.Top(); ok {
|
|
|
|
} else {
|
|
|
|
t.Error(v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
l := []int{90, 78, 53, 32, 10, 4, 1}
|
|
|
|
for _, lv := range l {
|
|
|
|
if v, ok := pq.Pop(); ok {
|
|
|
|
if v != lv {
|
|
|
|
t.Error(v)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
t.Error(v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if v, ok := pq.Pop(); ok {
|
2019-03-28 18:44:24 +00:00
|
|
|
t.Error(v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-07 18:11:37 +00:00
|
|
|
func TestQueueGet(t *testing.T) {
|
2019-03-28 18:44:24 +00:00
|
|
|
pq := New(compare.Int)
|
2019-04-07 18:11:37 +00:00
|
|
|
l := []int{32, 10, 53, 78, 90, 1, 4}
|
|
|
|
for _, v := range l {
|
2019-03-28 18:44:24 +00:00
|
|
|
pq.Push(v)
|
2019-04-07 18:11:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if v, ok := pq.Get(0); ok {
|
2019-03-28 18:44:24 +00:00
|
|
|
t.Error(v)
|
|
|
|
}
|
|
|
|
|
2019-04-07 18:11:37 +00:00
|
|
|
if v, ok := pq.Get(70); ok {
|
|
|
|
t.Error(v)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, v := range l {
|
|
|
|
if gv, ok := pq.Get(v); ok {
|
|
|
|
if gv != v {
|
|
|
|
t.Error("Get value is error, value is", gv)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestQueueIndex(t *testing.T) {
|
|
|
|
pq := New(compare.Int)
|
|
|
|
for _, v := range []int{32, 10, 53, 78, 90, 1, 4} {
|
|
|
|
pq.Push(v)
|
|
|
|
}
|
|
|
|
|
|
|
|
l := []int{90, 78, 53, 32, 10, 4, 1}
|
|
|
|
for i, lv := range l {
|
|
|
|
|
|
|
|
if v, ok := pq.Index(len(l) - i - 1); ok {
|
|
|
|
if v != l[len(l)-i-1] {
|
|
|
|
t.Error(v)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
t.Error(i, "index is not exist")
|
|
|
|
}
|
|
|
|
|
|
|
|
if v, ok := pq.Index(i); ok {
|
|
|
|
if v != lv {
|
|
|
|
t.Error(v)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
t.Error(i, "index is not exist")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if v, ok := pq.Index(-1); ok {
|
|
|
|
if v != 1 {
|
|
|
|
t.Error(v)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
t.Error("-1 index is not exist")
|
|
|
|
}
|
|
|
|
|
|
|
|
if v, ok := pq.Index(pq.Size()); ok {
|
|
|
|
t.Error("index is exits", pq.Size(), v)
|
|
|
|
}
|
|
|
|
|
|
|
|
if v, ok := pq.Index(pq.Size() - 1); !ok {
|
|
|
|
if v != 1 {
|
|
|
|
t.Error("the last value is 1 not is ", v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if v, ok := pq.Index(-10); ok {
|
|
|
|
t.Error("-10 index is exits", v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkQueueGet(b *testing.B) {
|
|
|
|
|
|
|
|
l := loadTestData()
|
|
|
|
|
|
|
|
pq := New(compare.Int)
|
|
|
|
for _, v := range l {
|
|
|
|
pq.Push(v)
|
|
|
|
}
|
|
|
|
|
|
|
|
execCount := 5
|
|
|
|
b.N = len(l) * execCount
|
|
|
|
|
|
|
|
b.ResetTimer()
|
|
|
|
b.StartTimer()
|
|
|
|
|
|
|
|
ALL:
|
|
|
|
for i := 0; i < execCount; i++ {
|
|
|
|
for _, v := range l {
|
|
|
|
if gv, ok := pq.Get(v); !ok {
|
|
|
|
b.Error(gv)
|
|
|
|
break ALL
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkQueueIndex(b *testing.B) {
|
|
|
|
|
|
|
|
l := loadTestData()
|
|
|
|
|
|
|
|
pq := New(compare.Int)
|
|
|
|
for _, v := range l {
|
|
|
|
pq.Push(v)
|
|
|
|
}
|
|
|
|
|
|
|
|
execCount := 2
|
|
|
|
b.N = len(l) * execCount
|
|
|
|
|
|
|
|
b.ResetTimer()
|
|
|
|
b.StartTimer()
|
|
|
|
|
|
|
|
ALL:
|
|
|
|
for i := 0; i < execCount; i++ {
|
|
|
|
for idx := range l {
|
|
|
|
if v, ok := pq.Index(idx); !ok {
|
|
|
|
b.Error(v)
|
|
|
|
break ALL
|
|
|
|
}
|
|
|
|
}
|
2019-03-28 18:44:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkPriorityPush(b *testing.B) {
|
|
|
|
|
|
|
|
l := loadTestData()
|
|
|
|
execCount := 5
|
|
|
|
b.N = len(l) * execCount
|
|
|
|
|
|
|
|
b.ResetTimer()
|
|
|
|
b.StartTimer()
|
|
|
|
|
|
|
|
for i := 0; i < execCount; i++ {
|
|
|
|
pq := New(compare.Int)
|
|
|
|
for _, v := range l {
|
|
|
|
pq.Push(v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkPriorityPop(b *testing.B) {
|
|
|
|
|
|
|
|
l := loadTestData()
|
|
|
|
|
|
|
|
pq := New(compare.Int)
|
|
|
|
for _, v := range l {
|
|
|
|
pq.Push(v)
|
|
|
|
}
|
|
|
|
|
2019-04-07 18:11:37 +00:00
|
|
|
b.N = len(l)
|
2019-03-28 18:44:24 +00:00
|
|
|
b.ResetTimer()
|
|
|
|
b.StartTimer()
|
|
|
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
pq.Pop()
|
|
|
|
}
|
|
|
|
}
|