]>
git.proxmox.com Git - systemd.git/blob - src/test/test-strv.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2010 Lennart Poettering
7 Copyright 2013 Thomas H.P. Andersen
9 systemd is free software; you can redistribute it and/or modify it
10 under the terms of the GNU Lesser General Public License as published by
11 the Free Software Foundation; either version 2.1 of the License, or
12 (at your option) any later version.
14 systemd is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 Lesser General Public License for more details.
19 You should have received a copy of the GNU Lesser General Public License
20 along with systemd; If not, see <http://www.gnu.org/licenses/>.
26 #include "specifier.h"
29 static void test_specifier_printf(void) {
32 const Specifier table
[] = {
33 { 'a', specifier_string
, (char*) "AAAA" },
34 { 'b', specifier_string
, (char*) "BBBB" },
38 w
= specifier_printf("xxx a=%a b=%b yyy", table
, NULL
);
43 static void test_strv_find(void) {
44 const char * const input_table
[] = {
51 assert(strv_find((char **)input_table
, "three"));
52 assert(!strv_find((char **)input_table
, "four"));
55 static void test_strv_find_prefix(void) {
56 const char * const input_table
[] = {
63 assert(strv_find_prefix((char **)input_table
, "o"));
64 assert(strv_find_prefix((char **)input_table
, "one"));
65 assert(strv_find_prefix((char **)input_table
, ""));
66 assert(!strv_find_prefix((char **)input_table
, "xxx"));
67 assert(!strv_find_prefix((char **)input_table
, "onee"));
70 static void test_strv_join(void) {
71 _cleanup_free_
char *p
= NULL
, *q
= NULL
, *r
= NULL
, *s
= NULL
, *t
= NULL
;
73 const char * const input_table_multiple
[] = {
79 const char * const input_table_one
[] = {
83 const char * const input_table_none
[] = {
87 p
= strv_join((char **)input_table_multiple
, ", ");
88 assert(streq(p
, "one, two, three"));
90 q
= strv_join((char **)input_table_multiple
, ";");
91 assert(streq(q
, "one;two;three"));
93 r
= strv_join((char **)input_table_multiple
, NULL
);
94 assert(streq(r
, "one two three"));
96 s
= strv_join((char **)input_table_one
, ", ");
97 assert(streq(s
, "one"));
99 t
= strv_join((char **)input_table_none
, ", ");
100 assert(streq(t
, ""));
103 static void test_strv_parse_nulstr(void) {
104 _cleanup_strv_free_
char **l
= NULL
;
105 const char nulstr
[] = "fuck\0fuck2\0fuck3\0\0fuck5\0\0xxx";
107 l
= strv_parse_nulstr(nulstr
, sizeof(nulstr
)-1);
108 puts("Parse nulstr:");
111 assert(streq(l
[0], "fuck"));
112 assert(streq(l
[1], "fuck2"));
113 assert(streq(l
[2], "fuck3"));
114 assert(streq(l
[3], ""));
115 assert(streq(l
[4], "fuck5"));
116 assert(streq(l
[5], ""));
117 assert(streq(l
[6], "xxx"));
120 static void test_strv_overlap(void) {
121 const char * const input_table
[] = {
127 const char * const input_table_overlap
[] = {
131 const char * const input_table_unique
[] = {
138 assert(strv_overlap((char **)input_table
, (char**)input_table_overlap
));
139 assert(!strv_overlap((char **)input_table
, (char**)input_table_unique
));
142 static void test_strv_sort(void) {
143 const char * const input_table
[] = {
147 "CAPITAL LETTERS FIRST",
152 strv_sort((char **)input_table
);
154 assert(streq(input_table
[0], "CAPITAL LETTERS FIRST"));
155 assert(streq(input_table
[1], "apple"));
156 assert(streq(input_table
[2], "banana"));
157 assert(streq(input_table
[3], "citrus"));
158 assert(streq(input_table
[4], "durian"));
161 static void test_strv_merge_concat(void) {
162 _cleanup_strv_free_
char **a
= NULL
, **b
= NULL
, **c
= NULL
;
164 a
= strv_new("without", "suffix", NULL
);
165 b
= strv_new("with", "suffix", NULL
);
167 c
= strv_merge_concat(a
, b
, "_suffix");
169 assert(streq(c
[0], "without"));
170 assert(streq(c
[1], "suffix"));
171 assert(streq(c
[2], "with_suffix"));
172 assert(streq(c
[3], "suffix_suffix"));
175 static void test_strv_merge(void) {
176 _cleanup_strv_free_
char **a
= NULL
, **b
= NULL
, **c
= NULL
;
178 a
= strv_new("abc", "def", "ghi", NULL
);
179 b
= strv_new("jkl", "mno", "pqr", NULL
);
181 c
= strv_merge(a
, b
);
183 assert(streq(c
[0], "abc"));
184 assert(streq(c
[1], "def"));
185 assert(streq(c
[2], "ghi"));
186 assert(streq(c
[3], "jkl"));
187 assert(streq(c
[4], "mno"));
188 assert(streq(c
[5], "pqr"));
190 assert(strv_length(c
) == 6);
193 static void test_strv_append(void) {
194 _cleanup_strv_free_
char **a
= NULL
, **b
= NULL
, **c
= NULL
;
196 a
= strv_new("test", "test1", NULL
);
197 b
= strv_append(a
, "test2");
198 c
= strv_append(NULL
, "test3");
200 assert(streq(b
[0], "test"));
201 assert(streq(b
[1], "test1"));
202 assert(streq(b
[2], "test2"));
203 assert(streq(c
[0], "test3"));
206 int main(int argc
, char *argv
[]) {
207 test_specifier_printf();
209 test_strv_find_prefix();
211 test_strv_parse_nulstr();
215 test_strv_merge_concat();