void test_trie_lookup(void) { Trie *trie; char buf[10]; int *val; int i; trie = generate_trie(); /* Test lookup for non-existent values */ assert(trie_lookup(trie, "000000000000000") == TRIE_NULL); assert(trie_lookup(trie, "") == TRIE_NULL); /* Look up all values */ for (i=0; i<NUM_TEST_VALUES; ++i) { sprintf(buf, "%i", i); val = (int *) trie_lookup(trie, buf); assert(*val == i); } trie_free(trie); }
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); }
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); }
// Replace the pairs in the given input, optionally ignoring escaped characters static char* string_replace(char* input, str_pair_t* pairs, char no_escape) { trie_node_t* trie = generate_trie(pairs); char* retval = trie_replace(input, trie, no_escape); free(trie); return retval; }