static void trie_free(struct trie *trie) { if (!trie) return; if (trie->root) trie_node_cleanup(trie->root); strbuf_cleanup(trie->strings); free(trie); }
static void test_strbuf(void) { struct strbuf *sb; _cleanup_strv_free_ char **l; ssize_t a, b, c, d, e, f, g; sb = strbuf_new(); a = add_string(sb, "waldo"); b = add_string(sb, "foo"); c = add_string(sb, "bar"); d = add_string(sb, "waldo"); /* duplicate */ e = add_string(sb, "aldo"); /* duplicate */ f = add_string(sb, "do"); /* duplicate */ g = add_string(sb, "waldorf"); /* not a duplicate: matches from tail */ /* check the content of the buffer directly */ l = strv_parse_nulstr(sb->buf, sb->len); assert(streq(l[0], "")); /* root*/ assert(streq(l[1], "waldo")); assert(streq(l[2], "foo")); assert(streq(l[3], "bar")); assert(streq(l[4], "waldorf")); assert(sb->nodes_count == 5); /* root + 4 non-duplicates */ assert(sb->dedup_count == 3); assert(sb->in_count == 7); assert(sb->in_len == 29); /* length of all strings added */ assert(sb->dedup_len == 11); /* length of all strings duplicated */ assert(sb->len == 23); /* buffer length: in - dedup + \0 for each node */ /* check the returned offsets and the respective content in the buffer */ assert(a == 1); assert(b == 7); assert(c == 11); assert(d == 1); assert(e == 2); assert(f == 4); assert(g == 15); assert(streq(sb->buf + a, "waldo")); assert(streq(sb->buf + b, "foo")); assert(streq(sb->buf + c, "bar")); assert(streq(sb->buf + d, "waldo")); assert(streq(sb->buf + e, "aldo")); assert(streq(sb->buf + f, "do")); assert(streq(sb->buf + g, "waldorf")); strbuf_complete(sb); assert(sb->root == NULL); strbuf_cleanup(sb); }
static void test_catalog_importing(void) { Hashmap *h; struct strbuf *sb; assert_se(h = hashmap_new(&catalog_hash_ops)); assert_se(sb = strbuf_new()); #define BUF "xxx" test_import(h, sb, BUF, sizeof(BUF), -EINVAL); #undef BUF assert_se(hashmap_isempty(h)); log_debug("----------------------------------------"); #define BUF \ "-- 0027229ca0644181a76c4e92458afaff dededededededededededededededede\n" \ "Subject: message\n" \ "\n" \ "payload\n" test_import(h, sb, BUF, sizeof(BUF), -EINVAL); #undef BUF log_debug("----------------------------------------"); #define BUF \ "-- 0027229ca0644181a76c4e92458afaff dededededededededededededededed\n" \ "Subject: message\n" \ "\n" \ "payload\n" test_import(h, sb, BUF, sizeof(BUF), 0); #undef BUF assert_se(hashmap_size(h) == 1); log_debug("----------------------------------------"); hashmap_free_free(h); strbuf_cleanup(sb); }