chiark / gitweb /
7898ea7cf6169d6ab783f0162c5658bb22b1f423
[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
8   systemd is free software; you can redistribute it and/or modify it
9   under the terms of the GNU Lesser General Public License as published by
10   the Free Software Foundation; either version 2.1 of the License, or
11   (at your option) any later version.
12
13   systemd is distributed in the hope that it will be useful, but
14   WITHOUT ANY WARRANTY; without even the implied warranty of
15   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16   Lesser General Public License for more details.
17
18   You should have received a copy of the GNU Lesser General Public License
19   along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 ***/
21
22 #include <string.h>
23
24 #include "util.h"
25 #include "specifier.h"
26 #include "strv.h"
27
28 static void test_specifier_printf(void) {
29         char *w;
30
31         const Specifier table[] = {
32                 { 'a', specifier_string, (char*) "AAAA" },
33                 { 'b', specifier_string, (char*) "BBBB" },
34                 { 0, NULL, NULL }
35         };
36
37         w = specifier_printf("xxx a=%a b=%b yyy", table, NULL);
38         printf("<%s>\n", w);
39         free(w);
40 }
41
42 static void test_foreach_word_quoted(void) {
43         char *w, *state;
44         size_t l;
45         const char test[] = "test a b c 'd' e '' '' hhh '' ''";
46         printf("<%s>\n", test);
47         FOREACH_WORD_QUOTED(w, l, test, state) {
48                 char *t;
49
50                 assert_se(t = strndup(w, l));
51                 printf("<%s>\n", t);
52                 free(t);
53         }
54 }
55
56 static void test_strv_join(void) {
57         char *r;
58
59         const char * const input_table_multiple[] = {
60                 "one",
61                 "two",
62                 "three",
63                 NULL
64         };
65         const char * const input_table_one[] = {
66                 "one",
67                 NULL
68         };
69         const char * const input_table_none[] = {
70                 NULL
71         };
72
73         r = strv_join((char **)input_table_multiple, ", ");
74         assert_se(streq(r, "one, two, three"));
75         puts(r);
76         free(r);
77
78         r = strv_join((char **)input_table_multiple, ";");
79         assert_se(streq(r, "one;two;three"));
80         puts(r);
81         free(r);
82
83         r = strv_join((char **)input_table_multiple, NULL);
84         assert_se(streq(r, "one two three"));
85         puts(r);
86         free(r);
87
88         r = strv_join((char **)input_table_one, ", ");
89         assert_se(streq(r, "one"));
90         puts(r);
91         free(r);
92
93         r = strv_join((char **)input_table_none, ", ");
94         assert_se(streq(r, ""));
95         puts(r);
96         free(r);
97 }
98
99 static void test_default_term_for_tty(void) {
100         printf("%s\n", default_term_for_tty("/dev/tty23"));
101         printf("%s\n", default_term_for_tty("/dev/ttyS23"));
102         printf("%s\n", default_term_for_tty("/dev/tty0"));
103         printf("%s\n", default_term_for_tty("/dev/pty0"));
104         printf("%s\n", default_term_for_tty("/dev/pts/0"));
105         printf("%s\n", default_term_for_tty("/dev/console"));
106         printf("%s\n", default_term_for_tty("tty23"));
107         printf("%s\n", default_term_for_tty("ttyS23"));
108         printf("%s\n", default_term_for_tty("tty0"));
109         printf("%s\n", default_term_for_tty("pty0"));
110         printf("%s\n", default_term_for_tty("pts/0"));
111         printf("%s\n", default_term_for_tty("console"));
112 }
113
114 int main(int argc, char *argv[]) {
115         test_default_term_for_tty();
116         test_foreach_word_quoted();
117         test_specifier_printf();
118         test_strv_join();
119
120         return 0;
121 }