merged Sys.Web.Services 2.0 support in my branch:
[mono.git] / eglib / test / path.c
1 #include <glib.h>
2 #include <string.h>
3 #include <stdio.h>
4 #include <unistd.h>
5 #include <pthread.h>
6 #include "test.h"
7
8 /* This test is just to be used with valgrind */
9 RESULT
10 test_buildpath ()
11 {
12         char *s;
13         
14         s = g_build_path ("/", "hola///", "//mundo", NULL);
15         if (strcmp (s, "hola/mundo") != 0)
16                 return FAILED ("1 Got wrong result, got: %s", s);
17         g_free (s);
18
19         s = g_build_path ("/", "hola/", "/mundo", NULL);
20         if (strcmp (s, "hola/mundo") != 0)
21                 return FAILED ("2 Got wrong result, got: %s", s);
22         g_free (s);
23
24         s = g_build_path ("/", "hola/", "mundo", NULL);
25         if (strcmp (s, "hola/mundo") != 0)
26                 return FAILED ("3 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 ("4 Got wrong result, got: %s", s);
32         g_free (s);
33
34         s = g_build_path ("/", "/hello", "world/", NULL);
35         if (strcmp (s, "/hello/world/") != 0)
36                 return FAILED ("5 Got wrong result, got: %s", s);
37         g_free (s);
38         
39         /* Now test multi-char-separators */
40         s = g_build_path ("**", "hello", "world", NULL);
41         if (strcmp (s, "hello**world") != 0)
42                 return FAILED ("6 Got wrong result, got: %s", s);
43         g_free (s);
44
45         s = g_build_path ("**", "hello**", "world", NULL);
46         if (strcmp (s, "hello**world") != 0)
47                 return FAILED ("7 Got wrong result, got: %s", s);
48         g_free (s);
49
50         s = g_build_path ("**", "hello**", "**world", NULL);
51         if (strcmp (s, "hello**world") != 0)
52                 return FAILED ("8 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 ("9 Got wrong result, got: %s", s);
58         g_free (s);
59
60         s = g_build_path ("1234567890", "hello", "world", NULL);
61         if (strcmp (s, "hello1234567890world") != 0)
62                 return FAILED ("10 Got wrong result, got: %s", s);
63         g_free (s);
64
65         s = g_build_path ("1234567890", "hello1234567890", "1234567890world", NULL);
66         if (strcmp (s, "hello1234567890world") != 0)
67                 return FAILED ("11 Got wrong result, got: %s", s);
68         g_free (s);
69
70         s = g_build_path ("1234567890", "hello12345678901234567890", "1234567890world", NULL);
71         if (strcmp (s, "hello1234567890world") != 0)
72                 return FAILED ("12 Got wrong result, got: %s", s);
73         g_free (s);
74
75         /* Multiple */
76         s = g_build_path ("/", "a", "b", "c", "d", NULL);
77         if (strcmp (s, "a/b/c/d") != 0)
78                 return FAILED ("13 Got wrong result, got: %s", s);
79         g_free (s);
80
81         s = g_build_path ("/", "/a", "", "/c/", NULL);
82         if (strcmp (s, "/a/c/") != 0)
83                 return FAILED ("14 Got wrong result, got: %s", s);
84         g_free (s);
85         return OK;
86         
87         return OK;
88 }
89
90 RESULT
91 test_buildfname ()
92 {
93         char *s;
94         
95         s = g_build_filename ("a", "b", "c", "d", NULL);
96         if (strcmp (s, "a/b/c/d") != 0)
97                 return FAILED ("1 Got wrong result, got: %s", s);
98         g_free (s);
99         
100         return OK;
101 }
102
103 char *
104 test_dirname ()
105 {
106         char *s;
107
108         s = g_path_get_dirname ("/home/miguel");
109         if (strcmp (s, "/home") != 0)
110                 return FAILED ("Expected /home, got %s", s);
111         g_free (s);
112
113         s = g_path_get_dirname ("/home/dingus/");
114         if (strcmp (s, "/home/dingus") != 0)
115                 return FAILED ("Expected /home/dingus, got %s", s);
116         g_free (s);
117
118         s = g_path_get_dirname ("dir.c");
119         if (strcmp (s, ".") != 0)
120                 return FAILED ("Expected `.', got %s", s);
121         g_free (s);
122
123         s = g_path_get_dirname ("/index.html");
124         if (strcmp (s, "/") != 0)
125                 return FAILED ("Expected [/], got [%s]", s);
126         
127         return OK;
128 }
129
130 char *
131 test_basename ()
132 {
133         char *s;
134
135         s = g_path_get_basename ("");
136         if (strcmp (s, ".") != 0)
137                 return FAILED ("Expected `.', got %s", s);
138         g_free (s);
139
140         s = g_path_get_basename ("/home/dingus/");
141         if (strcmp (s, "dingus") != 0)
142                 return FAILED ("1 Expected dingus, got %s", s);
143         g_free (s);
144
145         s = g_path_get_basename ("/home/dingus");
146         if (strcmp (s, "dingus") != 0)
147                 return FAILED ("2 Expected dingus, got %s", s);
148         g_free (s);
149
150         return OK;
151 }
152
153 gchar *
154 test_ppath ()
155 {
156         char *s;
157         
158         s = g_find_program_in_path ("ls");
159         if (s == NULL)
160                 return FAILED ("No shell on this system (This assumes Unix)?");
161         g_free (s);
162         return OK;
163 }
164
165 gchar *
166 test_ppath2 ()
167 {
168         char *s;
169         const char *path = g_getenv ("PATH");
170         
171         g_setenv ("PATH", "", TRUE);
172         s = g_find_program_in_path ("ls");
173         if (s != NULL) {
174                 g_setenv ("PATH", path, TRUE);
175                 return FAILED ("Found something interesting here: %s", s);
176         }
177         g_free (s);
178         s = g_find_program_in_path ("test-glib");
179         if (s == NULL) {
180                 g_setenv ("PATH", path, TRUE);
181                 return FAILED ("It should find 'test-glib' in the current directory.");
182         }
183         g_free (s);
184         g_setenv ("PATH", path, TRUE);
185         return OK;
186 }
187
188 gchar *
189 test_cwd ()
190 {
191         char *dir = g_get_current_dir ();
192
193         if (dir == NULL)
194                 return FAILED ("No current directory?");
195         g_free (dir);
196         
197         if (chdir ("/bin") == -1)
198                 return FAILED ("No /bin?");
199         
200         dir = g_get_current_dir ();
201         if (strcmp (dir, "/bin") != 0)
202                 return FAILED("Did not go to /bin?");
203         g_free (dir);
204         
205         return OK;
206 }
207
208 gchar *
209 test_misc ()
210 {
211         const char *home = g_get_home_dir ();
212         const char *tmp = g_get_tmp_dir ();
213         
214         if (home == NULL)
215                 return FAILED ("Where did my home go?");
216
217         if (tmp == NULL)
218                 return FAILED ("Where did my /tmp go?");
219
220         return OK;
221 }
222
223 static Test path_tests [] = {
224         {"g_buildpath", test_buildpath},
225         {"g_build_filename", test_buildfname},
226         {"g_path_get_dirname", test_dirname},
227         {"g_path_get_basename", test_basename},
228         {"g_find_program_in_path", test_ppath},
229         {"g_find_program_in_path2", test_ppath2},
230         {"test_cwd", test_cwd },
231         {"test_misc", test_misc },
232         {NULL, NULL}
233 };
234
235 DEFINE_TEST_GROUP_INIT(path_tests_init, path_tests)
236