Debian Codegraph @

madrisan-nagios-plugins-linux/nagios-plugins-linux/tests/tsload_normalize.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 check_load.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 #include "system.h"
25
26 #define ARRAY_CARDINALITY(ARRAY) (sizeof(ARRAY) / sizeof(*(ARRAY)))
27
28 /* silence the compiler's warning 'function defined but not used' */
29 static _Noreturn void print_version (void) __attribute__ ((unused));
30 static _Noreturn void usage (FILE * out) __attribute__ ((unused));
31 static void validate_input (int i, double w, double c) __attribute__ ((unused));
32 static int loadavg_status (const double *loadavg, const double *wload,
33 const double *cload, const bool *required)
34 __attribute__ ((unused));
35
36 #define NPL_TESTING
37 #include "../plugins/check_load.c"
38 #undef NPL_TESTING
39
40 typedef struct test_data
41 {
42 double loadavg[3];
43 double expect_value[3];
44 unsigned int active_cpu;
45 } test_data;
46
47 static int
48 test_loadavg_normalize (const void *tdata)
49 {
50 const struct test_data *data = tdata;
51 int ret = 0;
52 size_t i;
53
54 normalize_loadavg ((double *) data->loadavg, data->active_cpu);
55
56 for (i = 0; i < ARRAY_CARDINALITY (data->loadavg); ++i)
57 TEST_ASSERT_EQUAL_NUMERIC (data->loadavg[i], data->expect_value[i]);
58
59 return ret;
60 }
61
62 static int
63 mymain (void)
64 {
65 int ret = 0;
66
67 #define DO_TEST(L1, L2, L3, E1, E2, E3, NCPU) \
68 do \
69 { \
70 test_data data = { \
71 .loadavg = { L1, L2, L3 }, \
72 .expect_value = { E1, E2, E3 }, \
73 .active_cpu = NCPU \
74 }; \
75 if (test_run("check load normalization with " #NCPU " cpu(s)", \
76 test_loadavg_normalize, (&data)) < 0) \
77 ret = -1; \
78 } \
79 while (0)
80
81 DO_TEST (/* loadavg: */ 4.0, 2.0, 1.0, /* expect_value: */ 4.0, 2.0, 1.0, 1);
82 DO_TEST (/* loadavg: */ 4.0, 2.0, 1.0, /* expect_value: */ 2.0, 1.0, 0.5, 2);
83 DO_TEST (/* loadavg: */ 4.0, 2.0, 6.0, /* expect_value: */ 0.5, .25, .75, 8);
84
85 return ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
86 }
87
88 TEST_MAIN (mymain)