Merge branch '3723_skin_selector_location'
[midnight-commander.git] / tests / lib / strutil / parse_integer.c
blob667d51e15e81ae6627570743589db1b2d08462ff
1 /*
2 lib/strutil - tests for lib/strutil/parse_integer function.
4 Copyright (C) 2013-2016
5 Free Software Foundation, Inc.
7 Written by:
8 Andrew Borodin <aborodin@vmail.ru>, 2013
10 This file is part of the Midnight Commander.
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.
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.
22 You should have received a copy of the GNU General Public License
23 along with this program. If not, see <http://www.gnu.org/licenses/>.
26 #define TEST_SUITE_NAME "/lib/strutil"
28 #include "tests/mctest.h"
30 #include <inttypes.h>
32 #include "lib/strutil.h"
34 /* --------------------------------------------------------------------------------------------- */
36 /* @Before */
37 static void
38 setup (void)
42 /* --------------------------------------------------------------------------------------------- */
44 /* @After */
45 static void
46 teardown (void)
50 /* --------------------------------------------------------------------------------------------- */
52 /* @DataSource("str_replace_all_test_ds") */
53 /* *INDENT-OFF* */
54 static const struct parse_integer_test_ds
56 const char *haystack;
57 uintmax_t expected_result;
58 gboolean invalid;
59 } parse_integer_test_ds[] =
62 /* too big */
63 "99999999999999999999999999999999999999999999999999999999999999999999",
65 TRUE
68 "x",
70 TRUE
73 "9x",
75 TRUE
78 "1",
80 FALSE
83 "-1",
85 TRUE
88 "1k",
89 1024,
90 FALSE
93 "1K",
94 1024,
95 FALSE
98 "1M",
99 1024 * 1024,
100 FALSE
103 "1m",
105 TRUE
108 "64M",
109 64 * 1024 * 1024,
110 FALSE
113 "1G",
114 1 * 1024 * 1024 * 1024,
115 FALSE
118 /* *INDENT-ON* */
120 /* @Test(dataSource = "str_replace_all_test_ds") */
121 /* *INDENT-OFF* */
122 START_TEST (parse_integer_test)
123 /* *INDENT-ON* */
125 /* given */
126 uintmax_t actual_result;
127 gboolean invalid = FALSE;
128 const struct parse_integer_test_ds *data = &parse_integer_test_ds[_i];
130 /* when */
131 actual_result = parse_integer (data->haystack, &invalid);
133 /* then */
134 fail_unless (invalid == data->invalid && actual_result == data->expected_result,
135 "actial ( %" PRIuMAX ") not equal to\nexpected (%" PRIuMAX ")",
136 actual_result, data->expected_result);
138 /* *INDENT-OFF* */
139 END_TEST
140 /* *INDENT-ON* */
142 /* --------------------------------------------------------------------------------------------- */
145 main (void)
147 int number_failed;
149 Suite *s = suite_create (TEST_SUITE_NAME);
150 TCase *tc_core = tcase_create ("Core");
151 SRunner *sr;
153 tcase_add_checked_fixture (tc_core, setup, teardown);
155 /* Add new tests here: *************** */
156 tcase_add_loop_test (tc_core, parse_integer_test, 0, G_N_ELEMENTS (parse_integer_test_ds));
157 /* *********************************** */
159 suite_add_tcase (s, tc_core);
160 sr = srunner_create (s);
161 srunner_set_log (sr, "parse_integer.log");
162 srunner_run_all (sr, CK_ENV);
163 number_failed = srunner_ntests_failed (sr);
164 srunner_free (sr);
165 return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
168 /* --------------------------------------------------------------------------------------------- */