1 /* Unit tests for utilities
2 * Copyright (C) 2010 Red Hat, Inc.
4 * This work is provided "as is"; redistribution and modification
5 * in whole or in part, in any medium, physical or electronic is
6 * permitted without restriction.
8 * This work is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12 * In no event shall the authors or contributors be liable for any
13 * direct, indirect, incidental, special, exemplary, or consequential
14 * damages (including, but not limited to, procurement of substitute
15 * goods or services; loss of use, data, or profits; or business
16 * interruption) however caused and on any theory of liability, whether
17 * in contract, strict liability, or tort (including negligence or
18 * otherwise) arising in any way out of the use of this software, even
19 * if advised of the possibility of such damage.
21 * Author: Matthias Clasen
24 #define GLIB_DISABLE_DEPRECATION_WARNINGS
33 strv_check (const gchar * const *strv, ...)
39 va_start (args, strv);
40 for (i = 0; strv[i]; i++)
42 s = va_arg (args, gchar*);
43 if (g_strcmp0 (strv[i], s) != 0)
56 test_language_names (void)
58 const gchar * const *names;
60 g_setenv ("LANGUAGE", "de:en_US", TRUE);
61 names = g_get_language_names ();
62 g_assert (strv_check (names, "de", "en_US", "en", "C", NULL));
64 g_setenv ("LANGUAGE", "tt_RU.UTF-8@iqtelif", TRUE);
65 names = g_get_language_names ();
66 g_assert (strv_check (names,
67 "tt_RU.UTF-8@iqtelif",
80 test_locale_variants (void)
84 v = g_get_locale_variants ("fr_BE");
85 g_assert (strv_check ((const gchar * const *) v, "fr_BE", "fr", NULL));
88 v = g_get_locale_variants ("sr_SR@latin");
89 g_assert (strv_check ((const gchar * const *) v, "sr_SR@latin", "sr@latin", "sr_SR", "sr", NULL));
96 if (g_test_verbose ())
97 g_print ("(header %d.%d.%d library %d.%d.%d) ",
98 GLIB_MAJOR_VERSION, GLIB_MINOR_VERSION, GLIB_MICRO_VERSION,
99 glib_major_version, glib_minor_version, glib_micro_version);
101 g_assert (glib_check_version (GLIB_MAJOR_VERSION,
103 GLIB_MICRO_VERSION) == NULL);
104 g_assert (glib_check_version (GLIB_MAJOR_VERSION,
107 g_assert (glib_check_version (GLIB_MAJOR_VERSION - 1,
110 g_assert (glib_check_version (GLIB_MAJOR_VERSION + 1,
113 g_assert (glib_check_version (GLIB_MAJOR_VERSION,
114 GLIB_MINOR_VERSION + 1,
116 /* don't use + 1 here, since a +/-1 difference can
117 * happen due to post-release version bumps in git
119 g_assert (glib_check_version (GLIB_MAJOR_VERSION,
121 GLIB_MICRO_VERSION + 3) != NULL);
124 static const gchar *argv0;
129 const gchar *prgname;
130 const gchar *appname;
132 prgname = g_get_prgname ();
133 appname = g_get_application_name ();
134 g_assert_cmpstr (prgname, ==, argv0);
135 g_assert_cmpstr (appname, ==, prgname);
137 g_set_prgname ("prgname");
139 prgname = g_get_prgname ();
140 appname = g_get_application_name ();
141 g_assert_cmpstr (prgname, ==, "prgname");
142 g_assert_cmpstr (appname, ==, "prgname");
144 g_set_application_name ("appname");
146 prgname = g_get_prgname ();
147 appname = g_get_application_name ();
148 g_assert_cmpstr (prgname, ==, "prgname");
149 g_assert_cmpstr (appname, ==, "appname");
155 g_test_bug ("627969");
156 g_assert_cmpstr (g_get_tmp_dir (), !=, "");
166 pos = g_bit_nth_lsf (0, -1);
167 g_assert_cmpint (pos, ==, -1);
169 max_bit = sizeof (gulong) * 8;
170 for (i = 0; i < max_bit; i++)
174 pos = g_bit_nth_lsf (mask, -1);
175 g_assert_cmpint (pos, ==, i);
177 pos = g_bit_nth_lsf (mask, i - 3);
178 g_assert_cmpint (pos , ==, i);
180 pos = g_bit_nth_lsf (mask, i);
181 g_assert_cmpint (pos , ==, -1);
183 pos = g_bit_nth_lsf (mask, i + 1);
184 g_assert_cmpint (pos , ==, -1);
187 pos = g_bit_nth_msf (0, -1);
188 g_assert_cmpint (pos, ==, -1);
190 for (i = 0; i < max_bit; i++)
194 pos = g_bit_nth_msf (mask, -1);
195 g_assert_cmpint (pos, ==, i);
197 pos = g_bit_nth_msf (mask, i + 3);
198 g_assert_cmpint (pos , ==, i);
200 pos = g_bit_nth_msf (mask, i);
201 g_assert_cmpint (pos , ==, -1);
205 pos = g_bit_nth_msf (mask, i - 1);
206 g_assert_cmpint (pos , ==, -1);
221 g_assert_cmpint (GUINT16_SWAP_LE_BE (a16), ==, b16);
226 g_assert_cmpint (GUINT32_SWAP_LE_BE (a32), ==, b32);
228 a64 = G_GUINT64_CONSTANT(0xaaaaaaaabbbbbbbb);
229 b64 = G_GUINT64_CONSTANT(0xbbbbbbbbaaaaaaaa);
231 g_assert_cmpint (GUINT64_SWAP_LE_BE (a64), ==, b64);
235 test_find_program (void)
240 res = g_find_program_in_path ("sh");
241 g_assert (res != NULL);
244 res = g_find_program_in_path ("/bin/sh");
245 g_assert (res != NULL);
248 /* There's not a lot we can search for that would reliably work both
249 * on real Windows and mingw.
253 res = g_find_program_in_path ("this_program_does_not_exit");
254 g_assert (res == NULL);
256 res = g_find_program_in_path ("/bin");
257 g_assert (res == NULL);
259 res = g_find_program_in_path ("/etc/passwd");
260 g_assert (res == NULL);
264 test_debug_help (void)
273 res = g_parse_debug_string ("help", keys, G_N_ELEMENTS (keys));
274 g_assert_cmpint (res, ==, 0);
287 res = g_parse_debug_string (NULL, keys, G_N_ELEMENTS (keys));
288 g_assert_cmpint (res, ==, 0);
290 res = g_parse_debug_string ("foobabla;#!%!$%112 223", keys, G_N_ELEMENTS (keys));
291 g_assert_cmpint (res, ==, 0);
293 res = g_parse_debug_string ("key1:key2", keys, G_N_ELEMENTS (keys));
294 g_assert_cmpint (res, ==, 3);
296 res = g_parse_debug_string ("key1;key2", keys, G_N_ELEMENTS (keys));
297 g_assert_cmpint (res, ==, 3);
299 res = g_parse_debug_string ("key1,key2", keys, G_N_ELEMENTS (keys));
300 g_assert_cmpint (res, ==, 3);
302 res = g_parse_debug_string ("key1 key2", keys, G_N_ELEMENTS (keys));
303 g_assert_cmpint (res, ==, 3);
305 res = g_parse_debug_string ("key1\tkey2", keys, G_N_ELEMENTS (keys));
306 g_assert_cmpint (res, ==, 3);
308 res = g_parse_debug_string ("all", keys, G_N_ELEMENTS (keys));
309 g_assert_cmpint (res, ==, 7);
311 g_test_trap_subprocess ("/utils/debug/subprocess/help", 0, 0);
312 g_test_trap_assert_passed ();
313 g_test_trap_assert_stderr ("*Supported debug values: key1 key2 key3 all help*");
322 c = g_get_codeset ();
325 g_assert_cmpstr (c, ==, c2);
333 const gchar *path = "/path/to/a/file/deep/down.sh";
336 b = g_basename (path);
338 g_assert_cmpstr (b, ==, "down.sh");
341 extern const gchar *glib_pgettext (const gchar *msgidctxt, gsize msgidoffset);
346 const gchar *am0, *am1, *am2, *am3;
348 am0 = glib_pgettext ("GDateTime\004AM", strlen ("GDateTime") + 1);
349 am1 = g_dpgettext ("glib20", "GDateTime\004AM", strlen ("GDateTime") + 1);
350 am2 = g_dpgettext ("glib20", "GDateTime|AM", 0);
351 am3 = g_dpgettext2 ("glib20", "GDateTime", "AM");
353 g_assert_cmpstr (am0, ==, am1);
354 g_assert_cmpstr (am1, ==, am2);
355 g_assert_cmpstr (am2, ==, am3);
363 name = g_get_user_name ();
365 g_assert (name != NULL);
373 name = g_get_real_name ();
375 g_assert (name != NULL);
383 name = g_get_host_name ();
385 g_assert (name != NULL);
394 const gchar * const *dirs;
397 xdg = g_strdup (g_getenv ("XDG_CONFIG_HOME"));
399 xdg = g_build_filename (g_get_home_dir (), ".config", NULL);
401 dir = g_get_user_config_dir ();
403 g_assert_cmpstr (dir, ==, xdg);
406 xdg = g_strdup (g_getenv ("XDG_DATA_HOME"));
408 xdg = g_build_filename (g_get_home_dir (), ".local", "share", NULL);
410 dir = g_get_user_data_dir ();
412 g_assert_cmpstr (dir, ==, xdg);
415 xdg = g_strdup (g_getenv ("XDG_CACHE_HOME"));
417 xdg = g_build_filename (g_get_home_dir (), ".cache", NULL);
419 dir = g_get_user_cache_dir ();
421 g_assert_cmpstr (dir, ==, xdg);
424 xdg = g_strdup (g_getenv ("XDG_RUNTIME_DIR"));
426 xdg = g_strdup (g_get_user_cache_dir ());
428 dir = g_get_user_runtime_dir ();
430 g_assert_cmpstr (dir, ==, xdg);
433 xdg = (gchar *)g_getenv ("XDG_CONFIG_DIRS");
437 dirs = g_get_system_config_dirs ();
439 s = g_strjoinv (":", (gchar **)dirs);
441 g_assert_cmpstr (s, ==, xdg);
443 g_strfreev ((gchar **)dirs);
449 test_special_dir (void)
451 const gchar *dir, *dir2;
453 dir = g_get_user_special_dir (G_USER_DIRECTORY_DESKTOP);
454 g_reload_user_special_dirs_cache ();
455 dir2 = g_get_user_special_dir (G_USER_DIRECTORY_DESKTOP);
457 g_assert_cmpstr (dir, ==, dir2);
461 test_desktop_special_dir (void)
463 const gchar *dir, *dir2;
465 dir = g_get_user_special_dir (G_USER_DIRECTORY_DESKTOP);
466 g_assert (dir != NULL);
468 g_reload_user_special_dirs_cache ();
469 dir2 = g_get_user_special_dir (G_USER_DIRECTORY_DESKTOP);
470 g_assert (dir2 != NULL);
474 test_clear_pointer (void)
479 g_clear_pointer (&a, g_free);
480 g_assert (a == NULL);
483 (g_clear_pointer) (&a, g_free);
484 g_assert (a == NULL);
492 a = g_try_malloc (0);
493 g_assert (a == NULL);
495 a = g_try_malloc0 (0);
496 g_assert (a == NULL);
499 a = g_try_realloc (a, 20);
500 a = g_try_realloc (a, 0);
502 g_assert (a == NULL);
508 gpointer p = &test_nullify;
510 g_assert (p != NULL);
512 g_nullify_pointer (&p);
514 g_assert (p == NULL);
523 /* for tmpdir test, need to do this early before g_get_any_init */
524 g_setenv ("TMPDIR", "", TRUE);
528 /* g_test_init() only calls g_set_prgname() if g_get_prgname()
529 * returns %NULL, but g_get_prgname() on Windows never returns NULL.
530 * So we need to do this by hand to make test_appname() work on
533 g_set_prgname (argv[0]);
535 g_test_init (&argc, &argv, NULL);
536 g_test_bug_base ("http://bugzilla.gnome.org/");
538 g_test_add_func ("/utils/language-names", test_language_names);
539 g_test_add_func ("/utils/locale-variants", test_locale_variants);
540 g_test_add_func ("/utils/version", test_version);
541 g_test_add_func ("/utils/appname", test_appname);
542 g_test_add_func ("/utils/tmpdir", test_tmpdir);
543 g_test_add_func ("/utils/bits", test_bits);
544 g_test_add_func ("/utils/swap", test_swap);
545 g_test_add_func ("/utils/find-program", test_find_program);
546 g_test_add_func ("/utils/debug", test_debug);
547 g_test_add_func ("/utils/debug/subprocess/help", test_debug_help);
548 g_test_add_func ("/utils/codeset", test_codeset);
549 g_test_add_func ("/utils/basename", test_basename);
550 g_test_add_func ("/utils/gettext", test_gettext);
551 g_test_add_func ("/utils/username", test_username);
552 g_test_add_func ("/utils/realname", test_realname);
553 g_test_add_func ("/utils/hostname", test_hostname);
555 g_test_add_func ("/utils/xdgdirs", test_xdg_dirs);
557 g_test_add_func ("/utils/specialdir", test_special_dir);
558 g_test_add_func ("/utils/specialdir/desktop", test_desktop_special_dir);
559 g_test_add_func ("/utils/clear-pointer", test_clear_pointer);
560 g_test_add_func ("/utils/misc-mem", test_misc_mem);
561 g_test_add_func ("/utils/nullify", test_nullify);
563 return g_test_run ();