root/tests/lib/search/regex_replace_esc_seq.c

/* [previous][next][first][last][top][bottom][index][help]  */

DEFINITIONS

This source file includes following definitions.
  1. START_PARAMETRIZED_TEST
  2. main

   1 /*
   2    libmc - checks for processing esc sequences in replace string
   3 
   4    Copyright (C) 2011-2019
   5    Free Software Foundation, Inc.
   6 
   7    Written by:
   8    Slava Zanko <slavazanko@gmail.com>, 2011, 2013
   9 
  10    This file is part of the Midnight Commander.
  11 
  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.
  16 
  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.
  21 
  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/>.
  24  */
  25 
  26 #define TEST_SUITE_NAME "lib/search/regex"
  27 
  28 #include "tests/mctest.h"
  29 
  30 #include "regex.c"              /* for testing static functions */
  31 
  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 ); \
  38 }
  39 
  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     ); \
  48 }
  49 
  50 /* --------------------------------------------------------------------------------------------- */
  51 
  52 /* @DataSource("test_regex_replace_esc_seq_prepare_ds") */
  53 /* *INDENT-OFF* */
  54 static const struct test_regex_replace_esc_seq_prepare_ds
  55 {
  56     const char *input_string;
  57     const size_t input_pos;
  58 
  59     const gboolean expected_result;
  60     const gsize expected_skipped_len;
  61     const int expected_flags;
  62 } test_regex_replace_esc_seq_prepare_ds[] =
  63 {
  64     { /* 0. \\{123} */
  65         "bla-bla\\{123}bla-bla\\xabc234 bla-bla\\x{456abcd}bla-bla\\xtre\\n\\t\\v\\b\\r\\f\\a",
  66         7,
  67         FALSE,
  68         6,
  69         REPLACE_PREPARE_T_ESCAPE_SEQ
  70     },
  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,
  75         4,
  76         REPLACE_PREPARE_T_ESCAPE_SEQ
  77     },
  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
  84     },
  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,
  89         2,
  90         REPLACE_PREPARE_T_NOTHING_SPECIAL
  91     },
  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,
  96         2,
  97         REPLACE_PREPARE_T_ESCAPE_SEQ
  98     },
  99     { /* 5. \\t */
 100         "bla-bla\\{123}bla-bla\\xabc234 bla-bla\\x{456abcd}bla-bla\\xtre\\n\\t\\v\\b\\r\\f\\a",
 101         61,
 102         FALSE,
 103         2,
 104         REPLACE_PREPARE_T_ESCAPE_SEQ
 105     },
 106     { /* 6. \\v */
 107         "bla-bla\\{123}bla-bla\\xabc234 bla-bla\\x{456abcd}bla-bla\\xtre\\n\\t\\v\\b\\r\\f\\a",
 108         63,
 109         FALSE,
 110         2,
 111         REPLACE_PREPARE_T_ESCAPE_SEQ
 112     },
 113     { /* 7. \\b */
 114         "bla-bla\\{123}bla-bla\\xabc234 bla-bla\\x{456abcd}bla-bla\\xtre\\n\\t\\v\\b\\r\\f\\a",
 115         65,
 116         FALSE,
 117         2,
 118         REPLACE_PREPARE_T_ESCAPE_SEQ
 119     },
 120     { /* 8. \\r */
 121         "bla-bla\\{123}bla-bla\\xabc234 bla-bla\\x{456abcd}bla-bla\\xtre\\n\\t\\v\\b\\r\\f\\a",
 122         67,
 123         FALSE,
 124         2,
 125         REPLACE_PREPARE_T_ESCAPE_SEQ
 126     },
 127     { /* 9. \\f */
 128         "bla-bla\\{123}bla-bla\\xabc234 bla-bla\\x{456abcd}bla-bla\\xtre\\n\\t\\v\\b\\r\\f\\a",
 129         69,
 130         FALSE,
 131         2,
 132         REPLACE_PREPARE_T_ESCAPE_SEQ
 133     },
 134     {  /* 10. \\a */
 135         "bla-bla\\{123}bla-bla\\xabc234 bla-bla\\x{456abcd}bla-bla\\xtre\\n\\t\\v\\b\\r\\f\\a",
 136         71,
 137         FALSE,
 138         2,
 139         REPLACE_PREPARE_T_ESCAPE_SEQ
 140     },
 141     { /* 11. \\{123 */
 142         "\\{123 \\x{qwerty} \\12} \\x{456a-bcd}bla-bla\\satre",
 143         0,
 144         TRUE,
 145         5,
 146         REPLACE_PREPARE_T_NOTHING_SPECIAL
 147     },
 148     { /* 12. \\x{qwerty} */
 149         "\\{123 \\x{qwerty} \\12} \\x{456a-bcd}bla-bla\\satre",
 150         6,
 151         TRUE,
 152         3,
 153         REPLACE_PREPARE_T_NOTHING_SPECIAL
 154     },
 155     { /* 13. \\12} */
 156         "\\{123 \\x{qwerty} \\12} \\x{456a-bcd}bla-bla\\satre",
 157         17,
 158         TRUE,
 159         0,
 160         0
 161     },
 162     { /* 14. \\x{456a-bcd} */
 163         "\\{123 \\x{qwerty} \\12} \\x{456a-bcd}bla-bla\\satre",
 164         22,
 165         TRUE,
 166         7,
 167         REPLACE_PREPARE_T_NOTHING_SPECIAL
 168     },
 169     { /* 15. \\satre */
 170         "\\{123 \\x{qwerty} \\12} \\x{456a-bcd}bla-bla\\satre",
 171         41,
 172         TRUE,
 173         0,
 174         0
 175     },
 176 };
 177 /* *INDENT-ON* */
 178 
 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)
     /* [previous][next][first][last][top][bottom][index][help]  */
 182 /* *INDENT-ON* */
 183 {
 184     /* given */
 185     GString *replace_str;
 186     gsize actual_skipped_len = 0;
 187     int actual_flags = 0;
 188     gboolean actual_result;
 189 
 190     replace_str = g_string_new (data->input_string);
 191 
 192     /* when */
 193     actual_result =
 194         mc_search_regex__replace_handle_esc_seq (replace_str, data->input_pos, &actual_skipped_len,
 195                                                  &actual_flags);
 196 
 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);
 201 
 202     g_string_free (replace_str, TRUE);
 203 }
 204 /* *INDENT-OFF* */
 205 END_PARAMETRIZED_TEST
 206 /* *INDENT-ON* */
 207 
 208 /* --------------------------------------------------------------------------------------------- */
 209 
 210 int
 211 main (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 212 {
 213     int number_failed;
 214 
 215     Suite *s = suite_create (TEST_SUITE_NAME);
 216     TCase *tc_core = tcase_create ("Core");
 217     SRunner *sr;
 218 
 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     /* *********************************** */
 223 
 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;
 231 }
 232 
 233 /* --------------------------------------------------------------------------------------------- */

/* [previous][next][first][last][top][bottom][index][help]  */