]> git.ipfire.org Git - pakfire.git/blob - tests/libpakfire/util.c
c36bb06ba91e2ec3d8f9704a888b270ae167925a
[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_SUCCESS(pakfire_string_startswith("ABC", "A"));
58 ASSERT_FAILURE(pakfire_string_startswith("ABC", "B"));
59
60 return EXIT_SUCCESS;
61
62 FAIL:
63 return EXIT_FAILURE;
64 }
65
66 static int test_string_partition(const struct test* t) {
67 char* part1;
68 char* part2;
69
70 // Regular case
71 int r = pakfire_string_partition("ABC:DEF", ":", &part1, &part2);
72 ASSERT(r == 0);
73 ASSERT_STRING_EQUALS(part1, "ABC");
74 ASSERT_STRING_EQUALS(part2, "DEF");
75
76 free(part1);
77 free(part2);
78
79 // No delimiter
80 r = pakfire_string_partition("ABCDEF", ":", &part1, &part2);
81 ASSERT(r == 1);
82 ASSERT(part1 == NULL);
83 ASSERT(part2 == NULL);
84
85 // Nothing after the delimiter
86 r = pakfire_string_partition("ABC:", ":", &part1, &part2);
87 ASSERT(r == 0);
88 ASSERT_STRING_EQUALS(part1, "ABC");
89 ASSERT_STRING_EQUALS(part2, "");
90
91 free(part1);
92 free(part2);
93
94 // Nothing before the delimiter
95 r = pakfire_string_partition(":ABC", ":", &part1, &part2);
96 ASSERT(r == 0);
97 ASSERT_STRING_EQUALS(part1, "");
98 ASSERT_STRING_EQUALS(part2, "ABC");
99
100 free(part1);
101 free(part2);
102
103 // Multi-character delimiter
104 r = pakfire_string_partition("ABC:-:DEF", ":-:", &part1, &part2);
105 ASSERT(r == 0);
106 ASSERT_STRING_EQUALS(part1, "ABC");
107 ASSERT_STRING_EQUALS(part2, "DEF");
108
109 free(part1);
110 free(part2);
111
112 return EXIT_SUCCESS;
113
114 FAIL:
115 return EXIT_FAILURE;
116 }
117
118 static int test_string_replace(const struct test* t) {
119 const char* result = pakfire_string_replace(
120 "ABCABCABCABC", "AB", "CC"
121 );
122 ASSERT_STRING_EQUALS(result, "CCCCCCCCCCCC");
123
124 return EXIT_SUCCESS;
125
126 FAIL:
127 return EXIT_FAILURE;
128 }
129
130 static int test_string_split(const struct test* t) {
131 char** result = pakfire_split_string(NULL, 'X');
132
133 // Must return on invalid input
134 ASSERT_ERRNO(!result, EINVAL);
135
136 // Split a string
137 result = pakfire_split_string("ABCXABCXABC", 'X');
138 ASSERT(result);
139
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]);
144
145 // Split a string withtout the delimiter
146 result = pakfire_split_string("ABCABC", 'X');
147 ASSERT(result);
148
149 ASSERT_STRING_EQUALS(result[0], "ABCABC");
150 ASSERT_NULL(result[1]);
151
152 // String with only delimiters
153 result = pakfire_split_string("XXXX", 'X');
154 ASSERT(result);
155
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]);
161
162 return EXIT_SUCCESS;
163
164 FAIL:
165 return EXIT_FAILURE;
166 }
167
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);
175
176 return testsuite_run();
177 }