pacemaker 2.1.4-dc6eb4362e
Scalable High-Availability cluster resource manager
score2char_stack_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
18static void
19invalid_params(void **state)
20{
21 char *buf = malloc(9);
22
23 assert_null(score2char_stack(100, NULL, 9));
24 assert_null(score2char_stack(100, buf, 9));
25
26 free(buf);
27}
28
29static void
30outside_limits(void **state)
31{
32 char *buf = malloc(10);
33
34 buf = score2char_stack(CRM_SCORE_INFINITY * 2, buf, 10);
35 assert_string_equal(buf, CRM_INFINITY_S);
36
37 buf = score2char_stack(-CRM_SCORE_INFINITY * 2, buf, 10);
38 assert_string_equal(buf, CRM_MINUS_INFINITY_S);
39
40 free(buf);
41}
42
43static void
44inside_limits(void **state)
45{
46 char *buf = malloc(10);
47
48 buf = score2char_stack(0, buf, 10);
49 assert_string_equal(buf, "0");
50
51 buf = score2char_stack(1024, buf, 10);
52 assert_string_equal(buf, "1024");
53
54 buf = score2char_stack(-1024, buf, 10);
55 assert_string_equal(buf, "-1024");
56
57 free(buf);
58}
59
60int main(int argc, char **argv)
61{
62 const struct CMUnitTest tests[] = {
63 cmocka_unit_test(invalid_params),
64 cmocka_unit_test(outside_limits),
65 cmocka_unit_test(inside_limits),
66 };
67
68 cmocka_set_message_output(CM_OUTPUT_TAP);
69 return cmocka_run_group_tests(tests, NULL, NULL);
70}
char * score2char_stack(int score, char *buf, size_t len)
Convert an integer score to a string, using a provided buffer.
Definition: scores.c:85
#define CRM_SCORE_INFINITY
Definition: crm.h:85
#define CRM_INFINITY_S
Definition: crm.h:86
#define CRM_MINUS_INFINITY_S
Definition: crm.h:88
int main(int argc, char **argv)