1 /* 2 lib/strutil - tests for lib/strutil/str_verscmp function. 3 Testcases are taken from Gnulib. 4 5 Copyright (C) 2019-2024 6 Free Software Foundation, Inc. 7 8 Written by: 9 Andrew Borodin <aborodin@vmail.ru>, 2019 10 11 This file is part of the Midnight Commander. 12 13 The Midnight Commander is free software: you can redistribute it 14 and/or modify it under the terms of the GNU General Public License as 15 published by the Free Software Foundation, either version 3 of the License, 16 or (at your option) any later version. 17 18 The Midnight Commander is distributed in the hope that it will be useful, 19 but WITHOUT ANY WARRANTY; without even the implied warranty of 20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 21 GNU General Public License for more details. 22 23 You should have received a copy of the GNU General Public License 24 along with this program. If not, see <http://www.gnu.org/licenses/>. 25 */ 26 27 #define TEST_SUITE_NAME "/lib/strutil" 28 29 #include "tests/mctest.h" 30 31 #include "lib/strutil.h" 32 #include "lib/util.h" /* _GL_CMP() */ 33 34 /* --------------------------------------------------------------------------------------------- */ 35 36 /* From glibc bug 9913 */ 37 static char const a[] = "B0075022800016.gbp.corp.com"; 38 static char const b[] = "B007502280067.gbp.corp.com"; 39 static char const c[] = "B007502357019.GBP.CORP.COM"; 40 41 /* --------------------------------------------------------------------------------------------- */ 42 43 /* @Before */ 44 static void 45 setup (void) /* */ 46 { 47 } 48 49 /* --------------------------------------------------------------------------------------------- */ 50 51 /* @After */ 52 static void 53 teardown (void) /* */ 54 { 55 } 56 57 /* --------------------------------------------------------------------------------------------- */ 58 59 static int 60 sign (int n) /* */ 61 { 62 return _GL_CMP (n, 0); 63 } 64 65 /* --------------------------------------------------------------------------------------------- */ 66 67 /* @DataSource("str_verscmp_test_ds") */ 68 /* *INDENT-OFF* */ 69 static const struct str_verscmp_test_struct 70 { 71 const char *s1; 72 const char *s2; 73 int expected_result; 74 } str_verscmp_test_ds[] = 75 { 76 { "", "", 0 }, 77 { "a", "a", 0 }, 78 { "a", "b", -1 }, 79 { "b", "a", 1 }, 80 { "000", "00", -1 }, 81 { "00", "000", 1 }, 82 { "a0", "a", 1 }, 83 { "00", "01", -1 }, 84 { "01", "010", -1 }, 85 { "010", "09", -1 }, 86 { "09", "0", -1 }, 87 { "9", "10", -1 }, 88 { "0a", "0", 1 }, 89 /* From glibc bug 9913 */ 90 { a, b, -1 }, 91 { b, c, -1 }, 92 { a, c, -1 }, 93 { b, a, 1 }, 94 { c, b, 1 }, 95 { c, a, 1 } 96 }; 97 /* *INDENT-ON* */ 98 99 /* @Test(dataSource = "str_verscmp_test_ds") */ 100 /* *INDENT-OFF* */ 101 START_TEST (str_verscmp_test) /* */ 102 /* *INDENT-ON* */ 103 { 104 /* given */ 105 int actual_result; 106 const struct str_verscmp_test_struct *data = &str_verscmp_test_ds[_i]; 107 108 /* when */ 109 actual_result = str_verscmp (data->s1, data->s2); 110 111 /* then */ 112 ck_assert_int_eq (sign (actual_result), sign (data->expected_result)); 113 } 114 /* *INDENT-OFF* */ 115 END_TEST 116 /* *INDENT-ON* */ 117 118 /* --------------------------------------------------------------------------------------------- */ 119 120 int 121 main (void) /* */ 122 { 123 TCase *tc_core; 124 125 tc_core = tcase_create ("Core"); 126 127 tcase_add_checked_fixture (tc_core, setup, teardown); 128 129 /* Add new tests here: *************** */ 130 mctest_add_parameterized_test (tc_core, str_verscmp_test, str_verscmp_test_ds); 131 /* *********************************** */ 132 133 return mctest_run_all (tc_core); 134 } 135 136 /* --------------------------------------------------------------------------------------------- */