1 /*
2 lib/strutil - tests for lib/strutil/str_verscmp function.
3 Testcases are taken from Gnulib.
4
5 Copyright (C) 2019-2025
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 <https://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 static int
44 sign (int n)
/* ![[previous]](../icons/n_left.png)
![[next]](../icons/right.png)
![[first]](../icons/n_first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
45 {
46 return _GL_CMP (n, 0);
47 }
48
49 /* --------------------------------------------------------------------------------------------- */
50
51 /* @DataSource("str_verscmp_test_ds") */
52 static const struct str_verscmp_test_struct
53 {
54 const char *s1;
55 const char *s2;
56 int expected_result;
57 } str_verscmp_test_ds[] = {
58 { "", "", 0 },
59 { "a", "a", 0 },
60 { "a", "b", -1 },
61 { "b", "a", 1 },
62 { "000", "00", -1 },
63 { "00", "000", 1 },
64 { "a0", "a", 1 },
65 { "00", "01", -1 },
66 { "01", "010", -1 },
67 { "010", "09", -1 },
68 { "09", "0", -1 },
69 { "9", "10", -1 },
70 { "0a", "0", 1 },
71 // From glibc bug 9913
72 { a, b, -1 },
73 { b, c, -1 },
74 { a, c, -1 },
75 { b, a, 1 },
76 { c, b, 1 },
77 { c, a, 1 },
78 };
79
80 /* @Test(dataSource = "str_verscmp_test_ds") */
81 START_TEST (str_verscmp_test)
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
82 {
83 // given
84 int actual_result;
85 const struct str_verscmp_test_struct *data = &str_verscmp_test_ds[_i];
86
87 // when
88 actual_result = str_verscmp (data->s1, data->s2);
89
90 // then
91 ck_assert_int_eq (sign (actual_result), sign (data->expected_result));
92 }
93 END_TEST
94
95 /* --------------------------------------------------------------------------------------------- */
96
97 int
98 main (void)
/* ![[previous]](../icons/left.png)
![[next]](../icons/n_right.png)
![[first]](../icons/first.png)
![[last]](../icons/n_last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
99 {
100 TCase *tc_core;
101
102 tc_core = tcase_create ("Core");
103
104 // Add new tests here: ***************
105 mctest_add_parameterized_test (tc_core, str_verscmp_test, str_verscmp_test_ds);
106 // ***********************************
107
108 return mctest_run_all (tc_core);
109 }
110
111 /* --------------------------------------------------------------------------------------------- */