pacemaker 2.1.1-77db578727
Scalable High-Availability cluster resource manager
Loading...
Searching...
No Matches
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 <stdio.h>
13#include <stdbool.h>
14#include <glib.h>
15
16static void
17same_pointer(void) {
18 const char *s1 = "abcd";
19 const char *s2 = "wxyz";
20
21 g_assert_cmpint(pcmk__strcmp(s1, s1, pcmk__str_none), ==, 0);
22 g_assert_true(pcmk__str_eq(s1, s1, pcmk__str_none));
23 g_assert_cmpint(pcmk__strcmp(s1, s2, pcmk__str_none), !=, 0);
24 g_assert_false(pcmk__str_eq(s1, s2, pcmk__str_none));
25 g_assert_cmpint(pcmk__strcmp(NULL, NULL, pcmk__str_none), ==, 0);
26}
27
28static void
29one_is_null(void) {
30 const char *s1 = "abcd";
31
32 g_assert_cmpint(pcmk__strcmp(s1, NULL, pcmk__str_null_matches), ==, 0);
33 g_assert_true(pcmk__str_eq(s1, NULL, pcmk__str_null_matches));
34 g_assert_cmpint(pcmk__strcmp(NULL, s1, pcmk__str_null_matches), ==, 0);
35 g_assert_cmpint(pcmk__strcmp(s1, NULL, pcmk__str_none), >, 0);
36 g_assert_false(pcmk__str_eq(s1, NULL, pcmk__str_none));
37 g_assert_cmpint(pcmk__strcmp(NULL, s1, pcmk__str_none), <, 0);
38}
39
40static void
41case_matters(void) {
42 const char *s1 = "abcd";
43 const char *s2 = "ABCD";
44
45 g_assert_cmpint(pcmk__strcmp(s1, s2, pcmk__str_none), >, 0);
46 g_assert_false(pcmk__str_eq(s1, s2, pcmk__str_none));
47 g_assert_cmpint(pcmk__strcmp(s2, s1, pcmk__str_none), <, 0);
48}
49
50static void
51case_insensitive(void) {
52 const char *s1 = "abcd";
53 const char *s2 = "ABCD";
54
55 g_assert_cmpint(pcmk__strcmp(s1, s2, pcmk__str_casei), ==, 0);
56 g_assert_true(pcmk__str_eq(s1, s2, pcmk__str_casei));
57}
58
59static void
60regex(void) {
61 const char *s1 = "abcd";
62 const char *s2 = "ABCD";
63
64 g_assert_cmpint(pcmk__strcmp(NULL, "a..d", pcmk__str_regex), ==, 1);
65 g_assert_cmpint(pcmk__strcmp(s1, NULL, pcmk__str_regex), ==, 1);
66 g_assert_cmpint(pcmk__strcmp(s1, "a..d", pcmk__str_regex), ==, 0);
67 g_assert_true(pcmk__str_eq(s1, "a..d", pcmk__str_regex));
68 g_assert_cmpint(pcmk__strcmp(s1, "xxyy", pcmk__str_regex), !=, 0);
69 g_assert_false(pcmk__str_eq(s1, "xxyy", pcmk__str_regex));
70 g_assert_cmpint(pcmk__strcmp(s2, "a..d", pcmk__str_regex|pcmk__str_casei), ==, 0);
71 g_assert_true(pcmk__str_eq(s2, "a..d", pcmk__str_regex|pcmk__str_casei));
72 g_assert_cmpint(pcmk__strcmp(s2, "a..d", pcmk__str_regex), !=, 0);
73 g_assert_false(pcmk__str_eq(s2, "a..d", pcmk__str_regex));
74 g_assert_cmpint(pcmk__strcmp(s2, "*ab", pcmk__str_regex), ==, 1);
75}
76
77int main(int argc, char **argv) {
78 g_test_init(&argc, &argv, NULL);
79
80 g_test_add_func("/common/strings/strcmp/same_pointer", same_pointer);
81 g_test_add_func("/common/strings/strcmp/one_is_null", one_is_null);
82 g_test_add_func("/common/strings/strcmp/case_matters", case_matters);
83 g_test_add_func("/common/strings/strcmp/case_insensitive", case_insensitive);
84 g_test_add_func("/common/strings/strcmp/regex", regex);
85
86 return g_test_run();
87}
int main(int argc, char **argv)
@ pcmk__str_regex
@ pcmk__str_none
@ pcmk__str_null_matches
@ pcmk__str_casei
int pcmk__strcmp(const char *s1, const char *s2, uint32_t flags)
Definition strings.c:1123