void test_put_remove() { hash_map_put(map, "abcd", "the alphabet"); hash_map_put(map, "1234", "some numbers"); hash_map_remove(map, "abcd"); hash_map_remove(map, "1234"); TEST_ASSERT_NULL(hash_map_get(map, "abcd")); TEST_ASSERT_NULL(hash_map_get(map, "1234")); hash_map_put(map, "abcd", "try it again"); TEST_ASSERT_EQUAL_STRING("try it again", hash_map_get(map, "abcd")); }
int test_add_get_remove(struct harness_t *harness) { struct hash_map_t map; struct hash_map_bucket_t buckets[8]; struct hash_map_entry_t entries[4]; hash_map_init(&map, buckets, membersof(buckets), entries, membersof(entries), hash); /* Add three entries. */ BTASSERT(hash_map_add(&map, 37, (void *)34) == 0); BTASSERT(hash_map_add(&map, 38, (void *)35) == 0); BTASSERT(hash_map_add(&map, 39, (void *)36) == 0); BTASSERT(hash_map_add(&map, 39, (void *)36) == 0); /* Get them. */ BTASSERT(hash_map_get(&map, 38) == (void *)35); BTASSERT(hash_map_get(&map, 39) == (void *)36); BTASSERT(hash_map_get(&map, 37) == (void *)34); /* Remove first two. */ BTASSERT(hash_map_remove(&map, 37) == 0); BTASSERT(hash_map_remove(&map, 38) == 0); BTASSERT(hash_map_remove(&map, 38) == -1); /* Get removed entries. */ BTASSERT(hash_map_get(&map, 37) == NULL); BTASSERT(hash_map_get(&map, 38) == NULL); /* Get, remove and get last entry. */ BTASSERT(hash_map_get(&map, 39) == (void *)36); BTASSERT(hash_map_remove(&map, 39) == 0); BTASSERT(hash_map_remove(&map, 39) == -1); BTASSERT(hash_map_get(&map, 39) == NULL); /* Add one entry over limit. */ BTASSERT(hash_map_add(&map, 37, (void *)4) == 0); BTASSERT(hash_map_add(&map, 39, (void *)5) == 0); BTASSERT(hash_map_add(&map, 41, (void *)6) == 0); BTASSERT(hash_map_add(&map, 43, (void *)7) == 0); BTASSERT(hash_map_add(&map, 45, (void *)8) == -ENOMEM); return (0); }
void task_processor_remove_task_queue( task_processor* processor, task_queue* queue ) { hash_map_iterator iter; event_group_element_type group_element = NULL; synchronize_lock_mutex( processor->mutex ); HASH_MAP_ITERATOR_START( iter, &processor->task_queues ); while ( HASH_MAP_ITERATOR_IS_VALID( iter ) ) { if ( queue == *(task_queue**)hash_map_get( &processor->task_queues, HASH_MAP_ITERATOR_GET( iter ) ) ) { hash_map_remove( &processor->task_queues, HASH_MAP_ITERATOR_GET( iter ) ); synchronize_lock_mutex( queue->mutex ); LIST_ITERATOR_START( iter, &queue->event_list ); while ( LIST_ITERATOR_IS_VALID( iter ) ) { group_element = *(event_group_element_type*)LIST_ITERATOR_GET( iter ); if ( synchronize_event_group_get_group_by_element( group_element ) == processor->event_group ) { list_remove( &queue->event_list, iter ); break; } LIST_ITERATOR_NEXT( iter ); } synchronize_unlock_mutex( queue->mutex ); ASSERT( synchronize_event_group_get_group_by_element( group_element ) == processor->event_group ); synchronize_event_group_remove_element( processor->event_group, group_element ); break; } HASH_MAP_ITERATOR_NEXT( iter ); } synchronize_unlock_mutex( processor->mutex ); }
boolean task_processor_add_task_queue( task_processor* processor, task_queue* queue ) { boolean ret = FALSE; event_group_element_type group_element; ASSERT( NULL != queue ); synchronize_lock_mutex( processor->mutex ); do { group_element = synchronize_event_group_add_element( processor->event_group ); if ( NULL == group_element ) { ret = FALSE; break; } ret = hash_map_insert( &processor->task_queues, &group_element, &queue ); if ( !ret ) { synchronize_event_group_remove_element( processor->event_group, group_element ); break; } synchronize_lock_mutex( queue->mutex ); ret = list_push_back( &queue->event_list, &group_element ); synchronize_unlock_mutex( queue->mutex ); if ( !ret ) { hash_map_remove( &processor->task_queues, &group_element ); synchronize_event_group_remove_element( processor->event_group, group_element ); break; } } while (FALSE); synchronize_unlock_mutex( processor->mutex ); return ret; }
void test_size() { TEST_ASSERT_EQUAL_UINT(0, hash_map_size(map)); hash_map_put(map, "key", "value"); TEST_ASSERT_EQUAL_UINT(1, hash_map_size(map)); hash_map_put(map, "key2", "value"); TEST_ASSERT_EQUAL_UINT(2, hash_map_size(map)); // if the same key was updated, size should not change hash_map_put(map, "key", "value2"); TEST_ASSERT_EQUAL_UINT(2, hash_map_size(map)); // if hashs collide, size should still work hash_map_put(map, "1234567890", "9090"); hash_map_put(map, "1234567809", "0909"); TEST_ASSERT_EQUAL_UINT(4, hash_map_size(map)); hash_map_remove(map, "key"); hash_map_remove(map, "key2"); hash_map_remove(map, "1234567890"); hash_map_remove(map, "1234567809"); TEST_ASSERT_EQUAL_UINT(0, hash_map_size(map)); }
list* search_a_star(void* state, void* state_world, search_is_goal state_goal_func, search_gen_successors state_gen_func, search_link_parent state_link_func, search_goal_backtrace state_back_func, search_trans_cost state_trans_func, search_heuristic state_heur_func, search_set_f_cost state_f_cost_set_func, hash_func state_hash_alg, generic_comp state_comp_func, generic_cpy state_copy_func, generic_op state_free_func, heap_comp state_heap_func) { int* g_cost_ptr, *f_cost_ptr, f_cost, tmp_f, g_cost, found; void* current_state, *successor_state, *heap_memory_location; list* states_overflow, *successor_list, *path; hash_table* states_closed_set, *states_open_set; hash_map* states_g_cost, *states_f_cost, *states_heap_index; heap* states_heap; states_overflow = list_create(NULL, NULL, state_free_func); states_closed_set = hash_table_create(89, .75, state_hash_alg, state_comp_func, state_copy_func, state_free_func); states_open_set = hash_table_create(89, .75, state_hash_alg, state_comp_func, state_copy_func, state_free_func); states_g_cost = hash_map_create(89, .75, state_hash_alg, state_comp_func, NULL, NULL, NULL, state_free_func, (generic_op)free); states_f_cost = hash_map_create(89, .75, state_hash_alg, state_comp_func, NULL, NULL, NULL, state_free_func, (generic_op)free); states_heap_index = hash_map_create(89, .75, state_hash_alg, state_comp_func, NULL, NULL, NULL, NULL, NULL); states_heap = heap_create(89, state_heap_func, state_comp_func, state_copy_func, state_free_func); current_state = state; f_cost = state_heur_func(current_state, NULL); state_f_cost_set_func(current_state, f_cost); g_cost = 0; g_cost_ptr = malloc(sizeof(int)); *g_cost_ptr = g_cost; f_cost_ptr = malloc(sizeof(int)); *f_cost_ptr = f_cost; hash_map_insert(states_g_cost, current_state, g_cost_ptr, 0); heap_memory_location = heap_add(states_heap, state_copy_func(current_state)); hash_table_insert(states_open_set, state_copy_func(current_state), 0); hash_map_insert(states_f_cost, state_copy_func(current_state), f_cost_ptr, 0); hash_map_insert(states_heap_index, current_state, heap_memory_location, 1); path = NULL; found = 0; while(!heap_is_empty(states_heap) && !found) { current_state = state_copy_func(heap_peek(states_heap)); heap_remove(states_heap); hash_table_remove(states_open_set, current_state); hash_map_remove(states_heap_index, current_state); if(state_goal_func(current_state, state_world)) { path = state_back_func(current_state); found = 1; } else { if(!hash_table_insert(states_closed_set, current_state, 0)) { list_push_front(states_overflow, current_state); } successor_list = state_gen_func(current_state, state_world); while(!list_is_empty(successor_list)) { successor_state = list_front(successor_list); g_cost = *(int*)hash_map_get(states_g_cost, current_state) + state_trans_func(current_state, successor_state, state_world); f_cost = g_cost + state_heur_func(successor_state, state_world); tmp_f = hash_map_contains_key(states_f_cost, successor_state) ? *(int*)hash_map_get(states_f_cost, successor_state) : UINT_MAX; if(hash_table_contains(states_closed_set, successor_state) && f_cost > tmp_f) { list_remove_front(successor_list); continue; } if(!hash_table_contains(states_open_set, successor_state) || f_cost < tmp_f) { state_f_cost_set_func(successor_state, f_cost); state_link_func(successor_state, current_state); g_cost_ptr = malloc(sizeof(int)); f_cost_ptr = malloc(sizeof(int)); *g_cost_ptr = g_cost; *f_cost_ptr = f_cost; if(!hash_table_contains(states_open_set, successor_state)) { hash_table_insert(states_open_set, successor_state, 0); heap_memory_location = heap_add(states_heap, state_copy_func(successor_state)); hash_map_insert(states_heap_index, successor_state, heap_memory_location, 1); } else { heap_memory_location = hash_map_get(states_heap_index, successor_state); heap_up_mod_data(states_heap, heap_memory_location, successor_state); } if(!hash_map_set(states_g_cost, successor_state, g_cost_ptr)) { hash_map_insert(states_g_cost, state_copy_func(successor_state), g_cost_ptr, 0); } if(!hash_map_set(states_f_cost, successor_state, f_cost_ptr)) { hash_map_insert(states_f_cost, state_copy_func(successor_state), f_cost_ptr, 0); } list_pop(successor_list); } else { list_remove_front(successor_list); } } list_kill(successor_list); } } heap_kill(states_heap); list_kill(states_overflow); hash_map_kill(states_g_cost); hash_map_kill(states_f_cost); hash_table_kill(states_open_set); hash_table_kill(states_closed_set); hash_map_dissolve(states_heap_index); return path; }
void explore_node_map_remove_node_with_key( explore_node_map* map, const void* key ) { hash_map_remove( &map->nodes, key ); }
void test_remove_non_existent() { hash_map_remove(map, "not here"); TEST_ASSERT_EQUAL_UINT(0, hash_map_size(map)); }