单元测试通过

This commit is contained in:
eson 2019-07-22 03:28:27 +08:00
parent 05bc567249
commit 60c1442dca
4 changed files with 128 additions and 18 deletions

View File

@ -265,12 +265,67 @@ func TestPriorityQueue_Iterator(t *testing.T) {
t.Error(values[i], " != ", iter.Value()) t.Error(values[i], " != ", iter.Value())
} }
if !iter.Next() { if !iter.Prev() {
break break
} }
} }
} }
func TestPriorityQueue_Iterator2(t *testing.T) {
pq := New(compare.Int)
for i := 0; i < 5; i++ {
pq.Push(i)
}
iter := pq.Iterator()
iter.ToHead()
n, _ := pq.IndexNode(0)
if n.value != 4 {
t.Error(n)
}
if v, _ := pq.Top(); v != 4 {
t.Error("Top != 4, and is ", v)
}
if v := iter.GetNext(n, 2).value; v != 2 {
t.Error("iter.GetNext(n, 2) != 2, and is ", v)
}
pq = New(compare.Int)
for i := 100; i >= 0; i-- {
pq.Push(i)
}
if v, _ := pq.Top(); v != 100 {
t.Error("Top != 100, and is ", v)
}
for pq.Size() >= 50 {
pq.Pop()
}
if v, _ := pq.Top(); v != 48 {
t.Error("Top != 48, and is ", v)
}
pq = New(compare.Int)
for i := 0; i < 100; i++ {
pq.Push(i)
}
if v, _ := pq.Top(); v != 99 {
t.Error("Top != 99, and is ", v)
}
for pq.Size() >= 50 {
pq.Pop()
}
if v, _ := pq.Top(); v != 48 {
t.Error("Top != 49, and is ", v)
}
}
// func BenchmarkQueueGet(b *testing.B) { // func BenchmarkQueueGet(b *testing.B) {
// l := loadTestData() // l := loadTestData()

View File

