Skip to content

Commit ddde7e8

Browse files
committed
query: add regression test for frontier trimming
1 parent 9d0d61c commit ddde7e8

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

query/shortest_test.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package query
2+
3+
import (
4+
"container/heap"
5+
"testing"
6+
7+
"github.com/stretchr/testify/require"
8+
)
9+
10+
func TestPriorityQueueTrimToMax_RemovesHighestCost(t *testing.T) {
11+
var pq priorityQueue
12+
heap.Init(&pq)
13+
14+
costs := []float64{1, 50, 2, 60, 3, 70, 4}
15+
for _, c := range costs {
16+
heap.Push(&pq, &queueItem{cost: c})
17+
}
18+
19+
// Trim to keep N-1 elements.
20+
(&pq).TrimToMax(int64(len(costs) - 1))
21+
require.Equal(t, len(costs)-1, pq.Len())
22+
23+
// Pop all remaining costs and ensure the maximum was removed.
24+
seen := make(map[float64]bool, len(costs))
25+
for pq.Len() > 0 {
26+
seen[heap.Pop(&pq).(*queueItem).cost] = true
27+
}
28+
29+
require.False(t, seen[70], "expected highest cost to be removed")
30+
for _, c := range []float64{1, 2, 3, 4, 50, 60} {
31+
require.True(t, seen[c], "expected cost to remain: %v", c)
32+
}
33+
}

0 commit comments

Comments
 (0)