Debian Codegraph @

madrisan-nagios-plugins-linux/nagios-plugins-linux/tests/tslibxstrton_agetollint.c

raw symbols SCIP
1 // SPDX-License-Identifier: GPL-3.0-or-later
2 /*
3 * License: GPLv3+
4 * Copyright (c) 2022 Davide Madrisan <[email protected]>
5 *
6 * Unit test for lib/xstrton.c
7 *
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 *
21 */
22
23 #include "testutils.h"
24
25 # define NPL_TESTING
26 # include "../lib/xstrton.c"
27 # undef NPL_TESTING
28
29 typedef struct test_data
30 {
31 char *age;
32 int64_t expect_value;
33 } test_data;
34
35 static int
36 test_agetoint64 (const void *tdata)
37 {
38 const struct test_data *data = tdata;
39 long long int result;
40 char *errmegs;
41 int ret = 0;
42
43 agetollint (data->age, &result, &errmegs);
44 TEST_ASSERT_EQUAL_NUMERIC (result, data->expect_value);
45 return ret;
46 }
47
48 static int
49 mymain (void)
50 {
51 int ret = 0;
52
53 # define DO_TEST(AGE, EXPECT_VALUE) \
54 do \
55 { \
56 test_data data = { \
57 .age = AGE, \
58 .expect_value = EXPECT_VALUE, \
59 }; \
60 if (test_run("check function agetollint with arg " AGE, \
61 test_agetoint64, (&data)) < 0) \
62 ret = -1; \
63 } \
64 while (0)
65
66 /* test the function agetoint64() */
67
68 #define ONE_MIN 60
69 #define ONE_HOUR (60 * ONE_MIN)
70 #define ONE_DAY (24 * ONE_HOUR)
71 #define ONE_WEEK (7 * ONE_DAY)
72 #define ONE_YEAR (365.25 * ONE_DAY)
73
74 DO_TEST ("100s", 100);
75 DO_TEST ("30m", 30 * ONE_MIN);
76 DO_TEST ("4h", 4 * ONE_HOUR);
77 DO_TEST ("10d", 10 * ONE_DAY);
78 DO_TEST ("0.5d", 12 * ONE_HOUR);
79 DO_TEST ("4w", 4 * ONE_WEEK);
80 DO_TEST ("1y", ONE_YEAR);
81
82 DO_TEST ("100S", 100);
83 DO_TEST ("30M", 30 * ONE_MIN);
84 DO_TEST ("4H", 4 * ONE_HOUR);
85 DO_TEST ("10D", 10 * ONE_DAY);
86 DO_TEST ("0.5D", 12 * ONE_HOUR);
87 DO_TEST ("4W", 4 * ONE_WEEK);
88 DO_TEST ("1Y", ONE_YEAR);
89
90 return ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
91 }
92 TEST_MAIN (mymain)