/* * Reset the reader to its initial state. */ void mtree_reader_reset(struct mtree_reader *r) { assert(r != NULL); if (r->skip_trie != NULL) { mtree_trie_free(r->skip_trie); r->skip_trie = NULL; } if (r->entries != NULL) { mtree_entry_free_all(r->entries); r->entries = NULL; } if (r->loose != NULL) { mtree_entry_free_all(r->loose); r->loose = NULL; } r->parent = NULL; r->buflen = 0; r->path_last = -1; mtree_entry_free_data_items(&r->defaults); memset(&r->defaults, 0, sizeof(r->defaults)); }
static void test_trie_strings(void) { struct mtree_trie *trie; char buf[16]; char *s; size_t i; int ret; trie = mtree_trie_create(); TEST_ASSERT_ERRNO(trie != NULL); if (trie == NULL) return; TEST_ASSERT(mtree_trie_count(trie) == 0); for (i = 0; i < TRIE_STRINGS; i++) { snprintf(buf, sizeof(buf), "%zu", i); s = strdup(buf); TEST_ASSERT_ERRNO(s != NULL); if (s == NULL) continue; ret = mtree_trie_insert(trie, buf, s); TEST_ASSERT_ERRNO(ret != -1); if (ret != -1) TEST_ASSERT_MSG(ret == 0, "key: %s", buf); } TEST_ASSERT(mtree_trie_count(trie) == TRIE_STRINGS); /* * Make sure all the items can be found. */ for (i = 0; i < TRIE_STRINGS + 1; i++) { snprintf(buf, sizeof(buf), "%zu", i); if (i < TRIE_STRINGS) { s = mtree_trie_find(trie, buf); TEST_ASSERT_MSG(s != NULL, "key: %s", buf); } else TEST_ASSERT(mtree_trie_find(trie, buf) == NULL); } /* * Replace the first item, the function returns 1 on successful * replacement. */ // XXX leaks the old 0 ret = mtree_trie_insert(trie, "0", strdup("xy")); TEST_ASSERT_ERRNO(ret == 0 || ret == 1); if (ret != 1) { TEST_ASSERT(ret == 1); s = mtree_trie_find(trie, "0"); TEST_ASSERT(s != NULL); if (s != NULL) TEST_ASSERT_STRCMP(s, "xy"); } mtree_trie_free(trie, free); }
static struct mtree_trie * create_node(const char *key, void *item) { struct mtree_trie *u; u = mtree_trie_create(); if (u == NULL) return (NULL); u->item = item; u->key_len = strlen(key); u->key = strdup(key); if (u->key == NULL) { mtree_trie_free(u, NULL); return (NULL); } return (u); }