path_cmp.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. /* lib/vfs - vfs_path_t compare functions
  2. Copyright (C) 2011-2025
  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 <https://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 ("UTF-8");
  33. vfs_init ();
  34. vfs_init_localfs ();
  35. vfs_setup_work_dir ();
  36. #ifdef HAVE_CHARSET
  37. mc_global.sysconfig_dir = (char *) TEST_SHARE_DIR;
  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. static const struct test_path_equal_ds
  55. {
  56. const char *input_path1;
  57. const char *input_path2;
  58. const gboolean expected_result;
  59. } test_path_equal_ds[] = {
  60. {
  61. // 0.
  62. NULL,
  63. NULL,
  64. FALSE,
  65. },
  66. {
  67. // 1.
  68. NULL,
  69. "/test/path",
  70. FALSE,
  71. },
  72. {
  73. // 2.
  74. "/test/path",
  75. NULL,
  76. FALSE,
  77. },
  78. {
  79. // 3.
  80. "/test/path",
  81. "/test/path",
  82. TRUE,
  83. },
  84. #ifdef HAVE_CHARSET
  85. {
  86. // 4.
  87. "/#enc:KOI8-R/тестовый/путь",
  88. "/тестовый/путь",
  89. FALSE,
  90. },
  91. {
  92. // 5.
  93. "/тестовый/путь",
  94. "/#enc:KOI8-R/тестовый/путь",
  95. FALSE,
  96. },
  97. {
  98. // 6.
  99. "/#enc:KOI8-R/тестовый/путь",
  100. "/#enc:KOI8-R/тестовый/путь",
  101. TRUE,
  102. },
  103. #endif
  104. };
  105. /* @Test(dataSource = "test_path_equal_ds") */
  106. START_PARAMETRIZED_TEST (test_path_equal, test_path_equal_ds)
  107. {
  108. // given
  109. vfs_path_t *vpath1, *vpath2;
  110. gboolean actual_result;
  111. vpath1 = vfs_path_from_str (data->input_path1);
  112. vpath2 = vfs_path_from_str (data->input_path2);
  113. // when
  114. actual_result = vfs_path_equal (vpath1, vpath2);
  115. // then
  116. ck_assert_int_eq (actual_result, data->expected_result);
  117. vfs_path_free (vpath1, TRUE);
  118. vfs_path_free (vpath2, TRUE);
  119. }
  120. END_PARAMETRIZED_TEST
  121. /* --------------------------------------------------------------------------------------------- */
  122. /* @DataSource("test_path_equal_len_ds") */
  123. static const struct test_path_equal_len_ds
  124. {
  125. const char *input_path1;
  126. const char *input_path2;
  127. const size_t input_length;
  128. const gboolean expected_result;
  129. } test_path_equal_len_ds[] = {
  130. {
  131. // 0.
  132. NULL,
  133. NULL,
  134. 0,
  135. FALSE,
  136. },
  137. {
  138. // 1.
  139. NULL,
  140. NULL,
  141. 100,
  142. FALSE,
  143. },
  144. {
  145. // 2.
  146. NULL,
  147. "/тестовый/путь",
  148. 10,
  149. FALSE,
  150. },
  151. {
  152. // 3.
  153. "/тестовый/путь",
  154. NULL,
  155. 10,
  156. FALSE,
  157. },
  158. {
  159. // 4.
  160. "/тестовый/путь",
  161. "/тестовый/путь",
  162. 10,
  163. TRUE,
  164. },
  165. {
  166. // 5.
  167. "/тест/овый/путь",
  168. "/тестовый/путь",
  169. 8,
  170. TRUE,
  171. },
  172. {
  173. // 6.
  174. "/тест/овый/путь",
  175. "/тестовый/путь",
  176. 10,
  177. FALSE,
  178. },
  179. {
  180. // 7.
  181. "/тестовый/путь",
  182. "/тест/овый/путь",
  183. 10,
  184. FALSE,
  185. },
  186. };
  187. /* @Test(dataSource = "test_path_equal_len_ds") */
  188. START_PARAMETRIZED_TEST (test_path_equal_len, test_path_equal_len_ds)
  189. {
  190. // given
  191. vfs_path_t *vpath1, *vpath2;
  192. gboolean actual_result;
  193. vpath1 = vfs_path_from_str (data->input_path1);
  194. vpath2 = vfs_path_from_str (data->input_path2);
  195. // when
  196. actual_result = vfs_path_equal_len (vpath1, vpath2, data->input_length);
  197. // then
  198. ck_assert_int_eq (actual_result, data->expected_result);
  199. vfs_path_free (vpath1, TRUE);
  200. vfs_path_free (vpath2, TRUE);
  201. }
  202. END_PARAMETRIZED_TEST
  203. /* --------------------------------------------------------------------------------------------- */
  204. int
  205. main (void)
  206. {
  207. TCase *tc_core;
  208. tc_core = tcase_create ("Core");
  209. tcase_add_checked_fixture (tc_core, setup, teardown);
  210. // Add new tests here: ***************
  211. mctest_add_parameterized_test (tc_core, test_path_equal, test_path_equal_ds);
  212. mctest_add_parameterized_test (tc_core, test_path_equal_len, test_path_equal_len_ds);
  213. // ***********************************
  214. return mctest_run_all (tc_core);
  215. }
  216. /* --------------------------------------------------------------------------------------------- */