-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreaper_test.go
More file actions
191 lines (149 loc) · 3.7 KB
/
Copy pathreaper_test.go
File metadata and controls
191 lines (149 loc) · 3.7 KB
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
package reaper
import (
"reflect"
"slices"
"testing"
)
type IntHeap struct {
s []int
}
func NewIntHeap(vals ...int) *IntHeap {
h := &IntHeap{}
h.s = append(h.s, vals...)
slices.Sort(h.s)
return h
}
func (h *IntHeap) Push(elem int) {
h.s = append(h.s, elem)
slices.Sort(h.s)
}
func (h *IntHeap) Pop() int {
elem := h.s[0]
h.s = h.s[1:]
return elem
}
func (h *IntHeap) Empty() bool { return len(h.s) == 0 }
func (h *IntHeap) Lock() {}
func (h *IntHeap) Unlock() {}
func TestReaperEmpty(t *testing.T) {
h := NewIntHeap()
r := New[int](0)
var visited bool
visit := func(_ int, _ func(...int) error) (stop bool) {
visited = true
return false
}
if res := r.Reap(h, CallbackFunc[int](visit)); res != Exhausted {
t.Errorf("expected Exhausted, got: %v", res)
}
if visited {
t.Errorf("expected no visits on empty heap")
}
}
func TestReaperBasic(t *testing.T) {
h := NewIntHeap(1, 2, 3)
degree := 2
r := New[int](degree)
var visited []int
visit := func(front int, emit func(...int) error) (stop bool) {
if front == 2 {
return true
}
visited = append(visited, front)
if err := emit(4, 5); err != nil {
t.Errorf("unexpected emit error: %v", err)
}
return false
}
if res := r.Reap(h, CallbackFunc[int](visit)); res != Stopped {
t.Errorf("expected Stopped, got: %v", res)
}
if !reflect.DeepEqual(visited, []int{1}) {
t.Errorf("unexpected visits: %v", visited)
}
if !reflect.DeepEqual(h.s, []int{2, 3, 4, 5}) {
t.Errorf("unexpected heap state: %v", h.s)
}
}
func TestReaperExhaustion(t *testing.T) {
h := NewIntHeap(1, 2, 3)
r := New[int](2)
var visited []int
visit := func(front int, emit func(...int) error) (stop bool) {
switch front {
case 1:
_ = emit(5, 4)
case 5:
_ = emit(6)
case 6:
_ = emit(7)
_ = emit(8)
}
visited = append(visited, front)
return // continue
}
if res := r.Reap(h, CallbackFunc[int](visit)); res != Exhausted {
t.Errorf("expected Exhausted, got: %v", res)
}
if !reflect.DeepEqual(visited, []int{1, 2, 3, 4, 5, 6, 7, 8}) {
t.Errorf("unexpected visits: %v", visited)
}
if !h.Empty() {
t.Errorf("expected heap to be empty, got: %v", h.s)
}
}
func TestReaperEmitOverflow(t *testing.T) {
h := NewIntHeap(1, 2, 3)
degree := 2
r := New[int](degree)
visit := func(_ int, emit func(...int) error) (stop bool) {
if err := emit(4, 5, 6); err != ErrEmitBufferOverflow {
t.Errorf("expected overflow error, got: %v", err)
}
return true
}
if res := r.Reap(h, CallbackFunc[int](visit)); res != Stopped {
t.Errorf("expected Stopped, got: %v", res)
}
if !reflect.DeepEqual(h.s, []int{1, 2, 3}) {
t.Errorf("unexpected heap state: %v", h.s)
}
}
func TestReaperMultipleEmitCalls(t *testing.T) {
h := NewIntHeap(1, 2, 3)
degree := 3
r := New[int](degree)
visit := func(front int, emit func(...int) error) (stop bool) {
if front == 2 {
return true
}
if err := emit(4); err != nil {
t.Errorf("unexpected emit error: %v", err)
}
if err := emit(5, 6); err != nil {
t.Errorf("unexpected emit error: %v", err)
}
if err := emit(7); err != ErrEmitBufferOverflow {
t.Errorf("expected overflow error, got: %v", err)
}
return false
}
_ = r.Reap(h, CallbackFunc[int](visit))
if !reflect.DeepEqual(h.s, []int{2, 3, 4, 5, 6}) {
t.Errorf("unexpected heap state: %v", h.s)
}
}
func TestReaperZeroDegreeEmit(t *testing.T) {
h := NewIntHeap(1, 2, 3)
r := New[int](0)
visit := func(_ int, emit func(...int) error) (stop bool) {
if err := emit(4); err != ErrEmitBufferOverflow {
t.Errorf("expected overflow error, got: %v", err)
}
return true
}
_ = r.Reap(h, CallbackFunc[int](visit))
if !reflect.DeepEqual(h.s, []int{1, 2, 3}) {
t.Errorf("unexpected heap state: %v", h.s)
}
}