Debian Codegraph @

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

raw symbols SCIP
1 // SPDX-License-Identifier: GPL-3.0-or-later
2 /*
3 * License: GPLv3+
4 * Copyright (c) 2022,2024 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 *size;
32 long long int expect_value;
33 } test_data;
34
35 static int
36 test_sizetollint (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 sizetollint (data->size, &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(SIZE, EXPECT_VALUE) \
54 do \
55 { \
56 test_data data = { \
57 .size = SIZE, \
58 .expect_value = EXPECT_VALUE, \
59 }; \
60 if (test_run("check function sizetollint with arg " SIZE, \
61 test_sizetollint, (&data)) < 0) \
62 ret = -1; \
63 } \
64 while (0)
65
66 /* test the function sizetoint64() */
67
68 #define ONE_KILOBYTE 1000ULL
69 #define ONE_MEGABYTE (1000ULL * ONE_KILOBYTE)
70 #define ONE_GIGABYTE (1000ULL * ONE_MEGABYTE)
71 #define ONE_TERABYTE (1000ULL * ONE_GIGABYTE)
72 #define ONE_PETABYTE (1000ULL * ONE_TERABYTE)
73
74 DO_TEST ("1024b", 1024);
75 DO_TEST ("8k", 8 * ONE_KILOBYTE);
76 DO_TEST ("50m", 50 * ONE_MEGABYTE);
77 DO_TEST ("2g", 2 * ONE_GIGABYTE);
78 DO_TEST ("3t", 3 * ONE_TERABYTE);
79 DO_TEST ("2p", 2 * ONE_PETABYTE);
80
81 DO_TEST ("1024B", 1024);
82 DO_TEST ("8K", 8 * ONE_KILOBYTE);
83 DO_TEST ("50M", 50 * ONE_MEGABYTE);
84 DO_TEST ("2G", 2 * ONE_GIGABYTE);
85 DO_TEST ("3T", 3 * ONE_TERABYTE);
86 DO_TEST ("2P", 2 * ONE_PETABYTE);
87
88 return ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
89 }
90
91 TEST_MAIN (mymain)