-
Notifications
You must be signed in to change notification settings - Fork 1
/
priority_queue_test.go
60 lines (50 loc) · 1.54 KB
/
priority_queue_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package astar
import (
"container/heap"
"testing"
)
func Test__Pop_Takes_Item_With_Lowest_Priority_Value(t *testing.T) {
// given
pq := &PriorityQueue{}
heap.Init(pq)
pq.Push(&PriorityQueueAStarItem{ node: nil, estimatedTotalCost: 5, index: 0 })
pq.Push(&PriorityQueueAStarItem{ node: nil, estimatedTotalCost: 100, index: 1 })
// when
poppedItem := heap.Pop(pq).(*PriorityQueueAStarItem)
// then
if poppedItem.Priority() != 5 {
t.Error("Expected item with priority = 5")
}
}
func Test__Fix_Repairs_Heap_After_Manual_Priority_Modification(t *testing.T) {
// given
pq := &PriorityQueue{}
heap.Init(pq)
item := &PriorityQueueAStarItem{ node: nil, estimatedTotalCost: 5, index: 0 }
pq.Push(item)
pq.Push(&PriorityQueueAStarItem{ node: nil, estimatedTotalCost: 100, index: 1 })
// when
item.estimatedTotalCost = 200
heap.Fix(pq, item.index)
poppedItem := heap.Pop(pq).(*PriorityQueueAStarItem)
// then
if poppedItem.Priority() != 100 {
t.Error("Expected item with priority = 100")
}
}
func Test__Remove_Followed_By_Push_Reestablish_Heap_Ordering(t *testing.T) {
// given
pq := &PriorityQueue{}
heap.Init(pq)
pq.Push(&PriorityQueueAStarItem{ node: nil, estimatedTotalCost: 5, index: 0 })
pq.Push(&PriorityQueueAStarItem{ node: nil, estimatedTotalCost: 100, index: 1 })
// when
item := heap.Remove(pq, 0).(*PriorityQueueAStarItem)
item.estimatedTotalCost = 200
heap.Push(pq, item)
poppedItem := heap.Pop(pq).(*PriorityQueueAStarItem)
// then
if poppedItem.Priority() != 100 {
t.Error("Expected item with priority = 100")
}
}