]> git.datanom.net - hashtable.git/blame - test.c
print function
[hashtable.git] / test.c
CommitLineData
1d8fe1f7
MR
1#include <stdio.h>
2#include <stdlib.h>
3#include <string.h>
4#include <inttypes.h>
8c5be416 5#include <stdbool.h>
1d8fe1f7
MR
6#include "hashtable.h"
7
8#define MAX_LINE 4096
9
8c5be416
MR
10typedef struct _Node {
11 char* name;
12} Node;
13
14uint64_t word_hash_func(const void* o) {
1d8fe1f7 15 uint64_t hash_value = 0;
8c5be416
MR
16 const char* name = o;
17 for (size_t i = 0; i < strlen(name); i++) {
1d8fe1f7
MR
18 hash_value += name[i];
19 hash_value = hash_value * name[i];
20 }
21 return hash_value;
22}
23
24void generate_random_word(char* buffer, size_t length) {
25 for (size_t i = 0; i < length - 1; i++) {
26 buffer[i] = 'a' + (rand() % 26);
27 }
28 buffer[length - 1] = 0;
29}
30
8c5be416
MR
31void my_str_free(void* o) {
32 printf("Freeing %p\n", o);
33 free(o);
34}
35
36void my_ptr_free(void* o) {
37 Node* node = o;
38 //printf("Freeing %p\n", node);
39 free(node->name);
40 free(node);
41}
42
f3e1ef9f
MR
43void my_print(void* o) {
44 Node* n = (Node *) o;
45 printf("%s", n->name);
46}
47
8c5be416
MR
48bool my_ptr_equal(const void* a, const void* b) {
49 const Node* nodea = a;
50 const Node* nodeb = b;
51
52 if (nodea == NULL || nodeb == NULL) return false;
53 if (nodea->name == NULL || nodeb->name == NULL) return false;
54
55 printf("Comparing %s to %s\n", nodea->name, nodeb->name);
56 if (strcmp(nodea->name, nodeb->name) == 0)
57 return true;
58 return false;
59}
60
61void count_good_guesses(void* key, void* value, void* userdata) {
62 uint32_t* good_guesses = userdata;
63 Node* node = value;
64
65 (*good_guesses)++;
66 char* s1 = node->name;
67 printf("%u: Found: %p - %s\n", *good_guesses, key, s1);
68}
69
1d8fe1f7
MR
70int main(int argc, char** argv) {
71 if (argc != 3) {
72 printf("usage: %s <wordlist filename> <num guesses>\n", argv[0]);
73 return EXIT_FAILURE;
74 }
75
76 char* filename = argv[1];
77 uint32_t num_guesses = atol(argv[2]);
78
8c5be416 79 Hashtable* table = /*hash_table_create(str_hash, str_equal)*/ hash_table_create_notify(ptr_hash, my_ptr_equal, my_ptr_free);
1d8fe1f7
MR
80
81 FILE* fp = fopen(filename, "r");
82 char* buffer = calloc(sizeof(char), MAX_LINE);
83 uint32_t numwords = 0;
84 uint32_t failed = 0;
85 while (!feof(fp) && fgets(buffer, MAX_LINE, fp) != NULL) {
86 buffer[strcspn(buffer, "\r\n")] = 0;
8c5be416 87/* char* newentry = strdup(buffer);
1d8fe1f7
MR
88 bool result = hash_table_insert(table, newentry, newentry);
89 if (result == false) {
90 printf("Failed [%u] storing %s\n", ++failed, newentry);
91 free(newentry);
92 } else
93 numwords++;
8c5be416
MR
94*/
95 Node* node = calloc(sizeof(Node*), 1);
96 node->name = strdup(buffer);
97 bool result = hash_table_insert(table, node, node);
98 if (result == false) {
99 printf("Failed [%u] storing %s\n", ++failed, node->name);
100 free(node->name);
101 free(node);
102 } else
103 numwords++;
1d8fe1f7 104 }
1d8fe1f7 105 fclose(fp);
8c5be416 106
1d8fe1f7 107 printf("Loaded %d words into the table.\n", numwords);
f3e1ef9f
MR
108 hash_table_print(table);
109 hash_table_set_print_func(table, my_print);
110 hash_table_print(table);
1d8fe1f7 111
8c5be416
MR
112/*
113 uint32_t good_guesses = 0;
114 fp = fopen(filename, "r");
115 void* data = NULL;
116 Node* node = calloc(sizeof(Node*), 1);
117 while (!feof(fp) && fgets(buffer, MAX_LINE, fp) != NULL) {
118 buffer[strcspn(buffer, "\r\n")] = 0;
119 node->name = strdup(buffer);
120 //printf("Buffer: %s\n", node->name);
121 if ((data = hash_table_lookup(table, node)) != NULL) {
122 char* s1 = ((Node*) data)->name;
123 printf("Lookup: %s - Found: %s\n", node->name, s1);
124 good_guesses++;
125 }
126 free(node->name);
127 }
128 free(node);
129
130 fclose(fp);
131*/
132/*
1d8fe1f7
MR
133 //hash_table_print(table);
134 uint32_t good_guesses = 0;
135 const int shortest_guess = 2;
136 const int longest_guess = 15;
137 void* data = NULL;
8c5be416 138 Node* lookup = calloc(sizeof(Node*), 1);
1d8fe1f7
MR
139 for (uint32_t i = 0; i < num_guesses; i++) {
140 generate_random_word(buffer, shortest_guess +
141 (rand() % (longest_guess - shortest_guess)));
8c5be416
MR
142 lookup->name = strdup(buffer);
143 printf("Buffer: %s\n", lookup->name);
144 if ((data = hash_table_lookup(table, lookup)) != NULL) {
145 char* s1 = ((Node*) data)->name;
146 printf("Lookup: %s - Found: %s\n", lookup->name, s1);
1d8fe1f7
MR
147 good_guesses++;
148 }
8c5be416 149 free(lookup->name);
1d8fe1f7 150 }
8c5be416 151 free(lookup);
1d8fe1f7
MR
152
153 printf("%u out of %u guesses were in the table\n", good_guesses, num_guesses);
8c5be416 154*/
1d8fe1f7 155 free(buffer);
8c5be416
MR
156
157 printf("---------------------------------------\n");
158 List* values = hash_table_values(table);
159 List* head = values;
160 size_t i = 1;
161 void* data = NULL;
162 uint32_t good_guesses = 0;
163 while (head != NULL) {
164 Node* d = head->data;
165 printf("%lu: %s\n", i++, (char *) d->name);
166 if ((data = hash_table_lookup(table, d)) != NULL) {
167 char* s1 = ((Node*) data)->name;
168 printf("Lookup: %s - Found: %s\n", d->name, s1);
169 good_guesses++;
170 }
171 head = head->next;
172 }
173 hash_table_list_free(values);
174
175 printf("%u out of %u inserted words in table were in the table\n", good_guesses, numwords);
176
177/*
178 List* keys = hash_table_keys(table);
179 head = keys;
180 data = NULL;
181 good_guesses = 0;
182 i = 1;
183 while (head != NULL) {
184 Node* d = head->data;
185 printf("%lu: %p\n", i++, d->name);
186 if ((data = hash_table_lookup(table, d)) != NULL) {
187 char* s1 = ((Node*) data)->name;
188 printf("Lookup: %s - Found: %s\n", d->name, s1);
189 good_guesses++;
190 }
191 head = head->next;
192 }
193 hash_table_list_free(keys);
194*/
195 good_guesses = 0;
196 hash_table_foreach(table, count_good_guesses, &good_guesses);
197
198 printf("%u out of %u inserted words in table were in the table\n", good_guesses, numwords);
199
200 HashtableIter iter;
dd3da95a
MR
201 void* key;
202 void* value;
8c5be416
MR
203
204 hash_table_iter_init(&iter, table);
205 while (hash_table_iter_next(&iter, &key, &value)) {
206 char* s1 = ((Node*) value)->name;
207 printf("Key: %p value: %s\n", key, s1);
208 }
dd3da95a
MR
209
210 Node* node1 = calloc(sizeof(Node*), 1);
211 node1->name = strdup("Dummy Test Name");
212 if (hash_table_insert(table, node1, node1) == true) {
213 hash_table_iter_init(&iter, table);
214 printf("Hashtable size: %lu. Key found: %d\n", hash_table_size(table), hash_table_contains(table, node1));
215 void* data = hash_table_delete(table, node1);
216 printf("Deleted: [%p] %s\n", node1, ((Node*) data)->name);
217 free(((Node*) data)->name);
218 free(data);
219 printf("Hashtable size: %lu. Key found: %d\n", hash_table_size(table), hash_table_contains(table, node1));
220 Node* node2 = calloc(sizeof(Node*), 1);
221 node2->name = strdup("Test Name");
222 hash_table_replace(table, node2, node2);
223 printf("Hashtable size: %lu. Key found: %d\n", hash_table_size(table), hash_table_contains(table, node2));
224 }
8c5be416 225
1d8fe1f7 226 hash_table_destroy(table);
f3e1ef9f
MR
227
228 table = hash_table_create(str_hash, str_equal);
229 char *names[] = { "Adam", "Eva", "Abraham", "Kain", "Abel", NULL };
230 for (int i = 0; names[i]; i++) {
231 void* name = names[i];
232 hash_table_insert(table, name, strdup(name));
233 }
234 hash_table_iter_init(&iter, table);
235 while (hash_table_iter_next(&iter, &key, &value)) {
236 printf("Key: %p value: %s\n", (char*)key, (char*)value);
237 }
238 data = hash_table_delete(table, (void*) names[4]);
239 printf("Deleted: [%p] %s\n", data, (char*) data);
240 free(data);
241 hash_table_replace(table, (void*) names[4], strdup(names[4]));
242 hash_table_replace(table, (void*) names[4], strdup("Johannes"));
243 hash_table_iter_init(&iter, table);
244 while (hash_table_iter_next(&iter, &key, &value)) {
245 printf("Key: %p value: %s\n", (char*)key, (char*)value);
246 }
247
248 hash_table_destroy(table);
1d8fe1f7 249}
This page took 0.064368 seconds and 5 git commands to generate.