aa13ece6f7799c764fd99594d6ba1c58b61e77fa
[mono.git] / eglib / test / path.c
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"
12
13 #ifdef G_OS_WIN32
14 #include <direct.h>
15 #define chdir _chdir
16 #endif
17
18 /* This test is just to be used with valgrind */
19 RESULT
20 test_buildpath ()
21 {
22         char *s;
23         char *buffer = "var/private";
24         char *dir = "/";
25         
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);
30
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);
35
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);
40
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);
45
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);
50         
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);
56
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);
61
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);
66         
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);
71
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);
76
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);
81
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);
86
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);
92
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);
97
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");
104
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");
113
114         g_free (s);
115         return OK;
116 }
117
118 RESULT
119 test_buildfname ()
120 {
121         char *s;
122         
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);
131
132         s = g_build_filename ("/", "a", NULL);
133 #ifdef G_OS_WIN32
134         if (strcmp (s, "\\a") != 0)
135 #else
136         if (strcmp (s, "/a") != 0)
137 #endif
138                 return FAILED ("1 Got wrong result, got: %s", s);
139
140 #ifndef OS_WIN32
141         s = g_build_filename ("/", "foo", "/bar", "tolo/", "/meo/", NULL);
142         if (strcmp (s, "/foo/bar/tolo/meo/") != 0)
143                 return FAILED ("1 Got wrong result, got: %s", s);
144 #endif
145         
146         return OK;
147 }
148
149 char *
150 test_dirname ()
151 {
152         char *s;
153
154 #ifdef G_OS_WIN32
155         s = g_path_get_dirname ("c:\\home\\miguel");
156         if (strcmp (s, "c:\\home") != 0)
157                 return FAILED ("Expected c:\\home, got %s", s);
158         g_free (s);
159
160         s = g_path_get_dirname ("c:\\home\\dingus\\");
161         if (strcmp (s, "c:\\home\\dingus") != 0)
162                 return FAILED ("Expected c:\\home\\dingus, got %s", s);
163         g_free (s);
164
165         s = g_path_get_dirname ("dir.c");
166         if (strcmp (s, ".") != 0)
167                 return FAILED ("Expected `.', got %s", s);
168         g_free (s);
169
170         s = g_path_get_dirname ("c:\\index.html");
171         if (strcmp (s, "c:") != 0)
172                 return FAILED ("Expected [c:], got [%s]", s);
173 #else
174         s = g_path_get_dirname ("/home/miguel");
175         if (strcmp (s, "/home") != 0)
176                 return FAILED ("Expected /home, got %s", s);
177         g_free (s);
178
179         s = g_path_get_dirname ("/home/dingus/");
180         if (strcmp (s, "/home/dingus") != 0)
181                 return FAILED ("Expected /home/dingus, got %s", s);
182         g_free (s);
183
184         s = g_path_get_dirname ("dir.c");
185         if (strcmp (s, ".") != 0)
186                 return FAILED ("Expected `.', got %s", s);
187         g_free (s);
188
189         s = g_path_get_dirname ("/index.html");
190         if (strcmp (s, "/") != 0)
191                 return FAILED ("Expected [/], got [%s]", s);
192 #endif  
193         return OK;
194 }
195
196 char *
197 test_basename ()
198 {
199         char *s;
200
201 #ifdef G_OS_WIN32
202         s = g_path_get_basename ("");
203         if (strcmp (s, ".") != 0)
204                 return FAILED ("Expected `.', got %s", s);
205         g_free (s);
206
207         s = g_path_get_basename ("c:\\home\\dingus\\");
208         if (strcmp (s, "dingus") != 0)
209                 return FAILED ("1 Expected dingus, got %s", s);
210         g_free (s);
211
212         s = g_path_get_basename ("c:\\home\\dingus");
213         if (strcmp (s, "dingus") != 0)
214                 return FAILED ("2 Expected dingus, got %s", s);
215         g_free (s);
216 #else
217         s = g_path_get_basename ("");
218         if (strcmp (s, ".") != 0)
219                 return FAILED ("Expected `.', got %s", s);
220         g_free (s);
221
222         s = g_path_get_basename ("/home/dingus/");
223         if (strcmp (s, "dingus") != 0)
224                 return FAILED ("1 Expected dingus, got %s", s);
225         g_free (s);
226
227         s = g_path_get_basename ("/home/dingus");
228         if (strcmp (s, "dingus") != 0)
229                 return FAILED ("2 Expected dingus, got %s", s);
230         g_free (s);
231 #endif
232         return OK;
233 }
234
235 gchar *
236 test_ppath ()
237 {
238         char *s;
239 #ifdef G_OS_WIN32
240         const gchar *searchfor = "explorer.exe";
241 #else
242         const gchar *searchfor = "ls";
243 #endif
244         s = g_find_program_in_path (searchfor);
245         if (s == NULL)
246                 return FAILED ("No %s on this system?", searchfor);
247         g_free (s);
248         return OK;
249 }
250
251 gchar *
252 test_ppath2 ()
253 {
254         char *s;
255         const char *path = g_getenv ("PATH");
256 #ifdef G_OS_WIN32
257         const gchar *searchfor = "test_eglib.exe";
258 #else
259         const gchar *searchfor = "test-glib";
260 #endif
261         
262         g_setenv ("PATH", "", TRUE);
263         s = g_find_program_in_path ("ls");
264         if (s != NULL) {
265                 g_setenv ("PATH", path, TRUE);
266                 return FAILED ("Found something interesting here: %s", s);
267         }
268         g_free (s);
269         s = g_find_program_in_path (searchfor);
270         if (s == NULL) {
271                 g_setenv ("PATH", path, TRUE);
272                 return FAILED ("It should find '%s' in the current directory.", searchfor);
273         }
274         g_free (s);
275         g_setenv ("PATH", path, TRUE);
276         return OK;
277 }
278
279 #ifndef DISABLE_FILESYSTEM_TESTS
280 gchar *
281 test_cwd ()
282 {
283         char *dir = g_get_current_dir ();
284 #ifdef G_OS_WIN32
285         const gchar *newdir = "C:\\Windows";
286 #else
287         const gchar *newdir = "/bin";
288 #endif
289
290         if (dir == NULL)
291                 return FAILED ("No current directory?");
292         g_free (dir);
293         
294         if (chdir (newdir) == -1)
295                 return FAILED ("No %s?", newdir);
296         
297         dir = g_get_current_dir ();
298         if (strcmp (dir, newdir) != 0)
299                 return FAILED("Did not go to %s?", newdir);
300         g_free (dir);
301         
302         return OK;
303 }
304 #else
305 gchar *
306 test_cwd ()
307 {
308         return OK;
309 }
310 #endif
311
312 gchar *
313 test_misc ()
314 {
315         const char *home = g_get_home_dir ();
316         const char *tmp = g_get_tmp_dir ();
317         
318         if (home == NULL)
319                 return FAILED ("Where did my home go?");
320
321         if (tmp == NULL)
322                 return FAILED ("Where did my /tmp go?");
323
324         return OK;
325 }
326
327 static Test path_tests [] = {
328         {"g_build_filename", test_buildfname},
329         {"g_buildpath", test_buildpath},
330         {"g_path_get_dirname", test_dirname},
331         {"g_path_get_basename", test_basename},
332         {"g_find_program_in_path", test_ppath},
333         {"g_find_program_in_path2", test_ppath2},
334         {"test_cwd", test_cwd },
335         {"test_misc", test_misc },
336         {NULL, NULL}
337 };
338
339 DEFINE_TEST_GROUP_INIT(path_tests_init, path_tests)
340
341