Merge remote 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_version (void)
77 {
78   g_assert (glib_check_version (GLIB_MAJOR_VERSION,
79                                 GLIB_MINOR_VERSION,
80                                 GLIB_MICRO_VERSION) == NULL);
81   g_assert (glib_check_version (GLIB_MAJOR_VERSION,
82                                 GLIB_MINOR_VERSION,
83                                 0) == NULL);
84   g_assert (glib_check_version (GLIB_MAJOR_VERSION - 1,
85                                 0,
86                                 0) != NULL);
87   g_assert (glib_check_version (GLIB_MAJOR_VERSION + 1,
88                                 0,
89                                 0) != NULL);
90   g_assert (glib_check_version (GLIB_MAJOR_VERSION,
91                                 GLIB_MINOR_VERSION + 1,
92                                 0) != NULL);
93   g_assert (glib_check_version (GLIB_MAJOR_VERSION,
94                                 GLIB_MINOR_VERSION,
95                                 GLIB_MICRO_VERSION + 1) != NULL);
96 }
97
98 static const gchar *argv0;
99
100 static void
101 test_appname (void)
102 {
103   const gchar *prgname;
104   const gchar *appname;
105
106   prgname = g_get_prgname ();
107   appname = g_get_application_name ();
108   g_assert_cmpstr (prgname, ==, argv0);
109   g_assert_cmpstr (appname, ==, prgname);
110
111   g_set_prgname ("prgname");
112
113   prgname = g_get_prgname ();
114   appname = g_get_application_name ();
115   g_assert_cmpstr (prgname, ==, "prgname");
116   g_assert_cmpstr (appname, ==, "prgname");
117
118   g_set_application_name ("appname");
119
120   prgname = g_get_prgname ();
121   appname = g_get_application_name ();
122   g_assert_cmpstr (prgname, ==, "prgname");
123   g_assert_cmpstr (appname, ==, "appname");
124 }
125
126 static void
127 test_tmpdir (void)
128 {
129   g_test_bug ("627969");
130   g_assert_cmpstr (g_get_tmp_dir (), !=, "");
131 }
132
133 int
134 main (int   argc,
135       char *argv[])
136 {
137   argv0 = argv[0];
138
139   /* for tmpdir test, need to do this early before g_get_any_init */
140   g_setenv ("TMPDIR", "", TRUE);
141   g_unsetenv ("TMP");
142   g_unsetenv ("TEMP");
143
144   g_test_init (&argc, &argv, NULL);
145   g_test_bug_base ("http://bugzilla.gnome.org/");
146
147   g_test_add_func ("/utils/language-names", test_language_names);
148   g_test_add_func ("/utils/version", test_version);
149   g_test_add_func ("/utils/appname", test_appname);
150   g_test_add_func ("/utils/tmpdir", test_tmpdir);
151
152   return g_test_run();
153 }