-
Notifications
You must be signed in to change notification settings - Fork 3
/
minheap_test.go
75 lines (68 loc) · 1.76 KB
/
minheap_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
package htask
import (
"testing"
"time"
)
func TestMinHeap(t *testing.T) {
times := make([]time.Time, 10)
times[0] = time.Now()
for i := 1; i < 10; i++ {
times[i] = times[i-1].Add(time.Second)
}
t.Run("no limit min heap", func(t *testing.T) {
h := newMinHeap(0)
h.add(job{t: times[5]})
h.add(job{t: times[4]})
h.add(job{t: times[6]})
h.add(job{t: times[3]})
h.add(job{t: times[4]})
result := []int{3, 4, 4, 5, 6}
for _, i := range result {
if peek := h.peek(); !peek.t.Equal(times[i]) {
t.Errorf("peek = %v expected %v", peek.t, times[i])
}
if pop := h.pop(); !pop.t.Equal(times[i]) {
t.Errorf("pop = %v expected %v", pop.t, times[i])
}
}
if h.size() != 0 {
t.Errorf("expect empty but size = %v", h.size())
}
if peek := h.peek(); !peek.t.IsZero() {
t.Errorf("empty peek = %v", peek.t)
}
if pop := h.peek(); !pop.t.IsZero() {
t.Errorf("empty pop = %v", pop.t)
}
})
t.Run("limited min heap", func(t *testing.T) {
max := 5
h := newMinHeap(max)
h.add(job{t: times[5]})
h.add(job{t: times[4]})
h.add(job{t: times[6]})
h.add(job{t: times[3]})
h.add(job{t: times[4]})
if err := h.add(job{t: times[1]}); err != ErrMax {
t.Errorf("max add expected error: %v, but %v", ErrMax, err)
}
result := []int{3, 4, 4, 5, 6}
for _, i := range result {
if peek := h.peek(); !peek.t.Equal(times[i]) {
t.Errorf("peek = %v expected %v", peek.t, times[i])
}
if pop := h.pop(); !pop.t.Equal(times[i]) {
t.Errorf("pop = %v expected %v", pop.t, times[i])
}
}
if h.size() != 0 {
t.Errorf("expect empty but size = %v", h.size())
}
if peek := h.peek(); !peek.t.IsZero() {
t.Errorf("empty peek = %v", peek.t)
}
if pop := h.peek(); !pop.t.IsZero() {
t.Errorf("empty pop = %v", pop.t)
}
})
}