fusen-render/priority_queue_test.go

57 lines
1.2 KiB
Go
Raw Normal View History

2023-07-29 21:11:04 +00:00
package fusenrender_test
import (
"container/heap"
"fmt"
"fusenrender"
"testing"
)
func TestQueue(t *testing.T) {
q := make(fusenrender.PriorityQueue[*fusenrender.QueueItem], 0)
heap.Init(&q)
// 添加几个item
qitema := &fusenrender.QueueItem{Group: "testa", Priority: 2}
a := &fusenrender.Slice[*fusenrender.QueueItem]{
Key: qitema.GetKey(),
Value: qitema,
}
qitemb := &fusenrender.QueueItem{Group: "testa", Priority: 4}
b := &fusenrender.Slice[*fusenrender.QueueItem]{
Key: qitemb.GetKey(),
Value: qitemb,
}
qitemc := &fusenrender.QueueItem{Group: "testb", Priority: 3}
c := &fusenrender.Slice[*fusenrender.QueueItem]{
Key: qitemc.GetKey(),
Value: qitemc,
}
heap.Push(&q, a)
heap.Push(&q, a)
heap.Push(&q, a)
heap.Push(&q, b)
heap.Push(&q, c)
// 取出最高优先级的item
for len(q) != 0 {
item := heap.Pop(&q).(*fusenrender.QueueItem)
fmt.Printf("%v\n", item)
}
// 更新某一项的优先级
// banana := pq[1] // 拿到banana项
// banana.key = []byte("avocado") // 更新key
// heap.Fix(&pq, banana.index) // 修复堆
// // 再取出最高优先级
// item = heap.Pop(&pq).(*Item)
// fmt.Printf("%s %s\n", item.key, item.value) // "avocado" "香蕉"
}