97b5f608182773d7ef9ca18913085b3a3eccd943Thomas Hindoe Paaboel Andersen/***
97b5f608182773d7ef9ca18913085b3a3eccd943Thomas Hindoe Paaboel Andersen This file is part of systemd.
97b5f608182773d7ef9ca18913085b3a3eccd943Thomas Hindoe Paaboel Andersen
97b5f608182773d7ef9ca18913085b3a3eccd943Thomas Hindoe Paaboel Andersen Copyright 2013 Thomas H.P. Andersen
97b5f608182773d7ef9ca18913085b3a3eccd943Thomas Hindoe Paaboel Andersen
97b5f608182773d7ef9ca18913085b3a3eccd943Thomas Hindoe Paaboel Andersen systemd is free software; you can redistribute it and/or modify it
97b5f608182773d7ef9ca18913085b3a3eccd943Thomas Hindoe Paaboel Andersen under the terms of the GNU Lesser General Public License as published by
97b5f608182773d7ef9ca18913085b3a3eccd943Thomas Hindoe Paaboel Andersen the Free Software Foundation; either version 2.1 of the License, or
97b5f608182773d7ef9ca18913085b3a3eccd943Thomas Hindoe Paaboel Andersen (at your option) any later version.
97b5f608182773d7ef9ca18913085b3a3eccd943Thomas Hindoe Paaboel Andersen
97b5f608182773d7ef9ca18913085b3a3eccd943Thomas Hindoe Paaboel Andersen systemd is distributed in the hope that it will be useful, but
97b5f608182773d7ef9ca18913085b3a3eccd943Thomas Hindoe Paaboel Andersen WITHOUT ANY WARRANTY; without even the implied warranty of
97b5f608182773d7ef9ca18913085b3a3eccd943Thomas Hindoe Paaboel Andersen MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
97b5f608182773d7ef9ca18913085b3a3eccd943Thomas Hindoe Paaboel Andersen Lesser General Public License for more details.
97b5f608182773d7ef9ca18913085b3a3eccd943Thomas Hindoe Paaboel Andersen
97b5f608182773d7ef9ca18913085b3a3eccd943Thomas Hindoe Paaboel Andersen You should have received a copy of the GNU Lesser General Public License
97b5f608182773d7ef9ca18913085b3a3eccd943Thomas Hindoe Paaboel Andersen along with systemd; If not, see <http://www.gnu.org/licenses/>.
97b5f608182773d7ef9ca18913085b3a3eccd943Thomas Hindoe Paaboel Andersen***/
97b5f608182773d7ef9ca18913085b3a3eccd943Thomas Hindoe Paaboel Andersen
97b5f608182773d7ef9ca18913085b3a3eccd943Thomas Hindoe Paaboel Andersen#include <stdlib.h>
97b5f608182773d7ef9ca18913085b3a3eccd943Thomas Hindoe Paaboel Andersen#include <string.h>
97b5f608182773d7ef9ca18913085b3a3eccd943Thomas Hindoe Paaboel Andersen
97b5f608182773d7ef9ca18913085b3a3eccd943Thomas Hindoe Paaboel Andersen#include "strbuf.h"
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poettering#include "string-util.h"
97b5f608182773d7ef9ca18913085b3a3eccd943Thomas Hindoe Paaboel Andersen#include "strv.h"
97b5f608182773d7ef9ca18913085b3a3eccd943Thomas Hindoe Paaboel Andersen#include "util.h"
97b5f608182773d7ef9ca18913085b3a3eccd943Thomas Hindoe Paaboel Andersen
97b5f608182773d7ef9ca18913085b3a3eccd943Thomas Hindoe Paaboel Andersenstatic ssize_t add_string(struct strbuf *sb, const char *s) {
97b5f608182773d7ef9ca18913085b3a3eccd943Thomas Hindoe Paaboel Andersen return strbuf_add_string(sb, s, strlen(s));
97b5f608182773d7ef9ca18913085b3a3eccd943Thomas Hindoe Paaboel Andersen}
97b5f608182773d7ef9ca18913085b3a3eccd943Thomas Hindoe Paaboel Andersen
97b5f608182773d7ef9ca18913085b3a3eccd943Thomas Hindoe Paaboel Andersenstatic void test_strbuf(void) {
97b5f608182773d7ef9ca18913085b3a3eccd943Thomas Hindoe Paaboel Andersen struct strbuf *sb;
c62c294fd521e5b65bb52f831773916bbc4cd90aThomas Hindoe Paaboel Andersen _cleanup_strv_free_ char **l;
97b5f608182773d7ef9ca18913085b3a3eccd943Thomas Hindoe Paaboel Andersen ssize_t a, b, c, d, e, f, g;
97b5f608182773d7ef9ca18913085b3a3eccd943Thomas Hindoe Paaboel Andersen
97b5f608182773d7ef9ca18913085b3a3eccd943Thomas Hindoe Paaboel Andersen sb = strbuf_new();
97b5f608182773d7ef9ca18913085b3a3eccd943Thomas Hindoe Paaboel Andersen
97b5f608182773d7ef9ca18913085b3a3eccd943Thomas Hindoe Paaboel Andersen a = add_string(sb, "waldo");
97b5f608182773d7ef9ca18913085b3a3eccd943Thomas Hindoe Paaboel Andersen b = add_string(sb, "foo");
97b5f608182773d7ef9ca18913085b3a3eccd943Thomas Hindoe Paaboel Andersen c = add_string(sb, "bar");
97b5f608182773d7ef9ca18913085b3a3eccd943Thomas Hindoe Paaboel Andersen d = add_string(sb, "waldo"); /* duplicate */
97b5f608182773d7ef9ca18913085b3a3eccd943Thomas Hindoe Paaboel Andersen e = add_string(sb, "aldo"); /* duplicate */
97b5f608182773d7ef9ca18913085b3a3eccd943Thomas Hindoe Paaboel Andersen f = add_string(sb, "do"); /* duplicate */
97b5f608182773d7ef9ca18913085b3a3eccd943Thomas Hindoe Paaboel Andersen g = add_string(sb, "waldorf"); /* not a duplicate: matches from tail */
97b5f608182773d7ef9ca18913085b3a3eccd943Thomas Hindoe Paaboel Andersen
97b5f608182773d7ef9ca18913085b3a3eccd943Thomas Hindoe Paaboel Andersen /* check the content of the buffer directly */
97b5f608182773d7ef9ca18913085b3a3eccd943Thomas Hindoe Paaboel Andersen l = strv_parse_nulstr(sb->buf, sb->len);
97b5f608182773d7ef9ca18913085b3a3eccd943Thomas Hindoe Paaboel Andersen
f7340ab269828d917cd1281e33e6dd4fdfee67b3Torstein Husebø assert_se(streq(l[0], "")); /* root */
bdf7026e9557349cd3eeb291c01655d5f2a55db8Thomas Hindoe Paaboel Andersen assert_se(streq(l[1], "waldo"));
bdf7026e9557349cd3eeb291c01655d5f2a55db8Thomas Hindoe Paaboel Andersen assert_se(streq(l[2], "foo"));
bdf7026e9557349cd3eeb291c01655d5f2a55db8Thomas Hindoe Paaboel Andersen assert_se(streq(l[3], "bar"));
bdf7026e9557349cd3eeb291c01655d5f2a55db8Thomas Hindoe Paaboel Andersen assert_se(streq(l[4], "waldorf"));
97b5f608182773d7ef9ca18913085b3a3eccd943Thomas Hindoe Paaboel Andersen
bdf7026e9557349cd3eeb291c01655d5f2a55db8Thomas Hindoe Paaboel Andersen assert_se(sb->nodes_count == 5); /* root + 4 non-duplicates */
bdf7026e9557349cd3eeb291c01655d5f2a55db8Thomas Hindoe Paaboel Andersen assert_se(sb->dedup_count == 3);
bdf7026e9557349cd3eeb291c01655d5f2a55db8Thomas Hindoe Paaboel Andersen assert_se(sb->in_count == 7);
97b5f608182773d7ef9ca18913085b3a3eccd943Thomas Hindoe Paaboel Andersen
bdf7026e9557349cd3eeb291c01655d5f2a55db8Thomas Hindoe Paaboel Andersen assert_se(sb->in_len == 29); /* length of all strings added */
bdf7026e9557349cd3eeb291c01655d5f2a55db8Thomas Hindoe Paaboel Andersen assert_se(sb->dedup_len == 11); /* length of all strings duplicated */
bdf7026e9557349cd3eeb291c01655d5f2a55db8Thomas Hindoe Paaboel Andersen assert_se(sb->len == 23); /* buffer length: in - dedup + \0 for each node */
97b5f608182773d7ef9ca18913085b3a3eccd943Thomas Hindoe Paaboel Andersen
97b5f608182773d7ef9ca18913085b3a3eccd943Thomas Hindoe Paaboel Andersen /* check the returned offsets and the respective content in the buffer */
bdf7026e9557349cd3eeb291c01655d5f2a55db8Thomas Hindoe Paaboel Andersen assert_se(a == 1);
bdf7026e9557349cd3eeb291c01655d5f2a55db8Thomas Hindoe Paaboel Andersen assert_se(b == 7);
bdf7026e9557349cd3eeb291c01655d5f2a55db8Thomas Hindoe Paaboel Andersen assert_se(c == 11);
bdf7026e9557349cd3eeb291c01655d5f2a55db8Thomas Hindoe Paaboel Andersen assert_se(d == 1);
bdf7026e9557349cd3eeb291c01655d5f2a55db8Thomas Hindoe Paaboel Andersen assert_se(e == 2);
bdf7026e9557349cd3eeb291c01655d5f2a55db8Thomas Hindoe Paaboel Andersen assert_se(f == 4);
bdf7026e9557349cd3eeb291c01655d5f2a55db8Thomas Hindoe Paaboel Andersen assert_se(g == 15);
bdf7026e9557349cd3eeb291c01655d5f2a55db8Thomas Hindoe Paaboel Andersen
bdf7026e9557349cd3eeb291c01655d5f2a55db8Thomas Hindoe Paaboel Andersen assert_se(streq(sb->buf + a, "waldo"));
bdf7026e9557349cd3eeb291c01655d5f2a55db8Thomas Hindoe Paaboel Andersen assert_se(streq(sb->buf + b, "foo"));
bdf7026e9557349cd3eeb291c01655d5f2a55db8Thomas Hindoe Paaboel Andersen assert_se(streq(sb->buf + c, "bar"));
bdf7026e9557349cd3eeb291c01655d5f2a55db8Thomas Hindoe Paaboel Andersen assert_se(streq(sb->buf + d, "waldo"));
bdf7026e9557349cd3eeb291c01655d5f2a55db8Thomas Hindoe Paaboel Andersen assert_se(streq(sb->buf + e, "aldo"));
bdf7026e9557349cd3eeb291c01655d5f2a55db8Thomas Hindoe Paaboel Andersen assert_se(streq(sb->buf + f, "do"));
bdf7026e9557349cd3eeb291c01655d5f2a55db8Thomas Hindoe Paaboel Andersen assert_se(streq(sb->buf + g, "waldorf"));
97b5f608182773d7ef9ca18913085b3a3eccd943Thomas Hindoe Paaboel Andersen
97b5f608182773d7ef9ca18913085b3a3eccd943Thomas Hindoe Paaboel Andersen strbuf_complete(sb);
bdf7026e9557349cd3eeb291c01655d5f2a55db8Thomas Hindoe Paaboel Andersen assert_se(sb->root == NULL);
97b5f608182773d7ef9ca18913085b3a3eccd943Thomas Hindoe Paaboel Andersen
97b5f608182773d7ef9ca18913085b3a3eccd943Thomas Hindoe Paaboel Andersen strbuf_cleanup(sb);
97b5f608182773d7ef9ca18913085b3a3eccd943Thomas Hindoe Paaboel Andersen}
97b5f608182773d7ef9ca18913085b3a3eccd943Thomas Hindoe Paaboel Andersen
f168c27313e4d7b0aabee037dc9c78a5799f0597Thomas Hindoe Paaboel Andersenint main(int argc, const char *argv[]) {
97b5f608182773d7ef9ca18913085b3a3eccd943Thomas Hindoe Paaboel Andersen test_strbuf();
97b5f608182773d7ef9ca18913085b3a3eccd943Thomas Hindoe Paaboel Andersen
97b5f608182773d7ef9ca18913085b3a3eccd943Thomas Hindoe Paaboel Andersen return 0;
97b5f608182773d7ef9ca18913085b3a3eccd943Thomas Hindoe Paaboel Andersen}