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
30 #include <glib/gstdio.h>
39 #define S G_DIR_SEPARATOR_S
42 check_string (gchar *str, gchar *expected)
44 g_assert (str != NULL);
45 g_assert_cmpstr (str, ==, expected);
50 test_build_path (void)
52 /* check_string (g_build_path ("", NULL), "");*/
53 check_string (g_build_path ("", "", NULL), "");
54 check_string (g_build_path ("", "x", NULL), "x");
55 check_string (g_build_path ("", "x", "y", NULL), "xy");
56 check_string (g_build_path ("", "x", "y", "z", NULL), "xyz");
58 /* check_string (g_build_path (":", NULL), "");*/
59 check_string (g_build_path (":", ":", NULL), ":");
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", "::", NULL), "x::");
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", NULL), "x:y");
78 check_string (g_build_path (":", "x", "y", "z", NULL), "x:y:z");
79 check_string (g_build_path (":", ":x:", ":y:", ":z:", NULL), ":x:y:z:");
80 check_string (g_build_path (":", "::x::", "::y::", "::z::", NULL), "::x:y:z::");
82 /* check_string (g_build_path ("::", NULL), "");*/
83 check_string (g_build_path ("::", "::", NULL), "::");
84 check_string (g_build_path ("::", ":::", NULL), ":::");
85 check_string (g_build_path ("::", "::x", NULL), "::x");
86 check_string (g_build_path ("::", "x::", NULL), "x::");
87 check_string (g_build_path ("::", "", "x", NULL), "x");
88 check_string (g_build_path ("::", "", "::x", NULL), "::x");
89 check_string (g_build_path ("::", "::", "x", NULL), "::x");
90 check_string (g_build_path ("::", "::::", "x", NULL), "::::x");
91 check_string (g_build_path ("::", "x", "", NULL), "x");
92 check_string (g_build_path ("::", "x::", "", NULL), "x::");
93 check_string (g_build_path ("::", "x", "::", NULL), "x::");
95 /* This following is weird, but keeps the definition simple */
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", "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", NULL), "x::y");
107 check_string (g_build_path ("::", "x", "y", "z", NULL), "x::y::z");
108 check_string (g_build_path ("::", "::x::", "::y::", "::z::", NULL), "::x::y::z::");
109 check_string (g_build_path ("::", ":::x:::", ":::y:::", ":::z:::", NULL), ":::x::::y::::z:::");
110 check_string (g_build_path ("::", "::::x::::", "::::y::::", "::::z::::", NULL), "::::x::y::z::::");
114 test_build_pathv (void)
118 g_assert (g_build_pathv ("", NULL) == NULL);
120 check_string (g_build_pathv ("", args), "");
121 args[0] = ""; args[1] = NULL;
122 check_string (g_build_pathv ("", args), "");
123 args[0] = "x"; args[1] = NULL;
124 check_string (g_build_pathv ("", args), "x");
125 args[0] = "x"; args[1] = "y"; args[2] = NULL;
126 check_string (g_build_pathv ("", args), "xy");
127 args[0] = "x"; args[1] = "y"; args[2] = "z", args[3] = NULL;
128 check_string (g_build_pathv ("", args), "xyz");
131 check_string (g_build_pathv (":", args), "");
132 args[0] = ":"; args[1] = NULL;
133 check_string (g_build_pathv (":", args), ":");
134 args[0] = ":x"; args[1] = NULL;
135 check_string (g_build_pathv (":", args), ":x");
136 args[0] = "x:"; args[1] = NULL;
137 check_string (g_build_pathv (":", args), "x:");
138 args[0] = ""; args[1] = "x"; args[2] = NULL;
139 check_string (g_build_pathv (":", args), "x");
140 args[0] = ""; args[1] = ":x"; args[2] = NULL;
141 check_string (g_build_pathv (":", args), ":x");
142 args[0] = ":"; args[1] = "x"; args[2] = NULL;
143 check_string (g_build_pathv (":", args), ":x");
144 args[0] = "::"; args[1] = "x"; args[2] = NULL;
145 check_string (g_build_pathv (":", args), "::x");
146 args[0] = "x"; args[1] = ""; args[2] = NULL;
147 check_string (g_build_pathv (":", args), "x");
148 args[0] = "x:"; args[1] = ""; args[2] = NULL;
149 check_string (g_build_pathv (":", args), "x:");
150 args[0] = "x"; args[1] = ":"; args[2] = NULL;
151 check_string (g_build_pathv (":", args), "x:");
152 args[0] = "x"; args[1] = "::"; args[2] = NULL;
153 check_string (g_build_pathv (":", args), "x::");
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] = "y"; args[2] = NULL;
157 check_string (g_build_pathv (":", args), ":x:y");
158 args[0] = "x"; args[1] = "y:"; args[2] = NULL;
159 check_string (g_build_pathv (":", args), "x:y:");
160 args[0] = ":x:"; args[1] = ":y:"; args[2] = NULL;
161 check_string (g_build_pathv (":", args), ":x:y:");
162 args[0] = ":x::"; args[1] = "::y:"; args[2] = NULL;
163 check_string (g_build_pathv (":", args), ":x:y:");
164 args[0] = "x"; args[1] = ""; args[2] = "y"; args[3] = NULL;
165 check_string (g_build_pathv (":", args), "x:y");
166 args[0] = "x"; args[1] = ":"; args[2] = "y"; args[3] = NULL;
167 check_string (g_build_pathv (":", args), "x:y");
168 args[0] = "x"; args[1] = "::"; args[2] = "y"; args[3] = NULL;
169 check_string (g_build_pathv (":", args), "x:y");
170 args[0] = "x"; args[1] = "y"; args[2] = "z"; args[3] = NULL;
171 check_string (g_build_pathv (":", args), "x:y:z");
172 args[0] = ":x:"; args[1] = ":y:"; args[2] = ":z:"; args[3] = NULL;
173 check_string (g_build_pathv (":", args), ":x:y:z:");
174 args[0] = "::x::"; args[1] = "::y::"; args[2] = "::z::"; args[3] = NULL;
175 check_string (g_build_pathv (":", args), "::x:y:z::");
178 check_string (g_build_pathv ("::", args), "");
179 args[0] = "::"; args[1] = NULL;
180 check_string (g_build_pathv ("::", args), "::");
181 args[0] = ":::"; args[1] = NULL;
182 check_string (g_build_pathv ("::", args), ":::");
183 args[0] = "::x"; args[1] = NULL;
184 check_string (g_build_pathv ("::", args), "::x");
185 args[0] = "x::"; args[1] = NULL;
186 check_string (g_build_pathv ("::", args), "x::");
187 args[0] = ""; args[1] = "x"; args[2] = NULL;
188 check_string (g_build_pathv ("::", args), "x");
189 args[0] = ""; args[1] = "::x"; args[2] = NULL;
190 check_string (g_build_pathv ("::", args), "::x");
191 args[0] = "::"; args[1] = "x"; args[2] = NULL;
192 check_string (g_build_pathv ("::", args), "::x");
193 args[0] = "::::"; args[1] = "x"; args[2] = NULL;
194 check_string (g_build_pathv ("::", args), "::::x");
195 args[0] = "x"; args[1] = ""; args[2] = NULL;
196 check_string (g_build_pathv ("::", args), "x");
197 args[0] = "x::"; args[1] = ""; args[2] = NULL;
198 check_string (g_build_pathv ("::", args), "x::");
199 args[0] = "x"; args[1] = "::"; args[2] = NULL;
200 check_string (g_build_pathv ("::", args), "x::");
201 /* This following is weird, but keeps the definition simple */
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] = "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] = "y::"; args[2] = NULL;
211 check_string (g_build_pathv ("::", args), "x::y::");
212 args[0] = "::x::"; args[1] = "::y::"; args[2] = NULL;
213 check_string (g_build_pathv ("::", args), "::x::y::");
214 args[0] = "::x:::"; args[1] = ":::y::"; args[2] = NULL;
215 check_string (g_build_pathv ("::", args), "::x::::y::");
216 args[0] = "::x::::"; args[1] = "::::y::"; args[2] = NULL;
217 check_string (g_build_pathv ("::", args), "::x::y::");
218 args[0] = "x"; args[1] = ""; args[2] = "y"; args[3] = NULL;
219 check_string (g_build_pathv ("::", args), "x::y");
220 args[0] = "x"; args[1] = "::"; args[2] = "y"; args[3] = NULL;
221 check_string (g_build_pathv ("::", args), "x::y");
222 args[0] = "x"; args[1] = "::::"; args[2] = "y"; args[3] = NULL;
223 check_string (g_build_pathv ("::", args), "x::y");
224 args[0] = "x"; args[1] = "y"; args[2] = "z"; args[3] = NULL;
225 check_string (g_build_pathv ("::", args), "x::y::z");
226 args[0] = "::x::"; args[1] = "::y::"; args[2] = "::z::"; args[3] = NULL;
227 check_string (g_build_pathv ("::", args), "::x::y::z::");
228 args[0] = ":::x:::"; args[1] = ":::y:::"; args[2] = ":::z:::"; args[3] = NULL;
229 check_string (g_build_pathv ("::", args), ":::x::::y::::z:::");
230 args[0] = "::::x::::"; args[1] = "::::y::::"; args[2] = "::::z::::"; args[3] = NULL;
231 check_string (g_build_pathv ("::", args), "::::x::y::z::::");
235 test_build_filename (void)
237 /* check_string (g_build_filename (NULL), "");*/
238 check_string (g_build_filename (S, NULL), S);
239 check_string (g_build_filename (S"x", NULL), S"x");
240 check_string (g_build_filename ("x"S, NULL), "x"S);
241 check_string (g_build_filename ("", "x", NULL), "x");
242 check_string (g_build_filename ("", S"x", NULL), S"x");
243 check_string (g_build_filename (S, "x", NULL), S"x");
244 check_string (g_build_filename (S S, "x", NULL), S S"x");
245 check_string (g_build_filename ("x", "", NULL), "x");
246 check_string (g_build_filename ("x"S, "", NULL), "x"S);
247 check_string (g_build_filename ("x", S, NULL), "x"S);
248 check_string (g_build_filename ("x", S S, NULL), "x"S S);
249 check_string (g_build_filename ("x", "y", NULL), "x"S"y");
250 check_string (g_build_filename (S"x", "y", NULL), S"x"S"y");
251 check_string (g_build_filename ("x", "y"S, NULL), "x"S"y"S);
252 check_string (g_build_filename (S"x"S, S"y"S, NULL), S"x"S"y"S);
253 check_string (g_build_filename (S"x"S S, S S"y"S, NULL), S"x"S"y"S);
254 check_string (g_build_filename ("x", "", "y", NULL), "x"S"y");
255 check_string (g_build_filename ("x", S, "y", NULL), "x"S"y");
256 check_string (g_build_filename ("x", S S, "y", NULL), "x"S"y");
257 check_string (g_build_filename ("x", "y", "z", NULL), "x"S"y"S"z");
258 check_string (g_build_filename (S"x"S, S"y"S, S"z"S, NULL), S"x"S"y"S"z"S);
259 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);
263 /* Test also using the slash as file name separator */
265 /* check_string (g_build_filename (NULL), ""); */
266 check_string (g_build_filename (U, NULL), U);
267 check_string (g_build_filename (U"x", NULL), U"x");
268 check_string (g_build_filename ("x"U, NULL), "x"U);
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, "x", NULL), U"x");
272 check_string (g_build_filename (U U, "x", NULL), U U"x");
273 check_string (g_build_filename (U S, "x", NULL), U S"x");
274 check_string (g_build_filename ("x"U, "", NULL), "x"U);
275 check_string (g_build_filename ("x"S"y", "z"U"a", NULL), "x"S"y"S"z"U"a");
276 check_string (g_build_filename ("x", U, NULL), "x"U);
277 check_string (g_build_filename ("x", U U, NULL), "x"U U);
278 check_string (g_build_filename ("x", S U, NULL), "x"S U);
279 check_string (g_build_filename (U"x", "y", NULL), U"x"U"y");
280 check_string (g_build_filename ("x", "y"U, NULL), "x"U"y"U);
281 check_string (g_build_filename (U"x"U, U"y"U, NULL), U"x"U"y"U);
282 check_string (g_build_filename (U"x"U U, U U"y"U, NULL), U"x"U"y"U);
283 check_string (g_build_filename ("x", U, "y", NULL), "x"U"y");
284 check_string (g_build_filename ("x", U U, "y", NULL), "x"U"y");
285 check_string (g_build_filename ("x", U S, "y", NULL), "x"S"y");
286 check_string (g_build_filename ("x", S U, "y", NULL), "x"U"y");
287 check_string (g_build_filename ("x", U "y", "z", NULL), "x"U"y"U"z");
288 check_string (g_build_filename ("x", S "y", "z", NULL), "x"S"y"S"z");
289 check_string (g_build_filename ("x", S "y", "z", U, "a", "b", NULL), "x"S"y"S"z"U"a"U"b");
290 check_string (g_build_filename (U"x"U, U"y"U, U"z"U, NULL), U"x"U"y"U"z"U);
291 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);
295 #endif /* G_OS_WIN32 */
300 test_build_filenamev (void)
305 check_string (g_build_filenamev (args), "");
306 args[0] = S; args[1] = NULL;
307 check_string (g_build_filenamev (args), S);
308 args[0] = S"x"; args[1] = NULL;
309 check_string (g_build_filenamev (args), S"x");
310 args[0] = "x"S; args[1] = NULL;
311 check_string (g_build_filenamev (args), "x"S);
312 args[0] = ""; args[1] = "x"; args[2] = NULL;
313 check_string (g_build_filenamev (args), "x");
314 args[0] = ""; args[1] = S"x"; args[2] = NULL;
315 check_string (g_build_filenamev (args), S"x");
316 args[0] = S; args[1] = "x"; args[2] = NULL;
317 check_string (g_build_filenamev (args), S"x");
318 args[0] = S S; args[1] = "x"; args[2] = NULL;
319 check_string (g_build_filenamev (args), S S"x");
320 args[0] = "x"; args[1] = ""; args[2] = NULL;
321 check_string (g_build_filenamev (args), "x");
322 args[0] = "x"S; args[1] = ""; args[2] = NULL;
323 check_string (g_build_filenamev (args), "x"S);
324 args[0] = "x"; args[1] = S; args[2] = NULL;
325 check_string (g_build_filenamev (args), "x"S);
326 args[0] = "x"; args[1] = S S; args[2] = NULL;
327 check_string (g_build_filenamev (args), "x"S S);
328 args[0] = "x"; args[1] = "y"; args[2] = NULL;
329 check_string (g_build_filenamev (args), "x"S"y");
330 args[0] = S"x"; args[1] = "y"; args[2] = NULL;
331 check_string (g_build_filenamev (args), S"x"S"y");
332 args[0] = "x"; args[1] = "y"S; args[2] = NULL;
333 check_string (g_build_filenamev (args), "x"S"y"S);
334 args[0] = S"x"S; args[1] = S"y"S; args[2] = NULL;
335 check_string (g_build_filenamev (args), S"x"S"y"S);
336 args[0] = S"x"S S; args[1] = S S"y"S; args[2] = NULL;
337 check_string (g_build_filenamev (args), S"x"S"y"S);
338 args[0] = "x"; args[1] = ""; args[2] = "y"; args[3] = NULL;
339 check_string (g_build_filenamev (args), "x"S"y");
340 args[0] = "x"; args[1] = S; args[2] = "y"; args[3] = NULL;
341 check_string (g_build_filenamev (args), "x"S"y");
342 args[0] = "x"; args[1] = S S; args[2] = "y"; args[3] = NULL;
343 check_string (g_build_filenamev (args), "x"S"y");
344 args[0] = "x"; args[1] = "y"; args[2] = "z"; args[3] = NULL;
345 check_string (g_build_filenamev (args), "x"S"y"S"z");
346 args[0] = S"x"S; args[1] = S"y"S; args[2] = S"z"S; args[3] = NULL;
347 check_string (g_build_filenamev (args), S"x"S"y"S"z"S);
348 args[0] = S S"x"S S; args[1] = S S"y"S S; args[2] = S S"z"S S; args[3] = NULL;
349 check_string (g_build_filenamev (args), S S"x"S"y"S"z"S S);
353 /* Test also using the slash as file name separator */
356 check_string (g_build_filenamev (args), "");
357 args[0] = U; args[1] = NULL;
358 check_string (g_build_filenamev (args), U);
359 args[0] = U"x"; args[1] = NULL;
360 check_string (g_build_filenamev (args), U"x");
361 args[0] = "x"U; args[1] = NULL;
362 check_string (g_build_filenamev (args), "x"U);
363 args[0] = ""; args[1] = U"x"; args[2] = NULL;
364 check_string (g_build_filenamev (args), U"x");
365 args[0] = ""; args[1] = U"x"; args[2] = NULL;
366 check_string (g_build_filenamev (args), U"x");
367 args[0] = U; args[1] = "x"; args[2] = NULL;
368 check_string (g_build_filenamev (args), U"x");
369 args[0] = U U; args[1] = "x"; args[2] = NULL;
370 check_string (g_build_filenamev (args), U U"x");
371 args[0] = U S; args[1] = "x"; args[2] = NULL;
372 check_string (g_build_filenamev (args), U S"x");
373 args[0] = "x"U; args[1] = ""; args[2] = NULL;
374 check_string (g_build_filenamev (args), "x"U);
375 args[0] = "x"S"y"; args[1] = "z"U"a"; args[2] = NULL;
376 check_string (g_build_filenamev (args), "x"S"y"S"z"U"a");
377 args[0] = "x"; args[1] = U; args[2] = NULL;
378 check_string (g_build_filenamev (args), "x"U);
379 args[0] = "x"; args[1] = U U; args[2] = NULL;
380 check_string (g_build_filenamev (args), "x"U U);
381 args[0] = "x"; args[1] = S U; args[2] = NULL;
382 check_string (g_build_filenamev (args), "x"S U);
383 args[0] = U"x"; args[1] = "y"; args[2] = NULL;
384 check_string (g_build_filenamev (args), U"x"U"y");
385 args[0] = "x"; args[1] = "y"U; args[2] = NULL;
386 check_string (g_build_filenamev (args), "x"U"y"U);
387 args[0] = U"x"U; args[1] = U"y"U; args[2] = NULL;
388 check_string (g_build_filenamev (args), U"x"U"y"U);
389 args[0] = U"x"U U; args[1] = U U"y"U; args[2] = NULL;
390 check_string (g_build_filenamev (args), U"x"U"y"U);
391 args[0] = "x"; args[1] = U; args[2] = "y", args[3] = NULL;
392 check_string (g_build_filenamev (args), "x"U"y");
393 args[0] = "x"; args[1] = U U; args[2] = "y", args[3] = NULL;
394 check_string (g_build_filenamev (args), "x"U"y");
395 args[0] = "x"; args[1] = U S; args[2] = "y", args[3] = NULL;
396 check_string (g_build_filenamev (args), "x"S"y");
397 args[0] = "x"; args[1] = S U; args[2] = "y", args[3] = NULL;
398 check_string (g_build_filenamev (args), "x"U"y");
399 args[0] = "x"; args[1] = U "y"; args[2] = "z", args[3] = NULL;
400 check_string (g_build_filenamev (args), "x"U"y"U"z");
401 args[0] = "x"; args[1] = S "y"; args[2] = "z", args[3] = NULL;
402 check_string (g_build_filenamev (args), "x"S"y"S"z");
403 args[0] = "x"; args[1] = S "y"; args[2] = "z", args[3] = U;
404 args[4] = "a"; args[5] = "b"; args[6] = NULL;
405 check_string (g_build_filenamev (args), "x"S"y"S"z"U"a"U"b");
406 args[0] = U"x"U; args[1] = U"y"U; args[2] = U"z"U, args[3] = NULL;
407 check_string (g_build_filenamev (args), U"x"U"y"U"z"U);
408 args[0] = U U"x"U U; args[1] = U U"y"U U; args[2] = U U"z"U U, args[3] = NULL;
409 check_string (g_build_filenamev (args), U U"x"U"y"U"z"U U);
413 #endif /* G_OS_WIN32 */
419 test_mkdir_with_parents_1 (const gchar *base)
421 char *p0 = g_build_filename (base, "fum", NULL);
422 char *p1 = g_build_filename (p0, "tem", NULL);
423 char *p2 = g_build_filename (p1, "zap", NULL);
430 if (g_file_test (p0, G_FILE_TEST_EXISTS))
431 g_error ("failed, %s exists, cannot test g_mkdir_with_parents\n", p0);
433 if (g_file_test (p1, G_FILE_TEST_EXISTS))
434 g_error ("failed, %s exists, cannot test g_mkdir_with_parents\n", p1);
436 if (g_file_test (p2, G_FILE_TEST_EXISTS))
437 g_error ("failed, %s exists, cannot test g_mkdir_with_parents\n", p2);
439 if (g_mkdir_with_parents (p2, 0777) == -1)
440 g_error ("failed, g_mkdir_with_parents(%s) failed: %s\n", p2, g_strerror (errno));
442 if (!g_file_test (p2, G_FILE_TEST_IS_DIR))
443 g_error ("failed, g_mkdir_with_parents(%s) succeeded, but %s is not a directory\n", p2, p2);
445 if (!g_file_test (p1, G_FILE_TEST_IS_DIR))
446 g_error ("failed, g_mkdir_with_parents(%s) succeeded, but %s is not a directory\n", p2, p1);
448 if (!g_file_test (p0, G_FILE_TEST_IS_DIR))
449 g_error ("failed, g_mkdir_with_parents(%s) succeeded, but %s is not a directory\n", p2, p0);
452 if (g_file_test (p2, G_FILE_TEST_EXISTS))
453 g_error ("failed, did g_rmdir(%s), but %s is still there\n", p2, p2);
456 if (g_file_test (p1, G_FILE_TEST_EXISTS))
457 g_error ("failed, did g_rmdir(%s), but %s is still there\n", p1, p1);
459 f = g_fopen (p1, "w");
461 g_error ("failed, couldn't create file %s\n", p1);
464 if (g_mkdir_with_parents (p1, 0666) == 0)
465 g_error ("failed, g_mkdir_with_parents(%s) succeeded, even if %s is a file\n", p1, p1);
467 if (g_mkdir_with_parents (p2, 0666) == 0)
468 g_error("failed, g_mkdir_with_parents(%s) succeeded, even if %s is a file\n", p2, p1);
480 test_mkdir_with_parents (void)
483 if (g_test_verbose())
484 g_print ("checking g_mkdir_with_parents() in subdir ./hum/");
485 test_mkdir_with_parents_1 ("hum");
487 if (g_test_verbose())
488 g_print ("checking g_mkdir_with_parents() in subdir ./hii///haa/hee/");
489 test_mkdir_with_parents_1 ("hii///haa/hee");
490 g_remove ("hii/haa/hee");
491 g_remove ("hii/haa");
493 cwd = g_get_current_dir ();
494 if (g_test_verbose())
495 g_print ("checking g_mkdir_with_parents() in cwd: %s", cwd);
496 test_mkdir_with_parents_1 (cwd);
499 g_assert (g_mkdir_with_parents (NULL, 0) == -1);
500 g_assert (errno == EINVAL);
504 test_format_size_for_display (void)
507 SetThreadLocale (MAKELCID (MAKELANGID (LANG_ENGLISH, SUBLANG_ENGLISH_US), SORT_DEFAULT));
509 /* nobody called setlocale(), so we should get "C" behaviour... */
510 check_string (g_format_size_for_display (0), "0 bytes");
511 check_string (g_format_size_for_display (1), "1 byte");
512 check_string (g_format_size_for_display (2), "2 bytes");
513 check_string (g_format_size_for_display (1024), "1.0 KB");
514 check_string (g_format_size_for_display (1024 * 1024), "1.0 MB");
515 check_string (g_format_size_for_display (1024 * 1024 * 1024), "1.0 GB");
516 check_string (g_format_size_for_display (1024ULL * 1024 * 1024 * 1024), "1.0 TB");
517 check_string (g_format_size_for_display (1024ULL * 1024 * 1024 * 1024 * 1024), "1.0 PB");
518 check_string (g_format_size_for_display (1024ULL * 1024 * 1024 * 1024 * 1024 * 1024), "1.0 EB");
520 check_string (g_format_size (0), "0 bytes");
521 check_string (g_format_size (1), "1 byte");
522 check_string (g_format_size (2), "2 bytes");
523 check_string (g_format_size (1000ULL), "1.0 kB");
524 check_string (g_format_size (1000ULL * 1000), "1.0 MB");
525 check_string (g_format_size (1000ULL * 1000 * 1000), "1.0 GB");
526 check_string (g_format_size (1000ULL * 1000 * 1000 * 1000), "1.0 TB");
527 check_string (g_format_size (1000ULL * 1000 * 1000 * 1000 * 1000), "1.0 PB");
528 check_string (g_format_size (1000ULL * 1000 * 1000 * 1000 * 1000 * 1000), "1.0 EB");
530 check_string (g_format_size_full (0, G_FORMAT_SIZE_IEC_UNITS), "0 bytes");
531 check_string (g_format_size_full (1, G_FORMAT_SIZE_IEC_UNITS), "1 byte");
532 check_string (g_format_size_full (2, G_FORMAT_SIZE_IEC_UNITS), "2 bytes");
534 check_string (g_format_size_full (2048ULL, G_FORMAT_SIZE_IEC_UNITS), "2.0 KiB");
535 check_string (g_format_size_full (2048ULL * 1024, G_FORMAT_SIZE_IEC_UNITS), "2.0 MiB");
536 check_string (g_format_size_full (2048ULL * 1024 * 1024, G_FORMAT_SIZE_IEC_UNITS), "2.0 GiB");
537 check_string (g_format_size_full (2048ULL * 1024 * 1024 * 1024, G_FORMAT_SIZE_IEC_UNITS), "2.0 TiB");
538 check_string (g_format_size_full (2048ULL * 1024 * 1024 * 1024 * 1024, G_FORMAT_SIZE_IEC_UNITS), "2.0 PiB");
539 check_string (g_format_size_full (2048ULL * 1024 * 1024 * 1024 * 1024 * 1024, G_FORMAT_SIZE_IEC_UNITS), "2.0 EiB");
541 check_string (g_format_size_full (238472938, G_FORMAT_SIZE_IEC_UNITS), "227.4 MiB");
542 check_string (g_format_size_full (238472938, G_FORMAT_SIZE_DEFAULT), "238.5 MB");
543 check_string (g_format_size_full (238472938, G_FORMAT_SIZE_LONG_FORMAT), "238.5 MB (238472938 bytes)");
547 test_file_errors (void)
550 g_assert (g_file_error_from_errno (EEXIST) == G_FILE_ERROR_EXIST);
553 g_assert (g_file_error_from_errno (EISDIR) == G_FILE_ERROR_ISDIR);
556 g_assert (g_file_error_from_errno (EACCES) == G_FILE_ERROR_ACCES);
559 g_assert (g_file_error_from_errno (ENAMETOOLONG) == G_FILE_ERROR_NAMETOOLONG);
562 g_assert (g_file_error_from_errno (ENOENT) == G_FILE_ERROR_NOENT);
565 g_assert (g_file_error_from_errno (ENOTDIR) == G_FILE_ERROR_NOTDIR);
568 g_assert (g_file_error_from_errno (ENXIO) == G_FILE_ERROR_NXIO);
571 g_assert (g_file_error_from_errno (ENODEV) == G_FILE_ERROR_NODEV);
574 g_assert (g_file_error_from_errno (EROFS) == G_FILE_ERROR_ROFS);
577 g_assert (g_file_error_from_errno (ETXTBSY) == G_FILE_ERROR_TXTBSY);
580 g_assert (g_file_error_from_errno (EFAULT) == G_FILE_ERROR_FAULT);
583 g_assert (g_file_error_from_errno (ELOOP) == G_FILE_ERROR_LOOP);
586 g_assert (g_file_error_from_errno (ENOSPC) == G_FILE_ERROR_NOSPC);
589 g_assert (g_file_error_from_errno (ENOMEM) == G_FILE_ERROR_NOMEM);
592 g_assert (g_file_error_from_errno (EMFILE) == G_FILE_ERROR_MFILE);
595 g_assert (g_file_error_from_errno (ENFILE) == G_FILE_ERROR_NFILE);
598 g_assert (g_file_error_from_errno (EBADF) == G_FILE_ERROR_BADF);
601 g_assert (g_file_error_from_errno (EINVAL) == G_FILE_ERROR_INVAL);
604 g_assert (g_file_error_from_errno (EPIPE) == G_FILE_ERROR_PIPE);
607 g_assert (g_file_error_from_errno (EAGAIN) == G_FILE_ERROR_AGAIN);
610 g_assert (g_file_error_from_errno (EINTR) == G_FILE_ERROR_INTR);
613 g_assert (g_file_error_from_errno (EIO) == G_FILE_ERROR_IO);
616 g_assert (g_file_error_from_errno (EPERM) == G_FILE_ERROR_PERM);
619 g_assert (g_file_error_from_errno (ENOSYS) == G_FILE_ERROR_NOSYS);
628 b = g_path_get_basename ("");
629 g_assert_cmpstr (b, ==, ".");
632 b = g_path_get_basename ("///");
633 g_assert_cmpstr (b, ==, G_DIR_SEPARATOR_S);
636 b = g_path_get_basename ("/a/b/c/d");
637 g_assert_cmpstr (b, ==, "d");
642 test_dir_make_tmp (void)
645 GError *error = NULL;
647 name = g_dir_make_tmp ("testXXXXXXtest", &error);
648 g_assert_no_error (error);
649 g_assert (g_file_test (name, G_FILE_TEST_IS_DIR));
650 g_assert (g_rmdir (name) == 0);
653 name = g_dir_make_tmp (NULL, &error);
654 g_assert_no_error (error);
655 g_assert (g_file_test (name, G_FILE_TEST_IS_DIR));
656 g_assert (g_rmdir (name) == 0);
659 name = g_dir_make_tmp ("test/XXXXXX", &error);
660 g_assert_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED);
661 g_clear_error (&error);
662 g_assert (name == NULL);
664 name = g_dir_make_tmp ("XXXXxX", &error);
665 g_assert_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED);
666 g_clear_error (&error);
667 g_assert (name == NULL);
671 test_file_open_tmp (void)
674 GError *error = NULL;
677 fd = g_file_open_tmp ("testXXXXXXtest", &name, &error);
679 g_assert_no_error (error);
680 g_assert (name != NULL);
685 fd = g_file_open_tmp (NULL, &name, &error);
687 g_assert_no_error (error);
688 g_assert (name != NULL);
694 fd = g_file_open_tmp ("test/XXXXXX", &name, &error);
696 g_assert (name == NULL);
697 g_assert_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED);
698 g_clear_error (&error);
700 fd = g_file_open_tmp ("XXXXxX", &name, &error);
702 g_assert (name == NULL);
703 g_assert_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED);
704 g_clear_error (&error);
713 name = g_strdup ("testXXXXXXtest"),
714 fd = g_mkstemp (name);
716 g_assert (strstr (name, "XXXXXX") == NULL);
721 name = g_strdup ("testYYYYYYtest"),
722 fd = g_mkstemp (name);
733 name = g_strdup ("testXXXXXXtest"),
734 ret = g_mkdtemp (name);
735 g_assert (ret == name);
736 g_assert (strstr (name, "XXXXXX") == NULL);
740 name = g_strdup ("testYYYYYYtest"),
741 ret = g_mkdtemp (name);
742 g_assert (ret == NULL);
747 test_set_contents (void)
749 GError *error = NULL;
756 fd = g_file_open_tmp (NULL, &name, &error);
757 g_assert_no_error (error);
761 ret = g_file_get_contents (name, &buf, &len, &error);
763 g_assert_no_error (error);
764 g_assert_cmpstr (buf, ==, "a");
767 ret = g_file_set_contents (name, "b", 1, &error);
769 g_assert_no_error (error);
771 ret = g_file_get_contents (name, &buf, &len, &error);
773 g_assert_no_error (error);
774 g_assert_cmpstr (buf, ==, "b");
782 test_read_link (void)
787 const gchar *oldpath;
788 const gchar *newpath;
789 const gchar *badpath;
791 GError *error = NULL;
793 oldpath = g_test_get_filename (G_TEST_DIST, "4096-random-bytes", NULL);
794 newpath = g_test_get_filename (G_TEST_DIST, "page-of-junk", NULL);
795 badpath = g_test_get_filename (G_TEST_DIST, "4097-random-bytes", NULL);
797 ret = symlink (oldpath, newpath);
799 path = g_file_read_link (newpath, &error);
800 g_assert_no_error (error);
801 g_assert_cmpstr (path, ==, oldpath);
805 ret = symlink (badpath, newpath);
807 path = g_file_read_link (newpath, &error);
808 g_assert_no_error (error);
809 g_assert_cmpstr (path, ==, badpath);
812 path = g_file_read_link (oldpath, &error);
813 g_assert_error (error, G_FILE_ERROR, G_FILE_ERROR_INVAL);
814 g_assert_null (path);
818 g_test_skip ("Symbolic links not supported");
826 g_test_init (&argc, &argv, NULL);
828 g_test_add_func ("/fileutils/build-path", test_build_path);
829 g_test_add_func ("/fileutils/build-pathv", test_build_pathv);
830 g_test_add_func ("/fileutils/build-filename", test_build_filename);
831 g_test_add_func ("/fileutils/build-filenamev", test_build_filenamev);
832 g_test_add_func ("/fileutils/mkdir-with-parents", test_mkdir_with_parents);
833 g_test_add_func ("/fileutils/format-size-for-display", test_format_size_for_display);
834 g_test_add_func ("/fileutils/errors", test_file_errors);
835 g_test_add_func ("/fileutils/basename", test_basename);
836 g_test_add_func ("/fileutils/dir-make-tmp", test_dir_make_tmp);
837 g_test_add_func ("/fileutils/file-open-tmp", test_file_open_tmp);
838 g_test_add_func ("/fileutils/mkstemp", test_mkstemp);
839 g_test_add_func ("/fileutils/mkdtemp", test_mkdtemp);
840 g_test_add_func ("/fileutils/set-contents", test_set_contents);
841 g_test_add_func ("/fileutils/read-link", test_read_link);
843 return g_test_run ();