void frame_add(Hash *frame, char *key, boxed_value *value){ if (hash_contains(frame, key)){ boxed_value *current_value = hash_value(frame, key); if (value == current_value) return; else dec_ref_count(current_value); } inc_ref_count(value); hash_add(frame, strdup(key), value); }
/** Hashtable test */ static int hash_test( long elements ) { long i; int res=1; hash_table_t h; hash_init( &h, hash_func, compare_func ); for( i=1; i< elements+1; i++ ) { hash_put( &h, (void*)i, (void*)100l-i ); } for( i=1; i< elements+1; i++ ) { if( (long)hash_get( &h, (void*)i ) != (100l-i) ) { err( L"Key %d gave data %d, expected data %d", i, (long)hash_get( &h, (void*)i ), 100l-i ); res = 0; break; } } if( hash_get_count( &h ) != elements ) { err( L"Table holds %d elements, should hold %d elements", hash_get_count( &h ), elements ); res = 0; } for( i=1; i<elements+1; i+=2 ) { hash_remove( &h, (void*)i, 0, 0 ); } if( hash_get_count( &h ) != ((elements)/2) ) { err( L"Table contains %d elements, should contain %d elements", hash_get_count( &h ), elements/2 ); res = 0; } for( i=1; i<elements+1; i++ ) { if( hash_contains( &h, (void*)i) != (i+1l)%2l ) { if( i%2 ) err( L"Key %d remains, should be deleted", i ); else err( L"Key %d does not exist", i ); res = 0; break; } } hash_destroy( &h ); return res; }