-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.c
189 lines (130 loc) · 4.25 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
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#include "include/Node.h"
#include "include/Btree.h"
#include "include/utils.h"
#include <unistd.h>
void test_search_build(long len, int key) {
struct timespec requestStart, requestEnd;
Btree* tree = BTree_init("tree.dat", false);
Data *records = get_data("tmp/dataset.csv", len);
int i;
clock_gettime(CLOCK_REALTIME, &requestStart);
for(i = 0; i < len; i++) {
insert(tree, &records[i]);
}
clock_gettime(CLOCK_REALTIME, &requestEnd);
printf("\nTree built in: %f ms\n", accum_time(requestStart, requestEnd));
clock_gettime(CLOCK_REALTIME, &requestStart);
Data *res = search(tree, key);
clock_gettime(CLOCK_REALTIME, &requestEnd);
print_function(res);
printf("Result found in: %f ms\n\n", accum_time(requestStart, requestEnd));
// printf("\n%d\n", search(tree, key)->key);
FILE *fout = fopen("meta.dat", "w");
fwrite(tree, sizeof(Btree), 1, fout);
close(fout);
BTree_destroy(tree);
return;
}
void test_build(long len) {
struct timespec requestStart, requestEnd;
Btree* tree = BTree_init("tree.dat", false);
Data *records = get_data("tmp/dataset.csv", len);
int i;
clock_gettime(CLOCK_REALTIME, &requestStart);
for(i = 0; i < len; i++) {
insert(tree, &records[i]);
}
clock_gettime(CLOCK_REALTIME, &requestEnd);
FILE *fout = fopen("meta.dat", "w");
fwrite(tree, sizeof(Btree), 1, fout);
close(fout);
BTree_destroy(tree);
printf("\nTree built in: %f ms\n", accum_time(requestStart, requestEnd));
}
void test_search(int key) {
struct timespec requestStart, requestEnd;
Btree* tree = BTree_init("tree.dat", true);
clock_gettime(CLOCK_REALTIME, &requestStart);
Data *res = search(tree, key);
clock_gettime(CLOCK_REALTIME, &requestEnd);
print_function(res);
BTree_destroy(tree);
printf("Result found in: %f ms\n\n", accum_time(requestStart, requestEnd));
return;
}
void test_test(long len, int key) {
Btree* tree = BTree_init("tree.dat", true);
// Data *records = get_data("tmp/dataset.csv", len);
// int i;
// for(i = 0; i < len; i++) {
// insert(tree, &records[i]);
// }
// traverse(tree, tree->root);
// FILE *fin = fopen(tree->fp, "rb");
// Node temp;
// fread(&temp, sizeof(Node), 1, fin);
// printf("%d", temp.n);
// traverse(tree, tree->root);
// for(int i = 0; i < len; i++) {
// search(tree, records[i].key);
// }
// search(tree, key);
Node n;
long length = sizeof(n);
printf("%ld\n", length);
return;
Data *records = get_data("tmp/dataset.csv", len);
for(int i = 0; i < len; i++) {
removeFromTree(tree, records[i].key);
// res = search(tree, key);
// print_function(res);
}
BTree_destroy(tree);
return;
}
void test_delete(int key) {
struct timespec requestStart, requestEnd;
Btree* tree = BTree_init("tree.dat", true);
printf("Node to delete:\n");
Data *res = search(tree, key);
print_function(res);
clock_gettime(CLOCK_REALTIME, &requestStart);
bool isDelete = removeFromTree(tree, key);
clock_gettime(CLOCK_REALTIME, &requestEnd);
if(isDelete) {
printf("Delete successful\n");
}
else {
printf("Delete unsuccessful\n");
}
printf("Result found in: %f ms\n\n", accum_time(requestStart, requestEnd));
BTree_destroy(tree);
}
void verbose() {
}
void run_tests(char** args, int no_of_args) {
int i = 0;
char ptr[10];
if(no_of_args == 0) {
verbose();
}
if(!strcmp(args[0], "-sb")) {
test_search_build(strtol(args[1], ptr, 10), strtol(args[2], ptr, 10));
}
if(!strcmp(args[0], "-b")) {
test_build(strtol(args[1], ptr, 10));
}
if(!strcmp(args[0], "-s")) {
test_search(strtol(args[1], ptr, 10));
}
if(!strcmp(args[0], "-d")) {
test_delete(strtol(args[1], ptr, 10));
}
if(!strcmp(args[0], "-t")) {
test_test(strtol(args[1], ptr, 10), strtol(args[2], ptr, 10));
}
}
int main(int argl, char** argv) {
run_tests(argv + 1, argl - 1);
return 0;
}