-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathpriority_queue_test.go
82 lines (69 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package priority_queue
import (
"fmt"
"testing"
)
func TestMaxPriorityQueue(t *testing.T) {
h := NewMax()
h.Insert(*NewItem(8, 10))
h.Insert(*NewItem(7, 11))
h.Insert(*NewItem(6, 12))
h.Insert(*NewItem(3, 13))
h.Insert(*NewItem(1, 14))
h.Insert(*NewItem(0, 15))
h.Insert(*NewItem(2, 16))
h.Insert(*NewItem(4, 17))
h.Insert(*NewItem(9, 18))
h.Insert(*NewItem(5, 19))
sorted := make([]Item, 0)
for h.Len() > 0 {
sorted = append(sorted, h.Extract())
}
for i := 0; i < len(sorted)-2; i++ {
if sorted[i].Priority < sorted[i+1].Priority {
fmt.Println(sorted)
t.Error()
}
}
}
func TestMinPriorityQueue(t *testing.T) {
h := NewMin()
h.Insert(*NewItem(8, 10))
h.Insert(*NewItem(7, 11))
h.Insert(*NewItem(6, 12))
h.Insert(*NewItem(3, 13))
h.Insert(*NewItem(1, 14))
h.Insert(*NewItem(0, 15))
h.Insert(*NewItem(2, 16))
h.Insert(*NewItem(4, 17))
h.Insert(*NewItem(9, 18))
h.Insert(*NewItem(5, 19))
sorted := make([]Item, 0)
for h.Len() > 0 {
sorted = append(sorted, h.Extract())
}
for i := 0; i < len(sorted)-2; i++ {
if sorted[i].Priority > sorted[i+1].Priority {
fmt.Println(sorted)
t.Error()
}
}
}
func TestChangePriority(t *testing.T) {
h := NewMax()
h.Insert(*NewItem(8, 10))
h.Insert(*NewItem(7, 11))
h.Insert(*NewItem(6, 12))
h.Insert(*NewItem(3, 13))
h.Insert(*NewItem(1, 14))
h.Insert(*NewItem(0, 15))
h.Insert(*NewItem(2, 16))
h.Insert(*NewItem(4, 17))
h.Insert(*NewItem(9, 18))
h.Insert(*NewItem(5, 19))
h.ChangePriority(8, 66)
popped := h.Extract()
if popped.Value != 8 {
t.Error()
}
}