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