void test_trie_remove(void) { Trie *trie; char buf[10]; int i; unsigned int entries; trie = generate_trie(); /* Test remove on non-existent values. */ assert(trie_remove(trie, "000000000000000") == 0); assert(trie_remove(trie, "") == 0); entries = trie_num_entries(trie); assert(entries == NUM_TEST_VALUES); /* Remove all values */ for (i=0; i<NUM_TEST_VALUES; ++i) { sprintf(buf, "%i", i); /* Remove value and check counter */ assert(trie_remove(trie, buf) != 0); --entries; assert(trie_num_entries(trie) == entries); } trie_free(trie); }
void test_trie_insert(void) { Trie *trie; unsigned int entries; size_t allocated; trie = generate_trie(); /* Test insert of NULL value has no effect */ entries = trie_num_entries(trie); assert(trie_insert(trie, "hello world", NULL) == 0); assert(trie_num_entries(trie) == entries); /* Test out of memory scenario */ allocated = alloc_test_get_allocated(); alloc_test_set_limit(0); assert(trie_insert(trie, "a", "test value") == 0); assert(trie_num_entries(trie) == entries); /* Test rollback */ alloc_test_set_limit(5); assert(trie_insert(trie, "hello world", "test value") == 0); assert(alloc_test_get_allocated() == allocated); assert(trie_num_entries(trie) == entries); trie_free(trie); }
Trie *generate_trie(void) { Trie *trie; int i; unsigned int entries; /* Create a trie and fill it with a large number of values */ trie = trie_new(); entries = 0; for(i = 0; i < NUM_TEST_VALUES; ++i) { test_array[i] = i; sprintf(test_strings[i], "%i", i); trie_insert(trie, test_strings[i], &test_array[i]); ++entries; assert(trie_num_entries(trie) == entries); } return trie; }
Trie *generate_trie(void) { Trie *trie; int i; unsigned int entries; /* Create a trie and fill it with a large number of values */ trie = trie_new(); entries = 0; for (i=0; i<NUM_TEST_VALUES; ++i) { /* Create a string containing a text version of i, and use * it as a key for the value */ test_array[i] = i; sprintf(test_strings[i], "%i", i); assert(trie_insert(trie, test_strings[i], &test_array[i]) != 0); ++entries; assert(trie_num_entries(trie) == entries); } return trie; }
void test_trie_insert_empty(void) { Trie *trie; char buf[10]; trie = trie_new(); /* Test insert on empty string */ assert(trie_insert(trie, "", buf) != 0); assert(trie_num_entries(trie) != 0); assert(trie_lookup(trie, "") == buf); assert(trie_remove(trie, "") != 0); assert(trie_num_entries(trie) == 0); trie_free(trie); }
void test_trie_insert_out_of_memory(void) { Trie *trie; trie = generate_binary_trie(); alloc_test_set_limit(3); assert(trie_insert_binary(trie, bin_key4, sizeof(bin_key4), "test value") == 0); assert(trie_lookup_binary(trie, bin_key4, sizeof(bin_key4)) == NULL); assert(trie_num_entries(trie) == 2); trie_free(trie); }
void test_trie_replace(void) { Trie *trie; int *val; trie = generate_trie(); /* Test replacing values */ val = malloc(sizeof(int)); *val = 999; assert(trie_insert(trie, "999", val) != 0); assert(trie_num_entries(trie) == NUM_TEST_VALUES); assert(trie_lookup(trie, "999") == val); free(val); trie_free(trie); }