chiark / gitweb /
ebbdcfc6de454e22c99577ee53a35e1f09c5b957
[elogind.git] / src / test / test-util.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
27 static void test_streq_ptr(void) {
28         assert(streq_ptr(NULL, NULL));
29         assert(!streq_ptr("abc", "cdef"));
30 }
31
32 static void test_first_word(void) {
33         assert(first_word("Hello", ""));
34         assert(first_word("Hello", "Hello"));
35         assert(first_word("Hello world", "Hello"));
36         assert(first_word("Hello\tworld", "Hello"));
37         assert(first_word("Hello\nworld", "Hello"));
38         assert(first_word("Hello\rworld", "Hello"));
39         assert(first_word("Hello ", "Hello"));
40
41         assert(!first_word("Hello", "Hellooo"));
42         assert(!first_word("Hello", "xxxxx"));
43         assert(!first_word("Hellooo", "Hello"));
44 }
45
46 static void test_foreach_word_quoted(void) {
47         char *w, *state;
48         size_t l;
49         const char test[] = "test a b c 'd' e '' '' hhh '' ''";
50         printf("<%s>\n", test);
51         FOREACH_WORD_QUOTED(w, l, test, state) {
52                 char *t;
53
54                 assert_se(t = strndup(w, l));
55                 printf("<%s>\n", t);
56                 free(t);
57         }
58 }
59
60 static void test_default_term_for_tty(void) {
61         puts(default_term_for_tty("/dev/tty23"));
62         puts(default_term_for_tty("/dev/ttyS23"));
63         puts(default_term_for_tty("/dev/tty0"));
64         puts(default_term_for_tty("/dev/pty0"));
65         puts(default_term_for_tty("/dev/pts/0"));
66         puts(default_term_for_tty("/dev/console"));
67         puts(default_term_for_tty("tty23"));
68         puts(default_term_for_tty("ttyS23"));
69         puts(default_term_for_tty("tty0"));
70         puts(default_term_for_tty("pty0"));
71         puts(default_term_for_tty("pts/0"));
72         puts(default_term_for_tty("console"));
73 }
74
75 int main(int argc, char *argv[]) {
76         test_streq_ptr();
77         test_first_word();
78         test_default_term_for_tty();
79         test_foreach_word_quoted();
80
81         return 0;
82 }