path_cmp.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. /* lib/vfs - vfs_path_t compare functions
  2. Copyright (C) 2011-2019
  3. Free Software Foundation, Inc.
  4. Written by:
  5. Slava Zanko <slavazanko@gmail.com>, 2011, 2013
  6. This file is part of the Midnight Commander.
  7. The Midnight Commander is free software: you can redistribute it
  8. and/or modify it under the terms of the GNU General Public License as
  9. published by the Free Software Foundation, either version 3 of the License,
  10. or (at your option) any later version.
  11. The Midnight Commander is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. GNU General Public License for more details.
  15. You should have received a copy of the GNU General Public License
  16. along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #define TEST_SUITE_NAME "/lib/vfs"
  19. #include "tests/mctest.h"
  20. #ifdef HAVE_CHARSET
  21. #include "lib/charsets.h"
  22. #endif
  23. #include "lib/strutil.h"
  24. #include "lib/vfs/xdirentry.h"
  25. #include "lib/vfs/path.h"
  26. #include "src/vfs/local/local.c"
  27. /* --------------------------------------------------------------------------------------------- */
  28. /* @Before */
  29. static void
  30. setup (void)
  31. {
  32. str_init_strings (NULL);
  33. vfs_init ();
  34. vfs_init_localfs ();
  35. vfs_setup_work_dir ();
  36. mc_global.sysconfig_dir = (char *) TEST_SHARE_DIR;
  37. #ifdef HAVE_CHARSET
  38. load_codepages_list ();
  39. #endif
  40. }
  41. /* --------------------------------------------------------------------------------------------- */
  42. /* @After */
  43. static void
  44. teardown (void)
  45. {
  46. #ifdef HAVE_CHARSET
  47. free_codepages_list ();
  48. #endif
  49. vfs_shut ();
  50. str_uninit_strings ();
  51. }
  52. /* --------------------------------------------------------------------------------------------- */
  53. /* @DataSource("test_path_equal_ds") */
  54. /* *INDENT-OFF* */
  55. static const struct test_path_equal_ds
  56. {
  57. const char *input_path1;
  58. const char *input_path2;
  59. const gboolean expected_result;
  60. } test_path_equal_ds[] =
  61. {
  62. { /* 0. */
  63. NULL,
  64. NULL,
  65. FALSE
  66. },
  67. { /* 1. */
  68. NULL,
  69. "/test/path",
  70. FALSE
  71. },
  72. { /* 2. */
  73. "/test/path",
  74. NULL,
  75. FALSE
  76. },
  77. { /* 3. */
  78. "/test/path",
  79. "/test/path",
  80. TRUE
  81. },
  82. #ifdef HAVE_CHARSET
  83. { /* 4. */
  84. "/#enc:KOI8-R/тестовый/путь",
  85. "/тестовый/путь",
  86. FALSE
  87. },
  88. { /* 5. */
  89. "/тестовый/путь",
  90. "/#enc:KOI8-R/тестовый/путь",
  91. FALSE
  92. },
  93. { /* 6. */
  94. "/#enc:KOI8-R/тестовый/путь",
  95. "/#enc:KOI8-R/тестовый/путь",
  96. TRUE
  97. },
  98. #endif
  99. };
  100. /* *INDENT-ON* */
  101. /* @Test(dataSource = "test_path_equal_ds") */
  102. /* *INDENT-OFF* */
  103. START_PARAMETRIZED_TEST (test_path_equal, test_path_equal_ds)
  104. /* *INDENT-ON* */
  105. {
  106. /* given */
  107. vfs_path_t *vpath1, *vpath2;
  108. gboolean actual_result;
  109. vpath1 = vfs_path_from_str (data->input_path1);
  110. vpath2 = vfs_path_from_str (data->input_path2);
  111. /* when */
  112. actual_result = vfs_path_equal (vpath1, vpath2);
  113. /* then */
  114. mctest_assert_int_eq (actual_result, data->expected_result);
  115. vfs_path_free (vpath1);
  116. vfs_path_free (vpath2);
  117. }
  118. /* *INDENT-OFF* */
  119. END_PARAMETRIZED_TEST
  120. /* *INDENT-ON* */
  121. /* --------------------------------------------------------------------------------------------- */
  122. /* @DataSource("test_path_equal_len_ds") */
  123. /* *INDENT-OFF* */
  124. static const struct test_path_equal_len_ds
  125. {
  126. const char *input_path1;
  127. const char *input_path2;
  128. const size_t input_length;
  129. const gboolean expected_result;
  130. } test_path_equal_len_ds[] =
  131. {
  132. { /* 0. */
  133. NULL,
  134. NULL,
  135. 0,
  136. FALSE
  137. },
  138. { /* 1. */
  139. NULL,
  140. NULL,
  141. 100,
  142. FALSE
  143. },
  144. { /* 2. */
  145. NULL,
  146. "/тестовый/путь",
  147. 10,
  148. FALSE
  149. },
  150. { /* 3. */
  151. "/тестовый/путь",
  152. NULL,
  153. 10,
  154. FALSE
  155. },
  156. { /* 4. */
  157. "/тестовый/путь",
  158. "/тестовый/путь",
  159. 10,
  160. TRUE
  161. },
  162. { /* 5. */
  163. "/тест/овый/путь",
  164. "/тестовый/путь",
  165. 8,
  166. TRUE
  167. },
  168. { /* 6. */
  169. "/тест/овый/путь",
  170. "/тестовый/путь",
  171. 10,
  172. FALSE
  173. },
  174. { /* 7. */
  175. "/тестовый/путь",
  176. "/тест/овый/путь",
  177. 10,
  178. FALSE
  179. },
  180. };
  181. /* *INDENT-ON* */
  182. /* @Test(dataSource = "test_path_equal_len_ds") */
  183. /* *INDENT-OFF* */
  184. START_PARAMETRIZED_TEST (test_path_equal_len, test_path_equal_len_ds)
  185. /* *INDENT-ON* */
  186. {
  187. /* given */
  188. vfs_path_t *vpath1, *vpath2;
  189. gboolean actual_result;
  190. vpath1 = vfs_path_from_str (data->input_path1);
  191. vpath2 = vfs_path_from_str (data->input_path2);
  192. /* when */
  193. actual_result = vfs_path_equal_len (vpath1, vpath2, data->input_length);
  194. /* then */
  195. mctest_assert_int_eq (actual_result, data->expected_result);
  196. vfs_path_free (vpath1);
  197. vfs_path_free (vpath2);
  198. }
  199. /* *INDENT-OFF* */
  200. END_PARAMETRIZED_TEST
  201. /* *INDENT-ON* */
  202. /* --------------------------------------------------------------------------------------------- */
  203. int
  204. main (void)
  205. {
  206. int number_failed;
  207. Suite *s = suite_create (TEST_SUITE_NAME);
  208. TCase *tc_core = tcase_create ("Core");
  209. SRunner *sr;
  210. tcase_add_checked_fixture (tc_core, setup, teardown);
  211. /* Add new tests here: *************** */
  212. mctest_add_parameterized_test (tc_core, test_path_equal, test_path_equal_ds);
  213. mctest_add_parameterized_test (tc_core, test_path_equal_len, test_path_equal_len_ds);
  214. /* *********************************** */
  215. suite_add_tcase (s, tc_core);
  216. sr = srunner_create (s);
  217. srunner_set_log (sr, "path_cmp.log");
  218. srunner_run_all (sr, CK_ENV);
  219. number_failed = srunner_ntests_failed (sr);
  220. srunner_free (sr);
  221. return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
  222. }
  223. /* --------------------------------------------------------------------------------------------- */