/* * main.c * This is my linked list and hash table class. I probably should break them out into separate files with headers * Created on: Aug 25, 2009 * Author: bkaplan */ #include #include #include #include #include "list.h" #include "cmp.h" /*searches the list for the given key and returns the first value found associated with that key. *returns the value associated with that key or NULL if no node uses that key *precondition: list != NULL */ void test_cmp_string(void) { char* ref = "eecs"; char* first = "eecs"; char* second = "EECS"; char* third = "eec"; // char* null = NULL; assert(!cmp_string(ref,first)); assert(cmp_string(ref,second)); assert(cmp_string(ref, third)); } void test_list(void) { /**test the first list: doubles for keys and values * */ list_head_t *l = list_init(cmp_double); double one_db = 1.0; double *one; double two_db = 2.0; double *two; one = &one_db; two = &two_db; list_insert(l,one, two); double three_db = 3.0; double four_db = 4.0; list_insert(l, &three_db, &four_db); double five_db = 5.0; double six_db = 6.0; list_insert(l, &five_db, &six_db); printf("search(1): %f\n", *((double*)list_search(l,one))); printf("search(3): %f\n", *((double*)list_search(l,&three_db))); double seven_db = 7.0; list_insert(l, &three_db, &seven_db); double eight_db = 8.0; printf("search(8) is null: %u\n", list_search(l, &eight_db) == NULL); printf("search(3): %f\n",*((double*)list_search(l,&three_db))); /**test the second list: strings as keys, doubles as values * */ list_head_t *l2 = list_init(cmp_string); list_insert(l2, "one", &two_db); list_insert(l2, "two",&four_db); list_insert(l2,"three",&six_db); printf("search(\"one\") for l2: %f\n",*((double*)list_search(l2,"one"))); printf("search(\"three\") for l2: %f\n", *((double*)list_search(l2,"three"))); printf("search(\"five\") for l2 is null: %p\n", list_search(l2, "five")); list_insert(l2, "three",&seven_db); printf("search(\"three\") for l2: %f\n",*((double*)list_search(l2,"three"))); printf("search(\"eight\") for l2: %p (0 is NULL)\n",list_search(l2, "eight")); list_delete(l2); list_delete(l); } /**This is my test for the hash table*/ void test_hash() { hash_head_t *table = hash_init(&hash_string, &cmp_string); printf("hash hello: %d\n", hash_string("hello")); hash_insert(table, "hello", "world"); printf("hash get: %s\n", (char*)hash_search(table, "hello")); assert(!cmp_string(hash_search(table,"hello"),"world")); hash_insert(table, "lehlo","dlrow"); printf("hash get2: %s\n", (char*)hash_search(table,"lehlo")); hash_insert(table, "whatever I want\nbecause I can", "abcdefg"); printf("hash get3: %s\n",(char*)hash_search(table,"whatever I want\nbecause I can")); printf("hash string: %d\n",hash_string("whatever I want\nbecause I can")); hash_delete(table); }