bt_debug("%d (%.2f %%) chained of %d hashes \n",num_stacked_items,percent_stacked_items,MAX_NUM);
}
staticvoid
dump_nodes(void)
{
inti;
for(i=0;i<MAX_NUM;i++)
bt_debug("nodes[%3d] is at address %14p has .key %3d, .next %14p \n",i,&nodes[i],nodes[i].key,nodes[i].next);
}
staticvoid
init_hash_(uintorder)
{
resource_init();
my_pool=rp_new(&root_pool,"Test pool");
HASH_INIT(hash,my_pool,order);
inti;
for(i=0;i<MAX_NUM;i++)
{
nodes[i].key=i;
nodes[i].next=NULL;
}
bt_debug("MAX_NUM %d \n",MAX_NUM);
}
staticvoid
init_hash(void)
{
init_hash_(TEST_INIT_ORDER);
}
staticvoid
validate_filled_hash(void)
{
inti;
structtest_node*node;
for(i=0;i<MAX_NUM;i++)
{
node=HASH_FIND(hash,TEST,nodes[i].key);
bt_assert_msg(node==&nodes[i],"Hash should be filled, but we not find (%p) the node[%d] (%p) with .key = %u, .next %p \n",node,i,&nodes[i],nodes[i].key,nodes[i].next);
}
print_rate_of_fulfilment();
}
staticvoid
validate_empty_hash(void)
{
inti;
structtest_node*node;
for(i=0;i<MAX_NUM;i++)
{
node=HASH_FIND(hash,TEST,nodes[i].key);
bt_assert_msg(node==NULL,"Hash should be empty, but we find (%p) the node[%d] (%p) with .key %u, .next %p \n",node,i,&nodes[i],nodes[i].key,nodes[i].next);
}
}
staticvoid
fill_hash(void)
{
inti;
structtest_node*node;
for(i=0;i<MAX_NUM;i++)
{
nodes[i].key=i;
node=&nodes[i];
HASH_INSERT(hash,TEST,node);
}
}
staticint
t_insert_find(void)
{
init_hash();
fill_hash();
validate_filled_hash();
returnBT_SUCCESS;
}
staticint
t_insert_find_random(void)
{
init_hash();
inti;
structtest_node*node;
for(i=0;i<MAX_NUM;i++)
{
nodes[i].key=bt_rand_num();
node=&nodes[i];
HASH_INSERT(hash,TEST,node);
}
validate_filled_hash();
returnBT_SUCCESS;
}
staticint
t_insert2_find(void)
{
init_hash_(1);
inti;
structtest_node*node;
for(i=0;i<MAX_NUM;i++)
{
nodes[i].key=i;
node=&nodes[i];
HASH_INSERT2(hash,TEST,my_pool,node);
}
bt_assert_msg(hash.order!=1,"Surprisingly the hash did not auto-resize from order 2^1. The order of the hash is 2^%u.",hash.order);
validate_filled_hash();
returnBT_SUCCESS;
}
staticint
t_walk(void)
{
init_hash();
fill_hash();
uinti;
uintcheck[MAX_NUM];
for(i=0;i<MAX_NUM;i++)
check[i]=0;
HASH_WALK(hash,next,n)
{
check[n->key]++;
}
HASH_WALK_END;
for(i=0;i<MAX_NUM;i++)
bt_assert(check[i]==1);
returnBT_SUCCESS;
}
staticint
t_walk_delsafe_delete(void)
{
init_hash();
fill_hash();
HASH_WALK_DELSAFE(hash,next,n)
{
HASH_DELETE(hash,TEST,n->key);
}
HASH_WALK_DELSAFE_END;
validate_empty_hash();
returnBT_SUCCESS;
}
staticint
t_walk_delsafe_remove(void)
{
init_hash();
fill_hash();
HASH_WALK_DELSAFE(hash,next,n)
{
HASH_REMOVE(hash,TEST,n);
}
HASH_WALK_DELSAFE_END;
validate_empty_hash();
returnBT_SUCCESS;
}
staticint
t_walk_delsafe_delete2(void)
{
init_hash();
fill_hash();
HASH_WALK_DELSAFE(hash,next,n)
{
// HASH_DELETE2(hash, TEST, pool, n->key); TODO: UNCOMMENT
bt_debug("order: %u \n",hash.order);
}
HASH_WALK_DELSAFE_END;
validate_empty_hash();
returnBT_SUCCESS;
}
staticint
t_walk_delsafe_remove2(void)
{
init_hash();
fill_hash();
HASH_WALK_DELSAFE(hash,next,n)
{
// HASH_REMOVE2(hash, TEST, pool, n); TODO: UNCOMMENT