test-strv.c revision a1022300b9f5af6249292acf93f5c6d4bf45e655
62664c6703ed9e8d8f4f8e4c5f5e893559ecefecnd/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
62664c6703ed9e8d8f4f8e4c5f5e893559ecefecnd
62664c6703ed9e8d8f4f8e4c5f5e893559ecefecnd/***
62664c6703ed9e8d8f4f8e4c5f5e893559ecefecnd This file is part of systemd.
62664c6703ed9e8d8f4f8e4c5f5e893559ecefecnd
d5d794fc2f4cc9ca6d6da17cfa2cdcd8d244bacdnd Copyright 2010 Lennart Poettering
6fbd2e53c97ea6976d93e0ac521adabc55e0fb73nd Copyright 2013 Thomas H.P. Andersen
d5d794fc2f4cc9ca6d6da17cfa2cdcd8d244bacdnd
d5d794fc2f4cc9ca6d6da17cfa2cdcd8d244bacdnd systemd is free software; you can redistribute it and/or modify it
d5d794fc2f4cc9ca6d6da17cfa2cdcd8d244bacdnd under the terms of the GNU Lesser General Public License as published by
d5d794fc2f4cc9ca6d6da17cfa2cdcd8d244bacdnd the Free Software Foundation; either version 2.1 of the License, or
d5d794fc2f4cc9ca6d6da17cfa2cdcd8d244bacdnd (at your option) any later version.
d5d794fc2f4cc9ca6d6da17cfa2cdcd8d244bacdnd
d5d794fc2f4cc9ca6d6da17cfa2cdcd8d244bacdnd systemd is distributed in the hope that it will be useful, but
d5d794fc2f4cc9ca6d6da17cfa2cdcd8d244bacdnd WITHOUT ANY WARRANTY; without even the implied warranty of
d5d794fc2f4cc9ca6d6da17cfa2cdcd8d244bacdnd MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
d5d794fc2f4cc9ca6d6da17cfa2cdcd8d244bacdnd Lesser General Public License for more details.
d5d794fc2f4cc9ca6d6da17cfa2cdcd8d244bacdnd
d5d794fc2f4cc9ca6d6da17cfa2cdcd8d244bacdnd You should have received a copy of the GNU Lesser General Public License
d5d794fc2f4cc9ca6d6da17cfa2cdcd8d244bacdnd along with systemd; If not, see <http://www.gnu.org/licenses/>.
d5d794fc2f4cc9ca6d6da17cfa2cdcd8d244bacdnd***/
d5d794fc2f4cc9ca6d6da17cfa2cdcd8d244bacdnd
d5d794fc2f4cc9ca6d6da17cfa2cdcd8d244bacdnd#include <string.h>
62664c6703ed9e8d8f4f8e4c5f5e893559ecefecnd
62664c6703ed9e8d8f4f8e4c5f5e893559ecefecnd#include "util.h"
62664c6703ed9e8d8f4f8e4c5f5e893559ecefecnd#include "specifier.h"
62664c6703ed9e8d8f4f8e4c5f5e893559ecefecnd#include "strv.h"
62664c6703ed9e8d8f4f8e4c5f5e893559ecefecnd
62664c6703ed9e8d8f4f8e4c5f5e893559ecefecndstatic void test_specifier_printf(void) {
62664c6703ed9e8d8f4f8e4c5f5e893559ecefecnd char *w;
62664c6703ed9e8d8f4f8e4c5f5e893559ecefecnd
62664c6703ed9e8d8f4f8e4c5f5e893559ecefecnd const Specifier table[] = {
62664c6703ed9e8d8f4f8e4c5f5e893559ecefecnd { 'a', specifier_string, (char*) "AAAA" },
62664c6703ed9e8d8f4f8e4c5f5e893559ecefecnd { 'b', specifier_string, (char*) "BBBB" },
62664c6703ed9e8d8f4f8e4c5f5e893559ecefecnd { 0, NULL, NULL }
62664c6703ed9e8d8f4f8e4c5f5e893559ecefecnd };
62664c6703ed9e8d8f4f8e4c5f5e893559ecefecnd
62664c6703ed9e8d8f4f8e4c5f5e893559ecefecnd w = specifier_printf("xxx a=%a b=%b yyy", table, NULL);
62664c6703ed9e8d8f4f8e4c5f5e893559ecefecnd printf("<%s>\n", w);
62664c6703ed9e8d8f4f8e4c5f5e893559ecefecnd free(w);
62664c6703ed9e8d8f4f8e4c5f5e893559ecefecnd}
62664c6703ed9e8d8f4f8e4c5f5e893559ecefecnd
62664c6703ed9e8d8f4f8e4c5f5e893559ecefecndstatic void test_strv_find(void) {
62664c6703ed9e8d8f4f8e4c5f5e893559ecefecnd const char * const input_table[] = {
62664c6703ed9e8d8f4f8e4c5f5e893559ecefecnd "one",
62664c6703ed9e8d8f4f8e4c5f5e893559ecefecnd "two",
62664c6703ed9e8d8f4f8e4c5f5e893559ecefecnd "three",
62664c6703ed9e8d8f4f8e4c5f5e893559ecefecnd NULL
62664c6703ed9e8d8f4f8e4c5f5e893559ecefecnd };
62664c6703ed9e8d8f4f8e4c5f5e893559ecefecnd
62664c6703ed9e8d8f4f8e4c5f5e893559ecefecnd assert(strv_find((char **)input_table, "three"));
62664c6703ed9e8d8f4f8e4c5f5e893559ecefecnd assert(!strv_find((char **)input_table, "four"));
62664c6703ed9e8d8f4f8e4c5f5e893559ecefecnd}
62664c6703ed9e8d8f4f8e4c5f5e893559ecefecnd
62664c6703ed9e8d8f4f8e4c5f5e893559ecefecndstatic void test_strv_find_prefix(void) {
62664c6703ed9e8d8f4f8e4c5f5e893559ecefecnd const char * const input_table[] = {
62664c6703ed9e8d8f4f8e4c5f5e893559ecefecnd "one",
62664c6703ed9e8d8f4f8e4c5f5e893559ecefecnd "two",
62664c6703ed9e8d8f4f8e4c5f5e893559ecefecnd "three",
62664c6703ed9e8d8f4f8e4c5f5e893559ecefecnd NULL
62664c6703ed9e8d8f4f8e4c5f5e893559ecefecnd };
62664c6703ed9e8d8f4f8e4c5f5e893559ecefecnd
62664c6703ed9e8d8f4f8e4c5f5e893559ecefecnd assert(strv_find_prefix((char **)input_table, "o"));
62664c6703ed9e8d8f4f8e4c5f5e893559ecefecnd assert(strv_find_prefix((char **)input_table, "one"));
62664c6703ed9e8d8f4f8e4c5f5e893559ecefecnd assert(strv_find_prefix((char **)input_table, ""));
62664c6703ed9e8d8f4f8e4c5f5e893559ecefecnd assert(!strv_find_prefix((char **)input_table, "xxx"));
62664c6703ed9e8d8f4f8e4c5f5e893559ecefecnd assert(!strv_find_prefix((char **)input_table, "onee"));
62664c6703ed9e8d8f4f8e4c5f5e893559ecefecnd}
62664c6703ed9e8d8f4f8e4c5f5e893559ecefecnd
62664c6703ed9e8d8f4f8e4c5f5e893559ecefecndstatic void test_strv_join(void) {
62664c6703ed9e8d8f4f8e4c5f5e893559ecefecnd _cleanup_free_ char *p = NULL, *q = NULL, *r = NULL, *s = NULL, *t = NULL;
62664c6703ed9e8d8f4f8e4c5f5e893559ecefecnd
62664c6703ed9e8d8f4f8e4c5f5e893559ecefecnd const char * const input_table_multiple[] = {
62664c6703ed9e8d8f4f8e4c5f5e893559ecefecnd "one",
62664c6703ed9e8d8f4f8e4c5f5e893559ecefecnd "two",
62664c6703ed9e8d8f4f8e4c5f5e893559ecefecnd "three",
62664c6703ed9e8d8f4f8e4c5f5e893559ecefecnd NULL
62664c6703ed9e8d8f4f8e4c5f5e893559ecefecnd };
62664c6703ed9e8d8f4f8e4c5f5e893559ecefecnd const char * const input_table_one[] = {
62664c6703ed9e8d8f4f8e4c5f5e893559ecefecnd "one",
62664c6703ed9e8d8f4f8e4c5f5e893559ecefecnd NULL
62664c6703ed9e8d8f4f8e4c5f5e893559ecefecnd };
62664c6703ed9e8d8f4f8e4c5f5e893559ecefecnd const char * const input_table_none[] = {
62664c6703ed9e8d8f4f8e4c5f5e893559ecefecnd NULL
62664c6703ed9e8d8f4f8e4c5f5e893559ecefecnd };
62664c6703ed9e8d8f4f8e4c5f5e893559ecefecnd
62664c6703ed9e8d8f4f8e4c5f5e893559ecefecnd p = strv_join((char **)input_table_multiple, ", ");
62664c6703ed9e8d8f4f8e4c5f5e893559ecefecnd assert(streq(p, "one, two, three"));
62664c6703ed9e8d8f4f8e4c5f5e893559ecefecnd
62664c6703ed9e8d8f4f8e4c5f5e893559ecefecnd q = strv_join((char **)input_table_multiple, ";");
62664c6703ed9e8d8f4f8e4c5f5e893559ecefecnd assert(streq(q, "one;two;three"));
62664c6703ed9e8d8f4f8e4c5f5e893559ecefecnd
62664c6703ed9e8d8f4f8e4c5f5e893559ecefecnd r = strv_join((char **)input_table_multiple, NULL);
62664c6703ed9e8d8f4f8e4c5f5e893559ecefecnd assert(streq(r, "one two three"));
cd4620da15c004edd01caaa4716fd9cfd818b4c7nd
cd4620da15c004edd01caaa4716fd9cfd818b4c7nd s = strv_join((char **)input_table_one, ", ");
62664c6703ed9e8d8f4f8e4c5f5e893559ecefecnd assert(streq(s, "one"));
62664c6703ed9e8d8f4f8e4c5f5e893559ecefecnd
62664c6703ed9e8d8f4f8e4c5f5e893559ecefecnd t = strv_join((char **)input_table_none, ", ");
62664c6703ed9e8d8f4f8e4c5f5e893559ecefecnd assert(streq(t, ""));
62664c6703ed9e8d8f4f8e4c5f5e893559ecefecnd}
62664c6703ed9e8d8f4f8e4c5f5e893559ecefecnd
62664c6703ed9e8d8f4f8e4c5f5e893559ecefecndstatic void test_strv_parse_nulstr(void) {
62664c6703ed9e8d8f4f8e4c5f5e893559ecefecnd _cleanup_strv_free_ char **l = NULL;
62664c6703ed9e8d8f4f8e4c5f5e893559ecefecnd const char nulstr[] = "fuck\0fuck2\0fuck3\0\0fuck5\0\0xxx";
62664c6703ed9e8d8f4f8e4c5f5e893559ecefecnd
62664c6703ed9e8d8f4f8e4c5f5e893559ecefecnd l = strv_parse_nulstr(nulstr, sizeof(nulstr)-1);
62664c6703ed9e8d8f4f8e4c5f5e893559ecefecnd puts("Parse nulstr:");
62664c6703ed9e8d8f4f8e4c5f5e893559ecefecnd strv_print(l);
62664c6703ed9e8d8f4f8e4c5f5e893559ecefecnd
62664c6703ed9e8d8f4f8e4c5f5e893559ecefecnd assert(streq(l[0], "fuck"));
62664c6703ed9e8d8f4f8e4c5f5e893559ecefecnd assert(streq(l[1], "fuck2"));
62664c6703ed9e8d8f4f8e4c5f5e893559ecefecnd assert(streq(l[2], "fuck3"));
62664c6703ed9e8d8f4f8e4c5f5e893559ecefecnd assert(streq(l[3], ""));
62664c6703ed9e8d8f4f8e4c5f5e893559ecefecnd assert(streq(l[4], "fuck5"));
62664c6703ed9e8d8f4f8e4c5f5e893559ecefecnd assert(streq(l[5], ""));
62664c6703ed9e8d8f4f8e4c5f5e893559ecefecnd assert(streq(l[6], "xxx"));
62664c6703ed9e8d8f4f8e4c5f5e893559ecefecnd}
62664c6703ed9e8d8f4f8e4c5f5e893559ecefecnd
62664c6703ed9e8d8f4f8e4c5f5e893559ecefecndstatic void test_strv_overlap(void) {
62664c6703ed9e8d8f4f8e4c5f5e893559ecefecnd const char * const input_table[] = {
62664c6703ed9e8d8f4f8e4c5f5e893559ecefecnd "one",
62664c6703ed9e8d8f4f8e4c5f5e893559ecefecnd "two",
62664c6703ed9e8d8f4f8e4c5f5e893559ecefecnd "three",
62664c6703ed9e8d8f4f8e4c5f5e893559ecefecnd NULL
62664c6703ed9e8d8f4f8e4c5f5e893559ecefecnd };
62664c6703ed9e8d8f4f8e4c5f5e893559ecefecnd const char * const input_table_overlap[] = {
62664c6703ed9e8d8f4f8e4c5f5e893559ecefecnd "two",
62664c6703ed9e8d8f4f8e4c5f5e893559ecefecnd NULL
62664c6703ed9e8d8f4f8e4c5f5e893559ecefecnd };
62664c6703ed9e8d8f4f8e4c5f5e893559ecefecnd const char * const input_table_unique[] = {
62664c6703ed9e8d8f4f8e4c5f5e893559ecefecnd "four",
62664c6703ed9e8d8f4f8e4c5f5e893559ecefecnd "five",
62664c6703ed9e8d8f4f8e4c5f5e893559ecefecnd "six",
62664c6703ed9e8d8f4f8e4c5f5e893559ecefecnd NULL
62664c6703ed9e8d8f4f8e4c5f5e893559ecefecnd };
62664c6703ed9e8d8f4f8e4c5f5e893559ecefecnd
62664c6703ed9e8d8f4f8e4c5f5e893559ecefecnd assert(strv_overlap((char **)input_table, (char**)input_table_overlap));
62664c6703ed9e8d8f4f8e4c5f5e893559ecefecnd assert(!strv_overlap((char **)input_table, (char**)input_table_unique));
62664c6703ed9e8d8f4f8e4c5f5e893559ecefecnd}
62664c6703ed9e8d8f4f8e4c5f5e893559ecefecnd
62664c6703ed9e8d8f4f8e4c5f5e893559ecefecndstatic void test_strv_sort(void) {
62664c6703ed9e8d8f4f8e4c5f5e893559ecefecnd const char * const input_table[] = {
62664c6703ed9e8d8f4f8e4c5f5e893559ecefecnd "durian",
62664c6703ed9e8d8f4f8e4c5f5e893559ecefecnd "apple",
62664c6703ed9e8d8f4f8e4c5f5e893559ecefecnd "citrus",
62664c6703ed9e8d8f4f8e4c5f5e893559ecefecnd "CAPITAL LETTERS FIRST",
62664c6703ed9e8d8f4f8e4c5f5e893559ecefecnd "banana",
62664c6703ed9e8d8f4f8e4c5f5e893559ecefecnd NULL
62664c6703ed9e8d8f4f8e4c5f5e893559ecefecnd };
62664c6703ed9e8d8f4f8e4c5f5e893559ecefecnd
62664c6703ed9e8d8f4f8e4c5f5e893559ecefecnd strv_sort((char **)input_table);
62664c6703ed9e8d8f4f8e4c5f5e893559ecefecnd
62664c6703ed9e8d8f4f8e4c5f5e893559ecefecnd assert(streq(input_table[0], "CAPITAL LETTERS FIRST"));
62664c6703ed9e8d8f4f8e4c5f5e893559ecefecnd assert(streq(input_table[1], "apple"));
62664c6703ed9e8d8f4f8e4c5f5e893559ecefecnd assert(streq(input_table[2], "banana"));
62664c6703ed9e8d8f4f8e4c5f5e893559ecefecnd assert(streq(input_table[3], "citrus"));
62664c6703ed9e8d8f4f8e4c5f5e893559ecefecnd assert(streq(input_table[4], "durian"));
62664c6703ed9e8d8f4f8e4c5f5e893559ecefecnd}
62664c6703ed9e8d8f4f8e4c5f5e893559ecefecnd
62664c6703ed9e8d8f4f8e4c5f5e893559ecefecndstatic void test_strv_merge(void) {
62664c6703ed9e8d8f4f8e4c5f5e893559ecefecnd _cleanup_strv_free_ char **a = NULL, **b = NULL, **c = NULL;
62664c6703ed9e8d8f4f8e4c5f5e893559ecefecnd
62664c6703ed9e8d8f4f8e4c5f5e893559ecefecnd a = strv_new("abc", "def", "ghi", NULL);
62664c6703ed9e8d8f4f8e4c5f5e893559ecefecnd b = strv_new("jkl", "mno", "pqr", NULL);
62664c6703ed9e8d8f4f8e4c5f5e893559ecefecnd
62664c6703ed9e8d8f4f8e4c5f5e893559ecefecnd c = strv_merge(a, b);
62664c6703ed9e8d8f4f8e4c5f5e893559ecefecnd
62664c6703ed9e8d8f4f8e4c5f5e893559ecefecnd assert(streq(c[0], "abc"));
62664c6703ed9e8d8f4f8e4c5f5e893559ecefecnd assert(streq(c[1], "def"));
62664c6703ed9e8d8f4f8e4c5f5e893559ecefecnd assert(streq(c[2], "ghi"));
62664c6703ed9e8d8f4f8e4c5f5e893559ecefecnd assert(streq(c[3], "jkl"));
62664c6703ed9e8d8f4f8e4c5f5e893559ecefecnd assert(streq(c[4], "mno"));
62664c6703ed9e8d8f4f8e4c5f5e893559ecefecnd assert(streq(c[5], "pqr"));
62664c6703ed9e8d8f4f8e4c5f5e893559ecefecnd
62664c6703ed9e8d8f4f8e4c5f5e893559ecefecnd assert(strv_length(c) == 6);
62664c6703ed9e8d8f4f8e4c5f5e893559ecefecnd}
62664c6703ed9e8d8f4f8e4c5f5e893559ecefecnd
62664c6703ed9e8d8f4f8e4c5f5e893559ecefecndint main(int argc, char *argv[]) {
test_specifier_printf();
test_strv_find();
test_strv_find_prefix();
test_strv_join();
test_strv_parse_nulstr();
test_strv_overlap();
test_strv_sort();
test_strv_merge();
return 0;
}