fileutils: Improve test coverage
[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 #define S G_DIR_SEPARATOR_S
32
33 static void
34 check_string (gchar *str, gchar *expected)
35 {
36   g_assert (str != NULL);
37   g_assert_cmpstr (str, ==, expected);
38   g_free (str);
39 }
40
41 static void
42 test_build_path (void)
43 {
44 /*  check_string (g_build_path ("", NULL), "");*/
45   check_string (g_build_path ("", "", NULL), "");
46   check_string (g_build_path ("", "x", NULL), "x");
47   check_string (g_build_path ("", "x", "y",  NULL), "xy");
48   check_string (g_build_path ("", "x", "y", "z", NULL), "xyz");
49
50 /*  check_string (g_build_path (":", NULL), "");*/
51   check_string (g_build_path (":", ":", NULL), ":");
52   check_string (g_build_path (":", ":x", NULL), ":x");
53   check_string (g_build_path (":", "x:", NULL), "x:");
54   check_string (g_build_path (":", "", "x", NULL), "x");
55   check_string (g_build_path (":", "", ":x", NULL), ":x");
56   check_string (g_build_path (":", ":", "x", NULL), ":x");
57   check_string (g_build_path (":", "::", "x", NULL), "::x");
58   check_string (g_build_path (":", "x", "", NULL), "x");
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", "y",  NULL), "x:y");
63   check_string (g_build_path (":", ":x", "y", NULL), ":x:y");
64   check_string (g_build_path (":", "x", "y:", NULL), "x:y:");
65   check_string (g_build_path (":", ":x:", ":y:", NULL), ":x:y:");
66   check_string (g_build_path (":", ":x::", "::y:", NULL), ":x:y:");
67   check_string (g_build_path (":", "x", "","y",  NULL), "x:y");
68   check_string (g_build_path (":", "x", ":", "y",  NULL), "x:y");
69   check_string (g_build_path (":", "x", "::", "y",  NULL), "x:y");
70   check_string (g_build_path (":", "x", "y", "z", NULL), "x:y:z");
71   check_string (g_build_path (":", ":x:", ":y:", ":z:", NULL), ":x:y:z:");
72   check_string (g_build_path (":", "::x::", "::y::", "::z::", NULL), "::x:y:z::");
73
74 /*  check_string (g_build_path ("::", NULL), "");*/
75   check_string (g_build_path ("::", "::", NULL), "::");
76   check_string (g_build_path ("::", ":::", NULL), ":::");
77   check_string (g_build_path ("::", "::x", NULL), "::x");
78   check_string (g_build_path ("::", "x::", NULL), "x::");
79   check_string (g_build_path ("::", "", "x", NULL), "x");
80   check_string (g_build_path ("::", "", "::x", NULL), "::x");
81   check_string (g_build_path ("::", "::", "x", NULL), "::x");
82   check_string (g_build_path ("::", "::::", "x", NULL), "::::x");
83   check_string (g_build_path ("::", "x", "", NULL), "x");
84   check_string (g_build_path ("::", "x::", "", NULL), "x::");
85   check_string (g_build_path ("::", "x", "::", NULL), "x::");
86
87   /* This following is weird, but keeps the definition simple */
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", "y",  NULL), "x::y");
91   check_string (g_build_path ("::", "::x", "y", NULL), "::x::y");
92   check_string (g_build_path ("::", "x", "y::", NULL), "x::y::");
93   check_string (g_build_path ("::", "::x::", "::y::", NULL), "::x::y::");
94   check_string (g_build_path ("::", "::x:::", ":::y::", NULL), "::x::::y::");
95   check_string (g_build_path ("::", "::x::::", "::::y::", NULL), "::x::y::");
96   check_string (g_build_path ("::", "x", "", "y",  NULL), "x::y");
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", "z", NULL), "x::y::z");
100   check_string (g_build_path ("::", "::x::", "::y::", "::z::", NULL), "::x::y::z::");
101   check_string (g_build_path ("::", ":::x:::", ":::y:::", ":::z:::", NULL), ":::x::::y::::z:::");
102   check_string (g_build_path ("::", "::::x::::", "::::y::::", "::::z::::", NULL), "::::x::y::z::::");
103 }
104
105 static void
106 test_build_pathv (void)
107 {
108   gchar *args[10];
109
110   g_assert (g_build_pathv ("", NULL) == NULL);
111   args[0] = NULL;
112   check_string (g_build_pathv ("", args), "");
113   args[0] = ""; args[1] = NULL;
114   check_string (g_build_pathv ("", args), "");
115   args[0] = "x"; args[1] = NULL;
116   check_string (g_build_pathv ("", args), "x");
117   args[0] = "x"; args[1] = "y"; args[2] = NULL;
118   check_string (g_build_pathv ("", args), "xy");
119   args[0] = "x"; args[1] = "y"; args[2] = "z", args[3] = NULL;
120   check_string (g_build_pathv ("", args), "xyz");
121
122   args[0] = NULL;
123   check_string (g_build_pathv (":", args), "");
124   args[0] = ":"; args[1] = NULL;
125   check_string (g_build_pathv (":", args), ":");
126   args[0] = ":x"; args[1] = NULL;
127   check_string (g_build_pathv (":", args), ":x");
128   args[0] = "x:"; args[1] = NULL;
129   check_string (g_build_pathv (":", args), "x:");
130   args[0] = ""; args[1] = "x"; args[2] = NULL;
131   check_string (g_build_pathv (":", args), "x");
132   args[0] = ""; args[1] = ":x"; args[2] = NULL;
133   check_string (g_build_pathv (":", args), ":x");
134   args[0] = ":"; args[1] = "x"; args[2] = NULL;
135   check_string (g_build_pathv (":", args), ":x");
136   args[0] = "::"; args[1] = "x"; args[2] = NULL;
137   check_string (g_build_pathv (":", args), "::x");
138   args[0] = "x"; args[1] = ""; args[2] = NULL;
139   check_string (g_build_pathv (":", args), "x");
140   args[0] = "x:"; args[1] = ""; args[2] = NULL;
141   check_string (g_build_pathv (":", args), "x:");
142   args[0] = "x"; args[1] = ":"; args[2] = NULL;
143   check_string (g_build_pathv (":", args), "x:");
144   args[0] = "x"; args[1] = "::"; args[2] = NULL;
145   check_string (g_build_pathv (":", args), "x::");
146   args[0] = "x"; args[1] = "y"; args[2] = NULL;
147   check_string (g_build_pathv (":", args), "x:y");
148   args[0] = ":x"; args[1] = "y"; args[2] = NULL;
149   check_string (g_build_pathv (":", args), ":x:y");
150   args[0] = "x"; args[1] = "y:"; args[2] = NULL;
151   check_string (g_build_pathv (":", args), "x:y:");
152   args[0] = ":x:"; args[1] = ":y:"; args[2] = NULL;
153   check_string (g_build_pathv (":", args), ":x:y:");
154   args[0] = ":x::"; args[1] = "::y:"; args[2] = NULL;
155   check_string (g_build_pathv (":", args), ":x:y:");
156   args[0] = "x"; args[1] = ""; args[2] = "y"; args[3] = NULL;
157   check_string (g_build_pathv (":", args), "x:y");
158   args[0] = "x"; args[1] = ":"; args[2] = "y"; args[3] = NULL;
159   check_string (g_build_pathv (":", args), "x:y");
160   args[0] = "x"; args[1] = "::"; args[2] = "y"; args[3] = NULL;
161   check_string (g_build_pathv (":", args), "x:y");
162   args[0] = "x"; args[1] = "y"; args[2] = "z"; args[3] = NULL;
163   check_string (g_build_pathv (":", args), "x:y:z");
164   args[0] = ":x:"; args[1] = ":y:"; args[2] = ":z:"; args[3] = NULL;
165   check_string (g_build_pathv (":", args), ":x:y:z:");
166   args[0] = "::x::"; args[1] = "::y::"; args[2] = "::z::"; args[3] = NULL;
167   check_string (g_build_pathv (":", args), "::x:y:z::");
168
169   args[0] = NULL;
170   check_string (g_build_pathv ("::", args), "");
171   args[0] = "::"; args[1] = NULL;
172   check_string (g_build_pathv ("::", args), "::");
173   args[0] = ":::"; args[1] = NULL;
174   check_string (g_build_pathv ("::", args), ":::");
175   args[0] = "::x"; args[1] = NULL;
176   check_string (g_build_pathv ("::", args), "::x");
177   args[0] = "x::"; args[1] = NULL;
178   check_string (g_build_pathv ("::", args), "x::");
179   args[0] = ""; args[1] = "x"; args[2] = NULL;
180   check_string (g_build_pathv ("::", args), "x");
181   args[0] = ""; args[1] = "::x"; args[2] = NULL;
182   check_string (g_build_pathv ("::", args), "::x");
183   args[0] = "::"; args[1] = "x"; args[2] = NULL;
184   check_string (g_build_pathv ("::", args), "::x");
185   args[0] = "::::"; args[1] = "x"; args[2] = NULL;
186   check_string (g_build_pathv ("::", args), "::::x");
187   args[0] = "x"; args[1] = ""; args[2] = NULL;
188   check_string (g_build_pathv ("::", args), "x");
189   args[0] = "x::"; args[1] = ""; args[2] = NULL;
190   check_string (g_build_pathv ("::", args), "x::");
191   args[0] = "x"; args[1] = "::"; args[2] = NULL;
192   check_string (g_build_pathv ("::", args), "x::");
193   /* This following is weird, but keeps the definition simple */
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] = "y"; args[2] = NULL;
199   check_string (g_build_pathv ("::", args), "x::y");
200   args[0] = "::x"; args[1] = "y"; args[2] = NULL;
201   check_string (g_build_pathv ("::", args), "::x::y");
202   args[0] = "x"; args[1] = "y::"; args[2] = NULL;
203   check_string (g_build_pathv ("::", args), "x::y::");
204   args[0] = "::x::"; args[1] = "::y::"; args[2] = NULL;
205   check_string (g_build_pathv ("::", args), "::x::y::");
206   args[0] = "::x:::"; args[1] = ":::y::"; args[2] = NULL;
207   check_string (g_build_pathv ("::", args), "::x::::y::");
208   args[0] = "::x::::"; args[1] = "::::y::"; args[2] = NULL;
209   check_string (g_build_pathv ("::", args), "::x::y::");
210   args[0] = "x"; args[1] = ""; args[2] = "y"; args[3] = NULL;
211   check_string (g_build_pathv ("::", args), "x::y");
212   args[0] = "x"; args[1] = "::"; args[2] = "y"; args[3] = NULL;
213   check_string (g_build_pathv ("::", args), "x::y");
214   args[0] = "x"; args[1] = "::::"; args[2] = "y"; args[3] = NULL;
215   check_string (g_build_pathv ("::", args), "x::y");
216   args[0] = "x"; args[1] = "y"; args[2] = "z"; args[3] = NULL;
217   check_string (g_build_pathv ("::", args), "x::y::z");
218   args[0] = "::x::"; args[1] = "::y::"; args[2] = "::z::"; args[3] = NULL;
219   check_string (g_build_pathv ("::", args), "::x::y::z::");
220   args[0] = ":::x:::"; args[1] = ":::y:::"; args[2] = ":::z:::"; args[3] = NULL;
221   check_string (g_build_pathv ("::", args), ":::x::::y::::z:::");
222   args[0] = "::::x::::"; args[1] = "::::y::::"; args[2] = "::::z::::"; args[3] = NULL;
223   check_string (g_build_pathv ("::", args), "::::x::y::z::::");
224 }
225
226 static void
227 test_build_filename (void)
228 {
229 /*  check_string (g_build_filename (NULL), "");*/
230   check_string (g_build_filename (S, NULL), S);
231   check_string (g_build_filename (S"x", NULL), S"x");
232   check_string (g_build_filename ("x"S, NULL), "x"S);
233   check_string (g_build_filename ("", "x", NULL), "x");
234   check_string (g_build_filename ("", S"x", NULL), S"x");
235   check_string (g_build_filename (S, "x", NULL), S"x");
236   check_string (g_build_filename (S S, "x", NULL), S S"x");
237   check_string (g_build_filename ("x", "", NULL), "x");
238   check_string (g_build_filename ("x"S, "", NULL), "x"S);
239   check_string (g_build_filename ("x", S, NULL), "x"S);
240   check_string (g_build_filename ("x", S S, NULL), "x"S S);
241   check_string (g_build_filename ("x", "y",  NULL), "x"S"y");
242   check_string (g_build_filename (S"x", "y", NULL), S"x"S"y");
243   check_string (g_build_filename ("x", "y"S, NULL), "x"S"y"S);
244   check_string (g_build_filename (S"x"S, S"y"S, NULL), S"x"S"y"S);
245   check_string (g_build_filename (S"x"S S, S S"y"S, NULL), S"x"S"y"S);
246   check_string (g_build_filename ("x", "", "y",  NULL), "x"S"y");
247   check_string (g_build_filename ("x", S, "y",  NULL), "x"S"y");
248   check_string (g_build_filename ("x", S S, "y",  NULL), "x"S"y");
249   check_string (g_build_filename ("x", "y", "z", NULL), "x"S"y"S"z");
250   check_string (g_build_filename (S"x"S, S"y"S, S"z"S, NULL), S"x"S"y"S"z"S);
251   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);
252
253 #ifdef G_OS_WIN32
254
255   /* Test also using the slash as file name separator */
256 #define U "/"
257   check_string (g_build_filename (NULL), "");
258   check_string (g_build_filename (U, NULL), U);
259   check_string (g_build_filename (U"x", NULL), U"x");
260   check_string (g_build_filename ("x"U, NULL), "x"U);
261   check_string (g_build_filename ("", U"x", NULL), U"x");
262   check_string (g_build_filename ("", U"x", NULL), U"x");
263   check_string (g_build_filename (U, "x", NULL), U"x");
264   check_string (g_build_filename (U U, "x", NULL), U U"x");
265   check_string (g_build_filename (U S, "x", NULL), U S"x");
266   check_string (g_build_filename ("x"U, "", NULL), "x"U);
267   check_string (g_build_filename ("x"S"y", "z"U"a", NULL), "x"S"y"S"z"U"a");
268   check_string (g_build_filename ("x", U, NULL), "x"U);
269   check_string (g_build_filename ("x", U U, NULL), "x"U U);
270   check_string (g_build_filename ("x", S U, NULL), "x"S U);
271   check_string (g_build_filename (U"x", "y", NULL), U"x"U"y");
272   check_string (g_build_filename ("x", "y"U, NULL), "x"U"y"U);
273   check_string (g_build_filename (U"x"U, U"y"U, NULL), U"x"U"y"U);
274   check_string (g_build_filename (U"x"U U, U U"y"U, NULL), U"x"U"y"U);
275   check_string (g_build_filename ("x", U, "y",  NULL), "x"U"y");
276   check_string (g_build_filename ("x", U U, "y",  NULL), "x"U"y");
277   check_string (g_build_filename ("x", U S, "y",  NULL), "x"S"y");
278   check_string (g_build_filename ("x", S U, "y",  NULL), "x"U"y");
279   check_string (g_build_filename ("x", U "y", "z", NULL), "x"U"y"U"z");
280   check_string (g_build_filename ("x", S "y", "z", NULL), "x"S"y"S"z");
281   check_string (g_build_filename ("x", S "y", "z", U, "a", "b", NULL), "x"S"y"S"z"U"a"U"b");
282   check_string (g_build_filename (U"x"U, U"y"U, U"z"U, NULL), U"x"U"y"U"z"U);
283   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);
284
285 #undef U
286
287 #endif /* G_OS_WIN32 */
288
289 }
290
291 static void
292 test_build_filenamev (void)
293 {
294   gchar *args[10];
295
296   args[0] = NULL;
297   check_string (g_build_filenamev (args), "");
298   args[0] = S; args[1] = NULL;
299   check_string (g_build_filenamev (args), S);
300   args[0] = S"x"; args[1] = NULL;
301   check_string (g_build_filenamev (args), S"x");
302   args[0] = "x"S; args[1] = NULL;
303   check_string (g_build_filenamev (args), "x"S);
304   args[0] = ""; args[1] = "x"; args[2] = NULL;
305   check_string (g_build_filenamev (args), "x");
306   args[0] = ""; args[1] = S"x"; args[2] = NULL;
307   check_string (g_build_filenamev (args), S"x");
308   args[0] = S; args[1] = "x"; args[2] = NULL;
309   check_string (g_build_filenamev (args), S"x");
310   args[0] = S S; args[1] = "x"; args[2] = NULL;
311   check_string (g_build_filenamev (args), S S"x");
312   args[0] = "x"; args[1] = ""; args[2] = NULL;
313   check_string (g_build_filenamev (args), "x");
314   args[0] = "x"S; args[1] = ""; args[2] = NULL;
315   check_string (g_build_filenamev (args), "x"S);
316   args[0] = "x"; args[1] = S; args[2] = NULL;
317   check_string (g_build_filenamev (args), "x"S);
318   args[0] = "x"; args[1] = S S; args[2] = NULL;
319   check_string (g_build_filenamev (args), "x"S S);
320   args[0] = "x"; args[1] = "y"; args[2] = NULL;
321   check_string (g_build_filenamev (args), "x"S"y");
322   args[0] = S"x"; args[1] = "y"; args[2] = NULL;
323   check_string (g_build_filenamev (args), S"x"S"y");
324   args[0] = "x"; args[1] = "y"S; args[2] = NULL;
325   check_string (g_build_filenamev (args), "x"S"y"S);
326   args[0] = S"x"S; args[1] = S"y"S; args[2] = NULL;
327   check_string (g_build_filenamev (args), S"x"S"y"S);
328   args[0] = S"x"S S; args[1] = S S"y"S; args[2] = NULL;
329   check_string (g_build_filenamev (args), S"x"S"y"S);
330   args[0] = "x"; args[1] = ""; args[2] = "y"; args[3] = NULL;
331   check_string (g_build_filenamev (args), "x"S"y");
332   args[0] = "x"; args[1] = S; args[2] = "y"; args[3] = NULL;
333   check_string (g_build_filenamev (args), "x"S"y");
334   args[0] = "x"; args[1] = S S; args[2] = "y"; args[3] = NULL;
335   check_string (g_build_filenamev (args), "x"S"y");
336   args[0] = "x"; args[1] = "y"; args[2] = "z"; args[3] = NULL;
337   check_string (g_build_filenamev (args), "x"S"y"S"z");
338   args[0] = S"x"S; args[1] = S"y"S; args[2] = S"z"S; args[3] = NULL;
339   check_string (g_build_filenamev (args), S"x"S"y"S"z"S);
340   args[0] = S S"x"S S; args[1] = S S"y"S S; args[2] = S S"z"S S; args[3] = NULL;
341   check_string (g_build_filenamev (args), S S"x"S"y"S"z"S S);
342
343 #ifdef G_OS_WIN32
344
345   /* Test also using the slash as file name separator */
346 #define U "/"
347   args[0] = NULL;
348   check_string (g_build_filenamev (args), "");
349   args[0] = U; args[1] = NULL;
350   check_string (g_build_filenamev (args), U);
351   args[0] = U"x"; args[1] = NULL;
352   check_string (g_build_filenamev (args), U"x");
353   args[0] = "x"U; args[1] = NULL;
354   check_string (g_build_filenamev (args), "x"U);
355   args[0] = ""; args[1] = U"x"; args[2] = NULL;
356   check_string (g_build_filenamev (args), U"x");
357   args[0] = ""; args[1] = U"x"; args[2] = NULL;
358   check_string (g_build_filenamev (args), U"x");
359   args[0] = U; args[1] = "x"; args[2] = NULL;
360   check_string (g_build_filenamev (args), U"x");
361   args[0] = U U; args[1] = "x"; args[2] = NULL;
362   check_string (g_build_filenamev (args), U U"x");
363   args[0] = U S; args[1] = "x"; args[2] = NULL;
364   check_string (g_build_filenamev (args), U S"x");
365   args[0] = "x"U; args[1] = ""; args[2] = NULL;
366   check_string (g_build_filenamev (args), "x"U);
367   args[0] = "x"S"y"; args[1] = "z"U"a"; args[2] = NULL;
368   check_string (g_build_filenamev (args), "x"S"y"S"z"U"a");
369   args[0] = "x"; args[1] = U; args[2] = NULL;
370   check_string (g_build_filenamev (args), "x"U);
371   args[0] = "x"; args[1] = U U; args[2] = NULL;
372   check_string (g_build_filenamev (args), "x"U U);
373   args[0] = "x"; args[1] = S U; args[2] = NULL;
374   check_string (g_build_filenamev (args), "x"S U);
375   args[0] = U"x"; args[1] = "y"; args[2] = NULL;
376   check_string (g_build_filenamev (args), U"x"U"y");
377   args[0] = "x"; args[1] = "y"U; args[2] = NULL;
378   check_string (g_build_filenamev (args), "x"U"y"U);
379   args[0] = U"x"U; args[1] = U"y"U; args[2] = NULL;
380   check_string (g_build_filenamev (args), U"x"U"y"U);
381   args[0] = U"x"U U; args[1] = U U"y"U; args[2] = NULL;
382   check_string (g_build_filenamev (args), U"x"U"y"U);
383   args[0] = "x"; args[1] = U; args[2] = "y", args[3] = NULL;
384   check_string (g_build_filenamev (args), "x"U"y");
385   args[0] = "x"; args[1] = U U; args[2] = "y", args[3] = NULL;
386   check_string (g_build_filenamev (args), "x"U"y");
387   args[0] = "x"; args[1] = U S; args[2] = "y", args[3] = NULL;
388   check_string (g_build_filenamev (args), "x"S"y");
389   args[0] = "x"; args[1] = S U; args[2] = "y", args[3] = NULL;
390   check_string (g_build_filenamev (args), "x"U"y");
391   args[0] = "x"; args[1] = U "y"; args[2] = "z", args[3] = NULL;
392   check_string (g_build_filenamev (args), "x"U"y"U"z");
393   args[0] = "x"; args[1] = S "y"; args[2] = "z", args[3] = NULL;
394   check_string (g_build_filenamev (args), "x"S"y"S"z");
395   args[0] = "x"; args[1] = S "y"; args[2] = "z", args[3] = U;
396   args[4] = "a"; args[5] = "b"; args[6] = NULL;
397   check_string (g_build_filenamev (args), "x"S"y"S"z"U"a"U"b");
398   args[0] = U"x"U; args[1] = U"y"U; args[2] = U"z"U, args[3] = NULL;
399   check_string (g_build_filenamev (args), U"x"U"y"U"z"U);
400   args[0] = U U"x"U U; args[1] = U U"y"U U; args[2] = U U"z"U U, args[3] = NULL;
401   check_string (g_build_filenamev (args), U U"x"U"y"U"z"U U);
402
403 #undef U
404
405 #endif /* G_OS_WIN32 */
406 }
407
408 #undef S
409
410 static void
411 test_mkdir_with_parents_1 (const gchar *base)
412 {
413   char *p0 = g_build_filename (base, "fum", NULL);
414   char *p1 = g_build_filename (p0, "tem", NULL);
415   char *p2 = g_build_filename (p1, "zap", NULL);
416   FILE *f;
417
418   g_remove (p2);
419   g_remove (p1);
420   g_remove (p0);
421
422   if (g_file_test (p0, G_FILE_TEST_EXISTS))
423     g_error ("failed, %s exists, cannot test g_mkdir_with_parents\n", p0);
424
425   if (g_file_test (p1, G_FILE_TEST_EXISTS))
426     g_error ("failed, %s exists, cannot test g_mkdir_with_parents\n", p1);
427
428   if (g_file_test (p2, G_FILE_TEST_EXISTS))
429     g_error ("failed, %s exists, cannot test g_mkdir_with_parents\n", p2);
430
431   if (g_mkdir_with_parents (p2, 0777) == -1)
432     g_error ("failed, g_mkdir_with_parents(%s) failed: %s\n", p2, g_strerror (errno));
433
434   if (!g_file_test (p2, G_FILE_TEST_IS_DIR))
435     g_error ("failed, g_mkdir_with_parents(%s) succeeded, but %s is not a directory\n", p2, p2);
436
437   if (!g_file_test (p1, G_FILE_TEST_IS_DIR))
438     g_error ("failed, g_mkdir_with_parents(%s) succeeded, but %s is not a directory\n", p2, p1);
439
440   if (!g_file_test (p0, G_FILE_TEST_IS_DIR))
441     g_error ("failed, g_mkdir_with_parents(%s) succeeded, but %s is not a directory\n", p2, p0);
442
443   g_rmdir (p2);
444   if (g_file_test (p2, G_FILE_TEST_EXISTS))
445     g_error ("failed, did g_rmdir(%s), but %s is still there\n", p2, p2);
446
447   g_rmdir (p1);
448   if (g_file_test (p1, G_FILE_TEST_EXISTS))
449     g_error ("failed, did g_rmdir(%s), but %s is still there\n", p1, p1);
450
451   f = g_fopen (p1, "w");
452   if (f == NULL)
453     g_error ("failed, couldn't create file %s\n", p1);
454   fclose (f);
455
456   if (g_mkdir_with_parents (p1, 0666) == 0)
457     g_error ("failed, g_mkdir_with_parents(%s) succeeded, even if %s is a file\n", p1, p1);
458
459   if (g_mkdir_with_parents (p2, 0666) == 0)
460     g_error("failed, g_mkdir_with_parents(%s) succeeded, even if %s is a file\n", p2, p1);
461
462   g_remove (p2);
463   g_remove (p1);
464   g_remove (p0);
465
466   g_free (p2);
467   g_free (p1);
468   g_free (p0);
469 }
470
471 static void
472 test_mkdir_with_parents (void)
473 {
474   gchar *cwd;
475   if (g_test_verbose())
476     g_print ("checking g_mkdir_with_parents() in subdir ./hum/");
477   test_mkdir_with_parents_1 ("hum");
478   g_remove ("hum");
479   if (g_test_verbose())
480     g_print ("checking g_mkdir_with_parents() in subdir ./hii///haa/hee/");
481   test_mkdir_with_parents_1 ("hii///haa/hee");
482   g_remove ("hii/haa/hee");
483   g_remove ("hii/haa");
484   g_remove ("hii");
485   cwd = g_get_current_dir ();
486   if (g_test_verbose())
487     g_print ("checking g_mkdir_with_parents() in cwd: %s", cwd);
488   test_mkdir_with_parents_1 (cwd);
489   g_free (cwd);
490
491   g_assert (g_mkdir_with_parents (NULL, 0) == -1);
492   g_assert (errno == EINVAL);
493 }
494
495 static void
496 test_format_size_for_display (void)
497 {
498   /* nobody called setlocale(), so we should get "C" behaviour... */
499   check_string (g_format_size_for_display (0), "0 bytes");
500   check_string (g_format_size_for_display (1), "1 byte");
501   check_string (g_format_size_for_display (2), "2 bytes");
502   check_string (g_format_size_for_display (1024), "1.0 KB");
503   check_string (g_format_size_for_display (1024 * 1024), "1.0 MB");
504   check_string (g_format_size_for_display (1024 * 1024 * 1024), "1.0 GB");
505
506   check_string (g_format_size (0), "0 bytes");
507   check_string (g_format_size (1), "1 byte");
508   check_string (g_format_size (2), "2 bytes");
509   check_string (g_format_size (1000ULL), "1.0 kB");
510   check_string (g_format_size (1000ULL * 1000), "1.0 MB");
511   check_string (g_format_size (1000ULL * 1000 * 1000), "1.0 GB");
512   check_string (g_format_size (1000ULL * 1000 * 1000 * 1000), "1.0 TB");
513   check_string (g_format_size (1000ULL * 1000 * 1000 * 1000 * 1000), "1.0 PB");
514   check_string (g_format_size (1000ULL * 1000 * 1000 * 1000 * 1000 * 1000), "1.0 EB");
515
516   check_string (g_format_size_full (0, G_FORMAT_SIZE_IEC_UNITS), "0 bytes");
517   check_string (g_format_size_full (1, G_FORMAT_SIZE_IEC_UNITS), "1 byte");
518   check_string (g_format_size_full (2, G_FORMAT_SIZE_IEC_UNITS), "2 bytes");
519
520   check_string (g_format_size_full (2048ULL, G_FORMAT_SIZE_IEC_UNITS), "2.0 KiB");
521   check_string (g_format_size_full (2048ULL * 1024, G_FORMAT_SIZE_IEC_UNITS), "2.0 MiB");
522   check_string (g_format_size_full (2048ULL * 1024 * 1024, G_FORMAT_SIZE_IEC_UNITS), "2.0 GiB");
523   check_string (g_format_size_full (2048ULL * 1024 * 1024 * 1024, G_FORMAT_SIZE_IEC_UNITS), "2.0 TiB");
524   check_string (g_format_size_full (2048ULL * 1024 * 1024 * 1024 * 1024, G_FORMAT_SIZE_IEC_UNITS), "2.0 PiB");
525   check_string (g_format_size_full (2048ULL * 1024 * 1024 * 1024 * 1024 * 1024, G_FORMAT_SIZE_IEC_UNITS), "2.0 EiB");
526
527   check_string (g_format_size_full (238472938, G_FORMAT_SIZE_IEC_UNITS), "227.4 MiB");
528   check_string (g_format_size_full (238472938, G_FORMAT_SIZE_DEFAULT), "238.5 MB");
529   check_string (g_format_size_full (238472938, G_FORMAT_SIZE_LONG_FORMAT), "238.5 MB (238472938 bytes)");
530 }
531
532 static void
533 test_file_errors (void)
534 {
535   g_assert (g_file_error_from_errno (EEXIST) == G_FILE_ERROR_EXIST);
536   g_assert (g_file_error_from_errno (EISDIR) == G_FILE_ERROR_ISDIR);
537   g_assert (g_file_error_from_errno (EACCES) == G_FILE_ERROR_ACCES);
538   g_assert (g_file_error_from_errno (ENAMETOOLONG) == G_FILE_ERROR_NAMETOOLONG);
539   g_assert (g_file_error_from_errno (ENOENT) == G_FILE_ERROR_NOENT);
540   g_assert (g_file_error_from_errno (ENOTDIR) == G_FILE_ERROR_NOTDIR);
541   g_assert (g_file_error_from_errno (ENXIO) == G_FILE_ERROR_NXIO);
542   g_assert (g_file_error_from_errno (ENODEV) == G_FILE_ERROR_NODEV);
543   g_assert (g_file_error_from_errno (EROFS) == G_FILE_ERROR_ROFS);
544   g_assert (g_file_error_from_errno (ETXTBSY) == G_FILE_ERROR_TXTBSY);
545   g_assert (g_file_error_from_errno (EFAULT) == G_FILE_ERROR_FAULT);
546   g_assert (g_file_error_from_errno (ELOOP) == G_FILE_ERROR_LOOP);
547   g_assert (g_file_error_from_errno (ENOSPC) == G_FILE_ERROR_NOSPC);
548   g_assert (g_file_error_from_errno (ENOMEM) == G_FILE_ERROR_NOMEM);
549   g_assert (g_file_error_from_errno (EMFILE) == G_FILE_ERROR_MFILE);
550   g_assert (g_file_error_from_errno (ENFILE) == G_FILE_ERROR_NFILE);
551   g_assert (g_file_error_from_errno (EBADF) == G_FILE_ERROR_BADF);
552   g_assert (g_file_error_from_errno (EINVAL) == G_FILE_ERROR_INVAL);
553   g_assert (g_file_error_from_errno (EPIPE) == G_FILE_ERROR_PIPE);
554   g_assert (g_file_error_from_errno (EAGAIN) == G_FILE_ERROR_AGAIN);
555   g_assert (g_file_error_from_errno (EINTR) == G_FILE_ERROR_INTR);
556   g_assert (g_file_error_from_errno (EIO) == G_FILE_ERROR_IO);
557   g_assert (g_file_error_from_errno (EPERM) == G_FILE_ERROR_PERM);
558   g_assert (g_file_error_from_errno (ENOSYS) == G_FILE_ERROR_NOSYS);
559 }
560
561 static void
562 test_basename (void)
563 {
564   gchar *b;
565
566   b = g_path_get_basename ("");
567   g_assert_cmpstr (b, ==, ".");
568   g_free (b);
569
570   b = g_path_get_basename ("///");
571   g_assert_cmpstr (b, ==, "/");
572   g_free (b);
573
574   b = g_path_get_basename ("/a/b/c/d");
575   g_assert_cmpstr (b, ==, "d");
576   g_free (b);
577 }
578
579 int
580 main (int   argc,
581       char *argv[])
582 {
583   g_test_init (&argc, &argv, NULL);
584
585   g_test_add_func ("/fileutils/build-path", test_build_path);
586   g_test_add_func ("/fileutils/build-pathv", test_build_pathv);
587   g_test_add_func ("/fileutils/build-filename", test_build_filename);
588   g_test_add_func ("/fileutils/build-filenamev", test_build_filenamev);
589   g_test_add_func ("/fileutils/mkdir-with-parents", test_mkdir_with_parents);
590   g_test_add_func ("/fileutils/format-size-for-display", test_format_size_for_display);
591   g_test_add_func ("/fileutils/errors", test_file_errors);
592   g_test_add_func ("/fileutils/basename", test_basename);
593
594   return g_test_run();
595 }