]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/test/test-string-util.c
tests: move hexdecoct tests to test-hexdecoct.c
[thirdparty/systemd.git] / src / test / test-string-util.c
CommitLineData
9fe4ea21
LP
1/***
2 This file is part of systemd.
3
4 Copyright 2015 Lennart Poettering
5
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
10
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
18***/
19
20#include "string-util.h"
21
22static void test_string_erase(void) {
23 char *x;
24
25 x = strdupa("");
26 assert_se(streq(string_erase(x), ""));
27
28 x = strdupa("1");
29 assert_se(streq(string_erase(x), "x"));
30
31 x = strdupa("12");
32 assert_se(streq(string_erase(x), "xx"));
33
34 x = strdupa("123");
35 assert_se(streq(string_erase(x), "xxx"));
36
37 x = strdupa("1234");
38 assert_se(streq(string_erase(x), "xxxx"));
39
40 x = strdupa("12345");
41 assert_se(streq(string_erase(x), "xxxxx"));
42
43 x = strdupa("123456");
44 assert_se(streq(string_erase(x), "xxxxxx"));
45
46 x = strdupa("1234567");
47 assert_se(streq(string_erase(x), "xxxxxxx"));
48
49 x = strdupa("12345678");
50 assert_se(streq(string_erase(x), "xxxxxxxx"));
51
52 x = strdupa("123456789");
53 assert_se(streq(string_erase(x), "xxxxxxxxx"));
54}
55
522d85ae
LP
56static void test_ascii_strcasecmp_n(void) {
57
58 assert_se(ascii_strcasecmp_n("", "", 0) == 0);
59 assert_se(ascii_strcasecmp_n("", "", 1) == 0);
60 assert_se(ascii_strcasecmp_n("", "a", 1) < 0);
61 assert_se(ascii_strcasecmp_n("", "a", 2) < 0);
62 assert_se(ascii_strcasecmp_n("a", "", 1) > 0);
63 assert_se(ascii_strcasecmp_n("a", "", 2) > 0);
64 assert_se(ascii_strcasecmp_n("a", "a", 1) == 0);
65 assert_se(ascii_strcasecmp_n("a", "a", 2) == 0);
66 assert_se(ascii_strcasecmp_n("a", "b", 1) < 0);
67 assert_se(ascii_strcasecmp_n("a", "b", 2) < 0);
68 assert_se(ascii_strcasecmp_n("b", "a", 1) > 0);
69 assert_se(ascii_strcasecmp_n("b", "a", 2) > 0);
70 assert_se(ascii_strcasecmp_n("xxxxyxxxx", "xxxxYxxxx", 9) == 0);
71 assert_se(ascii_strcasecmp_n("xxxxxxxxx", "xxxxyxxxx", 9) < 0);
72 assert_se(ascii_strcasecmp_n("xxxxXxxxx", "xxxxyxxxx", 9) < 0);
73 assert_se(ascii_strcasecmp_n("xxxxxxxxx", "xxxxYxxxx", 9) < 0);
74 assert_se(ascii_strcasecmp_n("xxxxXxxxx", "xxxxYxxxx", 9) < 0);
75
76 assert_se(ascii_strcasecmp_n("xxxxYxxxx", "xxxxYxxxx", 9) == 0);
77 assert_se(ascii_strcasecmp_n("xxxxyxxxx", "xxxxxxxxx", 9) > 0);
78 assert_se(ascii_strcasecmp_n("xxxxyxxxx", "xxxxXxxxx", 9) > 0);
79 assert_se(ascii_strcasecmp_n("xxxxYxxxx", "xxxxxxxxx", 9) > 0);
80 assert_se(ascii_strcasecmp_n("xxxxYxxxx", "xxxxXxxxx", 9) > 0);
81}
82
c1749834
LP
83static void test_ascii_strcasecmp_nn(void) {
84 assert_se(ascii_strcasecmp_nn("", 0, "", 0) == 0);
85 assert_se(ascii_strcasecmp_nn("", 0, "", 1) < 0);
86 assert_se(ascii_strcasecmp_nn("", 1, "", 0) > 0);
87 assert_se(ascii_strcasecmp_nn("", 1, "", 1) == 0);
88
89 assert_se(ascii_strcasecmp_nn("aaaa", 4, "aaAa", 4) == 0);
90 assert_se(ascii_strcasecmp_nn("aaa", 3, "aaAa", 4) < 0);
91 assert_se(ascii_strcasecmp_nn("aaa", 4, "aaAa", 4) < 0);
92 assert_se(ascii_strcasecmp_nn("aaaa", 4, "aaA", 3) > 0);
93 assert_se(ascii_strcasecmp_nn("aaaa", 4, "AAA", 4) > 0);
94
95 assert_se(ascii_strcasecmp_nn("aaaa", 4, "bbbb", 4) < 0);
96 assert_se(ascii_strcasecmp_nn("aaAA", 4, "BBbb", 4) < 0);
97 assert_se(ascii_strcasecmp_nn("BBbb", 4, "aaaa", 4) > 0);
98}
99
9fe4ea21
LP
100int main(int argc, char *argv[]) {
101 test_string_erase();
522d85ae 102 test_ascii_strcasecmp_n();
c1749834 103 test_ascii_strcasecmp_nn();
9fe4ea21
LP
104 return 0;
105}