-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
216 lines (161 loc) · 6.04 KB
/
main.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "inc/legacy_macros.h"
#include "inc/generic_hash_table.h"
#define BASE_SIZE 1931
#define TEST_CASES 3250
typedef struct _test_struct {
int id;
float val;
} test_t;
int main()
{
srand(time(0));
UNUSED(RAND_GEN(100));
g_hash_table_t ht;
ght_ret_status_t ret_code;
test_t temp_var;
unsigned long temp_key;
unsigned int insert_counter = 0;
unsigned long inserted_keys[TEST_CASES];
clock_t begin = clock();
if(ght_init(&ht, BASE_SIZE, sizeof(test_t)) != GHT_SUCCESS) {
printf("Init Failed\n");
}
for(unsigned int itr = 0 ; itr < TEST_CASES; ++itr) {
ght_generate_key(&temp_key);
temp_var.id = RAND_GEN(40000);
temp_var.val = (float) RAND_GEN(80000);
if((ret_code = ght_insert(&ht, temp_key, &temp_var)) != GHT_SUCCESS)
printf("Insert Error -> E_CODE: %d\n", ret_code);
else {
inserted_keys[insert_counter++] = temp_key;
printf("Insterted data: Key -> %ld, Data 1st: %d 2nd: %f\n", temp_key, temp_var.id, temp_var.val);
}
temp_var.id = 0;
temp_var.val = 0.0;
if((ret_code = ght_get(&ht, temp_key, &temp_var)) != GHT_SUCCESS)
printf("Get Error -> E_CODE: %d\n", ret_code);
else
printf("Got data: Key -> %ld, Data 1st: %d 2nd: %f\n", temp_key, temp_var.id, temp_var.val);
printf("\n\n\n");
}
for(unsigned int itr = 0 ; itr < insert_counter; ++itr) {
if((ret_code = ght_delete(&ht, inserted_keys[itr])) != GHT_SUCCESS)
printf("Delete Error Key -> %ld E_CODE: %d\n", inserted_keys[itr], ret_code);
else
printf("Deleted Key -> %ld\n", inserted_keys[itr]);
printf("\n\n\n");
}
if(ght_deinit(&ht) != GHT_SUCCESS) {
printf("Init Failed\n");
return EXIT_FAILURE;
}
else {
clock_t end = clock();
double time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
printf("DE INIT SUCCESS!\n");
printf("Execution time: %f\n", time_spent);
return EXIT_SUCCESS;
}
return 0;
}
#if 0
int main() {
g_hash_table_t test_htable;
test_t temp_test, temp_test_ret;
unsigned long temp_key;
if(ght_init(&test_htable, BASE_SIZE, sizeof(test_t)) != GHT_SUCCESS)
printf("FAILED!\n");
unsigned int t_case_cntr = 0;
srand(time(0));
UNUSED(RAND_GEN(100));
while((t_case_cntr++) < (TEST_CASES)) {
temp_test.id = RAND_GEN(1000);
temp_test.val = (float) RAND_GEN(200);
temp_key = (RAND_GEN(10000));
printf("Data: %d %f\n", temp_test.id, temp_test.val);
if(t_case_cntr == 1000) {
printf("DELETE_BIGNS\n");
for(unsigned int titr = 0; (titr < NO_OF_DELETES) && (titr < key_arr_cntr); ++titr) {
if(ght_delete(&test_htable, key_arr[titr]) != GHT_SUCCESS)
printf("DELETE_FAIL\n");
}
}
if(ght_insert(&test_htable, temp_key, (void *)&temp_test) == GHT_SUCCESS) {
if(key_arr_cntr < NO_OF_DELETES) {
key_arr[key_arr_cntr] = temp_key;
key_arr_cntr++;
}
}
if(ght_get(&test_htable, temp_key, &temp_test_ret) == GHT_SUCCESS) {
printf("Return ID: %ld, %d %f\n", temp_key, temp_test_ret.id, temp_test_ret.val);
/* if(RAND_GEN(4) == 1)
ght_delete(&test_htable, temp_key); */
}
printf("\n\n");
}
if(ght_deinit(&test_htable) != GHT_SUCCESS)
printf("FAILED!\n");
return EXIT_SUCCESS;
}
#endif /* main */
#if 0 /* test main */
int main() {
g_hash_table_t test_htable;
test_t temp_test, temp_test_ret = {0, 0.0};
unsigned long temp_key;
unsigned int cntr = 0, insr_count = 0, retr_count = 0;
ght_ret_status_t ret_code;
clock_t begin = clock();
if(ght_init(&test_htable, BASE_SIZE, sizeof(test_t)) != GHT_SUCCESS)
printf("FAILED!\n");
while(cntr < TEST_CASES) {
temp_test.id = RAND_GEN(1000);
temp_test.val = (float) RAND_GEN(200);
ght_generate_key(&temp_key);
ret_code = ght_insert(&test_htable, temp_key, (void *)&temp_test);
if(ret_code == GHT_SUCCESS) {
key_arr[key_arr_cntr] = temp_key;
key_arr_cntr++;
insr_count++;
printf("INSERTION: ITR: %d NOS-> %d KEY-> %ld DATA: ID-> %d Val-> %f\n", \
cntr, insr_count, temp_key, temp_test.id, temp_test.val);
}
else if(ret_code == GHT_KEY_ALRDY_EXISTS)
printf("______INSERTION GHT_KEY_ALRDY_EXISTS: ITR: %d NOS-> %d KEY-> %ld DATA: ID-> %d Val-> %f\n", \
cntr, insr_count, temp_key, temp_test.id, temp_test.val);
else
printf("______INSERTION GHT_FAIL: ITR: %d NOS-> %d KEY-> %ld DATA: ID-> %d Val-> %f\n", \
cntr, insr_count, temp_key, temp_test.id, temp_test.val);
++cntr;
}
printf("KEY_ARR_SIZE: %d\n", key_arr_cntr);
cntr = 0;
while(cntr < key_arr_cntr) {
ret_code = ght_get(&test_htable, key_arr[cntr], (void *)&temp_test_ret);
if(ret_code == GHT_SUCCESS) {
retr_count++;
printf("RETRIEVING: CNTR-> %d NO-> %d KEY-> %ld DATA: ID-> %d Val-> %f\n", \
cntr, retr_count, key_arr[cntr], temp_test_ret.id, temp_test_ret.val);
}
else if(ret_code == GHT_FAIL)
printf("______RETRIEVING GHT_FAIL: CNTR-> %d NO-> %d KEY-> %ld DATA: ID-> %d Val-> %f\n", \
cntr, retr_count, key_arr[cntr], temp_test_ret.id, temp_test_ret.val);
cntr++;
}
if(ght_deinit(&test_htable)!= GHT_SUCCESS) {
printf("DE INIT FAILED!\n");
return EXIT_FAILURE;
}
else {
clock_t end = clock();
double time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
printf("DE INIT SUCCESS!\n");
printf("Execution time: %f\n", time_spent);
return EXIT_SUCCESS;
}
return 0;
}
#endif