root/tests/lib/search/hex_translate_to_regex.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 hex pattern parsing
   3 
   4    Copyright (C) 2017-2024
   5    Free Software Foundation, Inc.
   6 
   7    This file is part of the Midnight Commander.
   8 
   9    The Midnight Commander is free software: you can redistribute it
  10    and/or modify it under the terms of the GNU General Public License as
  11    published by the Free Software Foundation, either version 3 of the License,
  12    or (at your option) any later version.
  13 
  14    The Midnight Commander is distributed in the hope that it will be useful,
  15    but WITHOUT ANY WARRANTY; without even the implied warranty of
  16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17    GNU General Public License for more details.
  18 
  19    You should have received a copy of the GNU General Public License
  20    along with this program.  If not, see <http://www.gnu.org/licenses/>.
  21  */
  22 
  23 #define TEST_SUITE_NAME "lib/search/hex"
  24 
  25 #include "tests/mctest.h"
  26 
  27 #include "hex.c"                /* for testing static functions */
  28 
  29 /* --------------------------------------------------------------------------------------------- */
  30 
  31 /* @DataSource("test_hex_translate_to_regex_ds") */
  32 /* *INDENT-OFF* */
  33 static const struct test_hex_translate_to_regex_ds
  34 {
  35     const char *input_value;
  36     const char *expected_result;
  37     mc_search_hex_parse_error_t expected_error;
  38 } test_hex_translate_to_regex_ds[] =
  39 {
  40     {
  41         /* Simplest case */
  42         "12 34",
  43         "\\x12\\x34",
  44         MC_SEARCH_HEX_E_OK
  45     },
  46     {
  47         /* Prefixes (0x, 0X) */
  48         "0x12 0X34",
  49         "\\x12\\x34",
  50         MC_SEARCH_HEX_E_OK
  51     },
  52     {
  53         /* Prefix "0" doesn't signify octal! Numbers are always interpreted in hex. */
  54         "012",
  55         "\\x12",
  56         MC_SEARCH_HEX_E_OK
  57     },
  58     {
  59         /* Extra whitespace */
  60         "  12  34  ",
  61         "\\x12\\x34",
  62         MC_SEARCH_HEX_E_OK
  63     },
  64     {
  65         /* Min/max values */
  66         "0 ff",
  67         "\\x00\\xFF",
  68         MC_SEARCH_HEX_E_OK
  69     },
  70     {
  71         /* Error: Number out of range */
  72         "100",
  73         NULL,
  74         MC_SEARCH_HEX_E_NUM_OUT_OF_RANGE
  75     },
  76     {
  77         /* Error: Number out of range (negative) */
  78         "-1",
  79         NULL,
  80         MC_SEARCH_HEX_E_NUM_OUT_OF_RANGE
  81     },
  82     {
  83         /* Error: Invalid characters */
  84         "1 z 2",
  85         NULL,
  86         MC_SEARCH_HEX_E_INVALID_CHARACTER
  87     },
  88     /*
  89      * Quotes.
  90      */
  91     {
  92         " \"abc\" ",
  93         "abc",
  94         MC_SEARCH_HEX_E_OK
  95     },
  96     {
  97         /* Preserve upper/lower case */
  98         "\"aBc\"",
  99         "aBc",
 100         MC_SEARCH_HEX_E_OK
 101     },
 102     {
 103         " 12\"abc\"34 ",
 104         "\\x12abc\\x34",
 105         MC_SEARCH_HEX_E_OK
 106     },
 107     {
 108         "\"a\"\"b\"",
 109         "ab",
 110         MC_SEARCH_HEX_E_OK
 111     },
 112     /* Empty quotes */
 113     {
 114         "\"\"",
 115         "",
 116         MC_SEARCH_HEX_E_OK
 117     },
 118     {
 119         "12 \"\"",
 120         "\\x12",
 121         MC_SEARCH_HEX_E_OK
 122     },
 123     /* Error: Unmatched quotes */
 124     {
 125         "\"a",
 126         NULL,
 127         MC_SEARCH_HEX_E_UNMATCHED_QUOTES
 128     },
 129     {
 130         "\"",
 131         NULL,
 132         MC_SEARCH_HEX_E_UNMATCHED_QUOTES
 133     },
 134     /* Escaped quotes */
 135     {
 136         "\"a\\\"b\"",
 137         "a\"b",
 138         MC_SEARCH_HEX_E_OK
 139     },
 140     {
 141         "\"a\\\\b\"",
 142         "a\\b",
 143         MC_SEARCH_HEX_E_OK
 144     },
 145 };
 146 /* *INDENT-ON* */
 147 
 148 /* @Test(dataSource = "test_hex_translate_to_regex_ds") */
 149 /* *INDENT-OFF* */
 150 START_PARAMETRIZED_TEST (test_hex_translate_to_regex, test_hex_translate_to_regex_ds)
     /* [previous][next][first][last][top][bottom][index][help]  */
 151 /* *INDENT-ON* */
 152 {
 153     GString *tmp, *dest_str;
 154     mc_search_hex_parse_error_t error = MC_SEARCH_HEX_E_OK;
 155 
 156     /* given */
 157     tmp = g_string_new (data->input_value);
 158 
 159     /* when */
 160     dest_str = mc_search__hex_translate_to_regex (tmp, &error, NULL);
 161 
 162     g_string_free (tmp, TRUE);
 163 
 164     /* then */
 165     if (dest_str != NULL)
 166     {
 167         mctest_assert_str_eq (dest_str->str, data->expected_result);
 168         g_string_free (dest_str, TRUE);
 169     }
 170     else
 171         ck_assert_int_eq (error, data->expected_error);
 172 }
 173 /* *INDENT-OFF* */
 174 END_PARAMETRIZED_TEST
 175 /* *INDENT-ON* */
 176 
 177 /* --------------------------------------------------------------------------------------------- */
 178 
 179 int
 180 main (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 181 {
 182     TCase *tc_core;
 183 
 184     tc_core = tcase_create ("Core");
 185 
 186     /* Add new tests here: *************** */
 187     mctest_add_parameterized_test (tc_core, test_hex_translate_to_regex,
 188                                    test_hex_translate_to_regex_ds);
 189     /* *********************************** */
 190 
 191     return mctest_run_all (tc_core);
 192 }
 193 
 194 /* --------------------------------------------------------------------------------------------- */

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