]> git.datanom.net - hashtable.git/blobdiff - test.c
print function
[hashtable.git] / test.c
diff --git a/test.c b/test.c
index 8cf65c47d6f8d355bfbe7b806dde811b45d246d0..a9a08c05256a8d0222bd9ab328eb6faad0e047ea 100644 (file)
--- a/test.c
+++ b/test.c
@@ -40,6 +40,11 @@ void my_ptr_free(void* o) {
        free(node);
 }
 
+void my_print(void* o) {
+       Node* n = (Node *) o;
+       printf("%s", n->name);
+}
+
 bool my_ptr_equal(const void* a, const void* b) {
        const Node* nodea = a;
        const Node* nodeb = b;
@@ -100,6 +105,9 @@ int main(int argc, char** argv) {
        fclose(fp);
 
        printf("Loaded %d words into the table.\n", numwords);
+       hash_table_print(table);
+       hash_table_set_print_func(table, my_print);
+       hash_table_print(table);
 
 /*
        uint32_t good_guesses = 0;
@@ -190,16 +198,52 @@ int main(int argc, char** argv) {
        printf("%u out of %u inserted words in table were in the table\n", good_guesses, numwords);
 
        HashtableIter iter;
-       void* key/* = malloc(sizeof(void*))*/;
-       void* value/* = malloc(sizeof(void*))*/;
+       void* key;
+       void* value;
 
        hash_table_iter_init(&iter, table);
        while (hash_table_iter_next(&iter, &key, &value)) {
                char* s1 = ((Node*) value)->name;
                printf("Key: %p value: %s\n", key, s1);
        }
-       //free(key);
-       //free(value);
+
+       Node* node1 = calloc(sizeof(Node*), 1);
+       node1->name = strdup("Dummy Test Name");
+       if (hash_table_insert(table, node1, node1) == true) {
+               hash_table_iter_init(&iter, table);
+               printf("Hashtable size: %lu. Key found: %d\n", hash_table_size(table), hash_table_contains(table, node1));
+               void* data = hash_table_delete(table, node1);
+               printf("Deleted: [%p] %s\n", node1, ((Node*) data)->name);
+               free(((Node*) data)->name);
+               free(data);
+               printf("Hashtable size: %lu. Key found: %d\n", hash_table_size(table), hash_table_contains(table, node1));
+               Node* node2 = calloc(sizeof(Node*), 1);
+               node2->name = strdup("Test Name");
+               hash_table_replace(table, node2, node2);
+               printf("Hashtable size: %lu. Key found: %d\n", hash_table_size(table), hash_table_contains(table, node2));
+       }
+
+       hash_table_destroy(table);
+
+       table = hash_table_create(str_hash, str_equal);
+       char *names[] = { "Adam", "Eva", "Abraham", "Kain", "Abel", NULL };
+       for (int i = 0; names[i]; i++) {
+               void* name = names[i];
+               hash_table_insert(table, name, strdup(name));
+       }
+       hash_table_iter_init(&iter, table);
+       while (hash_table_iter_next(&iter, &key, &value)) {
+               printf("Key: %p value: %s\n", (char*)key, (char*)value);
+       }
+       data = hash_table_delete(table, (void*) names[4]);
+       printf("Deleted: [%p] %s\n", data, (char*) data);
+       free(data);
+       hash_table_replace(table, (void*) names[4], strdup(names[4]));
+       hash_table_replace(table, (void*) names[4], strdup("Johannes"));
+       hash_table_iter_init(&iter, table);
+       while (hash_table_iter_next(&iter, &key, &value)) {
+               printf("Key: %p value: %s\n", (char*)key, (char*)value);
+       }
 
        hash_table_destroy(table);
 }
This page took 0.030516 seconds and 5 git commands to generate.