6 #define UTIL_TEST_ADD(path, func) g_test_add_func("/utils/" path, func);
9 static void test_utils_strv_new(void)
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");
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");
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");
37 static void test_utils_strv_find_common_prefix(void)
41 s
= utils_strv_find_common_prefix(NULL
, 0);
44 data
= utils_strv_new("", NULL
);
45 s
= utils_strv_find_common_prefix(data
, -1);
47 g_assert_cmpstr(s
, ==, "");
51 data
= utils_strv_new("1", "2", "3", NULL
);
52 s
= utils_strv_find_common_prefix(data
, -1);
54 g_assert_cmpstr(s
, ==, "");
58 data
= utils_strv_new("abc", "amn", "axy", NULL
);
59 s
= utils_strv_find_common_prefix(data
, -1);
61 g_assert_cmpstr(s
, ==, "a");
65 data
= utils_strv_new("abc", "", "axy", NULL
);
66 s
= utils_strv_find_common_prefix(data
, -1);
68 g_assert_cmpstr(s
, ==, "");
72 data
= utils_strv_new("22", "23", "33", NULL
);
73 s
= utils_strv_find_common_prefix(data
, 1);
75 g_assert_cmpstr(s
, ==, "22");
77 s
= utils_strv_find_common_prefix(data
, 2);
79 g_assert_cmpstr(s
, ==, "2");
81 s
= utils_strv_find_common_prefix(data
, 3);
83 g_assert_cmpstr(s
, ==, "");
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",
94 s
= utils_strv_find_common_prefix(data
, 4);
96 g_assert_cmpstr(s
, ==, "/home/user/src/geany/src/s");
98 s
= utils_strv_find_common_prefix(data
, 5);
100 g_assert_cmpstr(s
, ==, "/home/user/src/geany/src/");
102 s
= utils_strv_find_common_prefix(data
, -1);
104 g_assert_cmpstr(s
, ==, "/home/user/src/geany");
109 #define DIR_SEP "\\/"
110 void test_utils_strv_find_lcs(void)
114 s
= utils_strv_find_lcs(NULL
, 0, "");
117 data
= utils_strv_new("", NULL
);
118 s
= utils_strv_find_lcs(data
, -1, "");
120 g_assert_cmpstr(s
, ==, "");
124 data
= utils_strv_new("1", "2", "3", NULL
);
125 s
= utils_strv_find_lcs(data
, -1, "");
127 g_assert_cmpstr(s
, ==, "");
129 s
= utils_strv_find_lcs(data
, -1, DIR_SEP
);
131 g_assert_cmpstr(s
, ==, "");
135 data
= utils_strv_new("abc", "amn", "axy", NULL
);
136 s
= utils_strv_find_lcs(data
, -1, "");
138 g_assert_cmpstr(s
, ==, "a");
140 s
= utils_strv_find_lcs(data
, -1, DIR_SEP
);
142 g_assert_cmpstr(s
, ==, "");
146 data
= utils_strv_new("bca", "mna", "xya", NULL
);
147 s
= utils_strv_find_lcs(data
, -1, "");
149 g_assert_cmpstr(s
, ==, "a");
151 s
= utils_strv_find_lcs(data
, -1, DIR_SEP
);
153 g_assert_cmpstr(s
, ==, "");
157 data
= utils_strv_new("abc", "", "axy", NULL
);
158 s
= utils_strv_find_lcs(data
, -1, "");
160 g_assert_cmpstr(s
, ==, "");
162 s
= utils_strv_find_lcs(data
, -1, DIR_SEP
);
164 g_assert_cmpstr(s
, ==, "");
168 data
= utils_strv_new("a123b", "b123c", "c123d", NULL
);
169 s
= utils_strv_find_lcs(data
, -1, "");
171 g_assert_cmpstr(s
, ==, "123");
173 s
= utils_strv_find_lcs(data
, -1, DIR_SEP
);
175 g_assert_cmpstr(s
, ==, "");
179 data
= utils_strv_new("22", "23", "33", NULL
);
180 s
= utils_strv_find_lcs(data
, 1, "");
182 g_assert_cmpstr(s
, ==, "22");
184 s
= utils_strv_find_lcs(data
, 2, "");
186 g_assert_cmpstr(s
, ==, "2");
188 s
= utils_strv_find_lcs(data
, 3, "");
190 g_assert_cmpstr(s
, ==, "");
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",
201 s
= utils_strv_find_lcs(data
, 4, "");
203 g_assert_cmpstr(s
, ==, "/home/user/src/geany/src/s");
205 s
= utils_strv_find_lcs(data
, 4, DIR_SEP
);
207 g_assert_cmpstr(s
, ==, "/home/user/src/geany/src/");
209 s
= utils_strv_find_lcs(data
, 5, "");
211 g_assert_cmpstr(s
, ==, "/home/user/src/geany/src/");
213 s
= utils_strv_find_lcs(data
, 5, DIR_SEP
);
215 g_assert_cmpstr(s
, ==, "/home/user/src/geany/src/");
217 s
= utils_strv_find_lcs(data
, -1, "");
219 g_assert_cmpstr(s
, ==, "/home/user/src/geany");
221 s
= utils_strv_find_lcs(data
, -1, DIR_SEP
);
223 g_assert_cmpstr(s
, ==, "/home/user/src/");
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, "");
231 g_assert_cmpstr(s
, ==, "/module/source.c");
233 s
= utils_strv_find_lcs(data
, -1, DIR_SEP
);
235 g_assert_cmpstr(s
, ==, "/module/");
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, "");
243 g_assert_cmpstr(s
, ==, "/module\\source.c");
245 s
= utils_strv_find_lcs(data
, -1, DIR_SEP
);
247 g_assert_cmpstr(s
, ==, "/module\\");
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, "");
255 g_assert_cmpstr(s
, ==, ".2.3/src/");
257 s
= utils_strv_find_lcs(data
, -1, DIR_SEP
);
259 g_assert_cmpstr(s
, ==, "/module/");
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, "");
266 g_assert_cmpstr(s
, ==, "/geany");
268 s
= utils_strv_find_lcs(data
, -1, DIR_SEP
);
270 g_assert_cmpstr(s
, ==, "");
276 /* g_strv_equal is too recent */
277 static gboolean
strv_eq(gchar
**strv1
, gchar
**strv2
)
280 gchar
*s1
= *strv1
++;
281 gchar
*s2
= *strv2
++;
289 else if (!g_str_equal(s1
, s2
))
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);
301 g_assert_true(strv_eq(result
, expected
));
304 data
= utils_strv_new("", NULL
);
305 result
= utils_strv_shorten_file_list(data
, -1);
307 g_assert_true(strv_eq(result
, expected
));
311 data
= utils_strv_new("1", "2", "3", NULL
);
312 result
= utils_strv_shorten_file_list(data
, -1);
314 g_assert_true(strv_eq(result
, expected
));
318 data
= utils_strv_new("abc", "amn", "axy", NULL
);
319 result
= utils_strv_shorten_file_list(data
, -1);
321 g_assert_true(strv_eq(result
, expected
));
325 data
= utils_strv_new("abc", "", "axy", NULL
);
326 result
= utils_strv_shorten_file_list(data
, -1);
328 g_assert_true(strv_eq(result
, expected
));
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
);
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
);
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
);
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",
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",
370 g_assert_true(strv_eq(result
, expected
));
371 g_strfreev(expected
);
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",
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",
387 g_assert_true(strv_eq(result
, expected
));
388 g_strfreev(expected
);
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
);
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
);
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
);