Fix variable shadowing
[geany-mirror.git] / tests / test_utils.c
blobea9a307f15a1c7c232221651e3275bf1c19c260d
2 #include "utils.h"
4 #include "gtkcompat.h"
6 #define UTIL_TEST_ADD(path, func) g_test_add_func("/utils/" path, func);
9 static void test_utils_strv_new(void)
11 gchar **data;
13 data = utils_strv_new("1", NULL);
14 g_assert_nonnull(data);
15 g_assert_cmpint(g_strv_length(data), ==, 1);
16 g_assert_cmpstr(data[0], ==, "1");
17 g_strfreev(data);
19 data = utils_strv_new("1", "2", "3", NULL);
20 g_assert_nonnull(data);
21 g_assert_cmpint(g_strv_length(data), ==, 3);
22 g_assert_cmpstr(data[0], ==, "1");
23 g_assert_cmpstr(data[1], ==, "2");
24 g_assert_cmpstr(data[2], ==, "3");
25 g_strfreev(data);
27 data = utils_strv_new("1", "", "", "4", NULL);
28 g_assert_nonnull(data);
29 g_assert_cmpint(g_strv_length(data), ==, 4);
30 g_assert_cmpstr(data[0], ==, "1");
31 g_assert_cmpstr(data[1], ==, "");
32 g_assert_cmpstr(data[2], ==, "");
33 g_assert_cmpstr(data[3], ==, "4");
34 g_strfreev(data);
37 static void test_utils_strv_find_common_prefix(void)
39 gchar **data, *s;
41 s = utils_strv_find_common_prefix(NULL, 0);
42 g_assert_null(s);
44 data = utils_strv_new("", NULL);
45 s = utils_strv_find_common_prefix(data, -1);
46 g_assert_nonnull(s);
47 g_assert_cmpstr(s, ==, "");
48 g_free(s);
49 g_strfreev(data);
51 data = utils_strv_new("1", "2", "3", NULL);
52 s = utils_strv_find_common_prefix(data, -1);
53 g_assert_nonnull(s);
54 g_assert_cmpstr(s, ==, "");
55 g_free(s);
56 g_strfreev(data);
58 data = utils_strv_new("abc", "amn", "axy", NULL);
59 s = utils_strv_find_common_prefix(data, -1);
60 g_assert_nonnull(s);
61 g_assert_cmpstr(s, ==, "a");
62 g_free(s);
63 g_strfreev(data);
65 data = utils_strv_new("abc", "", "axy", NULL);
66 s = utils_strv_find_common_prefix(data, -1);
67 g_assert_nonnull(s);
68 g_assert_cmpstr(s, ==, "");
69 g_free(s);
70 g_strfreev(data);
72 data = utils_strv_new("22", "23", "33", NULL);
73 s = utils_strv_find_common_prefix(data, 1);
74 g_assert_nonnull(s);
75 g_assert_cmpstr(s, ==, "22");
76 g_free(s);
77 s = utils_strv_find_common_prefix(data, 2);
78 g_assert_nonnull(s);
79 g_assert_cmpstr(s, ==, "2");
80 g_free(s);
81 s = utils_strv_find_common_prefix(data, 3);
82 g_assert_nonnull(s);
83 g_assert_cmpstr(s, ==, "");
84 g_free(s);
85 g_strfreev(data);
87 data = utils_strv_new("/home/user/src/geany/src/stash.c",
88 "/home/user/src/geany/src/sidebar.c",
89 "/home/user/src/geany/src/sidebar.h",
90 "/home/user/src/geany/src/sidebar.h",
91 "/home/user/src/geany/src/main.c",
92 "/home/user/src/geany-plugins/addons/src/addons.c",
93 NULL);
94 s = utils_strv_find_common_prefix(data, 4);
95 g_assert_nonnull(s);
96 g_assert_cmpstr(s, ==, "/home/user/src/geany/src/s");
97 g_free(s);
98 s = utils_strv_find_common_prefix(data, 5);
99 g_assert_nonnull(s);
100 g_assert_cmpstr(s, ==, "/home/user/src/geany/src/");
101 g_free(s);
102 s = utils_strv_find_common_prefix(data, -1);
103 g_assert_nonnull(s);
104 g_assert_cmpstr(s, ==, "/home/user/src/geany");
105 g_free(s);
106 g_strfreev(data);
109 #define DIR_SEP "\\/"
110 void test_utils_strv_find_lcs(void)
112 gchar **data, *s;
114 s = utils_strv_find_lcs(NULL, 0, "");
115 g_assert_null(s);
117 data = utils_strv_new("", NULL);
118 s = utils_strv_find_lcs(data, -1, "");
119 g_assert_nonnull(s);
120 g_assert_cmpstr(s, ==, "");
121 g_free(s);
122 g_strfreev(data);
124 data = utils_strv_new("1", "2", "3", NULL);
125 s = utils_strv_find_lcs(data, -1, "");
126 g_assert_nonnull(s);
127 g_assert_cmpstr(s, ==, "");
128 g_free(s);
129 s = utils_strv_find_lcs(data, -1, DIR_SEP);
130 g_assert_nonnull(s);
131 g_assert_cmpstr(s, ==, "");
132 g_free(s);
133 g_strfreev(data);
135 data = utils_strv_new("abc", "amn", "axy", NULL);
136 s = utils_strv_find_lcs(data, -1, "");
137 g_assert_nonnull(s);
138 g_assert_cmpstr(s, ==, "a");
139 g_free(s);
140 s = utils_strv_find_lcs(data, -1, DIR_SEP);
141 g_assert_nonnull(s);
142 g_assert_cmpstr(s, ==, "");
143 g_free(s);
144 g_strfreev(data);
146 data = utils_strv_new("bca", "mna", "xya", NULL);
147 s = utils_strv_find_lcs(data, -1, "");
148 g_assert_nonnull(s);
149 g_assert_cmpstr(s, ==, "a");
150 g_free(s);
151 s = utils_strv_find_lcs(data, -1, DIR_SEP);
152 g_assert_nonnull(s);
153 g_assert_cmpstr(s, ==, "");
154 g_free(s);
155 g_strfreev(data);
157 data = utils_strv_new("abc", "", "axy", NULL);
158 s = utils_strv_find_lcs(data, -1, "");
159 g_assert_nonnull(s);
160 g_assert_cmpstr(s, ==, "");
161 g_free(s);
162 s = utils_strv_find_lcs(data, -1, DIR_SEP);
163 g_assert_nonnull(s);
164 g_assert_cmpstr(s, ==, "");
165 g_free(s);
166 g_strfreev(data);
168 data = utils_strv_new("a123b", "b123c", "c123d", NULL);
169 s = utils_strv_find_lcs(data, -1, "");
170 g_assert_nonnull(s);
171 g_assert_cmpstr(s, ==, "123");
172 g_free(s);
173 s = utils_strv_find_lcs(data, -1, DIR_SEP);
174 g_assert_nonnull(s);
175 g_assert_cmpstr(s, ==, "");
176 g_free(s);
177 g_strfreev(data);
179 data = utils_strv_new("22", "23", "33", NULL);
180 s = utils_strv_find_lcs(data, 1, "");
181 g_assert_nonnull(s);
182 g_assert_cmpstr(s, ==, "22");
183 g_free(s);
184 s = utils_strv_find_lcs(data, 2, "");
185 g_assert_nonnull(s);
186 g_assert_cmpstr(s, ==, "2");
187 g_free(s);
188 s = utils_strv_find_lcs(data, 3, "");
189 g_assert_nonnull(s);
190 g_assert_cmpstr(s, ==, "");
191 g_free(s);
192 g_strfreev(data);
194 data = utils_strv_new("/home/user/src/geany/src/stash.c",
195 "/home/user/src/geany/src/sidebar.c",
196 "/home/user/src/geany/src/sidebar.h",
197 "/home/user/src/geany/src/sidebar.h",
198 "/home/user/src/geany/src/main.c",
199 "/home/user/src/geany-plugins/addons/src/addons.c",
200 NULL);
201 s = utils_strv_find_lcs(data, 4, "");
202 g_assert_nonnull(s);
203 g_assert_cmpstr(s, ==, "/home/user/src/geany/src/s");
204 g_free(s);
205 s = utils_strv_find_lcs(data, 4, DIR_SEP);
206 g_assert_nonnull(s);
207 g_assert_cmpstr(s, ==, "/home/user/src/geany/src/");
208 g_free(s);
209 s = utils_strv_find_lcs(data, 5, "");
210 g_assert_nonnull(s);
211 g_assert_cmpstr(s, ==, "/home/user/src/geany/src/");
212 g_free(s);
213 s = utils_strv_find_lcs(data, 5, DIR_SEP);
214 g_assert_nonnull(s);
215 g_assert_cmpstr(s, ==, "/home/user/src/geany/src/");
216 g_free(s);
217 s = utils_strv_find_lcs(data, -1, "");
218 g_assert_nonnull(s);
219 g_assert_cmpstr(s, ==, "/home/user/src/geany");
220 g_free(s);
221 s = utils_strv_find_lcs(data, -1, DIR_SEP);
222 g_assert_nonnull(s);
223 g_assert_cmpstr(s, ==, "/home/user/src/");
224 g_free(s);
225 g_strfreev(data);
227 data = utils_strv_new("/src/a/app-1.2.3/src/lib/module/source.c",
228 "/src/b/app-2.2.3/src/module/source.c", NULL);
229 s = utils_strv_find_lcs(data, -1, "");
230 g_assert_nonnull(s);
231 g_assert_cmpstr(s, ==, "/module/source.c");
232 g_free(s);
233 s = utils_strv_find_lcs(data, -1, DIR_SEP);
234 g_assert_nonnull(s);
235 g_assert_cmpstr(s, ==, "/module/");
236 g_free(s);
237 g_strfreev(data);
239 data = utils_strv_new("/src/a/app-1.2.3/src/lib/module\\source.c",
240 "/src/b/app-2.2.3/src/module\\source.c", NULL);
241 s = utils_strv_find_lcs(data, -1, "");
242 g_assert_nonnull(s);
243 g_assert_cmpstr(s, ==, "/module\\source.c");
244 g_free(s);
245 s = utils_strv_find_lcs(data, -1, DIR_SEP);
246 g_assert_nonnull(s);
247 g_assert_cmpstr(s, ==, "/module\\");
248 g_free(s);
249 g_strfreev(data);
251 data = utils_strv_new("/src/a/app-1.2.3/src/lib/module/",
252 "/src/b/app-2.2.3/src/module/", NULL);
253 s = utils_strv_find_lcs(data, -1, "");
254 g_assert_nonnull(s);
255 g_assert_cmpstr(s, ==, ".2.3/src/");
256 g_free(s);
257 s = utils_strv_find_lcs(data, -1, DIR_SEP);
258 g_assert_nonnull(s);
259 g_assert_cmpstr(s, ==, "/module/");
260 g_free(s);
261 g_strfreev(data);
263 data = utils_strv_new("/usr/local/bin/geany", "/usr/bin/geany", "/home/user/src/geany/src/geany", NULL);
264 s = utils_strv_find_lcs(data, -1, "");
265 g_assert_nonnull(s);
266 g_assert_cmpstr(s, ==, "/geany");
267 g_free(s);
268 s = utils_strv_find_lcs(data, -1, DIR_SEP);
269 g_assert_nonnull(s);
270 g_assert_cmpstr(s, ==, "");
271 g_free(s);
272 g_strfreev(data);
276 /* g_strv_equal is too recent */
277 static gboolean strv_eq(gchar **strv1, gchar **strv2)
279 while(1) {
280 gchar *s1 = *strv1++;
281 gchar *s2 = *strv2++;
283 if (!s1 && !s2)
284 return TRUE;
285 else if (s1 && !s2)
286 return FALSE;
287 else if (s2 && !s1)
288 return FALSE;
289 else if (!g_str_equal(s1, s2))
290 return FALSE;
294 void test_utils_strv_shorten_file_list(void)
296 gchar **data, **expected, **result;
297 gchar *empty[] = { NULL };
299 result = utils_strv_shorten_file_list(NULL, 0);
300 expected = empty;
301 g_assert_true(strv_eq(result, expected));
302 g_strfreev(result);
304 data = utils_strv_new("", NULL);
305 result = utils_strv_shorten_file_list(data, -1);
306 expected = data;
307 g_assert_true(strv_eq(result, expected));
308 g_strfreev(result);
309 g_strfreev(data);
311 data = utils_strv_new("1", "2", "3", NULL);
312 result = utils_strv_shorten_file_list(data, -1);
313 expected = data;
314 g_assert_true(strv_eq(result, expected));
315 g_strfreev(result);
316 g_strfreev(data);
318 data = utils_strv_new("abc", "amn", "axy", NULL);
319 result = utils_strv_shorten_file_list(data, -1);
320 expected = data;
321 g_assert_true(strv_eq(result, expected));
322 g_strfreev(result);
323 g_strfreev(data);
325 data = utils_strv_new("abc", "", "axy", NULL);
326 result = utils_strv_shorten_file_list(data, -1);
327 expected = data;
328 g_assert_true(strv_eq(result, expected));
329 g_strfreev(result);
330 g_strfreev(data);
332 data = utils_strv_new("22", "23", "33", NULL);
333 result = utils_strv_shorten_file_list(data, 1);
334 expected = utils_strv_new("22", NULL);
335 g_assert_true(strv_eq(result, expected));
336 g_strfreev(expected);
337 g_strfreev(result);
338 result = utils_strv_shorten_file_list(data, 2);
339 expected = utils_strv_new("22", "23", NULL);
340 g_assert_true(strv_eq(result, expected));
341 g_strfreev(expected);
342 g_strfreev(result);
343 result = utils_strv_shorten_file_list(data, 3);
344 expected = utils_strv_new("22", "23", "33", NULL);
345 g_assert_true(strv_eq(result, expected));
346 g_strfreev(expected);
347 g_strfreev(result);
348 g_strfreev(data);
350 data = utils_strv_new("/home/user/src/geany/src/stash.c",
351 "/home/user/src/geany/src/sidebar.c",
352 "/home/user/src/geany/src/sidebar.h",
353 "/home/user/src/geany/src/sidebar.h",
354 "/home/user/src/geany/src/main.c",
355 "/home/user/src/geany-plugins/addons/src/addons.c",
356 NULL);
357 result = utils_strv_shorten_file_list(data, 4);
358 expected = utils_strv_new("stash.c", "sidebar.c", "sidebar.h", "sidebar.h", NULL);
359 g_assert_true(strv_eq(result, expected));
360 g_strfreev(expected);
361 result = utils_strv_shorten_file_list(data, 5);
362 expected = utils_strv_new("stash.c", "sidebar.c", "sidebar.h", "sidebar.h", "main.c", NULL);
363 g_assert_true(strv_eq(result, expected));
364 g_strfreev(expected);
365 result = utils_strv_shorten_file_list(data, -1);
366 expected = utils_strv_new("geany/src/stash.c", "geany/src/sidebar.c",
367 "geany/src/sidebar.h", "geany/src/sidebar.h", "geany/src/main.c",
368 "geany-plugins/addons/src/addons.c",
369 NULL);
370 g_assert_true(strv_eq(result, expected));
371 g_strfreev(expected);
372 g_strfreev(data);
374 data = utils_strv_new("/home/user1/src/geany/src/stash.c",
375 "/home/user2/src/geany/src/sidebar.c",
376 "/home/user3/src/geany/src/sidebar.h",
377 "/home/user4/src/geany/src/sidebar.h",
378 "/home/user5/src/geany/src/main.c",
379 NULL);
380 result = utils_strv_shorten_file_list(data, -1);
381 expected = utils_strv_new("user1/.../stash.c",
382 "user2/.../sidebar.c",
383 "user3/.../sidebar.h",
384 "user4/.../sidebar.h",
385 "user5/.../main.c",
386 NULL);
387 g_assert_true(strv_eq(result, expected));
388 g_strfreev(expected);
389 g_strfreev(result);
390 g_strfreev(data);
392 data = utils_strv_new("/aaa/bbb/cc/ccccc/ddddd", "/aaa/bbb/xxx/yyy/cc/ccccc/ddddd", NULL);
393 result = utils_strv_shorten_file_list(data, -1);
394 expected = utils_strv_new("cc/.../ddddd", "xxx/yyy/cc/.../ddddd", NULL);
395 g_assert_true(strv_eq(result, expected));
396 g_strfreev(expected);
397 g_strfreev(result);
398 g_strfreev(data);
400 data = utils_strv_new("/src/a/app-1.2.3/src/lib/module/source.c",
401 "/src/b/app-2.2.3/src/module/source.c", NULL);
402 result = utils_strv_shorten_file_list(data, -1);
403 expected = utils_strv_new("a/app-1.2.3/src/lib/.../source.c",
404 "b/app-2.2.3/src/.../source.c", NULL);
405 g_assert_true(strv_eq(result, expected));
406 g_strfreev(expected);
407 g_strfreev(result);
408 g_strfreev(data);
411 int main(int argc, char **argv)
413 g_test_init(&argc, &argv, NULL);
415 UTIL_TEST_ADD("strv_join", test_utils_strv_new);
416 UTIL_TEST_ADD("strv_find_common_prefix", test_utils_strv_find_common_prefix);
417 UTIL_TEST_ADD("strv_find_lcs", test_utils_strv_find_lcs);
418 UTIL_TEST_ADD("strv_shorten_file_list", test_utils_strv_shorten_file_list);
420 return g_test_run();