chiark / gitweb /
1df8157b597cbfc8b9d9eb3ffda141ea3593583e
[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(strv_find((char **)input_table, "three"));
52         assert(!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(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"));
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(streq(p, "one, two, three"));
89
90         q = strv_join((char **)input_table_multiple, ";");
91         assert(streq(q, "one;two;three"));
92
93         r = strv_join((char **)input_table_multiple, NULL);
94         assert(streq(r, "one two three"));
95
96         s = strv_join((char **)input_table_one, ", ");
97         assert(streq(s, "one"));
98
99         t = strv_join((char **)input_table_none, ", ");
100         assert(streq(t, ""));
101 }
102
103 static void test_strv_overlap(void) {
104         const char * const input_table[] = {
105                 "one",
106                 "two",
107                 "three",
108                 NULL
109         };
110         const char * const input_table_overlap[] = {
111                 "two",
112                 NULL
113         };
114         const char * const input_table_unique[] = {
115                 "four",
116                 "five",
117                 "six",
118                 NULL
119         };
120
121         assert(strv_overlap((char **)input_table, (char**)input_table_overlap));
122         assert(!strv_overlap((char **)input_table, (char**)input_table_unique));
123 }
124
125 static void test_strv_sort(void) {
126         const char * const input_table[] = {
127                 "durian",
128                 "apple",
129                 "citrus",
130                  "CAPITAL LETTERS FIRST",
131                 "banana",
132                 NULL
133         };
134
135         strv_sort((char **)input_table);
136
137         assert(streq(input_table[0], "CAPITAL LETTERS FIRST"));
138         assert(streq(input_table[1], "apple"));
139         assert(streq(input_table[2], "banana"));
140         assert(streq(input_table[3], "citrus"));
141         assert(streq(input_table[4], "durian"));
142 }
143
144 int main(int argc, char *argv[]) {
145         test_specifier_printf();
146         test_strv_find();
147         test_strv_find_prefix();
148         test_strv_join();
149         test_strv_overlap();
150         test_strv_sort();
151
152         return 0;
153 }