1 /*
2 lib/strutil - tests for lib/strutil/parse_integer function.
3
4 Copyright (C) 2013-2025
5 Free Software Foundation, Inc.
6
7 Written by:
8 Andrew Borodin <aborodin@vmail.ru>, 2013
9
10 This file is part of the Midnight Commander.
11
12 The Midnight Commander is free software: you can redistribute it
13 and/or modify it under the terms of the GNU General Public License as
14 published by the Free Software Foundation, either version 3 of the License,
15 or (at your option) any later version.
16
17 The Midnight Commander is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
21
22 You should have received a copy of the GNU General Public License
23 along with this program. If not, see <https://www.gnu.org/licenses/>.
24 */
25
26 #define TEST_SUITE_NAME "/lib/strutil"
27
28 #include "tests/mctest.h"
29
30 #include <inttypes.h>
31
32 #include "lib/strutil.h"
33
34 /* --------------------------------------------------------------------------------------------- */
35
36 /* @DataSource("parse_integer_test_ds") */
37 static const struct parse_integer_test_ds
38 {
39 const char *haystack;
40 uintmax_t expected_result;
41 gboolean invalid;
42 } parse_integer_test_ds[] = {
43 { // too big
44 "99999999999999999999999999999999999999999999999999999999999999999999", 0, TRUE },
45 { "x", 0, TRUE },
46 { "9x", 0, TRUE },
47 { "1", 1, FALSE },
48 { "-1", 0, TRUE },
49 { "1k", 1024, FALSE },
50 { "1K", 1024, FALSE },
51 { "1M", 1024 * 1024, FALSE },
52 { "1m", 0, TRUE },
53 { "64M", 64 * 1024 * 1024, FALSE },
54 { "1G", 1 * 1024 * 1024 * 1024, FALSE },
55 };
56
57 /* @Test(dataSource = "parse_integer_test_ds") */
58 START_PARAMETRIZED_TEST (parse_integer_test, parse_integer_test_ds)
/* ![[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)
*/
59 {
60 // given
61 uintmax_t actual_result;
62 gboolean invalid = FALSE;
63
64 // when
65 actual_result = parse_integer (data->haystack, &invalid);
66
67 // then
68 ck_assert_msg (invalid == data->invalid && actual_result == data->expected_result,
69 "actual ( %" PRIuMAX ") not equal to\nexpected (%" PRIuMAX ")", actual_result,
70 data->expected_result);
71 }
72 END_PARAMETRIZED_TEST
73
74 /* --------------------------------------------------------------------------------------------- */
75
76 int
77 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)
*/
78 {
79 TCase *tc_core;
80
81 tc_core = tcase_create ("Core");
82
83 // Add new tests here: ***************
84 mctest_add_parameterized_test (tc_core, parse_integer_test, parse_integer_test_ds);
85 // ***********************************
86
87 return mctest_run_all (tc_core);
88 }
89
90 /* --------------------------------------------------------------------------------------------- */