regex_replace_esc_seq.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*
  2. libmc - checks for processing esc sequences in replace string
  3. Copyright (C) 2011
  4. The Free Software Foundation, Inc.
  5. Written by:
  6. Slava Zanko <slavazanko@gmail.com>, 2011
  7. This file is part of the Midnight Commander.
  8. The Midnight Commander is free software: you can redistribute it
  9. and/or modify it under the terms of the GNU General Public License as
  10. published by the Free Software Foundation, either version 3 of the License,
  11. or (at your option) any later version.
  12. The Midnight Commander is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. GNU General Public License for more details.
  16. You should have received a copy of the GNU General Public License
  17. along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #define TEST_SUITE_NAME "lib/search/regex"
  20. #include <config.h>
  21. #include <check.h>
  22. #include "regex.c" /* for testing static functions*/
  23. /* --------------------------------------------------------------------------------------------- */
  24. #define test_helper_check_valid_data( a, b, c, d, e, f ) \
  25. { \
  26. fail_unless( a == b, "ret_value != %s", (b) ? "TRUE": "FALSE" ); \
  27. fail_unless( c == d, "skip_len(%d) != %d", c, d ); \
  28. if (f!=0) fail_unless( e == f, "ret(%d) != %d", e, f ); \
  29. }
  30. #define test_helper_handle_esc_seq( pos, r, skip, flag ) \
  31. { \
  32. skip_len = 0;\
  33. test_helper_check_valid_data(\
  34. mc_search_regex__replace_handle_esc_seq( replace_str, pos, &skip_len, &ret ), r,\
  35. skip_len, skip,\
  36. ret, flag\
  37. ); \
  38. }
  39. /* --------------------------------------------------------------------------------------------- */
  40. START_TEST (test_regex_replace_esc_seq_prepare_valid)
  41. {
  42. GString *replace_str;
  43. gsize skip_len;
  44. int ret;
  45. replace_str = g_string_new("bla-bla\\{123}bla-bla\\xabc234 bla-bla\\x{456abcd}bla-bla\\xtre\\n\\t\\v\\b\\r\\f\\a");
  46. test_helper_handle_esc_seq( 7, FALSE, 6, REPLACE_PREPARE_T_ESCAPE_SEQ ); /* \\{123} */
  47. test_helper_handle_esc_seq( 20, FALSE, 4, REPLACE_PREPARE_T_ESCAPE_SEQ ); /* \\xab */
  48. test_helper_handle_esc_seq( 36, FALSE, 11, REPLACE_PREPARE_T_ESCAPE_SEQ ); /* \\x{456abcd} */
  49. test_helper_handle_esc_seq( 54, FALSE, 2, REPLACE_PREPARE_T_NOTHING_SPECIAL ); /* \\xtre */
  50. test_helper_handle_esc_seq( 59, FALSE, 2, REPLACE_PREPARE_T_ESCAPE_SEQ ); /* \\n */
  51. test_helper_handle_esc_seq( 61, FALSE, 2, REPLACE_PREPARE_T_ESCAPE_SEQ ); /* \\t */
  52. test_helper_handle_esc_seq( 63, FALSE, 2, REPLACE_PREPARE_T_ESCAPE_SEQ ); /* \\v */
  53. test_helper_handle_esc_seq( 65, FALSE, 2, REPLACE_PREPARE_T_ESCAPE_SEQ ); /* \\b */
  54. test_helper_handle_esc_seq( 67, FALSE, 2, REPLACE_PREPARE_T_ESCAPE_SEQ ); /* \\r */
  55. test_helper_handle_esc_seq( 69, FALSE, 2, REPLACE_PREPARE_T_ESCAPE_SEQ ); /* \\f */
  56. test_helper_handle_esc_seq( 71, FALSE, 2, REPLACE_PREPARE_T_ESCAPE_SEQ ); /* \\a */
  57. g_string_free(replace_str, TRUE);
  58. }
  59. END_TEST
  60. /* --------------------------------------------------------------------------------------------- */
  61. START_TEST (test_regex_replace_esc_seq_prepare_invalid)
  62. {
  63. GString *replace_str;
  64. gsize skip_len;
  65. int ret;
  66. replace_str = g_string_new("\\{123 \\x{qwerty} \\12} \\x{456a-bcd}bla-bla\\satre");
  67. test_helper_handle_esc_seq( 0, TRUE, 5, REPLACE_PREPARE_T_NOTHING_SPECIAL ); /* \\{123 */
  68. test_helper_handle_esc_seq( 6, TRUE, 3, REPLACE_PREPARE_T_NOTHING_SPECIAL ); /* \\x{qwerty} */
  69. test_helper_handle_esc_seq( 17, TRUE, 0, REPLACE_PREPARE_T_NOTHING_SPECIAL ); /* \\12} */
  70. test_helper_handle_esc_seq( 22, TRUE, 7, REPLACE_PREPARE_T_NOTHING_SPECIAL ); /* \\x{456a-bcd} */
  71. test_helper_handle_esc_seq( 41, TRUE, 0, REPLACE_PREPARE_T_NOTHING_SPECIAL ); /* \\satre */
  72. g_string_free(replace_str, TRUE);
  73. }
  74. END_TEST
  75. /* --------------------------------------------------------------------------------------------- */
  76. int
  77. main (void)
  78. {
  79. int number_failed;
  80. Suite *s = suite_create (TEST_SUITE_NAME);
  81. TCase *tc_core = tcase_create ("Core");
  82. SRunner *sr;
  83. /* Add new tests here: *************** */
  84. tcase_add_test (tc_core, test_regex_replace_esc_seq_prepare_valid);
  85. tcase_add_test (tc_core, test_regex_replace_esc_seq_prepare_invalid);
  86. /* *********************************** */
  87. suite_add_tcase (s, tc_core);
  88. sr = srunner_create (s);
  89. srunner_run_all (sr, CK_NORMAL);
  90. number_failed = srunner_ntests_failed (sr);
  91. srunner_free (sr);
  92. return (number_failed == 0) ? 0 : 1;
  93. }
  94. /* --------------------------------------------------------------------------------------------- */