New test.
[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         return OK;
82 }
83
84 RESULT
85 test_buildfname ()
86 {
87         char *s;
88         
89         s = g_build_filename ("a", "b", "c", "d", NULL);
90         if (strcmp (s, "a/b/c/d") != 0)
91                 return FAILED ("1 Got wrong result, got: %s", s);
92         g_free (s);
93         
94         return OK;
95 }
96
97 char *
98 test_dirname ()
99 {
100         char *s;
101
102         s = g_path_get_dirname ("/home/miguel");
103         if (strcmp (s, "/home") != 0)
104                 return FAILED ("Expected /home, got %s", s);
105         g_free (s);
106
107         s = g_path_get_dirname ("/home/dingus/");
108         if (strcmp (s, "/home/dingus") != 0)
109                 return FAILED ("Expected /home/dingus, got %s", s);
110         g_free (s);
111         
112         return OK;
113 }
114
115 char *
116 test_basename ()
117 {
118         char *s;
119
120         s = g_path_get_basename ("");
121         if (strcmp (s, ".") != 0)
122                 return FAILED ("Expected `.', got %s", s);
123         g_free (s);
124
125         s = g_path_get_basename ("/home/dingus/");
126         if (strcmp (s, "dingus") != 0)
127                 return FAILED ("1 Expected dingus, got %s", s);
128         g_free (s);
129
130         s = g_path_get_basename ("/home/dingus");
131         if (strcmp (s, "dingus") != 0)
132                 return FAILED ("2 Expected dingus, got %s", s);
133         g_free (s);
134
135         return OK;
136 }
137
138 gchar *
139 test_ppath ()
140 {
141         char *s;
142         
143         s = g_find_program_in_path ("ls");
144         if (s == NULL)
145                 return FAILED ("No shell on this system (This assumes Unix)?");
146         g_free (s);
147         return OK;
148 }
149
150 gchar *
151 test_ppath2 ()
152 {
153         char *s;
154         const char *path = g_getenv ("PATH");
155         
156         g_setenv ("PATH", "", TRUE);
157         s = g_find_program_in_path ("ls");
158         if (s != NULL) {
159                 g_setenv ("PATH", path, TRUE);
160                 return FAILED ("Found something interesting here: %s", s);
161         }
162         g_free (s);
163         s = g_find_program_in_path ("test-glib");
164         if (s == NULL) {
165                 g_setenv ("PATH", path, TRUE);
166                 return FAILED ("It should find 'test-glib' in the current directory.");
167         }
168         g_free (s);
169         g_setenv ("PATH", path, TRUE);
170         return OK;
171 }
172
173 gchar *
174 test_cwd ()
175 {
176         char *dir = g_get_current_dir ();
177
178         if (dir == NULL)
179                 return FAILED ("No current directory?");
180         g_free (dir);
181         
182         if (chdir ("/bin") == -1)
183                 return FAILED ("No /bin?");
184         
185         dir = g_get_current_dir ();
186         if (strcmp (dir, "/bin") != 0)
187                 return FAILED("Did not go to /bin?");
188         g_free (dir);
189         
190         return OK;
191 }
192
193 gchar *
194 test_misc ()
195 {
196         const char *home = g_get_home_dir ();
197         const char *tmp = g_get_tmp_dir ();
198         
199         if (home == NULL)
200                 return FAILED ("Where did my home go?");
201
202         if (tmp == NULL)
203                 return FAILED ("Where did my /tmp go?");
204
205         return OK;
206 }
207
208 static Test path_tests [] = {
209         {"g_buildpath", test_buildpath},
210         {"g_build_filename", test_buildfname},
211         {"g_path_get_dirname", test_dirname},
212         {"g_path_get_basename", test_basename},
213         {"g_find_program_in_path", test_ppath},
214         {"g_find_program_in_path2", test_ppath2},
215         {"test_cwd", test_cwd },
216         {"test_misc", test_misc },
217         {NULL, NULL}
218 };
219
220 DEFINE_TEST_GROUP_INIT(path_tests_init, path_tests)
221