mirror of
https://github.com/garethgeorge/backrest.git
synced 2025-12-16 02:25:37 +00:00
fix: refactor priority ordered task queue implementation
This commit is contained in:
48
internal/queue/genheap_test.go
Normal file
48
internal/queue/genheap_test.go
Normal file
@@ -0,0 +1,48 @@
|
||||
package queue
|
||||
|
||||
import (
|
||||
"container/heap"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type val struct {
|
||||
v int
|
||||
}
|
||||
|
||||
func (v val) Less(other val) bool {
|
||||
return v.v < other.v
|
||||
}
|
||||
|
||||
func TestGenericHeapInit(t *testing.T) {
|
||||
genHeap := genericHeap[val]{{v: 3}, {v: 2}, {v: 1}}
|
||||
heap.Init(&genHeap)
|
||||
|
||||
if genHeap.Len() != 3 {
|
||||
t.Errorf("expected length to be 3, got %d", genHeap.Len())
|
||||
}
|
||||
|
||||
for _, i := range []int{1, 2, 3} {
|
||||
v := heap.Pop(&genHeap).(val)
|
||||
if v.v != i {
|
||||
t.Errorf("expected %d, got %d", i, v.v)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestGenericHeapPushPop(t *testing.T) {
|
||||
genHeap := genericHeap[val]{} // empty heap
|
||||
heap.Push(&genHeap, val{v: 3})
|
||||
heap.Push(&genHeap, val{v: 2})
|
||||
heap.Push(&genHeap, val{v: 1})
|
||||
|
||||
if genHeap.Len() != 3 {
|
||||
t.Errorf("expected length to be 3, got %d", genHeap.Len())
|
||||
}
|
||||
|
||||
for _, i := range []int{1, 2, 3} {
|
||||
v := heap.Pop(&genHeap).(val)
|
||||
if v.v != i {
|
||||
t.Errorf("expected %d, got %d", i, v.v)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user