This source file includes following definitions.
- assert_score
- invalid_args
- null_score_string
- null_score
- bad_input
- special_values
- outside_limits
- inside_limits
1
2
3
4
5
6
7
8
9
10 #include <crm_internal.h>
11
12 #include <limits.h>
13
14 #include <crm/common/scores.h>
15 #include <crm/common/unittest_internal.h>
16
17 extern int pcmk__score_red;
18 extern int pcmk__score_green;
19 extern int pcmk__score_yellow;
20
21 static int default_score = 99;
22
23 static void
24 assert_score(const char *score_s, int expected_rc, int expected_score)
25 {
26 int score = 0;
27 int rc = pcmk_rc_ok;
28 xmlNode *xml = pcmk__xe_create(NULL, __func__);
29
30 crm_xml_add(xml, "test_attr", score_s);
31 rc = pcmk__xe_get_score(xml, "test_attr", &score, default_score);
32 assert_int_equal(rc, expected_rc);
33 assert_int_equal(score, expected_score);
34 free_xml(xml);
35 }
36
37 static void
38 invalid_args(void **state)
39 {
40 int score = 0;
41 xmlNode *xml = pcmk__xe_create(NULL, __func__);
42
43 assert_int_equal(pcmk__xe_get_score(NULL, NULL, &score, default_score),
44 EINVAL);
45 assert_int_equal(pcmk__xe_get_score(xml, NULL, &score, default_score),
46 EINVAL);
47 assert_int_equal(pcmk__xe_get_score(NULL, "test", &score, default_score),
48 EINVAL);
49 free_xml(xml);
50 }
51
52 static void
53 null_score_string(void **state)
54 {
55 assert_score(NULL, pcmk_rc_ok, default_score);
56
57
58
59 default_score = -2000000;
60 assert_score(NULL, pcmk_rc_ok, -PCMK_SCORE_INFINITY);
61
62 default_score = 2000000;
63 assert_score(NULL, pcmk_rc_ok, PCMK_SCORE_INFINITY);
64
65 default_score = 99;
66 }
67
68 static void
69 null_score(void **state)
70 {
71 xmlNode *xml = pcmk__xe_create(NULL, __func__);
72
73 assert_int_equal(pcmk__xe_get_score(xml, "test_attr", NULL, default_score),
74 pcmk_rc_ok);
75
76 crm_xml_add(xml, "test_attr", "0");
77 assert_int_equal(pcmk__xe_get_score(xml, "test_attr", NULL, default_score),
78 pcmk_rc_ok);
79
80 crm_xml_add(xml, "test_attr", "foo");
81 assert_int_equal(pcmk__xe_get_score(xml, "test_attr", NULL, default_score),
82 EINVAL);
83
84 free_xml(xml);
85 }
86
87 static void
88 bad_input(void **state)
89 {
90 assert_score("redder", EINVAL, default_score);
91 assert_score("3.141592", pcmk_rc_ok, 3);
92 assert_score("0xf00d", pcmk_rc_ok, 0);
93 }
94
95 static void
96 special_values(void **state)
97 {
98 assert_score("-INFINITY", pcmk_rc_ok, -PCMK_SCORE_INFINITY);
99 assert_score("INFINITY", pcmk_rc_ok, PCMK_SCORE_INFINITY);
100 assert_score("+INFINITY", pcmk_rc_ok, PCMK_SCORE_INFINITY);
101
102 pcmk__score_red = 10;
103 pcmk__score_green = 20;
104 pcmk__score_yellow = 30;
105
106 assert_score("red", pcmk_rc_ok, pcmk__score_red);
107 assert_score("green", pcmk_rc_ok, pcmk__score_green);
108 assert_score("yellow", pcmk_rc_ok, pcmk__score_yellow);
109
110 assert_score("ReD", pcmk_rc_ok, pcmk__score_red);
111 assert_score("GrEeN", pcmk_rc_ok, pcmk__score_green);
112 assert_score("yElLoW", pcmk_rc_ok, pcmk__score_yellow);
113 }
114
115
116 #define A(x) #x
117 #define B(x) A(x)
118
119 static void
120 outside_limits(void **state)
121 {
122 char *very_long = crm_strdup_printf(" %lld0", LLONG_MAX);
123
124
125 assert_score(B(PCMK_SCORE_INFINITY) "00", pcmk_rc_ok, PCMK_SCORE_INFINITY);
126 assert_score("-" B(PCMK_SCORE_INFINITY) "00", pcmk_rc_ok,
127 -PCMK_SCORE_INFINITY);
128
129
130 assert_score(very_long, pcmk_rc_ok, PCMK_SCORE_INFINITY);
131 very_long[0] = '-';
132 assert_score(very_long, pcmk_rc_ok, -PCMK_SCORE_INFINITY);
133 }
134
135 static void
136 inside_limits(void **state)
137 {
138 assert_score("1234", pcmk_rc_ok, 1234);
139 assert_score("-1234", pcmk_rc_ok, -1234);
140 }
141
142 PCMK__UNIT_TEST(NULL, NULL,
143 cmocka_unit_test(invalid_args),
144 cmocka_unit_test(null_score_string),
145 cmocka_unit_test(null_score),
146 cmocka_unit_test(bad_input),
147 cmocka_unit_test(special_values),
148 cmocka_unit_test(outside_limits),
149 cmocka_unit_test(inside_limits))