pacemaker  2.1.9-49aab99839
Scalable High-Availability cluster resource manager
pcmk__strcmp_test.c
Go to the documentation of this file.
1 /*
2  * Copyright 2020-2021 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 same_pointer(void **state) {
16  const char *s1 = "abcd";
17  const char *s2 = "wxyz";
18 
19  assert_int_equal(pcmk__strcmp(s1, s1, pcmk__str_none), 0);
20  assert_true(pcmk__str_eq(s1, s1, pcmk__str_none));
21  assert_int_not_equal(pcmk__strcmp(s1, s2, pcmk__str_none), 0);
22  assert_false(pcmk__str_eq(s1, s2, pcmk__str_none));
23  assert_int_equal(pcmk__strcmp(NULL, NULL, pcmk__str_none), 0);
24 }
25 
26 static void
27 one_is_null(void **state) {
28  const char *s1 = "abcd";
29 
30  assert_int_equal(pcmk__strcmp(s1, NULL, pcmk__str_null_matches), 0);
31  assert_true(pcmk__str_eq(s1, NULL, pcmk__str_null_matches));
32  assert_int_equal(pcmk__strcmp(NULL, s1, pcmk__str_null_matches), 0);
33  assert_true(pcmk__strcmp(s1, NULL, pcmk__str_none) > 0);
34  assert_false(pcmk__str_eq(s1, NULL, pcmk__str_none));
35  assert_true(pcmk__strcmp(NULL, s1, pcmk__str_none) < 0);
36 }
37 
38 static void
39 case_matters(void **state) {
40  const char *s1 = "abcd";
41  const char *s2 = "ABCD";
42 
43  assert_true(pcmk__strcmp(s1, s2, pcmk__str_none) > 0);
44  assert_false(pcmk__str_eq(s1, s2, pcmk__str_none));
45  assert_true(pcmk__strcmp(s2, s1, pcmk__str_none) < 0);
46 }
47 
48 static void
49 case_insensitive(void **state) {
50  const char *s1 = "abcd";
51  const char *s2 = "ABCD";
52 
53  assert_int_equal(pcmk__strcmp(s1, s2, pcmk__str_casei), 0);
54  assert_true(pcmk__str_eq(s1, s2, pcmk__str_casei));
55 }
56 
57 static void
58 regex(void **state) {
59  const char *s1 = "abcd";
60  const char *s2 = "ABCD";
61 
62  assert_true(pcmk__strcmp(NULL, "a..d", pcmk__str_regex) > 0);
63  assert_true(pcmk__strcmp(s1, NULL, pcmk__str_regex) > 0);
64  assert_int_equal(pcmk__strcmp(s1, "a..d", pcmk__str_regex), 0);
65  assert_true(pcmk__str_eq(s1, "a..d", pcmk__str_regex));
66  assert_int_not_equal(pcmk__strcmp(s1, "xxyy", pcmk__str_regex), 0);
67  assert_false(pcmk__str_eq(s1, "xxyy", pcmk__str_regex));
68  assert_int_equal(pcmk__strcmp(s2, "a..d", pcmk__str_regex|pcmk__str_casei), 0);
69  assert_true(pcmk__str_eq(s2, "a..d", pcmk__str_regex|pcmk__str_casei));
70  assert_int_not_equal(pcmk__strcmp(s2, "a..d", pcmk__str_regex), 0);
71  assert_false(pcmk__str_eq(s2, "a..d", pcmk__str_regex));
72  assert_true(pcmk__strcmp(s2, "*ab", pcmk__str_regex) > 0);
73 }
74 
75 PCMK__UNIT_TEST(NULL, NULL,
76  cmocka_unit_test(same_pointer),
77  cmocka_unit_test(one_is_null),
78  cmocka_unit_test(case_matters),
79  cmocka_unit_test(case_insensitive),
80  cmocka_unit_test(regex))
#define PCMK__UNIT_TEST(group_setup, group_teardown,...)
int pcmk__strcmp(const char *s1, const char *s2, uint32_t flags)
Definition: strings.c:1175