This source file includes following definitions.
- assert_score
- 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_parse_score(score_s, &score, default_score);
28
29 assert_int_equal(rc, expected_rc);
30 assert_int_equal(score, expected_score);
31 }
32
33 static void
34 null_score_string(void **state)
35 {
36 assert_score(NULL, pcmk_rc_ok, default_score);
37
38
39
40 default_score = -2000000;
41 assert_score(NULL, pcmk_rc_ok, -PCMK_SCORE_INFINITY);
42
43 default_score = 2000000;
44 assert_score(NULL, pcmk_rc_ok, PCMK_SCORE_INFINITY);
45
46 default_score = 99;
47 }
48
49 static void
50 null_score(void **state)
51 {
52 assert_int_equal(pcmk_parse_score(NULL, NULL, default_score), pcmk_rc_ok);
53 assert_int_equal(pcmk_parse_score("0", NULL, default_score), pcmk_rc_ok);
54 assert_int_equal(pcmk_parse_score("foo", NULL, default_score),
55 EINVAL);
56 }
57
58 static void
59 bad_input(void **state)
60 {
61 assert_score("redder", EINVAL, default_score);
62 assert_score("3.141592", pcmk_rc_ok, 3);
63 assert_score("0xf00d", pcmk_rc_ok, 0);
64 }
65
66 static void
67 special_values(void **state)
68 {
69 assert_score("-INFINITY", pcmk_rc_ok, -PCMK_SCORE_INFINITY);
70 assert_score("INFINITY", pcmk_rc_ok, PCMK_SCORE_INFINITY);
71 assert_score("+INFINITY", pcmk_rc_ok, PCMK_SCORE_INFINITY);
72
73 pcmk__score_red = 10;
74 pcmk__score_green = 20;
75 pcmk__score_yellow = 30;
76
77 assert_score("red", pcmk_rc_ok, pcmk__score_red);
78 assert_score("green", pcmk_rc_ok, pcmk__score_green);
79 assert_score("yellow", pcmk_rc_ok, pcmk__score_yellow);
80
81 assert_score("ReD", pcmk_rc_ok, pcmk__score_red);
82 assert_score("GrEeN", pcmk_rc_ok, pcmk__score_green);
83 assert_score("yElLoW", pcmk_rc_ok, pcmk__score_yellow);
84 }
85
86
87 #define A(x) #x
88 #define B(x) A(x)
89
90 static void
91 outside_limits(void **state)
92 {
93 char *very_long = crm_strdup_printf(" %lld0", LLONG_MAX);
94
95
96 assert_score(B(PCMK_SCORE_INFINITY) "00", pcmk_rc_ok, PCMK_SCORE_INFINITY);
97 assert_score("-" B(PCMK_SCORE_INFINITY) "00", pcmk_rc_ok,
98 -PCMK_SCORE_INFINITY);
99
100
101 assert_score(very_long, pcmk_rc_ok, PCMK_SCORE_INFINITY);
102 very_long[0] = '-';
103 assert_score(very_long, pcmk_rc_ok, -PCMK_SCORE_INFINITY);
104 free(very_long);
105 }
106
107 static void
108 inside_limits(void **state)
109 {
110 assert_score("1234", pcmk_rc_ok, 1234);
111 assert_score("-1234", pcmk_rc_ok, -1234);
112 }
113
114 PCMK__UNIT_TEST(NULL, NULL,
115 cmocka_unit_test(null_score_string),
116 cmocka_unit_test(null_score),
117 cmocka_unit_test(bad_input),
118 cmocka_unit_test(special_values),
119 cmocka_unit_test(outside_limits),
120 cmocka_unit_test(inside_limits))