pacemaker  2.1.3-ea053b43a
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 
18 extern int pcmk__score_red;
19 extern int pcmk__score_green;
20 extern int pcmk__score_yellow;
21 
22 static void
23 empty_input(void **state)
24 {
25  assert_int_equal(char2score(NULL), 0);
26 }
27 
28 static void
29 bad_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 
36 static void
37 special_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;
44  pcmk__score_green = 20;
45  pcmk__score_yellow = 30;
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 
60 static void
61 outside_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 
67 static void
68 inside_limits(void **state)
69 {
70  assert_int_equal(char2score("1234"), 1234);
71  assert_int_equal(char2score("-1234"), -1234);
72 }
73 
74 int 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_red
Definition: scores.c:20
int char2score(const char *score)
Get the integer value of a score string.
Definition: scores.c:36
int pcmk__score_yellow
Definition: scores.c:22
#define B(x)
#define CRM_SCORE_INFINITY
Definition: crm.h:85
int main(int argc, char **argv)
int pcmk__score_green
Definition: scores.c:21