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>
32 #define S G_DIR_SEPARATOR_S
35 check_string (gchar *str, gchar *expected)
37 g_assert (str != NULL);
38 g_assert_cmpstr (str, ==, expected);
43 test_build_path (void)
45 /* check_string (g_build_path ("", NULL), "");*/
46 check_string (g_build_path ("", "", NULL), "");
47 check_string (g_build_path ("", "x", NULL), "x");
48 check_string (g_build_path ("", "x", "y", NULL), "xy");
49 check_string (g_build_path ("", "x", "y", "z", NULL), "xyz");
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:", NULL), "x:");
55 check_string (g_build_path (":", "", "x", NULL), "x");
56 check_string (g_build_path (":", "", ":x", NULL), ":x");
57 check_string (g_build_path (":", ":", "x", NULL), ":x");
58 check_string (g_build_path (":", "::", "x", NULL), "::x");
59 check_string (g_build_path (":", "x", "", NULL), "x");
60 check_string (g_build_path (":", "x:", "", NULL), "x:");
61 check_string (g_build_path (":", "x", ":", NULL), "x:");
62 check_string (g_build_path (":", "x", "::", NULL), "x::");
63 check_string (g_build_path (":", "x", "y", NULL), "x:y");
64 check_string (g_build_path (":", ":x", "y", NULL), ":x:y");
65 check_string (g_build_path (":", "x", "y:", NULL), "x:y:");
66 check_string (g_build_path (":", ":x:", ":y:", NULL), ":x:y:");
67 check_string (g_build_path (":", ":x::", "::y:", NULL), ":x:y:");
68 check_string (g_build_path (":", "x", "","y", NULL), "x:y");
69 check_string (g_build_path (":", "x", ":", "y", NULL), "x:y");
70 check_string (g_build_path (":", "x", "::", "y", NULL), "x:y");
71 check_string (g_build_path (":", "x", "y", "z", NULL), "x:y:z");
72 check_string (g_build_path (":", ":x:", ":y:", ":z:", NULL), ":x:y:z:");
73 check_string (g_build_path (":", "::x::", "::y::", "::z::", NULL), "::x:y:z::");
75 /* check_string (g_build_path ("::", NULL), "");*/
76 check_string (g_build_path ("::", "::", NULL), "::");
77 check_string (g_build_path ("::", ":::", NULL), ":::");
78 check_string (g_build_path ("::", "::x", NULL), "::x");
79 check_string (g_build_path ("::", "x::", NULL), "x::");
80 check_string (g_build_path ("::", "", "x", NULL), "x");
81 check_string (g_build_path ("::", "", "::x", NULL), "::x");
82 check_string (g_build_path ("::", "::", "x", NULL), "::x");
83 check_string (g_build_path ("::", "::::", "x", NULL), "::::x");
84 check_string (g_build_path ("::", "x", "", NULL), "x");
85 check_string (g_build_path ("::", "x::", "", NULL), "x::");
86 check_string (g_build_path ("::", "x", "::", NULL), "x::");
88 /* This following is weird, but keeps the definition simple */
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", "y", NULL), "x::y");
92 check_string (g_build_path ("::", "::x", "y", NULL), "::x::y");
93 check_string (g_build_path ("::", "x", "y::", NULL), "x::y::");
94 check_string (g_build_path ("::", "::x::", "::y::", NULL), "::x::y::");
95 check_string (g_build_path ("::", "::x:::", ":::y::", NULL), "::x::::y::");
96 check_string (g_build_path ("::", "::x::::", "::::y::", NULL), "::x::y::");
97 check_string (g_build_path ("::", "x", "", "y", NULL), "x::y");
98 check_string (g_build_path ("::", "x", "::", "y", NULL), "x::y");
99 check_string (g_build_path ("::", "x", "::::", "y", NULL), "x::y");
100 check_string (g_build_path ("::", "x", "y", "z", NULL), "x::y::z");
101 check_string (g_build_path ("::", "::x::", "::y::", "::z::", NULL), "::x::y::z::");
102 check_string (g_build_path ("::", ":::x:::", ":::y:::", ":::z:::", NULL), ":::x::::y::::z:::");
103 check_string (g_build_path ("::", "::::x::::", "::::y::::", "::::z::::", NULL), "::::x::y::z::::");
107 test_build_pathv (void)
111 g_assert (g_build_pathv ("", NULL) == NULL);
113 check_string (g_build_pathv ("", args), "");
114 args[0] = ""; args[1] = NULL;
115 check_string (g_build_pathv ("", args), "");
116 args[0] = "x"; args[1] = NULL;
117 check_string (g_build_pathv ("", args), "x");
118 args[0] = "x"; args[1] = "y"; args[2] = NULL;
119 check_string (g_build_pathv ("", args), "xy");
120 args[0] = "x"; args[1] = "y"; args[2] = "z", args[3] = NULL;
121 check_string (g_build_pathv ("", args), "xyz");
124 check_string (g_build_pathv (":", args), "");
125 args[0] = ":"; args[1] = NULL;
126 check_string (g_build_pathv (":", args), ":");
127 args[0] = ":x"; args[1] = NULL;
128 check_string (g_build_pathv (":", args), ":x");
129 args[0] = "x:"; args[1] = NULL;
130 check_string (g_build_pathv (":", args), "x:");
131 args[0] = ""; args[1] = "x"; args[2] = NULL;
132 check_string (g_build_pathv (":", args), "x");
133 args[0] = ""; args[1] = ":x"; args[2] = NULL;
134 check_string (g_build_pathv (":", args), ":x");
135 args[0] = ":"; args[1] = "x"; args[2] = 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] = "x"; args[1] = ""; args[2] = NULL;
140 check_string (g_build_pathv (":", args), "x");
141 args[0] = "x:"; args[1] = ""; args[2] = NULL;
142 check_string (g_build_pathv (":", args), "x:");
143 args[0] = "x"; args[1] = ":"; 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] = "y"; args[2] = NULL;
148 check_string (g_build_pathv (":", args), "x:y");
149 args[0] = ":x"; args[1] = "y"; args[2] = NULL;
150 check_string (g_build_pathv (":", args), ":x:y");
151 args[0] = "x"; args[1] = "y:"; args[2] = NULL;
152 check_string (g_build_pathv (":", args), "x:y:");
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] = ""; args[2] = "y"; args[3] = NULL;
158 check_string (g_build_pathv (":", args), "x:y");
159 args[0] = "x"; args[1] = ":"; args[2] = "y"; args[3] = NULL;
160 check_string (g_build_pathv (":", args), "x:y");
161 args[0] = "x"; args[1] = "::"; args[2] = "y"; args[3] = NULL;
162 check_string (g_build_pathv (":", args), "x:y");
163 args[0] = "x"; args[1] = "y"; args[2] = "z"; args[3] = NULL;
164 check_string (g_build_pathv (":", args), "x:y:z");
165 args[0] = ":x:"; args[1] = ":y:"; args[2] = ":z:"; args[3] = NULL;
166 check_string (g_build_pathv (":", args), ":x:y:z:");
167 args[0] = "::x::"; args[1] = "::y::"; args[2] = "::z::"; args[3] = NULL;
168 check_string (g_build_pathv (":", args), "::x:y:z::");
171 check_string (g_build_pathv ("::", args), "");
172 args[0] = "::"; args[1] = NULL;
173 check_string (g_build_pathv ("::", args), "::");
174 args[0] = ":::"; args[1] = NULL;
175 check_string (g_build_pathv ("::", args), ":::");
176 args[0] = "::x"; args[1] = NULL;
177 check_string (g_build_pathv ("::", args), "::x");
178 args[0] = "x::"; args[1] = NULL;
179 check_string (g_build_pathv ("::", args), "x::");
180 args[0] = ""; args[1] = "x"; args[2] = NULL;
181 check_string (g_build_pathv ("::", args), "x");
182 args[0] = ""; args[1] = "::x"; args[2] = NULL;
183 check_string (g_build_pathv ("::", args), "::x");
184 args[0] = "::"; args[1] = "x"; args[2] = 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] = "x"; args[1] = ""; args[2] = NULL;
189 check_string (g_build_pathv ("::", args), "x");
190 args[0] = "x::"; args[1] = ""; args[2] = NULL;
191 check_string (g_build_pathv ("::", args), "x::");
192 args[0] = "x"; args[1] = "::"; args[2] = NULL;
193 check_string (g_build_pathv ("::", args), "x::");
194 /* This following is weird, but keeps the definition simple */
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] = "y"; args[2] = NULL;
200 check_string (g_build_pathv ("::", args), "x::y");
201 args[0] = "::x"; args[1] = "y"; args[2] = NULL;
202 check_string (g_build_pathv ("::", args), "::x::y");
203 args[0] = "x"; args[1] = "y::"; args[2] = NULL;
204 check_string (g_build_pathv ("::", args), "x::y::");
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] = ""; args[2] = "y"; args[3] = NULL;
212 check_string (g_build_pathv ("::", args), "x::y");
213 args[0] = "x"; args[1] = "::"; args[2] = "y"; args[3] = NULL;
214 check_string (g_build_pathv ("::", args), "x::y");
215 args[0] = "x"; args[1] = "::::"; args[2] = "y"; args[3] = NULL;
216 check_string (g_build_pathv ("::", args), "x::y");
217 args[0] = "x"; args[1] = "y"; args[2] = "z"; args[3] = NULL;
218 check_string (g_build_pathv ("::", args), "x::y::z");
219 args[0] = "::x::"; args[1] = "::y::"; args[2] = "::z::"; args[3] = NULL;
220 check_string (g_build_pathv ("::", args), "::x::y::z::");
221 args[0] = ":::x:::"; args[1] = ":::y:::"; args[2] = ":::z:::"; args[3] = NULL;
222 check_string (g_build_pathv ("::", args), ":::x::::y::::z:::");
223 args[0] = "::::x::::"; args[1] = "::::y::::"; args[2] = "::::z::::"; args[3] = NULL;
224 check_string (g_build_pathv ("::", args), "::::x::y::z::::");
228 test_build_filename (void)
230 /* check_string (g_build_filename (NULL), "");*/
231 check_string (g_build_filename (S, NULL), S);
232 check_string (g_build_filename (S"x", NULL), S"x");
233 check_string (g_build_filename ("x"S, NULL), "x"S);
234 check_string (g_build_filename ("", "x", NULL), "x");
235 check_string (g_build_filename ("", S"x", NULL), S"x");
236 check_string (g_build_filename (S, "x", NULL), S"x");
237 check_string (g_build_filename (S S, "x", NULL), S S"x");
238 check_string (g_build_filename ("x", "", NULL), "x");
239 check_string (g_build_filename ("x"S, "", NULL), "x"S);
240 check_string (g_build_filename ("x", S, NULL), "x"S);
241 check_string (g_build_filename ("x", S S, NULL), "x"S S);
242 check_string (g_build_filename ("x", "y", NULL), "x"S"y");
243 check_string (g_build_filename (S"x", "y", NULL), S"x"S"y");
244 check_string (g_build_filename ("x", "y"S, NULL), "x"S"y"S);
245 check_string (g_build_filename (S"x"S, S"y"S, NULL), S"x"S"y"S);
246 check_string (g_build_filename (S"x"S S, S S"y"S, NULL), S"x"S"y"S);
247 check_string (g_build_filename ("x", "", "y", NULL), "x"S"y");
248 check_string (g_build_filename ("x", S, "y", NULL), "x"S"y");
249 check_string (g_build_filename ("x", S S, "y", NULL), "x"S"y");
250 check_string (g_build_filename ("x", "y", "z", NULL), "x"S"y"S"z");
251 check_string (g_build_filename (S"x"S, S"y"S, S"z"S, NULL), S"x"S"y"S"z"S);
252 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);
256 /* Test also using the slash as file name separator */
258 /* check_string (g_build_filename (NULL), ""); */
259 check_string (g_build_filename (U, NULL), U);
260 check_string (g_build_filename (U"x", NULL), U"x");
261 check_string (g_build_filename ("x"U, NULL), "x"U);
262 check_string (g_build_filename ("", U"x", NULL), U"x");
263 check_string (g_build_filename ("", U"x", NULL), U"x");
264 check_string (g_build_filename (U, "x", NULL), U"x");
265 check_string (g_build_filename (U U, "x", NULL), U U"x");
266 check_string (g_build_filename (U S, "x", NULL), U S"x");
267 check_string (g_build_filename ("x"U, "", NULL), "x"U);
268 check_string (g_build_filename ("x"S"y", "z"U"a", NULL), "x"S"y"S"z"U"a");
269 check_string (g_build_filename ("x", U, NULL), "x"U);
270 check_string (g_build_filename ("x", U U, NULL), "x"U U);
271 check_string (g_build_filename ("x", S U, NULL), "x"S U);
272 check_string (g_build_filename (U"x", "y", NULL), U"x"U"y");
273 check_string (g_build_filename ("x", "y"U, NULL), "x"U"y"U);
274 check_string (g_build_filename (U"x"U, U"y"U, NULL), U"x"U"y"U);
275 check_string (g_build_filename (U"x"U U, U U"y"U, NULL), U"x"U"y"U);
276 check_string (g_build_filename ("x", U, "y", NULL), "x"U"y");
277 check_string (g_build_filename ("x", U U, "y", NULL), "x"U"y");
278 check_string (g_build_filename ("x", U S, "y", NULL), "x"S"y");
279 check_string (g_build_filename ("x", S U, "y", NULL), "x"U"y");
280 check_string (g_build_filename ("x", U "y", "z", NULL), "x"U"y"U"z");
281 check_string (g_build_filename ("x", S "y", "z", NULL), "x"S"y"S"z");
282 check_string (g_build_filename ("x", S "y", "z", U, "a", "b", NULL), "x"S"y"S"z"U"a"U"b");
283 check_string (g_build_filename (U"x"U, U"y"U, U"z"U, NULL), U"x"U"y"U"z"U);
284 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);
288 #endif /* G_OS_WIN32 */
293 test_build_filenamev (void)
298 check_string (g_build_filenamev (args), "");
299 args[0] = S; args[1] = NULL;
300 check_string (g_build_filenamev (args), S);
301 args[0] = S"x"; args[1] = NULL;
302 check_string (g_build_filenamev (args), S"x");
303 args[0] = "x"S; args[1] = NULL;
304 check_string (g_build_filenamev (args), "x"S);
305 args[0] = ""; args[1] = "x"; args[2] = NULL;
306 check_string (g_build_filenamev (args), "x");
307 args[0] = ""; args[1] = S"x"; args[2] = NULL;
308 check_string (g_build_filenamev (args), S"x");
309 args[0] = S; args[1] = "x"; args[2] = NULL;
310 check_string (g_build_filenamev (args), S"x");
311 args[0] = S S; args[1] = "x"; args[2] = NULL;
312 check_string (g_build_filenamev (args), S S"x");
313 args[0] = "x"; args[1] = ""; args[2] = NULL;
314 check_string (g_build_filenamev (args), "x");
315 args[0] = "x"S; args[1] = ""; args[2] = NULL;
316 check_string (g_build_filenamev (args), "x"S);
317 args[0] = "x"; args[1] = S; args[2] = NULL;
318 check_string (g_build_filenamev (args), "x"S);
319 args[0] = "x"; args[1] = S S; args[2] = NULL;
320 check_string (g_build_filenamev (args), "x"S S);
321 args[0] = "x"; args[1] = "y"; args[2] = NULL;
322 check_string (g_build_filenamev (args), "x"S"y");
323 args[0] = S"x"; args[1] = "y"; args[2] = NULL;
324 check_string (g_build_filenamev (args), S"x"S"y");
325 args[0] = "x"; args[1] = "y"S; args[2] = NULL;
326 check_string (g_build_filenamev (args), "x"S"y"S);
327 args[0] = S"x"S; args[1] = S"y"S; args[2] = NULL;
328 check_string (g_build_filenamev (args), S"x"S"y"S);
329 args[0] = S"x"S S; args[1] = S S"y"S; args[2] = NULL;
330 check_string (g_build_filenamev (args), S"x"S"y"S);
331 args[0] = "x"; args[1] = ""; args[2] = "y"; args[3] = NULL;
332 check_string (g_build_filenamev (args), "x"S"y");
333 args[0] = "x"; args[1] = S; args[2] = "y"; args[3] = NULL;
334 check_string (g_build_filenamev (args), "x"S"y");
335 args[0] = "x"; args[1] = S S; args[2] = "y"; args[3] = NULL;
336 check_string (g_build_filenamev (args), "x"S"y");
337 args[0] = "x"; args[1] = "y"; args[2] = "z"; args[3] = NULL;
338 check_string (g_build_filenamev (args), "x"S"y"S"z");
339 args[0] = S"x"S; args[1] = S"y"S; args[2] = S"z"S; args[3] = NULL;
340 check_string (g_build_filenamev (args), S"x"S"y"S"z"S);
341 args[0] = S S"x"S S; args[1] = S S"y"S S; args[2] = S S"z"S S; args[3] = NULL;
342 check_string (g_build_filenamev (args), S S"x"S"y"S"z"S S);
346 /* Test also using the slash as file name separator */
349 check_string (g_build_filenamev (args), "");
350 args[0] = U; args[1] = NULL;
351 check_string (g_build_filenamev (args), U);
352 args[0] = U"x"; args[1] = NULL;
353 check_string (g_build_filenamev (args), U"x");
354 args[0] = "x"U; args[1] = NULL;
355 check_string (g_build_filenamev (args), "x"U);
356 args[0] = ""; args[1] = U"x"; args[2] = NULL;
357 check_string (g_build_filenamev (args), U"x");
358 args[0] = ""; args[1] = U"x"; args[2] = NULL;
359 check_string (g_build_filenamev (args), U"x");
360 args[0] = U; args[1] = "x"; args[2] = NULL;
361 check_string (g_build_filenamev (args), U"x");
362 args[0] = U U; args[1] = "x"; args[2] = NULL;
363 check_string (g_build_filenamev (args), U U"x");
364 args[0] = U S; args[1] = "x"; args[2] = NULL;
365 check_string (g_build_filenamev (args), U S"x");
366 args[0] = "x"U; args[1] = ""; args[2] = NULL;
367 check_string (g_build_filenamev (args), "x"U);
368 args[0] = "x"S"y"; args[1] = "z"U"a"; args[2] = NULL;
369 check_string (g_build_filenamev (args), "x"S"y"S"z"U"a");
370 args[0] = "x"; args[1] = U; args[2] = NULL;
371 check_string (g_build_filenamev (args), "x"U);
372 args[0] = "x"; args[1] = U U; args[2] = NULL;
373 check_string (g_build_filenamev (args), "x"U U);
374 args[0] = "x"; args[1] = S U; args[2] = NULL;
375 check_string (g_build_filenamev (args), "x"S U);
376 args[0] = U"x"; args[1] = "y"; args[2] = NULL;
377 check_string (g_build_filenamev (args), U"x"U"y");
378 args[0] = "x"; args[1] = "y"U; args[2] = NULL;
379 check_string (g_build_filenamev (args), "x"U"y"U);
380 args[0] = U"x"U; args[1] = U"y"U; args[2] = NULL;
381 check_string (g_build_filenamev (args), U"x"U"y"U);
382 args[0] = U"x"U U; args[1] = U U"y"U; args[2] = NULL;
383 check_string (g_build_filenamev (args), U"x"U"y"U);
384 args[0] = "x"; args[1] = U; args[2] = "y", args[3] = NULL;
385 check_string (g_build_filenamev (args), "x"U"y");
386 args[0] = "x"; args[1] = U U; args[2] = "y", args[3] = NULL;
387 check_string (g_build_filenamev (args), "x"U"y");
388 args[0] = "x"; args[1] = U S; args[2] = "y", args[3] = NULL;
389 check_string (g_build_filenamev (args), "x"S"y");
390 args[0] = "x"; args[1] = S U; args[2] = "y", args[3] = NULL;
391 check_string (g_build_filenamev (args), "x"U"y");
392 args[0] = "x"; args[1] = U "y"; args[2] = "z", args[3] = NULL;
393 check_string (g_build_filenamev (args), "x"U"y"U"z");
394 args[0] = "x"; args[1] = S "y"; args[2] = "z", args[3] = NULL;
395 check_string (g_build_filenamev (args), "x"S"y"S"z");
396 args[0] = "x"; args[1] = S "y"; args[2] = "z", args[3] = U;
397 args[4] = "a"; args[5] = "b"; args[6] = NULL;
398 check_string (g_build_filenamev (args), "x"S"y"S"z"U"a"U"b");
399 args[0] = U"x"U; args[1] = U"y"U; args[2] = U"z"U, args[3] = NULL;
400 check_string (g_build_filenamev (args), U"x"U"y"U"z"U);
401 args[0] = U U"x"U U; args[1] = U U"y"U U; args[2] = U U"z"U U, args[3] = NULL;
402 check_string (g_build_filenamev (args), U U"x"U"y"U"z"U U);
406 #endif /* G_OS_WIN32 */
412 test_mkdir_with_parents_1 (const gchar *base)
414 char *p0 = g_build_filename (base, "fum", NULL);
415 char *p1 = g_build_filename (p0, "tem", NULL);
416 char *p2 = g_build_filename (p1, "zap", NULL);
423 if (g_file_test (p0, G_FILE_TEST_EXISTS))
424 g_error ("failed, %s exists, cannot test g_mkdir_with_parents\n", p0);
426 if (g_file_test (p1, G_FILE_TEST_EXISTS))
427 g_error ("failed, %s exists, cannot test g_mkdir_with_parents\n", p1);
429 if (g_file_test (p2, G_FILE_TEST_EXISTS))
430 g_error ("failed, %s exists, cannot test g_mkdir_with_parents\n", p2);
432 if (g_mkdir_with_parents (p2, 0777) == -1)
433 g_error ("failed, g_mkdir_with_parents(%s) failed: %s\n", p2, g_strerror (errno));
435 if (!g_file_test (p2, G_FILE_TEST_IS_DIR))
436 g_error ("failed, g_mkdir_with_parents(%s) succeeded, but %s is not a directory\n", p2, p2);
438 if (!g_file_test (p1, G_FILE_TEST_IS_DIR))
439 g_error ("failed, g_mkdir_with_parents(%s) succeeded, but %s is not a directory\n", p2, p1);
441 if (!g_file_test (p0, G_FILE_TEST_IS_DIR))
442 g_error ("failed, g_mkdir_with_parents(%s) succeeded, but %s is not a directory\n", p2, p0);
445 if (g_file_test (p2, G_FILE_TEST_EXISTS))
446 g_error ("failed, did g_rmdir(%s), but %s is still there\n", p2, p2);
449 if (g_file_test (p1, G_FILE_TEST_EXISTS))
450 g_error ("failed, did g_rmdir(%s), but %s is still there\n", p1, p1);
452 f = g_fopen (p1, "w");
454 g_error ("failed, couldn't create file %s\n", p1);
457 if (g_mkdir_with_parents (p1, 0666) == 0)
458 g_error ("failed, g_mkdir_with_parents(%s) succeeded, even if %s is a file\n", p1, p1);
460 if (g_mkdir_with_parents (p2, 0666) == 0)
461 g_error("failed, g_mkdir_with_parents(%s) succeeded, even if %s is a file\n", p2, p1);
473 test_mkdir_with_parents (void)
476 if (g_test_verbose())
477 g_print ("checking g_mkdir_with_parents() in subdir ./hum/");
478 test_mkdir_with_parents_1 ("hum");
480 if (g_test_verbose())
481 g_print ("checking g_mkdir_with_parents() in subdir ./hii///haa/hee/");
482 test_mkdir_with_parents_1 ("hii///haa/hee");
483 g_remove ("hii/haa/hee");
484 g_remove ("hii/haa");
486 cwd = g_get_current_dir ();
487 if (g_test_verbose())
488 g_print ("checking g_mkdir_with_parents() in cwd: %s", cwd);
489 test_mkdir_with_parents_1 (cwd);
492 g_assert (g_mkdir_with_parents (NULL, 0) == -1);
493 g_assert (errno == EINVAL);
497 test_format_size_for_display (void)
499 /* nobody called setlocale(), so we should get "C" behaviour... */
500 check_string (g_format_size_for_display (0), "0 bytes");
501 check_string (g_format_size_for_display (1), "1 byte");
502 check_string (g_format_size_for_display (2), "2 bytes");
503 check_string (g_format_size_for_display (1024), "1.0 KB");
504 check_string (g_format_size_for_display (1024 * 1024), "1.0 MB");
505 check_string (g_format_size_for_display (1024 * 1024 * 1024), "1.0 GB");
506 check_string (g_format_size_for_display (1024ULL * 1024 * 1024 * 1024), "1.0 TB");
507 check_string (g_format_size_for_display (1024ULL * 1024 * 1024 * 1024 * 1024), "1.0 PB");
508 check_string (g_format_size_for_display (1024ULL * 1024 * 1024 * 1024 * 1024 * 1024), "1.0 EB");
510 check_string (g_format_size (0), "0 bytes");
511 check_string (g_format_size (1), "1 byte");
512 check_string (g_format_size (2), "2 bytes");
513 check_string (g_format_size (1000ULL), "1.0 kB");
514 check_string (g_format_size (1000ULL * 1000), "1.0 MB");
515 check_string (g_format_size (1000ULL * 1000 * 1000), "1.0 GB");
516 check_string (g_format_size (1000ULL * 1000 * 1000 * 1000), "1.0 TB");
517 check_string (g_format_size (1000ULL * 1000 * 1000 * 1000 * 1000), "1.0 PB");
518 check_string (g_format_size (1000ULL * 1000 * 1000 * 1000 * 1000 * 1000), "1.0 EB");
520 check_string (g_format_size_full (0, G_FORMAT_SIZE_IEC_UNITS), "0 bytes");
521 check_string (g_format_size_full (1, G_FORMAT_SIZE_IEC_UNITS), "1 byte");
522 check_string (g_format_size_full (2, G_FORMAT_SIZE_IEC_UNITS), "2 bytes");
524 check_string (g_format_size_full (2048ULL, G_FORMAT_SIZE_IEC_UNITS), "2.0 KiB");
525 check_string (g_format_size_full (2048ULL * 1024, G_FORMAT_SIZE_IEC_UNITS), "2.0 MiB");
526 check_string (g_format_size_full (2048ULL * 1024 * 1024, G_FORMAT_SIZE_IEC_UNITS), "2.0 GiB");
527 check_string (g_format_size_full (2048ULL * 1024 * 1024 * 1024, G_FORMAT_SIZE_IEC_UNITS), "2.0 TiB");
528 check_string (g_format_size_full (2048ULL * 1024 * 1024 * 1024 * 1024, G_FORMAT_SIZE_IEC_UNITS), "2.0 PiB");
529 check_string (g_format_size_full (2048ULL * 1024 * 1024 * 1024 * 1024 * 1024, G_FORMAT_SIZE_IEC_UNITS), "2.0 EiB");
531 check_string (g_format_size_full (238472938, G_FORMAT_SIZE_IEC_UNITS), "227.4 MiB");
532 check_string (g_format_size_full (238472938, G_FORMAT_SIZE_DEFAULT), "238.5 MB");
533 check_string (g_format_size_full (238472938, G_FORMAT_SIZE_LONG_FORMAT), "238.5 MB (238472938 bytes)");
537 test_file_errors (void)
540 g_assert (g_file_error_from_errno (EEXIST) == G_FILE_ERROR_EXIST);
543 g_assert (g_file_error_from_errno (EISDIR) == G_FILE_ERROR_ISDIR);
546 g_assert (g_file_error_from_errno (EACCES) == G_FILE_ERROR_ACCES);
549 g_assert (g_file_error_from_errno (ENAMETOOLONG) == G_FILE_ERROR_NAMETOOLONG);
552 g_assert (g_file_error_from_errno (ENOENT) == G_FILE_ERROR_NOENT);
555 g_assert (g_file_error_from_errno (ENOTDIR) == G_FILE_ERROR_NOTDIR);
558 g_assert (g_file_error_from_errno (ENXIO) == G_FILE_ERROR_NXIO);
561 g_assert (g_file_error_from_errno (ENODEV) == G_FILE_ERROR_NODEV);
564 g_assert (g_file_error_from_errno (EROFS) == G_FILE_ERROR_ROFS);
567 g_assert (g_file_error_from_errno (ETXTBSY) == G_FILE_ERROR_TXTBSY);
570 g_assert (g_file_error_from_errno (EFAULT) == G_FILE_ERROR_FAULT);
573 g_assert (g_file_error_from_errno (ELOOP) == G_FILE_ERROR_LOOP);
576 g_assert (g_file_error_from_errno (ENOSPC) == G_FILE_ERROR_NOSPC);
579 g_assert (g_file_error_from_errno (ENOMEM) == G_FILE_ERROR_NOMEM);
582 g_assert (g_file_error_from_errno (EMFILE) == G_FILE_ERROR_MFILE);
585 g_assert (g_file_error_from_errno (ENFILE) == G_FILE_ERROR_NFILE);
588 g_assert (g_file_error_from_errno (EBADF) == G_FILE_ERROR_BADF);
591 g_assert (g_file_error_from_errno (EINVAL) == G_FILE_ERROR_INVAL);
594 g_assert (g_file_error_from_errno (EPIPE) == G_FILE_ERROR_PIPE);
597 g_assert (g_file_error_from_errno (EAGAIN) == G_FILE_ERROR_AGAIN);
600 g_assert (g_file_error_from_errno (EINTR) == G_FILE_ERROR_INTR);
603 g_assert (g_file_error_from_errno (EIO) == G_FILE_ERROR_IO);
606 g_assert (g_file_error_from_errno (EPERM) == G_FILE_ERROR_PERM);
609 g_assert (g_file_error_from_errno (ENOSYS) == G_FILE_ERROR_NOSYS);
618 b = g_path_get_basename ("");
619 g_assert_cmpstr (b, ==, ".");
622 b = g_path_get_basename ("///");
623 g_assert_cmpstr (b, ==, G_DIR_SEPARATOR_S);
626 b = g_path_get_basename ("/a/b/c/d");
627 g_assert_cmpstr (b, ==, "d");
632 test_dir_make_tmp (void)
635 GError *error = NULL;
637 name = g_dir_make_tmp ("testXXXXXXtest", &error);
638 g_assert_no_error (error);
639 g_assert (g_file_test (name, G_FILE_TEST_IS_DIR));
640 g_assert (g_rmdir (name) == 0);
643 name = g_dir_make_tmp (NULL, &error);
644 g_assert_no_error (error);
645 g_assert (g_file_test (name, G_FILE_TEST_IS_DIR));
646 g_assert (g_rmdir (name) == 0);
649 name = g_dir_make_tmp ("test/XXXXXX", &error);
650 g_assert_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED);
651 g_clear_error (&error);
652 g_assert (name == NULL);
654 name = g_dir_make_tmp ("XXXXxX", &error);
655 g_assert_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED);
656 g_clear_error (&error);
657 g_assert (name == NULL);
661 test_file_open_tmp (void)
664 GError *error = NULL;
667 fd = g_file_open_tmp ("testXXXXXXtest", &name, &error);
669 g_assert_no_error (error);
670 g_assert (name != NULL);
675 fd = g_file_open_tmp (NULL, &name, &error);
677 g_assert_no_error (error);
678 g_assert (name != NULL);
684 fd = g_file_open_tmp ("test/XXXXXX", &name, &error);
686 g_assert (name == NULL);
687 g_assert_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED);
688 g_clear_error (&error);
690 fd = g_file_open_tmp ("XXXXxX", &name, &error);
692 g_assert (name == NULL);
693 g_assert_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED);
694 g_clear_error (&error);
703 name = g_strdup ("testXXXXXXtest"),
704 fd = g_mkstemp (name);
706 g_assert (strstr (name, "XXXXXX") == NULL);
711 name = g_strdup ("testYYYYYYtest"),
712 fd = g_mkstemp (name);
723 name = g_strdup ("testXXXXXXtest"),
724 ret = g_mkdtemp (name);
725 g_assert (ret == name);
726 g_assert (strstr (name, "XXXXXX") == NULL);
730 name = g_strdup ("testYYYYYYtest"),
731 ret = g_mkdtemp (name);
732 g_assert (ret == NULL);
737 test_set_contents (void)
739 GError *error = NULL;
746 fd = g_file_open_tmp (NULL, &name, &error);
747 g_assert_no_error (error);
751 ret = g_file_get_contents (name, &buf, &len, &error);
753 g_assert_no_error (error);
754 g_assert_cmpstr (buf, ==, "a");
757 ret = g_file_set_contents (name, "b", 1, &error);
759 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, ==, "b");
775 g_test_init (&argc, &argv, NULL);
777 g_test_add_func ("/fileutils/build-path", test_build_path);
778 g_test_add_func ("/fileutils/build-pathv", test_build_pathv);
779 g_test_add_func ("/fileutils/build-filename", test_build_filename);
780 g_test_add_func ("/fileutils/build-filenamev", test_build_filenamev);
781 g_test_add_func ("/fileutils/mkdir-with-parents", test_mkdir_with_parents);
782 g_test_add_func ("/fileutils/format-size-for-display", test_format_size_for_display);
783 g_test_add_func ("/fileutils/errors", test_file_errors);
784 g_test_add_func ("/fileutils/basename", test_basename);
785 g_test_add_func ("/fileutils/dir-make-tmp", test_dir_make_tmp);
786 g_test_add_func ("/fileutils/file-open-tmp", test_file_open_tmp);
787 g_test_add_func ("/fileutils/mkstemp", test_mkstemp);
788 g_test_add_func ("/fileutils/mkdtemp", test_mkdtemp);
789 g_test_add_func ("/fileutils/set-contents", test_set_contents);
791 return g_test_run ();