Fix glib/tests/utils for g_parse_debug_string() changes
[platform/upstream/glib.git] / glib / tests / utils.c
1 /* Unit tests for utilities
2  * Copyright (C) 2010 Red Hat, Inc.
3  *
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.
7  *
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.
11  *
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.
20  *
21  * Author: Matthias Clasen
22  */
23
24 #define GLIB_DISABLE_DEPRECATION_WARNINGS
25
26 #include "glib.h"
27
28 #include <stdlib.h>
29 #include <stdarg.h>
30
31 static gboolean
32 strv_check (const gchar * const *strv, ...)
33 {
34   va_list args;
35   gchar *s;
36   gint i;
37
38   va_start (args, strv);
39   for (i = 0; strv[i]; i++)
40     {
41       s = va_arg (args, gchar*);
42       if (g_strcmp0 (strv[i], s) != 0)
43         {
44           va_end (args);
45           return FALSE;
46         }
47     }
48
49   va_end (args);
50
51   return TRUE;
52 }
53
54 static void
55 test_language_names (void)
56 {
57   const gchar * const *names;
58
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));
62
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",
67                         "tt_RU@iqtelif",
68                         "tt.UTF-8@iqtelif",
69                         "tt@iqtelif",
70                         "tt_RU.UTF-8",
71                         "tt_RU",
72                         "tt.UTF-8",
73                         "tt",
74                         "C",
75                         NULL));
76 }
77
78 static void
79 test_locale_variants (void)
80 {
81   char **v;
82
83   v = g_get_locale_variants ("fr_BE");
84   g_assert (strv_check ((const gchar * const *) v, "fr_BE", "fr", NULL));
85   g_strfreev (v);
86
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));
89   g_strfreev (v);
90 }
91
92 static void
93 test_version (void)
94 {
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);
99
100   g_assert (glib_check_version (GLIB_MAJOR_VERSION,
101                                 GLIB_MINOR_VERSION,
102                                 GLIB_MICRO_VERSION) == NULL);
103   g_assert (glib_check_version (GLIB_MAJOR_VERSION,
104                                 GLIB_MINOR_VERSION,
105                                 0) == NULL);
106   g_assert (glib_check_version (GLIB_MAJOR_VERSION - 1,
107                                 0,
108                                 0) != NULL);
109   g_assert (glib_check_version (GLIB_MAJOR_VERSION + 1,
110                                 0,
111                                 0) != NULL);
112   g_assert (glib_check_version (GLIB_MAJOR_VERSION,
113                                 GLIB_MINOR_VERSION + 1,
114                                 0) != NULL);
115   /* don't use + 1 here, since a +/-1 difference can
116    * happen due to post-release version bumps in git
117    */
118   g_assert (glib_check_version (GLIB_MAJOR_VERSION,
119                                 GLIB_MINOR_VERSION,
120                                 GLIB_MICRO_VERSION + 3) != NULL);
121 }
122
123 static const gchar *argv0;
124
125 static void
126 test_appname (void)
127 {
128   const gchar *prgname;
129   const gchar *appname;
130
131   prgname = g_get_prgname ();
132   appname = g_get_application_name ();
133   g_assert_cmpstr (prgname, ==, argv0);
134   g_assert_cmpstr (appname, ==, prgname);
135
136   g_set_prgname ("prgname");
137
138   prgname = g_get_prgname ();
139   appname = g_get_application_name ();
140   g_assert_cmpstr (prgname, ==, "prgname");
141   g_assert_cmpstr (appname, ==, "prgname");
142
143   g_set_application_name ("appname");
144
145   prgname = g_get_prgname ();
146   appname = g_get_application_name ();
147   g_assert_cmpstr (prgname, ==, "prgname");
148   g_assert_cmpstr (appname, ==, "appname");
149 }
150
151 static void
152 test_tmpdir (void)
153 {
154   g_test_bug ("627969");
155   g_assert_cmpstr (g_get_tmp_dir (), !=, "");
156 }
157
158 static void
159 test_bits (void)
160 {
161   gulong mask;
162   gint max_bit;
163   gint i, pos;
164
165   pos = g_bit_nth_lsf (0, -1);
166   g_assert_cmpint (pos, ==, -1);
167
168   max_bit = sizeof (gulong) * 8;
169   for (i = 0; i < max_bit; i++)
170     {
171       mask = 1UL << i;
172
173       pos = g_bit_nth_lsf (mask, -1);
174       g_assert_cmpint (pos, ==, i);
175
176       pos = g_bit_nth_lsf (mask, i - 3);
177       g_assert_cmpint (pos , ==, i);
178
179       pos = g_bit_nth_lsf (mask, i);
180       g_assert_cmpint (pos , ==, -1);
181
182       pos = g_bit_nth_lsf (mask, i + 1);
183       g_assert_cmpint (pos , ==, -1);
184     }
185
186   pos = g_bit_nth_msf (0, -1);
187   g_assert_cmpint (pos, ==, -1);
188
189   for (i = 0; i < max_bit; i++)
190     {
191       mask = 1UL << i;
192
193       pos = g_bit_nth_msf (mask, -1);
194       g_assert_cmpint (pos, ==, i);
195
196       pos = g_bit_nth_msf (mask, i + 3);
197       g_assert_cmpint (pos , ==, i);
198
199       pos = g_bit_nth_msf (mask, i);
200       g_assert_cmpint (pos , ==, -1);
201
202       if (i > 0)
203         {
204           pos = g_bit_nth_msf (mask, i - 1);
205           g_assert_cmpint (pos , ==, -1);
206         }
207     }
208 }
209
210 static void
211 test_find_program (void)
212 {
213   gchar *res;
214
215   res = g_find_program_in_path ("sh");
216   g_assert_cmpstr (res, ==, "/bin/sh");
217   g_free (res);
218
219   res = g_find_program_in_path ("/bin/sh");
220   g_assert_cmpstr (res, ==, "/bin/sh");
221   g_free (res);
222
223   res = g_find_program_in_path ("this_program_does_not_exit");
224   g_assert (res == NULL);
225
226   res = g_find_program_in_path ("/bin");
227   g_assert (res == NULL);
228
229   res = g_find_program_in_path ("/etc/passwd");
230   g_assert (res == NULL);
231 }
232
233 static void
234 test_debug (void)
235 {
236   GDebugKey keys[] = {
237     { "key1", 1 },
238     { "key2", 2 },
239     { "key3", 4 },
240   };
241   guint res;
242
243   res = g_parse_debug_string (NULL, keys, G_N_ELEMENTS (keys));
244   g_assert_cmpint (res, ==, 0);
245
246   res = g_parse_debug_string ("foobabla;#!%!$%112 223", keys, G_N_ELEMENTS (keys));
247   g_assert_cmpint (res, ==, 0);
248
249   res = g_parse_debug_string ("key1:key2", keys, G_N_ELEMENTS (keys));
250   g_assert_cmpint (res, ==, 3);
251
252   res = g_parse_debug_string ("key1;key2", keys, G_N_ELEMENTS (keys));
253   g_assert_cmpint (res, ==, 3);
254
255   res = g_parse_debug_string ("key1,key2", keys, G_N_ELEMENTS (keys));
256   g_assert_cmpint (res, ==, 3);
257
258   res = g_parse_debug_string ("key1   key2", keys, G_N_ELEMENTS (keys));
259   g_assert_cmpint (res, ==, 3);
260
261   res = g_parse_debug_string ("key1\tkey2", keys, G_N_ELEMENTS (keys));
262   g_assert_cmpint (res, ==, 3);
263
264   res = g_parse_debug_string ("all", keys, G_N_ELEMENTS (keys));
265   g_assert_cmpint (res, ==, 7);
266
267   if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
268     {
269       res = g_parse_debug_string ("help", keys, G_N_ELEMENTS (keys));
270       g_assert_cmpint (res, ==, 0);
271       exit (0);
272     }
273   g_test_trap_assert_passed ();
274   g_test_trap_assert_stderr ("*Supported debug values: key1 key2 key3 all help*");
275 }
276
277 static void
278 test_codeset (void)
279 {
280   gchar *c;
281   const gchar *c2;
282
283   c = g_get_codeset ();
284   g_get_charset (&c2);
285
286   g_assert_cmpstr (c, ==, c2);
287
288   g_free (c);
289 }
290
291 static void
292 test_basename (void)
293 {
294   const gchar *path = "/path/to/a/file/deep/down.sh";
295   const gchar *b;
296
297   b = g_basename (path);
298
299   g_assert_cmpstr (b, ==, "down.sh");
300 }
301
302 int
303 main (int   argc,
304       char *argv[])
305 {
306   argv0 = argv[0];
307
308   /* for tmpdir test, need to do this early before g_get_any_init */
309   g_setenv ("TMPDIR", "", TRUE);
310   g_unsetenv ("TMP");
311   g_unsetenv ("TEMP");
312
313   g_test_init (&argc, &argv, NULL);
314   g_test_bug_base ("http://bugzilla.gnome.org/");
315
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);
326
327   return g_test_run();
328 }