12e5b6d6dSopenharmony_ci// © 2016 and later: Unicode, Inc. and others.
22e5b6d6dSopenharmony_ci// License & terms of use: http://www.unicode.org/copyright.html
32e5b6d6dSopenharmony_ci/********************************************************************
42e5b6d6dSopenharmony_ci * COPYRIGHT:
52e5b6d6dSopenharmony_ci * Copyright (c) 2005-2016, International Business Machines Corporation and
62e5b6d6dSopenharmony_ci * others. All Rights Reserved.
72e5b6d6dSopenharmony_ci ********************************************************************/
82e5b6d6dSopenharmony_ci/************************************************************************
92e5b6d6dSopenharmony_ci*   Tests for the UText and UTextIterator text abstraction classes
102e5b6d6dSopenharmony_ci*
112e5b6d6dSopenharmony_ci************************************************************************/
122e5b6d6dSopenharmony_ci
132e5b6d6dSopenharmony_ci#include <string.h>
142e5b6d6dSopenharmony_ci#include <stdio.h>
152e5b6d6dSopenharmony_ci#include <stdlib.h>
162e5b6d6dSopenharmony_ci#include "unicode/utypes.h"
172e5b6d6dSopenharmony_ci#include "unicode/utext.h"
182e5b6d6dSopenharmony_ci#include "unicode/utf8.h"
192e5b6d6dSopenharmony_ci#include "unicode/utf16.h"
202e5b6d6dSopenharmony_ci#include "unicode/ustring.h"
212e5b6d6dSopenharmony_ci#include "unicode/uchriter.h"
222e5b6d6dSopenharmony_ci#include "cmemory.h"
232e5b6d6dSopenharmony_ci#include "cstr.h"
242e5b6d6dSopenharmony_ci#include "utxttest.h"
252e5b6d6dSopenharmony_ci
262e5b6d6dSopenharmony_cistatic UBool  gFailed = false;
272e5b6d6dSopenharmony_cistatic int    gTestNum = 0;
282e5b6d6dSopenharmony_ci
292e5b6d6dSopenharmony_ci// Forward decl
302e5b6d6dSopenharmony_ciUText *openFragmentedUnicodeString(UText *ut, UnicodeString *s, UErrorCode *status);
312e5b6d6dSopenharmony_ci
322e5b6d6dSopenharmony_ci#define TEST_ASSERT(x) UPRV_BLOCK_MACRO_BEGIN { \
332e5b6d6dSopenharmony_ci    if ((x)==false) { \
342e5b6d6dSopenharmony_ci        errln("Test #%d failure in file %s at line %d\n", gTestNum, __FILE__, __LINE__); \
352e5b6d6dSopenharmony_ci        gFailed = true; \
362e5b6d6dSopenharmony_ci    } \
372e5b6d6dSopenharmony_ci} UPRV_BLOCK_MACRO_END
382e5b6d6dSopenharmony_ci
392e5b6d6dSopenharmony_ci
402e5b6d6dSopenharmony_ci#define TEST_SUCCESS(status) UPRV_BLOCK_MACRO_BEGIN { \
412e5b6d6dSopenharmony_ci    if (U_FAILURE(status)) { \
422e5b6d6dSopenharmony_ci        errln("Test #%d failure in file %s at line %d. Error = \"%s\"\n", \
432e5b6d6dSopenharmony_ci              gTestNum, __FILE__, __LINE__, u_errorName(status)); \
442e5b6d6dSopenharmony_ci        gFailed = true; \
452e5b6d6dSopenharmony_ci    } \
462e5b6d6dSopenharmony_ci} UPRV_BLOCK_MACRO_END
472e5b6d6dSopenharmony_ci
482e5b6d6dSopenharmony_ciUTextTest::UTextTest() {
492e5b6d6dSopenharmony_ci}
502e5b6d6dSopenharmony_ci
512e5b6d6dSopenharmony_ciUTextTest::~UTextTest() {
522e5b6d6dSopenharmony_ci}
532e5b6d6dSopenharmony_ci
542e5b6d6dSopenharmony_ci
552e5b6d6dSopenharmony_civoid
562e5b6d6dSopenharmony_ciUTextTest::runIndexedTest(int32_t index, UBool exec,
572e5b6d6dSopenharmony_ci                          const char* &name, char* /*par*/) {
582e5b6d6dSopenharmony_ci    TESTCASE_AUTO_BEGIN;
592e5b6d6dSopenharmony_ci    TESTCASE_AUTO(TextTest);
602e5b6d6dSopenharmony_ci    TESTCASE_AUTO(ErrorTest);
612e5b6d6dSopenharmony_ci    TESTCASE_AUTO(FreezeTest);
622e5b6d6dSopenharmony_ci    TESTCASE_AUTO(Ticket5560);
632e5b6d6dSopenharmony_ci    TESTCASE_AUTO(Ticket6847);
642e5b6d6dSopenharmony_ci    TESTCASE_AUTO(Ticket10562);
652e5b6d6dSopenharmony_ci    TESTCASE_AUTO(Ticket10983);
662e5b6d6dSopenharmony_ci    TESTCASE_AUTO(Ticket12130);
672e5b6d6dSopenharmony_ci    TESTCASE_AUTO(Ticket13344);
682e5b6d6dSopenharmony_ci    TESTCASE_AUTO_END;
692e5b6d6dSopenharmony_ci}
702e5b6d6dSopenharmony_ci
712e5b6d6dSopenharmony_ci//
722e5b6d6dSopenharmony_ci// Quick and dirty random number generator.
732e5b6d6dSopenharmony_ci//   (don't use library so that results are portable.
742e5b6d6dSopenharmony_cistatic uint32_t m_seed = 1;
752e5b6d6dSopenharmony_cistatic uint32_t m_rand()
762e5b6d6dSopenharmony_ci{
772e5b6d6dSopenharmony_ci    m_seed = m_seed * 1103515245 + 12345;
782e5b6d6dSopenharmony_ci    return (uint32_t)(m_seed/65536) % 32768;
792e5b6d6dSopenharmony_ci}
802e5b6d6dSopenharmony_ci
812e5b6d6dSopenharmony_ci
822e5b6d6dSopenharmony_ci//
832e5b6d6dSopenharmony_ci//   TextTest()
842e5b6d6dSopenharmony_ci//
852e5b6d6dSopenharmony_ci//       Top Level function for UText testing.
862e5b6d6dSopenharmony_ci//       Specifies the strings to be tested, with the actual testing itself
872e5b6d6dSopenharmony_ci//       being carried out in another function, TestString().
882e5b6d6dSopenharmony_ci//
892e5b6d6dSopenharmony_civoid  UTextTest::TextTest() {
902e5b6d6dSopenharmony_ci    int32_t i, j;
912e5b6d6dSopenharmony_ci
922e5b6d6dSopenharmony_ci    TestString("abcd\\U00010001xyz");
932e5b6d6dSopenharmony_ci    TestString("");
942e5b6d6dSopenharmony_ci
952e5b6d6dSopenharmony_ci    // Supplementary chars at start or end
962e5b6d6dSopenharmony_ci    TestString("\\U00010001");
972e5b6d6dSopenharmony_ci    TestString("abc\\U00010001");
982e5b6d6dSopenharmony_ci    TestString("\\U00010001abc");
992e5b6d6dSopenharmony_ci
1002e5b6d6dSopenharmony_ci    // Test simple strings of lengths 1 to 60, looking for glitches at buffer boundaries
1012e5b6d6dSopenharmony_ci    UnicodeString s;
1022e5b6d6dSopenharmony_ci    for (i=1; i<60; i++) {
1032e5b6d6dSopenharmony_ci        s.truncate(0);
1042e5b6d6dSopenharmony_ci        for (j=0; j<i; j++) {
1052e5b6d6dSopenharmony_ci            if (j+0x30 == 0x5c) {
1062e5b6d6dSopenharmony_ci                // backslash.  Needs to be escaped
1072e5b6d6dSopenharmony_ci                s.append((UChar)0x5c);
1082e5b6d6dSopenharmony_ci            }
1092e5b6d6dSopenharmony_ci            s.append(UChar(j+0x30));
1102e5b6d6dSopenharmony_ci        }
1112e5b6d6dSopenharmony_ci        TestString(s);
1122e5b6d6dSopenharmony_ci    }
1132e5b6d6dSopenharmony_ci
1142e5b6d6dSopenharmony_ci   // Test strings with odd-aligned supplementary chars,
1152e5b6d6dSopenharmony_ci   //    looking for glitches at buffer boundaries
1162e5b6d6dSopenharmony_ci    for (i=1; i<60; i++) {
1172e5b6d6dSopenharmony_ci        s.truncate(0);
1182e5b6d6dSopenharmony_ci        s.append((UChar)0x41);
1192e5b6d6dSopenharmony_ci        for (j=0; j<i; j++) {
1202e5b6d6dSopenharmony_ci            s.append(UChar32(j+0x11000));
1212e5b6d6dSopenharmony_ci        }
1222e5b6d6dSopenharmony_ci        TestString(s);
1232e5b6d6dSopenharmony_ci    }
1242e5b6d6dSopenharmony_ci
1252e5b6d6dSopenharmony_ci    // String of chars of randomly varying size in utf-8 representation.
1262e5b6d6dSopenharmony_ci    //   Exercise the mapping, and the varying sized buffer.
1272e5b6d6dSopenharmony_ci    //
1282e5b6d6dSopenharmony_ci    s.truncate(0);
1292e5b6d6dSopenharmony_ci    UChar32  c1 = 0;
1302e5b6d6dSopenharmony_ci    UChar32  c2 = 0x100;
1312e5b6d6dSopenharmony_ci    UChar32  c3 = 0xa000;
1322e5b6d6dSopenharmony_ci    UChar32  c4 = 0x11000;
1332e5b6d6dSopenharmony_ci    for (i=0; i<1000; i++) {
1342e5b6d6dSopenharmony_ci        int len8 = m_rand()%4 + 1;
1352e5b6d6dSopenharmony_ci        switch (len8) {
1362e5b6d6dSopenharmony_ci            case 1:
1372e5b6d6dSopenharmony_ci                c1 = (c1+1)%0x80;
1382e5b6d6dSopenharmony_ci                // don't put 0 into string (0 terminated strings for some tests)
1392e5b6d6dSopenharmony_ci                // don't put '\', will cause unescape() to fail.
1402e5b6d6dSopenharmony_ci                if (c1==0x5c || c1==0) {
1412e5b6d6dSopenharmony_ci                    c1++;
1422e5b6d6dSopenharmony_ci                }
1432e5b6d6dSopenharmony_ci                s.append(c1);
1442e5b6d6dSopenharmony_ci                break;
1452e5b6d6dSopenharmony_ci            case 2:
1462e5b6d6dSopenharmony_ci                s.append(c2++);
1472e5b6d6dSopenharmony_ci                break;
1482e5b6d6dSopenharmony_ci            case 3:
1492e5b6d6dSopenharmony_ci                s.append(c3++);
1502e5b6d6dSopenharmony_ci                break;
1512e5b6d6dSopenharmony_ci            case 4:
1522e5b6d6dSopenharmony_ci                s.append(c4++);
1532e5b6d6dSopenharmony_ci                break;
1542e5b6d6dSopenharmony_ci        }
1552e5b6d6dSopenharmony_ci    }
1562e5b6d6dSopenharmony_ci    TestString(s);
1572e5b6d6dSopenharmony_ci}
1582e5b6d6dSopenharmony_ci
1592e5b6d6dSopenharmony_ci
1602e5b6d6dSopenharmony_ci//
1612e5b6d6dSopenharmony_ci//  TestString()     Run a suite of UText tests on a string.
1622e5b6d6dSopenharmony_ci//                   The test string is unescaped before use.
1632e5b6d6dSopenharmony_ci//
1642e5b6d6dSopenharmony_civoid UTextTest::TestString(const UnicodeString &s) {
1652e5b6d6dSopenharmony_ci    int32_t       i;
1662e5b6d6dSopenharmony_ci    int32_t       j;
1672e5b6d6dSopenharmony_ci    UChar32       c;
1682e5b6d6dSopenharmony_ci    int32_t       cpCount = 0;
1692e5b6d6dSopenharmony_ci    UErrorCode    status  = U_ZERO_ERROR;
1702e5b6d6dSopenharmony_ci    UText        *ut      = NULL;
1712e5b6d6dSopenharmony_ci    int32_t       saLen;
1722e5b6d6dSopenharmony_ci
1732e5b6d6dSopenharmony_ci    UnicodeString sa = s.unescape();
1742e5b6d6dSopenharmony_ci    saLen = sa.length();
1752e5b6d6dSopenharmony_ci
1762e5b6d6dSopenharmony_ci    //
1772e5b6d6dSopenharmony_ci    // Build up a mapping between code points and UTF-16 code unit indexes.
1782e5b6d6dSopenharmony_ci    //
1792e5b6d6dSopenharmony_ci    m *cpMap = new m[sa.length() + 1];
1802e5b6d6dSopenharmony_ci    j = 0;
1812e5b6d6dSopenharmony_ci    for (i=0; i<sa.length(); i=sa.moveIndex32(i, 1)) {
1822e5b6d6dSopenharmony_ci        c = sa.char32At(i);
1832e5b6d6dSopenharmony_ci        cpMap[j].nativeIdx = i;
1842e5b6d6dSopenharmony_ci        cpMap[j].cp = c;
1852e5b6d6dSopenharmony_ci        j++;
1862e5b6d6dSopenharmony_ci        cpCount++;
1872e5b6d6dSopenharmony_ci    }
1882e5b6d6dSopenharmony_ci    cpMap[j].nativeIdx = i;   // position following the last char in utf-16 string.
1892e5b6d6dSopenharmony_ci
1902e5b6d6dSopenharmony_ci
1912e5b6d6dSopenharmony_ci    // UChar * test, null terminated
1922e5b6d6dSopenharmony_ci    status = U_ZERO_ERROR;
1932e5b6d6dSopenharmony_ci    UChar *buf = new UChar[saLen+1];
1942e5b6d6dSopenharmony_ci    sa.extract(buf, saLen+1, status);
1952e5b6d6dSopenharmony_ci    TEST_SUCCESS(status);
1962e5b6d6dSopenharmony_ci    ut = utext_openUChars(NULL, buf, -1, &status);
1972e5b6d6dSopenharmony_ci    TEST_SUCCESS(status);
1982e5b6d6dSopenharmony_ci    TestAccess(sa, ut, cpCount, cpMap);
1992e5b6d6dSopenharmony_ci    utext_close(ut);
2002e5b6d6dSopenharmony_ci    delete [] buf;
2012e5b6d6dSopenharmony_ci
2022e5b6d6dSopenharmony_ci    // UChar * test, with length
2032e5b6d6dSopenharmony_ci    status = U_ZERO_ERROR;
2042e5b6d6dSopenharmony_ci    buf = new UChar[saLen+1];
2052e5b6d6dSopenharmony_ci    sa.extract(buf, saLen+1, status);
2062e5b6d6dSopenharmony_ci    TEST_SUCCESS(status);
2072e5b6d6dSopenharmony_ci    ut = utext_openUChars(NULL, buf, saLen, &status);
2082e5b6d6dSopenharmony_ci    TEST_SUCCESS(status);
2092e5b6d6dSopenharmony_ci    TestAccess(sa, ut, cpCount, cpMap);
2102e5b6d6dSopenharmony_ci    utext_close(ut);
2112e5b6d6dSopenharmony_ci    delete [] buf;
2122e5b6d6dSopenharmony_ci
2132e5b6d6dSopenharmony_ci
2142e5b6d6dSopenharmony_ci    // UnicodeString test
2152e5b6d6dSopenharmony_ci    status = U_ZERO_ERROR;
2162e5b6d6dSopenharmony_ci    ut = utext_openUnicodeString(NULL, &sa, &status);
2172e5b6d6dSopenharmony_ci    TEST_SUCCESS(status);
2182e5b6d6dSopenharmony_ci    TestAccess(sa, ut, cpCount, cpMap);
2192e5b6d6dSopenharmony_ci    TestCMR(sa, ut, cpCount, cpMap, cpMap);
2202e5b6d6dSopenharmony_ci    utext_close(ut);
2212e5b6d6dSopenharmony_ci
2222e5b6d6dSopenharmony_ci
2232e5b6d6dSopenharmony_ci    // Const UnicodeString test
2242e5b6d6dSopenharmony_ci    status = U_ZERO_ERROR;
2252e5b6d6dSopenharmony_ci    ut = utext_openConstUnicodeString(NULL, &sa, &status);
2262e5b6d6dSopenharmony_ci    TEST_SUCCESS(status);
2272e5b6d6dSopenharmony_ci    TestAccess(sa, ut, cpCount, cpMap);
2282e5b6d6dSopenharmony_ci    utext_close(ut);
2292e5b6d6dSopenharmony_ci
2302e5b6d6dSopenharmony_ci
2312e5b6d6dSopenharmony_ci    // Replaceable test.  (UnicodeString inherits Replaceable)
2322e5b6d6dSopenharmony_ci    status = U_ZERO_ERROR;
2332e5b6d6dSopenharmony_ci    ut = utext_openReplaceable(NULL, &sa, &status);
2342e5b6d6dSopenharmony_ci    TEST_SUCCESS(status);
2352e5b6d6dSopenharmony_ci    TestAccess(sa, ut, cpCount, cpMap);
2362e5b6d6dSopenharmony_ci    TestCMR(sa, ut, cpCount, cpMap, cpMap);
2372e5b6d6dSopenharmony_ci    utext_close(ut);
2382e5b6d6dSopenharmony_ci
2392e5b6d6dSopenharmony_ci    // Character Iterator Tests
2402e5b6d6dSopenharmony_ci    status = U_ZERO_ERROR;
2412e5b6d6dSopenharmony_ci    const UChar *cbuf = sa.getBuffer();
2422e5b6d6dSopenharmony_ci    CharacterIterator *ci = new UCharCharacterIterator(cbuf, saLen, status);
2432e5b6d6dSopenharmony_ci    TEST_SUCCESS(status);
2442e5b6d6dSopenharmony_ci    ut = utext_openCharacterIterator(NULL, ci, &status);
2452e5b6d6dSopenharmony_ci    TEST_SUCCESS(status);
2462e5b6d6dSopenharmony_ci    TestAccess(sa, ut, cpCount, cpMap);
2472e5b6d6dSopenharmony_ci    utext_close(ut);
2482e5b6d6dSopenharmony_ci    delete ci;
2492e5b6d6dSopenharmony_ci
2502e5b6d6dSopenharmony_ci
2512e5b6d6dSopenharmony_ci    // Fragmented UnicodeString  (Chunk size of one)
2522e5b6d6dSopenharmony_ci    //
2532e5b6d6dSopenharmony_ci    status = U_ZERO_ERROR;
2542e5b6d6dSopenharmony_ci    ut = openFragmentedUnicodeString(NULL, &sa, &status);
2552e5b6d6dSopenharmony_ci    TEST_SUCCESS(status);
2562e5b6d6dSopenharmony_ci    TestAccess(sa, ut, cpCount, cpMap);
2572e5b6d6dSopenharmony_ci    utext_close(ut);
2582e5b6d6dSopenharmony_ci
2592e5b6d6dSopenharmony_ci    //
2602e5b6d6dSopenharmony_ci    // UTF-8 test
2612e5b6d6dSopenharmony_ci    //
2622e5b6d6dSopenharmony_ci
2632e5b6d6dSopenharmony_ci    // Convert the test string from UnicodeString to (char *) in utf-8 format
2642e5b6d6dSopenharmony_ci    int32_t u8Len = sa.extract(0, sa.length(), NULL, 0, "utf-8");
2652e5b6d6dSopenharmony_ci    char *u8String = new char[u8Len + 1];
2662e5b6d6dSopenharmony_ci    sa.extract(0, sa.length(), u8String, u8Len+1, "utf-8");
2672e5b6d6dSopenharmony_ci
2682e5b6d6dSopenharmony_ci    // Build up the map of code point indices in the utf-8 string
2692e5b6d6dSopenharmony_ci    m * u8Map = new m[sa.length() + 1];
2702e5b6d6dSopenharmony_ci    i = 0;   // native utf-8 index
2712e5b6d6dSopenharmony_ci    for (j=0; j<cpCount ; j++) {  // code point number
2722e5b6d6dSopenharmony_ci        u8Map[j].nativeIdx = i;
2732e5b6d6dSopenharmony_ci        U8_NEXT(u8String, i, u8Len, c);
2742e5b6d6dSopenharmony_ci        u8Map[j].cp = c;
2752e5b6d6dSopenharmony_ci    }
2762e5b6d6dSopenharmony_ci    u8Map[cpCount].nativeIdx = u8Len;   // position following the last char in utf-8 string.
2772e5b6d6dSopenharmony_ci
2782e5b6d6dSopenharmony_ci    // Do the test itself
2792e5b6d6dSopenharmony_ci    status = U_ZERO_ERROR;
2802e5b6d6dSopenharmony_ci    ut = utext_openUTF8(NULL, u8String, -1, &status);
2812e5b6d6dSopenharmony_ci    TEST_SUCCESS(status);
2822e5b6d6dSopenharmony_ci    TestAccess(sa, ut, cpCount, u8Map);
2832e5b6d6dSopenharmony_ci    utext_close(ut);
2842e5b6d6dSopenharmony_ci
2852e5b6d6dSopenharmony_ci
2862e5b6d6dSopenharmony_ci
2872e5b6d6dSopenharmony_ci    delete []cpMap;
2882e5b6d6dSopenharmony_ci    delete []u8Map;
2892e5b6d6dSopenharmony_ci    delete []u8String;
2902e5b6d6dSopenharmony_ci}
2912e5b6d6dSopenharmony_ci
2922e5b6d6dSopenharmony_ci//  TestCMR   test Copy, Move and Replace operations.
2932e5b6d6dSopenharmony_ci//              us         UnicodeString containing the test text.
2942e5b6d6dSopenharmony_ci//              ut         UText containing the same test text.
2952e5b6d6dSopenharmony_ci//              cpCount    number of code points in the test text.
2962e5b6d6dSopenharmony_ci//              nativeMap  Mapping from code points to native indexes for the UText.
2972e5b6d6dSopenharmony_ci//              u16Map     Mapping from code points to UTF-16 indexes, for use with the UnicodeString.
2982e5b6d6dSopenharmony_ci//
2992e5b6d6dSopenharmony_ci//     This function runs a whole series of operations on each incoming UText.
3002e5b6d6dSopenharmony_ci//     The UText is deep-cloned prior to each operation, so that the original UText remains unchanged.
3012e5b6d6dSopenharmony_ci//
3022e5b6d6dSopenharmony_civoid UTextTest::TestCMR(const UnicodeString &us, UText *ut, int cpCount, m *nativeMap, m *u16Map) {
3032e5b6d6dSopenharmony_ci    TEST_ASSERT(utext_isWritable(ut) == true);
3042e5b6d6dSopenharmony_ci
3052e5b6d6dSopenharmony_ci    int  srcLengthType;       // Loop variables for selecting the position and length
3062e5b6d6dSopenharmony_ci    int  srcPosType;          //   of the block to operate on within the source text.
3072e5b6d6dSopenharmony_ci    int  destPosType;
3082e5b6d6dSopenharmony_ci
3092e5b6d6dSopenharmony_ci    int  srcIndex  = 0;       // Code Point indexes of the block to operate on for
3102e5b6d6dSopenharmony_ci    int  srcLength = 0;       //   a specific test.
3112e5b6d6dSopenharmony_ci
3122e5b6d6dSopenharmony_ci    int  destIndex = 0;       // Code point index of the destination for a copy/move test.
3132e5b6d6dSopenharmony_ci
3142e5b6d6dSopenharmony_ci    int32_t  nativeStart = 0; // Native unit indexes for a test.
3152e5b6d6dSopenharmony_ci    int32_t  nativeLimit = 0;
3162e5b6d6dSopenharmony_ci    int32_t  nativeDest  = 0;
3172e5b6d6dSopenharmony_ci
3182e5b6d6dSopenharmony_ci    int32_t  u16Start    = 0; // UTF-16 indexes for a test.
3192e5b6d6dSopenharmony_ci    int32_t  u16Limit    = 0; //   used when performing the same operation in a Unicode String
3202e5b6d6dSopenharmony_ci    int32_t  u16Dest     = 0;
3212e5b6d6dSopenharmony_ci
3222e5b6d6dSopenharmony_ci    // Iterate over a whole series of source index, length and a target indexes.
3232e5b6d6dSopenharmony_ci    // This is done with code point indexes; these will be later translated to native
3242e5b6d6dSopenharmony_ci    //   indexes using the cpMap.
3252e5b6d6dSopenharmony_ci    for (srcLengthType=1; srcLengthType<=3; srcLengthType++) {
3262e5b6d6dSopenharmony_ci        switch (srcLengthType) {
3272e5b6d6dSopenharmony_ci            case 1: srcLength = 1; break;
3282e5b6d6dSopenharmony_ci            case 2: srcLength = 5; break;
3292e5b6d6dSopenharmony_ci            case 3: srcLength = cpCount / 3;
3302e5b6d6dSopenharmony_ci        }
3312e5b6d6dSopenharmony_ci        for (srcPosType=1; srcPosType<=5; srcPosType++) {
3322e5b6d6dSopenharmony_ci            switch (srcPosType) {
3332e5b6d6dSopenharmony_ci                case 1: srcIndex = 0; break;
3342e5b6d6dSopenharmony_ci                case 2: srcIndex = 1; break;
3352e5b6d6dSopenharmony_ci                case 3: srcIndex = cpCount - srcLength; break;
3362e5b6d6dSopenharmony_ci                case 4: srcIndex = cpCount - srcLength - 1; break;
3372e5b6d6dSopenharmony_ci                case 5: srcIndex = cpCount / 2; break;
3382e5b6d6dSopenharmony_ci            }
3392e5b6d6dSopenharmony_ci            if (srcIndex < 0 || srcIndex + srcLength > cpCount) {
3402e5b6d6dSopenharmony_ci                // filter out bogus test cases -
3412e5b6d6dSopenharmony_ci                //   those with a source range that falls of an edge of the string.
3422e5b6d6dSopenharmony_ci                continue;
3432e5b6d6dSopenharmony_ci            }
3442e5b6d6dSopenharmony_ci
3452e5b6d6dSopenharmony_ci            //
3462e5b6d6dSopenharmony_ci            // Copy and move tests.
3472e5b6d6dSopenharmony_ci            //   iterate over a variety of destination positions.
3482e5b6d6dSopenharmony_ci            //
3492e5b6d6dSopenharmony_ci            for (destPosType=1; destPosType<=4; destPosType++) {
3502e5b6d6dSopenharmony_ci                switch (destPosType) {
3512e5b6d6dSopenharmony_ci                    case 1: destIndex = 0; break;
3522e5b6d6dSopenharmony_ci                    case 2: destIndex = 1; break;
3532e5b6d6dSopenharmony_ci                    case 3: destIndex = srcIndex - 1; break;
3542e5b6d6dSopenharmony_ci                    case 4: destIndex = srcIndex + srcLength + 1; break;
3552e5b6d6dSopenharmony_ci                    case 5: destIndex = cpCount-1; break;
3562e5b6d6dSopenharmony_ci                    case 6: destIndex = cpCount; break;
3572e5b6d6dSopenharmony_ci                }
3582e5b6d6dSopenharmony_ci                if (destIndex<0 || destIndex>cpCount) {
3592e5b6d6dSopenharmony_ci                    // filter out bogus test cases.
3602e5b6d6dSopenharmony_ci                    continue;
3612e5b6d6dSopenharmony_ci                }
3622e5b6d6dSopenharmony_ci
3632e5b6d6dSopenharmony_ci                nativeStart = nativeMap[srcIndex].nativeIdx;
3642e5b6d6dSopenharmony_ci                nativeLimit = nativeMap[srcIndex+srcLength].nativeIdx;
3652e5b6d6dSopenharmony_ci                nativeDest  = nativeMap[destIndex].nativeIdx;
3662e5b6d6dSopenharmony_ci
3672e5b6d6dSopenharmony_ci                u16Start    = u16Map[srcIndex].nativeIdx;
3682e5b6d6dSopenharmony_ci                u16Limit    = u16Map[srcIndex+srcLength].nativeIdx;
3692e5b6d6dSopenharmony_ci                u16Dest     = u16Map[destIndex].nativeIdx;
3702e5b6d6dSopenharmony_ci
3712e5b6d6dSopenharmony_ci                gFailed = false;
3722e5b6d6dSopenharmony_ci                TestCopyMove(us, ut, false,
3732e5b6d6dSopenharmony_ci                    nativeStart, nativeLimit, nativeDest,
3742e5b6d6dSopenharmony_ci                    u16Start, u16Limit, u16Dest);
3752e5b6d6dSopenharmony_ci
3762e5b6d6dSopenharmony_ci                TestCopyMove(us, ut, true,
3772e5b6d6dSopenharmony_ci                    nativeStart, nativeLimit, nativeDest,
3782e5b6d6dSopenharmony_ci                    u16Start, u16Limit, u16Dest);
3792e5b6d6dSopenharmony_ci
3802e5b6d6dSopenharmony_ci                if (gFailed) {
3812e5b6d6dSopenharmony_ci                    return;
3822e5b6d6dSopenharmony_ci                }
3832e5b6d6dSopenharmony_ci            }
3842e5b6d6dSopenharmony_ci
3852e5b6d6dSopenharmony_ci            //
3862e5b6d6dSopenharmony_ci            //  Replace tests.
3872e5b6d6dSopenharmony_ci            //
3882e5b6d6dSopenharmony_ci            UnicodeString fullRepString("This is an arbitrary string that will be used as replacement text");
3892e5b6d6dSopenharmony_ci            for (int32_t replStrLen=0; replStrLen<20; replStrLen++) {
3902e5b6d6dSopenharmony_ci                UnicodeString repStr(fullRepString, 0, replStrLen);
3912e5b6d6dSopenharmony_ci                TestReplace(us, ut,
3922e5b6d6dSopenharmony_ci                    nativeStart, nativeLimit,
3932e5b6d6dSopenharmony_ci                    u16Start, u16Limit,
3942e5b6d6dSopenharmony_ci                    repStr);
3952e5b6d6dSopenharmony_ci                if (gFailed) {
3962e5b6d6dSopenharmony_ci                    return;
3972e5b6d6dSopenharmony_ci                }
3982e5b6d6dSopenharmony_ci            }
3992e5b6d6dSopenharmony_ci
4002e5b6d6dSopenharmony_ci        }
4012e5b6d6dSopenharmony_ci    }
4022e5b6d6dSopenharmony_ci
4032e5b6d6dSopenharmony_ci}
4042e5b6d6dSopenharmony_ci
4052e5b6d6dSopenharmony_ci//
4062e5b6d6dSopenharmony_ci//   TestCopyMove    run a single test case for utext_copy.
4072e5b6d6dSopenharmony_ci//                   Test cases are created in TestCMR and dispatched here for execution.
4082e5b6d6dSopenharmony_ci//
4092e5b6d6dSopenharmony_civoid UTextTest::TestCopyMove(const UnicodeString &us, UText *ut, UBool move,
4102e5b6d6dSopenharmony_ci                    int32_t nativeStart, int32_t nativeLimit, int32_t nativeDest,
4112e5b6d6dSopenharmony_ci                    int32_t u16Start, int32_t u16Limit, int32_t u16Dest)
4122e5b6d6dSopenharmony_ci{
4132e5b6d6dSopenharmony_ci    UErrorCode      status   = U_ZERO_ERROR;
4142e5b6d6dSopenharmony_ci    UText          *targetUT = NULL;
4152e5b6d6dSopenharmony_ci    gTestNum++;
4162e5b6d6dSopenharmony_ci    gFailed = false;
4172e5b6d6dSopenharmony_ci
4182e5b6d6dSopenharmony_ci    //
4192e5b6d6dSopenharmony_ci    //  clone the UText.  The test will be run in the cloned copy
4202e5b6d6dSopenharmony_ci    //  so that we don't alter the original.
4212e5b6d6dSopenharmony_ci    //
4222e5b6d6dSopenharmony_ci    targetUT = utext_clone(NULL, ut, true, false, &status);
4232e5b6d6dSopenharmony_ci    TEST_SUCCESS(status);
4242e5b6d6dSopenharmony_ci    UnicodeString targetUS(us);    // And copy the reference string.
4252e5b6d6dSopenharmony_ci
4262e5b6d6dSopenharmony_ci    // do the test operation first in the reference
4272e5b6d6dSopenharmony_ci    targetUS.copy(u16Start, u16Limit, u16Dest);
4282e5b6d6dSopenharmony_ci    if (move) {
4292e5b6d6dSopenharmony_ci        // delete out the source range.
4302e5b6d6dSopenharmony_ci        if (u16Limit < u16Dest) {
4312e5b6d6dSopenharmony_ci            targetUS.removeBetween(u16Start, u16Limit);
4322e5b6d6dSopenharmony_ci        } else {
4332e5b6d6dSopenharmony_ci            int32_t amtCopied = u16Limit - u16Start;
4342e5b6d6dSopenharmony_ci            targetUS.removeBetween(u16Start+amtCopied, u16Limit+amtCopied);
4352e5b6d6dSopenharmony_ci        }
4362e5b6d6dSopenharmony_ci    }
4372e5b6d6dSopenharmony_ci
4382e5b6d6dSopenharmony_ci    // Do the same operation in the UText under test
4392e5b6d6dSopenharmony_ci    utext_copy(targetUT, nativeStart, nativeLimit, nativeDest, move, &status);
4402e5b6d6dSopenharmony_ci    if (nativeDest > nativeStart && nativeDest < nativeLimit) {
4412e5b6d6dSopenharmony_ci        TEST_ASSERT(status == U_INDEX_OUTOFBOUNDS_ERROR);
4422e5b6d6dSopenharmony_ci    } else {
4432e5b6d6dSopenharmony_ci        TEST_SUCCESS(status);
4442e5b6d6dSopenharmony_ci
4452e5b6d6dSopenharmony_ci        // Compare the results of the two parallel tests
4462e5b6d6dSopenharmony_ci        int32_t  usi = 0;    // UnicodeString position, utf-16 index.
4472e5b6d6dSopenharmony_ci        int64_t  uti = 0;    // UText position, native index.
4482e5b6d6dSopenharmony_ci        int32_t  cpi;        // char32 position (code point index)
4492e5b6d6dSopenharmony_ci        UChar32  usc;        // code point from Unicode String
4502e5b6d6dSopenharmony_ci        UChar32  utc;        // code point from UText
4512e5b6d6dSopenharmony_ci        utext_setNativeIndex(targetUT, 0);
4522e5b6d6dSopenharmony_ci        for (cpi=0; ; cpi++) {
4532e5b6d6dSopenharmony_ci            usc = targetUS.char32At(usi);
4542e5b6d6dSopenharmony_ci            utc = utext_next32(targetUT);
4552e5b6d6dSopenharmony_ci            if (utc < 0) {
4562e5b6d6dSopenharmony_ci                break;
4572e5b6d6dSopenharmony_ci            }
4582e5b6d6dSopenharmony_ci            TEST_ASSERT(uti == usi);
4592e5b6d6dSopenharmony_ci            TEST_ASSERT(utc == usc);
4602e5b6d6dSopenharmony_ci            usi = targetUS.moveIndex32(usi, 1);
4612e5b6d6dSopenharmony_ci            uti = utext_getNativeIndex(targetUT);
4622e5b6d6dSopenharmony_ci            if (gFailed) {
4632e5b6d6dSopenharmony_ci                goto cleanupAndReturn;
4642e5b6d6dSopenharmony_ci            }
4652e5b6d6dSopenharmony_ci        }
4662e5b6d6dSopenharmony_ci        int64_t expectedNativeLength = utext_nativeLength(ut);
4672e5b6d6dSopenharmony_ci        if (move == false) {
4682e5b6d6dSopenharmony_ci            expectedNativeLength += nativeLimit - nativeStart;
4692e5b6d6dSopenharmony_ci        }
4702e5b6d6dSopenharmony_ci        uti = utext_getNativeIndex(targetUT);
4712e5b6d6dSopenharmony_ci        TEST_ASSERT(uti == expectedNativeLength);
4722e5b6d6dSopenharmony_ci    }
4732e5b6d6dSopenharmony_ci
4742e5b6d6dSopenharmony_cicleanupAndReturn:
4752e5b6d6dSopenharmony_ci    utext_close(targetUT);
4762e5b6d6dSopenharmony_ci}
4772e5b6d6dSopenharmony_ci
4782e5b6d6dSopenharmony_ci
4792e5b6d6dSopenharmony_ci//
4802e5b6d6dSopenharmony_ci//  TestReplace   Test a single Replace operation.
4812e5b6d6dSopenharmony_ci//
4822e5b6d6dSopenharmony_civoid UTextTest::TestReplace(
4832e5b6d6dSopenharmony_ci            const UnicodeString &us,     // reference UnicodeString in which to do the replace
4842e5b6d6dSopenharmony_ci            UText         *ut,                // UnicodeText object under test.
4852e5b6d6dSopenharmony_ci            int32_t       nativeStart,        // Range to be replaced, in UText native units.
4862e5b6d6dSopenharmony_ci            int32_t       nativeLimit,
4872e5b6d6dSopenharmony_ci            int32_t       u16Start,           // Range to be replaced, in UTF-16 units
4882e5b6d6dSopenharmony_ci            int32_t       u16Limit,           //    for use in the reference UnicodeString.
4892e5b6d6dSopenharmony_ci            const UnicodeString &repStr)      // The replacement string
4902e5b6d6dSopenharmony_ci{
4912e5b6d6dSopenharmony_ci    UErrorCode      status   = U_ZERO_ERROR;
4922e5b6d6dSopenharmony_ci    UText          *targetUT = NULL;
4932e5b6d6dSopenharmony_ci    gTestNum++;
4942e5b6d6dSopenharmony_ci    gFailed = false;
4952e5b6d6dSopenharmony_ci
4962e5b6d6dSopenharmony_ci    //
4972e5b6d6dSopenharmony_ci    //  clone the target UText.  The test will be run in the cloned copy
4982e5b6d6dSopenharmony_ci    //  so that we don't alter the original.
4992e5b6d6dSopenharmony_ci    //
5002e5b6d6dSopenharmony_ci    targetUT = utext_clone(NULL, ut, true, false, &status);
5012e5b6d6dSopenharmony_ci    TEST_SUCCESS(status);
5022e5b6d6dSopenharmony_ci    UnicodeString targetUS(us);    // And copy the reference string.
5032e5b6d6dSopenharmony_ci
5042e5b6d6dSopenharmony_ci    //
5052e5b6d6dSopenharmony_ci    // Do the replace operation in the Unicode String, to
5062e5b6d6dSopenharmony_ci    //   produce a reference result.
5072e5b6d6dSopenharmony_ci    //
5082e5b6d6dSopenharmony_ci    targetUS.replace(u16Start, u16Limit-u16Start, repStr);
5092e5b6d6dSopenharmony_ci
5102e5b6d6dSopenharmony_ci    //
5112e5b6d6dSopenharmony_ci    // Do the replace on the UText under test
5122e5b6d6dSopenharmony_ci    //
5132e5b6d6dSopenharmony_ci    const UChar *rs = repStr.getBuffer();
5142e5b6d6dSopenharmony_ci    int32_t  rsLen = repStr.length();
5152e5b6d6dSopenharmony_ci    int32_t actualDelta = utext_replace(targetUT, nativeStart, nativeLimit, rs, rsLen, &status);
5162e5b6d6dSopenharmony_ci    int32_t expectedDelta = repStr.length() - (nativeLimit - nativeStart);
5172e5b6d6dSopenharmony_ci    TEST_ASSERT(actualDelta == expectedDelta);
5182e5b6d6dSopenharmony_ci
5192e5b6d6dSopenharmony_ci    //
5202e5b6d6dSopenharmony_ci    // Compare the results
5212e5b6d6dSopenharmony_ci    //
5222e5b6d6dSopenharmony_ci    int32_t  usi = 0;    // UnicodeString position, utf-16 index.
5232e5b6d6dSopenharmony_ci    int64_t  uti = 0;    // UText position, native index.
5242e5b6d6dSopenharmony_ci    int32_t  cpi;        // char32 position (code point index)
5252e5b6d6dSopenharmony_ci    UChar32  usc;        // code point from Unicode String
5262e5b6d6dSopenharmony_ci    UChar32  utc;        // code point from UText
5272e5b6d6dSopenharmony_ci    int64_t  expectedNativeLength = 0;
5282e5b6d6dSopenharmony_ci    utext_setNativeIndex(targetUT, 0);
5292e5b6d6dSopenharmony_ci    for (cpi=0; ; cpi++) {
5302e5b6d6dSopenharmony_ci        usc = targetUS.char32At(usi);
5312e5b6d6dSopenharmony_ci        utc = utext_next32(targetUT);
5322e5b6d6dSopenharmony_ci        if (utc < 0) {
5332e5b6d6dSopenharmony_ci            break;
5342e5b6d6dSopenharmony_ci        }
5352e5b6d6dSopenharmony_ci        TEST_ASSERT(uti == usi);
5362e5b6d6dSopenharmony_ci        TEST_ASSERT(utc == usc);
5372e5b6d6dSopenharmony_ci        usi = targetUS.moveIndex32(usi, 1);
5382e5b6d6dSopenharmony_ci        uti = utext_getNativeIndex(targetUT);
5392e5b6d6dSopenharmony_ci        if (gFailed) {
5402e5b6d6dSopenharmony_ci            goto cleanupAndReturn;
5412e5b6d6dSopenharmony_ci        }
5422e5b6d6dSopenharmony_ci    }
5432e5b6d6dSopenharmony_ci    expectedNativeLength = utext_nativeLength(ut) + expectedDelta;
5442e5b6d6dSopenharmony_ci    uti = utext_getNativeIndex(targetUT);
5452e5b6d6dSopenharmony_ci    TEST_ASSERT(uti == expectedNativeLength);
5462e5b6d6dSopenharmony_ci
5472e5b6d6dSopenharmony_cicleanupAndReturn:
5482e5b6d6dSopenharmony_ci    utext_close(targetUT);
5492e5b6d6dSopenharmony_ci}
5502e5b6d6dSopenharmony_ci
5512e5b6d6dSopenharmony_ci//
5522e5b6d6dSopenharmony_ci//  TestAccess      Test the read only access functions on a UText, including cloning.
5532e5b6d6dSopenharmony_ci//                  The text is accessed in a variety of ways, and compared with
5542e5b6d6dSopenharmony_ci//                  the reference UnicodeString.
5552e5b6d6dSopenharmony_ci//
5562e5b6d6dSopenharmony_civoid UTextTest::TestAccess(const UnicodeString &us, UText *ut, int cpCount, m *cpMap) {
5572e5b6d6dSopenharmony_ci    // Run the standard tests on the caller-supplied UText.
5582e5b6d6dSopenharmony_ci    TestAccessNoClone(us, ut, cpCount, cpMap);
5592e5b6d6dSopenharmony_ci
5602e5b6d6dSopenharmony_ci    // Re-run tests on a shallow clone.
5612e5b6d6dSopenharmony_ci    utext_setNativeIndex(ut, 0);
5622e5b6d6dSopenharmony_ci    UErrorCode status = U_ZERO_ERROR;
5632e5b6d6dSopenharmony_ci    UText *shallowClone = utext_clone(NULL, ut, false /*deep*/, false /*readOnly*/, &status);
5642e5b6d6dSopenharmony_ci    TEST_SUCCESS(status);
5652e5b6d6dSopenharmony_ci    TestAccessNoClone(us, shallowClone, cpCount, cpMap);
5662e5b6d6dSopenharmony_ci
5672e5b6d6dSopenharmony_ci    //
5682e5b6d6dSopenharmony_ci    // Rerun again on a deep clone.
5692e5b6d6dSopenharmony_ci    // Note that text providers are not required to provide deep cloning,
5702e5b6d6dSopenharmony_ci    //   so unsupported errors are ignored.
5712e5b6d6dSopenharmony_ci    //
5722e5b6d6dSopenharmony_ci    status = U_ZERO_ERROR;
5732e5b6d6dSopenharmony_ci    utext_setNativeIndex(shallowClone, 0);
5742e5b6d6dSopenharmony_ci    UText *deepClone = utext_clone(NULL, shallowClone, true, false, &status);
5752e5b6d6dSopenharmony_ci    utext_close(shallowClone);
5762e5b6d6dSopenharmony_ci    if (status != U_UNSUPPORTED_ERROR) {
5772e5b6d6dSopenharmony_ci        TEST_SUCCESS(status);
5782e5b6d6dSopenharmony_ci        TestAccessNoClone(us, deepClone, cpCount, cpMap);
5792e5b6d6dSopenharmony_ci    }
5802e5b6d6dSopenharmony_ci    utext_close(deepClone);
5812e5b6d6dSopenharmony_ci}
5822e5b6d6dSopenharmony_ci
5832e5b6d6dSopenharmony_ci
5842e5b6d6dSopenharmony_ci//
5852e5b6d6dSopenharmony_ci//  TestAccessNoClone()    Test the read only access functions on a UText.
5862e5b6d6dSopenharmony_ci//                         The text is accessed in a variety of ways, and compared with
5872e5b6d6dSopenharmony_ci//                         the reference UnicodeString.
5882e5b6d6dSopenharmony_ci//
5892e5b6d6dSopenharmony_civoid UTextTest::TestAccessNoClone(const UnicodeString &us, UText *ut, int cpCount, m *cpMap) {
5902e5b6d6dSopenharmony_ci    UErrorCode  status = U_ZERO_ERROR;
5912e5b6d6dSopenharmony_ci    gTestNum++;
5922e5b6d6dSopenharmony_ci
5932e5b6d6dSopenharmony_ci    //
5942e5b6d6dSopenharmony_ci    //  Check the length from the UText
5952e5b6d6dSopenharmony_ci    //
5962e5b6d6dSopenharmony_ci    int64_t expectedLen = cpMap[cpCount].nativeIdx;
5972e5b6d6dSopenharmony_ci    int64_t utlen = utext_nativeLength(ut);
5982e5b6d6dSopenharmony_ci    TEST_ASSERT(expectedLen == utlen);
5992e5b6d6dSopenharmony_ci
6002e5b6d6dSopenharmony_ci    //
6012e5b6d6dSopenharmony_ci    //  Iterate forwards, verify that we get the correct code points
6022e5b6d6dSopenharmony_ci    //   at the correct native offsets.
6032e5b6d6dSopenharmony_ci    //
6042e5b6d6dSopenharmony_ci    int         i = 0;
6052e5b6d6dSopenharmony_ci    int64_t     index;
6062e5b6d6dSopenharmony_ci    int64_t     expectedIndex = 0;
6072e5b6d6dSopenharmony_ci    int64_t     foundIndex = 0;
6082e5b6d6dSopenharmony_ci    UChar32     expectedC;
6092e5b6d6dSopenharmony_ci    UChar32     foundC;
6102e5b6d6dSopenharmony_ci    int64_t     len;
6112e5b6d6dSopenharmony_ci
6122e5b6d6dSopenharmony_ci    for (i=0; i<cpCount; i++) {
6132e5b6d6dSopenharmony_ci        expectedIndex = cpMap[i].nativeIdx;
6142e5b6d6dSopenharmony_ci        foundIndex    = utext_getNativeIndex(ut);
6152e5b6d6dSopenharmony_ci        TEST_ASSERT(expectedIndex == foundIndex);
6162e5b6d6dSopenharmony_ci        expectedC     = cpMap[i].cp;
6172e5b6d6dSopenharmony_ci        foundC        = utext_next32(ut);
6182e5b6d6dSopenharmony_ci        TEST_ASSERT(expectedC == foundC);
6192e5b6d6dSopenharmony_ci        foundIndex    = utext_getPreviousNativeIndex(ut);
6202e5b6d6dSopenharmony_ci        TEST_ASSERT(expectedIndex == foundIndex);
6212e5b6d6dSopenharmony_ci        if (gFailed) {
6222e5b6d6dSopenharmony_ci            return;
6232e5b6d6dSopenharmony_ci        }
6242e5b6d6dSopenharmony_ci    }
6252e5b6d6dSopenharmony_ci    foundC = utext_next32(ut);
6262e5b6d6dSopenharmony_ci    TEST_ASSERT(foundC == U_SENTINEL);
6272e5b6d6dSopenharmony_ci
6282e5b6d6dSopenharmony_ci    // Repeat above, using macros
6292e5b6d6dSopenharmony_ci    utext_setNativeIndex(ut, 0);
6302e5b6d6dSopenharmony_ci    for (i=0; i<cpCount; i++) {
6312e5b6d6dSopenharmony_ci        expectedIndex = cpMap[i].nativeIdx;
6322e5b6d6dSopenharmony_ci        foundIndex    = UTEXT_GETNATIVEINDEX(ut);
6332e5b6d6dSopenharmony_ci        TEST_ASSERT(expectedIndex == foundIndex);
6342e5b6d6dSopenharmony_ci        expectedC     = cpMap[i].cp;
6352e5b6d6dSopenharmony_ci        foundC        = UTEXT_NEXT32(ut);
6362e5b6d6dSopenharmony_ci        TEST_ASSERT(expectedC == foundC);
6372e5b6d6dSopenharmony_ci        if (gFailed) {
6382e5b6d6dSopenharmony_ci            return;
6392e5b6d6dSopenharmony_ci        }
6402e5b6d6dSopenharmony_ci    }
6412e5b6d6dSopenharmony_ci    foundC = UTEXT_NEXT32(ut);
6422e5b6d6dSopenharmony_ci    TEST_ASSERT(foundC == U_SENTINEL);
6432e5b6d6dSopenharmony_ci
6442e5b6d6dSopenharmony_ci    //
6452e5b6d6dSopenharmony_ci    //  Forward iteration (above) should have left index at the
6462e5b6d6dSopenharmony_ci    //   end of the input, which should == length().
6472e5b6d6dSopenharmony_ci    //
6482e5b6d6dSopenharmony_ci    len = utext_nativeLength(ut);
6492e5b6d6dSopenharmony_ci    foundIndex  = utext_getNativeIndex(ut);
6502e5b6d6dSopenharmony_ci    TEST_ASSERT(len == foundIndex);
6512e5b6d6dSopenharmony_ci
6522e5b6d6dSopenharmony_ci    //
6532e5b6d6dSopenharmony_ci    // Iterate backwards over entire test string
6542e5b6d6dSopenharmony_ci    //
6552e5b6d6dSopenharmony_ci    len = utext_getNativeIndex(ut);
6562e5b6d6dSopenharmony_ci    utext_setNativeIndex(ut, len);
6572e5b6d6dSopenharmony_ci    for (i=cpCount-1; i>=0; i--) {
6582e5b6d6dSopenharmony_ci        expectedC     = cpMap[i].cp;
6592e5b6d6dSopenharmony_ci        expectedIndex = cpMap[i].nativeIdx;
6602e5b6d6dSopenharmony_ci        int64_t prevIndex = utext_getPreviousNativeIndex(ut);
6612e5b6d6dSopenharmony_ci        foundC        = utext_previous32(ut);
6622e5b6d6dSopenharmony_ci        foundIndex    = utext_getNativeIndex(ut);
6632e5b6d6dSopenharmony_ci        TEST_ASSERT(expectedIndex == foundIndex);
6642e5b6d6dSopenharmony_ci        TEST_ASSERT(expectedC == foundC);
6652e5b6d6dSopenharmony_ci        TEST_ASSERT(prevIndex == foundIndex);
6662e5b6d6dSopenharmony_ci        if (gFailed) {
6672e5b6d6dSopenharmony_ci            return;
6682e5b6d6dSopenharmony_ci        }
6692e5b6d6dSopenharmony_ci    }
6702e5b6d6dSopenharmony_ci
6712e5b6d6dSopenharmony_ci    //
6722e5b6d6dSopenharmony_ci    //  Backwards iteration, above, should have left our iterator
6732e5b6d6dSopenharmony_ci    //   position at zero, and continued backwards iterationshould fail.
6742e5b6d6dSopenharmony_ci    //
6752e5b6d6dSopenharmony_ci    foundIndex = utext_getNativeIndex(ut);
6762e5b6d6dSopenharmony_ci    TEST_ASSERT(foundIndex == 0);
6772e5b6d6dSopenharmony_ci    foundIndex = utext_getPreviousNativeIndex(ut);
6782e5b6d6dSopenharmony_ci    TEST_ASSERT(foundIndex == 0);
6792e5b6d6dSopenharmony_ci
6802e5b6d6dSopenharmony_ci
6812e5b6d6dSopenharmony_ci    foundC = utext_previous32(ut);
6822e5b6d6dSopenharmony_ci    TEST_ASSERT(foundC == U_SENTINEL);
6832e5b6d6dSopenharmony_ci    foundIndex = utext_getNativeIndex(ut);
6842e5b6d6dSopenharmony_ci    TEST_ASSERT(foundIndex == 0);
6852e5b6d6dSopenharmony_ci    foundIndex = utext_getPreviousNativeIndex(ut);
6862e5b6d6dSopenharmony_ci    TEST_ASSERT(foundIndex == 0);
6872e5b6d6dSopenharmony_ci
6882e5b6d6dSopenharmony_ci
6892e5b6d6dSopenharmony_ci    // And again, with the macros
6902e5b6d6dSopenharmony_ci    utext_setNativeIndex(ut, len);
6912e5b6d6dSopenharmony_ci    for (i=cpCount-1; i>=0; i--) {
6922e5b6d6dSopenharmony_ci        expectedC     = cpMap[i].cp;
6932e5b6d6dSopenharmony_ci        expectedIndex = cpMap[i].nativeIdx;
6942e5b6d6dSopenharmony_ci        foundC        = UTEXT_PREVIOUS32(ut);
6952e5b6d6dSopenharmony_ci        foundIndex    = UTEXT_GETNATIVEINDEX(ut);
6962e5b6d6dSopenharmony_ci        TEST_ASSERT(expectedIndex == foundIndex);
6972e5b6d6dSopenharmony_ci        TEST_ASSERT(expectedC == foundC);
6982e5b6d6dSopenharmony_ci        if (gFailed) {
6992e5b6d6dSopenharmony_ci            return;
7002e5b6d6dSopenharmony_ci        }
7012e5b6d6dSopenharmony_ci    }
7022e5b6d6dSopenharmony_ci
7032e5b6d6dSopenharmony_ci    //
7042e5b6d6dSopenharmony_ci    //  Backwards iteration, above, should have left our iterator
7052e5b6d6dSopenharmony_ci    //   position at zero, and continued backwards iterationshould fail.
7062e5b6d6dSopenharmony_ci    //
7072e5b6d6dSopenharmony_ci    foundIndex = UTEXT_GETNATIVEINDEX(ut);
7082e5b6d6dSopenharmony_ci    TEST_ASSERT(foundIndex == 0);
7092e5b6d6dSopenharmony_ci
7102e5b6d6dSopenharmony_ci    foundC = UTEXT_PREVIOUS32(ut);
7112e5b6d6dSopenharmony_ci    TEST_ASSERT(foundC == U_SENTINEL);
7122e5b6d6dSopenharmony_ci    foundIndex = UTEXT_GETNATIVEINDEX(ut);
7132e5b6d6dSopenharmony_ci    TEST_ASSERT(foundIndex == 0);
7142e5b6d6dSopenharmony_ci    if (gFailed) {
7152e5b6d6dSopenharmony_ci        return;
7162e5b6d6dSopenharmony_ci    }
7172e5b6d6dSopenharmony_ci
7182e5b6d6dSopenharmony_ci    //
7192e5b6d6dSopenharmony_ci    //  next32From(), previous32From(), Iterate in a somewhat random order.
7202e5b6d6dSopenharmony_ci    //
7212e5b6d6dSopenharmony_ci    int  cpIndex = 0;
7222e5b6d6dSopenharmony_ci    for (i=0; i<cpCount; i++) {
7232e5b6d6dSopenharmony_ci        cpIndex = (cpIndex + 9973) % cpCount;
7242e5b6d6dSopenharmony_ci        index         = cpMap[cpIndex].nativeIdx;
7252e5b6d6dSopenharmony_ci        expectedC     = cpMap[cpIndex].cp;
7262e5b6d6dSopenharmony_ci        foundC        = utext_next32From(ut, index);
7272e5b6d6dSopenharmony_ci        TEST_ASSERT(expectedC == foundC);
7282e5b6d6dSopenharmony_ci        if (gFailed) {
7292e5b6d6dSopenharmony_ci            return;
7302e5b6d6dSopenharmony_ci        }
7312e5b6d6dSopenharmony_ci    }
7322e5b6d6dSopenharmony_ci
7332e5b6d6dSopenharmony_ci    cpIndex = 0;
7342e5b6d6dSopenharmony_ci    for (i=0; i<cpCount; i++) {
7352e5b6d6dSopenharmony_ci        cpIndex = (cpIndex + 9973) % cpCount;
7362e5b6d6dSopenharmony_ci        index         = cpMap[cpIndex+1].nativeIdx;
7372e5b6d6dSopenharmony_ci        expectedC     = cpMap[cpIndex].cp;
7382e5b6d6dSopenharmony_ci        foundC        = utext_previous32From(ut, index);
7392e5b6d6dSopenharmony_ci        TEST_ASSERT(expectedC == foundC);
7402e5b6d6dSopenharmony_ci        if (gFailed) {
7412e5b6d6dSopenharmony_ci            return;
7422e5b6d6dSopenharmony_ci        }
7432e5b6d6dSopenharmony_ci    }
7442e5b6d6dSopenharmony_ci
7452e5b6d6dSopenharmony_ci
7462e5b6d6dSopenharmony_ci    //
7472e5b6d6dSopenharmony_ci    // moveIndex(int32_t delta);
7482e5b6d6dSopenharmony_ci    //
7492e5b6d6dSopenharmony_ci
7502e5b6d6dSopenharmony_ci    // Walk through frontwards, incrementing by one
7512e5b6d6dSopenharmony_ci    utext_setNativeIndex(ut, 0);
7522e5b6d6dSopenharmony_ci    for (i=1; i<=cpCount; i++) {
7532e5b6d6dSopenharmony_ci        utext_moveIndex32(ut, 1);
7542e5b6d6dSopenharmony_ci        index = utext_getNativeIndex(ut);
7552e5b6d6dSopenharmony_ci        expectedIndex = cpMap[i].nativeIdx;
7562e5b6d6dSopenharmony_ci        TEST_ASSERT(expectedIndex == index);
7572e5b6d6dSopenharmony_ci        index = UTEXT_GETNATIVEINDEX(ut);
7582e5b6d6dSopenharmony_ci        TEST_ASSERT(expectedIndex == index);
7592e5b6d6dSopenharmony_ci    }
7602e5b6d6dSopenharmony_ci
7612e5b6d6dSopenharmony_ci    // Walk through frontwards, incrementing by two
7622e5b6d6dSopenharmony_ci    utext_setNativeIndex(ut, 0);
7632e5b6d6dSopenharmony_ci    for (i=2; i<cpCount; i+=2) {
7642e5b6d6dSopenharmony_ci        utext_moveIndex32(ut, 2);
7652e5b6d6dSopenharmony_ci        index = utext_getNativeIndex(ut);
7662e5b6d6dSopenharmony_ci        expectedIndex = cpMap[i].nativeIdx;
7672e5b6d6dSopenharmony_ci        TEST_ASSERT(expectedIndex == index);
7682e5b6d6dSopenharmony_ci        index = UTEXT_GETNATIVEINDEX(ut);
7692e5b6d6dSopenharmony_ci        TEST_ASSERT(expectedIndex == index);
7702e5b6d6dSopenharmony_ci    }
7712e5b6d6dSopenharmony_ci
7722e5b6d6dSopenharmony_ci    // walk through the string backwards, decrementing by one.
7732e5b6d6dSopenharmony_ci    i = cpMap[cpCount].nativeIdx;
7742e5b6d6dSopenharmony_ci    utext_setNativeIndex(ut, i);
7752e5b6d6dSopenharmony_ci    for (i=cpCount; i>=0; i--) {
7762e5b6d6dSopenharmony_ci        expectedIndex = cpMap[i].nativeIdx;
7772e5b6d6dSopenharmony_ci        index = utext_getNativeIndex(ut);
7782e5b6d6dSopenharmony_ci        TEST_ASSERT(expectedIndex == index);
7792e5b6d6dSopenharmony_ci        index = UTEXT_GETNATIVEINDEX(ut);
7802e5b6d6dSopenharmony_ci        TEST_ASSERT(expectedIndex == index);
7812e5b6d6dSopenharmony_ci        utext_moveIndex32(ut, -1);
7822e5b6d6dSopenharmony_ci    }
7832e5b6d6dSopenharmony_ci
7842e5b6d6dSopenharmony_ci
7852e5b6d6dSopenharmony_ci    // walk through backwards, decrementing by three
7862e5b6d6dSopenharmony_ci    i = cpMap[cpCount].nativeIdx;
7872e5b6d6dSopenharmony_ci    utext_setNativeIndex(ut, i);
7882e5b6d6dSopenharmony_ci    for (i=cpCount; i>=0; i-=3) {
7892e5b6d6dSopenharmony_ci        expectedIndex = cpMap[i].nativeIdx;
7902e5b6d6dSopenharmony_ci        index = utext_getNativeIndex(ut);
7912e5b6d6dSopenharmony_ci        TEST_ASSERT(expectedIndex == index);
7922e5b6d6dSopenharmony_ci        index = UTEXT_GETNATIVEINDEX(ut);
7932e5b6d6dSopenharmony_ci        TEST_ASSERT(expectedIndex == index);
7942e5b6d6dSopenharmony_ci        utext_moveIndex32(ut, -3);
7952e5b6d6dSopenharmony_ci    }
7962e5b6d6dSopenharmony_ci
7972e5b6d6dSopenharmony_ci
7982e5b6d6dSopenharmony_ci    //
7992e5b6d6dSopenharmony_ci    // Extract
8002e5b6d6dSopenharmony_ci    //
8012e5b6d6dSopenharmony_ci    int bufSize = us.length() + 10;
8022e5b6d6dSopenharmony_ci    UChar *buf = new UChar[bufSize];
8032e5b6d6dSopenharmony_ci    status = U_ZERO_ERROR;
8042e5b6d6dSopenharmony_ci    expectedLen = us.length();
8052e5b6d6dSopenharmony_ci    len = utext_extract(ut, 0, utlen, buf, bufSize, &status);
8062e5b6d6dSopenharmony_ci    TEST_SUCCESS(status);
8072e5b6d6dSopenharmony_ci    TEST_ASSERT(len == expectedLen);
8082e5b6d6dSopenharmony_ci    int compareResult = us.compare(buf, -1);
8092e5b6d6dSopenharmony_ci    TEST_ASSERT(compareResult == 0);
8102e5b6d6dSopenharmony_ci
8112e5b6d6dSopenharmony_ci    status = U_ZERO_ERROR;
8122e5b6d6dSopenharmony_ci    len = utext_extract(ut, 0, utlen, NULL, 0, &status);
8132e5b6d6dSopenharmony_ci    if (utlen == 0) {
8142e5b6d6dSopenharmony_ci        TEST_ASSERT(status == U_STRING_NOT_TERMINATED_WARNING);
8152e5b6d6dSopenharmony_ci    } else {
8162e5b6d6dSopenharmony_ci        TEST_ASSERT(status == U_BUFFER_OVERFLOW_ERROR);
8172e5b6d6dSopenharmony_ci    }
8182e5b6d6dSopenharmony_ci    TEST_ASSERT(len == expectedLen);
8192e5b6d6dSopenharmony_ci
8202e5b6d6dSopenharmony_ci    status = U_ZERO_ERROR;
8212e5b6d6dSopenharmony_ci    u_memset(buf, 0x5555, bufSize);
8222e5b6d6dSopenharmony_ci    len = utext_extract(ut, 0, utlen, buf, 1, &status);
8232e5b6d6dSopenharmony_ci    if (us.length() == 0) {
8242e5b6d6dSopenharmony_ci        TEST_SUCCESS(status);
8252e5b6d6dSopenharmony_ci        TEST_ASSERT(buf[0] == 0);
8262e5b6d6dSopenharmony_ci    } else {
8272e5b6d6dSopenharmony_ci        // Buf len == 1, extracting a single 16 bit value.
8282e5b6d6dSopenharmony_ci        // If the data char is supplementary, it doesn't matter whether the buffer remains unchanged,
8292e5b6d6dSopenharmony_ci        //   or whether the lead surrogate of the pair is extracted.
8302e5b6d6dSopenharmony_ci        //   It's a buffer overflow error in either case.
8312e5b6d6dSopenharmony_ci        TEST_ASSERT(buf[0] == us.charAt(0) ||
8322e5b6d6dSopenharmony_ci                    (buf[0] == 0x5555 && U_IS_SUPPLEMENTARY(us.char32At(0))));
8332e5b6d6dSopenharmony_ci        TEST_ASSERT(buf[1] == 0x5555);
8342e5b6d6dSopenharmony_ci        if (us.length() == 1) {
8352e5b6d6dSopenharmony_ci            TEST_ASSERT(status == U_STRING_NOT_TERMINATED_WARNING);
8362e5b6d6dSopenharmony_ci        } else {
8372e5b6d6dSopenharmony_ci            TEST_ASSERT(status == U_BUFFER_OVERFLOW_ERROR);
8382e5b6d6dSopenharmony_ci        }
8392e5b6d6dSopenharmony_ci    }
8402e5b6d6dSopenharmony_ci
8412e5b6d6dSopenharmony_ci    delete []buf;
8422e5b6d6dSopenharmony_ci}
8432e5b6d6dSopenharmony_ci
8442e5b6d6dSopenharmony_ci//
8452e5b6d6dSopenharmony_ci//  ErrorTest()    Check various error and edge cases.
8462e5b6d6dSopenharmony_ci//
8472e5b6d6dSopenharmony_civoid UTextTest::ErrorTest()
8482e5b6d6dSopenharmony_ci{
8492e5b6d6dSopenharmony_ci    // Close of an uninitialized UText.  Shouldn't blow up.
8502e5b6d6dSopenharmony_ci    {
8512e5b6d6dSopenharmony_ci        UText  ut;
8522e5b6d6dSopenharmony_ci        memset(&ut, 0, sizeof(UText));
8532e5b6d6dSopenharmony_ci        utext_close(&ut);
8542e5b6d6dSopenharmony_ci        utext_close(NULL);
8552e5b6d6dSopenharmony_ci    }
8562e5b6d6dSopenharmony_ci
8572e5b6d6dSopenharmony_ci    // Double-close of a UText.  Shouldn't blow up.  UText should still be usable.
8582e5b6d6dSopenharmony_ci    {
8592e5b6d6dSopenharmony_ci        UErrorCode status = U_ZERO_ERROR;
8602e5b6d6dSopenharmony_ci        UText ut = UTEXT_INITIALIZER;
8612e5b6d6dSopenharmony_ci        UnicodeString s("Hello, World");
8622e5b6d6dSopenharmony_ci        UText *ut2 = utext_openUnicodeString(&ut, &s, &status);
8632e5b6d6dSopenharmony_ci        TEST_SUCCESS(status);
8642e5b6d6dSopenharmony_ci        TEST_ASSERT(ut2 == &ut);
8652e5b6d6dSopenharmony_ci
8662e5b6d6dSopenharmony_ci        UText *ut3 = utext_close(&ut);
8672e5b6d6dSopenharmony_ci        TEST_ASSERT(ut3 == &ut);
8682e5b6d6dSopenharmony_ci
8692e5b6d6dSopenharmony_ci        UText *ut4 = utext_close(&ut);
8702e5b6d6dSopenharmony_ci        TEST_ASSERT(ut4 == &ut);
8712e5b6d6dSopenharmony_ci
8722e5b6d6dSopenharmony_ci        utext_openUnicodeString(&ut, &s, &status);
8732e5b6d6dSopenharmony_ci        TEST_SUCCESS(status);
8742e5b6d6dSopenharmony_ci        utext_close(&ut);
8752e5b6d6dSopenharmony_ci    }
8762e5b6d6dSopenharmony_ci
8772e5b6d6dSopenharmony_ci    // Re-use of a UText, chaining through each of the types of UText
8782e5b6d6dSopenharmony_ci    //   (If it doesn't blow up, and doesn't leak, it's probably working fine)
8792e5b6d6dSopenharmony_ci    {
8802e5b6d6dSopenharmony_ci        UErrorCode status = U_ZERO_ERROR;
8812e5b6d6dSopenharmony_ci        UText ut = UTEXT_INITIALIZER;
8822e5b6d6dSopenharmony_ci        UText  *utp;
8832e5b6d6dSopenharmony_ci        UnicodeString s1("Hello, World");
8842e5b6d6dSopenharmony_ci        UChar s2[] = {(UChar)0x41, (UChar)0x42, (UChar)0};
8852e5b6d6dSopenharmony_ci        const char  *s3 = "\x66\x67\x68";
8862e5b6d6dSopenharmony_ci
8872e5b6d6dSopenharmony_ci        utp = utext_openUnicodeString(&ut, &s1, &status);
8882e5b6d6dSopenharmony_ci        TEST_SUCCESS(status);
8892e5b6d6dSopenharmony_ci        TEST_ASSERT(utp == &ut);
8902e5b6d6dSopenharmony_ci
8912e5b6d6dSopenharmony_ci        utp = utext_openConstUnicodeString(&ut, &s1, &status);
8922e5b6d6dSopenharmony_ci        TEST_SUCCESS(status);
8932e5b6d6dSopenharmony_ci        TEST_ASSERT(utp == &ut);
8942e5b6d6dSopenharmony_ci
8952e5b6d6dSopenharmony_ci        utp = utext_openUTF8(&ut, s3, -1, &status);
8962e5b6d6dSopenharmony_ci        TEST_SUCCESS(status);
8972e5b6d6dSopenharmony_ci        TEST_ASSERT(utp == &ut);
8982e5b6d6dSopenharmony_ci
8992e5b6d6dSopenharmony_ci        utp = utext_openUChars(&ut, s2, -1, &status);
9002e5b6d6dSopenharmony_ci        TEST_SUCCESS(status);
9012e5b6d6dSopenharmony_ci        TEST_ASSERT(utp == &ut);
9022e5b6d6dSopenharmony_ci
9032e5b6d6dSopenharmony_ci        utp = utext_close(&ut);
9042e5b6d6dSopenharmony_ci        TEST_ASSERT(utp == &ut);
9052e5b6d6dSopenharmony_ci
9062e5b6d6dSopenharmony_ci        utp = utext_openUnicodeString(&ut, &s1, &status);
9072e5b6d6dSopenharmony_ci        TEST_SUCCESS(status);
9082e5b6d6dSopenharmony_ci        TEST_ASSERT(utp == &ut);
9092e5b6d6dSopenharmony_ci    }
9102e5b6d6dSopenharmony_ci
9112e5b6d6dSopenharmony_ci    // Invalid parameters on open
9122e5b6d6dSopenharmony_ci    //
9132e5b6d6dSopenharmony_ci    {
9142e5b6d6dSopenharmony_ci        UErrorCode status = U_ZERO_ERROR;
9152e5b6d6dSopenharmony_ci        UText ut = UTEXT_INITIALIZER;
9162e5b6d6dSopenharmony_ci
9172e5b6d6dSopenharmony_ci        utext_openUChars(&ut, NULL, 5, &status);
9182e5b6d6dSopenharmony_ci        TEST_ASSERT(status == U_ILLEGAL_ARGUMENT_ERROR);
9192e5b6d6dSopenharmony_ci
9202e5b6d6dSopenharmony_ci        status = U_ZERO_ERROR;
9212e5b6d6dSopenharmony_ci        utext_openUChars(&ut, NULL, -1, &status);
9222e5b6d6dSopenharmony_ci        TEST_ASSERT(status == U_ILLEGAL_ARGUMENT_ERROR);
9232e5b6d6dSopenharmony_ci
9242e5b6d6dSopenharmony_ci        status = U_ZERO_ERROR;
9252e5b6d6dSopenharmony_ci        utext_openUTF8(&ut, NULL, 4, &status);
9262e5b6d6dSopenharmony_ci        TEST_ASSERT(status == U_ILLEGAL_ARGUMENT_ERROR);
9272e5b6d6dSopenharmony_ci
9282e5b6d6dSopenharmony_ci        status = U_ZERO_ERROR;
9292e5b6d6dSopenharmony_ci        utext_openUTF8(&ut, NULL, -1, &status);
9302e5b6d6dSopenharmony_ci        TEST_ASSERT(status == U_ILLEGAL_ARGUMENT_ERROR);
9312e5b6d6dSopenharmony_ci    }
9322e5b6d6dSopenharmony_ci
9332e5b6d6dSopenharmony_ci    //
9342e5b6d6dSopenharmony_ci    //  UTF-8 with malformed sequences.
9352e5b6d6dSopenharmony_ci    //    These should come through as the Unicode replacement char, \ufffd
9362e5b6d6dSopenharmony_ci    //
9372e5b6d6dSopenharmony_ci    {
9382e5b6d6dSopenharmony_ci        UErrorCode status = U_ZERO_ERROR;
9392e5b6d6dSopenharmony_ci        UText *ut = NULL;
9402e5b6d6dSopenharmony_ci        const char *badUTF8 = "\x41\x81\x42\xf0\x81\x81\x43";
9412e5b6d6dSopenharmony_ci        UChar32  c;
9422e5b6d6dSopenharmony_ci
9432e5b6d6dSopenharmony_ci        ut = utext_openUTF8(NULL, badUTF8, -1, &status);
9442e5b6d6dSopenharmony_ci        TEST_SUCCESS(status);
9452e5b6d6dSopenharmony_ci        c = utext_char32At(ut, 1);
9462e5b6d6dSopenharmony_ci        TEST_ASSERT(c == 0xfffd);
9472e5b6d6dSopenharmony_ci        c = utext_char32At(ut, 3);
9482e5b6d6dSopenharmony_ci        TEST_ASSERT(c == 0xfffd);
9492e5b6d6dSopenharmony_ci        c = utext_char32At(ut, 5);
9502e5b6d6dSopenharmony_ci        TEST_ASSERT(c == 0xfffd);
9512e5b6d6dSopenharmony_ci        c = utext_char32At(ut, 6);
9522e5b6d6dSopenharmony_ci        TEST_ASSERT(c == 0x43);
9532e5b6d6dSopenharmony_ci
9542e5b6d6dSopenharmony_ci        UChar buf[10];
9552e5b6d6dSopenharmony_ci        int n = utext_extract(ut, 0, 9, buf, 10, &status);
9562e5b6d6dSopenharmony_ci        TEST_SUCCESS(status);
9572e5b6d6dSopenharmony_ci        TEST_ASSERT(n==7);
9582e5b6d6dSopenharmony_ci        TEST_ASSERT(buf[0] == 0x41);
9592e5b6d6dSopenharmony_ci        TEST_ASSERT(buf[1] == 0xfffd);
9602e5b6d6dSopenharmony_ci        TEST_ASSERT(buf[2] == 0x42);
9612e5b6d6dSopenharmony_ci        TEST_ASSERT(buf[3] == 0xfffd);
9622e5b6d6dSopenharmony_ci        TEST_ASSERT(buf[4] == 0xfffd);
9632e5b6d6dSopenharmony_ci        TEST_ASSERT(buf[5] == 0xfffd);
9642e5b6d6dSopenharmony_ci        TEST_ASSERT(buf[6] == 0x43);
9652e5b6d6dSopenharmony_ci        utext_close(ut);
9662e5b6d6dSopenharmony_ci    }
9672e5b6d6dSopenharmony_ci
9682e5b6d6dSopenharmony_ci
9692e5b6d6dSopenharmony_ci    //
9702e5b6d6dSopenharmony_ci    //  isLengthExpensive - does it make the expected transitions after
9712e5b6d6dSopenharmony_ci    //                      getting the length of a nul terminated string?
9722e5b6d6dSopenharmony_ci    //
9732e5b6d6dSopenharmony_ci    {
9742e5b6d6dSopenharmony_ci        UErrorCode status = U_ZERO_ERROR;
9752e5b6d6dSopenharmony_ci        UnicodeString sa("Hello, this is a string");
9762e5b6d6dSopenharmony_ci        UBool  isExpensive;
9772e5b6d6dSopenharmony_ci
9782e5b6d6dSopenharmony_ci        UChar sb[100];
9792e5b6d6dSopenharmony_ci        memset(sb, 0x20, sizeof(sb));
9802e5b6d6dSopenharmony_ci        sb[99] = 0;
9812e5b6d6dSopenharmony_ci
9822e5b6d6dSopenharmony_ci        UText *uta = utext_openUnicodeString(NULL, &sa, &status);
9832e5b6d6dSopenharmony_ci        TEST_SUCCESS(status);
9842e5b6d6dSopenharmony_ci        isExpensive = utext_isLengthExpensive(uta);
9852e5b6d6dSopenharmony_ci        TEST_ASSERT(isExpensive == false);
9862e5b6d6dSopenharmony_ci        utext_close(uta);
9872e5b6d6dSopenharmony_ci
9882e5b6d6dSopenharmony_ci        UText *utb = utext_openUChars(NULL, sb, -1, &status);
9892e5b6d6dSopenharmony_ci        TEST_SUCCESS(status);
9902e5b6d6dSopenharmony_ci        isExpensive = utext_isLengthExpensive(utb);
9912e5b6d6dSopenharmony_ci        TEST_ASSERT(isExpensive == true);
9922e5b6d6dSopenharmony_ci        int64_t  len = utext_nativeLength(utb);
9932e5b6d6dSopenharmony_ci        TEST_ASSERT(len == 99);
9942e5b6d6dSopenharmony_ci        isExpensive = utext_isLengthExpensive(utb);
9952e5b6d6dSopenharmony_ci        TEST_ASSERT(isExpensive == false);
9962e5b6d6dSopenharmony_ci        utext_close(utb);
9972e5b6d6dSopenharmony_ci    }
9982e5b6d6dSopenharmony_ci
9992e5b6d6dSopenharmony_ci    //
10002e5b6d6dSopenharmony_ci    // Index to positions not on code point boundaries.
10012e5b6d6dSopenharmony_ci    //
10022e5b6d6dSopenharmony_ci    {
10032e5b6d6dSopenharmony_ci        const char *u8str =         "\xc8\x81\xe1\x82\x83\xf1\x84\x85\x86";
10042e5b6d6dSopenharmony_ci        int32_t startMap[] =        {   0,  0,  2,  2,  2,  5,  5,  5,  5,  9,  9};
10052e5b6d6dSopenharmony_ci        int32_t nextMap[]  =        {   2,  2,  5,  5,  5,  9,  9,  9,  9,  9,  9};
10062e5b6d6dSopenharmony_ci        int32_t prevMap[]  =        {   0,  0,  0,  0,  0,  2,  2,  2,  2,  5,  5};
10072e5b6d6dSopenharmony_ci        UChar32  c32Map[] =    {0x201, 0x201, 0x1083, 0x1083, 0x1083, 0x044146, 0x044146, 0x044146, 0x044146, -1, -1};
10082e5b6d6dSopenharmony_ci        UChar32  pr32Map[] =   {    -1,   -1,  0x201,  0x201,  0x201,   0x1083,   0x1083,   0x1083,   0x1083, 0x044146, 0x044146};
10092e5b6d6dSopenharmony_ci
10102e5b6d6dSopenharmony_ci        // extractLen is the size, in UChars, of what will be extracted between index and index+1.
10112e5b6d6dSopenharmony_ci        //  is zero when both index positions lie within the same code point.
10122e5b6d6dSopenharmony_ci        int32_t  exLen[] =          {   0,  1,   0,  0,  1,  0,  0,  0,  2,  0,  0};
10132e5b6d6dSopenharmony_ci
10142e5b6d6dSopenharmony_ci
10152e5b6d6dSopenharmony_ci        UErrorCode status = U_ZERO_ERROR;
10162e5b6d6dSopenharmony_ci        UText *ut = utext_openUTF8(NULL, u8str, -1, &status);
10172e5b6d6dSopenharmony_ci        TEST_SUCCESS(status);
10182e5b6d6dSopenharmony_ci
10192e5b6d6dSopenharmony_ci        // Check setIndex
10202e5b6d6dSopenharmony_ci        int32_t i;
10212e5b6d6dSopenharmony_ci        int32_t startMapLimit = UPRV_LENGTHOF(startMap);
10222e5b6d6dSopenharmony_ci        for (i=0; i<startMapLimit; i++) {
10232e5b6d6dSopenharmony_ci            utext_setNativeIndex(ut, i);
10242e5b6d6dSopenharmony_ci            int64_t cpIndex = utext_getNativeIndex(ut);
10252e5b6d6dSopenharmony_ci            TEST_ASSERT(cpIndex == startMap[i]);
10262e5b6d6dSopenharmony_ci            cpIndex = UTEXT_GETNATIVEINDEX(ut);
10272e5b6d6dSopenharmony_ci            TEST_ASSERT(cpIndex == startMap[i]);
10282e5b6d6dSopenharmony_ci        }
10292e5b6d6dSopenharmony_ci
10302e5b6d6dSopenharmony_ci        // Check char32At
10312e5b6d6dSopenharmony_ci        for (i=0; i<startMapLimit; i++) {
10322e5b6d6dSopenharmony_ci            UChar32 c32 = utext_char32At(ut, i);
10332e5b6d6dSopenharmony_ci            TEST_ASSERT(c32 == c32Map[i]);
10342e5b6d6dSopenharmony_ci            int64_t cpIndex = utext_getNativeIndex(ut);
10352e5b6d6dSopenharmony_ci            TEST_ASSERT(cpIndex == startMap[i]);
10362e5b6d6dSopenharmony_ci        }
10372e5b6d6dSopenharmony_ci
10382e5b6d6dSopenharmony_ci        // Check utext_next32From
10392e5b6d6dSopenharmony_ci        for (i=0; i<startMapLimit; i++) {
10402e5b6d6dSopenharmony_ci            UChar32 c32 = utext_next32From(ut, i);
10412e5b6d6dSopenharmony_ci            TEST_ASSERT(c32 == c32Map[i]);
10422e5b6d6dSopenharmony_ci            int64_t cpIndex = utext_getNativeIndex(ut);
10432e5b6d6dSopenharmony_ci            TEST_ASSERT(cpIndex == nextMap[i]);
10442e5b6d6dSopenharmony_ci        }
10452e5b6d6dSopenharmony_ci
10462e5b6d6dSopenharmony_ci        // check utext_previous32From
10472e5b6d6dSopenharmony_ci        for (i=0; i<startMapLimit; i++) {
10482e5b6d6dSopenharmony_ci            gTestNum++;
10492e5b6d6dSopenharmony_ci            UChar32 c32 = utext_previous32From(ut, i);
10502e5b6d6dSopenharmony_ci            TEST_ASSERT(c32 == pr32Map[i]);
10512e5b6d6dSopenharmony_ci            int64_t cpIndex = utext_getNativeIndex(ut);
10522e5b6d6dSopenharmony_ci            TEST_ASSERT(cpIndex == prevMap[i]);
10532e5b6d6dSopenharmony_ci        }
10542e5b6d6dSopenharmony_ci
10552e5b6d6dSopenharmony_ci        // check Extract
10562e5b6d6dSopenharmony_ci        //   Extract from i to i+1, which may be zero or one code points,
10572e5b6d6dSopenharmony_ci        //     depending on whether the indices straddle a cp boundary.
10582e5b6d6dSopenharmony_ci        for (i=0; i<startMapLimit; i++) {
10592e5b6d6dSopenharmony_ci            UChar buf[3];
10602e5b6d6dSopenharmony_ci            status = U_ZERO_ERROR;
10612e5b6d6dSopenharmony_ci            int32_t  extractedLen = utext_extract(ut, i, i+1, buf, 3, &status);
10622e5b6d6dSopenharmony_ci            TEST_SUCCESS(status);
10632e5b6d6dSopenharmony_ci            TEST_ASSERT(extractedLen == exLen[i]);
10642e5b6d6dSopenharmony_ci            if (extractedLen > 0) {
10652e5b6d6dSopenharmony_ci                UChar32  c32;
10662e5b6d6dSopenharmony_ci                /* extractedLen-extractedLen == 0 is used to get around a compiler warning. */
10672e5b6d6dSopenharmony_ci                U16_GET(buf, 0, extractedLen-extractedLen, extractedLen, c32);
10682e5b6d6dSopenharmony_ci                TEST_ASSERT(c32 == c32Map[i]);
10692e5b6d6dSopenharmony_ci            }
10702e5b6d6dSopenharmony_ci        }
10712e5b6d6dSopenharmony_ci
10722e5b6d6dSopenharmony_ci        utext_close(ut);
10732e5b6d6dSopenharmony_ci    }
10742e5b6d6dSopenharmony_ci
10752e5b6d6dSopenharmony_ci
10762e5b6d6dSopenharmony_ci    {    //  Similar test, with utf16 instead of utf8
10772e5b6d6dSopenharmony_ci         //  TODO:  merge the common parts of these tests.
10782e5b6d6dSopenharmony_ci
10792e5b6d6dSopenharmony_ci        UnicodeString u16str("\\u1000\\U00011000\\u2000\\U00022000", -1, US_INV);
10802e5b6d6dSopenharmony_ci        int32_t startMap[]  ={ 0,     1,   1,    3,     4,  4,     6,  6};
10812e5b6d6dSopenharmony_ci        int32_t nextMap[]  = { 1,     3,   3,    4,     6,  6,     6,  6};
10822e5b6d6dSopenharmony_ci        int32_t prevMap[]  = { 0,     0,   0,    1,     3,  3,     4,  4};
10832e5b6d6dSopenharmony_ci        UChar32  c32Map[] =  {0x1000, 0x11000, 0x11000, 0x2000,  0x22000, 0x22000, -1, -1};
10842e5b6d6dSopenharmony_ci        UChar32  pr32Map[] = {    -1, 0x1000,  0x1000,  0x11000, 0x2000,  0x2000,   0x22000,   0x22000};
10852e5b6d6dSopenharmony_ci        int32_t  exLen[] =   {   1,  0,   2,  1,  0,  2,  0,  0,};
10862e5b6d6dSopenharmony_ci
10872e5b6d6dSopenharmony_ci        u16str = u16str.unescape();
10882e5b6d6dSopenharmony_ci        UErrorCode status = U_ZERO_ERROR;
10892e5b6d6dSopenharmony_ci        UText *ut = utext_openUnicodeString(NULL, &u16str, &status);
10902e5b6d6dSopenharmony_ci        TEST_SUCCESS(status);
10912e5b6d6dSopenharmony_ci
10922e5b6d6dSopenharmony_ci        int32_t startMapLimit = UPRV_LENGTHOF(startMap);
10932e5b6d6dSopenharmony_ci        int i;
10942e5b6d6dSopenharmony_ci        for (i=0; i<startMapLimit; i++) {
10952e5b6d6dSopenharmony_ci            utext_setNativeIndex(ut, i);
10962e5b6d6dSopenharmony_ci            int64_t cpIndex = utext_getNativeIndex(ut);
10972e5b6d6dSopenharmony_ci            TEST_ASSERT(cpIndex == startMap[i]);
10982e5b6d6dSopenharmony_ci        }
10992e5b6d6dSopenharmony_ci
11002e5b6d6dSopenharmony_ci        // Check char32At
11012e5b6d6dSopenharmony_ci        for (i=0; i<startMapLimit; i++) {
11022e5b6d6dSopenharmony_ci            UChar32 c32 = utext_char32At(ut, i);
11032e5b6d6dSopenharmony_ci            TEST_ASSERT(c32 == c32Map[i]);
11042e5b6d6dSopenharmony_ci            int64_t cpIndex = utext_getNativeIndex(ut);
11052e5b6d6dSopenharmony_ci            TEST_ASSERT(cpIndex == startMap[i]);
11062e5b6d6dSopenharmony_ci        }
11072e5b6d6dSopenharmony_ci
11082e5b6d6dSopenharmony_ci        // Check utext_next32From
11092e5b6d6dSopenharmony_ci        for (i=0; i<startMapLimit; i++) {
11102e5b6d6dSopenharmony_ci            UChar32 c32 = utext_next32From(ut, i);
11112e5b6d6dSopenharmony_ci            TEST_ASSERT(c32 == c32Map[i]);
11122e5b6d6dSopenharmony_ci            int64_t cpIndex = utext_getNativeIndex(ut);
11132e5b6d6dSopenharmony_ci            TEST_ASSERT(cpIndex == nextMap[i]);
11142e5b6d6dSopenharmony_ci        }
11152e5b6d6dSopenharmony_ci
11162e5b6d6dSopenharmony_ci        // check utext_previous32From
11172e5b6d6dSopenharmony_ci        for (i=0; i<startMapLimit; i++) {
11182e5b6d6dSopenharmony_ci            UChar32 c32 = utext_previous32From(ut, i);
11192e5b6d6dSopenharmony_ci            TEST_ASSERT(c32 == pr32Map[i]);
11202e5b6d6dSopenharmony_ci            int64_t cpIndex = utext_getNativeIndex(ut);
11212e5b6d6dSopenharmony_ci            TEST_ASSERT(cpIndex == prevMap[i]);
11222e5b6d6dSopenharmony_ci        }
11232e5b6d6dSopenharmony_ci
11242e5b6d6dSopenharmony_ci        // check Extract
11252e5b6d6dSopenharmony_ci        //   Extract from i to i+1, which may be zero or one code points,
11262e5b6d6dSopenharmony_ci        //     depending on whether the indices straddle a cp boundary.
11272e5b6d6dSopenharmony_ci        for (i=0; i<startMapLimit; i++) {
11282e5b6d6dSopenharmony_ci            UChar buf[3];
11292e5b6d6dSopenharmony_ci            status = U_ZERO_ERROR;
11302e5b6d6dSopenharmony_ci            int32_t  extractedLen = utext_extract(ut, i, i+1, buf, 3, &status);
11312e5b6d6dSopenharmony_ci            TEST_SUCCESS(status);
11322e5b6d6dSopenharmony_ci            TEST_ASSERT(extractedLen == exLen[i]);
11332e5b6d6dSopenharmony_ci            if (extractedLen > 0) {
11342e5b6d6dSopenharmony_ci                UChar32  c32;
11352e5b6d6dSopenharmony_ci                /* extractedLen-extractedLen == 0 is used to get around a compiler warning. */
11362e5b6d6dSopenharmony_ci                U16_GET(buf, 0, extractedLen-extractedLen, extractedLen, c32);
11372e5b6d6dSopenharmony_ci                TEST_ASSERT(c32 == c32Map[i]);
11382e5b6d6dSopenharmony_ci            }
11392e5b6d6dSopenharmony_ci        }
11402e5b6d6dSopenharmony_ci
11412e5b6d6dSopenharmony_ci        utext_close(ut);
11422e5b6d6dSopenharmony_ci    }
11432e5b6d6dSopenharmony_ci
11442e5b6d6dSopenharmony_ci    {    //  Similar test, with UText over Replaceable
11452e5b6d6dSopenharmony_ci         //  TODO:  merge the common parts of these tests.
11462e5b6d6dSopenharmony_ci
11472e5b6d6dSopenharmony_ci        UnicodeString u16str("\\u1000\\U00011000\\u2000\\U00022000", -1, US_INV);
11482e5b6d6dSopenharmony_ci        int32_t startMap[]  ={ 0,     1,   1,    3,     4,  4,     6,  6};
11492e5b6d6dSopenharmony_ci        int32_t nextMap[]  = { 1,     3,   3,    4,     6,  6,     6,  6};
11502e5b6d6dSopenharmony_ci        int32_t prevMap[]  = { 0,     0,   0,    1,     3,  3,     4,  4};
11512e5b6d6dSopenharmony_ci        UChar32  c32Map[] =  {0x1000, 0x11000, 0x11000, 0x2000,  0x22000, 0x22000, -1, -1};
11522e5b6d6dSopenharmony_ci        UChar32  pr32Map[] = {    -1, 0x1000,  0x1000,  0x11000, 0x2000,  0x2000,   0x22000,   0x22000};
11532e5b6d6dSopenharmony_ci        int32_t  exLen[] =   {   1,  0,   2,  1,  0,  2,  0,  0,};
11542e5b6d6dSopenharmony_ci
11552e5b6d6dSopenharmony_ci        u16str = u16str.unescape();
11562e5b6d6dSopenharmony_ci        UErrorCode status = U_ZERO_ERROR;
11572e5b6d6dSopenharmony_ci        UText *ut = utext_openReplaceable(NULL, &u16str, &status);
11582e5b6d6dSopenharmony_ci        TEST_SUCCESS(status);
11592e5b6d6dSopenharmony_ci
11602e5b6d6dSopenharmony_ci        int32_t startMapLimit = UPRV_LENGTHOF(startMap);
11612e5b6d6dSopenharmony_ci        int i;
11622e5b6d6dSopenharmony_ci        for (i=0; i<startMapLimit; i++) {
11632e5b6d6dSopenharmony_ci            utext_setNativeIndex(ut, i);
11642e5b6d6dSopenharmony_ci            int64_t cpIndex = utext_getNativeIndex(ut);
11652e5b6d6dSopenharmony_ci            TEST_ASSERT(cpIndex == startMap[i]);
11662e5b6d6dSopenharmony_ci        }
11672e5b6d6dSopenharmony_ci
11682e5b6d6dSopenharmony_ci        // Check char32At
11692e5b6d6dSopenharmony_ci        for (i=0; i<startMapLimit; i++) {
11702e5b6d6dSopenharmony_ci            UChar32 c32 = utext_char32At(ut, i);
11712e5b6d6dSopenharmony_ci            TEST_ASSERT(c32 == c32Map[i]);
11722e5b6d6dSopenharmony_ci            int64_t cpIndex = utext_getNativeIndex(ut);
11732e5b6d6dSopenharmony_ci            TEST_ASSERT(cpIndex == startMap[i]);
11742e5b6d6dSopenharmony_ci        }
11752e5b6d6dSopenharmony_ci
11762e5b6d6dSopenharmony_ci        // Check utext_next32From
11772e5b6d6dSopenharmony_ci        for (i=0; i<startMapLimit; i++) {
11782e5b6d6dSopenharmony_ci            UChar32 c32 = utext_next32From(ut, i);
11792e5b6d6dSopenharmony_ci            TEST_ASSERT(c32 == c32Map[i]);
11802e5b6d6dSopenharmony_ci            int64_t cpIndex = utext_getNativeIndex(ut);
11812e5b6d6dSopenharmony_ci            TEST_ASSERT(cpIndex == nextMap[i]);
11822e5b6d6dSopenharmony_ci        }
11832e5b6d6dSopenharmony_ci
11842e5b6d6dSopenharmony_ci        // check utext_previous32From
11852e5b6d6dSopenharmony_ci        for (i=0; i<startMapLimit; i++) {
11862e5b6d6dSopenharmony_ci            UChar32 c32 = utext_previous32From(ut, i);
11872e5b6d6dSopenharmony_ci            TEST_ASSERT(c32 == pr32Map[i]);
11882e5b6d6dSopenharmony_ci            int64_t cpIndex = utext_getNativeIndex(ut);
11892e5b6d6dSopenharmony_ci            TEST_ASSERT(cpIndex == prevMap[i]);
11902e5b6d6dSopenharmony_ci        }
11912e5b6d6dSopenharmony_ci
11922e5b6d6dSopenharmony_ci        // check Extract
11932e5b6d6dSopenharmony_ci        //   Extract from i to i+1, which may be zero or one code points,
11942e5b6d6dSopenharmony_ci        //     depending on whether the indices straddle a cp boundary.
11952e5b6d6dSopenharmony_ci        for (i=0; i<startMapLimit; i++) {
11962e5b6d6dSopenharmony_ci            UChar buf[3];
11972e5b6d6dSopenharmony_ci            status = U_ZERO_ERROR;
11982e5b6d6dSopenharmony_ci            int32_t  extractedLen = utext_extract(ut, i, i+1, buf, 3, &status);
11992e5b6d6dSopenharmony_ci            TEST_SUCCESS(status);
12002e5b6d6dSopenharmony_ci            TEST_ASSERT(extractedLen == exLen[i]);
12012e5b6d6dSopenharmony_ci            if (extractedLen > 0) {
12022e5b6d6dSopenharmony_ci                UChar32  c32;
12032e5b6d6dSopenharmony_ci                /* extractedLen-extractedLen == 0 is used to get around a compiler warning. */
12042e5b6d6dSopenharmony_ci                U16_GET(buf, 0, extractedLen-extractedLen, extractedLen, c32);
12052e5b6d6dSopenharmony_ci                TEST_ASSERT(c32 == c32Map[i]);
12062e5b6d6dSopenharmony_ci            }
12072e5b6d6dSopenharmony_ci        }
12082e5b6d6dSopenharmony_ci
12092e5b6d6dSopenharmony_ci        utext_close(ut);
12102e5b6d6dSopenharmony_ci    }
12112e5b6d6dSopenharmony_ci}
12122e5b6d6dSopenharmony_ci
12132e5b6d6dSopenharmony_ci
12142e5b6d6dSopenharmony_civoid UTextTest::FreezeTest() {
12152e5b6d6dSopenharmony_ci    // Check isWritable() and freeze() behavior.
12162e5b6d6dSopenharmony_ci    //
12172e5b6d6dSopenharmony_ci
12182e5b6d6dSopenharmony_ci    UnicodeString  ustr("Hello, World.");
12192e5b6d6dSopenharmony_ci    const char u8str[] = {char(0x31), (char)0x32, (char)0x33, 0};
12202e5b6d6dSopenharmony_ci    const UChar u16str[] = {(UChar)0x31, (UChar)0x32, (UChar)0x44, 0};
12212e5b6d6dSopenharmony_ci
12222e5b6d6dSopenharmony_ci    UErrorCode status = U_ZERO_ERROR;
12232e5b6d6dSopenharmony_ci    UText  *ut        = NULL;
12242e5b6d6dSopenharmony_ci    UText  *ut2       = NULL;
12252e5b6d6dSopenharmony_ci
12262e5b6d6dSopenharmony_ci    ut = utext_openUTF8(ut, u8str, -1, &status);
12272e5b6d6dSopenharmony_ci    TEST_SUCCESS(status);
12282e5b6d6dSopenharmony_ci    UBool writable = utext_isWritable(ut);
12292e5b6d6dSopenharmony_ci    TEST_ASSERT(writable == false);
12302e5b6d6dSopenharmony_ci    utext_copy(ut, 1, 2, 0, true, &status);
12312e5b6d6dSopenharmony_ci    TEST_ASSERT(status == U_NO_WRITE_PERMISSION);
12322e5b6d6dSopenharmony_ci
12332e5b6d6dSopenharmony_ci    status = U_ZERO_ERROR;
12342e5b6d6dSopenharmony_ci    ut = utext_openUChars(ut, u16str, -1, &status);
12352e5b6d6dSopenharmony_ci    TEST_SUCCESS(status);
12362e5b6d6dSopenharmony_ci    writable = utext_isWritable(ut);
12372e5b6d6dSopenharmony_ci    TEST_ASSERT(writable == false);
12382e5b6d6dSopenharmony_ci    utext_copy(ut, 1, 2, 0, true, &status);
12392e5b6d6dSopenharmony_ci    TEST_ASSERT(status == U_NO_WRITE_PERMISSION);
12402e5b6d6dSopenharmony_ci
12412e5b6d6dSopenharmony_ci    status = U_ZERO_ERROR;
12422e5b6d6dSopenharmony_ci    ut = utext_openUnicodeString(ut, &ustr, &status);
12432e5b6d6dSopenharmony_ci    TEST_SUCCESS(status);
12442e5b6d6dSopenharmony_ci    writable = utext_isWritable(ut);
12452e5b6d6dSopenharmony_ci    TEST_ASSERT(writable == true);
12462e5b6d6dSopenharmony_ci    utext_freeze(ut);
12472e5b6d6dSopenharmony_ci    writable = utext_isWritable(ut);
12482e5b6d6dSopenharmony_ci    TEST_ASSERT(writable == false);
12492e5b6d6dSopenharmony_ci    utext_copy(ut, 1, 2, 0, true, &status);
12502e5b6d6dSopenharmony_ci    TEST_ASSERT(status == U_NO_WRITE_PERMISSION);
12512e5b6d6dSopenharmony_ci
12522e5b6d6dSopenharmony_ci    status = U_ZERO_ERROR;
12532e5b6d6dSopenharmony_ci    ut = utext_openUnicodeString(ut, &ustr, &status);
12542e5b6d6dSopenharmony_ci    TEST_SUCCESS(status);
12552e5b6d6dSopenharmony_ci    ut2 = utext_clone(ut2, ut, false, false, &status);  // clone with readonly = false
12562e5b6d6dSopenharmony_ci    TEST_SUCCESS(status);
12572e5b6d6dSopenharmony_ci    writable = utext_isWritable(ut2);
12582e5b6d6dSopenharmony_ci    TEST_ASSERT(writable == true);
12592e5b6d6dSopenharmony_ci    ut2 = utext_clone(ut2, ut, false, true, &status);  // clone with readonly = true
12602e5b6d6dSopenharmony_ci    TEST_SUCCESS(status);
12612e5b6d6dSopenharmony_ci    writable = utext_isWritable(ut2);
12622e5b6d6dSopenharmony_ci    TEST_ASSERT(writable == false);
12632e5b6d6dSopenharmony_ci    utext_copy(ut2, 1, 2, 0, true, &status);
12642e5b6d6dSopenharmony_ci    TEST_ASSERT(status == U_NO_WRITE_PERMISSION);
12652e5b6d6dSopenharmony_ci
12662e5b6d6dSopenharmony_ci    status = U_ZERO_ERROR;
12672e5b6d6dSopenharmony_ci    ut = utext_openConstUnicodeString(ut, (const UnicodeString *)&ustr, &status);
12682e5b6d6dSopenharmony_ci    TEST_SUCCESS(status);
12692e5b6d6dSopenharmony_ci    writable = utext_isWritable(ut);
12702e5b6d6dSopenharmony_ci    TEST_ASSERT(writable == false);
12712e5b6d6dSopenharmony_ci    utext_copy(ut, 1, 2, 0, true, &status);
12722e5b6d6dSopenharmony_ci    TEST_ASSERT(status == U_NO_WRITE_PERMISSION);
12732e5b6d6dSopenharmony_ci
12742e5b6d6dSopenharmony_ci    // Deep Clone of a frozen UText should re-enable writing in the copy.
12752e5b6d6dSopenharmony_ci    status = U_ZERO_ERROR;
12762e5b6d6dSopenharmony_ci    ut = utext_openUnicodeString(ut, &ustr, &status);
12772e5b6d6dSopenharmony_ci    TEST_SUCCESS(status);
12782e5b6d6dSopenharmony_ci    utext_freeze(ut);
12792e5b6d6dSopenharmony_ci    ut2 = utext_clone(ut2, ut, true, false, &status);   // deep clone
12802e5b6d6dSopenharmony_ci    TEST_SUCCESS(status);
12812e5b6d6dSopenharmony_ci    writable = utext_isWritable(ut2);
12822e5b6d6dSopenharmony_ci    TEST_ASSERT(writable == true);
12832e5b6d6dSopenharmony_ci
12842e5b6d6dSopenharmony_ci
12852e5b6d6dSopenharmony_ci    // Deep clone of a frozen UText, where the base type is intrinsically non-writable,
12862e5b6d6dSopenharmony_ci    //  should NOT enable writing in the copy.
12872e5b6d6dSopenharmony_ci    status = U_ZERO_ERROR;
12882e5b6d6dSopenharmony_ci    ut = utext_openUChars(ut, u16str, -1, &status);
12892e5b6d6dSopenharmony_ci    TEST_SUCCESS(status);
12902e5b6d6dSopenharmony_ci    utext_freeze(ut);
12912e5b6d6dSopenharmony_ci    ut2 = utext_clone(ut2, ut, true, false, &status);   // deep clone
12922e5b6d6dSopenharmony_ci    TEST_SUCCESS(status);
12932e5b6d6dSopenharmony_ci    writable = utext_isWritable(ut2);
12942e5b6d6dSopenharmony_ci    TEST_ASSERT(writable == false);
12952e5b6d6dSopenharmony_ci
12962e5b6d6dSopenharmony_ci    // cleanup
12972e5b6d6dSopenharmony_ci    utext_close(ut);
12982e5b6d6dSopenharmony_ci    utext_close(ut2);
12992e5b6d6dSopenharmony_ci}
13002e5b6d6dSopenharmony_ci
13012e5b6d6dSopenharmony_ci
13022e5b6d6dSopenharmony_ci//
13032e5b6d6dSopenharmony_ci//  Fragmented UText
13042e5b6d6dSopenharmony_ci//      A UText type that works with a chunk size of 1.
13052e5b6d6dSopenharmony_ci//      Intended to test for edge cases.
13062e5b6d6dSopenharmony_ci//      Input comes from a UnicodeString.
13072e5b6d6dSopenharmony_ci//
13082e5b6d6dSopenharmony_ci//       ut.b    the character.  Put into both halves.
13092e5b6d6dSopenharmony_ci//
13102e5b6d6dSopenharmony_ci
13112e5b6d6dSopenharmony_ciU_CDECL_BEGIN
13122e5b6d6dSopenharmony_cistatic UBool U_CALLCONV
13132e5b6d6dSopenharmony_cifragTextAccess(UText *ut, int64_t index, UBool forward) {
13142e5b6d6dSopenharmony_ci    const UnicodeString *us = (const UnicodeString *)ut->context;
13152e5b6d6dSopenharmony_ci    UChar  c;
13162e5b6d6dSopenharmony_ci    int32_t length = us->length();
13172e5b6d6dSopenharmony_ci    if (forward && index>=0 && index<length) {
13182e5b6d6dSopenharmony_ci        c = us->charAt((int32_t)index);
13192e5b6d6dSopenharmony_ci        ut->b = c | c<<16;
13202e5b6d6dSopenharmony_ci        ut->chunkOffset = 0;
13212e5b6d6dSopenharmony_ci        ut->chunkLength = 1;
13222e5b6d6dSopenharmony_ci        ut->chunkNativeStart = index;
13232e5b6d6dSopenharmony_ci        ut->chunkNativeLimit = index+1;
13242e5b6d6dSopenharmony_ci        return true;
13252e5b6d6dSopenharmony_ci    }
13262e5b6d6dSopenharmony_ci    if (!forward && index>0 && index <=length) {
13272e5b6d6dSopenharmony_ci        c = us->charAt((int32_t)index-1);
13282e5b6d6dSopenharmony_ci        ut->b = c | c<<16;
13292e5b6d6dSopenharmony_ci        ut->chunkOffset = 1;
13302e5b6d6dSopenharmony_ci        ut->chunkLength = 1;
13312e5b6d6dSopenharmony_ci        ut->chunkNativeStart = index-1;
13322e5b6d6dSopenharmony_ci        ut->chunkNativeLimit = index;
13332e5b6d6dSopenharmony_ci        return true;
13342e5b6d6dSopenharmony_ci    }
13352e5b6d6dSopenharmony_ci    ut->b = 0;
13362e5b6d6dSopenharmony_ci    ut->chunkOffset = 0;
13372e5b6d6dSopenharmony_ci    ut->chunkLength = 0;
13382e5b6d6dSopenharmony_ci    if (index <= 0) {
13392e5b6d6dSopenharmony_ci        ut->chunkNativeStart = 0;
13402e5b6d6dSopenharmony_ci        ut->chunkNativeLimit = 0;
13412e5b6d6dSopenharmony_ci    } else {
13422e5b6d6dSopenharmony_ci        ut->chunkNativeStart = length;
13432e5b6d6dSopenharmony_ci        ut->chunkNativeLimit = length;
13442e5b6d6dSopenharmony_ci    }
13452e5b6d6dSopenharmony_ci    return false;
13462e5b6d6dSopenharmony_ci}
13472e5b6d6dSopenharmony_ci
13482e5b6d6dSopenharmony_ci// Function table to be used with this fragmented text provider.
13492e5b6d6dSopenharmony_ci//   Initialized in the open function.
13502e5b6d6dSopenharmony_cistatic UTextFuncs  fragmentFuncs;
13512e5b6d6dSopenharmony_ci
13522e5b6d6dSopenharmony_ci// Clone function for fragmented text provider.
13532e5b6d6dSopenharmony_ci//   Didn't really want to provide this, but it's easier to provide it than to keep it
13542e5b6d6dSopenharmony_ci//   out of the tests.
13552e5b6d6dSopenharmony_ci//
13562e5b6d6dSopenharmony_ciUText *
13572e5b6d6dSopenharmony_cicloneFragmentedUnicodeString(UText *dest, const UText *src, UBool deep, UErrorCode *status) {
13582e5b6d6dSopenharmony_ci    if (U_FAILURE(*status)) {
13592e5b6d6dSopenharmony_ci        return NULL;
13602e5b6d6dSopenharmony_ci    }
13612e5b6d6dSopenharmony_ci    if (deep) {
13622e5b6d6dSopenharmony_ci        *status = U_UNSUPPORTED_ERROR;
13632e5b6d6dSopenharmony_ci        return NULL;
13642e5b6d6dSopenharmony_ci    }
13652e5b6d6dSopenharmony_ci    dest = utext_openUnicodeString(dest, (UnicodeString *)src->context, status);
13662e5b6d6dSopenharmony_ci    utext_setNativeIndex(dest, utext_getNativeIndex(src));
13672e5b6d6dSopenharmony_ci    return dest;
13682e5b6d6dSopenharmony_ci}
13692e5b6d6dSopenharmony_ci
13702e5b6d6dSopenharmony_ciU_CDECL_END
13712e5b6d6dSopenharmony_ci
13722e5b6d6dSopenharmony_ci// Open function for the fragmented text provider.
13732e5b6d6dSopenharmony_ciUText *
13742e5b6d6dSopenharmony_ciopenFragmentedUnicodeString(UText *ut, UnicodeString *s, UErrorCode *status) {
13752e5b6d6dSopenharmony_ci    ut = utext_openUnicodeString(ut, s, status);
13762e5b6d6dSopenharmony_ci    if (U_FAILURE(*status)) {
13772e5b6d6dSopenharmony_ci        return ut;
13782e5b6d6dSopenharmony_ci    }
13792e5b6d6dSopenharmony_ci
13802e5b6d6dSopenharmony_ci    // Copy of the function table from the stock UnicodeString UText,
13812e5b6d6dSopenharmony_ci    //   and replace the entry for the access function.
13822e5b6d6dSopenharmony_ci    memcpy(&fragmentFuncs, ut->pFuncs, sizeof(fragmentFuncs));
13832e5b6d6dSopenharmony_ci    fragmentFuncs.access = fragTextAccess;
13842e5b6d6dSopenharmony_ci    fragmentFuncs.clone  = cloneFragmentedUnicodeString;
13852e5b6d6dSopenharmony_ci    ut->pFuncs = &fragmentFuncs;
13862e5b6d6dSopenharmony_ci
13872e5b6d6dSopenharmony_ci    ut->chunkContents = (UChar *)&ut->b;
13882e5b6d6dSopenharmony_ci    ut->pFuncs->access(ut, 0, true);
13892e5b6d6dSopenharmony_ci    return ut;
13902e5b6d6dSopenharmony_ci}
13912e5b6d6dSopenharmony_ci
13922e5b6d6dSopenharmony_ci// Regression test for Ticket 5560
13932e5b6d6dSopenharmony_ci//   Clone fails to update chunkContentPointer in the cloned copy.
13942e5b6d6dSopenharmony_ci//   This is only an issue for UText types that work in a local buffer,
13952e5b6d6dSopenharmony_ci//      (UTF-8 wrapper, for example)
13962e5b6d6dSopenharmony_ci//
13972e5b6d6dSopenharmony_ci//   The test:
13982e5b6d6dSopenharmony_ci//     1.  Create an initial UText
13992e5b6d6dSopenharmony_ci//     2.  Deep clone it.  Contents should match original.
14002e5b6d6dSopenharmony_ci//     3.  Reset original to something different.
14012e5b6d6dSopenharmony_ci//     4.  Check that clone contents did not change.
14022e5b6d6dSopenharmony_ci//
14032e5b6d6dSopenharmony_civoid UTextTest::Ticket5560() {
14042e5b6d6dSopenharmony_ci    /* The following two strings are in UTF-8 even on EBCDIC platforms. */
14052e5b6d6dSopenharmony_ci    static const char s1[] = {0x41,0x42,0x43,0x44,0x45,0x46,0}; /* "ABCDEF" */
14062e5b6d6dSopenharmony_ci    static const char s2[] = {0x31,0x32,0x33,0x34,0x35,0x36,0}; /* "123456" */
14072e5b6d6dSopenharmony_ci	UErrorCode status = U_ZERO_ERROR;
14082e5b6d6dSopenharmony_ci
14092e5b6d6dSopenharmony_ci	UText ut1 = UTEXT_INITIALIZER;
14102e5b6d6dSopenharmony_ci	UText ut2 = UTEXT_INITIALIZER;
14112e5b6d6dSopenharmony_ci
14122e5b6d6dSopenharmony_ci	utext_openUTF8(&ut1, s1, -1, &status);
14132e5b6d6dSopenharmony_ci	UChar c = utext_next32(&ut1);
14142e5b6d6dSopenharmony_ci	TEST_ASSERT(c == 0x41);  // c == 'A'
14152e5b6d6dSopenharmony_ci
14162e5b6d6dSopenharmony_ci	utext_clone(&ut2, &ut1, true, false, &status);
14172e5b6d6dSopenharmony_ci	TEST_SUCCESS(status);
14182e5b6d6dSopenharmony_ci    c = utext_next32(&ut2);
14192e5b6d6dSopenharmony_ci	TEST_ASSERT(c == 0x42);  // c == 'B'
14202e5b6d6dSopenharmony_ci    c = utext_next32(&ut1);
14212e5b6d6dSopenharmony_ci	TEST_ASSERT(c == 0x42);  // c == 'B'
14222e5b6d6dSopenharmony_ci
14232e5b6d6dSopenharmony_ci	utext_openUTF8(&ut1, s2, -1, &status);
14242e5b6d6dSopenharmony_ci	c = utext_next32(&ut1);
14252e5b6d6dSopenharmony_ci	TEST_ASSERT(c == 0x31);  // c == '1'
14262e5b6d6dSopenharmony_ci    c = utext_next32(&ut2);
14272e5b6d6dSopenharmony_ci	TEST_ASSERT(c == 0x43);  // c == 'C'
14282e5b6d6dSopenharmony_ci
14292e5b6d6dSopenharmony_ci    utext_close(&ut1);
14302e5b6d6dSopenharmony_ci    utext_close(&ut2);
14312e5b6d6dSopenharmony_ci}
14322e5b6d6dSopenharmony_ci
14332e5b6d6dSopenharmony_ci
14342e5b6d6dSopenharmony_ci// Test for Ticket 6847
14352e5b6d6dSopenharmony_ci//
14362e5b6d6dSopenharmony_civoid UTextTest::Ticket6847() {
14372e5b6d6dSopenharmony_ci    const int STRLEN = 90;
14382e5b6d6dSopenharmony_ci    UChar s[STRLEN+1];
14392e5b6d6dSopenharmony_ci    u_memset(s, 0x41, STRLEN);
14402e5b6d6dSopenharmony_ci    s[STRLEN] = 0;
14412e5b6d6dSopenharmony_ci
14422e5b6d6dSopenharmony_ci    UErrorCode status = U_ZERO_ERROR;
14432e5b6d6dSopenharmony_ci    UText *ut = utext_openUChars(NULL, s, -1, &status);
14442e5b6d6dSopenharmony_ci
14452e5b6d6dSopenharmony_ci    utext_setNativeIndex(ut, 0);
14462e5b6d6dSopenharmony_ci    int32_t count = 0;
14472e5b6d6dSopenharmony_ci    UChar32 c = 0;
14482e5b6d6dSopenharmony_ci    int64_t nativeIndex = UTEXT_GETNATIVEINDEX(ut);
14492e5b6d6dSopenharmony_ci    TEST_ASSERT(nativeIndex == 0);
14502e5b6d6dSopenharmony_ci    while ((c = utext_next32(ut)) != U_SENTINEL) {
14512e5b6d6dSopenharmony_ci        TEST_ASSERT(c == 0x41);
14522e5b6d6dSopenharmony_ci        TEST_ASSERT(count < STRLEN);
14532e5b6d6dSopenharmony_ci        if (count >= STRLEN) {
14542e5b6d6dSopenharmony_ci            break;
14552e5b6d6dSopenharmony_ci        }
14562e5b6d6dSopenharmony_ci        count++;
14572e5b6d6dSopenharmony_ci        nativeIndex = UTEXT_GETNATIVEINDEX(ut);
14582e5b6d6dSopenharmony_ci        TEST_ASSERT(nativeIndex == count);
14592e5b6d6dSopenharmony_ci    }
14602e5b6d6dSopenharmony_ci    TEST_ASSERT(count == STRLEN);
14612e5b6d6dSopenharmony_ci    nativeIndex = UTEXT_GETNATIVEINDEX(ut);
14622e5b6d6dSopenharmony_ci    TEST_ASSERT(nativeIndex == STRLEN);
14632e5b6d6dSopenharmony_ci    utext_close(ut);
14642e5b6d6dSopenharmony_ci}
14652e5b6d6dSopenharmony_ci
14662e5b6d6dSopenharmony_ci
14672e5b6d6dSopenharmony_civoid UTextTest::Ticket10562() {
14682e5b6d6dSopenharmony_ci    // Note: failures show as a heap error when the test is run under valgrind.
14692e5b6d6dSopenharmony_ci    UErrorCode status = U_ZERO_ERROR;
14702e5b6d6dSopenharmony_ci
14712e5b6d6dSopenharmony_ci    const char *utf8_string = "\x41\x41\x41\x41\x41\x41\x41\x41\x41\x41\x41\x41\x41\x41\x41";
14722e5b6d6dSopenharmony_ci    UText *utf8Text = utext_openUTF8(NULL, utf8_string, -1, &status);
14732e5b6d6dSopenharmony_ci    TEST_SUCCESS(status);
14742e5b6d6dSopenharmony_ci    UText *deepClone = utext_clone(NULL, utf8Text, true, false, &status);
14752e5b6d6dSopenharmony_ci    TEST_SUCCESS(status);
14762e5b6d6dSopenharmony_ci    UText *shallowClone = utext_clone(NULL, deepClone, false, false, &status);
14772e5b6d6dSopenharmony_ci    TEST_SUCCESS(status);
14782e5b6d6dSopenharmony_ci    utext_close(shallowClone);
14792e5b6d6dSopenharmony_ci    utext_close(deepClone);
14802e5b6d6dSopenharmony_ci    utext_close(utf8Text);
14812e5b6d6dSopenharmony_ci
14822e5b6d6dSopenharmony_ci    status = U_ZERO_ERROR;
14832e5b6d6dSopenharmony_ci    UnicodeString usString("Hello, World.");
14842e5b6d6dSopenharmony_ci    UText *usText = utext_openUnicodeString(NULL, &usString, &status);
14852e5b6d6dSopenharmony_ci    TEST_SUCCESS(status);
14862e5b6d6dSopenharmony_ci    UText *usDeepClone = utext_clone(NULL, usText, true, false, &status);
14872e5b6d6dSopenharmony_ci    TEST_SUCCESS(status);
14882e5b6d6dSopenharmony_ci    UText *usShallowClone = utext_clone(NULL, usDeepClone, false, false, &status);
14892e5b6d6dSopenharmony_ci    TEST_SUCCESS(status);
14902e5b6d6dSopenharmony_ci    utext_close(usShallowClone);
14912e5b6d6dSopenharmony_ci    utext_close(usDeepClone);
14922e5b6d6dSopenharmony_ci    utext_close(usText);
14932e5b6d6dSopenharmony_ci}
14942e5b6d6dSopenharmony_ci
14952e5b6d6dSopenharmony_ci
14962e5b6d6dSopenharmony_civoid UTextTest::Ticket10983() {
14972e5b6d6dSopenharmony_ci    // Note: failure shows as a seg fault when the defect is present.
14982e5b6d6dSopenharmony_ci
14992e5b6d6dSopenharmony_ci    UErrorCode status = U_ZERO_ERROR;
15002e5b6d6dSopenharmony_ci    UnicodeString s("Hello, World");
15012e5b6d6dSopenharmony_ci    UText *ut = utext_openConstUnicodeString(NULL, &s, &status);
15022e5b6d6dSopenharmony_ci    TEST_SUCCESS(status);
15032e5b6d6dSopenharmony_ci
15042e5b6d6dSopenharmony_ci    status = U_INVALID_STATE_ERROR;
15052e5b6d6dSopenharmony_ci    UText *cloned = utext_clone(NULL, ut, true, true, &status);
15062e5b6d6dSopenharmony_ci    TEST_ASSERT(cloned == NULL);
15072e5b6d6dSopenharmony_ci    TEST_ASSERT(status == U_INVALID_STATE_ERROR);
15082e5b6d6dSopenharmony_ci
15092e5b6d6dSopenharmony_ci    utext_close(ut);
15102e5b6d6dSopenharmony_ci}
15112e5b6d6dSopenharmony_ci
15122e5b6d6dSopenharmony_ci// Ticket 12130 - extract on a UText wrapping a null terminated UChar * string
15132e5b6d6dSopenharmony_ci//                leaves the iteration position set incorrectly when the
15142e5b6d6dSopenharmony_ci//                actual string length is not yet known.
15152e5b6d6dSopenharmony_ci//
15162e5b6d6dSopenharmony_ci//                The test text needs to be long enough that UText defers getting the length.
15172e5b6d6dSopenharmony_ci
15182e5b6d6dSopenharmony_civoid UTextTest::Ticket12130() {
15192e5b6d6dSopenharmony_ci    UErrorCode status = U_ZERO_ERROR;
15202e5b6d6dSopenharmony_ci
15212e5b6d6dSopenharmony_ci    const char *text8 =
15222e5b6d6dSopenharmony_ci        "Fundamentally, computers just deal with numbers. They store letters and other characters "
15232e5b6d6dSopenharmony_ci        "by assigning a number for each one. Before Unicode was invented, there were hundreds "
15242e5b6d6dSopenharmony_ci        "of different encoding systems for assigning these numbers. No single encoding could "
15252e5b6d6dSopenharmony_ci        "contain enough characters: for example, the European Union alone requires several "
15262e5b6d6dSopenharmony_ci        "different encodings to cover all its languages. Even for a single language like "
15272e5b6d6dSopenharmony_ci        "English no single encoding was adequate for all the letters, punctuation, and technical "
15282e5b6d6dSopenharmony_ci        "symbols in common use.";
15292e5b6d6dSopenharmony_ci
15302e5b6d6dSopenharmony_ci    UnicodeString str(text8);
15312e5b6d6dSopenharmony_ci    const UChar *ustr = str.getTerminatedBuffer();
15322e5b6d6dSopenharmony_ci    UText ut = UTEXT_INITIALIZER;
15332e5b6d6dSopenharmony_ci    utext_openUChars(&ut, ustr, -1, &status);
15342e5b6d6dSopenharmony_ci    UChar extractBuffer[50];
15352e5b6d6dSopenharmony_ci
15362e5b6d6dSopenharmony_ci    for (int32_t startIdx = 0; startIdx<str.length(); ++startIdx) {
15372e5b6d6dSopenharmony_ci        int32_t endIdx = startIdx + 20;
15382e5b6d6dSopenharmony_ci
15392e5b6d6dSopenharmony_ci        u_memset(extractBuffer, 0, UPRV_LENGTHOF(extractBuffer));
15402e5b6d6dSopenharmony_ci        utext_extract(&ut, startIdx, endIdx, extractBuffer, UPRV_LENGTHOF(extractBuffer), &status);
15412e5b6d6dSopenharmony_ci        if (U_FAILURE(status)) {
15422e5b6d6dSopenharmony_ci            errln("%s:%d %s", __FILE__, __LINE__, u_errorName(status));
15432e5b6d6dSopenharmony_ci            return;
15442e5b6d6dSopenharmony_ci        }
15452e5b6d6dSopenharmony_ci        int64_t ni  = utext_getNativeIndex(&ut);
15462e5b6d6dSopenharmony_ci        int64_t expectedni = startIdx + 20;
15472e5b6d6dSopenharmony_ci        if (expectedni > str.length()) {
15482e5b6d6dSopenharmony_ci            expectedni = str.length();
15492e5b6d6dSopenharmony_ci        }
15502e5b6d6dSopenharmony_ci        if (expectedni != ni) {
15512e5b6d6dSopenharmony_ci            errln("%s:%d utext_getNativeIndex() expected %d, got %d", __FILE__, __LINE__, expectedni, ni);
15522e5b6d6dSopenharmony_ci        }
15532e5b6d6dSopenharmony_ci        if (0 != str.tempSubString(startIdx, 20).compare(extractBuffer)) {
15542e5b6d6dSopenharmony_ci            errln("%s:%d utext_extract() failed. expected \"%s\", got \"%s\"",
15552e5b6d6dSopenharmony_ci                    __FILE__, __LINE__, CStr(str.tempSubString(startIdx, 20))(), CStr(UnicodeString(extractBuffer))());
15562e5b6d6dSopenharmony_ci        }
15572e5b6d6dSopenharmony_ci    }
15582e5b6d6dSopenharmony_ci    utext_close(&ut);
15592e5b6d6dSopenharmony_ci
15602e5b6d6dSopenharmony_ci    // Similar utext extract, this time with the string length provided to the UText in advance,
15612e5b6d6dSopenharmony_ci    // and a buffer of larger than required capacity.
15622e5b6d6dSopenharmony_ci
15632e5b6d6dSopenharmony_ci    utext_openUChars(&ut, ustr, str.length(), &status);
15642e5b6d6dSopenharmony_ci    for (int32_t startIdx = 0; startIdx<str.length(); ++startIdx) {
15652e5b6d6dSopenharmony_ci        int32_t endIdx = startIdx + 20;
15662e5b6d6dSopenharmony_ci        u_memset(extractBuffer, 0, UPRV_LENGTHOF(extractBuffer));
15672e5b6d6dSopenharmony_ci        utext_extract(&ut, startIdx, endIdx, extractBuffer, UPRV_LENGTHOF(extractBuffer), &status);
15682e5b6d6dSopenharmony_ci        if (U_FAILURE(status)) {
15692e5b6d6dSopenharmony_ci            errln("%s:%d %s", __FILE__, __LINE__, u_errorName(status));
15702e5b6d6dSopenharmony_ci            return;
15712e5b6d6dSopenharmony_ci        }
15722e5b6d6dSopenharmony_ci        int64_t ni  = utext_getNativeIndex(&ut);
15732e5b6d6dSopenharmony_ci        int64_t expectedni = startIdx + 20;
15742e5b6d6dSopenharmony_ci        if (expectedni > str.length()) {
15752e5b6d6dSopenharmony_ci            expectedni = str.length();
15762e5b6d6dSopenharmony_ci        }
15772e5b6d6dSopenharmony_ci        if (expectedni != ni) {
15782e5b6d6dSopenharmony_ci            errln("%s:%d utext_getNativeIndex() expected %d, got %d", __FILE__, __LINE__, expectedni, ni);
15792e5b6d6dSopenharmony_ci        }
15802e5b6d6dSopenharmony_ci        if (0 != str.tempSubString(startIdx, 20).compare(extractBuffer)) {
15812e5b6d6dSopenharmony_ci            errln("%s:%d utext_extract() failed. expected \"%s\", got \"%s\"",
15822e5b6d6dSopenharmony_ci                    __FILE__, __LINE__, CStr(str.tempSubString(startIdx, 20))(), CStr(UnicodeString(extractBuffer))());
15832e5b6d6dSopenharmony_ci        }
15842e5b6d6dSopenharmony_ci    }
15852e5b6d6dSopenharmony_ci    utext_close(&ut);
15862e5b6d6dSopenharmony_ci}
15872e5b6d6dSopenharmony_ci
15882e5b6d6dSopenharmony_ci// Ticket 13344 The macro form of UTEXT_SETNATIVEINDEX failed when target was a trail surrogate
15892e5b6d6dSopenharmony_ci//              of a supplementary character.
15902e5b6d6dSopenharmony_ci
15912e5b6d6dSopenharmony_civoid UTextTest::Ticket13344() {
15922e5b6d6dSopenharmony_ci    UErrorCode status = U_ZERO_ERROR;
15932e5b6d6dSopenharmony_ci    const char16_t *str = u"abc\U0010abcd xyz";
15942e5b6d6dSopenharmony_ci    LocalUTextPointer ut(utext_openUChars(NULL, str, -1, &status));
15952e5b6d6dSopenharmony_ci
15962e5b6d6dSopenharmony_ci    assertSuccess("UTextTest::Ticket13344-status", status);
15972e5b6d6dSopenharmony_ci    UTEXT_SETNATIVEINDEX(ut.getAlias(), 3);
15982e5b6d6dSopenharmony_ci    assertEquals("UTextTest::Ticket13344-lead", (int64_t)3, utext_getNativeIndex(ut.getAlias()));
15992e5b6d6dSopenharmony_ci    UTEXT_SETNATIVEINDEX(ut.getAlias(), 4);
16002e5b6d6dSopenharmony_ci    assertEquals("UTextTest::Ticket13344-trail", (int64_t)3, utext_getNativeIndex(ut.getAlias()));
16012e5b6d6dSopenharmony_ci    UTEXT_SETNATIVEINDEX(ut.getAlias(), 5);
16022e5b6d6dSopenharmony_ci    assertEquals("UTextTest::Ticket13344-bmp", (int64_t)5, utext_getNativeIndex(ut.getAlias()));
16032e5b6d6dSopenharmony_ci
16042e5b6d6dSopenharmony_ci    utext_setNativeIndex(ut.getAlias(), 3);
16052e5b6d6dSopenharmony_ci    assertEquals("UTextTest::Ticket13344-lead-2", (int64_t)3, utext_getNativeIndex(ut.getAlias()));
16062e5b6d6dSopenharmony_ci    utext_setNativeIndex(ut.getAlias(), 4);
16072e5b6d6dSopenharmony_ci    assertEquals("UTextTest::Ticket13344-trail-2", (int64_t)3, utext_getNativeIndex(ut.getAlias()));
16082e5b6d6dSopenharmony_ci    utext_setNativeIndex(ut.getAlias(), 5);
16092e5b6d6dSopenharmony_ci    assertEquals("UTextTest::Ticket13344-bmp-2", (int64_t)5, utext_getNativeIndex(ut.getAlias()));
16102e5b6d6dSopenharmony_ci}
16112e5b6d6dSopenharmony_ci
1612