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
27 #include "glib-private.h"
34 strv_check (const gchar
* const *strv
, ...)
40 va_start (args
, strv
);
41 for (i
= 0; strv
[i
]; i
++)
43 s
= va_arg (args
, gchar
*);
44 if (g_strcmp0 (strv
[i
], s
) != 0)
57 test_language_names (void)
59 const gchar
* const *names
;
61 g_setenv ("LANGUAGE", "de:en_US", TRUE
);
62 names
= g_get_language_names ();
63 g_assert (strv_check (names
, "de", "en_US", "en", "C", NULL
));
65 g_setenv ("LANGUAGE", "tt_RU.UTF-8@iqtelif", TRUE
);
66 names
= g_get_language_names ();
67 g_assert (strv_check (names
,
68 "tt_RU.UTF-8@iqtelif",
81 test_locale_variants (void)
85 v
= g_get_locale_variants ("fr_BE");
86 g_assert (strv_check ((const gchar
* const *) v
, "fr_BE", "fr", NULL
));
89 v
= g_get_locale_variants ("sr_SR@latin");
90 g_assert (strv_check ((const gchar
* const *) v
, "sr_SR@latin", "sr@latin", "sr_SR", "sr", NULL
));
97 if (g_test_verbose ())
98 g_printerr ("(header %d.%d.%d library %d.%d.%d) ",
99 GLIB_MAJOR_VERSION
, GLIB_MINOR_VERSION
, GLIB_MICRO_VERSION
,
100 glib_major_version
, glib_minor_version
, glib_micro_version
);
102 g_assert (glib_check_version (GLIB_MAJOR_VERSION
,
104 GLIB_MICRO_VERSION
) == NULL
);
105 g_assert (glib_check_version (GLIB_MAJOR_VERSION
,
108 g_assert (glib_check_version (GLIB_MAJOR_VERSION
- 1,
111 g_assert (glib_check_version (GLIB_MAJOR_VERSION
+ 1,
114 g_assert (glib_check_version (GLIB_MAJOR_VERSION
,
115 GLIB_MINOR_VERSION
+ 1,
117 /* don't use + 1 here, since a +/-1 difference can
118 * happen due to post-release version bumps in git
120 g_assert (glib_check_version (GLIB_MAJOR_VERSION
,
122 GLIB_MICRO_VERSION
+ 3) != NULL
);
125 static const gchar
*argv0
;
130 const gchar
*prgname
;
131 const gchar
*appname
;
133 prgname
= g_get_prgname ();
134 appname
= g_get_application_name ();
135 g_assert_cmpstr (prgname
, ==, argv0
);
136 g_assert_cmpstr (appname
, ==, prgname
);
138 g_set_prgname ("prgname");
140 prgname
= g_get_prgname ();
141 appname
= g_get_application_name ();
142 g_assert_cmpstr (prgname
, ==, "prgname");
143 g_assert_cmpstr (appname
, ==, "prgname");
145 g_set_application_name ("appname");
147 prgname
= g_get_prgname ();
148 appname
= g_get_application_name ();
149 g_assert_cmpstr (prgname
, ==, "prgname");
150 g_assert_cmpstr (appname
, ==, "appname");
156 g_test_bug ("627969");
157 g_assert_cmpstr (g_get_tmp_dir (), !=, "");
167 pos
= g_bit_nth_lsf (0, -1);
168 g_assert_cmpint (pos
, ==, -1);
170 max_bit
= sizeof (gulong
) * 8;
171 for (i
= 0; i
< max_bit
; i
++)
175 pos
= g_bit_nth_lsf (mask
, -1);
176 g_assert_cmpint (pos
, ==, i
);
178 pos
= g_bit_nth_lsf (mask
, i
- 3);
179 g_assert_cmpint (pos
, ==, i
);
181 pos
= g_bit_nth_lsf (mask
, i
);
182 g_assert_cmpint (pos
, ==, -1);
184 pos
= g_bit_nth_lsf (mask
, i
+ 1);
185 g_assert_cmpint (pos
, ==, -1);
188 pos
= g_bit_nth_msf (0, -1);
189 g_assert_cmpint (pos
, ==, -1);
191 for (i
= 0; i
< max_bit
; i
++)
195 pos
= g_bit_nth_msf (mask
, -1);
196 g_assert_cmpint (pos
, ==, i
);
198 pos
= g_bit_nth_msf (mask
, i
+ 3);
199 g_assert_cmpint (pos
, ==, i
);
201 pos
= g_bit_nth_msf (mask
, i
);
202 g_assert_cmpint (pos
, ==, -1);
206 pos
= g_bit_nth_msf (mask
, i
- 1);
207 g_assert_cmpint (pos
, ==, -1);
222 g_assert_cmpint (GUINT16_SWAP_LE_BE (a16
), ==, b16
);
227 g_assert_cmpint (GUINT32_SWAP_LE_BE (a32
), ==, b32
);
229 a64
= G_GUINT64_CONSTANT(0xaaaaaaaabbbbbbbb);
230 b64
= G_GUINT64_CONSTANT(0xbbbbbbbbaaaaaaaa);
232 g_assert_cmpint (GUINT64_SWAP_LE_BE (a64
), ==, b64
);
236 test_find_program (void)
241 res
= g_find_program_in_path ("sh");
242 g_assert (res
!= NULL
);
245 res
= g_find_program_in_path ("/bin/sh");
246 g_assert (res
!= NULL
);
249 /* There's not a lot we can search for that would reliably work both
250 * on real Windows and mingw.
254 res
= g_find_program_in_path ("this_program_does_not_exit");
255 g_assert (res
== NULL
);
257 res
= g_find_program_in_path ("/bin");
258 g_assert (res
== NULL
);
260 res
= g_find_program_in_path ("/etc/passwd");
261 g_assert (res
== NULL
);
274 res
= g_parse_debug_string (NULL
, keys
, G_N_ELEMENTS (keys
));
275 g_assert_cmpint (res
, ==, 0);
277 res
= g_parse_debug_string ("foobabla;#!%!$%112 223", keys
, G_N_ELEMENTS (keys
));
278 g_assert_cmpint (res
, ==, 0);
280 res
= g_parse_debug_string ("key1:key2", keys
, G_N_ELEMENTS (keys
));
281 g_assert_cmpint (res
, ==, 3);
283 res
= g_parse_debug_string ("key1;key2", keys
, G_N_ELEMENTS (keys
));
284 g_assert_cmpint (res
, ==, 3);
286 res
= g_parse_debug_string ("key1,key2", keys
, G_N_ELEMENTS (keys
));
287 g_assert_cmpint (res
, ==, 3);
289 res
= g_parse_debug_string ("key1 key2", keys
, G_N_ELEMENTS (keys
));
290 g_assert_cmpint (res
, ==, 3);
292 res
= g_parse_debug_string ("key1\tkey2", keys
, G_N_ELEMENTS (keys
));
293 g_assert_cmpint (res
, ==, 3);
295 res
= g_parse_debug_string ("all", keys
, G_N_ELEMENTS (keys
));
296 g_assert_cmpint (res
, ==, 7);
298 if (g_test_subprocess ())
300 res
= g_parse_debug_string ("help", keys
, G_N_ELEMENTS (keys
));
301 g_assert_cmpint (res
, ==, 0);
304 g_test_trap_subprocess (NULL
, 0, 0);
305 g_test_trap_assert_passed ();
306 g_test_trap_assert_stderr ("*Supported debug values: key1 key2 key3 all help*");
315 c
= g_get_codeset ();
318 g_assert_cmpstr (c
, ==, c2
);
326 if (g_test_subprocess ())
329 g_setenv ("CHARSET", "UTF-8", TRUE
);
331 g_assert_cmpstr (c
, ==, "UTF-8");
334 g_test_trap_subprocess (NULL
, 0, 0);
335 g_test_trap_assert_passed ();
341 const gchar
*path
= "/path/to/a/file/deep/down.sh";
344 b
= g_basename (path
);
346 g_assert_cmpstr (b
, ==, "down.sh");
349 extern const gchar
*glib_pgettext (const gchar
*msgidctxt
, gsize msgidoffset
);
354 const gchar
*am0
, *am1
, *am2
, *am3
;
356 am0
= glib_pgettext ("GDateTime\004AM", strlen ("GDateTime") + 1);
357 am1
= g_dpgettext ("glib20", "GDateTime\004AM", strlen ("GDateTime") + 1);
358 am2
= g_dpgettext ("glib20", "GDateTime|AM", 0);
359 am3
= g_dpgettext2 ("glib20", "GDateTime", "AM");
361 g_assert_cmpstr (am0
, ==, am1
);
362 g_assert_cmpstr (am1
, ==, am2
);
363 g_assert_cmpstr (am2
, ==, am3
);
371 name
= g_get_user_name ();
373 g_assert (name
!= NULL
);
381 name
= g_get_real_name ();
383 g_assert (name
!= NULL
);
391 name
= g_get_host_name ();
393 g_assert (name
!= NULL
);
394 g_assert_true (g_utf8_validate (name
, -1, NULL
));
403 const gchar
* const *dirs
;
406 xdg
= g_strdup (g_getenv ("XDG_CONFIG_HOME"));
408 xdg
= g_build_filename (g_get_home_dir (), ".config", NULL
);
410 dir
= g_get_user_config_dir ();
412 g_assert_cmpstr (dir
, ==, xdg
);
415 xdg
= g_strdup (g_getenv ("XDG_DATA_HOME"));
417 xdg
= g_build_filename (g_get_home_dir (), ".local", "share", NULL
);
419 dir
= g_get_user_data_dir ();
421 g_assert_cmpstr (dir
, ==, xdg
);
424 xdg
= g_strdup (g_getenv ("XDG_CACHE_HOME"));
426 xdg
= g_build_filename (g_get_home_dir (), ".cache", NULL
);
428 dir
= g_get_user_cache_dir ();
430 g_assert_cmpstr (dir
, ==, xdg
);
433 xdg
= g_strdup (g_getenv ("XDG_RUNTIME_DIR"));
435 xdg
= g_strdup (g_get_user_cache_dir ());
437 dir
= g_get_user_runtime_dir ();
439 g_assert_cmpstr (dir
, ==, xdg
);
442 xdg
= (gchar
*)g_getenv ("XDG_CONFIG_DIRS");
446 dirs
= g_get_system_config_dirs ();
448 s
= g_strjoinv (":", (gchar
**)dirs
);
450 g_assert_cmpstr (s
, ==, xdg
);
457 test_special_dir (void)
459 const gchar
*dir
, *dir2
;
461 dir
= g_get_user_special_dir (G_USER_DIRECTORY_DESKTOP
);
462 g_reload_user_special_dirs_cache ();
463 dir2
= g_get_user_special_dir (G_USER_DIRECTORY_DESKTOP
);
465 g_assert_cmpstr (dir
, ==, dir2
);
469 test_desktop_special_dir (void)
471 const gchar
*dir
, *dir2
;
473 dir
= g_get_user_special_dir (G_USER_DIRECTORY_DESKTOP
);
474 g_assert (dir
!= NULL
);
476 g_reload_user_special_dirs_cache ();
477 dir2
= g_get_user_special_dir (G_USER_DIRECTORY_DESKTOP
);
478 g_assert (dir2
!= NULL
);
482 source_test (gpointer data
)
484 g_assert_not_reached ();
485 return G_SOURCE_REMOVE
;
489 test_clear_source (void)
493 id
= g_idle_add (source_test
, NULL
);
494 g_assert_cmpuint (id
, >, 0);
496 g_clear_handle_id (&id
, g_source_remove
);
497 g_assert_cmpuint (id
, ==, 0);
499 id
= g_timeout_add (100, source_test
, NULL
);
500 g_assert_cmpuint (id
, >, 0);
502 g_clear_handle_id (&id
, g_source_remove
);
503 g_assert_cmpuint (id
, ==, 0);
507 test_clear_pointer (void)
512 g_clear_pointer (&a
, g_free
);
513 g_assert (a
== NULL
);
516 (g_clear_pointer
) (&a
, g_free
);
517 g_assert (a
== NULL
);
520 static int obj_count
;
523 get_obj (gpointer
*obj_out
)
525 gpointer obj
= g_malloc (5);
529 *obj_out
= g_steal_pointer (&obj
);
539 test_take_pointer (void)
549 /* ensure that it works to skip the macro */
550 b
= (g_steal_pointer
) (&a
);
555 g_assert (!obj_count
);
563 a
= g_try_malloc (0);
564 g_assert (a
== NULL
);
566 a
= g_try_malloc0 (0);
567 g_assert (a
== NULL
);
570 a
= g_try_realloc (a
, 20);
571 a
= g_try_realloc (a
, 0);
573 g_assert (a
== NULL
);
579 gpointer p
= &test_nullify
;
581 g_assert (p
!= NULL
);
583 g_nullify_pointer (&p
);
585 g_assert (p
== NULL
);
591 g_print ("atexit called");
597 if (g_test_subprocess ())
599 g_atexit (atexit_func
);
602 g_test_trap_subprocess (NULL
, 0, 0);
603 g_test_trap_assert_passed ();
604 g_test_trap_assert_stdout ("*atexit called*");
608 test_check_setuid (void)
612 res
= GLIB_PRIVATE_CALL(g_check_setuid
) ();
622 /* for tmpdir test, need to do this early before g_get_any_init */
623 g_setenv ("TMPDIR", "", TRUE
);
627 /* g_test_init() only calls g_set_prgname() if g_get_prgname()
628 * returns %NULL, but g_get_prgname() on Windows never returns NULL.
629 * So we need to do this by hand to make test_appname() work on
632 g_set_prgname (argv
[0]);
634 g_test_init (&argc
, &argv
, NULL
);
635 g_test_bug_base ("http://bugzilla.gnome.org/");
637 g_test_add_func ("/utils/language-names", test_language_names
);
638 g_test_add_func ("/utils/locale-variants", test_locale_variants
);
639 g_test_add_func ("/utils/version", test_version
);
640 g_test_add_func ("/utils/appname", test_appname
);
641 g_test_add_func ("/utils/tmpdir", test_tmpdir
);
642 g_test_add_func ("/utils/bits", test_bits
);
643 g_test_add_func ("/utils/swap", test_swap
);
644 g_test_add_func ("/utils/find-program", test_find_program
);
645 g_test_add_func ("/utils/debug", test_debug
);
646 g_test_add_func ("/utils/codeset", test_codeset
);
647 g_test_add_func ("/utils/codeset2", test_codeset2
);
648 g_test_add_func ("/utils/basename", test_basename
);
649 g_test_add_func ("/utils/gettext", test_gettext
);
650 g_test_add_func ("/utils/username", test_username
);
651 g_test_add_func ("/utils/realname", test_realname
);
652 g_test_add_func ("/utils/hostname", test_hostname
);
654 g_test_add_func ("/utils/xdgdirs", test_xdg_dirs
);
656 g_test_add_func ("/utils/specialdir", test_special_dir
);
657 g_test_add_func ("/utils/specialdir/desktop", test_desktop_special_dir
);
658 g_test_add_func ("/utils/clear-pointer", test_clear_pointer
);
659 g_test_add_func ("/utils/take-pointer", test_take_pointer
);
660 g_test_add_func ("/utils/clear-source", test_clear_source
);
661 g_test_add_func ("/utils/misc-mem", test_misc_mem
);
662 g_test_add_func ("/utils/nullify", test_nullify
);
663 g_test_add_func ("/utils/atexit", test_atexit
);
664 g_test_add_func ("/utils/check-setuid", test_check_setuid
);
666 return g_test_run ();