pacemaker  2.1.2-ada5c3b36
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 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 
18 static void
19 same_pointer(void **state) {
20  const char *s1 = "abcd";
21  const char *s2 = "wxyz";
22 
23  assert_int_equal(pcmk__strcmp(s1, s1, pcmk__str_none), 0);
24  assert_true(pcmk__str_eq(s1, s1, pcmk__str_none));
25  assert_int_not_equal(pcmk__strcmp(s1, s2, pcmk__str_none), 0);
26  assert_false(pcmk__str_eq(s1, s2, pcmk__str_none));
27  assert_int_equal(pcmk__strcmp(NULL, NULL, pcmk__str_none), 0);
28 }
29 
30 static void
31 one_is_null(void **state) {
32  const char *s1 = "abcd";
33 
34  assert_int_equal(pcmk__strcmp(s1, NULL, pcmk__str_null_matches), 0);
35  assert_true(pcmk__str_eq(s1, NULL, pcmk__str_null_matches));
36  assert_int_equal(pcmk__strcmp(NULL, s1, pcmk__str_null_matches), 0);
37  assert_in_range(pcmk__strcmp(s1, NULL, pcmk__str_none), 1, 255);
38  assert_false(pcmk__str_eq(s1, NULL, pcmk__str_none));
39  assert_in_range(pcmk__strcmp(NULL, s1, pcmk__str_none), -255, -1);
40 }
41 
42 static void
43 case_matters(void **state) {
44  const char *s1 = "abcd";
45  const char *s2 = "ABCD";
46 
47  assert_in_range(pcmk__strcmp(s1, s2, pcmk__str_none), 1, 255);
48  assert_false(pcmk__str_eq(s1, s2, pcmk__str_none));
49  assert_in_range(pcmk__strcmp(s2, s1, pcmk__str_none), -255, -1);
50 }
51 
52 static void
53 case_insensitive(void **state) {
54  const char *s1 = "abcd";
55  const char *s2 = "ABCD";
56 
57  assert_int_equal(pcmk__strcmp(s1, s2, pcmk__str_casei), 0);
58  assert_true(pcmk__str_eq(s1, s2, pcmk__str_casei));
59 }
60 
61 static void
62 regex(void **state) {
63  const char *s1 = "abcd";
64  const char *s2 = "ABCD";
65 
66  assert_int_equal(pcmk__strcmp(NULL, "a..d", pcmk__str_regex), 1);
67  assert_int_equal(pcmk__strcmp(s1, NULL, pcmk__str_regex), 1);
68  assert_int_equal(pcmk__strcmp(s1, "a..d", pcmk__str_regex), 0);
69  assert_true(pcmk__str_eq(s1, "a..d", pcmk__str_regex));
70  assert_int_not_equal(pcmk__strcmp(s1, "xxyy", pcmk__str_regex), 0);
71  assert_false(pcmk__str_eq(s1, "xxyy", pcmk__str_regex));
72  assert_int_equal(pcmk__strcmp(s2, "a..d", pcmk__str_regex|pcmk__str_casei), 0);
73  assert_true(pcmk__str_eq(s2, "a..d", pcmk__str_regex|pcmk__str_casei));
74  assert_int_not_equal(pcmk__strcmp(s2, "a..d", pcmk__str_regex), 0);
75  assert_false(pcmk__str_eq(s2, "a..d", pcmk__str_regex));
76  assert_int_equal(pcmk__strcmp(s2, "*ab", pcmk__str_regex), 1);
77 }
78 
79 int main(int argc, char **argv) {
80  const struct CMUnitTest tests[] = {
81  cmocka_unit_test(same_pointer),
82  cmocka_unit_test(one_is_null),
83  cmocka_unit_test(case_matters),
84  cmocka_unit_test(case_insensitive),
85  cmocka_unit_test(regex),
86  };
87 
88  cmocka_set_message_output(CM_OUTPUT_TAP);
89  return cmocka_run_group_tests(tests, NULL, NULL);
90 }
int main(int argc, char **argv)
int pcmk__strcmp(const char *s1, const char *s2, uint32_t flags)
Definition: strings.c:1101