]>
Commit | Line | Data |
---|---|---|
e1392437 MT |
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 | ||
c81287d2 | 21 | #include <errno.h> |
f0d6233d | 22 | #include <stdlib.h> |
e1392437 MT |
23 | #include <string.h> |
24 | ||
25 | #include <pakfire/parser.h> | |
26 | #include <pakfire/util.h> | |
27 | ||
28 | #include "../testsuite.h" | |
29 | ||
35bb0e8f | 30 | static int test_basename(const struct test* t) { |
e1392437 MT |
31 | const char* dir = "/a/b/c"; |
32 | ||
33 | char* output = pakfire_basename(dir); | |
89ed926c | 34 | ASSERT_STRING_EQUALS(output, "c"); |
f0d6233d | 35 | free(output); |
e1392437 MT |
36 | |
37 | return EXIT_SUCCESS; | |
e2e52986 MT |
38 | |
39 | FAIL: | |
40 | return EXIT_FAILURE; | |
e1392437 MT |
41 | } |
42 | ||
35bb0e8f | 43 | static int test_dirname(const struct test* t) { |
e1392437 MT |
44 | const char* dir = "/a/b/c"; |
45 | ||
46 | char* output = pakfire_dirname(dir); | |
89ed926c | 47 | ASSERT_STRING_EQUALS(output, "/a/b"); |
f0d6233d | 48 | free(output); |
e1392437 MT |
49 | |
50 | return EXIT_SUCCESS; | |
e2e52986 MT |
51 | |
52 | FAIL: | |
53 | return EXIT_FAILURE; | |
e1392437 MT |
54 | } |
55 | ||
35bb0e8f | 56 | static int test_string_startswith(const struct test* t) { |
fa5c92a2 MT |
57 | ASSERT_TRUE(pakfire_string_startswith("ABC", "A")); |
58 | ASSERT_FALSE(pakfire_string_startswith("ABC", "B")); | |
8ccb6637 | 59 | |
d967ba61 MT |
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 | ||
8ccb6637 | 65 | return EXIT_SUCCESS; |
e2e52986 MT |
66 | |
67 | FAIL: | |
68 | return EXIT_FAILURE; | |
8ccb6637 MT |
69 | } |
70 | ||
9266e977 MT |
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 | ||
7279504b MT |
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 | ||
5837952f MT |
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; | |
e2e52986 MT |
154 | |
155 | FAIL: | |
156 | return EXIT_FAILURE; | |
5837952f MT |
157 | } |
158 | ||
b8377a9f MT |
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; | |
e2e52986 MT |
166 | |
167 | FAIL: | |
168 | return EXIT_FAILURE; | |
b8377a9f MT |
169 | } |
170 | ||
c81287d2 MT |
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; | |
e2e52986 MT |
204 | |
205 | FAIL: | |
206 | return EXIT_FAILURE; | |
c81287d2 MT |
207 | } |
208 | ||
e1392437 | 209 | int main(int argc, char** argv) { |
1413be07 MT |
210 | testsuite_add_test(test_basename); |
211 | testsuite_add_test(test_dirname); | |
212 | testsuite_add_test(test_string_startswith); | |
9266e977 | 213 | testsuite_add_test(test_string_endswith); |
7279504b | 214 | testsuite_add_test(test_string_matches); |
5837952f | 215 | testsuite_add_test(test_string_partition); |
b8377a9f | 216 | testsuite_add_test(test_string_replace); |
c81287d2 | 217 | testsuite_add_test(test_string_split); |
e1392437 | 218 | |
1413be07 | 219 | return testsuite_run(); |
e1392437 | 220 | } |