-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.c
120 lines (108 loc) · 2.73 KB
/
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#include "algorithms.h"
#include "binary_heap.h"
#include "graph.h"
#include "linked_list.h"
#include "queue.h"
#include "stack.h"
#include "stdio.h"
#include "stdlib.h"
void test_linked_list() {
LIST_TYPE *data = malloc(sizeof(LIST_TYPE) * 10);
for (int i = 0; i < 10; i++) {
data[i] = i;
}
LinkedListNode *root = list_from_array(data, 10);
LinkedListNode *iter_root = root;
free(data);
while (iter_root != NULL) {
printf("%d\n", (int)iter_root->value);
iter_root = iter_root->next;
}
free_list(root);
}
void test_stack() {
Stack *s = make_stack();
for (int i = 0; i < 20; i++) {
stack_push(s, i);
}
for (int i = 0; i < 20; i++) {
printf("%lu\n", stack_pop(s));
}
free_stack(s);
}
void test_queue() {
Queue *q = make_queue();
for (int i = 0; i < 20; i++) {
queue_push(q, i);
}
for (int i = 0; i < 20; i++) {
printf("%lu\n", queue_pop(q));
}
free_queue(q);
}
void test_binary_search() {
size_t test_length = 1000000;
BINARY_SEARCH_TYPE *data = malloc(sizeof(BINARY_SEARCH_TYPE) * test_length);
for (int i = 0; i < test_length; i++) {
data[i] = i;
}
printf("100: %lli\n", binary_search(data, test_length, 100));
printf("1243: %lli\n", binary_search(data, test_length, 1243));
printf("10000000: %lli\n", binary_search(data, test_length, 10000000));
free(data);
}
void test_binary_heap() {
MaxBinaryHeap *heap = make_binary_heap();
for (int i = 0; i < 10; i++) {
binary_heap_push(heap, i);
}
for (int i = 0; i < 10; i++) {
printf("%lu\n", heap->data[i]);
}
printf("Max: %lu\n", binary_heap_pop(heap));
for (int i = 0; i < 9; i++) {
printf("%lu\n", heap->data[i]);
}
free_binary_heap(heap);
}
void test_binary_heap_sort() {
BINARY_HEAP_TYPE array[] = {2, 1, 4, 3, 7, 6, 5, 8, 9, 10};
binary_heap_sort(array, 10);
for (int i = 0; i < 10; i++) {
printf("%lu\n", array[i]);
}
}
void test_quick_sort() {
QUICK_SORT_TYPE array[] = {2, 1, 4, 3, 7, 6, 5, 8, 9, 10};
quick_sort(array, 10);
for (int i = 0; i < 10; i++) {
printf("%lu\n", array[i]);
}
}
void test_graph_dfs() {
MatrixGraph *graph = make_matrix_graph(4);
GraphVisitor *visitor = make_print_visitor();
matrix_graph_set_edge(graph, 0, 1, 1);
matrix_graph_set_edge(graph, 1, 3, 1);
matrix_graph_set_edge(graph, 0, 2, 1);
dfs(graph, visitor, 0);
free_matrix_graph(graph);
free_graph_visitor(visitor);
}
int main(int argc, char *argv[]) {
printf("Linked List\n");
test_linked_list();
printf("Stack\n");
test_stack();
printf("Queue\n");
test_queue();
printf("Binary Search\n");
test_binary_search();
printf("Binary Heap\n");
test_binary_heap();
printf("Quick Sort\n");
test_quick_sort();
printf("DFS\n");
test_graph_dfs();
return EXIT_SUCCESS;
}