path_cmp.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. #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. static void
  28. setup (void)
  29. {
  30. str_init_strings (NULL);
  31. vfs_init ();
  32. init_localfs ();
  33. vfs_setup_work_dir ();
  34. mc_global.sysconfig_dir = (char *) TEST_SHARE_DIR;
  35. #ifdef HAVE_CHARSET
  36. load_codepages_list ();
  37. #endif
  38. }
  39. static void
  40. teardown (void)
  41. {
  42. #ifdef HAVE_CHARSET
  43. free_codepages_list ();
  44. #endif
  45. vfs_shut ();
  46. str_uninit_strings ();
  47. }
  48. /* --------------------------------------------------------------------------------------------- */
  49. #define path_cmp_one_check(input1, input2, etalon_condition) {\
  50. vpath1 = vfs_path_from_str (input1);\
  51. vpath2 = vfs_path_from_str (input2);\
  52. result = vfs_path_cmp (vpath1, vpath2);\
  53. vfs_path_free (vpath1); \
  54. vfs_path_free (vpath2); \
  55. fail_unless ( result etalon_condition, "\ninput1: %s\ninput2: %s\nexpected: %d\nactual: %d\n",\
  56. input1, input2, #etalon_condition, result); \
  57. }
  58. START_TEST (test_path_compare)
  59. {
  60. vfs_path_t *vpath1, *vpath2;
  61. int result;
  62. path_cmp_one_check ("/тестовый/путь", "/тестовый/путь", ==0);
  63. #ifdef HAVE_CHARSET
  64. path_cmp_one_check ("/#enc:KOI8-R/тестовый/путь", "/тестовый/путь", <0);
  65. path_cmp_one_check ("/тестовый/путь", "/#enc:KOI8-R/тестовый/путь", >0);
  66. #endif
  67. path_cmp_one_check (NULL, "/тестовый/путь", -1);
  68. path_cmp_one_check ("/тестовый/путь", NULL, -1);
  69. path_cmp_one_check (NULL, NULL, -1);
  70. }
  71. END_TEST
  72. /* --------------------------------------------------------------------------------------------- */
  73. #undef path_cmp_one_check
  74. #define path_cmp_one_check(input1, input2, len, etalon_condition) {\
  75. vpath1 = vfs_path_from_str (input1);\
  76. vpath2 = vfs_path_from_str (input2);\
  77. result = vfs_path_ncmp (vpath1, vpath2, len);\
  78. vfs_path_free (vpath1); \
  79. vfs_path_free (vpath2); \
  80. fail_unless ( result etalon_condition, "\ninput1: %s\ninput2: %s\nexpected: %d\nactual: %d\n",\
  81. input1, input2, #etalon_condition, result); \
  82. }
  83. START_TEST (test_path_compare_len)
  84. {
  85. vfs_path_t *vpath1, *vpath2;
  86. int result;
  87. path_cmp_one_check ("/тестовый/путь", "/тестовый/путь", 10, ==0);
  88. path_cmp_one_check ("/тест/овый/путь", "/тестовый/путь", 10, <0);
  89. path_cmp_one_check ("/тестовый/путь", "/тест/овый/путь", 10, >0);
  90. path_cmp_one_check ("/тест/овый/путь", "/тестовый/путь", 9, ==0);
  91. path_cmp_one_check (NULL, "/тестовый/путь", 0, <0);
  92. path_cmp_one_check ("/тестовый/путь", NULL, 0, <0);
  93. path_cmp_one_check (NULL, NULL, 0, <0);
  94. }
  95. END_TEST
  96. /* --------------------------------------------------------------------------------------------- */
  97. int
  98. main (void)
  99. {
  100. int number_failed;
  101. Suite *s = suite_create (TEST_SUITE_NAME);
  102. TCase *tc_core = tcase_create ("Core");
  103. SRunner *sr;
  104. tcase_add_checked_fixture (tc_core, setup, teardown);
  105. /* Add new tests here: *************** */
  106. tcase_add_test (tc_core, test_path_compare);
  107. tcase_add_test (tc_core, test_path_compare_len);
  108. /* *********************************** */
  109. suite_add_tcase (s, tc_core);
  110. sr = srunner_create (s);
  111. srunner_set_log (sr, "path_cmp.log");
  112. srunner_run_all (sr, CK_NORMAL);
  113. number_failed = srunner_ntests_failed (sr);
  114. srunner_free (sr);
  115. return (number_failed == 0) ? 0 : 1;
  116. }
  117. /* --------------------------------------------------------------------------------------------- */