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
|
package queue
import (
"fmt"
"testing"
"codeberg.org/snonux/algorithms/ds"
)
const minLength int = 1
const maxLength int = 10000
const factor int = 100
// Store results here to avoid compiler optimizations
var benchResult ds.ArrayList
func TestElementaryPriority(t *testing.T) {
q := NewElementaryPriority(1)
for i := minLength; i <= maxLength; i *= factor {
test(q, i, t)
}
}
func TestHeapPriority(t *testing.T) {
q := NewHeapPriority(1)
for i := minLength; i <= maxLength; i *= factor {
test(q, i, t)
}
}
func test(q PriorityQueue, l int, t *testing.T) {
cb := func(t *testing.T) {
for _, a := range ds.NewRandomArrayList(l, -1) {
q.Insert(a)
}
prev, started := 0, false
t.Log("Size", q.Size(), q.Empty())
for !q.Empty() {
next := q.DeleteMax()
if started {
if next > prev {
t.Errorf("Expected element '%v' to be lower than previous '%v': %v",
next, prev, q)
}
prev = next
continue
}
started = true
prev = next
}
}
t.Run(fmt.Sprintf("%d", l), cb)
}
func BenchmarkElementaryPriority(b *testing.B) {
q := NewElementaryPriority(1)
for i := minLength; i <= maxLength; i *= factor {
benchmark(q, i, b)
}
}
func BenchmarkHeapPriority(b *testing.B) {
q := NewHeapPriority(1)
for i := minLength; i <= maxLength; i *= factor {
benchmark(q, i, b)
}
}
func benchmark(q PriorityQueue, l int, b *testing.B) {
benchResult = ds.NewRandomArrayList(l, -1)
b.Run(fmt.Sprintf("randomInsert(%d)", l), func(b *testing.B) {
for i := 0; i < b.N; i++ {
q.Clear()
for _, a := range benchResult {
q.Insert(a)
}
}
})
b.Run(fmt.Sprintf("randomInsertAndDeleteMax(%d)", l), func(b *testing.B) {
for i := 0; i < b.N; i++ {
q.Clear()
for _, a := range benchResult {
q.Insert(a)
}
for i := 0; !q.Empty(); i++ {
benchResult[i] = q.DeleteMax()
}
}
})
}
|