focus/tree/heap/heap_test.go

291 lines
4.7 KiB
Go
Raw Permalink Normal View History

new file: .gitignore new file: LICENSE new file: README.md new file: compare/compare.go new file: for_test.go new file: heap/heap.go new file: heap/heap_test.go new file: interface.go new file: lastack/lastack.go new file: lastack/lastack_test.go new file: list/list.go new file: list/list_test.go new file: map/hashmap/hashmap.go new file: map/hashmap/hashmap_test.go new file: priority_list/iterator.go new file: priority_list/priority_list.go new file: priority_list/priority_list_test.go new file: priority_queue/iterator.go new file: priority_queue/priority_queue.go new file: priority_queue/priority_queue_test.go new file: priority_queue/vbt.go new file: priority_queue/vbt_test.go new file: priority_queuekey/iterator.go new file: priority_queuekey/priority_queuekey.go new file: priority_queuekey/priority_queuekey_test.go new file: priority_queuekey/vbt.go new file: priority_queuekey/vbt_test.go new file: set/hashset/hashset.go new file: set/hashset/hashset_test.go new file: set/treeset/treeset.go new file: set/treeset/treeset_test.go new file: sparse_array/array2/array2.go new file: sparse_array/array2/array2_test.go new file: sparse_array/array3/array3.go new file: sparse_array/array3/array3_test.go new file: sparse_array/arrayn/arrayn.go new file: sparse_array/arrayn/arrayn_test.go new file: stack/stack.go new file: stack/stack_test.go new file: tree/avl/avl.go new file: tree/avl/avl_test.go new file: tree/avl/iterator.go new file: tree/avldup/avldup.go new file: tree/avldup/avldup_test.go new file: tree/avldup/iterator.go new file: tree/avlkey/avlkey.go new file: tree/avlkey/avlkey_test.go new file: tree/avlkey/iterator.go new file: tree/avlkeydup/avlkeydup.go new file: tree/avlkeydup/avlkeydup_test.go new file: tree/avlkeydup/iterator.go new file: tree/vbt/iterator.go new file: tree/vbt/vbt.go new file: tree/vbt/vbt_test.go new file: tree/vbtkey/iterator.go new file: tree/vbtkey/vbtkey.go new file: tree/vbtkey/vbtkey_test.go
2019-05-08 02:42:51 +00:00
package heap
import (
2019-08-01 02:07:26 +00:00
"sort"
new file: .gitignore new file: LICENSE new file: README.md new file: compare/compare.go new file: for_test.go new file: heap/heap.go new file: heap/heap_test.go new file: interface.go new file: lastack/lastack.go new file: lastack/lastack_test.go new file: list/list.go new file: list/list_test.go new file: map/hashmap/hashmap.go new file: map/hashmap/hashmap_test.go new file: priority_list/iterator.go new file: priority_list/priority_list.go new file: priority_list/priority_list_test.go new file: priority_queue/iterator.go new file: priority_queue/priority_queue.go new file: priority_queue/priority_queue_test.go new file: priority_queue/vbt.go new file: priority_queue/vbt_test.go new file: priority_queuekey/iterator.go new file: priority_queuekey/priority_queuekey.go new file: priority_queuekey/priority_queuekey_test.go new file: priority_queuekey/vbt.go new file: priority_queuekey/vbt_test.go new file: set/hashset/hashset.go new file: set/hashset/hashset_test.go new file: set/treeset/treeset.go new file: set/treeset/treeset_test.go new file: sparse_array/array2/array2.go new file: sparse_array/array2/array2_test.go new file: sparse_array/array3/array3.go new file: sparse_array/array3/array3_test.go new file: sparse_array/arrayn/arrayn.go new file: sparse_array/arrayn/arrayn_test.go new file: stack/stack.go new file: stack/stack_test.go new file: tree/avl/avl.go new file: tree/avl/avl_test.go new file: tree/avl/iterator.go new file: tree/avldup/avldup.go new file: tree/avldup/avldup_test.go new file: tree/avldup/iterator.go new file: tree/avlkey/avlkey.go new file: tree/avlkey/avlkey_test.go new file: tree/avlkey/iterator.go new file: tree/avlkeydup/avlkeydup.go new file: tree/avlkeydup/avlkeydup_test.go new file: tree/avlkeydup/iterator.go new file: tree/vbt/iterator.go new file: tree/vbt/vbt.go new file: tree/vbt/vbt_test.go new file: tree/vbtkey/iterator.go new file: tree/vbtkey/vbtkey.go new file: tree/vbtkey/vbtkey_test.go
2019-05-08 02:42:51 +00:00
"testing"
2019-05-08 09:33:58 +00:00
"github.com/474420502/focus/compare"
2019-08-01 02:07:26 +00:00
"github.com/Pallinder/go-randomdata"
new file: .gitignore new file: LICENSE new file: README.md new file: compare/compare.go new file: for_test.go new file: heap/heap.go new file: heap/heap_test.go new file: interface.go new file: lastack/lastack.go new file: lastack/lastack_test.go new file: list/list.go new file: list/list_test.go new file: map/hashmap/hashmap.go new file: map/hashmap/hashmap_test.go new file: priority_list/iterator.go new file: priority_list/priority_list.go new file: priority_list/priority_list_test.go new file: priority_queue/iterator.go new file: priority_queue/priority_queue.go new file: priority_queue/priority_queue_test.go new file: priority_queue/vbt.go new file: priority_queue/vbt_test.go new file: priority_queuekey/iterator.go new file: priority_queuekey/priority_queuekey.go new file: priority_queuekey/priority_queuekey_test.go new file: priority_queuekey/vbt.go new file: priority_queuekey/vbt_test.go new file: set/hashset/hashset.go new file: set/hashset/hashset_test.go new file: set/treeset/treeset.go new file: set/treeset/treeset_test.go new file: sparse_array/array2/array2.go new file: sparse_array/array2/array2_test.go new file: sparse_array/array3/array3.go new file: sparse_array/array3/array3_test.go new file: sparse_array/arrayn/arrayn.go new file: sparse_array/arrayn/arrayn_test.go new file: stack/stack.go new file: stack/stack_test.go new file: tree/avl/avl.go new file: tree/avl/avl_test.go new file: tree/avl/iterator.go new file: tree/avldup/avldup.go new file: tree/avldup/avldup_test.go new file: tree/avldup/iterator.go new file: tree/avlkey/avlkey.go new file: tree/avlkey/avlkey_test.go new file: tree/avlkey/iterator.go new file: tree/avlkeydup/avlkeydup.go new file: tree/avlkeydup/avlkeydup_test.go new file: tree/avlkeydup/iterator.go new file: tree/vbt/iterator.go new file: tree/vbt/vbt.go new file: tree/vbt/vbt_test.go new file: tree/vbtkey/iterator.go new file: tree/vbtkey/vbtkey.go new file: tree/vbtkey/vbtkey_test.go
2019-05-08 02:42:51 +00:00
)
2019-08-01 02:07:26 +00:00
func TestHeapGrowSlimming(t *testing.T) {
2019-08-01 09:07:28 +00:00
for ii := 0; ii < 1000; ii++ {
h := New(compare.Int)
var results []int
for i := 0; i < 100; i++ {
v := randomdata.Number(0, 100)
results = append(results, v)
h.Put(v)
2019-08-01 02:07:26 +00:00
}
2019-08-01 09:07:28 +00:00
sort.Slice(results, func(i, j int) bool {
if results[i] > results[j] {
return true
}
return false
})
2019-08-01 02:07:26 +00:00
2019-08-01 09:07:28 +00:00
if h.Size() != 100 || h.Empty() {
t.Error("size != 100")
}
2019-08-01 02:07:26 +00:00
2019-08-01 09:07:28 +00:00
for i := 0; !h.Empty(); i++ {
v, _ := h.Pop()
if results[i] != v {
t.Error("heap is error")
}
2019-08-01 02:07:26 +00:00
}
2019-08-01 09:07:28 +00:00
if h.Size() != 0 {
t.Error("size != 0")
}
2019-08-01 02:07:26 +00:00
2019-08-01 09:07:28 +00:00
h.Put(1)
h.Put(5)
h.Put(2)
2019-08-01 02:07:26 +00:00
2019-08-01 09:07:28 +00:00
if h.Values()[0] != 5 {
t.Error("top is not equal to 5")
}
2019-08-01 02:07:26 +00:00
2019-08-01 09:07:28 +00:00
h.Clear()
h.Reborn()
2019-08-01 02:07:26 +00:00
2019-08-01 09:07:28 +00:00
if !h.Empty() {
t.Error("clear reborn is error")
}
2019-08-01 02:07:26 +00:00
}
}
2019-05-08 09:33:58 +00:00
func TestHeapPushTopPop(t *testing.T) {
h := New(compare.Int)
l := []int{9, 5, 15, 2, 3}
ol := []int{15, 9, 5, 3, 2}
for _, v := range l {
2019-07-28 18:18:22 +00:00
h.Put(v)
new file: .gitignore new file: LICENSE new file: README.md new file: compare/compare.go new file: for_test.go new file: heap/heap.go new file: heap/heap_test.go new file: interface.go new file: lastack/lastack.go new file: lastack/lastack_test.go new file: list/list.go new file: list/list_test.go new file: map/hashmap/hashmap.go new file: map/hashmap/hashmap_test.go new file: priority_list/iterator.go new file: priority_list/priority_list.go new file: priority_list/priority_list_test.go new file: priority_queue/iterator.go new file: priority_queue/priority_queue.go new file: priority_queue/priority_queue_test.go new file: priority_queue/vbt.go new file: priority_queue/vbt_test.go new file: priority_queuekey/iterator.go new file: priority_queuekey/priority_queuekey.go new file: priority_queuekey/priority_queuekey_test.go new file: priority_queuekey/vbt.go new file: priority_queuekey/vbt_test.go new file: set/hashset/hashset.go new file: set/hashset/hashset_test.go new file: set/treeset/treeset.go new file: set/treeset/treeset_test.go new file: sparse_array/array2/array2.go new file: sparse_array/array2/array2_test.go new file: sparse_array/array3/array3.go new file: sparse_array/array3/array3_test.go new file: sparse_array/arrayn/arrayn.go new file: sparse_array/arrayn/arrayn_test.go new file: stack/stack.go new file: stack/stack_test.go new file: tree/avl/avl.go new file: tree/avl/avl_test.go new file: tree/avl/iterator.go new file: tree/avldup/avldup.go new file: tree/avldup/avldup_test.go new file: tree/avldup/iterator.go new file: tree/avlkey/avlkey.go new file: tree/avlkey/avlkey_test.go new file: tree/avlkey/iterator.go new file: tree/avlkeydup/avlkeydup.go new file: tree/avlkeydup/avlkeydup_test.go new file: tree/avlkeydup/iterator.go new file: tree/vbt/iterator.go new file: tree/vbt/vbt.go new file: tree/vbt/vbt_test.go new file: tree/vbtkey/iterator.go new file: tree/vbtkey/vbtkey.go new file: tree/vbtkey/vbtkey_test.go
2019-05-08 02:42:51 +00:00
}
2019-05-08 09:33:58 +00:00
for _, tv := range ol {
if v, isfound := h.Top(); isfound {
if !(isfound && v == tv) {
t.Error(v)
new file: .gitignore new file: LICENSE new file: README.md new file: compare/compare.go new file: for_test.go new file: heap/heap.go new file: heap/heap_test.go new file: interface.go new file: lastack/lastack.go new file: lastack/lastack_test.go new file: list/list.go new file: list/list_test.go new file: map/hashmap/hashmap.go new file: map/hashmap/hashmap_test.go new file: priority_list/iterator.go new file: priority_list/priority_list.go new file: priority_list/priority_list_test.go new file: priority_queue/iterator.go new file: priority_queue/priority_queue.go new file: priority_queue/priority_queue_test.go new file: priority_queue/vbt.go new file: priority_queue/vbt_test.go new file: priority_queuekey/iterator.go new file: priority_queuekey/priority_queuekey.go new file: priority_queuekey/priority_queuekey_test.go new file: priority_queuekey/vbt.go new file: priority_queuekey/vbt_test.go new file: set/hashset/hashset.go new file: set/hashset/hashset_test.go new file: set/treeset/treeset.go new file: set/treeset/treeset_test.go new file: sparse_array/array2/array2.go new file: sparse_array/array2/array2_test.go new file: sparse_array/array3/array3.go new file: sparse_array/array3/array3_test.go new file: sparse_array/arrayn/arrayn.go new file: sparse_array/arrayn/arrayn_test.go new file: stack/stack.go new file: stack/stack_test.go new file: tree/avl/avl.go new file: tree/avl/avl_test.go new file: tree/avl/iterator.go new file: tree/avldup/avldup.go new file: tree/avldup/avldup_test.go new file: tree/avldup/iterator.go new file: tree/avlkey/avlkey.go new file: tree/avlkey/avlkey_test.go new file: tree/avlkey/iterator.go new file: tree/avlkeydup/avlkeydup.go new file: tree/avlkeydup/avlkeydup_test.go new file: tree/avlkeydup/iterator.go new file: tree/vbt/iterator.go new file: tree/vbt/vbt.go new file: tree/vbt/vbt_test.go new file: tree/vbtkey/iterator.go new file: tree/vbtkey/vbtkey.go new file: tree/vbtkey/vbtkey_test.go
2019-05-08 02:42:51 +00:00
}
}
2019-05-08 09:33:58 +00:00
if v, isfound := h.Pop(); isfound {
if !(isfound && v == tv) {
t.Error(v)
}
new file: .gitignore new file: LICENSE new file: README.md new file: compare/compare.go new file: for_test.go new file: heap/heap.go new file: heap/heap_test.go new file: interface.go new file: lastack/lastack.go new file: lastack/lastack_test.go new file: list/list.go new file: list/list_test.go new file: map/hashmap/hashmap.go new file: map/hashmap/hashmap_test.go new file: priority_list/iterator.go new file: priority_list/priority_list.go new file: priority_list/priority_list_test.go new file: priority_queue/iterator.go new file: priority_queue/priority_queue.go new file: priority_queue/priority_queue_test.go new file: priority_queue/vbt.go new file: priority_queue/vbt_test.go new file: priority_queuekey/iterator.go new file: priority_queuekey/priority_queuekey.go new file: priority_queuekey/priority_queuekey_test.go new file: priority_queuekey/vbt.go new file: priority_queuekey/vbt_test.go new file: set/hashset/hashset.go new file: set/hashset/hashset_test.go new file: set/treeset/treeset.go new file: set/treeset/treeset_test.go new file: sparse_array/array2/array2.go new file: sparse_array/array2/array2_test.go new file: sparse_array/array3/array3.go new file: sparse_array/array3/array3_test.go new file: sparse_array/arrayn/arrayn.go new file: sparse_array/arrayn/arrayn_test.go new file: stack/stack.go new file: stack/stack_test.go new file: tree/avl/avl.go new file: tree/avl/avl_test.go new file: tree/avl/iterator.go new file: tree/avldup/avldup.go new file: tree/avldup/avldup_test.go new file: tree/avldup/iterator.go new file: tree/avlkey/avlkey.go new file: tree/avlkey/avlkey_test.go new file: tree/avlkey/iterator.go new file: tree/avlkeydup/avlkeydup.go new file: tree/avlkeydup/avlkeydup_test.go new file: tree/avlkeydup/iterator.go new file: tree/vbt/iterator.go new file: tree/vbt/vbt.go new file: tree/vbt/vbt_test.go new file: tree/vbtkey/iterator.go new file: tree/vbtkey/vbtkey.go new file: tree/vbtkey/vbtkey_test.go
2019-05-08 02:42:51 +00:00
}
}
2019-05-08 09:33:58 +00:00
if h.Size() != 0 {
t.Error("heap size is not equals to zero")
new file: .gitignore new file: LICENSE new file: README.md new file: compare/compare.go new file: for_test.go new file: heap/heap.go new file: heap/heap_test.go new file: interface.go new file: lastack/lastack.go new file: lastack/lastack_test.go new file: list/list.go new file: list/list_test.go new file: map/hashmap/hashmap.go new file: map/hashmap/hashmap_test.go new file: priority_list/iterator.go new file: priority_list/priority_list.go new file: priority_list/priority_list_test.go new file: priority_queue/iterator.go new file: priority_queue/priority_queue.go new file: priority_queue/priority_queue_test.go new file: priority_queue/vbt.go new file: priority_queue/vbt_test.go new file: priority_queuekey/iterator.go new file: priority_queuekey/priority_queuekey.go new file: priority_queuekey/priority_queuekey_test.go new file: priority_queuekey/vbt.go new file: priority_queuekey/vbt_test.go new file: set/hashset/hashset.go new file: set/hashset/hashset_test.go new file: set/treeset/treeset.go new file: set/treeset/treeset_test.go new file: sparse_array/array2/array2.go new file: sparse_array/array2/array2_test.go new file: sparse_array/array3/array3.go new file: sparse_array/array3/array3_test.go new file: sparse_array/arrayn/arrayn.go new file: sparse_array/arrayn/arrayn_test.go new file: stack/stack.go new file: stack/stack_test.go new file: tree/avl/avl.go new file: tree/avl/avl_test.go new file: tree/avl/iterator.go new file: tree/avldup/avldup.go new file: tree/avldup/avldup_test.go new file: tree/avldup/iterator.go new file: tree/avlkey/avlkey.go new file: tree/avlkey/avlkey_test.go new file: tree/avlkey/iterator.go new file: tree/avlkeydup/avlkeydup.go new file: tree/avlkeydup/avlkeydup_test.go new file: tree/avlkeydup/iterator.go new file: tree/vbt/iterator.go new file: tree/vbt/vbt.go new file: tree/vbt/vbt_test.go new file: tree/vbtkey/iterator.go new file: tree/vbtkey/vbtkey.go new file: tree/vbtkey/vbtkey_test.go
2019-05-08 02:42:51 +00:00
}
2019-08-01 03:21:17 +00:00
h.Clear()
l = []int{3, 5, 2, 7, 1}
for _, v := range l {
h.Put(v)
}
sort.Slice(l, func(i, j int) bool {
if l[i] > l[j] {
return true
}
return false
})
for i := 0; !h.Empty(); i++ {
v, _ := h.Pop()
if l[i] != v {
t.Error("heap is error")
}
}
new file: .gitignore new file: LICENSE new file: README.md new file: compare/compare.go new file: for_test.go new file: heap/heap.go new file: heap/heap_test.go new file: interface.go new file: lastack/lastack.go new file: lastack/lastack_test.go new file: list/list.go new file: list/list_test.go new file: map/hashmap/hashmap.go new file: map/hashmap/hashmap_test.go new file: priority_list/iterator.go new file: priority_list/priority_list.go new file: priority_list/priority_list_test.go new file: priority_queue/iterator.go new file: priority_queue/priority_queue.go new file: priority_queue/priority_queue_test.go new file: priority_queue/vbt.go new file: priority_queue/vbt_test.go new file: priority_queuekey/iterator.go new file: priority_queuekey/priority_queuekey.go new file: priority_queuekey/priority_queuekey_test.go new file: priority_queuekey/vbt.go new file: priority_queuekey/vbt_test.go new file: set/hashset/hashset.go new file: set/hashset/hashset_test.go new file: set/treeset/treeset.go new file: set/treeset/treeset_test.go new file: sparse_array/array2/array2.go new file: sparse_array/array2/array2_test.go new file: sparse_array/array3/array3.go new file: sparse_array/array3/array3_test.go new file: sparse_array/arrayn/arrayn.go new file: sparse_array/arrayn/arrayn_test.go new file: stack/stack.go new file: stack/stack_test.go new file: tree/avl/avl.go new file: tree/avl/avl_test.go new file: tree/avl/iterator.go new file: tree/avldup/avldup.go new file: tree/avldup/avldup_test.go new file: tree/avldup/iterator.go new file: tree/avlkey/avlkey.go new file: tree/avlkey/avlkey_test.go new file: tree/avlkey/iterator.go new file: tree/avlkeydup/avlkeydup.go new file: tree/avlkeydup/avlkeydup_test.go new file: tree/avlkeydup/iterator.go new file: tree/vbt/iterator.go new file: tree/vbt/vbt.go new file: tree/vbt/vbt_test.go new file: tree/vbtkey/iterator.go new file: tree/vbtkey/vbtkey.go new file: tree/vbtkey/vbtkey_test.go
2019-05-08 02:42:51 +00:00
}
2019-08-01 09:07:28 +00:00
// func BenchmarkPush(b *testing.B) {
// h := New(compare.Int)
// b.N = 40000000
// var results []int
// for i := 0; i < b.N; i++ {
// results = append(results, randomdata.Number(0, 1000000000))
// }
// b.ResetTimer()
// for _, v := range results {
// h.Put(v)
// }
// }
2019-05-08 09:33:58 +00:00
// 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) {
// for i := 0; i < 200000; i++ {
// h := New(Int)
// // m := make(map[int]int)
// gods := binaryheap.NewWithIntComparator()
// for c := 0; c < 40; c++ {
// 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) {
// l := loadTestData()
// b.ResetTimer()
// execCount := 50
// b.N = len(l) * execCount
// for c := 0; c < execCount; c++ {
// b.StopTimer()
// h := New(Int)
// b.StartTimer()
// for _, v := range l {
// h.Push(v)
// }
// }
// }
// func BenchmarkPop(b *testing.B) {
// h := New(Int)
// l := loadTestData()
// b.ResetTimer()
// execCount := 20
// 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()
// execCount := 20
// 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 BenchmarkGodsPush(b *testing.B) {
// l := loadTestData()
// 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)
// }
// }
// }
// 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
// }