pacemaker 2.1.5-a3f44794f94
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
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"), -CRM_SCORE_INFINITY);
36 assert_int_equal(char2score("INFINITY"), CRM_SCORE_INFINITY);
37 assert_int_equal(char2score("+INFINITY"), CRM_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(CRM_SCORE_INFINITY) "00"), CRM_SCORE_INFINITY);
60 assert_int_equal(char2score("-" B(CRM_SCORE_INFINITY) "00"), -CRM_SCORE_INFINITY);
61}
62
63static void
64inside_limits(void **state)
65{
66 assert_int_equal(char2score("1234"), 1234);
67 assert_int_equal(char2score("-1234"), -1234);
68}
69
70PCMK__UNIT_TEST(NULL, NULL,
71 cmocka_unit_test(empty_input),
72 cmocka_unit_test(bad_input),
73 cmocka_unit_test(special_values),
74 cmocka_unit_test(outside_limits),
75 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 CRM_SCORE_INFINITY
Definition: crm.h:85
#define PCMK__UNIT_TEST(group_setup, group_teardown,...)