-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_minheap.c
149 lines (131 loc) · 3.81 KB
/
test_minheap.c
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
#include "minheap.h" // Includes ImgUtils.h
#include "unittest.h"
/**
* Recursively check if the heap property is satisfied.
*/
int checkRec(MinHeap *heap, int curIdx, double minp)
{
int correct = 1;
double pr = heap->arr[curIdx].priority;
if (pr < minp)
{
printf("arr[%d].priority = %f is less than some parent (%f) \n", curIdx, pr, minp);
correct = 0;
}
int lchild = (curIdx + 1) * 2;
int rchild = lchild - 1;
if (lchild < heap->numItems)
correct = correct && checkRec(heap, lchild, pr);
if (rchild < heap->numItems)
correct = correct && checkRec(heap, rchild, pr);
return correct;
}
/**
* Check if the heap looks OK (does not necessarily mean it is fully correct).
*/
static int checkHeap(MinHeap *heap)
{
int correct = 1;
// Check if the indices match up
for (int i = 0; i < heap->maxSize; i++)
if (heap->indices[i] >= 0)
if (heap->arr[heap->indices[i]].val != i)
printf("indices[%d] is not the right index.\n", i), correct = 0;
// Recursively cheap heap property
return correct && checkRec(heap, 0, heap->arr[0].priority);
}
/**
* Print out the heap to help debug
*/
void printHeap(MinHeap *heap)
{
printf("Heap: (numItems = %d / size = %d)\n", heap->numItems, heap->maxSize);
printf(" arr = [");
for (int i = 0; i < heap->numItems; i++)
{
printf("(%d, %f), ", heap->arr[i].val, heap->arr[i].priority);
}
printf("]\n ind = [");
for (int i = 0; i < heap->maxSize; i++)
{
printf("%d, ", heap->indices[i]);
}
printf("]\n");
}
/*****************************************************************************/
TEST(new_heap)
{
MinHeap *heap = newMinHeap(1024);
if (heap->numItems != 0)
TEST_FAIL("heap->numItems is not correct.\n");
if (heap->maxSize != 1024)
TEST_FAIL("heap->maxSize is not correct.\n");
for (int i = 0; i < 1024; i++)
if (heap->indices[i] != -1)
TEST_FAIL("heap->indices[%d] != -1\n", i);
}
TEST(in_order_insert)
{
MinHeap *heap = newMinHeap(10);
for (int i = 0; i < 10; i++)
heapPush(heap, i, (double)i);
for (int i = 0; i < 10; i++)
if (heap->arr[i].val != i || heap->arr[i].priority != i)
TEST_FAIL("Heap values not inserted correctly\n");
}
TEST(reverse_insert)
{
MinHeap *heap = newMinHeap(10);
for (int i = 9; i >= 0; i--)
heapPush(heap, i, (double)i);
if (!checkHeap(heap))
TEST_FAIL("Failed insert\n");
}
TEST(extract_min_ordered)
{
MinHeap *heap = newMinHeap(10);
for (int i = 0; i < 10; i++)
heapPush(heap, i, (double)i);
if (!checkHeap(heap))
TEST_FAIL("Failed checkHeap()\n");
double pri;
for (int i = 0; i < 10; i++)
if (heapExtractMin(heap, &pri) != i || pri != i)
TEST_FAIL("ExtractMin did not return correct values\n");
}
TEST(extract_min_random)
{
double p[] = {3.9, 2.2, 7.7, 6.5, 7.6, 8.9, 4.6, 3.0, 8.3, 1.9, 4.7, 2.8, 7.3, 5.1, 1.4};
MinHeap *heap = newMinHeap(15);
for (int i = 0; i < 15; i++)
heapPush(heap, i, p[i]);
if (!checkHeap(heap))
TEST_FAIL("Failed checkHeap()\n");
// Sorted indices
int si[] = {14, 9, 1, 11, 7, 0, 6, 10, 13, 3, 12, 4, 2, 8, 5};
double pri;
for (int i = 0; i < 15; i++)
if (heapExtractMin(heap, &pri) != si[i] || pri != p[si[i]])
TEST_FAIL("ExtractMin did not return correct values\n");
}
TEST(decrease_priorities_1)
{
MinHeap *heap = newMinHeap(100);
// Start off with a big priority for all elements
for (int i = 0; i < 100; i++)
heapPush(heap, i, 100000.0);
// Reduce all their priorities
for (int i = 0; i < 100; i++)
heapDecreasePriority(heap, i, 99 - i);
if (!checkHeap(heap))
TEST_FAIL("Failed checkHeap()\n");
double pri;
for (int i = 0; i < 100; i++)
if (heapExtractMin(heap, &pri) != 99 - i || pri != i)
TEST_FAIL("Decrease Priority didn't assign priorities correctly.\n");
}
int main(int argc, char *argv[])
{
unit_main(argc, argv);
return 0;
}