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.
25 /* We are testing some deprecated APIs here */
26 #define GLIB_DISABLE_DEPRECATION_WARNINGS
29 #include <glib/gstdio.h>
38 #define S G_DIR_SEPARATOR_S
41 check_string (gchar *str, gchar *expected)
43 g_assert (str != NULL);
44 g_assert_cmpstr (str, ==, expected);
49 test_build_path (void)
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");
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::");
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::");
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::::");
113 test_build_pathv (void)
117 g_assert (g_build_pathv ("", NULL) == 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");
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::");
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::::");
234 test_build_filename (void)
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);
262 /* Test also using the slash as file name separator */
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);
294 #endif /* G_OS_WIN32 */
299 test_build_filenamev (void)
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);
352 /* Test also using the slash as file name separator */
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);
412 #endif /* G_OS_WIN32 */
418 test_mkdir_with_parents_1 (const gchar *base)
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);
429 if (g_file_test (p0, G_FILE_TEST_EXISTS))
430 g_error ("failed, %s exists, cannot test g_mkdir_with_parents\n", p0);
432 if (g_file_test (p1, G_FILE_TEST_EXISTS))
433 g_error ("failed, %s exists, cannot test g_mkdir_with_parents\n", p1);
435 if (g_file_test (p2, G_FILE_TEST_EXISTS))
436 g_error ("failed, %s exists, cannot test g_mkdir_with_parents\n", p2);
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));
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);
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);
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);
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);
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);
458 f = g_fopen (p1, "w");
460 g_error ("failed, couldn't create file %s\n", p1);
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);
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);
479 test_mkdir_with_parents (void)
482 if (g_test_verbose())
483 g_print ("checking g_mkdir_with_parents() in subdir ./hum/");
484 test_mkdir_with_parents_1 ("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");
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);
498 g_assert (g_mkdir_with_parents (NULL, 0) == -1);
499 g_assert (errno == EINVAL);
503 test_format_size_for_display (void)
506 SetThreadLocale (MAKELCID (MAKELANGID (LANG_ENGLISH, SUBLANG_ENGLISH_US), SORT_DEFAULT));
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");
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");
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");
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");
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)");
546 test_file_errors (void)
549 g_assert (g_file_error_from_errno (EEXIST) == G_FILE_ERROR_EXIST);
552 g_assert (g_file_error_from_errno (EISDIR) == G_FILE_ERROR_ISDIR);
555 g_assert (g_file_error_from_errno (EACCES) == G_FILE_ERROR_ACCES);
558 g_assert (g_file_error_from_errno (ENAMETOOLONG) == G_FILE_ERROR_NAMETOOLONG);
561 g_assert (g_file_error_from_errno (ENOENT) == G_FILE_ERROR_NOENT);
564 g_assert (g_file_error_from_errno (ENOTDIR) == G_FILE_ERROR_NOTDIR);
567 g_assert (g_file_error_from_errno (ENXIO) == G_FILE_ERROR_NXIO);
570 g_assert (g_file_error_from_errno (ENODEV) == G_FILE_ERROR_NODEV);
573 g_assert (g_file_error_from_errno (EROFS) == G_FILE_ERROR_ROFS);
576 g_assert (g_file_error_from_errno (ETXTBSY) == G_FILE_ERROR_TXTBSY);
579 g_assert (g_file_error_from_errno (EFAULT) == G_FILE_ERROR_FAULT);
582 g_assert (g_file_error_from_errno (ELOOP) == G_FILE_ERROR_LOOP);
585 g_assert (g_file_error_from_errno (ENOSPC) == G_FILE_ERROR_NOSPC);
588 g_assert (g_file_error_from_errno (ENOMEM) == G_FILE_ERROR_NOMEM);
591 g_assert (g_file_error_from_errno (EMFILE) == G_FILE_ERROR_MFILE);
594 g_assert (g_file_error_from_errno (ENFILE) == G_FILE_ERROR_NFILE);
597 g_assert (g_file_error_from_errno (EBADF) == G_FILE_ERROR_BADF);
600 g_assert (g_file_error_from_errno (EINVAL) == G_FILE_ERROR_INVAL);
603 g_assert (g_file_error_from_errno (EPIPE) == G_FILE_ERROR_PIPE);
606 g_assert (g_file_error_from_errno (EAGAIN) == G_FILE_ERROR_AGAIN);
609 g_assert (g_file_error_from_errno (EINTR) == G_FILE_ERROR_INTR);
612 g_assert (g_file_error_from_errno (EIO) == G_FILE_ERROR_IO);
615 g_assert (g_file_error_from_errno (EPERM) == G_FILE_ERROR_PERM);
618 g_assert (g_file_error_from_errno (ENOSYS) == G_FILE_ERROR_NOSYS);
627 b = g_path_get_basename ("");
628 g_assert_cmpstr (b, ==, ".");
631 b = g_path_get_basename ("///");
632 g_assert_cmpstr (b, ==, G_DIR_SEPARATOR_S);
635 b = g_path_get_basename ("/a/b/c/d");
636 g_assert_cmpstr (b, ==, "d");
641 test_dir_make_tmp (void)
644 GError *error = NULL;
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);
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);
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);
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);
670 test_file_open_tmp (void)
673 GError *error = NULL;
676 fd = g_file_open_tmp ("testXXXXXXtest", &name, &error);
678 g_assert_no_error (error);
679 g_assert (name != NULL);
684 fd = g_file_open_tmp (NULL, &name, &error);
686 g_assert_no_error (error);
687 g_assert (name != NULL);
693 fd = g_file_open_tmp ("test/XXXXXX", &name, &error);
695 g_assert (name == NULL);
696 g_assert_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED);
697 g_clear_error (&error);
699 fd = g_file_open_tmp ("XXXXxX", &name, &error);
701 g_assert (name == NULL);
702 g_assert_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED);
703 g_clear_error (&error);
712 name = g_strdup ("testXXXXXXtest"),
713 fd = g_mkstemp (name);
715 g_assert (strstr (name, "XXXXXX") == NULL);
720 name = g_strdup ("testYYYYYYtest"),
721 fd = g_mkstemp (name);
732 name = g_strdup ("testXXXXXXtest"),
733 ret = g_mkdtemp (name);
734 g_assert (ret == name);
735 g_assert (strstr (name, "XXXXXX") == NULL);
739 name = g_strdup ("testYYYYYYtest"),
740 ret = g_mkdtemp (name);
741 g_assert (ret == NULL);
746 test_set_contents (void)
748 GError *error = NULL;
755 fd = g_file_open_tmp (NULL, &name, &error);
756 g_assert_no_error (error);
760 ret = g_file_get_contents (name, &buf, &len, &error);
762 g_assert_no_error (error);
763 g_assert_cmpstr (buf, ==, "a");
766 ret = g_file_set_contents (name, "b", 1, &error);
768 g_assert_no_error (error);
770 ret = g_file_get_contents (name, &buf, &len, &error);
772 g_assert_no_error (error);
773 g_assert_cmpstr (buf, ==, "b");
784 g_test_init (&argc, &argv, NULL);
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);
800 return g_test_run ();