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
32 strv_check (const gchar * const *strv, ...)
38 va_start (args, strv);
39 for (i = 0; strv[i]; i++)
41 s = va_arg (args, gchar*);
42 if (g_strcmp0 (strv[i], s) != 0)
55 test_language_names (void)
57 const gchar * const *names;
59 g_setenv ("LANGUAGE", "de:en_US", TRUE);
60 names = g_get_language_names ();
61 g_assert (strv_check (names, "de", "en_US", "en", "C", NULL));
63 g_setenv ("LANGUAGE", "tt_RU.UTF-8@iqtelif", TRUE);
64 names = g_get_language_names ();
65 g_assert (strv_check (names,
66 "tt_RU.UTF-8@iqtelif",
79 test_locale_variants (void)
83 v = g_get_locale_variants ("fr_BE");
84 g_assert (strv_check ((const gchar * const *) v, "fr_BE", "fr", NULL));
87 v = g_get_locale_variants ("sr_SR@latin");
88 g_assert (strv_check ((const gchar * const *) v, "sr_SR@latin", "sr@latin", "sr_SR", "sr", NULL));
95 if (g_test_verbose ())
96 g_print ("(header %d.%d.%d library %d.%d.%d) ",
97 GLIB_MAJOR_VERSION, GLIB_MINOR_VERSION, GLIB_MICRO_VERSION,
98 glib_major_version, glib_minor_version, glib_micro_version);
100 g_assert (glib_check_version (GLIB_MAJOR_VERSION,
102 GLIB_MICRO_VERSION) == NULL);
103 g_assert (glib_check_version (GLIB_MAJOR_VERSION,
106 g_assert (glib_check_version (GLIB_MAJOR_VERSION - 1,
109 g_assert (glib_check_version (GLIB_MAJOR_VERSION + 1,
112 g_assert (glib_check_version (GLIB_MAJOR_VERSION,
113 GLIB_MINOR_VERSION + 1,
115 /* don't use + 1 here, since a +/-1 difference can
116 * happen due to post-release version bumps in git
118 g_assert (glib_check_version (GLIB_MAJOR_VERSION,
120 GLIB_MICRO_VERSION + 3) != NULL);
123 static const gchar *argv0;
128 const gchar *prgname;
129 const gchar *appname;
131 prgname = g_get_prgname ();
132 appname = g_get_application_name ();
133 g_assert_cmpstr (prgname, ==, argv0);
134 g_assert_cmpstr (appname, ==, prgname);
136 g_set_prgname ("prgname");
138 prgname = g_get_prgname ();
139 appname = g_get_application_name ();
140 g_assert_cmpstr (prgname, ==, "prgname");
141 g_assert_cmpstr (appname, ==, "prgname");
143 g_set_application_name ("appname");
145 prgname = g_get_prgname ();
146 appname = g_get_application_name ();
147 g_assert_cmpstr (prgname, ==, "prgname");
148 g_assert_cmpstr (appname, ==, "appname");
154 g_test_bug ("627969");
155 g_assert_cmpstr (g_get_tmp_dir (), !=, "");
165 pos = g_bit_nth_lsf (0, -1);
166 g_assert_cmpint (pos, ==, -1);
168 max_bit = sizeof (gulong) * 8;
169 for (i = 0; i < max_bit; i++)
173 pos = g_bit_nth_lsf (mask, -1);
174 g_assert_cmpint (pos, ==, i);
176 pos = g_bit_nth_lsf (mask, i - 3);
177 g_assert_cmpint (pos , ==, i);
179 pos = g_bit_nth_lsf (mask, i);
180 g_assert_cmpint (pos , ==, -1);
182 pos = g_bit_nth_lsf (mask, i + 1);
183 g_assert_cmpint (pos , ==, -1);
186 pos = g_bit_nth_msf (0, -1);
187 g_assert_cmpint (pos, ==, -1);
189 for (i = 0; i < max_bit; i++)
193 pos = g_bit_nth_msf (mask, -1);
194 g_assert_cmpint (pos, ==, i);
196 pos = g_bit_nth_msf (mask, i + 3);
197 g_assert_cmpint (pos , ==, i);
199 pos = g_bit_nth_msf (mask, i);
200 g_assert_cmpint (pos , ==, -1);
204 pos = g_bit_nth_msf (mask, i - 1);
205 g_assert_cmpint (pos , ==, -1);
211 test_find_program (void)
215 res = g_find_program_in_path ("sh");
216 g_assert_cmpstr (res, ==, "/bin/sh");
219 res = g_find_program_in_path ("/bin/sh");
220 g_assert_cmpstr (res, ==, "/bin/sh");
223 res = g_find_program_in_path ("this_program_does_not_exit");
224 g_assert (res == NULL);
226 res = g_find_program_in_path ("/bin");
227 g_assert (res == NULL);
229 res = g_find_program_in_path ("/etc/passwd");
230 g_assert (res == NULL);
243 res = g_parse_debug_string (NULL, keys, G_N_ELEMENTS (keys));
244 g_assert_cmpint (res, ==, 0);
246 res = g_parse_debug_string ("foobabla;#!%!$%112 223", keys, G_N_ELEMENTS (keys));
247 g_assert_cmpint (res, ==, 0);
249 res = g_parse_debug_string ("key1:key2", keys, G_N_ELEMENTS (keys));
250 g_assert_cmpint (res, ==, 3);
252 res = g_parse_debug_string ("key1;key2", keys, G_N_ELEMENTS (keys));
253 g_assert_cmpint (res, ==, 3);
255 res = g_parse_debug_string ("key1,key2", keys, G_N_ELEMENTS (keys));
256 g_assert_cmpint (res, ==, 3);
258 res = g_parse_debug_string ("key1 key2", keys, G_N_ELEMENTS (keys));
259 g_assert_cmpint (res, ==, 3);
261 res = g_parse_debug_string ("key1\tkey2", keys, G_N_ELEMENTS (keys));
262 g_assert_cmpint (res, ==, 3);
264 res = g_parse_debug_string ("all", keys, G_N_ELEMENTS (keys));
265 g_assert_cmpint (res, ==, 7);
267 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
269 res = g_parse_debug_string ("help", keys, G_N_ELEMENTS (keys));
270 g_assert_cmpint (res, ==, 0);
273 g_test_trap_assert_passed ();
274 g_test_trap_assert_stderr ("*Supported debug values: key1 key2 key3 all help*");
283 c = g_get_codeset ();
286 g_assert_cmpstr (c, ==, c2);
294 const gchar *path = "/path/to/a/file/deep/down.sh";
297 b = g_basename (path);
299 g_assert_cmpstr (b, ==, "down.sh");
308 /* for tmpdir test, need to do this early before g_get_any_init */
309 g_setenv ("TMPDIR", "", TRUE);
313 g_test_init (&argc, &argv, NULL);
314 g_test_bug_base ("http://bugzilla.gnome.org/");
316 g_test_add_func ("/utils/language-names", test_language_names);
317 g_test_add_func ("/utils/locale-variants", test_locale_variants);
318 g_test_add_func ("/utils/version", test_version);
319 g_test_add_func ("/utils/appname", test_appname);
320 g_test_add_func ("/utils/tmpdir", test_tmpdir);
321 g_test_add_func ("/utils/bits", test_bits);
322 g_test_add_func ("/utils/find-program", test_find_program);
323 g_test_add_func ("/utils/debug", test_debug);
324 g_test_add_func ("/utils/codeset", test_codeset);
325 g_test_add_func ("/utils/basename", test_basename);