test-strbuf.c revision 97b5f608182773d7ef9ca18913085b3a3eccd943
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering/***
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering This file is part of systemd.
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering Copyright 2013 Thomas H.P. Andersen
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering systemd is free software; you can redistribute it and/or modify it
5430f7f2bc7330f3088b894166bf3524a067e3d8Lennart Poettering under the terms of the GNU Lesser General Public License as published by
5430f7f2bc7330f3088b894166bf3524a067e3d8Lennart Poettering the Free Software Foundation; either version 2.1 of the License, or
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering (at your option) any later version.
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering systemd is distributed in the hope that it will be useful, but
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering WITHOUT ANY WARRANTY; without even the implied warranty of
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
5430f7f2bc7330f3088b894166bf3524a067e3d8Lennart Poettering Lesser General Public License for more details.
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
5430f7f2bc7330f3088b894166bf3524a067e3d8Lennart Poettering You should have received a copy of the GNU Lesser General Public License
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering along with systemd; If not, see <http://www.gnu.org/licenses/>.
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering***/
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering#include <stdlib.h>
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering#include <string.h>
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering#include "strbuf.h"
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering#include "strv.h"
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering#include "util.h"
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poetteringstatic ssize_t add_string(struct strbuf *sb, const char *s) {
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering return strbuf_add_string(sb, s, strlen(s));
6e409ce10d134625626d1eddfd6152755ef1908dLennart Poettering}
cee5e9a7ca75f05d4a0f7759566e657587a500d5Lennart Poettering
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poetteringstatic void test_strbuf(void) {
81527be142678057215665be66e4b3c8306a7ab3Lennart Poettering struct strbuf *sb;
81527be142678057215665be66e4b3c8306a7ab3Lennart Poettering _cleanup_strv_free_ char **l;
81527be142678057215665be66e4b3c8306a7ab3Lennart Poettering ssize_t a, b, c, d, e, f, g;
81527be142678057215665be66e4b3c8306a7ab3Lennart Poettering
ba1261bc02693ac8a7712ade14aab8e22989ba88Lennart Poettering sb = strbuf_new();
ba1261bc02693ac8a7712ade14aab8e22989ba88Lennart Poettering
ba1261bc02693ac8a7712ade14aab8e22989ba88Lennart Poettering a = add_string(sb, "waldo");
ba1261bc02693ac8a7712ade14aab8e22989ba88Lennart Poettering b = add_string(sb, "foo");
49e942b2bc9fdedba79cd266a076ce9c9d91fc13Kay Sievers c = add_string(sb, "bar");
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering d = add_string(sb, "waldo"); /* duplicate */
cec736d21ff86c4ac81b4d306ddba2120333818cLennart Poettering e = add_string(sb, "aldo"); /* duplicate */
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering f = add_string(sb, "do"); /* duplicate */
69e5d42db09dfb638bc74055c33bb2645f81563dLennart Poettering g = add_string(sb, "waldorf"); /* not a duplicate: matches from tail */
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering
6e409ce10d134625626d1eddfd6152755ef1908dLennart Poettering /* check the content of the buffer directly */
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering l = strv_parse_nulstr(sb->buf, sb->len);
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering
89fef99014662a5a63e7deaedd6292b7fb4ab2f8Lennart Poettering assert(streq(l[0], "")); /* root*/
e6960940b6dc5ab81eb2fca4061c333e1795f38dLennart Poettering assert(streq(l[1], "waldo"));
e6960940b6dc5ab81eb2fca4061c333e1795f38dLennart Poettering assert(streq(l[2], "foo"));
effb1102d3f1d6bc2d0bda5328d3ce2139af9850Lennart Poettering assert(streq(l[3], "bar"));
7f2c63cbf47c89ec56f50469f6551df473dd65d8Lennart Poettering assert(streq(l[4], "waldorf"));
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
e6520a0fcebb4d5e07f4c81a51382501b0beb22fLennart Poettering assert(sb->nodes_count == 5); /* root + 4 non-duplicates */
e6520a0fcebb4d5e07f4c81a51382501b0beb22fLennart Poettering assert(sb->dedup_count == 3);
e6520a0fcebb4d5e07f4c81a51382501b0beb22fLennart Poettering assert(sb->in_count == 7);
79c077224be5a868d0bba66972ef9546dae85977Kay Sievers
e6520a0fcebb4d5e07f4c81a51382501b0beb22fLennart Poettering assert(sb->in_len == 29); /* length of all strings added */
e6520a0fcebb4d5e07f4c81a51382501b0beb22fLennart Poettering assert(sb->dedup_len == 11); /* length of all strings duplicated */
8a0f04e6a283cc6734ee09a20305c13e09ba0418Lennart Poettering assert(sb->len == 23); /* buffer length: in - dedup + \0 for each node */
8a0f04e6a283cc6734ee09a20305c13e09ba0418Lennart Poettering
8a0f04e6a283cc6734ee09a20305c13e09ba0418Lennart Poettering /* check the returned offsets and the respective content in the buffer */
8a0f04e6a283cc6734ee09a20305c13e09ba0418Lennart Poettering assert(a == 1);
cab8ac60837b489b27a247990f741315c71cb389Lennart Poettering assert(b == 7);
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering assert(c == 11);
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering assert(d == 1);
de97b26ac5e29063632312ec1a20eb6318ca924cLennart Poettering assert(e == 2);
de97b26ac5e29063632312ec1a20eb6318ca924cLennart Poettering assert(f == 4);
de97b26ac5e29063632312ec1a20eb6318ca924cLennart Poettering assert(g == 15);
9cfb57c989b62d11c073c77179df4bb7fa19f35dLennart Poettering
9cfb57c989b62d11c073c77179df4bb7fa19f35dLennart Poettering assert(streq(sb->buf + a, "waldo"));
8a0f04e6a283cc6734ee09a20305c13e09ba0418Lennart Poettering assert(streq(sb->buf + b, "foo"));
e7573d7fa9366f72fb81e0a02d82dbb0dfd106aaLennart Poettering assert(streq(sb->buf + c, "bar"));
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering assert(streq(sb->buf + d, "waldo"));
0dad12c190b7493955cd60d2a1625199b1709f69Lennart Poettering assert(streq(sb->buf + e, "aldo"));
0dad12c190b7493955cd60d2a1625199b1709f69Lennart Poettering assert(streq(sb->buf + f, "do"));
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering assert(streq(sb->buf + g, "waldorf"));
4cd9a9d9ecf3a8835e21930f3215a5f5b74144beLennart Poettering
62bca2c657bf95fd1f69935eef09915afa5c69d9Eelco Dolstra strbuf_complete(sb);
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering assert(sb->root == NULL);
258cdffc2f9f8cd3d243bd19f507bc73009d77d9Lennart Poettering
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering strbuf_cleanup(sb);
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering}
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poetteringint main(int argc, const char *argv[])
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering{
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering test_strbuf();
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering return 0;
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering}
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering