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 <stdarg.h>
27
28 static gboolean
29 strv_check (const gchar * const *strv, ...)
30 {
31   va_list args;
32   gchar *s;
33   gint i;
34
35   va_start (args, strv);
36   for (i = 0; strv[i]; i++)
37     {
38       s = va_arg (args, gchar*);
39       if (g_strcmp0 (strv[i], s) != 0)
40         {
41           va_end (args);
42           return FALSE;
43         }
44     }
45
46   va_end (args);
47
48   return TRUE;
49 }
50
51 static void
52 test_language_names (void)
53 {
54   const gchar * const *names;
55
56   g_setenv ("LANGUAGE", "de:en_US", TRUE);
57   names = g_get_language_names ();
58   g_assert (strv_check (names, "de", "en_US", "en", "C", NULL));
59
60   g_setenv ("LANGUAGE", "tt_RU.UTF-8@iqtelif", TRUE);
61   names = g_get_language_names ();
62   g_assert (strv_check (names,
63                         "tt_RU.UTF-8@iqtelif",
64                         "tt_RU@iqtelif",
65                         "tt.UTF-8@iqtelif",
66                         "tt@iqtelif",
67                         "tt_RU.UTF-8",
68                         "tt_RU",
69                         "tt.UTF-8",
70                         "tt",
71                         "C",
72                         NULL));
73 }
74
75 static void
76 test_locale_variants (void)
77 {
78   char **v;
79
80   v = g_get_locale_variants ("fr_BE");
81   g_assert (strv_check ((const gchar * const *) v, "fr_BE", "fr", NULL));
82   g_strfreev (v);
83
84   v = g_get_locale_variants ("sr_SR@latin");
85   g_assert (strv_check ((const gchar * const *) v, "sr_SR@latin", "sr@latin", "sr_SR", "sr", NULL));
86   g_strfreev (v);
87 }
88
89 static void
90 test_version (void)
91 {
92   g_print ("(header %d.%d.%d library %d.%d.%d) ",
93                   GLIB_MAJOR_VERSION, GLIB_MINOR_VERSION, GLIB_MICRO_VERSION,
94                   glib_major_version, glib_minor_version, glib_micro_version);
95
96   g_assert (glib_check_version (GLIB_MAJOR_VERSION,
97                                 GLIB_MINOR_VERSION,
98                                 GLIB_MICRO_VERSION) == NULL);
99   g_assert (glib_check_version (GLIB_MAJOR_VERSION,
100                                 GLIB_MINOR_VERSION,
101                                 0) == NULL);
102   g_assert (glib_check_version (GLIB_MAJOR_VERSION - 1,
103                                 0,
104                                 0) != NULL);
105   g_assert (glib_check_version (GLIB_MAJOR_VERSION + 1,
106                                 0,
107                                 0) != NULL);
108   g_assert (glib_check_version (GLIB_MAJOR_VERSION,
109                                 GLIB_MINOR_VERSION + 1,
110                                 0) != NULL);
111   /* don't use + 1 here, since a +/-1 difference can
112    * happen due to post-release version bumps in git
113    */
114   g_assert (glib_check_version (GLIB_MAJOR_VERSION,
115                                 GLIB_MINOR_VERSION,
116                                 GLIB_MICRO_VERSION + 3) != NULL);
117 }
118
119 static const gchar *argv0;
120
121 static void
122 test_appname (void)
123 {
124   const gchar *prgname;
125   const gchar *appname;
126
127   prgname = g_get_prgname ();
128   appname = g_get_application_name ();
129   g_assert_cmpstr (prgname, ==, argv0);
130   g_assert_cmpstr (appname, ==, prgname);
131
132   g_set_prgname ("prgname");
133
134   prgname = g_get_prgname ();
135   appname = g_get_application_name ();
136   g_assert_cmpstr (prgname, ==, "prgname");
137   g_assert_cmpstr (appname, ==, "prgname");
138
139   g_set_application_name ("appname");
140
141   prgname = g_get_prgname ();
142   appname = g_get_application_name ();
143   g_assert_cmpstr (prgname, ==, "prgname");
144   g_assert_cmpstr (appname, ==, "appname");
145 }
146
147 static void
148 test_tmpdir (void)
149 {
150   g_test_bug ("627969");
151   g_assert_cmpstr (g_get_tmp_dir (), !=, "");
152 }
153
154 static void
155 test_bits (void)
156 {
157   gulong mask;
158   gint max_bit;
159   gint i, pos;
160
161   pos = g_bit_nth_lsf (0, -1);
162   g_assert_cmpint (pos, ==, -1);
163
164   max_bit = sizeof (gulong) * 8;
165   for (i = 0; i < max_bit; i++)
166     {
167       mask = 1UL << i;
168
169       pos = g_bit_nth_lsf (mask, -1);
170       g_assert_cmpint (pos, ==, i);
171
172       pos = g_bit_nth_lsf (mask, i - 3);
173       g_assert_cmpint (pos , ==, i);
174
175       pos = g_bit_nth_lsf (mask, i);
176       g_assert_cmpint (pos , ==, -1);
177
178       pos = g_bit_nth_lsf (mask, i + 1);
179       g_assert_cmpint (pos , ==, -1);
180     }
181
182   pos = g_bit_nth_msf (0, -1);
183   g_assert_cmpint (pos, ==, -1);
184
185   for (i = 0; i < max_bit; i++)
186     {
187       mask = 1UL << i;
188
189       pos = g_bit_nth_msf (mask, -1);
190       g_assert_cmpint (pos, ==, i);
191
192       pos = g_bit_nth_msf (mask, i + 3);
193       g_assert_cmpint (pos , ==, i);
194
195       pos = g_bit_nth_msf (mask, i);
196       g_assert_cmpint (pos , ==, -1);
197
198       if (i > 0)
199         {
200           pos = g_bit_nth_msf (mask, i - 1);
201           g_assert_cmpint (pos , ==, -1);
202         }
203     }
204 }
205
206 int
207 main (int   argc,
208       char *argv[])
209 {
210   argv0 = argv[0];
211
212   /* for tmpdir test, need to do this early before g_get_any_init */
213   g_setenv ("TMPDIR", "", TRUE);
214   g_unsetenv ("TMP");
215   g_unsetenv ("TEMP");
216
217   g_test_init (&argc, &argv, NULL);
218   g_test_bug_base ("http://bugzilla.gnome.org/");
219
220   g_test_add_func ("/utils/language-names", test_language_names);
221   g_test_add_func ("/utils/locale-variants", test_locale_variants);
222   g_test_add_func ("/utils/version", test_version);
223   g_test_add_func ("/utils/appname", test_appname);
224   g_test_add_func ("/utils/tmpdir", test_tmpdir);
225   g_test_add_func ("/utils/bits", test_bits);
226
227   return g_test_run();
228 }