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
30 strv_check (const gchar * const *strv, ...)
36 va_start (args, strv);
37 for (i = 0; strv[i]; i++)
39 s = va_arg (args, gchar*);
40 if (g_strcmp0 (strv[i], s) != 0)
53 test_language_names (void)
55 const gchar * const *names;
57 g_setenv ("LANGUAGE", "de:en_US", TRUE);
58 names = g_get_language_names ();
59 g_assert (strv_check (names, "de", "en_US", "en", "C", NULL));
61 g_setenv ("LANGUAGE", "tt_RU.UTF-8@iqtelif", TRUE);
62 names = g_get_language_names ();
63 g_assert (strv_check (names,
64 "tt_RU.UTF-8@iqtelif",
77 test_locale_variants (void)
81 v = g_get_locale_variants ("fr_BE");
82 g_assert (strv_check ((const gchar * const *) v, "fr_BE", "fr", NULL));
85 v = g_get_locale_variants ("sr_SR@latin");
86 g_assert (strv_check ((const gchar * const *) v, "sr_SR@latin", "sr@latin", "sr_SR", "sr", NULL));
93 if (g_test_verbose ())
94 g_print ("(header %d.%d.%d library %d.%d.%d) ",
95 GLIB_MAJOR_VERSION, GLIB_MINOR_VERSION, GLIB_MICRO_VERSION,
96 glib_major_version, glib_minor_version, glib_micro_version);
98 g_assert (glib_check_version (GLIB_MAJOR_VERSION,
100 GLIB_MICRO_VERSION) == NULL);
101 g_assert (glib_check_version (GLIB_MAJOR_VERSION,
104 g_assert (glib_check_version (GLIB_MAJOR_VERSION - 1,
107 g_assert (glib_check_version (GLIB_MAJOR_VERSION + 1,
110 g_assert (glib_check_version (GLIB_MAJOR_VERSION,
111 GLIB_MINOR_VERSION + 1,
113 /* don't use + 1 here, since a +/-1 difference can
114 * happen due to post-release version bumps in git
116 g_assert (glib_check_version (GLIB_MAJOR_VERSION,
118 GLIB_MICRO_VERSION + 3) != NULL);
121 static const gchar *argv0;
126 const gchar *prgname;
127 const gchar *appname;
129 prgname = g_get_prgname ();
130 appname = g_get_application_name ();
131 g_assert_cmpstr (prgname, ==, argv0);
132 g_assert_cmpstr (appname, ==, prgname);
134 g_set_prgname ("prgname");
136 prgname = g_get_prgname ();
137 appname = g_get_application_name ();
138 g_assert_cmpstr (prgname, ==, "prgname");
139 g_assert_cmpstr (appname, ==, "prgname");
141 g_set_application_name ("appname");
143 prgname = g_get_prgname ();
144 appname = g_get_application_name ();
145 g_assert_cmpstr (prgname, ==, "prgname");
146 g_assert_cmpstr (appname, ==, "appname");
152 g_test_bug ("627969");
153 g_assert_cmpstr (g_get_tmp_dir (), !=, "");
163 pos = g_bit_nth_lsf (0, -1);
164 g_assert_cmpint (pos, ==, -1);
166 max_bit = sizeof (gulong) * 8;
167 for (i = 0; i < max_bit; i++)
171 pos = g_bit_nth_lsf (mask, -1);
172 g_assert_cmpint (pos, ==, i);
174 pos = g_bit_nth_lsf (mask, i - 3);
175 g_assert_cmpint (pos , ==, i);
177 pos = g_bit_nth_lsf (mask, i);
178 g_assert_cmpint (pos , ==, -1);
180 pos = g_bit_nth_lsf (mask, i + 1);
181 g_assert_cmpint (pos , ==, -1);
184 pos = g_bit_nth_msf (0, -1);
185 g_assert_cmpint (pos, ==, -1);
187 for (i = 0; i < max_bit; i++)
191 pos = g_bit_nth_msf (mask, -1);
192 g_assert_cmpint (pos, ==, i);
194 pos = g_bit_nth_msf (mask, i + 3);
195 g_assert_cmpint (pos , ==, i);
197 pos = g_bit_nth_msf (mask, i);
198 g_assert_cmpint (pos , ==, -1);
202 pos = g_bit_nth_msf (mask, i - 1);
203 g_assert_cmpint (pos , ==, -1);
209 test_find_program (void)
213 res = g_find_program_in_path ("sh");
214 g_assert_cmpstr (res, ==, "/bin/sh");
217 res = g_find_program_in_path ("/bin/sh");
218 g_assert_cmpstr (res, ==, "/bin/sh");
221 res = g_find_program_in_path ("this_program_does_not_exit");
222 g_assert (res == NULL);
224 res = g_find_program_in_path ("/bin");
225 g_assert (res == NULL);
227 res = g_find_program_in_path ("/etc/passwd");
228 g_assert (res == NULL);
241 res = g_parse_debug_string (NULL, keys, G_N_ELEMENTS (keys));
242 g_assert_cmpint (res, ==, 0);
244 res = g_parse_debug_string ("foobabla;#!%!$%112 223", keys, G_N_ELEMENTS (keys));
245 g_assert_cmpint (res, ==, 0);
247 res = g_parse_debug_string ("key1:key2", keys, G_N_ELEMENTS (keys));
248 g_assert_cmpint (res, ==, 3);
250 res = g_parse_debug_string ("key1;key2", keys, G_N_ELEMENTS (keys));
251 g_assert_cmpint (res, ==, 3);
253 res = g_parse_debug_string ("key1,key2", keys, G_N_ELEMENTS (keys));
254 g_assert_cmpint (res, ==, 3);
256 res = g_parse_debug_string ("key1 key2", keys, G_N_ELEMENTS (keys));
257 g_assert_cmpint (res, ==, 3);
259 res = g_parse_debug_string ("key1\tkey2", keys, G_N_ELEMENTS (keys));
260 g_assert_cmpint (res, ==, 3);
262 res = g_parse_debug_string ("all", keys, G_N_ELEMENTS (keys));
263 g_assert_cmpint (res, ==, 7);
265 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
267 res = g_parse_debug_string ("help", keys, G_N_ELEMENTS (keys));
268 g_assert_cmpint (res, ==, 0);
271 g_test_trap_assert_passed ();
272 g_test_trap_assert_stderr ("*Supported debug values: key1 key2 key3*");
281 /* for tmpdir test, need to do this early before g_get_any_init */
282 g_setenv ("TMPDIR", "", TRUE);
286 g_test_init (&argc, &argv, NULL);
287 g_test_bug_base ("http://bugzilla.gnome.org/");
289 g_test_add_func ("/utils/language-names", test_language_names);
290 g_test_add_func ("/utils/locale-variants", test_locale_variants);
291 g_test_add_func ("/utils/version", test_version);
292 g_test_add_func ("/utils/appname", test_appname);
293 g_test_add_func ("/utils/tmpdir", test_tmpdir);
294 g_test_add_func ("/utils/bits", test_bits);
295 g_test_add_func ("/utils/find-program", test_find_program);
296 g_test_add_func ("/utils/debug", test_debug);