pacemaker  2.1.8-3980678f03
Scalable High-Availability cluster resource manager
pcmk__numeric_strcasecmp_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 General Public License version 2
7  * or later (GPLv2+) WITHOUT ANY WARRANTY.
8  */
9 
10 #include <crm_internal.h>
11 
13 
14 static void
15 null_ptr(void **state)
16 {
20 }
21 
22 static void
23 no_numbers(void **state)
24 {
25  /* All comparisons are done case-insensitively. */
26  assert_int_equal(pcmk__numeric_strcasecmp("abcd", "efgh"), -1);
27  assert_int_equal(pcmk__numeric_strcasecmp("abcd", "abcd"), 0);
28  assert_int_equal(pcmk__numeric_strcasecmp("efgh", "abcd"), 1);
29 
30  assert_int_equal(pcmk__numeric_strcasecmp("AbCd", "eFgH"), -1);
31  assert_int_equal(pcmk__numeric_strcasecmp("ABCD", "abcd"), 0);
32  assert_int_equal(pcmk__numeric_strcasecmp("EFgh", "ABcd"), 1);
33 }
34 
35 static void
36 trailing_numbers(void **state)
37 {
38  assert_int_equal(pcmk__numeric_strcasecmp("node1", "node2"), -1);
39  assert_int_equal(pcmk__numeric_strcasecmp("node1", "node1"), 0);
40  assert_int_equal(pcmk__numeric_strcasecmp("node2", "node1"), 1);
41 
42  assert_int_equal(pcmk__numeric_strcasecmp("node1", "node10"), -1);
43  assert_int_equal(pcmk__numeric_strcasecmp("node10", "node10"), 0);
44  assert_int_equal(pcmk__numeric_strcasecmp("node10", "node1"), 1);
45 
46  assert_int_equal(pcmk__numeric_strcasecmp("node10", "remotenode9"), -1);
47  assert_int_equal(pcmk__numeric_strcasecmp("remotenode9", "node10"), 1);
48 
49  /* Longer numbers sort higher than shorter numbers. */
50  assert_int_equal(pcmk__numeric_strcasecmp("node001", "node1"), 1);
51  assert_int_equal(pcmk__numeric_strcasecmp("node1", "node001"), -1);
52 }
53 
54 static void
55 middle_numbers(void **state)
56 {
57  assert_int_equal(pcmk__numeric_strcasecmp("node1abc", "node1def"), -1);
58  assert_int_equal(pcmk__numeric_strcasecmp("node1def", "node1abc"), 1);
59 
60  assert_int_equal(pcmk__numeric_strcasecmp("node1abc", "node2abc"), -1);
61  assert_int_equal(pcmk__numeric_strcasecmp("node2abc", "node1abc"), 1);
62 }
63 
64 static void
65 unequal_lengths(void **state)
66 {
67  assert_int_equal(pcmk__numeric_strcasecmp("node-ab", "node-abc"), -1);
68  assert_int_equal(pcmk__numeric_strcasecmp("node-abc", "node-ab"), 1);
69 
70  assert_int_equal(pcmk__numeric_strcasecmp("node1ab", "node1abc"), -1);
71  assert_int_equal(pcmk__numeric_strcasecmp("node1abc", "node1ab"), 1);
72 }
73 
74 PCMK__UNIT_TEST(NULL, NULL,
75  cmocka_unit_test(null_ptr),
76  cmocka_unit_test(no_numbers),
77  cmocka_unit_test(trailing_numbers),
78  cmocka_unit_test(middle_numbers),
79  cmocka_unit_test(unequal_lengths))
#define PCMK__UNIT_TEST(group_setup, group_teardown,...)
#define pcmk__assert_asserts(stmt)
int pcmk__numeric_strcasecmp(const char *s1, const char *s2)
Definition: strings.c:1078