Merge pull request #3416 from ludovic-henry/iolayer-extract-wait
[mono-project.git] / eglib / test / path.c
blobe3832a04be493815e423203c66616f86418e33db
1 #include <config.h>
2 #include <glib.h>
3 #include <string.h>
4 #include <stdio.h>
5 #ifdef HAVE_UNISTD_H
6 #include <unistd.h>
7 #endif
8 #ifdef G_OS_UNIX
9 #include <pthread.h>
10 #endif
11 #include "test.h"
13 #ifdef G_OS_WIN32
14 #include <direct.h>
15 #define chdir _chdir
16 #endif
18 /* This test is just to be used with valgrind */
19 RESULT
20 test_buildpath ()
22 char *s;
23 char *buffer = "var/private";
24 char *dir = "/";
26 s = g_build_path ("/", "hola///", "//mundo", NULL);
27 if (strcmp (s, "hola/mundo") != 0)
28 return FAILED ("1 Got wrong result, got: %s", s);
29 g_free (s);
31 s = g_build_path ("/", "hola/", "/mundo", NULL);
32 if (strcmp (s, "hola/mundo") != 0)
33 return FAILED ("2 Got wrong result, got: %s", s);
34 g_free (s);
36 s = g_build_path ("/", "hola/", "mundo", NULL);
37 if (strcmp (s, "hola/mundo") != 0)
38 return FAILED ("3 Got wrong result, got: %s", s);
39 g_free (s);
41 s = g_build_path ("/", "hola", "/mundo", NULL);
42 if (strcmp (s, "hola/mundo") != 0)
43 return FAILED ("4 Got wrong result, got: %s", s);
44 g_free (s);
46 s = g_build_path ("/", "/hello", "world/", NULL);
47 if (strcmp (s, "/hello/world/") != 0)
48 return FAILED ("5 Got wrong result, got: %s", s);
49 g_free (s);
51 /* Now test multi-char-separators */
52 s = g_build_path ("**", "hello", "world", NULL);
53 if (strcmp (s, "hello**world") != 0)
54 return FAILED ("6 Got wrong result, got: %s", s);
55 g_free (s);
57 s = g_build_path ("**", "hello**", "world", NULL);
58 if (strcmp (s, "hello**world") != 0)
59 return FAILED ("7 Got wrong result, got: %s", s);
60 g_free (s);
62 s = g_build_path ("**", "hello**", "**world", NULL);
63 if (strcmp (s, "hello**world") != 0)
64 return FAILED ("8 Got wrong result, got: %s", s);
65 g_free (s);
67 s = g_build_path ("**", "hello**", "**world", NULL);
68 if (strcmp (s, "hello**world") != 0)
69 return FAILED ("9 Got wrong result, got: %s", s);
70 g_free (s);
72 s = g_build_path ("1234567890", "hello", "world", NULL);
73 if (strcmp (s, "hello1234567890world") != 0)
74 return FAILED ("10 Got wrong result, got: %s", s);
75 g_free (s);
77 s = g_build_path ("1234567890", "hello1234567890", "1234567890world", NULL);
78 if (strcmp (s, "hello1234567890world") != 0)
79 return FAILED ("11 Got wrong result, got: %s", s);
80 g_free (s);
82 s = g_build_path ("1234567890", "hello12345678901234567890", "1234567890world", NULL);
83 if (strcmp (s, "hello1234567890world") != 0)
84 return FAILED ("12 Got wrong result, got: %s", s);
85 g_free (s);
87 /* Multiple */
88 s = g_build_path ("/", "a", "b", "c", "d", NULL);
89 if (strcmp (s, "a/b/c/d") != 0)
90 return FAILED ("13 Got wrong result, got: %s", s);
91 g_free (s);
93 s = g_build_path ("/", "/a", "", "/c/", NULL);
94 if (strcmp (s, "/a/c/") != 0)
95 return FAILED ("14 Got wrong result, got: %s", s);
96 g_free (s);
98 /* Null */
99 s = g_build_path ("/", NULL, NULL);
100 if (s == NULL)
101 return FAILED ("must get a non-NULL return");
102 if (s [0] != 0)
103 return FAILED ("must get an empty string");
105 // This is to test the regression introduced by Levi for the Windows support
106 // that code errouneously read below the allowed area (in this case dir [-1]).
107 // and caused all kinds of random errors.
108 dir = "//";
109 dir++;
110 s = g_build_filename (dir, buffer, NULL);
111 if (s [0] != '/')
112 return FAILED ("Must have a '/' at the start");
114 g_free (s);
115 return OK;
118 RESULT
119 test_buildfname ()
121 char *s;
123 s = g_build_filename ("a", "b", "c", "d", NULL);
124 #ifdef G_OS_WIN32
125 if (strcmp (s, "a\\b\\c\\d") != 0)
126 #else
127 if (strcmp (s, "a/b/c/d") != 0)
128 #endif
129 return FAILED ("1 Got wrong result, got: %s", s);
130 g_free (s);
132 #ifdef G_OS_WIN32
133 s = g_build_filename ("C:\\", "a", NULL);
134 if (strcmp (s, "C:\\a") != 0)
135 #else
136 s = g_build_filename ("/", "a", NULL);
137 if (strcmp (s, "/a") != 0)
138 #endif
139 return FAILED ("1 Got wrong result, got: %s", s);
141 #ifndef G_OS_WIN32
142 s = g_build_filename ("/", "foo", "/bar", "tolo/", "/meo/", NULL);
143 if (strcmp (s, "/foo/bar/tolo/meo/") != 0)
144 return FAILED ("1 Got wrong result, got: %s", s);
145 #endif
147 return OK;
150 char *
151 test_dirname ()
153 char *s;
155 #ifdef G_OS_WIN32
156 s = g_path_get_dirname ("c:\\home\\miguel");
157 if (strcmp (s, "c:\\home") != 0)
158 return FAILED ("Expected c:\\home, got %s", s);
159 g_free (s);
161 s = g_path_get_dirname ("c:/home/miguel");
162 if (strcmp (s, "c:/home") != 0)
163 return FAILED ("Expected c:/home, got %s", s);
164 g_free (s);
166 s = g_path_get_dirname ("c:\\home\\dingus\\");
167 if (strcmp (s, "c:\\home\\dingus") != 0)
168 return FAILED ("Expected c:\\home\\dingus, got %s", s);
169 g_free (s);
171 s = g_path_get_dirname ("dir.c");
172 if (strcmp (s, ".") != 0)
173 return FAILED ("Expected `.', got %s", s);
174 g_free (s);
176 s = g_path_get_dirname ("c:\\index.html");
177 if (strcmp (s, "c:") != 0)
178 return FAILED ("Expected [c:], got [%s]", s);
179 #else
180 s = g_path_get_dirname ("/home/miguel");
181 if (strcmp (s, "/home") != 0)
182 return FAILED ("Expected /home, got %s", s);
183 g_free (s);
185 s = g_path_get_dirname ("/home/dingus/");
186 if (strcmp (s, "/home/dingus") != 0)
187 return FAILED ("Expected /home/dingus, got %s", s);
188 g_free (s);
190 s = g_path_get_dirname ("dir.c");
191 if (strcmp (s, ".") != 0)
192 return FAILED ("Expected `.', got %s", s);
193 g_free (s);
195 s = g_path_get_dirname ("/index.html");
196 if (strcmp (s, "/") != 0)
197 return FAILED ("Expected [/], got [%s]", s);
198 #endif
199 return OK;
202 char *
203 test_basename ()
205 char *s;
207 #ifdef G_OS_WIN32
208 s = g_path_get_basename ("");
209 if (strcmp (s, ".") != 0)
210 return FAILED ("Expected `.', got %s", s);
211 g_free (s);
213 s = g_path_get_basename ("c:\\home\\dingus\\");
214 if (strcmp (s, "dingus") != 0)
215 return FAILED ("1 Expected dingus, got %s", s);
216 g_free (s);
218 s = g_path_get_basename ("c:/home/dingus/");
219 if (strcmp (s, "dingus") != 0)
220 return FAILED ("1 Expected dingus, got %s", s);
221 g_free (s);
223 s = g_path_get_basename ("c:\\home\\dingus");
224 if (strcmp (s, "dingus") != 0)
225 return FAILED ("2 Expected dingus, got %s", s);
226 g_free (s);
228 s = g_path_get_basename ("c:/home/dingus");
229 if (strcmp (s, "dingus") != 0)
230 return FAILED ("2 Expected dingus, got %s", s);
231 g_free (s);
232 #else
233 s = g_path_get_basename ("");
234 if (strcmp (s, ".") != 0)
235 return FAILED ("Expected `.', got %s", s);
236 g_free (s);
238 s = g_path_get_basename ("/home/dingus/");
239 if (strcmp (s, "dingus") != 0)
240 return FAILED ("1 Expected dingus, got %s", s);
241 g_free (s);
243 s = g_path_get_basename ("/home/dingus");
244 if (strcmp (s, "dingus") != 0)
245 return FAILED ("2 Expected dingus, got %s", s);
246 g_free (s);
247 #endif
248 return OK;
251 gchar *
252 test_ppath ()
254 char *s;
255 #ifdef G_OS_WIN32
256 const gchar *searchfor = "explorer.exe";
257 #else
258 const gchar *searchfor = "ls";
259 #endif
260 s = g_find_program_in_path (searchfor);
261 if (s == NULL)
262 return FAILED ("No %s on this system?", searchfor);
263 g_free (s);
264 return OK;
267 gchar *
268 test_ppath2 ()
270 char *s;
271 const char *path = g_getenv ("PATH");
272 #ifdef G_OS_WIN32
273 const gchar *searchfor = "test_eglib.exe";
274 #else
275 const gchar *searchfor = "test-glib";
276 #endif
278 g_setenv ("PATH", "", TRUE);
279 s = g_find_program_in_path ("ls");
280 if (s != NULL) {
281 g_setenv ("PATH", path, TRUE);
282 return FAILED ("Found something interesting here: %s", s);
284 g_free (s);
285 s = g_find_program_in_path (searchfor);
286 if (s == NULL) {
287 g_setenv ("PATH", path, TRUE);
288 return FAILED ("It should find '%s' in the current directory.", searchfor);
290 g_free (s);
291 g_setenv ("PATH", path, TRUE);
292 return OK;
295 #ifndef DISABLE_FILESYSTEM_TESTS
296 gchar *
297 test_cwd ()
299 char *dir = g_get_current_dir ();
300 #ifdef G_OS_WIN32
301 const gchar *newdir = "C:\\Windows";
302 #else
303 const gchar *newdir = "/bin";
304 #endif
306 if (dir == NULL)
307 return FAILED ("No current directory?");
308 g_free (dir);
310 if (chdir (newdir) == -1)
311 return FAILED ("No %s?", newdir);
313 dir = g_get_current_dir ();
314 if (strcmp (dir, newdir) != 0)
315 return FAILED("Did not go to %s?", newdir);
316 g_free (dir);
318 return OK;
320 #else
321 gchar *
322 test_cwd ()
324 return OK;
326 #endif
328 gchar *
329 test_misc ()
331 const char *home = g_get_home_dir ();
332 const char *tmp = g_get_tmp_dir ();
334 if (home == NULL)
335 return FAILED ("Where did my home go?");
337 if (tmp == NULL)
338 return FAILED ("Where did my /tmp go?");
340 return OK;
343 static Test path_tests [] = {
344 {"g_build_filename", test_buildfname},
345 {"g_buildpath", test_buildpath},
346 {"g_path_get_dirname", test_dirname},
347 {"g_path_get_basename", test_basename},
348 {"g_find_program_in_path", test_ppath},
349 {"g_find_program_in_path2", test_ppath2},
350 {"test_cwd", test_cwd },
351 {"test_misc", test_misc },
352 {NULL, NULL}
355 DEFINE_TEST_GROUP_INIT(path_tests_init, path_tests)