Debian Codegraph @

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

raw symbols SCIP
1 // SPDX-License-Identifier: GPL-3.0-or-later
2 /*
3 * License: GPLv3+
4 * Copyright (c) 2017 Davide Madrisan <[email protected]>
5 *
6 * Unit test for lib/meminfo.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 "meminfo.h"
24 #include "testutils.h"
25 #include "units.h"
26
27 typedef struct test_data
28 {
29 unsigned long long memsize;
30 unsigned long long expect_value;
31 unit_shift shift;
32 } test_data;
33
34 static int
35 test_memory_unit_conversion (const void *tdata)
36 {
37 const struct test_data *data = tdata;
38 int ret = 0;
39
40 TEST_ASSERT_EQUAL_NUMERIC (
41 UNIT_CONVERT (data->memsize, data->shift), data->expect_value);
42 return ret;
43 }
44
45 static int
46 mymain (void)
47 {
48 int ret = 0;
49
50 #define KILO 1024UL
51 #define MEGA KILO*KILO
52
53 #define DO_TEST(MSG, MEMSIZE, SHIFT, EXPECT_VALUE) \
54 do \
55 { \
56 test_data data = { \
57 .memsize = MEMSIZE, \
58 .shift = SHIFT, \
59 .expect_value = EXPECT_VALUE, \
60 }; \
61 if (test_run(MSG, test_memory_unit_conversion, &data) < 0) \
62 ret = -1; \
63 } \
64 while (0)
65
66 /* unit conversion */
67 DO_TEST ("check memory size conversion into kbytes",
68 KILO, k_shift, KILO);
69 DO_TEST ("check memory size conversion into mbytes",
70 2*KILO, m_shift, 2UL);
71 DO_TEST ("check memory size conversion into gbytes",
72 4*MEGA, g_shift, 4UL);
73
74 return ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
75 }
76
77 TEST_MAIN (mymain)