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)");
556 check_string (g_format_size_full (0, G_FORMAT_SIZE_BITS
), "0 bits");
557 check_string (g_format_size_full (1, G_FORMAT_SIZE_BITS
), "1 bit");
558 check_string (g_format_size_full (2, G_FORMAT_SIZE_BITS
), "2 bits");
560 check_string (g_format_size_full (2000ULL, G_FORMAT_SIZE_BITS
), "2.0 kb");
561 check_string (g_format_size_full (2000ULL * 1000, G_FORMAT_SIZE_BITS
), "2.0 Mb");
562 check_string (g_format_size_full (2000ULL * 1000 * 1000, G_FORMAT_SIZE_BITS
), "2.0 Gb");
563 check_string (g_format_size_full (2000ULL * 1000 * 1000 * 1000, G_FORMAT_SIZE_BITS
), "2.0 Tb");
564 check_string (g_format_size_full (2000ULL * 1000 * 1000 * 1000 * 1000, G_FORMAT_SIZE_BITS
), "2.0 Pb");
565 check_string (g_format_size_full (2000ULL * 1000 * 1000 * 1000 * 1000 * 1000, G_FORMAT_SIZE_BITS
), "2.0 Eb");
567 check_string (g_format_size_full (238472938, G_FORMAT_SIZE_BITS
), "238.5 Mb");
568 check_string (g_format_size_full (238472938, G_FORMAT_SIZE_BITS
| G_FORMAT_SIZE_LONG_FORMAT
), "238.5 Mb (238472938 bits)");
571 check_string (g_format_size_full (0, G_FORMAT_SIZE_BITS
| G_FORMAT_SIZE_IEC_UNITS
), "0 bits");
572 check_string (g_format_size_full (1, G_FORMAT_SIZE_BITS
| G_FORMAT_SIZE_IEC_UNITS
), "1 bit");
573 check_string (g_format_size_full (2, G_FORMAT_SIZE_BITS
| G_FORMAT_SIZE_IEC_UNITS
), "2 bits");
575 check_string (g_format_size_full (2048ULL, G_FORMAT_SIZE_BITS
| G_FORMAT_SIZE_IEC_UNITS
), "2.0 Kib");
576 check_string (g_format_size_full (2048ULL * 1024, G_FORMAT_SIZE_BITS
| G_FORMAT_SIZE_IEC_UNITS
), "2.0 Mib");
577 check_string (g_format_size_full (2048ULL * 1024 * 1024, G_FORMAT_SIZE_BITS
| G_FORMAT_SIZE_IEC_UNITS
), "2.0 Gib");
578 check_string (g_format_size_full (2048ULL * 1024 * 1024 * 1024, G_FORMAT_SIZE_BITS
| G_FORMAT_SIZE_IEC_UNITS
), "2.0 Tib");
579 check_string (g_format_size_full (2048ULL * 1024 * 1024 * 1024 * 1024, G_FORMAT_SIZE_BITS
| G_FORMAT_SIZE_IEC_UNITS
), "2.0 Pib");
580 check_string (g_format_size_full (2048ULL * 1024 * 1024 * 1024 * 1024 * 1024, G_FORMAT_SIZE_BITS
| G_FORMAT_SIZE_IEC_UNITS
), "2.0 Eib");
582 check_string (g_format_size_full (238472938, G_FORMAT_SIZE_BITS
| G_FORMAT_SIZE_IEC_UNITS
), "227.4 Mib");
583 check_string (g_format_size_full (238472938, G_FORMAT_SIZE_BITS
| G_FORMAT_SIZE_IEC_UNITS
| G_FORMAT_SIZE_LONG_FORMAT
), "227.4 Mib (238472938 bits)");
587 test_file_errors (void)
590 g_assert_cmpint (g_file_error_from_errno (EEXIST
), ==, G_FILE_ERROR_EXIST
);
593 g_assert_cmpint (g_file_error_from_errno (EISDIR
), ==, G_FILE_ERROR_ISDIR
);
596 g_assert_cmpint (g_file_error_from_errno (EACCES
), ==, G_FILE_ERROR_ACCES
);
599 g_assert_cmpint (g_file_error_from_errno (ENAMETOOLONG
), ==, G_FILE_ERROR_NAMETOOLONG
);
602 g_assert_cmpint (g_file_error_from_errno (ENOENT
), ==, G_FILE_ERROR_NOENT
);
605 g_assert_cmpint (g_file_error_from_errno (ENOTDIR
), ==, G_FILE_ERROR_NOTDIR
);
608 g_assert_cmpint (g_file_error_from_errno (ENXIO
), ==, G_FILE_ERROR_NXIO
);
611 g_assert_cmpint (g_file_error_from_errno (ENODEV
), ==, G_FILE_ERROR_NODEV
);
614 g_assert_cmpint (g_file_error_from_errno (EROFS
), ==, G_FILE_ERROR_ROFS
);
617 g_assert_cmpint (g_file_error_from_errno (ETXTBSY
), ==, G_FILE_ERROR_TXTBSY
);
620 g_assert_cmpint (g_file_error_from_errno (EFAULT
), ==, G_FILE_ERROR_FAULT
);
623 g_assert_cmpint (g_file_error_from_errno (ELOOP
), ==, G_FILE_ERROR_LOOP
);
626 g_assert_cmpint (g_file_error_from_errno (ENOSPC
), ==, G_FILE_ERROR_NOSPC
);
629 g_assert_cmpint (g_file_error_from_errno (ENOMEM
), ==, G_FILE_ERROR_NOMEM
);
632 g_assert_cmpint (g_file_error_from_errno (EMFILE
), ==, G_FILE_ERROR_MFILE
);
635 g_assert_cmpint (g_file_error_from_errno (ENFILE
), ==, G_FILE_ERROR_NFILE
);
638 g_assert_cmpint (g_file_error_from_errno (EBADF
), ==, G_FILE_ERROR_BADF
);
641 g_assert_cmpint (g_file_error_from_errno (EINVAL
), ==, G_FILE_ERROR_INVAL
);
644 g_assert_cmpint (g_file_error_from_errno (EPIPE
), ==, G_FILE_ERROR_PIPE
);
647 g_assert_cmpint (g_file_error_from_errno (EAGAIN
), ==, G_FILE_ERROR_AGAIN
);
650 g_assert_cmpint (g_file_error_from_errno (EINTR
), ==, G_FILE_ERROR_INTR
);
653 g_assert_cmpint (g_file_error_from_errno (EIO
), ==, G_FILE_ERROR_IO
);
656 g_assert_cmpint (g_file_error_from_errno (EPERM
), ==, G_FILE_ERROR_PERM
);
659 g_assert_cmpint (g_file_error_from_errno (ENOSYS
), ==, G_FILE_ERROR_NOSYS
);
668 b
= g_path_get_basename ("");
669 g_assert_cmpstr (b
, ==, ".");
672 b
= g_path_get_basename ("///");
673 g_assert_cmpstr (b
, ==, G_DIR_SEPARATOR_S
);
676 b
= g_path_get_basename ("/a/b/c/d");
677 g_assert_cmpstr (b
, ==, "d");
682 test_dir_make_tmp (void)
685 GError
*error
= NULL
;
688 name
= g_dir_make_tmp ("testXXXXXXtest", &error
);
689 g_assert_no_error (error
);
690 g_assert_true (g_file_test (name
, G_FILE_TEST_IS_DIR
));
691 ret
= g_rmdir (name
);
692 g_assert_cmpint (ret
, ==, 0);
695 name
= g_dir_make_tmp (NULL
, &error
);
696 g_assert_no_error (error
);
697 g_assert_true (g_file_test (name
, G_FILE_TEST_IS_DIR
));
698 ret
= g_rmdir (name
);
699 g_assert_cmpint (ret
, ==, 0);
702 name
= g_dir_make_tmp ("test/XXXXXX", &error
);
703 g_assert_error (error
, G_FILE_ERROR
, G_FILE_ERROR_FAILED
);
704 g_clear_error (&error
);
705 g_assert_null (name
);
707 name
= g_dir_make_tmp ("XXXXxX", &error
);
708 g_assert_error (error
, G_FILE_ERROR
, G_FILE_ERROR_FAILED
);
709 g_clear_error (&error
);
710 g_assert_null (name
);
714 test_file_open_tmp (void)
717 GError
*error
= NULL
;
720 fd
= g_file_open_tmp ("testXXXXXXtest", &name
, &error
);
721 g_assert_cmpint (fd
, !=, -1);
722 g_assert_no_error (error
);
723 g_assert_nonnull (name
);
728 fd
= g_file_open_tmp (NULL
, &name
, &error
);
729 g_assert_cmpint (fd
, !=, -1);
730 g_assert_no_error (error
);
731 g_assert_nonnull (name
);
737 fd
= g_file_open_tmp ("test/XXXXXX", &name
, &error
);
738 g_assert_cmpint (fd
, ==, -1);
739 g_assert_null (name
);
740 g_assert_error (error
, G_FILE_ERROR
, G_FILE_ERROR_FAILED
);
741 g_clear_error (&error
);
743 fd
= g_file_open_tmp ("XXXXxX", &name
, &error
);
744 g_assert_cmpint (fd
, ==, -1);
745 g_assert_null (name
);
746 g_assert_error (error
, G_FILE_ERROR
, G_FILE_ERROR_FAILED
);
747 g_clear_error (&error
);
756 name
= g_strdup ("testXXXXXXtest"),
757 fd
= g_mkstemp (name
);
758 g_assert_cmpint (fd
, !=, -1);
759 g_assert_null (strstr (name
, "XXXXXX"));
764 name
= g_strdup ("testYYYYYYtest"),
765 fd
= g_mkstemp (name
);
766 g_assert_cmpint (fd
, ==, -1);
776 name
= g_strdup ("testXXXXXXtest"),
777 ret
= g_mkdtemp (name
);
778 g_assert (ret
== name
);
779 g_assert_null (strstr (name
, "XXXXXX"));
783 name
= g_strdup ("testYYYYYYtest"),
784 ret
= g_mkdtemp (name
);
790 test_set_contents (void)
792 GError
*error
= NULL
;
799 fd
= g_file_open_tmp (NULL
, &name
, &error
);
800 g_assert_no_error (error
);
804 ret
= g_file_get_contents (name
, &buf
, &len
, &error
);
806 g_assert_no_error (error
);
807 g_assert_cmpstr (buf
, ==, "a");
810 ret
= g_file_set_contents (name
, "b", 1, &error
);
812 g_assert_no_error (error
);
814 ret
= g_file_get_contents (name
, &buf
, &len
, &error
);
816 g_assert_no_error (error
);
817 g_assert_cmpstr (buf
, ==, "b");
825 test_read_link (void)
830 const gchar
*oldpath
;
835 GError
*error
= NULL
;
837 cwd
= g_get_current_dir ();
839 oldpath
= g_test_get_filename (G_TEST_DIST
, "4096-random-bytes", NULL
);
840 newpath
= g_build_filename (cwd
, "page-of-junk", NULL
);
841 badpath
= g_build_filename (cwd
, "4097-random-bytes", NULL
);
843 ret
= symlink (oldpath
, newpath
);
844 g_assert_cmpint (ret
, ==, 0);
845 path
= g_file_read_link (newpath
, &error
);
846 g_assert_no_error (error
);
847 g_assert_cmpstr (path
, ==, oldpath
);
851 ret
= symlink (badpath
, newpath
);
852 g_assert_cmpint (ret
, ==, 0);
853 path
= g_file_read_link (newpath
, &error
);
854 g_assert_no_error (error
);
855 g_assert_cmpstr (path
, ==, badpath
);
858 path
= g_file_read_link (oldpath
, &error
);
859 g_assert_error (error
, G_FILE_ERROR
, G_FILE_ERROR_INVAL
);
860 g_assert_null (path
);
861 g_error_free (error
);
870 g_test_skip ("Symbolic links not supported");
875 test_stdio_wrappers (void)
881 GError
*error
= NULL
;
883 g_remove ("mkdir-test/test-create");
884 ret
= g_rmdir ("mkdir-test");
885 g_assert (ret
== 0 || errno
== ENOENT
);
887 ret
= g_stat ("mkdir-test", &buf
);
888 g_assert_cmpint (ret
, ==, -1);
889 ret
= g_mkdir ("mkdir-test", 0666);
890 g_assert_cmpint (ret
, ==, 0);
891 ret
= g_stat ("mkdir-test", &buf
);
892 g_assert_cmpint (ret
, ==, 0);
893 g_assert_cmpint (S_ISDIR (buf
.st_mode
), !=, 0);
895 cwd
= g_get_current_dir ();
896 path
= g_build_filename (cwd
, "mkdir-test", NULL
);
898 ret
= g_chdir (path
);
899 g_assert_cmpint (errno
, ==, EACCES
);
900 g_assert_cmpint (ret
, ==, -1);
901 ret
= g_chmod (path
, 0777);
902 g_assert_cmpint (ret
, ==, 0);
903 ret
= g_chdir (path
);
904 g_assert_cmpint (ret
, ==, 0);
905 cwd
= g_get_current_dir ();
906 g_assert_true (g_str_equal (cwd
, path
));
910 ret
= g_creat ("test-creat", 0555);
911 g_close (ret
, &error
);
912 g_assert_no_error (error
);
914 ret
= g_access ("test-creat", F_OK
);
915 g_assert_cmpint (ret
, ==, 0);
917 ret
= g_rename ("test-creat", "test-create");
918 g_assert_cmpint (ret
, ==, 0);
920 ret
= g_open ("test-create", O_RDONLY
, 0666);
921 g_close (ret
, &error
);
922 g_assert_no_error (error
);
924 ut
.actime
= ut
.modtime
= (time_t)0;
925 ret
= g_utime ("test-create", &ut
);
926 g_assert_cmpint (ret
, ==, 0);
928 ret
= g_lstat ("test-create", &buf
);
929 g_assert_cmpint (ret
, ==, 0);
930 g_assert_cmpint (buf
.st_atime
, ==, (time_t)0);
931 g_assert_cmpint (buf
.st_mtime
, ==, (time_t)0);
934 g_remove ("mkdir-test/test-create");
935 g_rmdir ("mkdir-test");
942 g_setenv ("LC_ALL", "C", TRUE
);
943 g_test_init (&argc
, &argv
, NULL
);
945 g_test_add_func ("/fileutils/build-path", test_build_path
);
946 g_test_add_func ("/fileutils/build-pathv", test_build_pathv
);
947 g_test_add_func ("/fileutils/build-filename", test_build_filename
);
948 g_test_add_func ("/fileutils/build-filenamev", test_build_filenamev
);
949 g_test_add_func ("/fileutils/mkdir-with-parents", test_mkdir_with_parents
);
950 g_test_add_func ("/fileutils/format-size-for-display", test_format_size_for_display
);
951 g_test_add_func ("/fileutils/errors", test_file_errors
);
952 g_test_add_func ("/fileutils/basename", test_basename
);
953 g_test_add_func ("/fileutils/dir-make-tmp", test_dir_make_tmp
);
954 g_test_add_func ("/fileutils/file-open-tmp", test_file_open_tmp
);
955 g_test_add_func ("/fileutils/mkstemp", test_mkstemp
);
956 g_test_add_func ("/fileutils/mkdtemp", test_mkdtemp
);
957 g_test_add_func ("/fileutils/set-contents", test_set_contents
);
958 g_test_add_func ("/fileutils/read-link", test_read_link
);
959 g_test_add_func ("/fileutils/stdio-wrappers", test_stdio_wrappers
);
961 return g_test_run ();