test-strbuf.c revision 97b5f608182773d7ef9ca18913085b3a3eccd943
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering This file is part of systemd.
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering Copyright 2013 Thomas H.P. Andersen
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 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.
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/>.
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poetteringstatic ssize_t add_string(struct strbuf *sb, const char *s) {
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering return strbuf_add_string(sb, s, strlen(s));
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poetteringstatic void test_strbuf(void) {
81527be142678057215665be66e4b3c8306a7ab3Lennart Poettering ssize_t a, b, c, d, e, f, g;
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 */
6e409ce10d134625626d1eddfd6152755ef1908dLennart Poettering /* check the content of the buffer directly */
e6520a0fcebb4d5e07f4c81a51382501b0beb22fLennart Poettering assert(sb->nodes_count == 5); /* root + 4 non-duplicates */
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 /* check the returned offsets and the respective content in the buffer */