-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathheap_test.c
103 lines (80 loc) · 2.14 KB
/
heap_test.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
#include <stdlib.h>
#include <stdint.h>
#include <stdio.h>
#include <time.h>
#include "heap.h"
struct myfancystruct_t {
int value;
int specialvalue1;
int specialvalue2;
int specialvalue3;
};
void initialize_fancy(struct myfancystruct_t* fancy, int value) {
fancy->value = value;
fancy->specialvalue1 = fancy->value * 5;
fancy->specialvalue2 = fancy->specialvalue1 >> 2;
fancy->specialvalue3 = fancy->specialvalue2 & 0x5353;
}
int check_fancy(struct myfancystruct_t* fancy) {
struct myfancystruct_t clone;
initialize_fancy(&clone, fancy->value);
if(clone.specialvalue1 != fancy->specialvalue1)
return 0;
if(clone.specialvalue2 != fancy->specialvalue2)
return 0;
if(clone.specialvalue3 != fancy->specialvalue3)
return 0;
return 1;
}
int compare(void* a, void* b) {
return ((struct myfancystruct_t*)a)->value - ((struct myfancystruct_t*)b)->value;
}
int main() {
struct heap_t* heap = heap_create(compare);
printf("Adding values...");
srand((unsigned int)time(NULL));
for(int i = 0; i < 10000; i++) {
struct myfancystruct_t* fancy = malloc(sizeof(struct myfancystruct_t));
initialize_fancy(fancy, rand());
heap_insert(heap, fancy);
}
printf("Heap size: %d\n", heap_size(heap));
printf("\nPopping elements:\n");
while(heap_size(heap)) {
struct myfancystruct_t* fancy = heap_pop(heap);
printf("Next element: %d", fancy->value);
if(!check_fancy(fancy)) {
printf(" - INVALID FANCY!");
}
printf("\n");
free(fancy);
}
printf("\nMore random testing...\n");
for(int i = 0; i < 100000; i++) {
if((i / 10) % 3 != 0) {
struct myfancystruct_t* fancy = malloc(sizeof(struct myfancystruct_t));
initialize_fancy(fancy, rand());
heap_insert(heap, fancy);
}
else {
struct myfancystruct_t* fancy = heap_pop(heap);
if(!fancy) {
printf("Pop returned NULL, heap size: %d\n", heap_size(heap));
}
else if(!check_fancy(fancy)) {
printf("INVALID FANCY!\n");
}
free(fancy);
}
}
while(heap_size(heap)) {
struct myfancystruct_t* fancy = heap_pop(heap);
if(!check_fancy(fancy)) {
printf("INVALID FANCY!\n");
}
free(fancy);
}
printf("SUCCESS!\n");
heap_delete(heap, free);
return 0;
}