void test_hash_table_remove(void) { HashTable *hash_table; char buf[10]; hash_table = generate_hash_table(); assert(hash_table_num_entries(hash_table) == NUM_TEST_VALUES); sprintf(buf, "%i", 5000); assert(hash_table_lookup(hash_table, buf) != NULL); /* Remove an entry */ hash_table_remove(hash_table, buf); /* Check entry counter */ assert(hash_table_num_entries(hash_table) == 9999); /* Check that NULL is returned now */ assert(hash_table_lookup(hash_table, buf) == NULL); /* Try removing a non-existent entry */ sprintf(buf, "%i", -1); hash_table_remove(hash_table, buf); assert(hash_table_num_entries(hash_table) == 9999); hash_table_free(hash_table); }
void test_hash_table_iterating_remove(void) { HashTable *hash_table; HashTableIterator iterator; char buf[10]; char *val; HashTablePair pair; int count; unsigned int removed; int i; hash_table = generate_hash_table(); /* Iterate over all values in the table */ count = 0; removed = 0; hash_table_iterate(hash_table, &iterator); while (hash_table_iter_has_more(&iterator)) { /* Read the next value */ pair = hash_table_iter_next(&iterator); val = pair.value; /* Remove every hundredth entry */ if ((atoi(val) % 100) == 0) { hash_table_remove(hash_table, val); ++removed; } ++count; } /* Check counts */ assert(removed == 100); assert(count == NUM_TEST_VALUES); assert(hash_table_num_entries(hash_table) == NUM_TEST_VALUES - removed); /* Check all entries divisible by 100 were really removed */ for (i=0; i<NUM_TEST_VALUES; ++i) { sprintf(buf, "%i", i); if (i % 100 == 0) { assert(hash_table_lookup(hash_table, buf) == NULL); } else { assert(hash_table_lookup(hash_table, buf) != NULL); } } hash_table_free(hash_table); }
void test_hash_table_out_of_memory(void) { HashTable *hash_table; int values[66]; unsigned int i; hash_table = hash_table_new(int_hash, int_equal); /* Test normal failure */ alloc_test_set_limit(0); values[0] = 0; assert(hash_table_insert(hash_table, &values[0], &values[0]) == 0); assert(hash_table_num_entries(hash_table) == 0); alloc_test_set_limit(-1); /* Test failure when increasing table size. * The initial table size is 193 entries. The table increases in * size when 1/3 full, so the 66th entry should cause the insert * to fail. */ for (i=0; i<65; ++i) { values[i] = (int) i; assert(hash_table_insert(hash_table, &values[i], &values[i]) != 0); assert(hash_table_num_entries(hash_table) == i + 1); } assert(hash_table_num_entries(hash_table) == 65); /* Test the 66th insert */ alloc_test_set_limit(0); values[65] = 65; assert(hash_table_insert(hash_table, &values[65], &values[65]) == 0); assert(hash_table_num_entries(hash_table) == 65); hash_table_free(hash_table); }
void la_finalize(la_UUID_t seed) { /* TODO check seed has been init */ int num_entries = 0; if (la_buffer_table) { #ifndef NDEBUG __la_dump_table(la_buffer_table, &__la_dump_value); #endif num_entries = hash_table_num_entries(la_buffer_table); hash_table_free(la_buffer_table); la_buffer_table = LA_NULL; } LOGD("%llu (entries removed from buffer table: %i)", seed, num_entries); }
void test_hash_table_insert_lookup(void) { HashTable *hash_table; char buf[10]; char *value; int i; /* Generate a hash table */ hash_table = generate_hash_table(); assert(hash_table_num_entries(hash_table) == NUM_TEST_VALUES); /* Check all values */ for (i=0; i<NUM_TEST_VALUES; ++i) { sprintf(buf, "%i", i); value = hash_table_lookup(hash_table, buf); assert(strcmp(value, buf) == 0); } /* Lookup on invalid values returns NULL */ sprintf(buf, "%i", -1); assert(hash_table_lookup(hash_table, buf) == NULL); sprintf(buf, "%i", NUM_TEST_VALUES); assert(hash_table_lookup(hash_table, buf) == NULL); /* Insert overwrites existing entries with the same key */ sprintf(buf, "%i", 12345); hash_table_insert(hash_table, buf, strdup("hello world")); value = hash_table_lookup(hash_table, buf); assert(strcmp(value, "hello world") == 0); hash_table_free(hash_table); }
int main(int arg, char *argv) { char * str1 = "HTTP/1.1 index.html"; char * str2 = "HTTP/1.0 aaaaa.html"; char * str3 = "HTTP/1.0 bbbbb.html"; char * str4 = "HTTP/1.1 ccccc.html"; char * str5 = "HTTP/0.9 ddddd.html"; char * str6 = "HTTP/1.1 fffff.html"; char * str7 = "HTTP/0.9 eeeee.html"; LST_String * nbytes1 = lst_string_new(str1, 1, strlen(str1)); LST_String * nbytes2 = lst_string_new(str2, 1, strlen(str2)); LST_String * nbytes3 = lst_string_new(str3, 1, strlen(str3)); LST_String * nbytes4 = lst_string_new(str4, 1, strlen(str4)); LST_String * nbytes5 = lst_string_new(str5, 1, strlen(str5)); LST_String * nbytes6 = lst_string_new(str6, 1, strlen(str6)); LST_String * nbytes7 = lst_string_new(str7, 1, strlen(str7)); LST_StringSet * set = lst_stringset_new(); lst_stringset_add(set, nbytes1); lst_stringset_add(set, nbytes2); lst_stringset_add(set, nbytes3); lst_stringset_add(set, nbytes4); lst_stringset_add(set, nbytes5); lst_stringset_add(set, nbytes6); lst_stringset_add(set, nbytes7); int first_bytes = 8; int last_bytes = 10; int num_bytes = 19; int gamma_merge = 2; product_distribution_t * pd = product_distribution_new(set, first_bytes, last_bytes, num_bytes); /* print pd */ HashTableIterator iterator1; hash_table_iterate(pd->offset_distribution, &iterator1); while (hash_table_iter_has_more(&iterator1)){ HashTablePair pair1 = hash_table_iter_next(&iterator1); int *key1 = (int *) pair1.key; byte_distribution_t *value1 = (byte_distribution_t *) pair1.value; HashTableIterator iterator2; hash_table_iterate(value1->value_frequency, &iterator2); if (hash_table_num_entries(value1->value_frequency) > gamma_merge) { continue; } printf("offset %d : ", *key1); while(hash_table_iter_has_more(&iterator2)){ HashTablePair pair2 = hash_table_iter_next(&iterator2); char *key2 = (char *) pair2.key; int *value2 = (int *) pair2.value; //printf("<%c, %d>\t", key2[0], *value2); if (0 == *key1) { printf("^%s\t", key2); } else if (num_bytes - 1 == *key1) { printf("%s$\t", key2); } else { printf("%s\t", key2); } } printf("\n"); } product_distribution_free(pd); return 0; }