summaryrefslogtreecommitdiffstats
path: root/lib/common/tests/strings/pcmk__ends_with_test.c
blob: 75035714fabf0c81404feb98897cf4992858b171 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/*
 * Copyright 2021 the Pacemaker project contributors
 *
 * The version control history for this file may have further details.
 *
 * This source code is licensed under the GNU General Public License version 2
 * or later (GPLv2+) WITHOUT ANY WARRANTY.
 */

#include <crm_internal.h>

#include <crm/common/unittest_internal.h>

static void
bad_input(void **state) {
    assert_false(pcmk__ends_with(NULL, "xyz"));

    assert_true(pcmk__ends_with(NULL, NULL));
    assert_true(pcmk__ends_with(NULL, ""));
    assert_true(pcmk__ends_with("", NULL));
    assert_true(pcmk__ends_with("", ""));
    assert_true(pcmk__ends_with("abc", NULL));
    assert_true(pcmk__ends_with("abc", ""));
}

static void
ends_with(void **state) {
    assert_true(pcmk__ends_with("abc", "abc"));
    assert_true(pcmk__ends_with("abc", "bc"));
    assert_true(pcmk__ends_with("abc", "c"));
    assert_true(pcmk__ends_with("abcbc", "bc"));

    assert_false(pcmk__ends_with("abc", "def"));
    assert_false(pcmk__ends_with("abc", "defg"));
    assert_false(pcmk__ends_with("abc", "bcd"));
    assert_false(pcmk__ends_with("abc", "ab"));

    assert_false(pcmk__ends_with("abc", "BC"));
}

static void
ends_with_ext(void **state) {
    assert_true(pcmk__ends_with_ext("ab.c", ".c"));
    assert_true(pcmk__ends_with_ext("ab.cb.c", ".c"));

    assert_false(pcmk__ends_with_ext("ab.c", ".def"));
    assert_false(pcmk__ends_with_ext("ab.c", ".defg"));
    assert_false(pcmk__ends_with_ext("ab.c", ".cd"));
    assert_false(pcmk__ends_with_ext("ab.c", "ab"));

    assert_false(pcmk__ends_with_ext("ab.c", ".C"));
}

PCMK__UNIT_TEST(NULL, NULL,
                cmocka_unit_test(bad_input),
                cmocka_unit_test(ends_with),
                cmocka_unit_test(ends_with_ext))