Ticket #3694: fix hex pattern parsing.
[midnight-commander.git] / tests / lib / search / regex_replace_esc_seq.c
blob90e0e011e3e14f5afeebec7c876e8eba8ecc140e
1 /*
2 libmc - checks for processing esc sequences in replace string
4 Copyright (C) 2011-2016
5 Free Software Foundation, Inc.
7 Written by:
8 Slava Zanko <slavazanko@gmail.com>, 2011, 2013
10 This file is part of the Midnight Commander.
12 The Midnight Commander is free software: you can redistribute it
13 and/or modify it under the terms of the GNU General Public License as
14 published by the Free Software Foundation, either version 3 of the License,
15 or (at your option) any later version.
17 The Midnight Commander is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
22 You should have received a copy of the GNU General Public License
23 along with this program. If not, see <http://www.gnu.org/licenses/>.
26 #define TEST_SUITE_NAME "lib/search/regex"
28 #include "tests/mctest.h"
30 #include "regex.c" /* for testing static functions */
32 /* --------------------------------------------------------------------------------------------- */
33 #define test_helper_check_valid_data( a, b, c, d, e, f ) \
34 { \
35 fail_unless( a == b, "ret_value != %s", (b) ? "TRUE": "FALSE" ); \
36 fail_unless( c == d, "skip_len(%d) != %d", c, d ); \
37 if (f!=0) fail_unless( e == f, "ret(%d) != %d", e, f ); \
40 #define test_helper_handle_esc_seq( pos, r, skip, flag ) \
41 { \
42 skip_len = 0;\
43 test_helper_check_valid_data(\
44 mc_search_regex__replace_handle_esc_seq( replace_str, pos, &skip_len, &ret ), r,\
45 skip_len, skip,\
46 ret, flag\
47 ); \
50 /* --------------------------------------------------------------------------------------------- */
52 /* @DataSource("test_regex_replace_esc_seq_prepare_ds") */
53 /* *INDENT-OFF* */
54 static const struct test_regex_replace_esc_seq_prepare_ds
56 const char *input_string;
57 const size_t input_pos;
59 const gboolean expected_result;
60 const gsize expected_skipped_len;
61 const int expected_flags;
62 } test_regex_replace_esc_seq_prepare_ds[] =
64 { /* 0. \\{123} */
65 "bla-bla\\{123}bla-bla\\xabc234 bla-bla\\x{456abcd}bla-bla\\xtre\\n\\t\\v\\b\\r\\f\\a",
67 FALSE,
69 REPLACE_PREPARE_T_ESCAPE_SEQ
71 { /* 1. \\xab */
72 "bla-bla\\{123}bla-bla\\xabc234 bla-bla\\x{456abcd}bla-bla\\xtre\\n\\t\\v\\b\\r\\f\\a",
73 20,
74 FALSE,
76 REPLACE_PREPARE_T_ESCAPE_SEQ
78 { /* 2. \\x{456abcd} */
79 "bla-bla\\{123}bla-bla\\xabc234 bla-bla\\x{456abcd}bla-bla\\xtre\\n\\t\\v\\b\\r\\f\\a",
80 36,
81 FALSE,
82 11,
83 REPLACE_PREPARE_T_ESCAPE_SEQ
85 { /* 3. \\xtre */
86 "bla-bla\\{123}bla-bla\\xabc234 bla-bla\\x{456abcd}bla-bla\\xtre\\n\\t\\v\\b\\r\\f\\a",
87 54,
88 FALSE,
90 REPLACE_PREPARE_T_NOTHING_SPECIAL
92 { /* 4. \\n */
93 "bla-bla\\{123}bla-bla\\xabc234 bla-bla\\x{456abcd}bla-bla\\xtre\\n\\t\\v\\b\\r\\f\\a",
94 59,
95 FALSE,
97 REPLACE_PREPARE_T_ESCAPE_SEQ
99 { /* 5. \\t */
100 "bla-bla\\{123}bla-bla\\xabc234 bla-bla\\x{456abcd}bla-bla\\xtre\\n\\t\\v\\b\\r\\f\\a",
102 FALSE,
104 REPLACE_PREPARE_T_ESCAPE_SEQ
106 { /* 6. \\v */
107 "bla-bla\\{123}bla-bla\\xabc234 bla-bla\\x{456abcd}bla-bla\\xtre\\n\\t\\v\\b\\r\\f\\a",
109 FALSE,
111 REPLACE_PREPARE_T_ESCAPE_SEQ
113 { /* 7. \\b */
114 "bla-bla\\{123}bla-bla\\xabc234 bla-bla\\x{456abcd}bla-bla\\xtre\\n\\t\\v\\b\\r\\f\\a",
116 FALSE,
118 REPLACE_PREPARE_T_ESCAPE_SEQ
120 { /* 8. \\r */
121 "bla-bla\\{123}bla-bla\\xabc234 bla-bla\\x{456abcd}bla-bla\\xtre\\n\\t\\v\\b\\r\\f\\a",
123 FALSE,
125 REPLACE_PREPARE_T_ESCAPE_SEQ
127 { /* 9. \\f */
128 "bla-bla\\{123}bla-bla\\xabc234 bla-bla\\x{456abcd}bla-bla\\xtre\\n\\t\\v\\b\\r\\f\\a",
130 FALSE,
132 REPLACE_PREPARE_T_ESCAPE_SEQ
134 { /* 10. \\a */
135 "bla-bla\\{123}bla-bla\\xabc234 bla-bla\\x{456abcd}bla-bla\\xtre\\n\\t\\v\\b\\r\\f\\a",
137 FALSE,
139 REPLACE_PREPARE_T_ESCAPE_SEQ
141 { /* 11. \\{123 */
142 "\\{123 \\x{qwerty} \\12} \\x{456a-bcd}bla-bla\\satre",
144 TRUE,
146 REPLACE_PREPARE_T_NOTHING_SPECIAL
148 { /* 12. \\x{qwerty} */
149 "\\{123 \\x{qwerty} \\12} \\x{456a-bcd}bla-bla\\satre",
151 TRUE,
153 REPLACE_PREPARE_T_NOTHING_SPECIAL
155 { /* 13. \\12} */
156 "\\{123 \\x{qwerty} \\12} \\x{456a-bcd}bla-bla\\satre",
158 TRUE,
162 { /* 14. \\x{456a-bcd} */
163 "\\{123 \\x{qwerty} \\12} \\x{456a-bcd}bla-bla\\satre",
165 TRUE,
167 REPLACE_PREPARE_T_NOTHING_SPECIAL
169 { /* 15. \\satre */
170 "\\{123 \\x{qwerty} \\12} \\x{456a-bcd}bla-bla\\satre",
172 TRUE,
177 /* *INDENT-ON* */
179 /* @Test(dataSource = "test_regex_replace_esc_seq_prepare_ds") */
180 /* *INDENT-OFF* */
181 START_PARAMETRIZED_TEST (test_regex_replace_esc_seq_prepare, test_regex_replace_esc_seq_prepare_ds)
182 /* *INDENT-ON* */
184 /* given */
185 GString *replace_str;
186 gsize actual_skipped_len = 0;
187 int actual_flags = 0;
188 gboolean actual_result;
190 replace_str = g_string_new (data->input_string);
192 /* when */
193 actual_result =
194 mc_search_regex__replace_handle_esc_seq (replace_str, data->input_pos, &actual_skipped_len,
195 &actual_flags);
197 /* then */
198 mctest_assert_int_eq (actual_result, data->expected_result);
199 mctest_assert_int_eq (actual_skipped_len, data->expected_skipped_len);
200 mctest_assert_int_eq (actual_flags, data->expected_flags);
202 g_string_free (replace_str, TRUE);
204 /* *INDENT-OFF* */
205 END_PARAMETRIZED_TEST
206 /* *INDENT-ON* */
208 /* --------------------------------------------------------------------------------------------- */
211 main (void)
213 int number_failed;
215 Suite *s = suite_create (TEST_SUITE_NAME);
216 TCase *tc_core = tcase_create ("Core");
217 SRunner *sr;
219 /* Add new tests here: *************** */
220 mctest_add_parameterized_test (tc_core, test_regex_replace_esc_seq_prepare,
221 test_regex_replace_esc_seq_prepare_ds);
222 /* *********************************** */
224 suite_add_tcase (s, tc_core);
225 sr = srunner_create (s);
226 srunner_set_log (sr, "regex_replace_esc_seq.log");
227 srunner_run_all (sr, CK_ENV);
228 number_failed = srunner_ntests_failed (sr);
229 srunner_free (sr);
230 return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
233 /* --------------------------------------------------------------------------------------------- */