aboutsummaryrefslogtreecommitdiffstats
path: root/util/string/subst_ut.cpp
blob: d55e4d890bef8267e07034dd5c5a951f2402df3e (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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
#include "join.h"
#include "subst.h"
#include <string>

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

Y_UNIT_TEST_SUITE(TStringSubst) {
    static const size_t MIN_FROM_CTX = 4;
    static const TVector<TString> ALL_FROM{TString("F"), TString("FF")};
    static const TVector<TString> ALL_TO{TString(""), TString("T"), TString("TT"), TString("TTT")};

    static void AssertSubstGlobal(const TString& sFrom, const TString& sTo, const TString& from, const TString& to, const size_t fromPos, const size_t numSubst) {
        TString s = sFrom;
        size_t res = SubstGlobal(s, from, to, fromPos);
        UNIT_ASSERT_VALUES_EQUAL_C(res, numSubst,
                                   TStringBuilder() << "numSubst=" << numSubst << ", fromPos=" << fromPos << ", " << sFrom << " -> " << sTo);
        if (numSubst) {
            UNIT_ASSERT_STRINGS_EQUAL_C(s, sTo,
                                        TStringBuilder() << "numSubst=" << numSubst << ", fromPos=" << fromPos << ", " << sFrom << " -> " << sTo);
        } else {
            // ensure s didn't trigger copy-on-write
            UNIT_ASSERT_VALUES_EQUAL_C(s.c_str(), sFrom.c_str(),
                                       TStringBuilder() << "numSubst=" << numSubst << ", fromPos=" << fromPos << ", " << sFrom << " -> " << sTo);
        }
    }

    Y_UNIT_TEST(TestSubstGlobalNoSubstA) {
        for (const auto& from : ALL_FROM) {
            const size_t fromSz = from.size();
            const size_t minSz = fromSz;
            const size_t maxSz = fromSz + MIN_FROM_CTX;
            for (size_t sz = minSz; sz <= maxSz; ++sz) {
                for (size_t fromPos = 0; fromPos < sz; ++fromPos) {
                    TString s{sz, '.'};
                    for (const auto& to : ALL_TO) {
                        AssertSubstGlobal(s, s, from, to, fromPos, 0);
                    }
                }
            }
        }
    }

    Y_UNIT_TEST(TestSubstGlobalNoSubstB) {
        for (const auto& from : ALL_FROM) {
            const size_t fromSz = from.size();
            const size_t minSz = fromSz;
            const size_t maxSz = fromSz + MIN_FROM_CTX;
            for (size_t sz = minSz; sz <= maxSz; ++sz) {
                for (size_t fromPos = 0; fromPos <= sz - fromSz; ++fromPos) {
                    for (size_t fromBeg = 0; fromBeg < fromPos; ++fromBeg) {
                        const auto parts = {
                            TString{fromBeg, '.'},
                            TString{sz - fromSz - fromBeg, '.'}};
                        TString s = JoinSeq(from, parts);
                        for (const auto& to : ALL_TO) {
                            AssertSubstGlobal(s, s, from, to, fromPos, 0);
                        }
                    }
                }
            }
        }
    }

    static void DoTestSubstGlobal(TVector<TString>& parts, const size_t minBeg, const size_t sz,
                                  const TString& from, const size_t fromPos, const size_t numSubst) {
        const size_t numLeft = numSubst - parts.size();
        for (size_t fromBeg = minBeg; fromBeg <= sz - numLeft * from.size(); ++fromBeg) {
            if (parts.empty()) {
                parts.emplace_back(fromBeg, '.');
            } else {
                parts.emplace_back(fromBeg - minBeg, '.');
            }

            if (numLeft == 1) {
                parts.emplace_back(sz - fromBeg - from.size(), '.');
                TString sFrom = JoinSeq(from, parts);
                UNIT_ASSERT_VALUES_EQUAL_C(sFrom.size(), sz, sFrom);
                for (const auto& to : ALL_TO) {
                    TString sTo = JoinSeq(to, parts);
                    AssertSubstGlobal(sFrom, sTo, from, to, fromPos, numSubst);
                }
                parts.pop_back();
            } else {
                DoTestSubstGlobal(parts, fromBeg + from.size(), sz, from, fromPos, numSubst);
            }

            parts.pop_back();
        }
    }

    static void DoTestSubstGlobal(size_t numSubst) {
        TVector<TString> parts;
        for (const auto& from : ALL_FROM) {
            const size_t fromSz = from.size();
            const size_t minSz = numSubst * fromSz;
            const size_t maxSz = numSubst * (fromSz + MIN_FROM_CTX);
            for (size_t sz = minSz; sz <= maxSz; ++sz) {
                const size_t maxPos = sz - numSubst * fromSz;
                for (size_t fromPos = 0; fromPos <= maxPos; ++fromPos) {
                    DoTestSubstGlobal(parts, fromPos, sz, from, fromPos, numSubst);
                }
            }
        }
    }

    Y_UNIT_TEST(TestSubstGlobalSubst1) {
        DoTestSubstGlobal(1);
    }

    Y_UNIT_TEST(TestSubstGlobalSubst2) {
        DoTestSubstGlobal(2);
    }

    Y_UNIT_TEST(TestSubstGlobalSubst3) {
        DoTestSubstGlobal(3);
    }

    Y_UNIT_TEST(TestSubstGlobalSubst4) {
        DoTestSubstGlobal(4);
    }

    Y_UNIT_TEST(TestSubstGlobalOld) {
        TString s;
        s = "aaa";
        SubstGlobal(s, "a", "bb");
        UNIT_ASSERT_EQUAL(s, TString("bbbbbb"));
        s = "aaa";
        SubstGlobal(s, "a", "b");
        UNIT_ASSERT_EQUAL(s, TString("bbb"));
        s = "aaa";
        SubstGlobal(s, "a", "");
        UNIT_ASSERT_EQUAL(s, TString(""));
        s = "abcdefbcbcdfb";
        SubstGlobal(s, "bc", "bbc", 2);
        UNIT_ASSERT_EQUAL(s, TString("abcdefbbcbbcdfb"));
        s = "Москва ~ Париж";
        SubstGlobal(s, " ~ ", " ");
        UNIT_ASSERT_EQUAL(s, TString("Москва Париж"));
    } 

    Y_UNIT_TEST(TestSubstGlobalOldRet) {
        const TString s1 = "aaa";
        const TString s2 = SubstGlobalCopy(s1, "a", "bb");
        UNIT_ASSERT_EQUAL(s2, TString("bbbbbb"));

        const TString s3 = "aaa";
        const TString s4 = SubstGlobalCopy(s3, "a", "b");
        UNIT_ASSERT_EQUAL(s4, TString("bbb"));

        const TString s5 = "aaa";
        const TString s6 = SubstGlobalCopy(s5, "a", "");
        UNIT_ASSERT_EQUAL(s6, TString(""));

        const TString s7 = "abcdefbcbcdfb";
        const TString s8 = SubstGlobalCopy(s7, "bc", "bbc", 2);
        UNIT_ASSERT_EQUAL(s8, TString("abcdefbbcbbcdfb"));

        const TString s9 = "Москва ~ Париж";
        const TString s10 = SubstGlobalCopy(s9, " ~ ", " ");
        UNIT_ASSERT_EQUAL(s10, TString("Москва Париж"));
    }

    Y_UNIT_TEST(TestSubstCharGlobal) {
        TUtf16String w = u"abcdabcd";
        SubstGlobal(w, wchar16('b'), wchar16('B'), 3);
        UNIT_ASSERT_EQUAL(w, u"abcdaBcd");
 
        TString s = "aaa";
        SubstGlobal(s, 'a', 'b', 1);
        UNIT_ASSERT_EQUAL(s, TString("abb"));
    }

    Y_UNIT_TEST(TestSubstCharGlobalRet) {
        const TUtf16String w1 = u"abcdabcd";
        const TUtf16String w2 = SubstGlobalCopy(w1, wchar16('b'), wchar16('B'), 3);
        UNIT_ASSERT_EQUAL(w2, u"abcdaBcd");

        const TString s1 = "aaa";
        const TString s2 = SubstGlobalCopy(s1, 'a', 'b', 1);
        UNIT_ASSERT_EQUAL(s2, TString("abb"));
    }

    Y_UNIT_TEST(TestSubstStdString) {
        std::string s = "aaa";
        SubstGlobal(s, "a", "b", 1);
        UNIT_ASSERT_EQUAL(s, "abb");
    }

    Y_UNIT_TEST(TestSubstStdStringRet) {
        const std::string s1 = "aaa";
        const std::string s2 = SubstGlobalCopy(s1, "a", "b", 1);
        UNIT_ASSERT_EQUAL(s2, "abb");
    }

    Y_UNIT_TEST(TestSubstGlobalChar) {
        {
            const TString s = "a";
            const TString st = "b";
            TString ss = s;
            UNIT_ASSERT_VALUES_EQUAL(s.size(), SubstGlobal(ss, 'a', 'b'));
            UNIT_ASSERT_VALUES_EQUAL(st, ss);
        }
        {
            const TString s = "aa";
            const TString st = "bb";
            TString ss = s;
            UNIT_ASSERT_VALUES_EQUAL(s.size(), SubstGlobal(ss, 'a', 'b'));
            UNIT_ASSERT_VALUES_EQUAL(st, ss);
        }
        {
            const TString s = "aaa";
            const TString st = "bbb";
            TString ss = s;
            UNIT_ASSERT_VALUES_EQUAL(s.size(), SubstGlobal(ss, 'a', 'b'));
            UNIT_ASSERT_VALUES_EQUAL(st, ss);
        }
        {
            const TString s = "aaaa";
            const TString st = "bbbb";
            TString ss = s;
            UNIT_ASSERT_VALUES_EQUAL(s.size(), SubstGlobal(ss, 'a', 'b'));
            UNIT_ASSERT_VALUES_EQUAL(st, ss);
        }
        {
            const TString s = "aaaaa";
            const TString st = "bbbbb";
            TString ss = s;
            UNIT_ASSERT_VALUES_EQUAL(s.size(), SubstGlobal(ss, 'a', 'b'));
            UNIT_ASSERT_VALUES_EQUAL(st, ss);
        }
        {
            const TString s = "aaaaaa";
            const TString st = "bbbbbb";
            TString ss = s;
            UNIT_ASSERT_VALUES_EQUAL(s.size(), SubstGlobal(ss, 'a', 'b'));
            UNIT_ASSERT_VALUES_EQUAL(st, ss);
        }
        {
            const TString s = "aaaaaaa";
            const TString st = "bbbbbbb";
            TString ss = s;
            UNIT_ASSERT_VALUES_EQUAL(s.size(), SubstGlobal(ss, 'a', 'b'));
            UNIT_ASSERT_VALUES_EQUAL(st, ss);
        }
        {
            const TString s = "aaaaaaaa";
            const TString st = "bbbbbbbb";
            TString ss = s;
            UNIT_ASSERT_VALUES_EQUAL(s.size(), SubstGlobal(ss, 'a', 'b'));
            UNIT_ASSERT_VALUES_EQUAL(st, ss);
        }
    }
}