Make sure x86 ATOMIC_CAS doesn't overwrite its own operands.
[mono-debugger.git] / eglib / test / path.c
blob63714817b96b055c2fe2387510549a18537aa53f
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"
13 #ifdef G_OS_WIN32
14 #include <direct.h>
15 #define chdir _chdir
16 #endif
18 /* This test is just to be used with valgrind */
19 RESULT
20 test_buildpath ()
22 char *s;
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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
96 return OK;
99 RESULT
100 test_buildfname ()
102 char *s;
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);
113 return OK;
116 char *
117 test_dirname ()
119 char *s;
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);
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);
132 s = g_path_get_dirname ("dir.c");
133 if (strcmp (s, ".") != 0)
134 return FAILED ("Expected `.', got %s", s);
135 g_free (s);
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);
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);
151 s = g_path_get_dirname ("dir.c");
152 if (strcmp (s, ".") != 0)
153 return FAILED ("Expected `.', got %s", s);
154 g_free (s);
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;
163 char *
164 test_basename ()
166 char *s;
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);
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);
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);
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);
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;
202 gchar *
203 test_ppath ()
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;
218 gchar *
219 test_ppath2 ()
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
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);
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);
241 g_free (s);
242 g_setenv ("PATH", path, TRUE);
243 return OK;
246 gchar *
247 test_cwd ()
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
256 if (dir == NULL)
257 return FAILED ("No current directory?");
258 g_free (dir);
260 if (chdir (newdir) == -1)
261 return FAILED ("No %s?", newdir);
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);
268 return OK;
271 gchar *
272 test_misc ()
274 const char *home = g_get_home_dir ();
275 const char *tmp = g_get_tmp_dir ();
277 if (home == NULL)
278 return FAILED ("Where did my home go?");
280 if (tmp == NULL)
281 return FAILED ("Where did my /tmp go?");
283 return OK;
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}
298 DEFINE_TEST_GROUP_INIT(path_tests_init, path_tests)