path_len.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /* lib/vfs - tests for vfspath_len() function.
  2. Copyright (C) 2011-2024
  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 ("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_length_ds") */
  54. /* *INDENT-OFF* */
  55. static const struct test_path_length_ds
  56. {
  57. const char *input_path;
  58. const size_t expected_length_element_encoding;
  59. const size_t expected_length_terminal_encoding;
  60. } test_path_length_ds[] =
  61. {
  62. { /* 0. */
  63. NULL,
  64. 0,
  65. 0
  66. },
  67. { /* 1. */
  68. "/",
  69. 1,
  70. 1
  71. },
  72. { /* 2. */
  73. "/тестовый/путь",
  74. 26,
  75. 26
  76. },
  77. #ifdef HAVE_CHARSET
  78. { /* 3. */
  79. "/#enc:KOI8-R/тестовый/путь",
  80. 14,
  81. 38,
  82. },
  83. #endif /* HAVE_CHARSET */
  84. };
  85. /* *INDENT-ON* */
  86. /* @Test(dataSource = "test_path_length_ds") */
  87. /* *INDENT-OFF* */
  88. START_PARAMETRIZED_TEST (test_path_length, test_path_length_ds)
  89. /* *INDENT-ON* */
  90. {
  91. /* given */
  92. vfs_path_t *vpath;
  93. char *path;
  94. size_t actual_length_terminal_encoding, actual_length_element_encoding;
  95. vpath = vfs_path_from_str (data->input_path);
  96. path = vpath != NULL ? vfs_path_get_by_index (vpath, 0)->path : NULL;
  97. /* when */
  98. actual_length_terminal_encoding = vfs_path_len (vpath);
  99. actual_length_element_encoding = path != NULL ? strlen (path) : 0;
  100. /* then */
  101. ck_assert_int_eq (actual_length_terminal_encoding, data->expected_length_terminal_encoding);
  102. ck_assert_int_eq (actual_length_element_encoding, data->expected_length_element_encoding);
  103. vfs_path_free (vpath, TRUE);
  104. }
  105. /* *INDENT-OFF* */
  106. END_PARAMETRIZED_TEST
  107. /* *INDENT-ON* */
  108. /* --------------------------------------------------------------------------------------------- */
  109. int
  110. main (void)
  111. {
  112. TCase *tc_core;
  113. tc_core = tcase_create ("Core");
  114. tcase_add_checked_fixture (tc_core, setup, teardown);
  115. /* Add new tests here: *************** */
  116. mctest_add_parameterized_test (tc_core, test_path_length, test_path_length_ds);
  117. /* *********************************** */
  118. return mctest_run_all (tc_core);
  119. }
  120. /* --------------------------------------------------------------------------------------------- */