chiark / gitweb /
missing: define MS_REC and MS_SHARED if not defined already
[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_se(streq_ptr(NULL, NULL));
29         assert_se(!streq_ptr("abc", "cdef"));
30 }
31
32 static void test_first_word(void) {
33         assert_se(first_word("Hello", ""));
34         assert_se(first_word("Hello", "Hello"));
35         assert_se(first_word("Hello world", "Hello"));
36         assert_se(first_word("Hello\tworld", "Hello"));
37         assert_se(first_word("Hello\nworld", "Hello"));
38         assert_se(first_word("Hello\rworld", "Hello"));
39         assert_se(first_word("Hello ", "Hello"));
40
41         assert_se(!first_word("Hello", "Hellooo"));
42         assert_se(!first_word("Hello", "xxxxx"));
43         assert_se(!first_word("Hellooo", "Hello"));
44 }
45
46 static void test_parse_boolean(void) {
47         assert_se(parse_boolean("1") == 1);
48         assert_se(parse_boolean("y") == 1);
49         assert_se(parse_boolean("Y") == 1);
50         assert_se(parse_boolean("yes") == 1);
51         assert_se(parse_boolean("YES") == 1);
52         assert_se(parse_boolean("true") == 1);
53         assert_se(parse_boolean("TRUE") == 1);
54         assert_se(parse_boolean("on") == 1);
55         assert_se(parse_boolean("ON") == 1);
56
57         assert_se(parse_boolean("0") == 0);
58         assert_se(parse_boolean("n") == 0);
59         assert_se(parse_boolean("N") == 0);
60         assert_se(parse_boolean("no") == 0);
61         assert_se(parse_boolean("NO") == 0);
62         assert_se(parse_boolean("false") == 0);
63         assert_se(parse_boolean("FALSE") == 0);
64         assert_se(parse_boolean("off") == 0);
65         assert_se(parse_boolean("OFF") == 0);
66
67         assert_se(parse_boolean("garbage") < 0);
68         assert_se(parse_boolean("") < 0);
69 }
70
71 static void test_foreach_word_quoted(void) {
72         char *w, *state;
73         size_t l;
74         const char test[] = "test a b c 'd' e '' '' hhh '' ''";
75         printf("<%s>\n", test);
76         FOREACH_WORD_QUOTED(w, l, test, state) {
77                 char *t;
78
79                 assert_se(t = strndup(w, l));
80                 printf("<%s>\n", t);
81                 free(t);
82         }
83 }
84
85 static void test_default_term_for_tty(void) {
86         puts(default_term_for_tty("/dev/tty23"));
87         puts(default_term_for_tty("/dev/ttyS23"));
88         puts(default_term_for_tty("/dev/tty0"));
89         puts(default_term_for_tty("/dev/pty0"));
90         puts(default_term_for_tty("/dev/pts/0"));
91         puts(default_term_for_tty("/dev/console"));
92         puts(default_term_for_tty("tty23"));
93         puts(default_term_for_tty("ttyS23"));
94         puts(default_term_for_tty("tty0"));
95         puts(default_term_for_tty("pty0"));
96         puts(default_term_for_tty("pts/0"));
97         puts(default_term_for_tty("console"));
98 }
99
100 int main(int argc, char *argv[]) {
101         test_streq_ptr();
102         test_first_word();
103         test_parse_boolean();
104         test_default_term_for_tty();
105         test_foreach_word_quoted();
106
107         return 0;
108 }