int main(int argc, const char **argv) { hashtable_p table = NULL; int size = START_CAPACITY; float best_utilization = 0.0; float tmp_utilization = 0.0; (void)argc; (void)argv; pdebug(DEBUG_INFO,"Starting hashtable tests."); set_debug_level(DEBUG_SPEW); /* create a hashtable */ pdebug(DEBUG_INFO,"Creating hashtable with at least capacity %d.", START_CAPACITY); table = hashtable_create(START_CAPACITY); assert(table != NULL); assert(hashtable_capacity(table) >= START_CAPACITY); assert(hashtable_entries(table) == 0); size = hashtable_capacity(table); /* insert tests. */ for(int i=1; i <= INSERT_ENTRIES; i++) { int rc = hashtable_put(table, i, (void*)(intptr_t)i); assert(rc == PLCTAG_STATUS_OK); if(hashtable_capacity(table) != size) { pdebug(DEBUG_INFO, "Hashtable expanded from %d entries to %d entries after inserting %d entries.", size, hashtable_capacity(table), hashtable_entries(table)); size = hashtable_capacity(table); } } tmp_utilization = (float)(hashtable_entries(table))/(float)(hashtable_capacity(table)); pdebug(DEBUG_INFO, "Current table utilization %f%%", tmp_utilization*100.0); if(tmp_utilization > best_utilization) { best_utilization = tmp_utilization; } assert(hashtable_entries(table) == INSERT_ENTRIES); pdebug(DEBUG_INFO, "Hash table has correct number of used entries, %d.", hashtable_entries(table)); pdebug(DEBUG_INFO, "Hashtable using %d entries of %d entries capacity.", hashtable_entries(table), hashtable_capacity(table)); /* retrieval tests. */ pdebug(DEBUG_INFO, "Running retrieval tests."); for(int i=INSERT_ENTRIES; i > 0; i--) { void *res = hashtable_get(table, i); assert(res != NULL); assert(i == (int)(intptr_t)res); } /* insert + delete tests. */ pdebug(DEBUG_INFO, "Running combined insert and delete tests."); for(int i=INSERT_ENTRIES+1; i < (INSERT_ENTRIES*2); i++) { int rc = hashtable_put(table, i, (void*)(intptr_t)i); void *res = NULL; assert(rc == PLCTAG_STATUS_OK); res = hashtable_remove(table, (i - INSERT_ENTRIES)); assert((i - INSERT_ENTRIES) == (int)(intptr_t)res); } assert(hashtable_entries(table) == INSERT_ENTRIES); pdebug(DEBUG_INFO, "Hash table has correct number of used entries, %d.", hashtable_entries(table)); tmp_utilization = (float)(hashtable_entries(table))/(float)(hashtable_capacity(table)); pdebug(DEBUG_INFO, "Current table utilization %f%%", tmp_utilization*100.0); if(tmp_utilization > best_utilization) { best_utilization = tmp_utilization; } pdebug(DEBUG_INFO, "Best table utilization %f%%", best_utilization*100.0); hashtable_destroy(table); pdebug(DEBUG_INFO, "Done."); }
int main(int argc, char **argv) { int *vals = (int *)GC_MALLOC(SIZE * sizeof(int)); int i; for(i=0;i<SIZE; i++) vals[i] = i; hashtable_t *tab = hashtable_create(1000001); clock_t start, diff; int msec; start = clock(); for(i=0; i<SIZE; i++) hashtable_put(tab, (void *)(vals[i]), (void *)(i*i)); diff = clock() - start; msec = diff * 1000 / CLOCKS_PER_SEC; printf("Put took %d seconds %d milliseconds\n", msec/1000, msec%1000); start = clock(); for(i=0; i<SIZE; i++) { hashtable_entry_t *b = hashtable_get(tab, (void *)(vals[i])); assert((int)(b->ptr) == i); assert((int)(b->value) == i*i); } assert(tab->count == SIZE); diff = clock() - start; msec = diff * 1000 / CLOCKS_PER_SEC; printf("Get took %d seconds %d milliseconds\n", msec/1000, msec%1000); start = clock(); hashtable_remove(tab, (void *)(vals[SIZE/2])); assert(tab->count == SIZE-1); diff = clock() - start; msec = diff * 1000 / CLOCKS_PER_SEC; printf("Remove took %d seconds %d milliseconds\n", msec/1000, msec%1000); assert(hashtable_get(tab, (void *)(vals[SIZE/2])) == NULL); hashtable_remove(tab, (void *)(vals[0])); hashtable_delete(tab); tab = hashtable_create(1000001); for(i=0; i<SIZE; i++) hashtable_put(tab, (void *)vals[i], (void *)(i*i)); hashtable_entry_t *entries = hashtable_entries(tab); while(entries) { assert((int)(entries->value) == (int)(entries->ptr) * (int)(entries->ptr)); entries = entries->next; } hashtable_delete(tab); //free(vals); }