END_TEST START_TEST(test_count) { xhash_t* ht = g_ht; hashable_t a[4] = {{"0", 0}, {"1", 1}, {"2", 2}, {"3", 3}}; fail_unless(xhash_count(ht) == g_hashableslen, "invalid count (fixture table)"); ht = xhash_init(hashable_identify, NULL, NULL, 0); xhash_add(ht, a); xhash_add(ht, a+1); xhash_add(ht, a+2); xhash_add(ht, a+3); fail_unless(xhash_count(ht) == 4, "invalid count (fresh table)"); xhash_free(ht); }
static void setup(void) { int i; g_ht = xhash_init(hashable_identify, NULL, NULL, 0); if (!g_ht) return; /* fatal error, will be detected by test cases */ for (i = 0; i < g_hashableslen; ++i) { g_hashables[i].id[0] = 0; g_hashables[i].idn = i; /* it is an error if xhash_add returns null but it will be * detected by test cases */ if (!xhash_add(g_ht, g_hashables + i)) return; } }
/* * rehash_node - build a hash table of the node_record entries. * NOTE: using xhash implementation */ extern void rehash_node (void) { int i; struct node_record *node_ptr = node_record_table_ptr; xhash_free (node_hash_table); node_hash_table = xhash_init(node_record_hash_identity, NULL, NULL, 0); for (i = 0; i < node_record_count; i++, node_ptr++) { if ((node_ptr->name == NULL) || (node_ptr->name[0] == '\0')) continue; /* vestigial record */ xhash_add(node_hash_table, node_ptr); } #if _DEBUG _dump_hash(); #endif return; }
int entity_add_data(entity_t* entity, const char* key, void* value, void (*_free)(void*)) { entity_data_t* result; entity_data_t* new_data_item; if (!key || !*key || !value) return 0; result = (entity_data_t*)xhash_get(entity->data, key); if (result != NULL) { if (_free) _free(result->value); result->value = value; return 1; } new_data_item = (entity_data_t*)xmalloc(sizeof(entity_data_t)); new_data_item->key = key; new_data_item->value = value; result = xhash_add(entity->data, new_data_item); if (result == NULL) { xfree(new_data_item); return 0; } return 1; }
END_TEST START_TEST(test_add) { xhash_t* ht = NULL; hashable_t a[4] = {{"0", 0}, {"1", 1}, {"2", 2}, {"3", 3}}; int i, len = sizeof(a)/sizeof(a[0]); char buffer[255]; ht = xhash_init(hashable_identify, NULL, NULL, 0); fail_unless(xhash_add(NULL, a) == NULL, "invalid cases not null"); fail_unless(xhash_add(ht, NULL) == NULL, "invalid cases not null"); fail_unless(xhash_add(ht, a) != NULL, "xhash_add failed"); fail_unless(xhash_add(ht, a+1) != NULL, "xhash_add failed"); fail_unless(xhash_add(ht, a+2) != NULL, "xhash_add failed"); fail_unless(xhash_add(ht, a+3) != NULL, "xhash_add failed"); for (i = 0; i < len; ++i) { snprintf(buffer, sizeof(buffer), "%d", i); fail_unless(xhash_get(ht, buffer) == (a + i), "bad hashable item returned"); } xhash_free(ht); }