aboutsummaryrefslogtreecommitdiffstats
path: root/util/string/strspn_ut.cpp
blob: 975374cba4e2dde2e5035f392f6a56c4937040fc (plain) (blame)
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include "strspn.h"

#include <library/cpp/testing/unittest/registar.h>

Y_UNIT_TEST_SUITE(TStrSpnTest) {
    Y_UNIT_TEST(FindFirstOf) {
        const TString s("some text!");

        UNIT_ASSERT_EQUAL(TCompactStrSpn("mos").FindFirstOf(s.begin(), s.end()), s.begin());
        UNIT_ASSERT_EQUAL(TCompactStrSpn("mos").FindFirstOf(s.c_str()), s.begin());

        UNIT_ASSERT_EQUAL(TCompactStrSpn("xt").FindFirstOf(s.begin(), s.end()), s.begin() + 5);
        UNIT_ASSERT_EQUAL(TCompactStrSpn("xt").FindFirstOf(s.c_str()), s.begin() + 5);

        UNIT_ASSERT_EQUAL(TCompactStrSpn(".?!").FindFirstOf(s.begin(), s.end()), s.end() - 1);
        UNIT_ASSERT_EQUAL(TCompactStrSpn(".?!").FindFirstOf(s.c_str()), s.end() - 1);

        UNIT_ASSERT_EQUAL(TCompactStrSpn("abc").FindFirstOf(s.begin(), s.end()), s.end());
        UNIT_ASSERT_EQUAL(TCompactStrSpn("abc").FindFirstOf(s.c_str()), s.end());

        // Must be const. If not, non-const begin() will clone the shared empty string
        // and the next assertion will possibly use invalidated end iterator.
        const TString empty;

        UNIT_ASSERT_EQUAL(TCompactStrSpn("abc").FindFirstOf(empty.begin(), empty.end()), empty.end());
        UNIT_ASSERT_EQUAL(TCompactStrSpn("abc").FindFirstOf(empty.c_str()), empty.end());
    }

    Y_UNIT_TEST(FindFirstNotOf) {
        const TString s("abacabaxyz");

        UNIT_ASSERT_EQUAL(TCompactStrSpn("123").FindFirstNotOf(s.begin(), s.end()), s.begin());
        UNIT_ASSERT_EQUAL(TCompactStrSpn("123").FindFirstNotOf(s.c_str()), s.begin());

        UNIT_ASSERT_EQUAL(TCompactStrSpn("abc").FindFirstNotOf(s.begin(), s.end()), s.begin() + 7);
        UNIT_ASSERT_EQUAL(TCompactStrSpn("abc").FindFirstNotOf(s.c_str()), s.begin() + 7);

        UNIT_ASSERT_EQUAL(TCompactStrSpn("abcxy").FindFirstNotOf(s.begin(), s.end()), s.end() - 1);
        UNIT_ASSERT_EQUAL(TCompactStrSpn("abcxy").FindFirstNotOf(s.c_str()), s.end() - 1);

        UNIT_ASSERT_EQUAL(TCompactStrSpn("abcxyz").FindFirstNotOf(s.begin(), s.end()), s.end());
        UNIT_ASSERT_EQUAL(TCompactStrSpn("abcxyz").FindFirstNotOf(s.c_str()), s.end());

        const TString empty;

        UNIT_ASSERT_EQUAL(TCompactStrSpn("abc").FindFirstNotOf(empty.begin(), empty.end()), empty.end());
        UNIT_ASSERT_EQUAL(TCompactStrSpn("abc").FindFirstNotOf(empty.c_str()), empty.end());
    }

    Y_UNIT_TEST(FindFirstOfReverse) {
        TStringBuf s("some text");

        UNIT_ASSERT_EQUAL(TCompactStrSpn("xt").FindFirstOf(s.rbegin(), s.rend()), s.rbegin());
        UNIT_ASSERT_EQUAL(TCompactStrSpn("mos").FindFirstOf(s.rbegin(), s.rend()), s.rend() - 3);
        UNIT_ASSERT_EQUAL(TCompactStrSpn("s").FindFirstOf(s.rbegin(), s.rend()), s.rend() - 1);
        UNIT_ASSERT_EQUAL(TCompactStrSpn("abc").FindFirstOf(s.rbegin(), s.rend()), s.rend());

        TStringBuf empty;
        UNIT_ASSERT_EQUAL(TCompactStrSpn("abc").FindFirstOf(empty.rbegin(), empty.rend()), empty.rend());
    }

    Y_UNIT_TEST(FindFirstNotOfReverse) {
        TStringBuf s("_abacabaxyz");

        UNIT_ASSERT_EQUAL(TCompactStrSpn("abc").FindFirstNotOf(s.rbegin(), s.rend()), s.rbegin());
        UNIT_ASSERT_EQUAL(TCompactStrSpn("xyz").FindFirstNotOf(s.rbegin(), s.rend()), s.rbegin() + 3);
        UNIT_ASSERT_EQUAL(TCompactStrSpn("abcxyz").FindFirstNotOf(s.rbegin(), s.rend()), s.rend() - 1);
        UNIT_ASSERT_EQUAL(TCompactStrSpn("abcxyz_").FindFirstNotOf(s.rbegin(), s.rend()), s.rend());

        TStringBuf empty;
        UNIT_ASSERT_EQUAL(TCompactStrSpn("abc").FindFirstNotOf(empty.rbegin(), empty.rend()), empty.rend());
    }
}