]>
git.ipfire.org Git - pakfire.git/blob - tests/libpakfire/util.c
c36bb06ba91e2ec3d8f9704a888b270ae167925a
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_SUCCESS(pakfire_string_startswith("ABC", "A"));
58 ASSERT_FAILURE(pakfire_string_startswith("ABC", "B"));
66 static int test_string_partition(const struct test
* t
) {
71 int r
= pakfire_string_partition("ABC:DEF", ":", &part1
, &part2
);
73 ASSERT_STRING_EQUALS(part1
, "ABC");
74 ASSERT_STRING_EQUALS(part2
, "DEF");
80 r
= pakfire_string_partition("ABCDEF", ":", &part1
, &part2
);
82 ASSERT(part1
== NULL
);
83 ASSERT(part2
== NULL
);
85 // Nothing after the delimiter
86 r
= pakfire_string_partition("ABC:", ":", &part1
, &part2
);
88 ASSERT_STRING_EQUALS(part1
, "ABC");
89 ASSERT_STRING_EQUALS(part2
, "");
94 // Nothing before the delimiter
95 r
= pakfire_string_partition(":ABC", ":", &part1
, &part2
);
97 ASSERT_STRING_EQUALS(part1
, "");
98 ASSERT_STRING_EQUALS(part2
, "ABC");
103 // Multi-character delimiter
104 r
= pakfire_string_partition("ABC:-:DEF", ":-:", &part1
, &part2
);
106 ASSERT_STRING_EQUALS(part1
, "ABC");
107 ASSERT_STRING_EQUALS(part2
, "DEF");
118 static int test_string_replace(const struct test
* t
) {
119 const char* result
= pakfire_string_replace(
120 "ABCABCABCABC", "AB", "CC"
122 ASSERT_STRING_EQUALS(result
, "CCCCCCCCCCCC");
130 static int test_string_split(const struct test
* t
) {
131 char** result
= pakfire_split_string(NULL
, 'X');
133 // Must return on invalid input
134 ASSERT_ERRNO(!result
, EINVAL
);
137 result
= pakfire_split_string("ABCXABCXABC", 'X');
140 ASSERT_STRING_EQUALS(result
[0], "ABC");
141 ASSERT_STRING_EQUALS(result
[1], "ABC");
142 ASSERT_STRING_EQUALS(result
[2], "ABC");
143 ASSERT_NULL(result
[3]);
145 // Split a string withtout the delimiter
146 result
= pakfire_split_string("ABCABC", 'X');
149 ASSERT_STRING_EQUALS(result
[0], "ABCABC");
150 ASSERT_NULL(result
[1]);
152 // String with only delimiters
153 result
= pakfire_split_string("XXXX", 'X');
156 ASSERT_STRING_EQUALS(result
[0], "");
157 ASSERT_STRING_EQUALS(result
[1], "");
158 ASSERT_STRING_EQUALS(result
[2], "");
159 ASSERT_STRING_EQUALS(result
[3], "");
160 ASSERT_NULL(result
[4]);
168 int main(int argc
, char** argv
) {
169 testsuite_add_test(test_basename
);
170 testsuite_add_test(test_dirname
);
171 testsuite_add_test(test_string_startswith
);
172 testsuite_add_test(test_string_partition
);
173 testsuite_add_test(test_string_replace
);
174 testsuite_add_test(test_string_split
);
176 return testsuite_run();