File: testutil.cpp

package info (click to toggle)
icu 78.2-1
  • links: PTS
  • area: main
  • in suites: experimental
  • size: 123,992 kB
  • sloc: cpp: 527,891; ansic: 112,789; sh: 4,983; makefile: 4,657; perl: 3,199; python: 2,933; xml: 749; sed: 36; lisp: 12
file content (303 lines) | stat: -rw-r--r-- 12,509 bytes parent folder | download | duplicates (9)
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
// © 2016 and later: Unicode, Inc. and others.
// License & terms of use: http://www.unicode.org/copyright.html
/*
**********************************************************************
*   Copyright (C) 2001-2009, International Business Machines
*   Corporation and others.  All Rights Reserved.
**********************************************************************
*   Date        Name        Description
*   05/23/00    aliu        Creation.
**********************************************************************
*/

#include <algorithm>
#include <vector>
#include "unicode/utypes.h"
#include "unicode/edits.h"
#include "unicode/unistr.h"
#include "unicode/utf16.h"
#include "cmemory.h"
#include "testutil.h"
#include "intltest.h"

static const char16_t HEX[] = u"0123456789ABCDEF";

UnicodeString &TestUtility::appendHex(UnicodeString &buf, UChar32 ch) {
    if (ch >= 0x10000) {
        if (ch >= 0x100000) {
            buf.append(HEX[0xF&(ch>>20)]);
        }
        buf.append(HEX[0xF&(ch>>16)]);
    }
    buf.append(HEX[0xF&(ch>>12)]);
    buf.append(HEX[0xF&(ch>>8)]);
    buf.append(HEX[0xF&(ch>>4)]);
    buf.append(HEX[0xF&ch]);
    return buf;
}

UnicodeString TestUtility::hex(UChar32 ch) {
    UnicodeString buf;
    appendHex(buf, ch);
    return buf;
}

UnicodeString TestUtility::hex(const UnicodeString& s) {
    return hex(s, u',');
}

UnicodeString TestUtility::hex(const UnicodeString& s, char16_t sep) {
    UnicodeString result;
    if (s.isEmpty()) return result;
    UChar32 c;
    for (int32_t i = 0; i < s.length(); i += U16_LENGTH(c)) {
        c = s.char32At(i);
        if (i > 0) {
            result.append(sep);
        }
        appendHex(result, c);
    }
    return result;
}

UnicodeString TestUtility::hex(const uint8_t* bytes, int32_t len) {
    UnicodeString buf;
    for (int32_t i = 0; i < len; ++i) {
        buf.append(HEX[0x0F & (bytes[i] >> 4)]);
        buf.append(HEX[0x0F & bytes[i]]);
    }
    return buf;
}

namespace {

UnicodeString printOneEdit(const Edits::Iterator &ei) {
    if (ei.hasChange()) {
        return UnicodeString() + ei.oldLength() + u"->" + ei.newLength();
    } else {
        return UnicodeString() + ei.oldLength() + u"=" + ei.newLength();
    }
}

/**
 * Maps indexes according to the expected edits.
 * A destination index can occur multiple times when there are source deletions.
 * Map according to the last occurrence, normally in a non-empty destination span.
 * Simplest is to search from the back.
 */
int32_t srcIndexFromDest(const EditChange expected[], int32_t expLength,
                         int32_t srcLength, int32_t destLength, int32_t index) {
    int32_t srcIndex = srcLength;
    int32_t destIndex = destLength;
    int32_t i = expLength;
    while (index < destIndex && i > 0) {
        --i;
        int32_t prevSrcIndex = srcIndex - expected[i].oldLength;
        int32_t prevDestIndex = destIndex - expected[i].newLength;
        if (index == prevDestIndex) {
            return prevSrcIndex;
        } else if (index > prevDestIndex) {
            if (expected[i].change) {
                // In a change span, map to its end.
                return srcIndex;
            } else {
                // In an unchanged span, offset within it.
                return prevSrcIndex + (index - prevDestIndex);
            }
        }
        srcIndex = prevSrcIndex;
        destIndex = prevDestIndex;
    }
    // index is outside the string.
    return srcIndex;
}

int32_t destIndexFromSrc(const EditChange expected[], int32_t expLength,
                         int32_t srcLength, int32_t destLength, int32_t index) {
    int32_t srcIndex = srcLength;
    int32_t destIndex = destLength;
    int32_t i = expLength;
    while (index < srcIndex && i > 0) {
        --i;
        int32_t prevSrcIndex = srcIndex - expected[i].oldLength;
        int32_t prevDestIndex = destIndex - expected[i].newLength;
        if (index == prevSrcIndex) {
            return prevDestIndex;
        } else if (index > prevSrcIndex) {
            if (expected[i].change) {
                // In a change span, map to its end.
                return destIndex;
            } else {
                // In an unchanged span, offset within it.
                return prevDestIndex + (index - prevSrcIndex);
            }
        }
        srcIndex = prevSrcIndex;
        destIndex = prevDestIndex;
    }
    // index is outside the string.
    return destIndex;
}

}  // namespace

