chiark / gitweb /
test-strv.c: added OOM check for current tests
[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(p);
89         assert_se(streq(p, "one, two, three"));
90
91         q = strv_join((char **)input_table_multiple, ";");
92         assert_se(q);
93         assert_se(streq(q, "one;two;three"));
94
95         r = strv_join((char **)input_table_multiple, NULL);
96         assert_se(r);
97         assert_se(streq(r, "one two three"));
98
99         s = strv_join((char **)input_table_one, ", ");
100         assert_se(s);
101         assert_se(streq(s, "one"));
102
103         t = strv_join((char **)input_table_none, ", ");
104         assert_se(t);
105         assert_se(streq(t, ""));
106 }
107
108 static void test_strv_split_nulstr(void) {
109         _cleanup_strv_free_ char **l = NULL;
110         const char nulstr[] = "str0\0str1\0str2\0str3\0";
111
112         l = strv_split_nulstr (nulstr);
113         assert_se(l);
114
115         assert_se(streq(l[0], "str0"));
116         assert_se(streq(l[1], "str1"));
117         assert_se(streq(l[2], "str2"));
118         assert_se(streq(l[3], "str3"));
119 }
120
121 static void test_strv_parse_nulstr(void) {
122         _cleanup_strv_free_ char **l = NULL;
123         const char nulstr[] = "fuck\0fuck2\0fuck3\0\0fuck5\0\0xxx";
124
125         l = strv_parse_nulstr(nulstr, sizeof(nulstr)-1);
126         assert_se(l);
127         puts("Parse nulstr:");
128         strv_print(l);
129
130         assert_se(streq(l[0], "fuck"));
131         assert_se(streq(l[1], "fuck2"));
132         assert_se(streq(l[2], "fuck3"));
133         assert_se(streq(l[3], ""));
134         assert_se(streq(l[4], "fuck5"));
135         assert_se(streq(l[5], ""));
136         assert_se(streq(l[6], "xxx"));
137 }
138
139 static void test_strv_overlap(void) {
140         const char * const input_table[] = {
141                 "one",
142                 "two",
143                 "three",
144                 NULL
145         };
146         const char * const input_table_overlap[] = {
147                 "two",
148                 NULL
149         };
150         const char * const input_table_unique[] = {
151                 "four",
152                 "five",
153                 "six",
154                 NULL
155         };
156
157         assert_se(strv_overlap((char **)input_table, (char**)input_table_overlap));
158         assert_se(!strv_overlap((char **)input_table, (char**)input_table_unique));
159 }
160
161 static void test_strv_sort(void) {
162         const char * const input_table[] = {
163                 "durian",
164                 "apple",
165                 "citrus",
166                  "CAPITAL LETTERS FIRST",
167                 "banana",
168                 NULL
169         };
170
171         strv_sort((char **)input_table);
172
173         assert_se(streq(input_table[0], "CAPITAL LETTERS FIRST"));
174         assert_se(streq(input_table[1], "apple"));
175         assert_se(streq(input_table[2], "banana"));
176         assert_se(streq(input_table[3], "citrus"));
177         assert_se(streq(input_table[4], "durian"));
178 }
179
180 static void test_strv_merge_concat(void) {
181          _cleanup_strv_free_ char **a = NULL, **b = NULL, **c = NULL;
182
183         a = strv_new("without", "suffix", NULL);
184         b = strv_new("with", "suffix", NULL);
185         assert_se(a);
186         assert_se(b);
187
188         c = strv_merge_concat(a, b, "_suffix");
189         assert_se(c);
190
191         assert_se(streq(c[0], "without"));
192         assert_se(streq(c[1], "suffix"));
193         assert_se(streq(c[2], "with_suffix"));
194         assert_se(streq(c[3], "suffix_suffix"));
195 }
196
197 static void test_strv_merge(void) {
198          _cleanup_strv_free_ char **a = NULL, **b = NULL, **c = NULL;
199
200         a = strv_new("abc", "def", "ghi", NULL);
201         b = strv_new("jkl", "mno", "pqr", NULL);
202         assert_se(a);
203         assert_se(b);
204
205         c = strv_merge(a, b);
206         assert_se(c);
207
208         assert_se(streq(c[0], "abc"));
209         assert_se(streq(c[1], "def"));
210         assert_se(streq(c[2], "ghi"));
211         assert_se(streq(c[3], "jkl"));
212         assert_se(streq(c[4], "mno"));
213         assert_se(streq(c[5], "pqr"));
214
215         assert_se(strv_length(c) == 6);
216 }
217
218 static void test_strv_append(void) {
219         _cleanup_strv_free_ char **a = NULL, **b = NULL, **c = NULL;
220
221         a = strv_new("test", "test1", NULL);
222         assert_se(a);
223         b = strv_append(a, "test2");
224         c = strv_append(NULL, "test3");
225         assert_se(b);
226         assert_se(c);
227
228         assert_se(streq(b[0], "test"));
229         assert_se(streq(b[1], "test1"));
230         assert_se(streq(b[2], "test2"));
231         assert_se(streq(c[0], "test3"));
232 }
233
234 int main(int argc, char *argv[]) {
235         test_specifier_printf();
236         test_strv_find();
237         test_strv_find_prefix();
238         test_strv_join();
239         test_strv_split_nulstr();
240         test_strv_parse_nulstr();
241         test_strv_overlap();
242         test_strv_sort();
243         test_strv_merge();
244         test_strv_merge_concat();
245         test_strv_append();
246
247         return 0;
248 }