HashTable *generate_hash_table(void) { HashTable *hash_table; char buf[10]; char *value; int i; /* Allocate a new hash table. We use a hash table with keys that are * string versions of the integer values 0..9999 to ensure that there * will be collisions within the hash table (using integer values * with int_hash causes no collisions) */ hash_table = hash_table_new(string_hash, string_equal); /* Insert lots of values */ for (i=0; i<NUM_TEST_VALUES; ++i) { sprintf(buf, "%i", i); value = strdup(buf); hash_table_insert(hash_table, value, value); } /* Automatically free all the values with the hash table */ hash_table_register_free_functions(hash_table, NULL, free); return hash_table; }
void test_hash_table_free_functions(void) { HashTable *hash_table; int *key; int *value; int i; /* Create a hash table, fill it with values */ hash_table = hash_table_new(int_hash, int_equal); hash_table_register_free_functions(hash_table, free_key, free_value); allocated_values = 0; for (i=0; i<NUM_TEST_VALUES; ++i) { key = new_key(i); value = new_value(99); hash_table_insert(hash_table, key, value); } assert(allocated_keys == NUM_TEST_VALUES); assert(allocated_values == NUM_TEST_VALUES); /* Check that removing a key works */ i = NUM_TEST_VALUES / 2; hash_table_remove(hash_table, &i); assert(allocated_keys == NUM_TEST_VALUES - 1); assert(allocated_values == NUM_TEST_VALUES - 1); /* Check that replacing an existing key works */ key = new_key(NUM_TEST_VALUES / 3); value = new_value(999); assert(allocated_keys == NUM_TEST_VALUES); assert(allocated_values == NUM_TEST_VALUES); hash_table_insert(hash_table, key, value); assert(allocated_keys == NUM_TEST_VALUES - 1); assert(allocated_values == NUM_TEST_VALUES - 1); /* A free of the hash table should free all of the keys and values */ hash_table_free(hash_table); assert(allocated_keys == 0); assert(allocated_values == 0); }
void la_initialize(la_UUID_t seed) { assert(la_buffer_table == LA_NULL); la_buffer_table = hash_table_new( &__la_hash_buffer_table_key, &__la_compare_buffer_table_keys); assert(la_buffer_table != LA_NULL); hash_table_register_free_functions( la_buffer_table, &__la_free_buffer_table_key, &__la_free_buffer_table_value); LOGD("%lu (successfully created buffer table)", seed); }
void _cwt_colorx_init(void) { CwtStrNS *strNS = cwt_str_ns(); size_t i; size_t n; __cwt_color_table = hash_table_new(string_hash, string_equal); hash_table_register_free_functions(__cwt_color_table, cwt_free, NULL); n = sizeof(__cwt_color_names)/sizeof(__cwt_color_names[0]); for( i = 0; i < n; i++ ) { CWT_CHAR *key = strNS->strDup(__cwt_color_names[i].name); strNS->toLowerStr(key, key, strNS->strLen(key)); CWT_ASSERT(hash_table_insert(__cwt_color_table , key, &__cwt_color_names[i].color)); } }