path.c 7.3 KB

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