path.c 6.8 KB

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