2007-10-19 Nagappan A <anagappan@novell.com>
[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         return OK;
97 }
98
99 RESULT
100 test_buildfname ()
101 {
102         char *s;
103         
104         s = g_build_filename ("a", "b", "c", "d", NULL);
105 #ifdef G_OS_WIN32
106         if (strcmp (s, "a\\b\\c\\d") != 0)
107 #else
108         if (strcmp (s, "a/b/c/d") != 0)
109 #endif
110                 return FAILED ("1 Got wrong result, got: %s", s);
111         g_free (s);
112         
113         return OK;
114 }
115
116 char *
117 test_dirname ()
118 {
119         char *s;
120
121 #ifdef G_OS_WIN32
122         s = g_path_get_dirname ("c:\\home\\miguel");
123         if (strcmp (s, "c:\\home") != 0)
124                 return FAILED ("Expected c:\\home, got %s", s);
125         g_free (s);
126
127         s = g_path_get_dirname ("c:\\home\\dingus\\");
128         if (strcmp (s, "c:\\home\\dingus") != 0)
129                 return FAILED ("Expected c:\\home\\dingus, got %s", s);
130         g_free (s);
131
132         s = g_path_get_dirname ("dir.c");
133         if (strcmp (s, ".") != 0)
134                 return FAILED ("Expected `.', got %s", s);
135         g_free (s);
136
137         s = g_path_get_dirname ("c:\\index.html");
138         if (strcmp (s, "c:") != 0)
139                 return FAILED ("Expected [c:], got [%s]", s);
140 #else
141         s = g_path_get_dirname ("/home/miguel");
142         if (strcmp (s, "/home") != 0)
143                 return FAILED ("Expected /home, got %s", s);
144         g_free (s);
145
146         s = g_path_get_dirname ("/home/dingus/");
147         if (strcmp (s, "/home/dingus") != 0)
148                 return FAILED ("Expected /home/dingus, got %s", s);
149         g_free (s);
150
151         s = g_path_get_dirname ("dir.c");
152         if (strcmp (s, ".") != 0)
153                 return FAILED ("Expected `.', got %s", s);
154         g_free (s);
155
156         s = g_path_get_dirname ("/index.html");
157         if (strcmp (s, "/") != 0)
158                 return FAILED ("Expected [/], got [%s]", s);
159 #endif  
160         return OK;
161 }
162
163 char *
164 test_basename ()
165 {
166         char *s;
167
168 #ifdef G_OS_WIN32
169         s = g_path_get_basename ("");
170         if (strcmp (s, ".") != 0)
171                 return FAILED ("Expected `.', got %s", s);
172         g_free (s);
173
174         s = g_path_get_basename ("c:\\home\\dingus\\");
175         if (strcmp (s, "dingus") != 0)
176                 return FAILED ("1 Expected dingus, got %s", s);
177         g_free (s);
178
179         s = g_path_get_basename ("c:\\home\\dingus");
180         if (strcmp (s, "dingus") != 0)
181                 return FAILED ("2 Expected dingus, got %s", s);
182         g_free (s);
183 #else
184         s = g_path_get_basename ("");
185         if (strcmp (s, ".") != 0)
186                 return FAILED ("Expected `.', got %s", s);
187         g_free (s);
188
189         s = g_path_get_basename ("/home/dingus/");
190         if (strcmp (s, "dingus") != 0)
191                 return FAILED ("1 Expected dingus, got %s", s);
192         g_free (s);
193
194         s = g_path_get_basename ("/home/dingus");
195         if (strcmp (s, "dingus") != 0)
196                 return FAILED ("2 Expected dingus, got %s", s);
197         g_free (s);
198 #endif
199         return OK;
200 }
201
202 gchar *
203 test_ppath ()
204 {
205         char *s;
206 #ifdef G_OS_WIN32
207         const gchar *searchfor = "explorer.exe";
208 #else
209         const gchar *searchfor = "ls";
210 #endif
211         s = g_find_program_in_path (searchfor);
212         if (s == NULL)
213                 return FAILED ("No %s on this system?", searchfor);
214         g_free (s);
215         return OK;
216 }
217
218 gchar *
219 test_ppath2 ()
220 {
221         char *s;
222         const char *path = g_getenv ("PATH");
223 #ifdef G_OS_WIN32
224         const gchar *searchfor = "test_eglib.exe";
225 #else
226         const gchar *searchfor = "test-glib";
227 #endif
228         
229         g_setenv ("PATH", "", TRUE);
230         s = g_find_program_in_path ("ls");
231         if (s != NULL) {
232                 g_setenv ("PATH", path, TRUE);
233                 return FAILED ("Found something interesting here: %s", s);
234         }
235         g_free (s);
236         s = g_find_program_in_path (searchfor);
237         if (s == NULL) {
238                 g_setenv ("PATH", path, TRUE);
239                 return FAILED ("It should find '%s' in the current directory.", searchfor);
240         }
241         g_free (s);
242         g_setenv ("PATH", path, TRUE);
243         return OK;
244 }
245
246 gchar *
247 test_cwd ()
248 {
249         char *dir = g_get_current_dir ();
250 #ifdef G_OS_WIN32
251         const gchar *newdir = "C:\\Windows";
252 #else
253         const gchar *newdir = "/bin";
254 #endif
255
256         if (dir == NULL)
257                 return FAILED ("No current directory?");
258         g_free (dir);
259         
260         if (chdir (newdir) == -1)
261                 return FAILED ("No %s?", newdir);
262         
263         dir = g_get_current_dir ();
264         if (strcmp (dir, newdir) != 0)
265                 return FAILED("Did not go to %s?", newdir);
266         g_free (dir);
267         
268         return OK;
269 }
270
271 gchar *
272 test_misc ()
273 {
274         const char *home = g_get_home_dir ();
275         const char *tmp = g_get_tmp_dir ();
276         
277         if (home == NULL)
278                 return FAILED ("Where did my home go?");
279
280         if (tmp == NULL)
281                 return FAILED ("Where did my /tmp go?");
282
283         return OK;
284 }
285
286 static Test path_tests [] = {
287         {"g_buildpath", test_buildpath},
288         {"g_build_filename", test_buildfname},
289         {"g_path_get_dirname", test_dirname},
290         {"g_path_get_basename", test_basename},
291         {"g_find_program_in_path", test_ppath},
292         {"g_find_program_in_path2", test_ppath2},
293         {"test_cwd", test_cwd },
294         {"test_misc", test_misc },
295         {NULL, NULL}
296 };
297
298 DEFINE_TEST_GROUP_INIT(path_tests_init, path_tests)
299
300