]> git.datanom.net - hashtable.git/blobdiff - test.c
almost complete
[hashtable.git] / test.c
diff --git a/test.c b/test.c
index 5259e7e81932cad1b7f5778fd8adc471af4919c0..8cf65c47d6f8d355bfbe7b806dde811b45d246d0 100644 (file)
--- a/test.c
+++ b/test.c
@@ -2,13 +2,19 @@
 #include <stdlib.h>
 #include <string.h>
 #include <inttypes.h>
+#include <stdbool.h>
 #include "hashtable.h"
 
 #define MAX_LINE 4096
 
-uint64_t word_hash_func(const char* name, size_t length) {
+typedef struct _Node {
+       char* name;
+} Node;
+
+uint64_t word_hash_func(const void* o) {
        uint64_t hash_value = 0;
-       for (size_t i = 0; i < length; i++) {
+       const char* name = o;
+       for (size_t i = 0; i < strlen(name); i++) {
                hash_value += name[i];
                hash_value = hash_value * name[i];
        }
@@ -22,6 +28,40 @@ void generate_random_word(char* buffer, size_t length) {
        buffer[length - 1] = 0;
 }
 
+void my_str_free(void* o) {
+       printf("Freeing %p\n", o);
+       free(o);
+}
+
+void my_ptr_free(void* o) {
+       Node* node = o;
+       //printf("Freeing %p\n", node);
+       free(node->name);
+       free(node);
+}
+
+bool my_ptr_equal(const void* a, const void* b) {
+       const Node* nodea = a;
+       const Node* nodeb = b;
+
+       if (nodea == NULL || nodeb == NULL) return false;
+       if (nodea->name == NULL || nodeb->name == NULL) return false;
+
+       printf("Comparing %s to %s\n", nodea->name, nodeb->name);
+       if (strcmp(nodea->name, nodeb->name) == 0)
+               return true;
+       return false;
+}
+
+void count_good_guesses(void* key, void* value, void* userdata) {
+       uint32_t* good_guesses = userdata;
+       Node* node = value;
+
+       (*good_guesses)++;
+       char* s1 = node->name;
+       printf("%u: Found: %p - %s\n", *good_guesses, key, s1);
+}
+
 int main(int argc, char** argv) {
        if (argc != 3) {
                printf("usage: %s <wordlist filename> <num guesses>\n", argv[0]);
@@ -31,7 +71,7 @@ int main(int argc, char** argv) {
        char* filename = argv[1];
        uint32_t num_guesses = atol(argv[2]);
 
-       Hashtable* table = hash_table_create(str_hash, str_equal);
+       Hashtable* table = /*hash_table_create(str_hash, str_equal)*/ hash_table_create_notify(ptr_hash, my_ptr_equal, my_ptr_free);
 
        FILE* fp = fopen(filename, "r");
        char* buffer = calloc(sizeof(char), MAX_LINE);
@@ -39,35 +79,127 @@ int main(int argc, char** argv) {
        uint32_t failed = 0;
        while (!feof(fp) && fgets(buffer, MAX_LINE, fp) != NULL) {
                buffer[strcspn(buffer, "\r\n")] = 0;
-               char* newentry = malloc(strlen(buffer) + 1);
-               strcpy(newentry, buffer);
+/*             char* newentry = strdup(buffer);
                bool result = hash_table_insert(table, newentry, newentry);
                if (result == false) {
                        printf("Failed [%u] storing %s\n", ++failed, newentry);
                        free(newentry);
                } else
                        numwords++;
+*/
+               Node* node = calloc(sizeof(Node*), 1);
+               node->name = strdup(buffer);
+               bool result = hash_table_insert(table, node, node);
+               if (result == false) {
+                       printf("Failed [%u] storing %s\n", ++failed, node->name);
+                       free(node->name);
+                       free(node);
+               } else
+                       numwords++;
        }
-
        fclose(fp);
+
        printf("Loaded %d words into the table.\n", numwords);
 
+/*
+       uint32_t good_guesses = 0;
+       fp = fopen(filename, "r");
+       void* data = NULL;
+       Node* node = calloc(sizeof(Node*), 1);
+       while (!feof(fp) && fgets(buffer, MAX_LINE, fp) != NULL) {
+               buffer[strcspn(buffer, "\r\n")] = 0;
+               node->name = strdup(buffer);
+               //printf("Buffer: %s\n", node->name);
+               if ((data = hash_table_lookup(table, node)) != NULL) {
+                       char* s1 = ((Node*) data)->name;
+                       printf("Lookup: %s - Found: %s\n", node->name, s1);
+                       good_guesses++;
+               }
+               free(node->name);
+       }
+       free(node);
+
+       fclose(fp);
+*/
+/*
        //hash_table_print(table);
        uint32_t good_guesses = 0;
        const int shortest_guess = 2;
        const int longest_guess = 15;
        void* data = NULL;
+       Node* lookup = calloc(sizeof(Node*), 1);
        for (uint32_t i = 0; i < num_guesses; i++) {
                generate_random_word(buffer, shortest_guess +
                        (rand() % (longest_guess - shortest_guess)));
-               if ((data = hash_table_lookup(table, buffer)) != NULL) {
-                       printf("Lookup: %s - Found: %s\n", buffer, (char*) data);
+               lookup->name = strdup(buffer);
+               printf("Buffer: %s\n", lookup->name);
+               if ((data = hash_table_lookup(table, lookup)) != NULL) {
+                       char* s1 = ((Node*) data)->name;
+                       printf("Lookup: %s - Found: %s\n", lookup->name, s1);
                        good_guesses++;
                }
+               free(lookup->name);
        }
+       free(lookup);
 
        printf("%u out of %u guesses were in the table\n", good_guesses, num_guesses);
-
+*/
        free(buffer);
+
+       printf("---------------------------------------\n");
+       List* values = hash_table_values(table);
+       List* head = values;
+       size_t i = 1;
+       void* data = NULL;
+       uint32_t good_guesses = 0;
+       while (head != NULL) {
+               Node* d = head->data;
+               printf("%lu: %s\n", i++, (char *) d->name);
+               if ((data = hash_table_lookup(table, d)) != NULL) {
+                       char* s1 = ((Node*) data)->name;
+                       printf("Lookup: %s - Found: %s\n", d->name, s1);
+                       good_guesses++;
+               }
+               head = head->next;
+       }
+       hash_table_list_free(values);
+
+       printf("%u out of %u inserted words in table were in the table\n", good_guesses, numwords);
+
+/*
+       List* keys = hash_table_keys(table);
+       head = keys;
+       data = NULL;
+       good_guesses = 0;
+       i = 1;
+       while (head != NULL) {
+               Node* d = head->data;
+               printf("%lu: %p\n", i++, d->name);
+               if ((data = hash_table_lookup(table, d)) != NULL) {
+                       char* s1 = ((Node*) data)->name;
+                       printf("Lookup: %s - Found: %s\n", d->name, s1);
+                       good_guesses++;
+               }
+               head = head->next;
+       }
+       hash_table_list_free(keys);
+*/
+       good_guesses = 0;
+       hash_table_foreach(table, count_good_guesses, &good_guesses);
+
+       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*))*/;
+
+       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);
+
        hash_table_destroy(table);
 }
This page took 0.035904 seconds and 5 git commands to generate.