添加heap单元测试
This commit is contained in:
parent
c0ad187222
commit
f63759645e
@ -130,12 +130,9 @@ func (h *Heap) Pop() (interface{}, bool) {
|
|||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
cidx = c1
|
cidx = c1
|
||||||
if c1 < h.size {
|
if c1 >= h.size {
|
||||||
cvalue1 = h.elements[c1]
|
|
||||||
} else {
|
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if h.Compare(h.elements[cidx], downvalue) > 0 {
|
if h.Compare(h.elements[cidx], downvalue) > 0 {
|
||||||
|
@ -80,6 +80,28 @@ func TestHeapPushTopPop(t *testing.T) {
|
|||||||
if h.Size() != 0 {
|
if h.Size() != 0 {
|
||||||
t.Error("heap size is not equals to zero")
|
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 {
|
// func Int(k1, k2 interface{}) int {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user