// For debugging, set -v to see matching edits up to a failure.
UBool TestUtility::checkEqualEdits(IntlTest &test, const UnicodeString &name,
                                   const Edits &e1, const Edits &e2, UErrorCode &errorCode) {
    Edits::Iterator ei1 = e1.getFineIterator();
    Edits::Iterator ei2 = e2.getFineIterator();
    UBool ok = true;
    for (int32_t i = 0; ok; ++i) {
        UBool ei1HasNext = ei1.next(errorCode);
        UBool ei2HasNext = ei2.next(errorCode);
        ok &= test.assertEquals(name + u" next()[" + i + u"]" + __LINE__,
                                ei1HasNext, ei2HasNext);
        ok &= test.assertSuccess(name + u" errorCode[" + i + u"]" + __LINE__, errorCode);
        ok &= test.assertEquals(name + u" edit[" + i + u"]" + __LINE__,
                                printOneEdit(ei1), printOneEdit(ei2));
        if (!ei1HasNext || !ei2HasNext) {
            break;
        }
        test.logln();
    }
    return ok;
}

void TestUtility::checkEditsIter(
        IntlTest &test,
        const UnicodeString &name,
        Edits::Iterator ei1, Edits::Iterator ei2,  // two equal iterators
        const EditChange expected[], int32_t expLength, UBool withUnchanged,
        UErrorCode &errorCode) {
    test.assertFalse(name + u":" + __LINE__, ei2.findSourceIndex(-1, errorCode));
    test.assertFalse(name + u":" + __LINE__, ei2.findDestinationIndex(-1, errorCode));

    int32_t expSrcIndex = 0;
    int32_t expDestIndex = 0;
    int32_t expReplIndex = 0;
    for (int32_t expIndex = 0; expIndex < expLength; ++expIndex) {
        const EditChange &expect = expected[expIndex];
        UnicodeString msg = UnicodeString(name).append(u' ') + expIndex;
        if (withUnchanged || expect.change) {
            test.assertTrue(msg + u":" + __LINE__, ei1.next(errorCode));
            test.assertEquals(msg + u":" + __LINE__, expect.change, ei1.hasChange());
            test.assertEquals(msg + u":" + __LINE__, expect.oldLength, ei1.oldLength());
            test.assertEquals(msg + u":" + __LINE__, expect.newLength, ei1.newLength());
            test.assertEquals(msg + u":" + __LINE__, expSrcIndex, ei1.sourceIndex());
            test.assertEquals(msg + u":" + __LINE__, expDestIndex, ei1.destinationIndex());
            test.assertEquals(msg + u":" + __LINE__, expReplIndex, ei1.replacementIndex());
        }

        if (expect.oldLength > 0) {
            test.assertTrue(msg + u":" + __LINE__, ei2.findSourceIndex(expSrcIndex, errorCode));
            test.assertEquals(msg + u":" + __LINE__, expect.change, ei2.hasChange());
            test.assertEquals(msg + u":" + __LINE__, expect.oldLength, ei2.oldLength());
            test.assertEquals(msg + u":" + __LINE__, expect.newLength, ei2.newLength());
            test.assertEquals(msg + u":" + __LINE__, expSrcIndex, ei2.sourceIndex());
            test.assertEquals(msg + u":" + __LINE__, expDestIndex, ei2.destinationIndex());
            test.assertEquals(msg + u":" + __LINE__, expReplIndex, ei2.replacementIndex());
            if (!withUnchanged) {
                // For some iterators, move past the current range
                // so that findSourceIndex() has to look before the current index.
                ei2.next(errorCode);
                ei2.next(errorCode);
            }
        }

        if (expect.newLength > 0) {
            test.assertTrue(msg + u":" + __LINE__, ei2.findDestinationIndex(expDestIndex, errorCode));
            test.assertEquals(msg + u":" + __LINE__, expect.change, ei2.hasChange());
            test.assertEquals(msg + u":" + __LINE__, expect.oldLength, ei2.oldLength());
            test.assertEquals(msg + u":" + __LINE__, expect.newLength, ei2.newLength());
            test.assertEquals(msg + u":" + __LINE__, expSrcIndex, ei2.sourceIndex());
            test.assertEquals(msg + u":" + __LINE__, expDestIndex, ei2.destinationIndex());
            test.assertEquals(msg + u":" + __LINE__, expReplIndex, ei2.replacementIndex());
            if (!withUnchanged) {
                // For some iterators, move past the current range
                // so that findSourceIndex() has to look before the current index.
                ei2.next(errorCode);
                ei2.next(errorCode);
            }
        }

        expSrcIndex += expect.oldLength;
        expDestIndex += expect.newLength;
        if (expect.change) {
            expReplIndex += expect.newLength;
        }
    }
    UnicodeString msg = UnicodeString(name).append(u" end");
    test.assertFalse(msg + u":" + __LINE__, ei1.next(errorCode));
    test.assertFalse(msg + u":" + __LINE__, ei1.hasChange());
    test.assertEquals(msg + u":" + __LINE__, 0, ei1.oldLength());
    test.assertEquals(msg + u":" + __LINE__, 0, ei1.newLength());
    test.assertEquals(msg + u":" + __LINE__, expSrcIndex, ei1.sourceIndex());
    test.assertEquals(msg + u":" + __LINE__, expDestIndex, ei1.destinationIndex());
    test.assertEquals(msg + u":" + __LINE__, expReplIndex, ei1.replacementIndex());

    test.assertFalse(name + u":" + __LINE__, ei2.findSourceIndex(expSrcIndex, errorCode));
    test.assertFalse(name + u":" + __LINE__, ei2.findDestinationIndex(expDestIndex, errorCode));

    // Check mapping of all indexes against a simple implementation
    // that works on the expected changes.
    // Iterate once forward, once backward, to cover more runtime conditions.
    int32_t srcLength = expSrcIndex;
    int32_t destLength = expDestIndex;
    std::vector<int32_t> srcIndexes;
    std::vector<int32_t> destIndexes;
    srcIndexes.push_back(-1);
    destIndexes.push_back(-1);
    int32_t srcIndex = 0;
    int32_t destIndex = 0;
    for (int32_t i = 0; i < expLength; ++i) {
        if (expected[i].oldLength > 0) {
            srcIndexes.push_back(srcIndex);
            if (expected[i].oldLength > 1) {
                srcIndexes.push_back(srcIndex + 1);
                if (expected[i].oldLength > 2) {
                    srcIndexes.push_back(srcIndex + expected[i].oldLength - 1);
                }
            }
        }
        if (expected[i].newLength > 0) {
            destIndexes.push_back(destIndex);
            if (expected[i].newLength > 1) {
                destIndexes.push_back(destIndex + 1);
                if (expected[i].newLength > 2) {
                    destIndexes.push_back(destIndex + expected[i].newLength - 1);
                }
            }
        }
        srcIndex += expected[i].oldLength;
        destIndex += expected[i].newLength;
    }
    srcIndexes.push_back(srcLength);
    destIndexes.push_back(destLength);
    srcIndexes.push_back(srcLength + 1);
    destIndexes.push_back(destLength + 1);
    std::reverse(destIndexes.begin(), destIndexes.end());
    // Zig-zag across the indexes to stress next() <-> previous().
    static const int32_t ZIG_ZAG[] = { 0, 1, 2, 3, 2, 1 };
    for (auto i = 0; i < static_cast<int32_t>(srcIndexes.size()); ++i) {
        for (int32_t ij = 0; ij < UPRV_LENGTHOF(ZIG_ZAG); ++ij) {
            int32_t j = ZIG_ZAG[ij];
            if ((i + j) < static_cast<int32_t>(srcIndexes.size())) {
                int32_t si = srcIndexes[i + j];
                test.assertEquals(name + u" destIndexFromSrc(" + si + u"):" + __LINE__,
                                  destIndexFromSrc(expected, expLength, srcLength, destLength, si),
                                  ei2.destinationIndexFromSourceIndex(si, errorCode));
            }
        }
    }
    for (auto i = 0; i < static_cast<int32_t>(destIndexes.size()); ++i) {
        for (int32_t ij = 0; ij < UPRV_LENGTHOF(ZIG_ZAG); ++ij) {
            int32_t j = ZIG_ZAG[ij];
            if ((i + j) < static_cast<int32_t>(destIndexes.size())) {
                int32_t di = destIndexes[i + j];
                test.assertEquals(name + u" srcIndexFromDest(" + di + u"):" + __LINE__,
                                  srcIndexFromDest(expected, expLength, srcLength, destLength, di),
                                  ei2.sourceIndexFromDestinationIndex(di, errorCode));
            }
        }
    }
}