glib/tests/fileutils.c: Include unistd.h on *NIX only
[platform/upstream/glib.git] / glib / tests / fileutils.c
1 /* Unit tests for gfileutils
2  * Copyright (C) 1995-1997  Peter Mattis, Spencer Kimball and Josh MacDonald
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
22 #include <string.h>
23 #include <errno.h>
24
25 /* We are testing some deprecated APIs here */
26 #define GLIB_DISABLE_DEPRECATION_WARNINGS
27
28 #include <glib.h>
29 #include <glib/gstdio.h>
30
31 #ifdef G_OS_UNIX
32 #include <unistd.h>
33 #endif
34 #ifdef G_OS_WIN32
35 #include <windows.h>
36 #endif
37
38 #define S G_DIR_SEPARATOR_S
39
40 static void
41 check_string (gchar *str, gchar *expected)
42 {
43   g_assert (str != NULL);
44   g_assert_cmpstr (str, ==, expected);
45   g_free (str);
46 }
47
48 static void
49 test_build_path (void)
50 {
51 /*  check_string (g_build_path ("", NULL), "");*/
52   check_string (g_build_path ("", "", NULL), "");
53   check_string (g_build_path ("", "x", NULL), "x");
54   check_string (g_build_path ("", "x", "y",  NULL), "xy");
55   check_string (g_build_path ("", "x", "y", "z", NULL), "xyz");
56
57 /*  check_string (g_build_path (":", NULL), "");*/
58   check_string (g_build_path (":", ":", NULL), ":");
59   check_string (g_build_path (":", ":x", NULL), ":x");
60   check_string (g_build_path (":", "x:", NULL), "x:");
61   check_string (g_build_path (":", "", "x", NULL), "x");
62   check_string (g_build_path (":", "", ":x", NULL), ":x");
63   check_string (g_build_path (":", ":", "x", NULL), ":x");
64   check_string (g_build_path (":", "::", "x", NULL), "::x");
65   check_string (g_build_path (":", "x", "", NULL), "x");
66   check_string (g_build_path (":", "x:", "", NULL), "x:");
67   check_string (g_build_path (":", "x", ":", NULL), "x:");
68   check_string (g_build_path (":", "x", "::", NULL), "x::");
69   check_string (g_build_path (":", "x", "y",  NULL), "x:y");
70   check_string (g_build_path (":", ":x", "y", NULL), ":x:y");
71   check_string (g_build_path (":", "x", "y:", NULL), "x:y:");
72   check_string (g_build_path (":", ":x:", ":y:", NULL), ":x:y:");
73   check_string (g_build_path (":", ":x::", "::y:", NULL), ":x:y:");
74   check_string (g_build_path (":", "x", "","y",  NULL), "x:y");
75   check_string (g_build_path (":", "x", ":", "y",  NULL), "x:y");
76   check_string (g_build_path (":", "x", "::", "y",  NULL), "x:y");
77   check_string (g_build_path (":", "x", "y", "z", NULL), "x:y:z");
78   check_string (g_build_path (":", ":x:", ":y:", ":z:", NULL), ":x:y:z:");
79   check_string (g_build_path (":", "::x::", "::y::", "::z::", NULL), "::x:y:z::");
80
81 /*  check_string (g_build_path ("::", NULL), "");*/
82   check_string (g_build_path ("::", "::", NULL), "::");
83   check_string (g_build_path ("::", ":::", NULL), ":::");
84   check_string (g_build_path ("::", "::x", NULL), "::x");
85   check_string (g_build_path ("::", "x::", NULL), "x::");
86   check_string (g_build_path ("::", "", "x", NULL), "x");
87   check_string (g_build_path ("::", "", "::x", NULL), "::x");
88   check_string (g_build_path ("::", "::", "x", NULL), "::x");
89   check_string (g_build_path ("::", "::::", "x", NULL), "::::x");
90   check_string (g_build_path ("::", "x", "", NULL), "x");
91   check_string (g_build_path ("::", "x::", "", NULL), "x::");
92   check_string (g_build_path ("::", "x", "::", NULL), "x::");
93
94   /* This following is weird, but keeps the definition simple */
95   check_string (g_build_path ("::", "x", ":::", NULL), "x:::::");
96   check_string (g_build_path ("::", "x", "::::", NULL), "x::::");
97   check_string (g_build_path ("::", "x", "y",  NULL), "x::y");
98   check_string (g_build_path ("::", "::x", "y", NULL), "::x::y");
99   check_string (g_build_path ("::", "x", "y::", NULL), "x::y::");
100   check_string (g_build_path ("::", "::x::", "::y::", NULL), "::x::y::");
101   check_string (g_build_path ("::", "::x:::", ":::y::", NULL), "::x::::y::");
102   check_string (g_build_path ("::", "::x::::", "::::y::", NULL), "::x::y::");
103   check_string (g_build_path ("::", "x", "", "y",  NULL), "x::y");
104   check_string (g_build_path ("::", "x", "::", "y",  NULL), "x::y");
105   check_string (g_build_path ("::", "x", "::::", "y",  NULL), "x::y");
106   check_string (g_build_path ("::", "x", "y", "z", NULL), "x::y::z");
107   check_string (g_build_path ("::", "::x::", "::y::", "::z::", NULL), "::x::y::z::");
108   check_string (g_build_path ("::", ":::x:::", ":::y:::", ":::z:::", NULL), ":::x::::y::::z:::");
109   check_string (g_build_path ("::", "::::x::::", "::::y::::", "::::z::::", NULL), "::::x::y::z::::");
110 }
111
112 static void
113 test_build_pathv (void)
114 {
115   gchar *args[10];
116
117   g_assert (g_build_pathv ("", NULL) == NULL);
118   args[0] = NULL;
119   check_string (g_build_pathv ("", args), "");
120   args[0] = ""; args[1] = NULL;
121   check_string (g_build_pathv ("", args), "");
122   args[0] = "x"; args[1] = NULL;
123   check_string (g_build_pathv ("", args), "x");
124   args[0] = "x"; args[1] = "y"; args[2] = NULL;
125   check_string (g_build_pathv ("", args), "xy");
126   args[0] = "x"; args[1] = "y"; args[2] = "z", args[3] = NULL;
127   check_string (g_build_pathv ("", args), "xyz");
128
129   args[0] = NULL;
130   check_string (g_build_pathv (":", args), "");
131   args[0] = ":"; args[1] = NULL;
132   check_string (g_build_pathv (":", args), ":");
133   args[0] = ":x"; args[1] = NULL;
134   check_string (g_build_pathv (":", args), ":x");
135   args[0] = "x:"; args[1] = NULL;
136   check_string (g_build_pathv (":", args), "x:");
137   args[0] = ""; args[1] = "x"; args[2] = NULL;
138   check_string (g_build_pathv (":", args), "x");
139   args[0] = ""; args[1] = ":x"; args[2] = NULL;
140   check_string (g_build_pathv (":", args), ":x");
141   args[0] = ":"; args[1] = "x"; args[2] = NULL;
142   check_string (g_build_pathv (":", args), ":x");
143   args[0] = "::"; args[1] = "x"; args[2] = NULL;
144   check_string (g_build_pathv (":", args), "::x");
145   args[0] = "x"; args[1] = ""; args[2] = NULL;
146   check_string (g_build_pathv (":", args), "x");
147   args[0] = "x:"; args[1] = ""; args[2] = NULL;
148   check_string (g_build_pathv (":", args), "x:");
149   args[0] = "x"; args[1] = ":"; args[2] = NULL;
150   check_string (g_build_pathv (":", args), "x:");
151   args[0] = "x"; args[1] = "::"; args[2] = NULL;
152   check_string (g_build_pathv (":", args), "x::");
153   args[0] = "x"; args[1] = "y"; args[2] = NULL;
154   check_string (g_build_pathv (":", args), "x:y");
155   args[0] = ":x"; args[1] = "y"; args[2] = NULL;
156   check_string (g_build_pathv (":", args), ":x:y");
157   args[0] = "x"; args[1] = "y:"; args[2] = NULL;
158   check_string (g_build_pathv (":", args), "x:y:");
159   args[0] = ":x:"; args[1] = ":y:"; args[2] = NULL;
160   check_string (g_build_pathv (":", args), ":x:y:");
161   args[0] = ":x::"; args[1] = "::y:"; args[2] = NULL;
162   check_string (g_build_pathv (":", args), ":x:y:");
163   args[0] = "x"; args[1] = ""; args[2] = "y"; args[3] = NULL;
164   check_string (g_build_pathv (":", args), "x:y");
165   args[0] = "x"; args[1] = ":"; args[2] = "y"; args[3] = NULL;
166   check_string (g_build_pathv (":", args), "x:y");
167   args[0] = "x"; args[1] = "::"; args[2] = "y"; args[3] = NULL;
168   check_string (g_build_pathv (":", args), "x:y");
169   args[0] = "x"; args[1] = "y"; args[2] = "z"; args[3] = NULL;
170   check_string (g_build_pathv (":", args), "x:y:z");
171   args[0] = ":x:"; args[1] = ":y:"; args[2] = ":z:"; args[3] = NULL;
172   check_string (g_build_pathv (":", args), ":x:y:z:");
173   args[0] = "::x::"; args[1] = "::y::"; args[2] = "::z::"; args[3] = NULL;
174   check_string (g_build_pathv (":", args), "::x:y:z::");
175
176   args[0] = NULL;
177   check_string (g_build_pathv ("::", args), "");
178   args[0] = "::"; args[1] = NULL;
179   check_string (g_build_pathv ("::", args), "::");
180   args[0] = ":::"; args[1] = NULL;
181   check_string (g_build_pathv ("::", args), ":::");
182   args[0] = "::x"; args[1] = NULL;
183   check_string (g_build_pathv ("::", args), "::x");
184   args[0] = "x::"; args[1] = NULL;
185   check_string (g_build_pathv ("::", args), "x::");
186   args[0] = ""; args[1] = "x"; args[2] = NULL;
187   check_string (g_build_pathv ("::", args), "x");
188   args[0] = ""; args[1] = "::x"; args[2] = NULL;
189   check_string (g_build_pathv ("::", args), "::x");
190   args[0] = "::"; args[1] = "x"; args[2] = NULL;
191   check_string (g_build_pathv ("::", args), "::x");
192   args[0] = "::::"; args[1] = "x"; args[2] = NULL;
193   check_string (g_build_pathv ("::", args), "::::x");
194   args[0] = "x"; args[1] = ""; args[2] = NULL;
195   check_string (g_build_pathv ("::", args), "x");
196   args[0] = "x::"; args[1] = ""; args[2] = NULL;
197   check_string (g_build_pathv ("::", args), "x::");
198   args[0] = "x"; args[1] = "::"; args[2] = NULL;
199   check_string (g_build_pathv ("::", args), "x::");
200   /* This following is weird, but keeps the definition simple */
201   args[0] = "x"; args[1] = ":::"; args[2] = NULL;
202   check_string (g_build_pathv ("::", args), "x:::::");
203   args[0] = "x"; args[1] = "::::"; args[2] = NULL;
204   check_string (g_build_pathv ("::", args), "x::::");
205   args[0] = "x"; args[1] = "y"; args[2] = NULL;
206   check_string (g_build_pathv ("::", args), "x::y");
207   args[0] = "::x"; args[1] = "y"; args[2] = NULL;
208   check_string (g_build_pathv ("::", args), "::x::y");
209   args[0] = "x"; args[1] = "y::"; args[2] = NULL;
210   check_string (g_build_pathv ("::", args), "x::y::");
211   args[0] = "::x::"; args[1] = "::y::"; args[2] = NULL;
212   check_string (g_build_pathv ("::", args), "::x::y::");
213   args[0] = "::x:::"; args[1] = ":::y::"; args[2] = NULL;
214   check_string (g_build_pathv ("::", args), "::x::::y::");
215   args[0] = "::x::::"; args[1] = "::::y::"; args[2] = NULL;
216   check_string (g_build_pathv ("::", args), "::x::y::");
217   args[0] = "x"; args[1] = ""; args[2] = "y"; args[3] = NULL;
218   check_string (g_build_pathv ("::", args), "x::y");
219   args[0] = "x"; args[1] = "::"; args[2] = "y"; args[3] = NULL;
220   check_string (g_build_pathv ("::", args), "x::y");
221   args[0] = "x"; args[1] = "::::"; args[2] = "y"; args[3] = NULL;
222   check_string (g_build_pathv ("::", args), "x::y");
223   args[0] = "x"; args[1] = "y"; args[2] = "z"; args[3] = NULL;
224   check_string (g_build_pathv ("::", args), "x::y::z");
225   args[0] = "::x::"; args[1] = "::y::"; args[2] = "::z::"; args[3] = NULL;
226   check_string (g_build_pathv ("::", args), "::x::y::z::");
227   args[0] = ":::x:::"; args[1] = ":::y:::"; args[2] = ":::z:::"; args[3] = NULL;
228   check_string (g_build_pathv ("::", args), ":::x::::y::::z:::");
229   args[0] = "::::x::::"; args[1] = "::::y::::"; args[2] = "::::z::::"; args[3] = NULL;
230   check_string (g_build_pathv ("::", args), "::::x::y::z::::");
231 }
232
233 static void
234 test_build_filename (void)
235 {
236 /*  check_string (g_build_filename (NULL), "");*/
237   check_string (g_build_filename (S, NULL), S);
238   check_string (g_build_filename (S"x", NULL), S"x");
239   check_string (g_build_filename ("x"S, NULL), "x"S);
240   check_string (g_build_filename ("", "x", NULL), "x");
241   check_string (g_build_filename ("", S"x", NULL), S"x");
242   check_string (g_build_filename (S, "x", NULL), S"x");
243   check_string (g_build_filename (S S, "x", NULL), S S"x");
244   check_string (g_build_filename ("x", "", NULL), "x");
245   check_string (g_build_filename ("x"S, "", NULL), "x"S);
246   check_string (g_build_filename ("x", S, NULL), "x"S);
247   check_string (g_build_filename ("x", S S, NULL), "x"S S);
248   check_string (g_build_filename ("x", "y",  NULL), "x"S"y");
249   check_string (g_build_filename (S"x", "y", NULL), S"x"S"y");
250   check_string (g_build_filename ("x", "y"S, NULL), "x"S"y"S);
251   check_string (g_build_filename (S"x"S, S"y"S, NULL), S"x"S"y"S);
252   check_string (g_build_filename (S"x"S S, S S"y"S, NULL), S"x"S"y"S);
253   check_string (g_build_filename ("x", "", "y",  NULL), "x"S"y");
254   check_string (g_build_filename ("x", S, "y",  NULL), "x"S"y");
255   check_string (g_build_filename ("x", S S, "y",  NULL), "x"S"y");
256   check_string (g_build_filename ("x", "y", "z", NULL), "x"S"y"S"z");
257   check_string (g_build_filename (S"x"S, S"y"S, S"z"S, NULL), S"x"S"y"S"z"S);
258   check_string (g_build_filename (S S"x"S S, S S"y"S S, S S"z"S S, NULL), S S"x"S"y"S"z"S S);
259
260 #ifdef G_OS_WIN32
261
262   /* Test also using the slash as file name separator */
263 #define U "/"
264   /* check_string (g_build_filename (NULL), ""); */
265   check_string (g_build_filename (U, NULL), U);
266   check_string (g_build_filename (U"x", NULL), U"x");
267   check_string (g_build_filename ("x"U, NULL), "x"U);
268   check_string (g_build_filename ("", U"x", NULL), U"x");
269   check_string (g_build_filename ("", U"x", NULL), U"x");
270   check_string (g_build_filename (U, "x", NULL), U"x");
271   check_string (g_build_filename (U U, "x", NULL), U U"x");
272   check_string (g_build_filename (U S, "x", NULL), U S"x");
273   check_string (g_build_filename ("x"U, "", NULL), "x"U);
274   check_string (g_build_filename ("x"S"y", "z"U"a", NULL), "x"S"y"S"z"U"a");
275   check_string (g_build_filename ("x", U, NULL), "x"U);
276   check_string (g_build_filename ("x", U U, NULL), "x"U U);
277   check_string (g_build_filename ("x", S U, NULL), "x"S U);
278   check_string (g_build_filename (U"x", "y", NULL), U"x"U"y");
279   check_string (g_build_filename ("x", "y"U, NULL), "x"U"y"U);
280   check_string (g_build_filename (U"x"U, U"y"U, NULL), U"x"U"y"U);
281   check_string (g_build_filename (U"x"U U, U U"y"U, NULL), U"x"U"y"U);
282   check_string (g_build_filename ("x", U, "y",  NULL), "x"U"y");
283   check_string (g_build_filename ("x", U U, "y",  NULL), "x"U"y");
284   check_string (g_build_filename ("x", U S, "y",  NULL), "x"S"y");
285   check_string (g_build_filename ("x", S U, "y",  NULL), "x"U"y");
286   check_string (g_build_filename ("x", U "y", "z", NULL), "x"U"y"U"z");
287   check_string (g_build_filename ("x", S "y", "z", NULL), "x"S"y"S"z");
288   check_string (g_build_filename ("x", S "y", "z", U, "a", "b", NULL), "x"S"y"S"z"U"a"U"b");
289   check_string (g_build_filename (U"x"U, U"y"U, U"z"U, NULL), U"x"U"y"U"z"U);
290   check_string (g_build_filename (U U"x"U U, U U"y"U U, U U"z"U U, NULL), U U"x"U"y"U"z"U U);
291
292 #undef U
293
294 #endif /* G_OS_WIN32 */
295
296 }
297
298 static void
299 test_build_filenamev (void)
300 {
301   gchar *args[10];
302
303   args[0] = NULL;
304   check_string (g_build_filenamev (args), "");
305   args[0] = S; args[1] = NULL;
306   check_string (g_build_filenamev (args), S);
307   args[0] = S"x"; args[1] = NULL;
308   check_string (g_build_filenamev (args), S"x");
309   args[0] = "x"S; args[1] = NULL;
310   check_string (g_build_filenamev (args), "x"S);
311   args[0] = ""; args[1] = "x"; args[2] = NULL;
312   check_string (g_build_filenamev (args), "x");
313   args[0] = ""; args[1] = S"x"; args[2] = NULL;
314   check_string (g_build_filenamev (args), S"x");
315   args[0] = S; args[1] = "x"; args[2] = NULL;
316   check_string (g_build_filenamev (args), S"x");
317   args[0] = S S; args[1] = "x"; args[2] = NULL;
318   check_string (g_build_filenamev (args), S S"x");
319   args[0] = "x"; args[1] = ""; args[2] = NULL;
320   check_string (g_build_filenamev (args), "x");
321   args[0] = "x"S; args[1] = ""; args[2] = NULL;
322   check_string (g_build_filenamev (args), "x"S);
323   args[0] = "x"; args[1] = S; args[2] = NULL;
324   check_string (g_build_filenamev (args), "x"S);
325   args[0] = "x"; args[1] = S S; args[2] = NULL;
326   check_string (g_build_filenamev (args), "x"S S);
327   args[0] = "x"; args[1] = "y"; args[2] = NULL;
328   check_string (g_build_filenamev (args), "x"S"y");
329   args[0] = S"x"; args[1] = "y"; args[2] = NULL;
330   check_string (g_build_filenamev (args), S"x"S"y");
331   args[0] = "x"; args[1] = "y"S; args[2] = NULL;
332   check_string (g_build_filenamev (args), "x"S"y"S);
333   args[0] = S"x"S; args[1] = S"y"S; args[2] = NULL;
334   check_string (g_build_filenamev (args), S"x"S"y"S);
335   args[0] = S"x"S S; args[1] = S S"y"S; args[2] = NULL;
336   check_string (g_build_filenamev (args), S"x"S"y"S);
337   args[0] = "x"; args[1] = ""; args[2] = "y"; args[3] = NULL;
338   check_string (g_build_filenamev (args), "x"S"y");
339   args[0] = "x"; args[1] = S; args[2] = "y"; args[3] = NULL;
340   check_string (g_build_filenamev (args), "x"S"y");
341   args[0] = "x"; args[1] = S S; args[2] = "y"; args[3] = NULL;
342   check_string (g_build_filenamev (args), "x"S"y");
343   args[0] = "x"; args[1] = "y"; args[2] = "z"; args[3] = NULL;
344   check_string (g_build_filenamev (args), "x"S"y"S"z");
345   args[0] = S"x"S; args[1] = S"y"S; args[2] = S"z"S; args[3] = NULL;
346   check_string (g_build_filenamev (args), S"x"S"y"S"z"S);
347   args[0] = S S"x"S S; args[1] = S S"y"S S; args[2] = S S"z"S S; args[3] = NULL;
348   check_string (g_build_filenamev (args), S S"x"S"y"S"z"S S);
349
350 #ifdef G_OS_WIN32
351
352   /* Test also using the slash as file name separator */
353 #define U "/"
354   args[0] = NULL;
355   check_string (g_build_filenamev (args), "");
356   args[0] = U; args[1] = NULL;
357   check_string (g_build_filenamev (args), U);
358   args[0] = U"x"; args[1] = NULL;
359   check_string (g_build_filenamev (args), U"x");
360   args[0] = "x"U; args[1] = NULL;
361   check_string (g_build_filenamev (args), "x"U);
362   args[0] = ""; args[1] = U"x"; args[2] = NULL;
363   check_string (g_build_filenamev (args), U"x");
364   args[0] = ""; args[1] = U"x"; args[2] = NULL;
365   check_string (g_build_filenamev (args), U"x");
366   args[0] = U; args[1] = "x"; args[2] = NULL;
367   check_string (g_build_filenamev (args), U"x");
368   args[0] = U U; args[1] = "x"; args[2] = NULL;
369   check_string (g_build_filenamev (args), U U"x");
370   args[0] = U S; args[1] = "x"; args[2] = NULL;
371   check_string (g_build_filenamev (args), U S"x");
372   args[0] = "x"U; args[1] = ""; args[2] = NULL;
373   check_string (g_build_filenamev (args), "x"U);
374   args[0] = "x"S"y"; args[1] = "z"U"a"; args[2] = NULL;
375   check_string (g_build_filenamev (args), "x"S"y"S"z"U"a");
376   args[0] = "x"; args[1] = U; args[2] = NULL;
377   check_string (g_build_filenamev (args), "x"U);
378   args[0] = "x"; args[1] = U U; args[2] = NULL;
379   check_string (g_build_filenamev (args), "x"U U);
380   args[0] = "x"; args[1] = S U; args[2] = NULL;
381   check_string (g_build_filenamev (args), "x"S U);
382   args[0] = U"x"; args[1] = "y"; args[2] = NULL;
383   check_string (g_build_filenamev (args), U"x"U"y");
384   args[0] = "x"; args[1] = "y"U; args[2] = NULL;
385   check_string (g_build_filenamev (args), "x"U"y"U);
386   args[0] = U"x"U; args[1] = U"y"U; args[2] = NULL;
387   check_string (g_build_filenamev (args), U"x"U"y"U);
388   args[0] = U"x"U U; args[1] = U U"y"U; args[2] = NULL;
389   check_string (g_build_filenamev (args), U"x"U"y"U);
390   args[0] = "x"; args[1] = U; args[2] = "y", args[3] = NULL;
391   check_string (g_build_filenamev (args), "x"U"y");
392   args[0] = "x"; args[1] = U U; args[2] = "y", args[3] = NULL;
393   check_string (g_build_filenamev (args), "x"U"y");
394   args[0] = "x"; args[1] = U S; args[2] = "y", args[3] = NULL;
395   check_string (g_build_filenamev (args), "x"S"y");
396   args[0] = "x"; args[1] = S U; args[2] = "y", args[3] = NULL;
397   check_string (g_build_filenamev (args), "x"U"y");
398   args[0] = "x"; args[1] = U "y"; args[2] = "z", args[3] = NULL;
399   check_string (g_build_filenamev (args), "x"U"y"U"z");
400   args[0] = "x"; args[1] = S "y"; args[2] = "z", args[3] = NULL;
401   check_string (g_build_filenamev (args), "x"S"y"S"z");
402   args[0] = "x"; args[1] = S "y"; args[2] = "z", args[3] = U;
403   args[4] = "a"; args[5] = "b"; args[6] = NULL;
404   check_string (g_build_filenamev (args), "x"S"y"S"z"U"a"U"b");
405   args[0] = U"x"U; args[1] = U"y"U; args[2] = U"z"U, args[3] = NULL;
406   check_string (g_build_filenamev (args), U"x"U"y"U"z"U);
407   args[0] = U U"x"U U; args[1] = U U"y"U U; args[2] = U U"z"U U, args[3] = NULL;
408   check_string (g_build_filenamev (args), U U"x"U"y"U"z"U U);
409
410 #undef U
411
412 #endif /* G_OS_WIN32 */
413 }
414
415 #undef S
416
417 static void
418 test_mkdir_with_parents_1 (const gchar *base)
419 {
420   char *p0 = g_build_filename (base, "fum", NULL);
421   char *p1 = g_build_filename (p0, "tem", NULL);
422   char *p2 = g_build_filename (p1, "zap", NULL);
423   FILE *f;
424
425   g_remove (p2);
426   g_remove (p1);
427   g_remove (p0);
428
429   if (g_file_test (p0, G_FILE_TEST_EXISTS))
430     g_error ("failed, %s exists, cannot test g_mkdir_with_parents\n", p0);
431
432   if (g_file_test (p1, G_FILE_TEST_EXISTS))
433     g_error ("failed, %s exists, cannot test g_mkdir_with_parents\n", p1);
434
435   if (g_file_test (p2, G_FILE_TEST_EXISTS))
436     g_error ("failed, %s exists, cannot test g_mkdir_with_parents\n", p2);
437
438   if (g_mkdir_with_parents (p2, 0777) == -1)
439     g_error ("failed, g_mkdir_with_parents(%s) failed: %s\n", p2, g_strerror (errno));
440
441   if (!g_file_test (p2, G_FILE_TEST_IS_DIR))
442     g_error ("failed, g_mkdir_with_parents(%s) succeeded, but %s is not a directory\n", p2, p2);
443
444   if (!g_file_test (p1, G_FILE_TEST_IS_DIR))
445     g_error ("failed, g_mkdir_with_parents(%s) succeeded, but %s is not a directory\n", p2, p1);
446
447   if (!g_file_test (p0, G_FILE_TEST_IS_DIR))
448     g_error ("failed, g_mkdir_with_parents(%s) succeeded, but %s is not a directory\n", p2, p0);
449
450   g_rmdir (p2);
451   if (g_file_test (p2, G_FILE_TEST_EXISTS))
452     g_error ("failed, did g_rmdir(%s), but %s is still there\n", p2, p2);
453
454   g_rmdir (p1);
455   if (g_file_test (p1, G_FILE_TEST_EXISTS))
456     g_error ("failed, did g_rmdir(%s), but %s is still there\n", p1, p1);
457
458   f = g_fopen (p1, "w");
459   if (f == NULL)
460     g_error ("failed, couldn't create file %s\n", p1);
461   fclose (f);
462
463   if (g_mkdir_with_parents (p1, 0666) == 0)
464     g_error ("failed, g_mkdir_with_parents(%s) succeeded, even if %s is a file\n", p1, p1);
465
466   if (g_mkdir_with_parents (p2, 0666) == 0)
467     g_error("failed, g_mkdir_with_parents(%s) succeeded, even if %s is a file\n", p2, p1);
468
469   g_remove (p2);
470   g_remove (p1);
471   g_remove (p0);
472
473   g_free (p2);
474   g_free (p1);
475   g_free (p0);
476 }
477
478 static void
479 test_mkdir_with_parents (void)
480 {
481   gchar *cwd;
482   if (g_test_verbose())
483     g_print ("checking g_mkdir_with_parents() in subdir ./hum/");
484   test_mkdir_with_parents_1 ("hum");
485   g_remove ("hum");
486   if (g_test_verbose())
487     g_print ("checking g_mkdir_with_parents() in subdir ./hii///haa/hee/");
488   test_mkdir_with_parents_1 ("hii///haa/hee");
489   g_remove ("hii/haa/hee");
490   g_remove ("hii/haa");
491   g_remove ("hii");
492   cwd = g_get_current_dir ();
493   if (g_test_verbose())
494     g_print ("checking g_mkdir_with_parents() in cwd: %s", cwd);
495   test_mkdir_with_parents_1 (cwd);
496   g_free (cwd);
497
498   g_assert (g_mkdir_with_parents (NULL, 0) == -1);
499   g_assert (errno == EINVAL);
500 }
501
502 static void
503 test_format_size_for_display (void)
504 {
505 #ifdef G_OS_WIN32
506   SetThreadLocale (MAKELCID (MAKELANGID (LANG_ENGLISH, SUBLANG_ENGLISH_US), SORT_DEFAULT));
507 #endif
508   /* nobody called setlocale(), so we should get "C" behaviour... */
509   check_string (g_format_size_for_display (0), "0 bytes");
510   check_string (g_format_size_for_display (1), "1 byte");
511   check_string (g_format_size_for_display (2), "2 bytes");
512   check_string (g_format_size_for_display (1024), "1.0 KB");
513   check_string (g_format_size_for_display (1024 * 1024), "1.0 MB");
514   check_string (g_format_size_for_display (1024 * 1024 * 1024), "1.0 GB");
515   check_string (g_format_size_for_display (1024ULL * 1024 * 1024 * 1024), "1.0 TB");
516   check_string (g_format_size_for_display (1024ULL * 1024 * 1024 * 1024 * 1024), "1.0 PB");
517   check_string (g_format_size_for_display (1024ULL * 1024 * 1024 * 1024 * 1024 * 1024), "1.0 EB");
518
519   check_string (g_format_size (0), "0 bytes");
520   check_string (g_format_size (1), "1 byte");
521   check_string (g_format_size (2), "2 bytes");
522   check_string (g_format_size (1000ULL), "1.0 kB");
523   check_string (g_format_size (1000ULL * 1000), "1.0 MB");
524   check_string (g_format_size (1000ULL * 1000 * 1000), "1.0 GB");
525   check_string (g_format_size (1000ULL * 1000 * 1000 * 1000), "1.0 TB");
526   check_string (g_format_size (1000ULL * 1000 * 1000 * 1000 * 1000), "1.0 PB");
527   check_string (g_format_size (1000ULL * 1000 * 1000 * 1000 * 1000 * 1000), "1.0 EB");
528
529   check_string (g_format_size_full (0, G_FORMAT_SIZE_IEC_UNITS), "0 bytes");
530   check_string (g_format_size_full (1, G_FORMAT_SIZE_IEC_UNITS), "1 byte");
531   check_string (g_format_size_full (2, G_FORMAT_SIZE_IEC_UNITS), "2 bytes");
532
533   check_string (g_format_size_full (2048ULL, G_FORMAT_SIZE_IEC_UNITS), "2.0 KiB");
534   check_string (g_format_size_full (2048ULL * 1024, G_FORMAT_SIZE_IEC_UNITS), "2.0 MiB");
535   check_string (g_format_size_full (2048ULL * 1024 * 1024, G_FORMAT_SIZE_IEC_UNITS), "2.0 GiB");
536   check_string (g_format_size_full (2048ULL * 1024 * 1024 * 1024, G_FORMAT_SIZE_IEC_UNITS), "2.0 TiB");
537   check_string (g_format_size_full (2048ULL * 1024 * 1024 * 1024 * 1024, G_FORMAT_SIZE_IEC_UNITS), "2.0 PiB");
538   check_string (g_format_size_full (2048ULL * 1024 * 1024 * 1024 * 1024 * 1024, G_FORMAT_SIZE_IEC_UNITS), "2.0 EiB");
539
540   check_string (g_format_size_full (238472938, G_FORMAT_SIZE_IEC_UNITS), "227.4 MiB");
541   check_string (g_format_size_full (238472938, G_FORMAT_SIZE_DEFAULT), "238.5 MB");
542   check_string (g_format_size_full (238472938, G_FORMAT_SIZE_LONG_FORMAT), "238.5 MB (238472938 bytes)");
543 }
544
545 static void
546 test_file_errors (void)
547 {
548 #ifdef EEXIST
549   g_assert (g_file_error_from_errno (EEXIST) == G_FILE_ERROR_EXIST);
550 #endif
551 #ifdef EISDIR
552   g_assert (g_file_error_from_errno (EISDIR) == G_FILE_ERROR_ISDIR);
553 #endif
554 #ifdef EACCES
555   g_assert (g_file_error_from_errno (EACCES) == G_FILE_ERROR_ACCES);
556 #endif
557 #ifdef ENAMETOOLONG
558   g_assert (g_file_error_from_errno (ENAMETOOLONG) == G_FILE_ERROR_NAMETOOLONG);
559 #endif
560 #ifdef ENOENT
561   g_assert (g_file_error_from_errno (ENOENT) == G_FILE_ERROR_NOENT);
562 #endif
563 #ifdef ENOTDIR
564   g_assert (g_file_error_from_errno (ENOTDIR) == G_FILE_ERROR_NOTDIR);
565 #endif
566 #ifdef ENXIO
567   g_assert (g_file_error_from_errno (ENXIO) == G_FILE_ERROR_NXIO);
568 #endif
569 #ifdef ENODEV
570   g_assert (g_file_error_from_errno (ENODEV) == G_FILE_ERROR_NODEV);
571 #endif
572 #ifdef EROFS
573   g_assert (g_file_error_from_errno (EROFS) == G_FILE_ERROR_ROFS);
574 #endif
575 #ifdef ETXTBSY
576   g_assert (g_file_error_from_errno (ETXTBSY) == G_FILE_ERROR_TXTBSY);
577 #endif
578 #ifdef EFAULT
579   g_assert (g_file_error_from_errno (EFAULT) == G_FILE_ERROR_FAULT);
580 #endif
581 #ifdef ELOOP
582   g_assert (g_file_error_from_errno (ELOOP) == G_FILE_ERROR_LOOP);
583 #endif
584 #ifdef ENOSPC
585   g_assert (g_file_error_from_errno (ENOSPC) == G_FILE_ERROR_NOSPC);
586 #endif
587 #ifdef ENOMEM
588   g_assert (g_file_error_from_errno (ENOMEM) == G_FILE_ERROR_NOMEM);
589 #endif
590 #ifdef EMFILE
591   g_assert (g_file_error_from_errno (EMFILE) == G_FILE_ERROR_MFILE);
592 #endif
593 #ifdef ENFILE
594   g_assert (g_file_error_from_errno (ENFILE) == G_FILE_ERROR_NFILE);
595 #endif
596 #ifdef EBADF
597   g_assert (g_file_error_from_errno (EBADF) == G_FILE_ERROR_BADF);
598 #endif
599 #ifdef EINVAL
600   g_assert (g_file_error_from_errno (EINVAL) == G_FILE_ERROR_INVAL);
601 #endif
602 #ifdef EPIPE
603   g_assert (g_file_error_from_errno (EPIPE) == G_FILE_ERROR_PIPE);
604 #endif
605 #ifdef EAGAIN
606   g_assert (g_file_error_from_errno (EAGAIN) == G_FILE_ERROR_AGAIN);
607 #endif
608 #ifdef EINTR
609   g_assert (g_file_error_from_errno (EINTR) == G_FILE_ERROR_INTR);
610 #endif
611 #ifdef EIO
612   g_assert (g_file_error_from_errno (EIO) == G_FILE_ERROR_IO);
613 #endif
614 #ifdef EPERM
615   g_assert (g_file_error_from_errno (EPERM) == G_FILE_ERROR_PERM);
616 #endif
617 #ifdef ENOSYS
618   g_assert (g_file_error_from_errno (ENOSYS) == G_FILE_ERROR_NOSYS);
619 #endif
620 }
621
622 static void
623 test_basename (void)
624 {
625   gchar *b;
626
627   b = g_path_get_basename ("");
628   g_assert_cmpstr (b, ==, ".");
629   g_free (b);
630
631   b = g_path_get_basename ("///");
632   g_assert_cmpstr (b, ==, G_DIR_SEPARATOR_S);
633   g_free (b);
634
635   b = g_path_get_basename ("/a/b/c/d");
636   g_assert_cmpstr (b, ==, "d");
637   g_free (b);
638 }
639
640 static void
641 test_dir_make_tmp (void)
642 {
643   gchar *name;
644   GError *error = NULL;
645
646   name = g_dir_make_tmp ("testXXXXXXtest", &error);
647   g_assert_no_error (error);
648   g_assert (g_file_test (name, G_FILE_TEST_IS_DIR));
649   g_assert (g_rmdir (name) == 0);
650   g_free (name);
651
652   name = g_dir_make_tmp (NULL, &error);
653   g_assert_no_error (error);
654   g_assert (g_file_test (name, G_FILE_TEST_IS_DIR));
655   g_assert (g_rmdir (name) == 0);
656   g_free (name);
657
658   name = g_dir_make_tmp ("test/XXXXXX", &error);
659   g_assert_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED);
660   g_clear_error (&error);
661   g_assert (name == NULL);
662
663   name = g_dir_make_tmp ("XXXXxX", &error);
664   g_assert_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED);
665   g_clear_error (&error);
666   g_assert (name == NULL);
667 }
668
669 static void
670 test_file_open_tmp (void)
671 {
672   gchar *name = NULL;
673   GError *error = NULL;
674   gint fd;
675
676   fd = g_file_open_tmp ("testXXXXXXtest", &name, &error);
677   g_assert (fd != -1);
678   g_assert_no_error (error);
679   g_assert (name != NULL);
680   unlink (name);
681   g_free (name);
682   close (fd);
683
684   fd = g_file_open_tmp (NULL, &name, &error);
685   g_assert (fd != -1);
686   g_assert_no_error (error);
687   g_assert (name != NULL);
688   g_unlink (name);
689   g_free (name);
690   close (fd);
691
692   name = NULL;
693   fd = g_file_open_tmp ("test/XXXXXX", &name, &error);
694   g_assert (fd == -1);
695   g_assert (name == NULL);
696   g_assert_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED);
697   g_clear_error (&error);
698
699   fd = g_file_open_tmp ("XXXXxX", &name, &error);
700   g_assert (fd == -1);
701   g_assert (name == NULL);
702   g_assert_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED);
703   g_clear_error (&error);
704 }
705
706 static void
707 test_mkstemp (void)
708 {
709   gchar *name;
710   gint fd;
711
712   name = g_strdup ("testXXXXXXtest"),
713   fd = g_mkstemp (name);
714   g_assert (fd != -1);
715   g_assert (strstr (name, "XXXXXX") == NULL);
716   unlink (name);
717   close (fd);
718   g_free (name);
719
720   name = g_strdup ("testYYYYYYtest"),
721   fd = g_mkstemp (name);
722   g_assert (fd == -1);
723   g_free (name);
724 }
725
726 static void
727 test_mkdtemp (void)
728 {
729   gchar *name;
730   gchar *ret;
731
732   name = g_strdup ("testXXXXXXtest"),
733   ret = g_mkdtemp (name);
734   g_assert (ret == name);
735   g_assert (strstr (name, "XXXXXX") == NULL);
736   g_rmdir (name);
737   g_free (name);
738
739   name = g_strdup ("testYYYYYYtest"),
740   ret = g_mkdtemp (name);
741   g_assert (ret == NULL);
742   g_free (name);
743 }
744
745 static void
746 test_set_contents (void)
747 {
748   GError *error = NULL;
749   gint fd;
750   gchar *name;
751   gchar *buf;
752   gsize len;
753   gboolean ret;
754
755   fd = g_file_open_tmp (NULL, &name, &error);
756   g_assert_no_error (error);
757   write (fd, "a", 1);
758   close (fd);
759
760   ret = g_file_get_contents (name, &buf, &len, &error);
761   g_assert (ret);
762   g_assert_no_error (error);
763   g_assert_cmpstr (buf, ==, "a");
764   g_free (buf);
765
766   ret = g_file_set_contents (name, "b", 1, &error);
767   g_assert (ret);
768   g_assert_no_error (error);
769
770   ret = g_file_get_contents (name, &buf, &len, &error);
771   g_assert (ret);
772   g_assert_no_error (error);
773   g_assert_cmpstr (buf, ==, "b");
774   g_free (buf);
775
776   g_remove (name);
777   g_free (name);
778 }
779
780 int
781 main (int   argc,
782       char *argv[])
783 {
784   g_test_init (&argc, &argv, NULL);
785
786   g_test_add_func ("/fileutils/build-path", test_build_path);
787   g_test_add_func ("/fileutils/build-pathv", test_build_pathv);
788   g_test_add_func ("/fileutils/build-filename", test_build_filename);
789   g_test_add_func ("/fileutils/build-filenamev", test_build_filenamev);
790   g_test_add_func ("/fileutils/mkdir-with-parents", test_mkdir_with_parents);
791   g_test_add_func ("/fileutils/format-size-for-display", test_format_size_for_display);
792   g_test_add_func ("/fileutils/errors", test_file_errors);
793   g_test_add_func ("/fileutils/basename", test_basename);
794   g_test_add_func ("/fileutils/dir-make-tmp", test_dir_make_tmp);
795   g_test_add_func ("/fileutils/file-open-tmp", test_file_open_tmp);
796   g_test_add_func ("/fileutils/mkstemp", test_mkstemp);
797   g_test_add_func ("/fileutils/mkdtemp", test_mkdtemp);
798   g_test_add_func ("/fileutils/set-contents", test_set_contents);
799
800   return g_test_run ();
801 }