Manual pages: mcmcdiffmceditmcview

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-2025
   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 <https://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 static const struct test_hex_translate_to_regex_ds
  33 {
  34     const char *input_value;
  35     const char *expected_result;
  36     mc_search_hex_parse_error_t expected_error;
  37 } test_hex_translate_to_regex_ds[] = {
  38     {
  39         // Simplest case
  40         "12 34",
  41         "\\x12\\x34",
  42         MC_SEARCH_HEX_E_OK,
  43     },
  44     {
  45         // Prefixes (0x, 0X)
  46         "0x12 0X34",
  47         "\\x12\\x34",
  48         MC_SEARCH_HEX_E_OK,
  49     },
  50     {
  51         // Prefix "0" doesn't signify octal! Numbers are always interpreted in hex.
  52         "012",
  53         "\\x12",
  54         MC_SEARCH_HEX_E_OK,
  55     },
  56     {
  57         // Extra whitespace
  58         "  12  34  ",
  59         "\\x12\\x34",
  60         MC_SEARCH_HEX_E_OK,
  61     },
  62     {
  63         // Min/max values
  64         "0 ff",
  65         "\\x00\\xFF",
  66         MC_SEARCH_HEX_E_OK,
  67     },
  68     {
  69         // Error: Number out of range
  70         "100",
  71         NULL,
  72         MC_SEARCH_HEX_E_NUM_OUT_OF_RANGE,
  73     },
  74     {
  75         // Error: Number out of range (negative)
  76         "-1",
  77         NULL,
  78         MC_SEARCH_HEX_E_NUM_OUT_OF_RANGE,
  79     },
  80     {
  81         // Error: Invalid characters
  82         "1 z 2",
  83         NULL,
  84         MC_SEARCH_HEX_E_INVALID_CHARACTER,
  85     },
  86     /*
  87      * Quotes.
  88      */
  89     {
  90         " \"abc\" ",
  91         "abc",
  92         MC_SEARCH_HEX_E_OK,
  93     },
  94     {
  95         // Preserve upper/lower case
  96         "\"aBc\"",
  97         "aBc",
  98         MC_SEARCH_HEX_E_OK,
  99     },
 100     {
 101         " 12\"abc\"34 ",
 102         "\\x12abc\\x34",
 103         MC_SEARCH_HEX_E_OK,
 104     },
 105     {
 106         "\"a\"\"b\"",
 107         "ab",
 108         MC_SEARCH_HEX_E_OK,
 109     },
 110     // Empty quotes
 111     {
 112         "\"\"",
 113         "",
 114         MC_SEARCH_HEX_E_OK,
 115     },
 116     {
 117         "12 \"\"",
 118         "\\x12",
 119         MC_SEARCH_HEX_E_OK,
 120     },
 121     // Error: Unmatched quotes
 122     {
 123         "\"a",
 124         NULL,
 125         MC_SEARCH_HEX_E_UNMATCHED_QUOTES,
 126     },
 127     {
 128         "\"",
 129         NULL,
 130         MC_SEARCH_HEX_E_UNMATCHED_QUOTES,
 131     },
 132     // Escaped quotes
 133     {
 134         "\"a\\\"b\"",
 135         "a\"b",
 136         MC_SEARCH_HEX_E_OK,
 137     },
 138     {
 139         "\"a\\\\b\"",
 140         "a\\b",
 141         MC_SEARCH_HEX_E_OK,
 142     },
 143 };
 144 
 145 /* @Test(dataSource = "test_hex_translate_to_regex_ds") */
 146 START_PARAMETRIZED_TEST (test_hex_translate_to_regex, test_hex_translate_to_regex_ds)
     /* [previous][next][first][last][top][bottom][index][help]  */
 147 {
 148     GString *tmp, *dest_str;
 149     mc_search_hex_parse_error_t error = MC_SEARCH_HEX_E_OK;
 150 
 151     // given
 152     tmp = g_string_new (data->input_value);
 153 
 154     // when
 155     dest_str = mc_search__hex_translate_to_regex (tmp, &error, NULL);
 156 
 157     g_string_free (tmp, TRUE);
 158 
 159     // then
 160     if (dest_str != NULL)
 161     {
 162         mctest_assert_str_eq (dest_str->str, data->expected_result);
 163         g_string_free (dest_str, TRUE);
 164     }
 165     else
 166         ck_assert_int_eq (error, data->expected_error);
 167 }
 168 END_PARAMETRIZED_TEST
 169 
 170 /* --------------------------------------------------------------------------------------------- */
 171 
 172 int
 173 main (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 174 {
 175     TCase *tc_core;
 176 
 177     tc_core = tcase_create ("Core");
 178 
 179     // Add new tests here: ***************
 180     mctest_add_parameterized_test (tc_core, test_hex_translate_to_regex,
 181                                    test_hex_translate_to_regex_ds);
 182     // ***********************************
 183 
 184     return mctest_run_all (tc_core);
 185 }
 186 
 187 /* --------------------------------------------------------------------------------------------- */

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