DECLARE_TEST( hashmap, erase ) { hashmap_t* map = hashmap_allocate( 0, 0 ); void* prev = 0; EXPECT_EQ( hashmap_lookup( map, 0 ), 0 ); EXPECT_EQ( hashmap_size( map ), 0 ); prev = hashmap_insert( map, 0, map ); EXPECT_EQ( prev, 0 ); EXPECT_EQ( hashmap_size( map ), 1 ); EXPECT_TRUE( hashmap_has_key( map, 0 ) ); prev = hashmap_erase( map, 0 ); EXPECT_EQ( prev, map ); EXPECT_EQ( hashmap_size( map ), 0 ); EXPECT_FALSE( hashmap_has_key( map, 0 ) ); prev = hashmap_erase( map, 0 ); EXPECT_EQ( prev, 0 ); EXPECT_EQ( hashmap_size( map ), 0 ); EXPECT_FALSE( hashmap_has_key( map, 0 ) ); prev = hashmap_erase( map, (hash_t)(uintptr_t)map ); EXPECT_EQ( prev, 0 ); EXPECT_EQ( hashmap_size( map ), 0 ); EXPECT_FALSE( hashmap_has_key( map, (hash_t)(uintptr_t)map ) ); hashmap_deallocate( map ); return 0; }
DECLARE_TEST( hashmap, lookup ) { hashmap_t* map = hashmap_allocate( 31, 0 ); char* value = (void*)(uintptr_t)1234; hash_t key = (hash_t)4321; unsigned int ikey = 0; for( ; ikey < 1024; ++ikey, ++key, ++value ) { void* prev = hashmap_insert( map, key, value ); EXPECT_EQ( prev, 0 ); } for( ikey = 0, key = (hash_t)4321, value = (void*)(uintptr_t)1234; ikey < 1024; ++ikey, ++key, ++value ) { void* prev = hashmap_lookup( map, key ); EXPECT_EQ( prev, value ); EXPECT_TRUE( hashmap_has_key( map, key ) ); hashmap_erase( map, key ); EXPECT_FALSE( hashmap_has_key( map, key ) ); } hashmap_deallocate( map ); return 0; }
static void arbitrary( void ) { int res, val; hashmap_t *hashmap; hashmap = hashmap_create(); assert( hashmap ); res = hashmap_insert( hashmap, "test", 100 ); assert( 0 == res ); res = hashmap_insert( hashmap, "test", 101 ); assert( 0 == res ); res = hashmap_insert( hashmap, "test2", 3 ); assert( 0 == res ); res = hashmap_find( hashmap, "test2", NULL ); assert( 1 == res ); res = hashmap_find( hashmap, "test2", &val ); assert( 1 == res ); assert( 3 == val ); res = hashmap_find( hashmap, "test", &val ); assert( 1 == res ); assert( 101 == val ); hashmap_erase( hashmap, "test" ); res = hashmap_find( hashmap, "test", NULL ); assert( 0 == res ); hashmap_erase( hashmap, "test" ); res = hashmap_find( hashmap, "test", NULL ); assert( 0 == res ); hashmap_destroy( hashmap ); }
void global_erase_plug(int id){ if(id >= 0 && id < GLOBAL_DEVICE_DEFAULT){ global_init(); hashmap_erase(self.plugs, id); } }
void global_erase_device(int id){ if(id >= 0 && id < GLOBAL_DEVICE_DEFAULT){ global_init(); hashmap_erase(self.devices, id); } }
static void size_test( void ) { int res; hashmap_t hashmap; size_t size; unsigned int empty; res = hashmap_init( &hashmap ); assert( 0 == res ); /* Confirm initial size is 0. */ size = hashmap_size( &hashmap ); assert( 0 == size ); empty = hashmap_empty( &hashmap ); assert( 0 != empty ); /* Confirm insertion increases by 1. */ res = hashmap_insert( &hashmap, "test", 1 ); assert( 0 == res ); size = hashmap_size( &hashmap ); assert( 1 == size ); empty = hashmap_empty( &hashmap ); assert( 0 == empty ); /* Confirm duplicate insert doesn't change size. */ res = hashmap_insert( &hashmap, "test", 100 ); assert( 0 == res ); size = hashmap_size( &hashmap ); assert( 1 == size ); empty = hashmap_empty( &hashmap ); assert( 0 == empty ); /* Confirm insertion increases by 1. */ res = hashmap_insert( &hashmap, "test2", 4 ); assert( 0 == res ); size = hashmap_size( &hashmap ); assert( 2 == size ); empty = hashmap_empty( &hashmap ); assert( 0 == empty ); /* Confirm removal decreases by 1. */ hashmap_erase( &hashmap, "test" ); size = hashmap_size( &hashmap ); assert( 1 == size ); empty = hashmap_empty( &hashmap ); assert( 0 == empty ); /* Confirm removal of key that doesn't exist doesn't change the size. */ hashmap_erase( &hashmap, "doesn't exist" ); size = hashmap_size( &hashmap ); assert( 1 == size ); empty = hashmap_empty( &hashmap ); assert( 0 == empty ); /* Confirm removal of key that once existed but doesn't now doesn't change the size. */ hashmap_erase( &hashmap, "test" ); size = hashmap_size( &hashmap ); assert( 1 == size ); empty = hashmap_empty( &hashmap ); assert( 0 == empty ); /* Confirm removal decreases by 1. */ hashmap_erase( &hashmap, "test2" ); size = hashmap_size( &hashmap ); assert( 0 == size ); empty = hashmap_empty( &hashmap ); assert( 0 != empty ); hashmap_term( &hashmap ); }
static void iter_test( void ) { int res; hashmap_t *hashmap; hashmap_iter_t iter; unsigned int count = 0; hashmap = hashmap_create(); assert( hashmap ); for( hashmap_iter_begin( hashmap, &iter ); !hashmap_iter_end( hashmap, &iter ); hashmap_iter_next( hashmap, &iter ) ) { assert( 0 ); } assert( 0 == count ); res = hashmap_insert( hashmap, "test", 100 ); assert( 0 == res ); count = 0; for( hashmap_iter_begin( hashmap, &iter ); !hashmap_iter_end( hashmap, &iter ); hashmap_iter_next( hashmap, &iter ) ) { hashmap_key_t key = hashmap_iter_key( &iter ); hashmap_value_t value = hashmap_iter_value( &iter ); assert( 0 == strcmp( key, "test" ) ); assert( value == 100 ); ++count; } assert( 1 == count ); res = hashmap_insert( hashmap, "next", 2 ); assert( 0 == res ); res = hashmap_insert( hashmap, "another", 3 ); assert( 0 == res ); res = hashmap_insert( hashmap, "yet another", 4 ); assert( 0 == res ); res = hashmap_insert( hashmap, "penultimate", 5 ); assert( 0 == res ); res = hashmap_insert( hashmap, "final", 5 ); assert( 0 == res ); count = 0; for( hashmap_iter_begin( hashmap, &iter ); !hashmap_iter_end( hashmap, &iter ); hashmap_iter_next( hashmap, &iter ) ) { ++count; } assert( 6 == count ); hashmap_erase( hashmap, "penultimate" ); count = 0; for( hashmap_iter_begin( hashmap, &iter ); !hashmap_iter_end( hashmap, &iter ); hashmap_iter_next( hashmap, &iter ) ) { ++count; } assert( 5 == count ); hashmap_destroy( hashmap ); }