单元测试通过
This commit is contained in:
parent
05bc567249
commit
60c1442dca
|
@ -265,12 +265,67 @@ func TestPriorityQueue_Iterator(t *testing.T) {
|
|||
t.Error(values[i], " != ", iter.Value())
|
||||
}
|
||||
|
||||
if !iter.Next() {
|
||||
if !iter.Prev() {
|
||||
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) {
|
||||
|
||||
// l := loadTestData()
|
||||
|
|
|
@ -118,7 +118,7 @@ func (tree *vbTree) IndexRange(idx1, idx2 int) (result []interface{}, ok bool) {
|
|||
iter := tree.iter
|
||||
result = make([]interface{}, 0, idx1-idx2)
|
||||
for i := idx2; i <= idx1; i++ {
|
||||
if iter.Next() {
|
||||
if iter.Prev() {
|
||||
result = append(result, iter.Value())
|
||||
} else {
|
||||
ok = false
|
||||
|
@ -140,7 +140,7 @@ func (tree *vbTree) IndexRange(idx1, idx2 int) (result []interface{}, ok bool) {
|
|||
iter := tree.iter
|
||||
result = make([]interface{}, 0, idx2-idx1)
|
||||
for i := idx1; i <= idx2; i++ {
|
||||
if iter.Prev() {
|
||||
if iter.Next() {
|
||||
result = append(result, iter.Value())
|
||||
} else {
|
||||
ok = false
|
||||
|
@ -278,7 +278,7 @@ func (tree *vbTree) GetRange(k1, k2 interface{}) (result []interface{}) {
|
|||
// iter := NewIterator(min)
|
||||
tree.iter.SetNode(min)
|
||||
iter := tree.iter
|
||||
for iter.Next() {
|
||||
for iter.Prev() {
|
||||
result = append(result, iter.Value())
|
||||
if iter.cur == max {
|
||||
break
|
||||
|
@ -305,7 +305,7 @@ func (tree *vbTree) GetRange(k1, k2 interface{}) (result []interface{}) {
|
|||
// iter := NewIterator(max)
|
||||
tree.iter.SetNode(max)
|
||||
iter := tree.iter
|
||||
for iter.Prev() {
|
||||
for iter.Next() {
|
||||
result = append(result, iter.Value())
|
||||
if iter.cur == min {
|
||||
break
|
||||
|
@ -358,8 +358,8 @@ func (tree *vbTree) getArounNode(key interface{}) (result [3]*Node) {
|
|||
|
||||
tree.iter.SetNode(n)
|
||||
iter := tree.iter
|
||||
iter.Prev()
|
||||
for iter.Prev() {
|
||||
iter.Next()
|
||||
for iter.Next() {
|
||||
if tree.Compare(iter.cur.value, n.value) == 0 {
|
||||
n = iter.cur
|
||||
} else {
|
||||
|
@ -378,21 +378,21 @@ func (tree *vbTree) getArounNode(key interface{}) (result [3]*Node) {
|
|||
|
||||
if result[1] != nil {
|
||||
|
||||
result[0] = tree.iter.GetPrev(result[1], 1)
|
||||
result[2] = tree.iter.GetNext(result[1], 1)
|
||||
result[0] = tree.iter.GetNext(result[1], 1)
|
||||
result[2] = tree.iter.GetPrev(result[1], 1)
|
||||
} else {
|
||||
result[0] = last
|
||||
result[2] = tree.iter.GetNext(last, 1)
|
||||
result[2] = tree.iter.GetPrev(last, 1)
|
||||
}
|
||||
|
||||
case -1:
|
||||
|
||||
if result[1] != nil {
|
||||
result[0] = tree.iter.GetPrev(result[1], 1)
|
||||
result[2] = tree.iter.GetNext(result[1], 1)
|
||||
result[0] = tree.iter.GetNext(result[1], 1)
|
||||
result[2] = tree.iter.GetPrev(result[1], 1)
|
||||
} else {
|
||||
result[2] = last
|
||||
result[0] = tree.iter.GetPrev(last, 1)
|
||||
result[0] = tree.iter.GetNext(last, 1)
|
||||
}
|
||||
|
||||
case 0:
|
||||
|
@ -400,8 +400,8 @@ func (tree *vbTree) getArounNode(key interface{}) (result [3]*Node) {
|
|||
if result[1] == nil {
|
||||
return
|
||||
}
|
||||
result[0] = tree.iter.GetPrev(result[1], 1)
|
||||
result[2] = tree.iter.GetNext(result[1], 1)
|
||||
result[0] = tree.iter.GetNext(result[1], 1)
|
||||
result[2] = tree.iter.GetPrev(result[1], 1)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
@ -418,8 +418,8 @@ func (tree *vbTree) GetNode(value interface{}) (*Node, bool) {
|
|||
|
||||
tree.iter.SetNode(n)
|
||||
iter := tree.iter
|
||||
iter.Prev()
|
||||
for iter.Prev() {
|
||||
iter.Next()
|
||||
for iter.Next() {
|
||||
if tree.Compare(iter.cur.value, n.value) == 0 {
|
||||
n = iter.cur
|
||||
} else {
|
||||
|
|
|
@ -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) {
|
||||
pq := New(compare.Int)
|
||||
pq.Push(1, 1)
|
||||
|
|
|
@ -175,7 +175,7 @@ func (tree *vbTree) removeNode(n *Node) {
|
|||
}
|
||||
|
||||
if tree.top == n {
|
||||
tree.top = tree.iter.GetNext(n, 1)
|
||||
tree.top = tree.iter.GetNext(n, 1) // 最大值 Next一位后为下个最大值
|
||||
}
|
||||
|
||||
ls, rs := getChildrenSize(n)
|
||||
|
|
Loading…
Reference in New Issue
Block a user