pacemaker 2.1.8-2.1.8
Scalable High-Availability cluster resource manager
Loading...
Searching...
No Matches
char2score_test.c
Go to the documentation of this file.
1/*
2 * Copyright 2022-2024 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 General Public License version 2
7 * or later (GPLv2+) WITHOUT ANY WARRANTY.
8 */
9
10#include <crm_internal.h>
11
13
14extern int pcmk__score_red;
15extern int pcmk__score_green;
16extern int pcmk__score_yellow;
17
18static void
19empty_input(void **state)
20{
21 assert_int_equal(char2score(NULL), 0);
22}
23
24static void
25bad_input(void **state)
26{
27 assert_int_equal(char2score("PQRST"), 0);
28 assert_int_equal(char2score("3.141592"), 3);
29 assert_int_equal(char2score("0xf00d"), 0);
30}
31
32static void
33special_values(void **state)
34{
35 assert_int_equal(char2score("-INFINITY"), -PCMK_SCORE_INFINITY);
36 assert_int_equal(char2score("INFINITY"), PCMK_SCORE_INFINITY);
37 assert_int_equal(char2score("+INFINITY"), PCMK_SCORE_INFINITY);
38
39 pcmk__score_red = 10;
42
43 assert_int_equal(char2score("red"), pcmk__score_red);
44 assert_int_equal(char2score("green"), pcmk__score_green);
45 assert_int_equal(char2score("yellow"), pcmk__score_yellow);
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
52/* These ridiculous macros turn an integer constant into a string constant. */
53#define A(x) #x
54#define B(x) A(x)
55
56static void
57outside_limits(void **state)
58{
59 assert_int_equal(char2score(B(PCMK_SCORE_INFINITY) "00"),
61 assert_int_equal(char2score("-" B(PCMK_SCORE_INFINITY) "00"),
63}
64
65static void
66inside_limits(void **state)
67{
68 assert_int_equal(char2score("1234"), 1234);
69 assert_int_equal(char2score("-1234"), -1234);
70}
71
72PCMK__UNIT_TEST(NULL, NULL,
73 cmocka_unit_test(empty_input),
74 cmocka_unit_test(bad_input),
75 cmocka_unit_test(special_values),
76 cmocka_unit_test(outside_limits),
77 cmocka_unit_test(inside_limits))
int pcmk__score_yellow
Definition scores.c:22
#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 PCMK_SCORE_INFINITY
Integer score to use to represent "infinity".
Definition scores.h:24
#define PCMK__UNIT_TEST(group_setup, group_teardown,...)