]> git.ipfire.org Git - pakfire.git/blob - tests/libpakfire/util.c
util: Add test for pakfire_string_matches
[pakfire.git] / tests / libpakfire / util.c
1 /*#############################################################################
2 # #
3 # Pakfire - The IPFire package management system #
4 # Copyright (C) 2019 Pakfire development team #
5 # #
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. #
10 # #
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. #
15 # #
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/>. #
18 # #
19 #############################################################################*/
20
21 #include <errno.h>
22 #include <stdlib.h>
23 #include <string.h>
24
25 #include <pakfire/parser.h>
26 #include <pakfire/util.h>
27
28 #include "../testsuite.h"
29
30 static int test_basename(const struct test* t) {
31 const char* dir = "/a/b/c";
32
33 char* output = pakfire_basename(dir);
34 ASSERT_STRING_EQUALS(output, "c");
35 free(output);
36
37 return EXIT_SUCCESS;
38
39 FAIL:
40 return EXIT_FAILURE;
41 }
42
43 static int test_dirname(const struct test* t) {
44 const char* dir = "/a/b/c";
45
46 char* output = pakfire_dirname(dir);
47 ASSERT_STRING_EQUALS(output, "/a/b");
48 free(output);
49
50 return EXIT_SUCCESS;
51
52 FAIL:
53 return EXIT_FAILURE;
54 }
55
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"));
59
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);
64
65 return EXIT_SUCCESS;
66
67 FAIL:
68 return EXIT_FAILURE;
69 }
70
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"));
74
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);
79
80 return EXIT_SUCCESS;
81
82 FAIL:
83 return EXIT_FAILURE;
84 }
85
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"));
95
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);
100
101 return EXIT_SUCCESS;
102
103 FAIL:
104 return EXIT_FAILURE;
105 }
106
107 static int test_string_partition(const struct test* t) {
108 char* part1;
109 char* part2;
110
111 // Regular case
112 int r = pakfire_string_partition("ABC:DEF", ":", &part1, &part2);
113 ASSERT(r == 0);
114 ASSERT_STRING_EQUALS(part1, "ABC");
115 ASSERT_STRING_EQUALS(part2, "DEF");
116
117 free(part1);
118 free(part2);
119
120 // No delimiter
121 r = pakfire_string_partition("ABCDEF", ":", &part1, &part2);
122 ASSERT(r == 1);
123 ASSERT(part1 == NULL);
124 ASSERT(part2 == NULL);
125
126 // Nothing after the delimiter
127 r = pakfire_string_partition("ABC:", ":", &part1, &part2);
128 ASSERT(r == 0);
129 ASSERT_STRING_EQUALS(part1, "ABC");
130 ASSERT_STRING_EQUALS(part2, "");
131
132 free(part1);
133 free(part2);
134
135 // Nothing before the delimiter
136 r = pakfire_string_partition(":ABC", ":", &part1, &part2);
137 ASSERT(r == 0);
138 ASSERT_STRING_EQUALS(part1, "");
139 ASSERT_STRING_EQUALS(part2, "ABC");
140
141 free(part1);
142 free(part2);
143
144 // Multi-character delimiter
145 r = pakfire_string_partition("ABC:-:DEF", ":-:", &part1, &part2);
146 ASSERT(r == 0);
147 ASSERT_STRING_EQUALS(part1, "ABC");
148 ASSERT_STRING_EQUALS(part2, "DEF");
149
150 free(part1);
151 free(part2);
152
153 return EXIT_SUCCESS;
154
155 FAIL:
156 return EXIT_FAILURE;
157 }
158
159 static int test_string_replace(const struct test* t) {
160 const char* result = pakfire_string_replace(
161 "ABCABCABCABC", "AB", "CC"
162 );
163 ASSERT_STRING_EQUALS(result, "CCCCCCCCCCCC");
164
165 return EXIT_SUCCESS;
166
167 FAIL:
168 return EXIT_FAILURE;
169 }
170
171 static int test_string_split(const struct test* t) {
172 char** result = pakfire_split_string(NULL, 'X');
173
174 // Must return on invalid input
175 ASSERT_ERRNO(!result, EINVAL);
176
177 // Split a string
178 result = pakfire_split_string("ABCXABCXABC", 'X');
179 ASSERT(result);
180
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]);
185
186 // Split a string withtout the delimiter
187 result = pakfire_split_string("ABCABC", 'X');
188 ASSERT(result);
189
190 ASSERT_STRING_EQUALS(result[0], "ABCABC");
191 ASSERT_NULL(result[1]);
192
193 // String with only delimiters
194 result = pakfire_split_string("XXXX", 'X');
195 ASSERT(result);
196
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]);
202
203 return EXIT_SUCCESS;
204
205 FAIL:
206 return EXIT_FAILURE;
207 }
208
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);
218
219 return testsuite_run();
220 }