/* * test strmap from http://pokristensson.com/strmap.html * * $ gcc test_strmap.c strmap.c jims_utils.c -o test_strmap * $ ./test_strmap * */ #include "strmap.h" #include "jims_utils.h" #include #include // string of nth alphabet letter, i.e. nth_letter(3) is "c" char* nth_alpha(int n){ char* letter = calloc(4, sizeof(char)); letter[0] = letter[1] = (char)((int)'a' + n); return letter; } // iteration callback for loop over hash table. static void iter(const char *key, const char *value, const void *obj){ printf(" key: %s value: %s\n", key, value); } int main(){ StrMap *strmap; char buf[255]; int result; char* letter, number; char* c = "c"; int hash_size = 10; int n_pairs = 9; int i; printf("-- making hash table with %i slots -- \n", hash_size); strmap = strmap_new(10); if (strmap == NULL){ printf("error creating hash table\n"); exit(1); } printf("-- inserting %i (aa,1) (bb,2) ... pairs --\n", n_pairs); for (i=0; i < n_pairs; i++){ strmap_put(strmap, nth_alpha(i), itoa(i+1)); } printf("-- number of pairs in hash table = %i -- \n", strmap_get_count(strmap)); printf("-- number of collisions in hash table = %i -- \n", strmap_get_collisions(strmap)); printf("-- looping over hash table, printing out key,value --\n"); strmap_enum(strmap, iter, NULL); printf("-- dumping hash table --\n"); strmap_dump(strmap); printf("-- fetching value for \"%s\" --\n", c); result = strmap_get(strmap, c, buf, sizeof(buf)); if (result == 0){ printf("error fetching \"%s\" \n", c); exit(0); } printf("value of \"%s\" is \"%s\"\n", c, buf); exit(0); }