1 /* Unit tests for gfileutils
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
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.
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.
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.
26 /* We are testing some deprecated APIs here */
27 #define GLIB_DISABLE_DEPRECATION_WARNINGS
31 /* Test our stdio wrappers here */
32 #define G_STDIO_NO_WRAP_ON_UNIX
33 #include <glib/gstdio.h>
37 #include <sys/types.h>
46 #define S G_DIR_SEPARATOR_S
49 check_string (gchar *str, const gchar *expected)
51 g_assert_nonnull (str);
52 g_assert_cmpstr (str, ==, expected);
57 test_build_path (void)
59 /* check_string (g_build_path ("", NULL), "");*/
60 check_string (g_build_path ("", "", NULL), "");
61 check_string (g_build_path ("", "x", NULL), "x");
62 check_string (g_build_path ("", "x", "y", NULL), "xy");
63 check_string (g_build_path ("", "x", "y", "z", NULL), "xyz");
65 /* check_string (g_build_path (":", NULL), "");*/
66 check_string (g_build_path (":", ":", NULL), ":");
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", NULL), "x");
70 check_string (g_build_path (":", "", ":x", NULL), ":x");
71 check_string (g_build_path (":", ":", "x", NULL), ":x");
72 check_string (g_build_path (":", "::", "x", NULL), "::x");
73 check_string (g_build_path (":", "x", "", NULL), "x");
74 check_string (g_build_path (":", "x:", "", NULL), "x:");
75 check_string (g_build_path (":", "x", ":", NULL), "x:");
76 check_string (g_build_path (":", "x", "::", NULL), "x::");
77 check_string (g_build_path (":", "x", "y", NULL), "x:y");
78 check_string (g_build_path (":", ":x", "y", NULL), ":x:y");
79 check_string (g_build_path (":", "x", "y:", NULL), "x:y:");
80 check_string (g_build_path (":", ":x:", ":y:", NULL), ":x:y:");
81 check_string (g_build_path (":", ":x::", "::y:", NULL), ":x:y:");
82 check_string (g_build_path (":", "x", "","y", NULL), "x:y");
83 check_string (g_build_path (":", "x", ":", "y", NULL), "x:y");
84 check_string (g_build_path (":", "x", "::", "y", NULL), "x:y");
85 check_string (g_build_path (":", "x", "y", "z", NULL), "x:y:z");
86 check_string (g_build_path (":", ":x:", ":y:", ":z:", NULL), ":x:y:z:");
87 check_string (g_build_path (":", "::x::", "::y::", "::z::", NULL), "::x:y:z::");
89 /* check_string (g_build_path ("::", NULL), "");*/
90 check_string (g_build_path ("::", "::", NULL), "::");
91 check_string (g_build_path ("::", ":::", NULL), ":::");
92 check_string (g_build_path ("::", "::x", NULL), "::x");
93 check_string (g_build_path ("::", "x::", NULL), "x::");
94 check_string (g_build_path ("::", "", "x", NULL), "x");
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", NULL), "::::x");
98 check_string (g_build_path ("::", "x", "", NULL), "x");
99 check_string (g_build_path ("::", "x::", "", NULL), "x::");
100 check_string (g_build_path ("::", "x", "::", NULL), "x::");
102 /* This following is weird, but keeps the definition simple */
103 check_string (g_build_path ("::", "x", ":::", NULL), "x:::::");
104 check_string (g_build_path ("::", "x", "::::", NULL), "x::::");
105 check_string (g_build_path ("::", "x", "y", NULL), "x::y");
106 check_string (g_build_path ("::", "::x", "y", NULL), "::x::y");
107 check_string (g_build_path ("::", "x", "y::", NULL), "x::y::");
108 check_string (g_build_path ("::", "::x::", "::y::", NULL), "::x::y::");
109 check_string (g_build_path ("::", "::x:::", ":::y::", NULL), "::x::::y::");
110 check_string (g_build_path ("::", "::x::::", "::::y::", NULL), "::x::y::");
111 check_string (g_build_path ("::", "x", "", "y", NULL), "x::y");
112 check_string (g_build_path ("::", "x", "::", "y", NULL), "x::y");
113 check_string (g_build_path ("::", "x", "::::", "y", NULL), "x::y");
114 check_string (g_build_path ("::", "x", "y", "z", NULL), "x::y::z");
115 check_string (g_build_path ("::", "::x::", "::y::", "::z::", NULL), "::x::y::z::");
116 check_string (g_build_path ("::", ":::x:::", ":::y:::", ":::z:::", NULL), ":::x::::y::::z:::");
117 check_string (g_build_path ("::", "::::x::::", "::::y::::", "::::z::::", NULL), "::::x::y::z::::");
121 test_build_pathv (void)
125 g_assert_null (g_build_pathv ("", NULL));
127 check_string (g_build_pathv ("", args), "");
128 args[0] = ""; args[1] = NULL;
129 check_string (g_build_pathv ("", args), "");
130 args[0] = "x"; args[1] = NULL;
131 check_string (g_build_pathv ("", args), "x");
132 args[0] = "x"; args[1] = "y"; args[2] = NULL;
133 check_string (g_build_pathv ("", args), "xy");
134 args[0] = "x"; args[1] = "y"; args[2] = "z", args[3] = NULL;
135 check_string (g_build_pathv ("", args), "xyz");
138 check_string (g_build_pathv (":", args), "");
139 args[0] = ":"; args[1] = NULL;
140 check_string (g_build_pathv (":", args), ":");
141 args[0] = ":x"; args[1] = NULL;
142 check_string (g_build_pathv (":", args), ":x");
143 args[0] = "x:"; args[1] = NULL;
144 check_string (g_build_pathv (":", args), "x:");
145 args[0] = ""; args[1] = "x"; args[2] = NULL;
146 check_string (g_build_pathv (":", args), "x");
147 args[0] = ""; args[1] = ":x"; args[2] = NULL;
148 check_string (g_build_pathv (":", args), ":x");
149 args[0] = ":"; args[1] = "x"; args[2] = NULL;
150 check_string (g_build_pathv (":", args), ":x");
151 args[0] = "::"; args[1] = "x"; args[2] = NULL;
152 check_string (g_build_pathv (":", args), "::x");
153 args[0] = "x"; args[1] = ""; args[2] = NULL;
154 check_string (g_build_pathv (":", args), "x");
155 args[0] = "x:"; args[1] = ""; args[2] = NULL;
156 check_string (g_build_pathv (":", args), "x:");
157 args[0] = "x"; args[1] = ":"; args[2] = NULL;
158 check_string (g_build_pathv (":", args), "x:");
159 args[0] = "x"; args[1] = "::"; args[2] = NULL;
160 check_string (g_build_pathv (":", args), "x::");
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] = "y"; args[2] = NULL;
164 check_string (g_build_pathv (":", args), ":x:y");
165 args[0] = "x"; args[1] = "y:"; args[2] = NULL;
166 check_string (g_build_pathv (":", args), "x:y:");
167 args[0] = ":x:"; args[1] = ":y:"; args[2] = NULL;
168 check_string (g_build_pathv (":", args), ":x:y:");
169 args[0] = ":x::"; args[1] = "::y:"; args[2] = NULL;
170 check_string (g_build_pathv (":", args), ":x:y:");
171 args[0] = "x"; args[1] = ""; args[2] = "y"; args[3] = NULL;
172 check_string (g_build_pathv (":", args), "x:y");
173 args[0] = "x"; args[1] = ":"; args[2] = "y"; args[3] = NULL;
174 check_string (g_build_pathv (":", args), "x:y");
175 args[0] = "x"; args[1] = "::"; args[2] = "y"; args[3] = NULL;
176 check_string (g_build_pathv (":", args), "x:y");
177 args[0] = "x"; args[1] = "y"; args[2] = "z"; args[3] = NULL;
178 check_string (g_build_pathv (":", args), "x:y:z");
179 args[0] = ":x:"; args[1] = ":y:"; args[2] = ":z:"; args[3] = NULL;
180 check_string (g_build_pathv (":", args), ":x:y:z:");
181 args[0] = "::x::"; args[1] = "::y::"; args[2] = "::z::"; args[3] = NULL;
182 check_string (g_build_pathv (":", args), "::x:y:z::");
185 check_string (g_build_pathv ("::", args), "");
186 args[0] = "::"; args[1] = NULL;
187 check_string (g_build_pathv ("::", args), "::");
188 args[0] = ":::"; args[1] = NULL;
189 check_string (g_build_pathv ("::", args), ":::");
190 args[0] = "::x"; args[1] = NULL;
191 check_string (g_build_pathv ("::", args), "::x");
192 args[0] = "x::"; args[1] = NULL;
193 check_string (g_build_pathv ("::", args), "x::");
194 args[0] = ""; args[1] = "x"; args[2] = NULL;
195 check_string (g_build_pathv ("::", args), "x");
196 args[0] = ""; args[1] = "::x"; args[2] = NULL;
197 check_string (g_build_pathv ("::", args), "::x");
198 args[0] = "::"; args[1] = "x"; args[2] = NULL;
199 check_string (g_build_pathv ("::", args), "::x");
200 args[0] = "::::"; args[1] = "x"; args[2] = NULL;
201 check_string (g_build_pathv ("::", args), "::::x");
202 args[0] = "x"; args[1] = ""; args[2] = NULL;
203 check_string (g_build_pathv ("::", args), "x");
204 args[0] = "x::"; args[1] = ""; args[2] = NULL;
205 check_string (g_build_pathv ("::", args), "x::");
206 args[0] = "x"; args[1] = "::"; args[2] = NULL;
207 check_string (g_build_pathv ("::", args), "x::");
208 /* This following is weird, but keeps the definition simple */
209 args[0] = "x"; args[1] = ":::"; args[2] = NULL;
210 check_string (g_build_pathv ("::", args), "x:::::");
211 args[0] = "x"; args[1] = "::::"; args[2] = NULL;
212 check_string (g_build_pathv ("::", args), "x::::");
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] = "y::"; args[2] = NULL;
218 check_string (g_build_pathv ("::", args), "x::y::");
219 args[0] = "::x::"; args[1] = "::y::"; args[2] = NULL;
220 check_string (g_build_pathv ("::", args), "::x::y::");
221 args[0] = "::x:::"; args[1] = ":::y::"; args[2] = NULL;
222 check_string (g_build_pathv ("::", args), "::x::::y::");
223 args[0] = "::x::::"; args[1] = "::::y::"; args[2] = NULL;
224 check_string (g_build_pathv ("::", args), "::x::y::");
225 args[0] = "x"; args[1] = ""; args[2] = "y"; args[3] = NULL;
226 check_string (g_build_pathv ("::", args), "x::y");
227 args[0] = "x"; args[1] = "::"; args[2] = "y"; args[3] = NULL;
228 check_string (g_build_pathv ("::", args), "x::y");
229 args[0] = "x"; args[1] = "::::"; args[2] = "y"; args[3] = NULL;
230 check_string (g_build_pathv ("::", args), "x::y");
231 args[0] = "x"; args[1] = "y"; args[2] = "z"; args[3] = NULL;
232 check_string (g_build_pathv ("::", args), "x::y::z");
233 args[0] = "::x::"; args[1] = "::y::"; args[2] = "::z::"; args[3] = NULL;
234 check_string (g_build_pathv ("::", args), "::x::y::z::");
235 args[0] = ":::x:::"; args[1] = ":::y:::"; args[2] = ":::z:::"; args[3] = NULL;
236 check_string (g_build_pathv ("::", args), ":::x::::y::::z:::");
237 args[0] = "::::x::::"; args[1] = "::::y::::"; args[2] = "::::z::::"; args[3] = NULL;
238 check_string (g_build_pathv ("::", args), "::::x::y::z::::");
242 test_build_filename (void)
244 /* check_string (g_build_filename (NULL), "");*/
245 check_string (g_build_filename (S, NULL), S);
246 check_string (g_build_filename (S"x", NULL), S"x");
247 check_string (g_build_filename ("x"S, NULL), "x"S);
248 check_string (g_build_filename ("", "x", NULL), "x");
249 check_string (g_build_filename ("", S"x", NULL), S"x");
250 check_string (g_build_filename (S, "x", NULL), S"x");
251 check_string (g_build_filename (S S, "x", NULL), S S"x");
252 check_string (g_build_filename ("x", "", NULL), "x");
253 check_string (g_build_filename ("x"S, "", NULL), "x"S);
254 check_string (g_build_filename ("x", S, NULL), "x"S);
255 check_string (g_build_filename ("x", S S, NULL), "x"S S);
256 check_string (g_build_filename ("x", "y", NULL), "x"S"y");
257 check_string (g_build_filename (S"x", "y", NULL), S"x"S"y");
258 check_string (g_build_filename ("x", "y"S, NULL), "x"S"y"S);
259 check_string (g_build_filename (S"x"S, S"y"S, NULL), S"x"S"y"S);
260 check_string (g_build_filename (S"x"S S, S S"y"S, NULL), S"x"S"y"S);
261 check_string (g_build_filename ("x", "", "y", NULL), "x"S"y");
262 check_string (g_build_filename ("x", S, "y", NULL), "x"S"y");
263 check_string (g_build_filename ("x", S S, "y", NULL), "x"S"y");
264 check_string (g_build_filename ("x", "y", "z", NULL), "x"S"y"S"z");
265 check_string (g_build_filename (S"x"S, S"y"S, S"z"S, NULL), S"x"S"y"S"z"S);
266 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);
270 /* Test also using the slash as file name separator */
272 /* check_string (g_build_filename (NULL), ""); */
273 check_string (g_build_filename (U, NULL), U);
274 check_string (g_build_filename (U"x", NULL), U"x");
275 check_string (g_build_filename ("x"U, NULL), "x"U);
276 check_string (g_build_filename ("", U"x", NULL), U"x");
277 check_string (g_build_filename ("", U"x", NULL), U"x");
278 check_string (g_build_filename (U, "x", NULL), U"x");
279 check_string (g_build_filename (U U, "x", NULL), U U"x");
280 check_string (g_build_filename (U S, "x", NULL), U S"x");
281 check_string (g_build_filename ("x"U, "", NULL), "x"U);
282 check_string (g_build_filename ("x"S"y", "z"U"a", NULL), "x"S"y"S"z"U"a");
283 check_string (g_build_filename ("x", U, NULL), "x"U);
284 check_string (g_build_filename ("x", U U, NULL), "x"U U);
285 check_string (g_build_filename ("x", S U, NULL), "x"S U);
286 check_string (g_build_filename (U"x", "y", NULL), U"x"U"y");
287 check_string (g_build_filename ("x", "y"U, NULL), "x"U"y"U);
288 check_string (g_build_filename (U"x"U, U"y"U, NULL), U"x"U"y"U);
289 check_string (g_build_filename (U"x"U U, U U"y"U, NULL), U"x"U"y"U);
290 check_string (g_build_filename ("x", U, "y", NULL), "x"U"y");
291 check_string (g_build_filename ("x", U U, "y", NULL), "x"U"y");
292 check_string (g_build_filename ("x", U S, "y", NULL), "x"S"y");
293 check_string (g_build_filename ("x", S U, "y", NULL), "x"U"y");
294 check_string (g_build_filename ("x", U "y", "z", NULL), "x"U"y"U"z");
295 check_string (g_build_filename ("x", S "y", "z", NULL), "x"S"y"S"z");
296 check_string (g_build_filename ("x", S "y", "z", U, "a", "b", NULL), "x"S"y"S"z"U"a"U"b");
297 check_string (g_build_filename (U"x"U, U"y"U, U"z"U, NULL), U"x"U"y"U"z"U);
298 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);
302 #endif /* G_OS_WIN32 */
307 test_build_filenamev (void)
312 check_string (g_build_filenamev (args), "");
313 args[0] = S; args[1] = NULL;
314 check_string (g_build_filenamev (args), S);
315 args[0] = S"x"; args[1] = NULL;
316 check_string (g_build_filenamev (args), S"x");
317 args[0] = "x"S; args[1] = NULL;
318 check_string (g_build_filenamev (args), "x"S);
319 args[0] = ""; args[1] = "x"; args[2] = NULL;
320 check_string (g_build_filenamev (args), "x");
321 args[0] = ""; args[1] = S"x"; args[2] = NULL;
322 check_string (g_build_filenamev (args), S"x");
323 args[0] = S; args[1] = "x"; args[2] = NULL;
324 check_string (g_build_filenamev (args), S"x");
325 args[0] = S S; args[1] = "x"; args[2] = NULL;
326 check_string (g_build_filenamev (args), S S"x");
327 args[0] = "x"; args[1] = ""; args[2] = NULL;
328 check_string (g_build_filenamev (args), "x");
329 args[0] = "x"S; args[1] = ""; args[2] = NULL;
330 check_string (g_build_filenamev (args), "x"S);
331 args[0] = "x"; args[1] = S; args[2] = NULL;
332 check_string (g_build_filenamev (args), "x"S);
333 args[0] = "x"; args[1] = S S; args[2] = NULL;
334 check_string (g_build_filenamev (args), "x"S S);
335 args[0] = "x"; args[1] = "y"; args[2] = NULL;
336 check_string (g_build_filenamev (args), "x"S"y");
337 args[0] = S"x"; args[1] = "y"; args[2] = NULL;
338 check_string (g_build_filenamev (args), S"x"S"y");
339 args[0] = "x"; args[1] = "y"S; args[2] = NULL;
340 check_string (g_build_filenamev (args), "x"S"y"S);
341 args[0] = S"x"S; args[1] = S"y"S; args[2] = NULL;
342 check_string (g_build_filenamev (args), S"x"S"y"S);
343 args[0] = S"x"S S; args[1] = S S"y"S; args[2] = NULL;
344 check_string (g_build_filenamev (args), S"x"S"y"S);
345 args[0] = "x"; args[1] = ""; args[2] = "y"; args[3] = NULL;
346 check_string (g_build_filenamev (args), "x"S"y");
347 args[0] = "x"; args[1] = S; args[2] = "y"; args[3] = NULL;
348 check_string (g_build_filenamev (args), "x"S"y");
349 args[0] = "x"; args[1] = S S; args[2] = "y"; args[3] = NULL;
350 check_string (g_build_filenamev (args), "x"S"y");
351 args[0] = "x"; args[1] = "y"; args[2] = "z"; args[3] = NULL;
352 check_string (g_build_filenamev (args), "x"S"y"S"z");
353 args[0] = S"x"S; args[1] = S"y"S; args[2] = S"z"S; args[3] = NULL;
354 check_string (g_build_filenamev (args), S"x"S"y"S"z"S);
355 args[0] = S S"x"S S; args[1] = S S"y"S S; args[2] = S S"z"S S; args[3] = NULL;
356 check_string (g_build_filenamev (args), S S"x"S"y"S"z"S S);
360 /* Test also using the slash as file name separator */
363 check_string (g_build_filenamev (args), "");
364 args[0] = U; args[1] = NULL;
365 check_string (g_build_filenamev (args), U);
366 args[0] = U"x"; args[1] = NULL;
367 check_string (g_build_filenamev (args), U"x");
368 args[0] = "x"U; args[1] = NULL;
369 check_string (g_build_filenamev (args), "x"U);
370 args[0] = ""; args[1] = U"x"; args[2] = NULL;
371 check_string (g_build_filenamev (args), U"x");
372 args[0] = ""; args[1] = U"x"; args[2] = NULL;
373 check_string (g_build_filenamev (args), U"x");
374 args[0] = U; args[1] = "x"; args[2] = NULL;
375 check_string (g_build_filenamev (args), U"x");
376 args[0] = U U; args[1] = "x"; args[2] = NULL;
377 check_string (g_build_filenamev (args), U U"x");
378 args[0] = U S; args[1] = "x"; args[2] = NULL;
379 check_string (g_build_filenamev (args), U S"x");
380 args[0] = "x"U; args[1] = ""; args[2] = NULL;
381 check_string (g_build_filenamev (args), "x"U);
382 args[0] = "x"S"y"; args[1] = "z"U"a"; args[2] = NULL;
383 check_string (g_build_filenamev (args), "x"S"y"S"z"U"a");
384 args[0] = "x"; args[1] = U; args[2] = NULL;
385 check_string (g_build_filenamev (args), "x"U);
386 args[0] = "x"; args[1] = U U; args[2] = NULL;
387 check_string (g_build_filenamev (args), "x"U U);
388 args[0] = "x"; args[1] = S U; args[2] = NULL;
389 check_string (g_build_filenamev (args), "x"S U);
390 args[0] = U"x"; args[1] = "y"; args[2] = NULL;
391 check_string (g_build_filenamev (args), U"x"U"y");
392 args[0] = "x"; args[1] = "y"U; args[2] = NULL;
393 check_string (g_build_filenamev (args), "x"U"y"U);
394 args[0] = U"x"U; args[1] = U"y"U; args[2] = NULL;
395 check_string (g_build_filenamev (args), U"x"U"y"U);
396 args[0] = U"x"U U; args[1] = U U"y"U; args[2] = NULL;
397 check_string (g_build_filenamev (args), U"x"U"y"U);
398 args[0] = "x"; args[1] = U; args[2] = "y", args[3] = NULL;
399 check_string (g_build_filenamev (args), "x"U"y");
400 args[0] = "x"; args[1] = U U; args[2] = "y", args[3] = NULL;
401 check_string (g_build_filenamev (args), "x"U"y");
402 args[0] = "x"; args[1] = U S; args[2] = "y", args[3] = NULL;
403 check_string (g_build_filenamev (args), "x"S"y");
404 args[0] = "x"; args[1] = S U; args[2] = "y", args[3] = NULL;
405 check_string (g_build_filenamev (args), "x"U"y");
406 args[0] = "x"; args[1] = U "y"; args[2] = "z", args[3] = NULL;
407 check_string (g_build_filenamev (args), "x"U"y"U"z");
408 args[0] = "x"; args[1] = S "y"; args[2] = "z", args[3] = NULL;
409 check_string (g_build_filenamev (args), "x"S"y"S"z");
410 args[0] = "x"; args[1] = S "y"; args[2] = "z", args[3] = U;
411 args[4] = "a"; args[5] = "b"; args[6] = NULL;
412 check_string (g_build_filenamev (args), "x"S"y"S"z"U"a"U"b");
413 args[0] = U"x"U; args[1] = U"y"U; args[2] = U"z"U, args[3] = NULL;
414 check_string (g_build_filenamev (args), U"x"U"y"U"z"U);
415 args[0] = U U"x"U U; args[1] = U U"y"U U; args[2] = U U"z"U U, args[3] = NULL;
416 check_string (g_build_filenamev (args), U U"x"U"y"U"z"U U);
420 #endif /* G_OS_WIN32 */
426 test_mkdir_with_parents_1 (const gchar *base)
428 char *p0 = g_build_filename (base, "fum", NULL);
429 char *p1 = g_build_filename (p0, "tem", NULL);
430 char *p2 = g_build_filename (p1, "zap", NULL);
437 if (g_file_test (p0, G_FILE_TEST_EXISTS))
438 g_error ("failed, %s exists, cannot test g_mkdir_with_parents\n", p0);
440 if (g_file_test (p1, G_FILE_TEST_EXISTS))
441 g_error ("failed, %s exists, cannot test g_mkdir_with_parents\n", p1);
443 if (g_file_test (p2, G_FILE_TEST_EXISTS))
444 g_error ("failed, %s exists, cannot test g_mkdir_with_parents\n", p2);
446 if (g_mkdir_with_parents (p2, 0777) == -1)
449 g_error ("failed, g_mkdir_with_parents(%s) failed: %s\n", p2, g_strerror (errsv));
452 if (!g_file_test (p2, G_FILE_TEST_IS_DIR))
453 g_error ("failed, g_mkdir_with_parents(%s) succeeded, but %s is not a directory\n", p2, p2);
455 if (!g_file_test (p1, G_FILE_TEST_IS_DIR))
456 g_error ("failed, g_mkdir_with_parents(%s) succeeded, but %s is not a directory\n", p2, p1);
458 if (!g_file_test (p0, G_FILE_TEST_IS_DIR))
459 g_error ("failed, g_mkdir_with_parents(%s) succeeded, but %s is not a directory\n", p2, p0);
462 if (g_file_test (p2, G_FILE_TEST_EXISTS))
463 g_error ("failed, did g_rmdir(%s), but %s is still there\n", p2, p2);
466 if (g_file_test (p1, G_FILE_TEST_EXISTS))
467 g_error ("failed, did g_rmdir(%s), but %s is still there\n", p1, p1);
469 f = g_fopen (p1, "w");
471 g_error ("failed, couldn't create file %s\n", p1);
474 if (g_mkdir_with_parents (p1, 0666) == 0)
475 g_error ("failed, g_mkdir_with_parents(%s) succeeded, even if %s is a file\n", p1, p1);
477 if (g_mkdir_with_parents (p2, 0666) == 0)
478 g_error("failed, g_mkdir_with_parents(%s) succeeded, even if %s is a file\n", p2, p1);
490 test_mkdir_with_parents (void)
493 if (g_test_verbose())
494 g_printerr ("checking g_mkdir_with_parents() in subdir ./hum/");
495 test_mkdir_with_parents_1 ("hum");
497 if (g_test_verbose())
498 g_printerr ("checking g_mkdir_with_parents() in subdir ./hii///haa/hee/");
499 test_mkdir_with_parents_1 ("hii///haa/hee");
500 g_remove ("hii/haa/hee");
501 g_remove ("hii/haa");
503 cwd = g_get_current_dir ();
504 if (g_test_verbose())
505 g_printerr ("checking g_mkdir_with_parents() in cwd: %s", cwd);
506 test_mkdir_with_parents_1 (cwd);
509 g_assert_cmpint (g_mkdir_with_parents (NULL, 0), ==, -1);
510 g_assert_cmpint (errno, ==, EINVAL);
514 test_format_size_for_display (void)
517 SetThreadLocale (MAKELCID (MAKELANGID (LANG_ENGLISH, SUBLANG_ENGLISH_US), SORT_DEFAULT));
519 /* nobody called setlocale(), so we should get "C" behaviour... */
520 check_string (g_format_size_for_display (0), "0 bytes");
521 check_string (g_format_size_for_display (1), "1 byte");
522 check_string (g_format_size_for_display (2), "2 bytes");
523 check_string (g_format_size_for_display (1024), "1.0 KB");
524 check_string (g_format_size_for_display (1024 * 1024), "1.0 MB");
525 check_string (g_format_size_for_display (1024 * 1024 * 1024), "1.0 GB");
526 check_string (g_format_size_for_display (1024ULL * 1024 * 1024 * 1024), "1.0 TB");
527 check_string (g_format_size_for_display (1024ULL * 1024 * 1024 * 1024 * 1024), "1.0 PB");
528 check_string (g_format_size_for_display (1024ULL * 1024 * 1024 * 1024 * 1024 * 1024), "1.0 EB");
530 check_string (g_format_size (0), "0 bytes");
531 check_string (g_format_size (1), "1 byte");
532 check_string (g_format_size (2), "2 bytes");
533 check_string (g_format_size (1000ULL), "1.0 kB");
534 check_string (g_format_size (1000ULL * 1000), "1.0 MB");
535 check_string (g_format_size (1000ULL * 1000 * 1000), "1.0 GB");
536 check_string (g_format_size (1000ULL * 1000 * 1000 * 1000), "1.0 TB");
537 check_string (g_format_size (1000ULL * 1000 * 1000 * 1000 * 1000), "1.0 PB");
538 check_string (g_format_size (1000ULL * 1000 * 1000 * 1000 * 1000 * 1000), "1.0 EB");
540 check_string (g_format_size_full (0, G_FORMAT_SIZE_IEC_UNITS), "0 bytes");
541 check_string (g_format_size_full (1, G_FORMAT_SIZE_IEC_UNITS), "1 byte");
542 check_string (g_format_size_full (2, G_FORMAT_SIZE_IEC_UNITS), "2 bytes");
544 check_string (g_format_size_full (2048ULL, G_FORMAT_SIZE_IEC_UNITS), "2.0 KiB");
545 check_string (g_format_size_full (2048ULL * 1024, G_FORMAT_SIZE_IEC_UNITS), "2.0 MiB");
546 check_string (g_format_size_full (2048ULL * 1024 * 1024, G_FORMAT_SIZE_IEC_UNITS), "2.0 GiB");
547 check_string (g_format_size_full (2048ULL * 1024 * 1024 * 1024, G_FORMAT_SIZE_IEC_UNITS), "2.0 TiB");
548 check_string (g_format_size_full (2048ULL * 1024 * 1024 * 1024 * 1024, G_FORMAT_SIZE_IEC_UNITS), "2.0 PiB");
549 check_string (g_format_size_full (2048ULL * 1024 * 1024 * 1024 * 1024 * 1024, G_FORMAT_SIZE_IEC_UNITS), "2.0 EiB");
551 check_string (g_format_size_full (238472938, G_FORMAT_SIZE_IEC_UNITS), "227.4 MiB");
552 check_string (g_format_size_full (238472938, G_FORMAT_SIZE_DEFAULT), "238.5 MB");
553 check_string (g_format_size_full (238472938, G_FORMAT_SIZE_LONG_FORMAT), "238.5 MB (238472938 bytes)");
557 test_file_errors (void)
560 g_assert_cmpint (g_file_error_from_errno (EEXIST), ==, G_FILE_ERROR_EXIST);
563 g_assert_cmpint (g_file_error_from_errno (EISDIR), ==, G_FILE_ERROR_ISDIR);
566 g_assert_cmpint (g_file_error_from_errno (EACCES), ==, G_FILE_ERROR_ACCES);
569 g_assert_cmpint (g_file_error_from_errno (ENAMETOOLONG), ==, G_FILE_ERROR_NAMETOOLONG);
572 g_assert_cmpint (g_file_error_from_errno (ENOENT), ==, G_FILE_ERROR_NOENT);
575 g_assert_cmpint (g_file_error_from_errno (ENOTDIR), ==, G_FILE_ERROR_NOTDIR);
578 g_assert_cmpint (g_file_error_from_errno (ENXIO), ==, G_FILE_ERROR_NXIO);
581 g_assert_cmpint (g_file_error_from_errno (ENODEV), ==, G_FILE_ERROR_NODEV);
584 g_assert_cmpint (g_file_error_from_errno (EROFS), ==, G_FILE_ERROR_ROFS);
587 g_assert_cmpint (g_file_error_from_errno (ETXTBSY), ==, G_FILE_ERROR_TXTBSY);
590 g_assert_cmpint (g_file_error_from_errno (EFAULT), ==, G_FILE_ERROR_FAULT);
593 g_assert_cmpint (g_file_error_from_errno (ELOOP), ==, G_FILE_ERROR_LOOP);
596 g_assert_cmpint (g_file_error_from_errno (ENOSPC), ==, G_FILE_ERROR_NOSPC);
599 g_assert_cmpint (g_file_error_from_errno (ENOMEM), ==, G_FILE_ERROR_NOMEM);
602 g_assert_cmpint (g_file_error_from_errno (EMFILE), ==, G_FILE_ERROR_MFILE);
605 g_assert_cmpint (g_file_error_from_errno (ENFILE), ==, G_FILE_ERROR_NFILE);
608 g_assert_cmpint (g_file_error_from_errno (EBADF), ==, G_FILE_ERROR_BADF);
611 g_assert_cmpint (g_file_error_from_errno (EINVAL), ==, G_FILE_ERROR_INVAL);
614 g_assert_cmpint (g_file_error_from_errno (EPIPE), ==, G_FILE_ERROR_PIPE);
617 g_assert_cmpint (g_file_error_from_errno (EAGAIN), ==, G_FILE_ERROR_AGAIN);
620 g_assert_cmpint (g_file_error_from_errno (EINTR), ==, G_FILE_ERROR_INTR);
623 g_assert_cmpint (g_file_error_from_errno (EIO), ==, G_FILE_ERROR_IO);
626 g_assert_cmpint (g_file_error_from_errno (EPERM), ==, G_FILE_ERROR_PERM);
629 g_assert_cmpint (g_file_error_from_errno (ENOSYS), ==, G_FILE_ERROR_NOSYS);
638 b = g_path_get_basename ("");
639 g_assert_cmpstr (b, ==, ".");
642 b = g_path_get_basename ("///");
643 g_assert_cmpstr (b, ==, G_DIR_SEPARATOR_S);
646 b = g_path_get_basename ("/a/b/c/d");
647 g_assert_cmpstr (b, ==, "d");
652 test_dir_make_tmp (void)
655 GError *error = NULL;
658 name = g_dir_make_tmp ("testXXXXXXtest", &error);
659 g_assert_no_error (error);
660 g_assert_true (g_file_test (name, G_FILE_TEST_IS_DIR));
661 ret = g_rmdir (name);
662 g_assert_cmpint (ret, ==, 0);
665 name = g_dir_make_tmp (NULL, &error);
666 g_assert_no_error (error);
667 g_assert_true (g_file_test (name, G_FILE_TEST_IS_DIR));
668 ret = g_rmdir (name);
669 g_assert_cmpint (ret, ==, 0);
672 name = g_dir_make_tmp ("test/XXXXXX", &error);
673 g_assert_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED);
674 g_clear_error (&error);
675 g_assert_null (name);
677 name = g_dir_make_tmp ("XXXXxX", &error);
678 g_assert_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED);
679 g_clear_error (&error);
680 g_assert_null (name);
684 test_file_open_tmp (void)
687 GError *error = NULL;
690 fd = g_file_open_tmp ("testXXXXXXtest", &name, &error);
691 g_assert_cmpint (fd, !=, -1);
692 g_assert_no_error (error);
693 g_assert_nonnull (name);
698 fd = g_file_open_tmp (NULL, &name, &error);
699 g_assert_cmpint (fd, !=, -1);
700 g_assert_no_error (error);
701 g_assert_nonnull (name);
707 fd = g_file_open_tmp ("test/XXXXXX", &name, &error);
708 g_assert_cmpint (fd, ==, -1);
709 g_assert_null (name);
710 g_assert_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED);
711 g_clear_error (&error);
713 fd = g_file_open_tmp ("XXXXxX", &name, &error);
714 g_assert_cmpint (fd, ==, -1);
715 g_assert_null (name);
716 g_assert_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED);
717 g_clear_error (&error);
726 name = g_strdup ("testXXXXXXtest"),
727 fd = g_mkstemp (name);
728 g_assert_cmpint (fd, !=, -1);
729 g_assert_null (strstr (name, "XXXXXX"));
734 name = g_strdup ("testYYYYYYtest"),
735 fd = g_mkstemp (name);
736 g_assert_cmpint (fd, ==, -1);
746 name = g_strdup ("testXXXXXXtest"),
747 ret = g_mkdtemp (name);
748 g_assert (ret == name);
749 g_assert_null (strstr (name, "XXXXXX"));
753 name = g_strdup ("testYYYYYYtest"),
754 ret = g_mkdtemp (name);
760 test_set_contents (void)
762 GError *error = NULL;
769 fd = g_file_open_tmp (NULL, &name, &error);
770 g_assert_no_error (error);
774 ret = g_file_get_contents (name, &buf, &len, &error);
776 g_assert_no_error (error);
777 g_assert_cmpstr (buf, ==, "a");
780 ret = g_file_set_contents (name, "b", 1, &error);
782 g_assert_no_error (error);
784 ret = g_file_get_contents (name, &buf, &len, &error);
786 g_assert_no_error (error);
787 g_assert_cmpstr (buf, ==, "b");
795 test_read_link (void)
800 const gchar *oldpath;
805 GError *error = NULL;
807 cwd = g_get_current_dir ();
809 oldpath = g_test_get_filename (G_TEST_DIST, "4096-random-bytes", NULL);
810 newpath = g_build_filename (cwd, "page-of-junk", NULL);
811 badpath = g_build_filename (cwd, "4097-random-bytes", NULL);
813 ret = symlink (oldpath, newpath);
814 g_assert_cmpint (ret, ==, 0);
815 path = g_file_read_link (newpath, &error);
816 g_assert_no_error (error);
817 g_assert_cmpstr (path, ==, oldpath);
821 ret = symlink (badpath, newpath);
822 g_assert_cmpint (ret, ==, 0);
823 path = g_file_read_link (newpath, &error);
824 g_assert_no_error (error);
825 g_assert_cmpstr (path, ==, badpath);
828 path = g_file_read_link (oldpath, &error);
829 g_assert_error (error, G_FILE_ERROR, G_FILE_ERROR_INVAL);
830 g_assert_null (path);
831 g_error_free (error);
840 g_test_skip ("Symbolic links not supported");
845 test_stdio_wrappers (void)
851 GError *error = NULL;
853 g_remove ("mkdir-test/test-create");
854 ret = g_rmdir ("mkdir-test");
855 g_assert (ret == 0 || errno == ENOENT);
857 ret = g_stat ("mkdir-test", &buf);
858 g_assert_cmpint (ret, ==, -1);
859 ret = g_mkdir ("mkdir-test", 0666);
860 g_assert_cmpint (ret, ==, 0);
861 ret = g_stat ("mkdir-test", &buf);
862 g_assert_cmpint (ret, ==, 0);
863 g_assert_cmpint (S_ISDIR (buf.st_mode), !=, 0);
865 cwd = g_get_current_dir ();
866 path = g_build_filename (cwd, "mkdir-test", NULL);
868 ret = g_chdir (path);
869 g_assert_cmpint (errno, ==, EACCES);
870 g_assert_cmpint (ret, ==, -1);
871 ret = g_chmod (path, 0777);
872 g_assert_cmpint (ret, ==, 0);
873 ret = g_chdir (path);
874 g_assert_cmpint (ret, ==, 0);
875 cwd = g_get_current_dir ();
876 g_assert_true (g_str_equal (cwd, path));
880 ret = g_creat ("test-creat", 0555);
881 g_close (ret, &error);
882 g_assert_no_error (error);
884 ret = g_access ("test-creat", F_OK);
885 g_assert_cmpint (ret, ==, 0);
887 ret = g_rename ("test-creat", "test-create");
888 g_assert_cmpint (ret, ==, 0);
890 ret = g_open ("test-create", O_RDONLY, 0666);
891 g_close (ret, &error);
892 g_assert_no_error (error);
894 ut.actime = ut.modtime = (time_t)0;
895 ret = g_utime ("test-create", &ut);
896 g_assert_cmpint (ret, ==, 0);
898 ret = g_lstat ("test-create", &buf);
899 g_assert_cmpint (ret, ==, 0);
900 g_assert_cmpint (buf.st_atime, ==, (time_t)0);
901 g_assert_cmpint (buf.st_mtime, ==, (time_t)0);
904 g_remove ("mkdir-test/test-create");
905 g_rmdir ("mkdir-test");
912 g_setenv ("LC_ALL", "C", TRUE);
913 g_test_init (&argc, &argv, NULL);
915 g_test_add_func ("/fileutils/build-path", test_build_path);
916 g_test_add_func ("/fileutils/build-pathv", test_build_pathv);
917 g_test_add_func ("/fileutils/build-filename", test_build_filename);
918 g_test_add_func ("/fileutils/build-filenamev", test_build_filenamev);
919 g_test_add_func ("/fileutils/mkdir-with-parents", test_mkdir_with_parents);
920 g_test_add_func ("/fileutils/format-size-for-display", test_format_size_for_display);
921 g_test_add_func ("/fileutils/errors", test_file_errors);
922 g_test_add_func ("/fileutils/basename", test_basename);
923 g_test_add_func ("/fileutils/dir-make-tmp", test_dir_make_tmp);
924 g_test_add_func ("/fileutils/file-open-tmp", test_file_open_tmp);
925 g_test_add_func ("/fileutils/mkstemp", test_mkstemp);
926 g_test_add_func ("/fileutils/mkdtemp", test_mkdtemp);
927 g_test_add_func ("/fileutils/set-contents", test_set_contents);
928 g_test_add_func ("/fileutils/read-link", test_read_link);
929 g_test_add_func ("/fileutils/stdio-wrappers", test_stdio_wrappers);
931 return g_test_run ();