-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbinomheap_test.go
132 lines (89 loc) · 2.49 KB
/
binomheap_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
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
package BinomialHeap
import (
"testing"
"math/rand"
)
func Test_insert_1(t *testing.T) {
heap := NewBinomialHeap()
node := newBinomialHeapNode(1)
heap.insert(node)
if heap.forest_head != node {t.Errorf("something went wrong.")}
}
func Test_insert_2(t *testing.T) {
const RANGE = 16
heap := NewBinomialHeap()
insert_mult(heap, interval(0, RANGE))
if heap.forest_head.order != 4 {t.Errorf("something went wrong")}
}
func Test_pop_1(t *testing.T) {
const RANGE = 16
heap := NewBinomialHeap()
insert_mult(heap, reverse(interval(0, RANGE)))
for i:=0; i<RANGE; i++ {
pval := heap.Pop()
if pval != i {t.Errorf("expected %d, got %d", i, pval)}
}
}
func Test_pop_shuffle_1(t *testing.T) {
const RANGE = 10000
heap := NewBinomialHeap()
elems := shuffle(interval(0, RANGE))
insert_mult(heap, elems)
for i:=0; i<RANGE; i++ {
pval := heap.Pop()
if pval != i {t.Errorf("expected %d, got %d", i, pval)}
}
}
func Test_merge_1(t *testing.T) {
const (
SIZE1 = 1000
SIZE2 = 2000
)
h1 := NewBinomialHeap()
h2 := NewBinomialHeap()
insert_mult(h1, shuffle(interval(0, SIZE1)))
insert_mult(h2, shuffle(interval(SIZE1, SIZE2)))
h1.Merge(h2)
if h1.size != SIZE2 {t.Errorf("size error, expected %d, got %d,", SIZE2, h1.size)}
for i:=0; i<SIZE2; i++ {
pval := h1.Pop()
if pval != i {t.Errorf("expected %d, got %d", i, pval)}
}
}
// Helpers
func interval(start int, end int) []int {
// [start, end)
slice := make([]int, 0, end-start)
for i:=start; i<end; i++ {
slice = append(slice, i)
}
return slice
}
func reverse(values []int) []int {
// reverse the array
reversed := make([]int, len(values), len(values))
for i:=0; i<len(values); i++ {
reversed[len(values)-i-1] = values[i]
}
return reversed
}
func shuffle(slice []int) []int {
// shuffle the elements of the array
shuffled := make([]int, len(slice), len(slice))
copy(shuffled, slice)
for i:=0; i<len(shuffled); i++ {
index := rand.Intn(len(shuffled)-i)
index += i
// swap i'th and index'th element
tmp := shuffled[i]
shuffled[i] = shuffled[index]
shuffled[index] = tmp
}
return shuffled
}
func insert_mult(bh* BinomialHeap, values []int) {
// wrapper for inserting multiple elements into a heap.
for _, elem := range values {
bh.Insert(elem)
}
}