aboutsummaryrefslogtreecommitdiffstats
path: root/util/string/strip_ut.cpp
blob: df4f9bc57d3976963140001a6321ebb45c580653 (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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#include "strip.h"

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

#include <util/charset/wide.h>

Y_UNIT_TEST_SUITE(TStripStringTest) {
    Y_UNIT_TEST(TestStrip) {
        struct TTest {
            const char* Str;
            const char* StripLeftRes;
            const char* StripRightRes;
            const char* StripRes;
        };
        static const TTest tests[] = {
            {"  012  ", "012  ", "  012", "012"},
            {"  012", "012", "  012", "012"},
            {"012\t\t", "012\t\t", "012", "012"},
            {"\t012\t", "012\t", "\t012", "012"},
            {"012", "012", "012", "012"},
            {"012\r\n", "012\r\n", "012", "012"},
            {"\n012\r", "012\r", "\n012", "012"},
            {"\n \t\r", "", "", ""},
            {"", "", "", ""},
            {"abc", "abc", "abc", "abc"},
            {"a c", "a c", "a c", "a c"},
        };

        for (const auto& test : tests) {
            TString inputStr(test.Str);

            TString s;
            Strip(inputStr, s);
            UNIT_ASSERT_EQUAL(s, test.StripRes);

            UNIT_ASSERT_EQUAL(StripString(inputStr), test.StripRes);
            UNIT_ASSERT_EQUAL(StripStringLeft(inputStr), test.StripLeftRes);
            UNIT_ASSERT_EQUAL(StripStringRight(inputStr), test.StripRightRes);

            TStringBuf inputStrBuf(test.Str);
            UNIT_ASSERT_EQUAL(StripString(inputStrBuf), test.StripRes);
            UNIT_ASSERT_EQUAL(StripStringLeft(inputStrBuf), test.StripLeftRes);
            UNIT_ASSERT_EQUAL(StripStringRight(inputStrBuf), test.StripRightRes);
        };
    }

    Y_UNIT_TEST(TestCustomStrip) {
        struct TTest {
            const char* Str;
            const char* Result;
        };
        static const TTest tests[] = {
            {"//012//", "012"},
            {"//012", "012"},
            {"012", "012"},
            {"012//", "012"},
        };

        for (auto test : tests) {
            UNIT_ASSERT_EQUAL(
                StripString(TString(test.Str), EqualsStripAdapter('/')),
                test.Result);
        };
    }

    Y_UNIT_TEST(TestCustomStripLeftRight) {
        struct TTest {
            const char* Str;
            const char* ResultLeft;
            const char* ResultRight;
        };
        static const TTest tests[] = {
            {"//012//", "012//", "//012"},
            {"//012", "012", "//012"},
            {"012", "012", "012"},
            {"012//", "012//", "012"},
        };

        for (const auto& test : tests) {
            UNIT_ASSERT_EQUAL(
                StripStringLeft(TString(test.Str), EqualsStripAdapter('/')),
                test.ResultLeft);
            UNIT_ASSERT_EQUAL(
                StripStringRight(TString(test.Str), EqualsStripAdapter('/')),
                test.ResultRight);
        };
    }

    Y_UNIT_TEST(TestNullStringStrip) {
        TStringBuf nullString(nullptr, nullptr);
        UNIT_ASSERT_EQUAL(
            StripString(nullString),
            TString());
    }

    Y_UNIT_TEST(TestWtrokaStrip) {
        UNIT_ASSERT_EQUAL(StripString(TWtringBuf(u" abc ")), u"abc");
        UNIT_ASSERT_EQUAL(StripStringLeft(TWtringBuf(u" abc ")), u"abc ");
        UNIT_ASSERT_EQUAL(StripStringRight(TWtringBuf(u" abc ")), u" abc");
    }

    Y_UNIT_TEST(TestWtrokaCustomStrip) {
        UNIT_ASSERT_EQUAL(
            StripString(
                TWtringBuf(u"/abc/"),
                EqualsStripAdapter(u'/')),
            u"abc");
    }

    Y_UNIT_TEST(TestCollapseUtf32) {
        TUtf32String s;
        Collapse(UTF8ToUTF32<true>("  123    456  "), s, IsWhitespace);
        UNIT_ASSERT(s == UTF8ToUTF32<true>(" 123 456 "));
        Collapse(UTF8ToUTF32<true>("  123    456  "), s, IsWhitespace, 10);
        UNIT_ASSERT(s == UTF8ToUTF32<true>(" 123 456  "));

        s = UTF8ToUTF32<true>(" a b c ");
        TUtf32String s2 = s;
        CollapseInPlace(s2, IsWhitespace);

        UNIT_ASSERT(s == s2);
#ifndef TSTRING_IS_STD_STRING
        UNIT_ASSERT(s.c_str() == s2.c_str()); // Collapse() does not change the string at all
#endif
    }

    Y_UNIT_TEST(TestCollapse) {
        TString s;
        Collapse(TString("  123    456  "), s);
        UNIT_ASSERT(s == " 123 456 ");
        Collapse(TString("  123    456  "), s, 10);
        UNIT_ASSERT(s == " 123 456  ");

        s = TString(" a b c ");
        TString s2 = s;
        CollapseInPlace(s2);

        UNIT_ASSERT(s == s2);
#ifndef TSTRING_IS_STD_STRING
        UNIT_ASSERT(s.c_str() == s2.c_str()); // Collapse() does not change the string at all
#endif
    }

    Y_UNIT_TEST(TestCollapseText) {
        TString abs1("Very long description string written in unknown language.");
        TString abs2(abs1);
        TString abs3(abs1);
        CollapseText(abs1, 204);
        CollapseText(abs2, 54);
        CollapseText(abs3, 49);
        UNIT_ASSERT_EQUAL(abs1 == "Very long description string written in unknown language.", true);
        UNIT_ASSERT_EQUAL(abs2 == "Very long description string written in unknown ...", true);
        UNIT_ASSERT_EQUAL(abs3 == "Very long description string written in ...", true);
    }
}