chiark / gitweb /
ea1f0881feead8c93c8fa9ce58a186a933053a01
[elogind.git] / src / test / test-strv.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /***
4   This file is part of systemd.
5
6   Copyright 2010 Lennart Poettering
7   Copyright 2013 Thomas H.P. Andersen
8
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.
13
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.
18
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/>.
21 ***/
22
23 #include <string.h>
24
25 #include "util.h"
26 #include "specifier.h"
27 #include "strv.h"
28
29 static void test_specifier_printf(void) {
30         char *w;
31
32         const Specifier table[] = {
33                 { 'a', specifier_string, (char*) "AAAA" },
34                 { 'b', specifier_string, (char*) "BBBB" },
35                 { 0, NULL, NULL }
36         };
37
38         w = specifier_printf("xxx a=%a b=%b yyy", table, NULL);
39         printf("<%s>\n", w);
40         free(w);
41 }
42
43 static void test_strv_find(void) {
44         const char * const input_table[] = {
45                 "one",
46                 "two",
47                 "three",
48                 NULL
49         };
50
51         assert_se(strv_find((char **)input_table, "three"));
52         assert_se(!strv_find((char **)input_table, "four"));
53 }
54
55 static void test_strv_find_prefix(void) {
56         const char * const input_table[] = {
57                 "one",
58                 "two",
59                 "three",
60                 NULL
61         };
62
63         assert_se(strv_find_prefix((char **)input_table, "o"));
64         assert_se(strv_find_prefix((char **)input_table, "one"));
65         assert_se(strv_find_prefix((char **)input_table, ""));
66         assert_se(!strv_find_prefix((char **)input_table, "xxx"));
67         assert_se(!strv_find_prefix((char **)input_table, "onee"));
68 }
69
70 static void test_strv_join(void) {
71         _cleanup_free_ char *p = NULL, *q = NULL, *r = NULL, *s = NULL, *t = NULL;
72
73         const char * const input_table_multiple[] = {
74                 "one",
75                 "two",
76                 "three",
77                 NULL
78         };
79         const char * const input_table_one[] = {
80                 "one",
81                 NULL
82         };
83         const char * const input_table_none[] = {
84                 NULL
85         };
86
87         p = strv_join((char **)input_table_multiple, ", ");
88         assert_se(streq(p, "one, two, three"));
89
90         q = strv_join((char **)input_table_multiple, ";");
91         assert_se(streq(q, "one;two;three"));
92
93         r = strv_join((char **)input_table_multiple, NULL);
94         assert_se(streq(r, "one two three"));
95
96         s = strv_join((char **)input_table_one, ", ");
97         assert_se(streq(s, "one"));
98
99         t = strv_join((char **)input_table_none, ", ");
100         assert_se(streq(t, ""));
101 }
102
103 static void test_strv_split_nulstr(void) {
104         _cleanup_strv_free_ char **l = NULL;
105         const char nulstr[] = "str0\0str1\0str2\0str3\0";
106
107         l = strv_split_nulstr (nulstr);
108
109         assert_se(streq(l[0], "str0"));
110         assert_se(streq(l[1], "str1"));
111         assert_se(streq(l[2], "str2"));
112         assert_se(streq(l[3], "str3"));
113 }
114
115 static void test_strv_parse_nulstr(void) {
116         _cleanup_strv_free_ char **l = NULL;
117         const char nulstr[] = "fuck\0fuck2\0fuck3\0\0fuck5\0\0xxx";
118
119         l = strv_parse_nulstr(nulstr, sizeof(nulstr)-1);
120         puts("Parse nulstr:");
121         strv_print(l);
122
123         assert_se(streq(l[0], "fuck"));
124         assert_se(streq(l[1], "fuck2"));
125         assert_se(streq(l[2], "fuck3"));
126         assert_se(streq(l[3], ""));
127         assert_se(streq(l[4], "fuck5"));
128         assert_se(streq(l[5], ""));
129         assert_se(streq(l[6], "xxx"));
130 }
131
132 static void test_strv_overlap(void) {
133         const char * const input_table[] = {
134                 "one",
135                 "two",
136                 "three",
137                 NULL
138         };
139         const char * const input_table_overlap[] = {
140                 "two",
141                 NULL
142         };
143         const char * const input_table_unique[] = {
144                 "four",
145                 "five",
146                 "six",
147                 NULL
148         };
149
150         assert_se(strv_overlap((char **)input_table, (char**)input_table_overlap));
151         assert_se(!strv_overlap((char **)input_table, (char**)input_table_unique));
152 }
153
154 static void test_strv_sort(void) {
155         const char * const input_table[] = {
156                 "durian",
157                 "apple",
158                 "citrus",
159                  "CAPITAL LETTERS FIRST",
160                 "banana",
161                 NULL
162         };
163
164         strv_sort((char **)input_table);
165
166         assert_se(streq(input_table[0], "CAPITAL LETTERS FIRST"));
167         assert_se(streq(input_table[1], "apple"));
168         assert_se(streq(input_table[2], "banana"));
169         assert_se(streq(input_table[3], "citrus"));
170         assert_se(streq(input_table[4], "durian"));
171 }
172
173 static void test_strv_merge_concat(void) {
174          _cleanup_strv_free_ char **a = NULL, **b = NULL, **c = NULL;
175
176         a = strv_new("without", "suffix", NULL);
177         b = strv_new("with", "suffix", NULL);
178
179         c = strv_merge_concat(a, b, "_suffix");
180
181         assert_se(streq(c[0], "without"));
182         assert_se(streq(c[1], "suffix"));
183         assert_se(streq(c[2], "with_suffix"));
184         assert_se(streq(c[3], "suffix_suffix"));
185 }
186
187 static void test_strv_merge(void) {
188          _cleanup_strv_free_ char **a = NULL, **b = NULL, **c = NULL;
189
190         a = strv_new("abc", "def", "ghi", NULL);
191         b = strv_new("jkl", "mno", "pqr", NULL);
192
193         c = strv_merge(a, b);
194
195         assert_se(streq(c[0], "abc"));
196         assert_se(streq(c[1], "def"));
197         assert_se(streq(c[2], "ghi"));
198         assert_se(streq(c[3], "jkl"));
199         assert_se(streq(c[4], "mno"));
200         assert_se(streq(c[5], "pqr"));
201
202         assert_se(strv_length(c) == 6);
203 }
204
205 static void test_strv_append(void) {
206         _cleanup_strv_free_ char **a = NULL, **b = NULL, **c = NULL;
207
208         a = strv_new("test", "test1", NULL);
209         b = strv_append(a, "test2");
210         c = strv_append(NULL, "test3");
211
212         assert_se(streq(b[0], "test"));
213         assert_se(streq(b[1], "test1"));
214         assert_se(streq(b[2], "test2"));
215         assert_se(streq(c[0], "test3"));
216 }
217
218 int main(int argc, char *argv[]) {
219         test_specifier_printf();
220         test_strv_find();
221         test_strv_find_prefix();
222         test_strv_join();
223         test_strv_split_nulstr();
224         test_strv_parse_nulstr();
225         test_strv_overlap();
226         test_strv_sort();
227         test_strv_merge();
228         test_strv_merge_concat();
229         test_strv_append();
230
231         return 0;
232 }