pacemaker 2.1.4-dc6eb4362e
Scalable High-Availability cluster resource manager
char2score_test.c
Go to the documentation of this file.
1/*
2 * Copyright 2022 the Pacemaker project contributors
3 *
4 * The version control history for this file may have further details.
5 *
6 * This source code is licensed under the GNU Lesser General Public License
7 * version 2.1 or later (LGPLv2.1+) WITHOUT ANY WARRANTY.
8 */
9
10#include <crm_internal.h>
11
12#include <stdarg.h>
13#include <stddef.h>
14#include <stdint.h>
15#include <setjmp.h>
16#include <cmocka.h>
17
18extern int pcmk__score_red;
19extern int pcmk__score_green;
20extern int pcmk__score_yellow;
21
22static void
23empty_input(void **state)
24{
25 assert_int_equal(char2score(NULL), 0);
26}
27
28static void
29bad_input(void **state)
30{
31 assert_int_equal(char2score("PQRST"), 0);
32 assert_int_equal(char2score("3.141592"), 3);
33 assert_int_equal(char2score("0xf00d"), 0);
34}
35
36static void
37special_values(void **state)
38{
39 assert_int_equal(char2score("-INFINITY"), -CRM_SCORE_INFINITY);
40 assert_int_equal(char2score("INFINITY"), CRM_SCORE_INFINITY);
41 assert_int_equal(char2score("+INFINITY"), CRM_SCORE_INFINITY);
42
43 pcmk__score_red = 10;
46
47 assert_int_equal(char2score("red"), pcmk__score_red);
48 assert_int_equal(char2score("green"), pcmk__score_green);
49 assert_int_equal(char2score("yellow"), pcmk__score_yellow);
50
51 assert_int_equal(char2score("ReD"), pcmk__score_red);
52 assert_int_equal(char2score("GrEeN"), pcmk__score_green);
53 assert_int_equal(char2score("yElLoW"), pcmk__score_yellow);
54}
55
56/* These ridiculous macros turn an integer constant into a string constant. */
57#define A(x) #x
58#define B(x) A(x)
59
60static void
61outside_limits(void **state)
62{
63 assert_int_equal(char2score(B(CRM_SCORE_INFINITY) "00"), CRM_SCORE_INFINITY);
64 assert_int_equal(char2score("-" B(CRM_SCORE_INFINITY) "00"), -CRM_SCORE_INFINITY);
65}
66
67static void
68inside_limits(void **state)
69{
70 assert_int_equal(char2score("1234"), 1234);
71 assert_int_equal(char2score("-1234"), -1234);
72}
73
74int main(int argc, char **argv)
75{
76 const struct CMUnitTest tests[] = {
77 cmocka_unit_test(empty_input),
78 cmocka_unit_test(bad_input),
79 cmocka_unit_test(special_values),
80 cmocka_unit_test(outside_limits),
81 cmocka_unit_test(inside_limits),
82 };
83
84 cmocka_set_message_output(CM_OUTPUT_TAP);
85 return cmocka_run_group_tests(tests, NULL, NULL);
86}
int pcmk__score_yellow
Definition: scores.c:22
int main(int argc, char **argv)
#define B(x)
int pcmk__score_green
Definition: scores.c:21
int pcmk__score_red
Definition: scores.c:20
int char2score(const char *score)
Get the integer value of a score string.
Definition: scores.c:36
#define CRM_SCORE_INFINITY
Definition: crm.h:85