-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprqueue_test.go
More file actions
298 lines (269 loc) · 6.12 KB
/
prqueue_test.go
File metadata and controls
298 lines (269 loc) · 6.12 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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
package prqueue_test
import (
"errors"
"fmt"
"math/rand"
"sort"
"testing"
"github.com/turneps403/prqueue"
)
func TestNew_GenericType(t *testing.T) {
t.Run("New for generic int", func(t *testing.T) {
defer func() {
if r := recover(); r != nil {
t.Errorf("func New is in a panic: %v", r)
}
}()
pq := prqueue.New(func(a, b int) bool {
return a < b
})
_ = pq
})
t.Run("New for generic string", func(t *testing.T) {
defer func() {
if r := recover(); r != nil {
t.Errorf("func New is in a panic: %v", r)
}
}()
pq := prqueue.New(func(a, b string) bool {
return a < b
})
_ = pq
})
t.Run("New for generic struct", func(t *testing.T) {
defer func() {
if r := recover(); r != nil {
t.Errorf("func New is in a panic: %v", r)
}
}()
type tmp struct {
t int
}
pq := prqueue.New(func(a, b tmp) bool {
return a.t < b.t
})
_ = pq
})
t.Run("New for generic pointer", func(t *testing.T) {
defer func() {
if r := recover(); r != nil {
t.Errorf("func New is in a panic: %v", r)
}
}()
type tmp struct {
t int
}
pq := prqueue.New(func(a, b *tmp) bool {
return a.t < b.t
})
_ = pq
})
}
func TestAdd(t *testing.T) {
t.Run("Add for int", func(t *testing.T) {
defer func() {
if r := recover(); r != nil {
t.Errorf("func Add is in a panic: %v", r)
}
}()
pq := prqueue.New(func(a, b int) bool {
return a < b
})
for i := 0; i < 10; i++ {
pq.Add(i)
}
})
t.Run("Add for struct", func(t *testing.T) {
defer func() {
if r := recover(); r != nil {
t.Errorf("func Add is in a panic: %v", r)
}
}()
type tmp struct {
t int
}
pq := prqueue.New[tmp](func(a, b tmp) bool {
return a.t < b.t
})
for i := 0; i < 10; i++ {
pq.Add(tmp{i})
}
})
}
func TestLen(t *testing.T) {
pq := prqueue.New(func(a, b int) bool {
return a < b
})
max := rand.Intn(100) + 100
for i := 0; i < max; i++ {
pq.Push(i)
if pq.Len() != i+1 {
t.Fatalf("func Len isn't correct: expected %v but got %v", i, pq.Len())
}
}
t.Logf("func Len was successfully checked in %d loops", max)
}
func TestPoll0(t *testing.T) {
defer func() {
if r := recover(); r != nil {
t.Errorf("func Poll is in a panic: %v", r)
}
}()
pq := prqueue.New(func(a, b int) bool {
return a < b
})
if _, err := pq.Poll(); err == nil {
t.Errorf("func Poll from empty queue doesnt return error")
}
tint := 100500
pq.Add(tint)
v, err := pq.Poll()
if err != nil {
t.Errorf("func Poll from queue with 1 element return error: %v", err)
}
if v != tint {
t.Errorf("func Poll from queue with 1 element return wron value: expected %v got %v", tint, v)
}
if _, err := pq.Poll(); err == nil {
t.Errorf("func Poll from empty queue (second) doesnt return error")
}
}
func TestPoll1(t *testing.T) {
t.Run("Min heap correctness", func(t *testing.T) {
l := []int{1, 3, 5, 2, 4}
ordl := make([]int, len(l))
copy(ordl, l)
sort.Ints(ordl)
pq := prqueue.New(func(a, b int) bool {
return a < b
})
for _, v := range l {
pq.Add(v)
}
for i := 0; i < len(ordl); i++ {
e, _ := pq.Poll()
if ordl[i] != e {
t.Errorf("func Poll as min heap broken: expected %v got %v", ordl[i], e)
}
}
})
t.Run("Max heap correctness", func(t *testing.T) {
l := []int{1, 3, 5, 2, 4}
ordl := make([]int, len(l))
copy(ordl, l)
sort.Sort(sort.Reverse(sort.IntSlice(ordl)))
pq := prqueue.New(func(a, b int) bool {
return a > b
})
for _, v := range l {
pq.Add(v)
}
for i := 0; i < len(ordl); i++ {
e, _ := pq.Poll()
if ordl[i] != e {
t.Errorf("func Poll as max heap broken: expected %v got %v", ordl[i], e)
}
}
})
}
func TestPoll2(t *testing.T) {
pq := prqueue.New(func(a, b int) bool {
return a > b
})
_, err := pq.Poll()
if err == nil || !errors.Is(err, prqueue.ErrEmptyQueue) {
t.Errorf("func Poll returned unexpected error: expected %v got %v", "prqueue.ErrEmptyQueue", err)
}
}
func TestIsEmpty(t *testing.T) {
defer func() {
if r := recover(); r != nil {
t.Errorf("func IsEmpty is in a panic: %v", r)
}
}()
pq := prqueue.New(func(a, b int) bool {
return a < b
})
if !pq.IsEmpty() {
t.Errorf("func IsEmpty has broken: has to be empty")
}
pq.Add(100500)
if pq.IsEmpty() {
t.Errorf("func IsEmpty has broken: has to be not empty")
}
_, _ = pq.Poll()
if !pq.IsEmpty() {
t.Errorf("func IsEmpty has broken: has to be empty")
}
}
func TestPeek0(t *testing.T) {
t.Run("Min heap correctness", func(t *testing.T) {
l := []int{1, 3, 5, 2, 4}
pq := prqueue.New(func(a, b int) bool {
return a < b
})
for _, v := range l {
pq.Add(v)
}
for i := 0; i < len(l); i++ {
v, _ := pq.Peek()
e, _ := pq.Poll()
if v != e {
t.Errorf("func Peek as min heap broken: expected %v got %v", e, v)
}
}
})
t.Run("Max heap correctness", func(t *testing.T) {
l := []int{1, 3, 5, 2, 4}
pq := prqueue.New(func(a, b int) bool {
return a > b
})
for _, v := range l {
pq.Add(v)
}
for i := 0; i < len(l); i++ {
v, _ := pq.Peek()
e, _ := pq.Poll()
if v != e {
t.Errorf("func Peek as min heap broken: expected %v got %v", e, v)
}
}
})
}
func TestPeek1(t *testing.T) {
pq := prqueue.New(func(a, b int) bool {
return a > b
})
_, err := pq.Peek()
if err == nil || !errors.Is(err, prqueue.ErrEmptyQueue) {
t.Errorf("func Peek returned unexpected error: expected %v got %v", "prqueue.ErrEmptyQueue", err)
}
}
func TestString(t *testing.T) {
t.Run("String representation for int", func(t *testing.T) {
l := []int{1, 3, 5, 2, 4}
heapVer := "[1 2 5 3 4]"
pq := prqueue.New(func(a, b int) bool {
return a < b
})
for _, v := range l {
pq.Add(v)
}
if fmt.Sprintf("%v", pq) != heapVer {
t.Errorf("func String has broken format: expected %v got %v", heapVer, fmt.Sprintf("%v", pq))
}
})
t.Run("String representation for rune", func(t *testing.T) {
l := []string{"a", "z", "b", "k", "c"}
heapVer := "[a c b z k]"
pq := prqueue.New[string](func(a, b string) bool {
return a < b
})
for _, v := range l {
pq.Add(v)
}
if fmt.Sprintf("%v", pq) != heapVer {
t.Errorf("func String has broken format: expected %v got %v", heapVer, fmt.Sprintf("%v", pq))
}
})
}