[xbuild] Fix test on windows.
[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         
24         s = g_build_path ("/", "hola///", "//mundo", NULL);
25         if (strcmp (s, "hola/mundo") != 0)
26                 return FAILED ("1 Got wrong result, got: %s", s);
27         g_free (s);
28
29         s = g_build_path ("/", "hola/", "/mundo", NULL);
30         if (strcmp (s, "hola/mundo") != 0)
31                 return FAILED ("2 Got wrong result, got: %s", s);
32         g_free (s);
33
34         s = g_build_path ("/", "hola/", "mundo", NULL);
35         if (strcmp (s, "hola/mundo") != 0)
36                 return FAILED ("3 Got wrong result, got: %s", s);
37         g_free (s);
38
39         s = g_build_path ("/", "hola", "/mundo", NULL);
40         if (strcmp (s, "hola/mundo") != 0)
41                 return FAILED ("4 Got wrong result, got: %s", s);
42         g_free (s);
43
44         s = g_build_path ("/", "/hello", "world/", NULL);
45         if (strcmp (s, "/hello/world/") != 0)
46                 return FAILED ("5 Got wrong result, got: %s", s);
47         g_free (s);
48         
49         /* Now test multi-char-separators */
50         s = g_build_path ("**", "hello", "world", NULL);
51         if (strcmp (s, "hello**world") != 0)
52                 return FAILED ("6 Got wrong result, got: %s", s);
53         g_free (s);
54
55         s = g_build_path ("**", "hello**", "world", NULL);
56         if (strcmp (s, "hello**world") != 0)
57                 return FAILED ("7 Got wrong result, got: %s", s);
58         g_free (s);
59
60         s = g_build_path ("**", "hello**", "**world", NULL);
61         if (strcmp (s, "hello**world") != 0)
62                 return FAILED ("8 Got wrong result, got: %s", s);
63         g_free (s);
64         
65         s = g_build_path ("**", "hello**", "**world", NULL);
66         if (strcmp (s, "hello**world") != 0)
67                 return FAILED ("9 Got wrong result, got: %s", s);
68         g_free (s);
69
70         s = g_build_path ("1234567890", "hello", "world", NULL);
71         if (strcmp (s, "hello1234567890world") != 0)
72                 return FAILED ("10 Got wrong result, got: %s", s);
73         g_free (s);
74
75         s = g_build_path ("1234567890", "hello1234567890", "1234567890world", NULL);
76         if (strcmp (s, "hello1234567890world") != 0)
77                 return FAILED ("11 Got wrong result, got: %s", s);
78         g_free (s);
79
80         s = g_build_path ("1234567890", "hello12345678901234567890", "1234567890world", NULL);
81         if (strcmp (s, "hello1234567890world") != 0)
82                 return FAILED ("12 Got wrong result, got: %s", s);
83         g_free (s);
84
85         /* Multiple */
86         s = g_build_path ("/", "a", "b", "c", "d", NULL);
87         if (strcmp (s, "a/b/c/d") != 0)
88                 return FAILED ("13 Got wrong result, got: %s", s);
89         g_free (s);
90
91         s = g_build_path ("/", "/a", "", "/c/", NULL);
92         if (strcmp (s, "/a/c/") != 0)
93                 return FAILED ("14 Got wrong result, got: %s", s);
94         g_free (s);
95
96         /* Null */
97         s = g_build_path ("/", NULL, NULL);
98         if (s == NULL)
99                 return FAILED ("must get a non-NULL return");
100         if (s [0] != 0)
101                 return FAILED ("must get an empty string");
102         g_free (s);
103         return OK;
104 }
105
106 RESULT
107 test_buildfname ()
108 {
109         char *s;
110         
111         s = g_build_filename ("a", "b", "c", "d", NULL);
112 #ifdef G_OS_WIN32
113         if (strcmp (s, "a\\b\\c\\d") != 0)
114 #else
115         if (strcmp (s, "a/b/c/d") != 0)
116 #endif
117                 return FAILED ("1 Got wrong result, got: %s", s);
118         g_free (s);
119
120         s = g_build_filename ("/", "a", NULL);
121 #ifdef G_OS_WIN32
122         if (strcmp (s, "\\a") != 0)
123 #else
124         if (strcmp (s, "/a") != 0)
125 #endif
126                 return FAILED ("1 Got wrong result, got: %s", s);
127
128 #ifndef OS_WIN32
129         s = g_build_filename ("/", "foo", "/bar", "tolo/", "/meo/", NULL);
130         if (strcmp (s, "/foo/bar/tolo/meo/") != 0)
131                 return FAILED ("1 Got wrong result, got: %s", s);
132 #endif
133         
134         return OK;
135 }
136
137 char *
138 test_dirname ()
139 {
140         char *s;
141
142 #ifdef G_OS_WIN32
143         s = g_path_get_dirname ("c:\\home\\miguel");
144         if (strcmp (s, "c:\\home") != 0)
145                 return FAILED ("Expected c:\\home, got %s", s);
146         g_free (s);
147
148         s = g_path_get_dirname ("c:\\home\\dingus\\");
149         if (strcmp (s, "c:\\home\\dingus") != 0)
150                 return FAILED ("Expected c:\\home\\dingus, got %s", s);
151         g_free (s);
152
153         s = g_path_get_dirname ("dir.c");
154         if (strcmp (s, ".") != 0)
155                 return FAILED ("Expected `.', got %s", s);
156         g_free (s);
157
158         s = g_path_get_dirname ("c:\\index.html");
159         if (strcmp (s, "c:") != 0)
160                 return FAILED ("Expected [c:], got [%s]", s);
161 #else
162         s = g_path_get_dirname ("/home/miguel");
163         if (strcmp (s, "/home") != 0)
164                 return FAILED ("Expected /home, got %s", s);
165         g_free (s);
166
167         s = g_path_get_dirname ("/home/dingus/");
168         if (strcmp (s, "/home/dingus") != 0)
169                 return FAILED ("Expected /home/dingus, got %s", s);
170         g_free (s);
171
172         s = g_path_get_dirname ("dir.c");
173         if (strcmp (s, ".") != 0)
174                 return FAILED ("Expected `.', got %s", s);
175         g_free (s);
176
177         s = g_path_get_dirname ("/index.html");
178         if (strcmp (s, "/") != 0)
179                 return FAILED ("Expected [/], got [%s]", s);
180 #endif  
181         return OK;
182 }
183
184 char *
185 test_basename ()
186 {
187         char *s;
188
189 #ifdef G_OS_WIN32
190         s = g_path_get_basename ("");
191         if (strcmp (s, ".") != 0)
192                 return FAILED ("Expected `.', got %s", s);
193         g_free (s);
194
195         s = g_path_get_basename ("c:\\home\\dingus\\");
196         if (strcmp (s, "dingus") != 0)
197                 return FAILED ("1 Expected dingus, got %s", s);
198         g_free (s);
199
200         s = g_path_get_basename ("c:\\home\\dingus");
201         if (strcmp (s, "dingus") != 0)
202                 return FAILED ("2 Expected dingus, got %s", s);
203         g_free (s);
204 #else
205         s = g_path_get_basename ("");
206         if (strcmp (s, ".") != 0)
207                 return FAILED ("Expected `.', got %s", s);
208         g_free (s);
209
210         s = g_path_get_basename ("/home/dingus/");
211         if (strcmp (s, "dingus") != 0)
212                 return FAILED ("1 Expected dingus, got %s", s);
213         g_free (s);
214
215         s = g_path_get_basename ("/home/dingus");
216         if (strcmp (s, "dingus") != 0)
217                 return FAILED ("2 Expected dingus, got %s", s);
218         g_free (s);
219 #endif
220         return OK;
221 }
222
223 gchar *
224 test_ppath ()
225 {
226         char *s;
227 #ifdef G_OS_WIN32
228         const gchar *searchfor = "explorer.exe";
229 #else
230         const gchar *searchfor = "ls";
231 #endif
232         s = g_find_program_in_path (searchfor);
233         if (s == NULL)
234                 return FAILED ("No %s on this system?", searchfor);
235         g_free (s);
236         return OK;
237 }
238
239 gchar *
240 test_ppath2 ()
241 {
242         char *s;
243         const char *path = g_getenv ("PATH");
244 #ifdef G_OS_WIN32
245         const gchar *searchfor = "test_eglib.exe";
246 #else
247         const gchar *searchfor = "test-glib";
248 #endif
249         
250         g_setenv ("PATH", "", TRUE);
251         s = g_find_program_in_path ("ls");
252         if (s != NULL) {
253                 g_setenv ("PATH", path, TRUE);
254                 return FAILED ("Found something interesting here: %s", s);
255         }
256         g_free (s);
257         s = g_find_program_in_path (searchfor);
258         if (s == NULL) {
259                 g_setenv ("PATH", path, TRUE);
260                 return FAILED ("It should find '%s' in the current directory.", searchfor);
261         }
262         g_free (s);
263         g_setenv ("PATH", path, TRUE);
264         return OK;
265 }
266
267 #ifndef DISABLE_FILESYSTEM_TESTS
268 gchar *
269 test_cwd ()
270 {
271         char *dir = g_get_current_dir ();
272 #ifdef G_OS_WIN32
273         const gchar *newdir = "C:\\Windows";
274 #else
275         const gchar *newdir = "/bin";
276 #endif
277
278         if (dir == NULL)
279                 return FAILED ("No current directory?");
280         g_free (dir);
281         
282         if (chdir (newdir) == -1)
283                 return FAILED ("No %s?", newdir);
284         
285         dir = g_get_current_dir ();
286         if (strcmp (dir, newdir) != 0)
287                 return FAILED("Did not go to %s?", newdir);
288         g_free (dir);
289         
290         return OK;
291 }
292 #else
293 gchar *
294 test_cwd ()
295 {
296         return OK;
297 }
298 #endif
299
300 gchar *
301 test_misc ()
302 {
303         const char *home = g_get_home_dir ();
304         const char *tmp = g_get_tmp_dir ();
305         
306         if (home == NULL)
307                 return FAILED ("Where did my home go?");
308
309         if (tmp == NULL)
310                 return FAILED ("Where did my /tmp go?");
311
312         return OK;
313 }
314
315 static Test path_tests [] = {
316         {"g_build_filename", test_buildfname},
317         {"g_buildpath", test_buildpath},
318         {"g_path_get_dirname", test_dirname},
319         {"g_path_get_basename", test_basename},
320         {"g_find_program_in_path", test_ppath},
321         {"g_find_program_in_path2", test_ppath2},
322         {"test_cwd", test_cwd },
323         {"test_misc", test_misc },
324         {NULL, NULL}
325 };
326
327 DEFINE_TEST_GROUP_INIT(path_tests_init, path_tests)
328
329