@ -118,7 +118,7 @@ func (tree *vbTree) IndexRange(idx1, idx2 int) (result []interface{}, ok bool) {
iter := tree.iter iter := tree.iter
result = make([]interface{}, 0, idx1-idx2) result = make([]interface{}, 0, idx1-idx2)
for i := idx2; i <= idx1; i++ { for i := idx2; i <= idx1; i++ {
if iter.Next() { if iter.Prev() {
result = append(result, iter.Value()) result = append(result, iter.Value())
} else { } else {
ok = false ok = false
@ -140,7 +140,7 @@ func (tree *vbTree) IndexRange(idx1, idx2 int) (result []interface{}, ok bool) {
iter := tree.iter iter := tree.iter
result = make([]interface{}, 0, idx2-idx1) result = make([]interface{}, 0, idx2-idx1)
for i := idx1; i <= idx2; i++ { for i := idx1; i <= idx2; i++ {
if iter.Prev() { if iter.Next() {
result = append(result, iter.Value()) result = append(result, iter.Value())
} else { } else {
ok = false ok = false
@ -278,7 +278,7 @@ func (tree *vbTree) GetRange(k1, k2 interface{}) (result []interface{}) {
// iter := NewIterator(min) // iter := NewIterator(min)
tree.iter.SetNode(min) tree.iter.SetNode(min)
iter := tree.iter iter := tree.iter
for iter.Next() { for iter.Prev() {
result = append(result, iter.Value()) result = append(result, iter.Value())
if iter.cur == max { if iter.cur == max {
break break
@ -305,7 +305,7 @@ func (tree *vbTree) GetRange(k1, k2 interface{}) (result []interface{}) {
// iter := NewIterator(max) // iter := NewIterator(max)
tree.iter.SetNode(max) tree.iter.SetNode(max)
iter := tree.iter iter := tree.iter
for iter.Prev() { for iter.Next() {
result = append(result, iter.Value()) result = append(result, iter.Value())
if iter.cur == min { if iter.cur == min {
break break
@ -358,8 +358,8 @@ func (tree *vbTree) getArounNode(key interface{}) (result [3]*Node) {
tree.iter.SetNode(n) tree.iter.SetNode(n)
iter := tree.iter iter := tree.iter
iter.Prev() iter.Next()
for iter.Prev() { for iter.Next() {
if tree.Compare(iter.cur.value, n.value) == 0 { if tree.Compare(iter.cur.value, n.value) == 0 {
n = iter.cur n = iter.cur
} else { } else {
@ -378,21 +378,21 @@ func (tree *vbTree) getArounNode(key interface{}) (result [3]*Node) {
if result[1] != nil { if result[1] != nil {
result[0] = tree.iter.GetPrev(result[1], 1) result[0] = tree.iter.GetNext(result[1], 1)
result[2] = tree.iter.GetNext(result[1], 1) result[2] = tree.iter.GetPrev(result[1], 1)
} else { } else {
result[0] = last result[0] = last
result[2] = tree.iter.GetNext(last, 1) result[2] = tree.iter.GetPrev(last, 1)
} }
case -1: case -1:
if result[1] != nil { if result[1] != nil {
result[0] = tree.iter.GetPrev(result[1], 1) result[0] = tree.iter.GetNext(result[1], 1)
result[2] = tree.iter.GetNext(result[1], 1) result[2] = tree.iter.GetPrev(result[1], 1)
} else { } else {
result[2] = last result[2] = last
result[0] = tree.iter.GetPrev(last, 1) result[0] = tree.iter.GetNext(last, 1)
} }
case 0: case 0:
@ -400,8 +400,8 @@ func (tree *vbTree) getArounNode(key interface{}) (result [3]*Node) {
if result[1] == nil { if result[1] == nil {
return return
} }
result[0] = tree.iter.GetPrev(result[1], 1) result[0] = tree.iter.GetNext(result[1], 1)
result[2] = tree.iter.GetNext(result[1], 1) result[2] = tree.iter.GetPrev(result[1], 1)
} }
return return
} }
@ -418,8 +418,8 @@ func (tree *vbTree) GetNode(value interface{}) (*Node, bool) {
tree.iter.SetNode(n) tree.iter.SetNode(n)
iter := tree.iter iter := tree.iter
iter.Prev() iter.Next()
for iter.Prev() { for iter.Next() {
if tree.Compare(iter.cur.value, n.value) == 0 { if tree.Compare(iter.cur.value, n.value) == 0 {
n = iter.cur n = iter.cur
} else { } else {

View File

@ -375,6 +375,61 @@ func TestPriorityQueue_Iterator(t *testing.T) {
} }
} }
func TestPriorityQueue_Iterator2(t *testing.T) {
pq := New(compare.Int)
for i := 0; i < 5; i++ {
pq.Push(i, i)
}
iter := pq.Iterator()
iter.ToHead()
n, _ := pq.IndexNode(0)
if n.value != 4 {
t.Error(n)
}
if v, _ := pq.Top(); v != 4 {
t.Error("Top != 4, and is ", v)
}
if v := iter.GetNext(n, 2).value; v != 2 {
t.Error("iter.GetNext(n, 2) != 2, and is ", v)
}
pq = New(compare.Int)
for i := 100; i >= 0; i-- {
pq.Push(i, i)
}
if v, _ := pq.Top(); v != 100 {
t.Error("Top != 100, and is ", v)
}
for pq.Size() >= 50 {
pq.Pop()
}
if v, _ := pq.Top(); v != 48 {
t.Error("Top != 48, and is ", v)
}
pq = New(compare.Int)
for i := 0; i < 100; i++ {
pq.Push(i, i)
}
if v, _ := pq.Top(); v != 99 {
t.Error("Top != 99, and is ", v)
}
for pq.Size() >= 50 {
pq.Pop()
}
if v, _ := pq.Top(); v != 48 {
t.Error("Top != 49, and is ", v)
}
}
func TestMain(t *testing.T) { func TestMain(t *testing.T) {
pq := New(compare.Int) pq := New(compare.Int)
pq.Push(1, 1) pq.Push(1, 1)

View File

@ -175,7 +175,7 @@ func (tree *vbTree) removeNode(n *Node) {
} }
if tree.top == n { if tree.top == n {
tree.top = tree.iter.GetNext(n, 1) tree.top = tree.iter.GetNext(n, 1) // 最大值 Next一位后为下个最大值
} }
ls, rs := getChildrenSize(n) ls, rs := getChildrenSize(n)