| 1 | /* |
| 2 | libmc - checks for hex pattern parsing |
| 3 | |
| 4 | Copyright (C) 2011-2016 |
| 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 | } test_hex_translate_to_regex_ds[] = |
| 38 | { |
| 39 | { |
| 40 | /* Simplest case */ |
| 41 | "12 34", |
| 42 | "\\x12\\x34" |
| 43 | }, |
| 44 | { |
| 45 | /* Prefixes (0x, 0X) */ |
| 46 | "0x12 0X34", |
| 47 | "\\x12\\x34" |
| 48 | }, |
| 49 | { |
| 50 | /* Prefix "0" doesn't signify octal! Numbers are always interpreted in hex. */ |
| 51 | "012", |
| 52 | "\\x12" |
| 53 | }, |
| 54 | { |
| 55 | /* Extra whitespace (but not trailing one) */ |
| 56 | " 12 34", |
| 57 | "\\x12\\x34" |
| 58 | }, |
| 59 | { |
| 60 | /* Min/max values */ |
| 61 | "0 ff", |
| 62 | "\\x00\\xFF" |
| 63 | }, |
| 64 | }; |
| 65 | /* *INDENT-ON* */ |
| 66 | |
| 67 | /* @Test(dataSource = "test_hex_translate_to_regex_ds") */ |
| 68 | /* *INDENT-OFF* */ |
| 69 | START_PARAMETRIZED_TEST (test_hex_translate_to_regex, test_hex_translate_to_regex_ds) |
| 70 | /* *INDENT-ON* */ |
| 71 | { |
| 72 | /* given */ |
| 73 | GString *tmp = g_string_new (data->input_value); |
| 74 | GString *dest_str; |
| 75 | |
| 76 | /* when */ |
| 77 | dest_str = mc_search__hex_translate_to_regex (tmp); |
| 78 | |
| 79 | /* then */ |
| 80 | g_string_free (tmp, TRUE); |
| 81 | |
| 82 | mctest_assert_str_eq (dest_str->str, data->expected_result); |
| 83 | g_string_free (dest_str, TRUE); |
| 84 | } |
| 85 | /* *INDENT-OFF* */ |
| 86 | END_PARAMETRIZED_TEST |
| 87 | /* *INDENT-ON* */ |
| 88 | |
| 89 | /* --------------------------------------------------------------------------------------------- */ |
| 90 | |
| 91 | int |
| 92 | main (void) |
| 93 | { |
| 94 | int number_failed; |
| 95 | |
| 96 | Suite *s = suite_create (TEST_SUITE_NAME); |
| 97 | TCase *tc_core = tcase_create ("Core"); |
| 98 | SRunner *sr; |
| 99 | |
| 100 | /* Add new tests here: *************** */ |
| 101 | mctest_add_parameterized_test (tc_core, test_hex_translate_to_regex, |
| 102 | test_hex_translate_to_regex_ds); |
| 103 | /* *********************************** */ |
| 104 | |
| 105 | suite_add_tcase (s, tc_core); |
| 106 | sr = srunner_create (s); |
| 107 | srunner_set_log (sr, "hex_translate_to_regex.log"); |
| 108 | srunner_run_all (sr, CK_ENV); |
| 109 | number_failed = srunner_ntests_failed (sr); |
| 110 | srunner_free (sr); |
| 111 | return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE; |
| 112 | } |
| 113 | |
| 114 | /* --------------------------------------------------------------------------------------------- */ |