添加heap单元测试

This commit is contained in:
huangsimin 2019-08-01 11:21:17 +08:00
parent c0ad187222
commit f63759645e
2 changed files with 23 additions and 4 deletions

View File

@ -130,12 +130,9 @@ func (h *Heap) Pop() (interface{}, bool) {
}
} else {
cidx = c1
if c1 < h.size {
cvalue1 = h.elements[c1]
} else {
if c1 >= h.size {
break
}
}
if h.Compare(h.elements[cidx], downvalue) > 0 {

View File

@ -80,6 +80,28 @@ func TestHeapPushTopPop(t *testing.T) {
if h.Size() != 0 {
t.Error("heap size is not equals to zero")
}
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")
}
}
}
// func Int(k1, k2 interface{}) int {