]>
git.ipfire.org Git - pakfire.git/blob - tests/libpakfire/util.c
1 /*#############################################################################
3 # Pakfire - The IPFire package management system #
4 # Copyright (C) 2019 Pakfire development team #
6 # This program is free software: you can redistribute it and/or modify #
7 # it under the terms of the GNU General Public License as published by #
8 # the Free Software Foundation, either version 3 of the License, or #
9 # (at your option) any later version. #
11 # This program is distributed in the hope that it will be useful, #
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of #
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
14 # GNU General Public License for more details. #
16 # You should have received a copy of the GNU General Public License #
17 # along with this program. If not, see <http://www.gnu.org/licenses/>. #
19 #############################################################################*/
25 #include <pakfire/parser.h>
26 #include <pakfire/util.h>
28 #include "../testsuite.h"
30 static int test_basename(const struct test
* t
) {
31 const char* dir
= "/a/b/c";
33 char* output
= pakfire_basename(dir
);
34 ASSERT_STRING_EQUALS(output
, "c");
43 static int test_dirname(const struct test
* t
) {
44 const char* dir
= "/a/b/c";
46 char* output
= pakfire_dirname(dir
);
47 ASSERT_STRING_EQUALS(output
, "/a/b");
56 static int test_string_startswith(const struct test
* t
) {
57 ASSERT_TRUE(pakfire_string_startswith("ABC", "A"));
58 ASSERT_FALSE(pakfire_string_startswith("ABC", "B"));
60 // Check for invalid inputs
61 ASSERT_ERRNO(pakfire_string_startswith("ABC", NULL
), EINVAL
);
62 ASSERT_ERRNO(pakfire_string_startswith(NULL
, "ABC"), EINVAL
);
63 ASSERT_ERRNO(pakfire_string_startswith(NULL
, NULL
), EINVAL
);
71 static int test_string_endswith(const struct test
* t
) {
72 ASSERT_TRUE(pakfire_string_endswith("ABC", "C"));
73 ASSERT_FALSE(pakfire_string_endswith("ABC", "B"));
75 // Check for invalid inputs
76 ASSERT_ERRNO(pakfire_string_endswith("ABC", NULL
), EINVAL
);
77 ASSERT_ERRNO(pakfire_string_endswith(NULL
, "ABC"), EINVAL
);
78 ASSERT_ERRNO(pakfire_string_endswith(NULL
, NULL
), EINVAL
);
86 static int test_string_matches(const struct test
* t
) {
87 ASSERT_TRUE(pakfire_string_matches("ABC", "A"));
88 ASSERT_TRUE(pakfire_string_matches("ABC", "B"));
89 ASSERT_TRUE(pakfire_string_matches("ABC", "C"));
90 ASSERT_TRUE(pakfire_string_matches("ABC", "AB"));
91 ASSERT_TRUE(pakfire_string_matches("ABC", "BC"));
92 ASSERT_TRUE(pakfire_string_matches("ABC", "ABC"));
93 ASSERT_FALSE(pakfire_string_matches("ABC", "D"));
94 ASSERT_FALSE(pakfire_string_matches("ABC", "ABCD"));
96 // Check for invalid inputs
97 ASSERT_ERRNO(pakfire_string_matches("ABC", NULL
), EINVAL
);
98 ASSERT_ERRNO(pakfire_string_matches(NULL
, "ABC"), EINVAL
);
99 ASSERT_ERRNO(pakfire_string_matches(NULL
, NULL
), EINVAL
);
107 static int test_string_partition(const struct test
* t
) {
112 int r
= pakfire_string_partition("ABC:DEF", ":", &part1
, &part2
);
114 ASSERT_STRING_EQUALS(part1
, "ABC");
115 ASSERT_STRING_EQUALS(part2
, "DEF");
121 r
= pakfire_string_partition("ABCDEF", ":", &part1
, &part2
);
123 ASSERT(part1
== NULL
);
124 ASSERT(part2
== NULL
);
126 // Nothing after the delimiter
127 r
= pakfire_string_partition("ABC:", ":", &part1
, &part2
);
129 ASSERT_STRING_EQUALS(part1
, "ABC");
130 ASSERT_STRING_EQUALS(part2
, "");
135 // Nothing before the delimiter
136 r
= pakfire_string_partition(":ABC", ":", &part1
, &part2
);
138 ASSERT_STRING_EQUALS(part1
, "");
139 ASSERT_STRING_EQUALS(part2
, "ABC");
144 // Multi-character delimiter
145 r
= pakfire_string_partition("ABC:-:DEF", ":-:", &part1
, &part2
);
147 ASSERT_STRING_EQUALS(part1
, "ABC");
148 ASSERT_STRING_EQUALS(part2
, "DEF");
159 static int test_string_replace(const struct test
* t
) {
160 const char* result
= pakfire_string_replace(
161 "ABCABCABCABC", "AB", "CC"
163 ASSERT_STRING_EQUALS(result
, "CCCCCCCCCCCC");
171 static int test_string_split(const struct test
* t
) {
172 char** result
= pakfire_split_string(NULL
, 'X');
174 // Must return on invalid input
175 ASSERT_ERRNO(!result
, EINVAL
);
178 result
= pakfire_split_string("ABCXABCXABC", 'X');
181 ASSERT_STRING_EQUALS(result
[0], "ABC");
182 ASSERT_STRING_EQUALS(result
[1], "ABC");
183 ASSERT_STRING_EQUALS(result
[2], "ABC");
184 ASSERT_NULL(result
[3]);
186 // Split a string withtout the delimiter
187 result
= pakfire_split_string("ABCABC", 'X');
190 ASSERT_STRING_EQUALS(result
[0], "ABCABC");
191 ASSERT_NULL(result
[1]);
193 // String with only delimiters
194 result
= pakfire_split_string("XXXX", 'X');
197 ASSERT_STRING_EQUALS(result
[0], "");
198 ASSERT_STRING_EQUALS(result
[1], "");
199 ASSERT_STRING_EQUALS(result
[2], "");
200 ASSERT_STRING_EQUALS(result
[3], "");
201 ASSERT_NULL(result
[4]);
209 int main(int argc
, char** argv
) {
210 testsuite_add_test(test_basename
);
211 testsuite_add_test(test_dirname
);
212 testsuite_add_test(test_string_startswith
);
213 testsuite_add_test(test_string_endswith
);
214 testsuite_add_test(test_string_matches
);
215 testsuite_add_test(test_string_partition
);
216 testsuite_add_test(test_string_replace
);
217 testsuite_add_test(test_string_split
);
219 return testsuite_run();