path_cmp.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /* lib/vfs - vfs_path_t compare functions
  2. Copyright (C) 2011 Free Software Foundation, Inc.
  3. Written by:
  4. Slava Zanko <slavazanko@gmail.com>, 2011
  5. This program is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Library General Public License
  7. as published by the Free Software Foundation; either version 2 of
  8. the License, or (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU Library General Public License for more details.
  13. You should have received a copy of the GNU Library General Public
  14. License along with this program; if not, write to the Free Software
  15. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  16. */
  17. #define TEST_SUITE_NAME "/lib/vfs"
  18. #include <check.h>
  19. #include "lib/global.c"
  20. #ifndef HAVE_CHARSET
  21. #define HAVE_CHARSET 1
  22. #endif
  23. #include "lib/charsets.h"
  24. #include "lib/strutil.h"
  25. #include "lib/vfs/xdirentry.h"
  26. #include "lib/vfs/path.h"
  27. #include "src/vfs/local/local.c"
  28. static void
  29. setup (void)
  30. {
  31. str_init_strings (NULL);
  32. vfs_init ();
  33. init_localfs ();
  34. vfs_setup_work_dir ();
  35. mc_global.sysconfig_dir = (char *) TEST_SHARE_DIR;
  36. load_codepages_list ();
  37. }
  38. static void
  39. teardown (void)
  40. {
  41. free_codepages_list ();
  42. vfs_shut ();
  43. str_uninit_strings ();
  44. }
  45. /* --------------------------------------------------------------------------------------------- */
  46. #define path_cmp_one_check(input1, input2, etalon) {\
  47. vpath1 = vfs_path_from_str (input1);\
  48. vpath2 = vfs_path_from_str (input2);\
  49. result = vfs_path_cmp (vpath1, vpath2);\
  50. vfs_path_free (vpath1); \
  51. vfs_path_free (vpath2); \
  52. fail_unless ( result == etalon, "\ninput1: %s\ninput2: %s\nexpected: %d\nactual: %d\n",\
  53. input1, input2, etalon, result); \
  54. }
  55. START_TEST (test_path_compare)
  56. {
  57. vfs_path_t *vpath1, *vpath2;
  58. int result;
  59. path_cmp_one_check ("/тестовый/путь", "/тестовый/путь", 0);
  60. path_cmp_one_check ("/#enc:KOI8-R/тестовый/путь", "/тестовый/путь", -174);
  61. path_cmp_one_check ("/тестовый/путь", "/#enc:KOI8-R/тестовый/путь", 174);
  62. path_cmp_one_check (NULL, "/тестовый/путь", -1);
  63. path_cmp_one_check ("/тестовый/путь", NULL, -1);
  64. path_cmp_one_check (NULL, NULL, -1);
  65. }
  66. END_TEST
  67. /* --------------------------------------------------------------------------------------------- */
  68. #undef path_cmp_one_check
  69. #define path_cmp_one_check(input1, input2, len, etalon) {\
  70. vpath1 = vfs_path_from_str (input1);\
  71. vpath2 = vfs_path_from_str (input2);\
  72. result = vfs_path_ncmp (vpath1, vpath2, len);\
  73. vfs_path_free (vpath1); \
  74. vfs_path_free (vpath2); \
  75. fail_unless ( result == etalon, "\ninput1: %s\ninput2: %s\nexpected: %d\nactual: %d\n",\
  76. input1, input2, etalon, result); \
  77. }
  78. START_TEST (test_path_compare_len)
  79. {
  80. vfs_path_t *vpath1, *vpath2;
  81. int result;
  82. path_cmp_one_check ("/тестовый/путь", "/тестовый/путь", 10, 0);
  83. path_cmp_one_check ("/тест/овый/путь", "/тестовый/путь", 10, -161);
  84. path_cmp_one_check ("/тестовый/путь", "/тест/овый/путь", 10, 161);
  85. path_cmp_one_check ("/тест/овый/путь", "/тестовый/путь", 9, 0);
  86. path_cmp_one_check (NULL, "/тестовый/путь", 0, -1);
  87. path_cmp_one_check ("/тестовый/путь", NULL, 0, -1);
  88. path_cmp_one_check (NULL, NULL, 0, -1);
  89. }
  90. END_TEST
  91. /* --------------------------------------------------------------------------------------------- */
  92. int
  93. main (void)
  94. {
  95. int number_failed;
  96. Suite *s = suite_create (TEST_SUITE_NAME);
  97. TCase *tc_core = tcase_create ("Core");
  98. SRunner *sr;
  99. tcase_add_checked_fixture (tc_core, setup, teardown);
  100. /* Add new tests here: *************** */
  101. tcase_add_test (tc_core, test_path_compare);
  102. tcase_add_test (tc_core, test_path_compare_len);
  103. /* *********************************** */
  104. suite_add_tcase (s, tc_core);
  105. sr = srunner_create (s);
  106. srunner_set_log (sr, "path_cmp.log");
  107. srunner_run_all (sr, CK_NORMAL);
  108. number_failed = srunner_ntests_failed (sr);
  109. srunner_free (sr);
  110. return (number_failed == 0) ? 0 : 1;
  111. }
  112. /* --------------------------------------------------------------------------------------------- */