-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_str.c
237 lines (183 loc) · 6.2 KB
/
test_str.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
/////////////////////////////////
// Header comment place holder //
/////////////////////////////////
#include "node_sparse_hashtable.h"
#include <stdio.h>
#include <string.h>
#include <time.h>
/* Our testcases */
char **dictionary = NULL;
char **duplicates = NULL;
int dict_size = 0;
int dupl_size = 0;
typedef struct test_entry_data_struct
{
char *key;
int num;
} test_entry;
/* Destructor function */
void destruct(void *data, void *key)
{
test_entry *temp = (test_entry *)data;
free(temp->key);
free(temp);
}
/* Destructor function */
int comp(const void *a, const void *b)
{
char *temp_a = (char *)a;
char *temp_b = (char *)b;
return !strcmp(temp_a, temp_b);
}
/* Printer function */
size_t hash(const void *key)
{
char *temp_key = (char *)key;
return strlen(temp_key);
}
/* Utility - Parses testcase file */
int parse_testcases(char *file)
{
FILE *fp = NULL;
int iters = 0;
char *readline = NULL;
size_t linelen = 0;
size_t bytesread = 1;
fp = fopen(file, "r");
if(!fp)
{
printf("File does not exist\n");
printf("Usage\n./test_str input_filename\n");
return -1;
}
/* Till failure parse the file */
while(bytesread)
{
char *temp = NULL;
char *del_char = NULL;
iters++;
bytesread = getline(&readline, &linelen, fp);
if(bytesread == 0 || bytesread == -1)
{
printf("Finished parsing of file\n**********************\n");
break;
}
dictionary = realloc(dictionary, (dict_size + 1) * sizeof(char *));
temp = malloc((bytesread + 1) * sizeof(char));
strcpy(temp, readline);
/* Remove the newline return */
del_char = strchr(temp, '\n');
if(del_char)
*del_char = '\0';
/* Remove the return character (Some files have it) */
del_char = strchr(temp, '\r');
if(del_char)
*del_char = '\0';
// printf("Entry %s\n", temp);
dictionary[dict_size] = temp;
dict_size++;
}
/* Free the allocated pointers */
free(readline);
fclose(fp);
return 1;
}
/* Utility - Frees memory used to create testcases */
void free_testcase(int flag)
{
/* Free the main table */
if(flag)
{
for(int i = 0; i < dict_size; i++)
free(dictionary[i]);
}
free(dictionary);
/* Free the duplicate entry table */
for(int i = 0; i < dupl_size; i++)
free(duplicates[i]);
free(duplicates);
}
int main(int argc, char **argv)
{
/* PART 1 - Parse the testcases */
if(parse_testcases(argv[1]) == -1)
return -1;
/*************************************************************************************************/
/* PART 2 - Set the main parameters */
/* Main parameters */
node_hashtable_t *hashtable = NULL;
const int hashtable_size = dict_size;
const int test_size = dict_size;
const int search_factor = 20;
const int delete_factor = 2;
/* Checker values */
int err_code;
int fail_insertions = 0, fail_searches = 0, fail_deletes = 0;
/* Timers for each part */
clock_t insert_s, insert_e;
clock_t search_s, search_e;
clock_t delete_s, delete_e;
/*************************************************************************************************/
/* PART 3 - Create the hashatable and perform insertions */
/* Time from creation along with rehashing and insertions */
insert_s = clock();
hashtable = ht_node_create(hashtable_size, comp, destruct, hash, &err_code);
for(size_t i = 0; i < test_size; i++)
{
test_entry *entry = (test_entry *)malloc(sizeof(test_entry));
entry->key = dictionary[i];
entry->num = i;
if(ht_node_insert(hashtable, entry->key, entry, &err_code))
{
fail_insertions++;
/* Insert it at the duplicate table */
duplicates = realloc(duplicates, (dupl_size + 1) * sizeof(char *));
duplicates[dupl_size] = dictionary[i];
dupl_size++;
/* Free the entry itself */
free(entry);
}
}
/*************************************************************************************************/
/* PART 4 - Create a search sequence and perform a round of searches */
/* Create a random testcase */
int *search_idx = malloc(search_factor * test_size * sizeof(int));
/* Limit the testcase in the range of our test_size */
for(int i = 0; i < search_factor * test_size; i++)
search_idx[i] = i % test_size;
/* Time for searches */
insert_e = clock() - insert_s;
/* Time for search_factor * num_of_testcases searches */
search_s = clock();
for(size_t i = 0; i < search_factor * test_size; i++)
{
if(!ht_node_search(hashtable, dictionary[search_idx[i]]))
fail_searches++;
}
/* Time for searches */
search_e = clock() - search_s;
/* Free the random access search idxs */
free(search_idx);
/*************************************************************************************************/
/* PART 5 - Perform a round of deletes */
/* Time for deletes */
delete_s = clock();
/* Delete from start of dictionary - Random access is not permited might cause SEGF */
for(size_t i = 0; i < test_size / delete_factor; i++)
{
if(ht_node_delete(hashtable, dictionary[i]) != 0)
fail_deletes++;
}
/* Time for deletes */
delete_e = clock() - delete_s;
/* Print statistics */
ht_node_print_mem_usage(hashtable);
printf("******** TIME statistics for {%s} with size {%d} ********\n", argv[1], dict_size - dupl_size);
printf("Part 3 {#%d Insertions - #%d Insert Fails}: %f\n", dict_size - fail_insertions, fail_insertions, (double)insert_e / CLOCKS_PER_SEC);
printf("Part 4 {#%d Searches - #%d Search Fails}: %f\n", search_factor * dict_size, fail_searches, (double)search_e / CLOCKS_PER_SEC);
printf("Part 5 {#%d Deletes - #%d Delete Fails}: %f\n", dict_size / delete_factor, fail_deletes, (double)delete_e / CLOCKS_PER_SEC);
/* FINAL PART - Free the hashtable and redundant duplicates */
ht_node_free(hashtable);
free_testcase(0);
return 1;
}