Home | History | Annotate | Download | only in intltest
      1 /********************************************************************
      2  * COPYRIGHT:
      3  * Copyright (c) 2005-2009, International Business Machines Corporation and
      4  * others. All Rights Reserved.
      5  ********************************************************************/
      6 /************************************************************************
      7 *   Tests for the UText and UTextIterator text abstraction classses
      8 *
      9 ************************************************************************/
     10 
     11 #include "unicode/utypes.h"
     12 
     13 #include <string.h>
     14 #include <stdio.h>
     15 #include <stdlib.h>
     16 #include <unicode/utext.h>
     17 #include <unicode/utf8.h>
     18 #include <unicode/ustring.h>
     19 #include <unicode/uchriter.h>
     20 #include "utxttest.h"
     21 
     22 static UBool  gFailed = FALSE;
     23 static int    gTestNum = 0;
     24 
     25 // Forward decl
     26 UText *openFragmentedUnicodeString(UText *ut, UnicodeString *s, UErrorCode *status);
     27 
     28 #define TEST_ASSERT(x) \
     29 { if ((x)==FALSE) {errln("Test #%d failure in file %s at line %d\n", gTestNum, __FILE__, __LINE__);\
     30                      gFailed = TRUE;\
     31    }}
     32 
     33 
     34 #define TEST_SUCCESS(status) \
     35 { if (U_FAILURE(status)) {errln("Test #%d failure in file %s at line %d. Error = \"%s\"\n", \
     36        gTestNum, __FILE__, __LINE__, u_errorName(status)); \
     37        gFailed = TRUE;\
     38    }}
     39 
     40 UTextTest::UTextTest() {
     41 }
     42 
     43 UTextTest::~UTextTest() {
     44 }
     45 
     46 
     47 void
     48 UTextTest::runIndexedTest(int32_t index, UBool exec,
     49                           const char* &name, char* /*par*/) {
     50     switch (index) {
     51         case 0: name = "TextTest";
     52             if (exec) TextTest();    break;
     53         case 1: name = "ErrorTest";
     54             if (exec) ErrorTest();   break;
     55         case 2: name = "FreezeTest";
     56             if (exec) FreezeTest();  break;
     57         case 3: name = "Ticket5560";
     58             if (exec) Ticket5560();  break;
     59         case 4: name = "Ticket6847";
     60             if (exec) Ticket6847();  break;
     61         default: name = "";          break;
     62     }
     63 }
     64 
     65 //
     66 // Quick and dirty random number generator.
     67 //   (don't use library so that results are portable.
     68 static uint32_t m_seed = 1;
     69 static uint32_t m_rand()
     70 {
     71     m_seed = m_seed * 1103515245 + 12345;
     72     return (uint32_t)(m_seed/65536) % 32768;
     73 }
     74 
     75 
     76 //
     77 //   TextTest()
     78 //
     79 //       Top Level function for UText testing.
     80 //       Specifies the strings to be tested, with the acutal testing itself
     81 //       being carried out in another function, TestString().
     82 //
     83 void  UTextTest::TextTest() {
     84     int32_t i, j;
     85 
     86     TestString("abcd\\U00010001xyz");
     87     TestString("");
     88 
     89     // Supplementary chars at start or end
     90     TestString("\\U00010001");
     91     TestString("abc\\U00010001");
     92     TestString("\\U00010001abc");
     93 
     94     // Test simple strings of lengths 1 to 60, looking for glitches at buffer boundaries
     95     UnicodeString s;
     96     for (i=1; i<60; i++) {
     97         s.truncate(0);
     98         for (j=0; j<i; j++) {
     99             if (j+0x30 == 0x5c) {
    100                 // backslash.  Needs to be escaped
    101                 s.append((UChar)0x5c);
    102             }
    103             s.append(UChar(j+0x30));
    104         }
    105         TestString(s);
    106     }
    107 
    108    // Test strings with odd-aligned supplementary chars,
    109    //    looking for glitches at buffer boundaries
    110     for (i=1; i<60; i++) {
    111         s.truncate(0);
    112         s.append((UChar)0x41);
    113         for (j=0; j<i; j++) {
    114             s.append(UChar32(j+0x11000));
    115         }
    116         TestString(s);
    117     }
    118 
    119     // String of chars of randomly varying size in utf-8 representation.
    120     //   Exercise the mapping, and the varying sized buffer.
    121     //
    122     s.truncate(0);
    123     UChar32  c1 = 0;
    124     UChar32  c2 = 0x100;
    125     UChar32  c3 = 0xa000;
    126     UChar32  c4 = 0x11000;
    127     for (i=0; i<1000; i++) {
    128         int len8 = m_rand()%4 + 1;
    129         switch (len8) {
    130             case 1:
    131                 c1 = (c1+1)%0x80;
    132                 // don't put 0 into string (0 terminated strings for some tests)
    133                 // don't put '\', will cause unescape() to fail.
    134                 if (c1==0x5c || c1==0) {
    135                     c1++;
    136                 }
    137                 s.append(c1);
    138                 break;
    139             case 2:
    140                 s.append(c2++);
    141                 break;
    142             case 3:
    143                 s.append(c3++);
    144                 break;
    145             case 4:
    146                 s.append(c4++);
    147                 break;
    148         }
    149     }
    150     TestString(s);
    151 }
    152 
    153 
    154 //
    155 //  TestString()     Run a suite of UText tests on a string.
    156 //                   The test string is unescaped before use.
    157 //
    158 void UTextTest::TestString(const UnicodeString &s) {
    159     int32_t       i;
    160     int32_t       j;
    161     UChar32       c;
    162     int32_t       cpCount = 0;
    163     UErrorCode    status  = U_ZERO_ERROR;
    164     UText        *ut      = NULL;
    165     int32_t       saLen;
    166 
    167     UnicodeString sa = s.unescape();
    168     saLen = sa.length();
    169 
    170     //
    171     // Build up a mapping between code points and UTF-16 code unit indexes.
    172     //
    173     m *cpMap = new m[sa.length() + 1];
    174     j = 0;
    175     for (i=0; i<sa.length(); i=sa.moveIndex32(i, 1)) {
    176         c = sa.char32At(i);
    177         cpMap[j].nativeIdx = i;
    178         cpMap[j].cp = c;
    179         j++;
    180         cpCount++;
    181     }
    182     cpMap[j].nativeIdx = i;   // position following the last char in utf-16 string.
    183 
    184 
    185     // UChar * test, null terminated
    186     status = U_ZERO_ERROR;
    187     UChar *buf = new UChar[saLen+1];
    188     sa.extract(buf, saLen+1, status);
    189     TEST_SUCCESS(status);
    190     ut = utext_openUChars(NULL, buf, -1, &status);
    191     TEST_SUCCESS(status);
    192     TestAccess(sa, ut, cpCount, cpMap);
    193     utext_close(ut);
    194     delete [] buf;
    195 
    196     // UChar * test, with length
    197     status = U_ZERO_ERROR;
    198     buf = new UChar[saLen+1];
    199     sa.extract(buf, saLen+1, status);
    200     TEST_SUCCESS(status);
    201     ut = utext_openUChars(NULL, buf, saLen, &status);
    202     TEST_SUCCESS(status);
    203     TestAccess(sa, ut, cpCount, cpMap);
    204     utext_close(ut);
    205     delete [] buf;
    206 
    207 
    208     // UnicodeString test
    209     status = U_ZERO_ERROR;
    210     ut = utext_openUnicodeString(NULL, &sa, &status);
    211     TEST_SUCCESS(status);
    212     TestAccess(sa, ut, cpCount, cpMap);
    213     TestCMR(sa, ut, cpCount, cpMap, cpMap);
    214     utext_close(ut);
    215 
    216 
    217     // Const UnicodeString test
    218     status = U_ZERO_ERROR;
    219     ut = utext_openConstUnicodeString(NULL, &sa, &status);
    220     TEST_SUCCESS(status);
    221     TestAccess(sa, ut, cpCount, cpMap);
    222     utext_close(ut);
    223 
    224 
    225     // Replaceable test.  (UnicodeString inherits Replaceable)
    226     status = U_ZERO_ERROR;
    227     ut = utext_openReplaceable(NULL, &sa, &status);
    228     TEST_SUCCESS(status);
    229     TestAccess(sa, ut, cpCount, cpMap);
    230     TestCMR(sa, ut, cpCount, cpMap, cpMap);
    231     utext_close(ut);
    232 
    233     // Character Iterator Tests
    234     status = U_ZERO_ERROR;
    235     const UChar *cbuf = sa.getBuffer();
    236     CharacterIterator *ci = new UCharCharacterIterator(cbuf, saLen, status);
    237     TEST_SUCCESS(status);
    238     ut = utext_openCharacterIterator(NULL, ci, &status);
    239     TEST_SUCCESS(status);
    240     TestAccess(sa, ut, cpCount, cpMap);
    241     utext_close(ut);
    242     delete ci;
    243 
    244 
    245     // Fragmented UnicodeString  (Chunk size of one)
    246     //
    247     status = U_ZERO_ERROR;
    248     ut = openFragmentedUnicodeString(NULL, &sa, &status);
    249     TEST_SUCCESS(status);
    250     TestAccess(sa, ut, cpCount, cpMap);
    251     utext_close(ut);
    252 
    253     //
    254     // UTF-8 test
    255     //
    256 
    257     // Convert the test string from UnicodeString to (char *) in utf-8 format
    258     int32_t u8Len = sa.extract(0, sa.length(), NULL, 0, "utf-8");
    259     char *u8String = new char[u8Len + 1];
    260     sa.extract(0, sa.length(), u8String, u8Len+1, "utf-8");
    261 
    262     // Build up the map of code point indices in the utf-8 string
    263     m * u8Map = new m[sa.length() + 1];
    264     i = 0;   // native utf-8 index
    265     for (j=0; j<cpCount ; j++) {  // code point number
    266         u8Map[j].nativeIdx = i;
    267         U8_NEXT(u8String, i, u8Len, c)
    268         u8Map[j].cp = c;
    269     }
    270     u8Map[cpCount].nativeIdx = u8Len;   // position following the last char in utf-8 string.
    271 
    272     // Do the test itself
    273     status = U_ZERO_ERROR;
    274     ut = utext_openUTF8(NULL, u8String, -1, &status);
    275     TEST_SUCCESS(status);
    276     TestAccess(sa, ut, cpCount, u8Map);
    277     utext_close(ut);
    278 
    279 
    280 
    281     delete []cpMap;
    282     delete []u8Map;
    283     delete []u8String;
    284 }
    285 
    286 //  TestCMR   test Copy, Move and Replace operations.
    287 //              us         UnicodeString containing the test text.
    288 //              ut         UText containing the same test text.
    289 //              cpCount    number of code points in the test text.
    290 //              nativeMap  Mapping from code points to native indexes for the UText.
    291 //              u16Map     Mapping from code points to UTF-16 indexes, for use with the UnicodeString.
    292 //
    293 //     This function runs a whole series of opertions on each incoming UText.
    294 //     The UText is deep-cloned prior to each operation, so that the original UText remains unchanged.
    295 //
    296 void UTextTest::TestCMR(const UnicodeString &us, UText *ut, int cpCount, m *nativeMap, m *u16Map) {
    297     TEST_ASSERT(utext_isWritable(ut) == TRUE);
    298 
    299     int  srcLengthType;       // Loop variables for selecting the postion and length
    300     int  srcPosType;          //   of the block to operate on within the source text.
    301     int  destPosType;
    302 
    303     int  srcIndex  = 0;       // Code Point indexes of the block to operate on for
    304     int  srcLength = 0;       //   a specific test.
    305 
    306     int  destIndex = 0;       // Code point index of the destination for a copy/move test.
    307 
    308     int32_t  nativeStart = 0; // Native unit indexes for a test.
    309     int32_t  nativeLimit = 0;
    310     int32_t  nativeDest  = 0;
    311 
    312     int32_t  u16Start    = 0; // UTF-16 indexes for a test.
    313     int32_t  u16Limit    = 0; //   used when performing the same operation in a Unicode String
    314     int32_t  u16Dest     = 0;
    315 
    316     // Iterate over a whole series of source index, length and a target indexes.
    317     // This is done with code point indexes; these will be later translated to native
    318     //   indexes using the cpMap.
    319     for (srcLengthType=1; srcLengthType<=3; srcLengthType++) {
    320         switch (srcLengthType) {
    321             case 1: srcLength = 1; break;
    322             case 2: srcLength = 5; break;
    323             case 3: srcLength = cpCount / 3;
    324         }
    325         for (srcPosType=1; srcPosType<=5; srcPosType++) {
    326             switch (srcPosType) {
    327                 case 1: srcIndex = 0; break;
    328                 case 2: srcIndex = 1; break;
    329                 case 3: srcIndex = cpCount - srcLength; break;
    330                 case 4: srcIndex = cpCount - srcLength - 1; break;
    331                 case 5: srcIndex = cpCount / 2; break;
    332             }
    333             if (srcIndex < 0 || srcIndex + srcLength > cpCount) {
    334                 // filter out bogus test cases -
    335                 //   those with a source range that falls of an edge of the string.
    336                 continue;
    337             }
    338 
    339             //
    340             // Copy and move tests.
    341             //   iterate over a variety of destination positions.
    342             //
    343             for (destPosType=1; destPosType<=4; destPosType++) {
    344                 switch (destPosType) {
    345                     case 1: destIndex = 0; break;
    346                     case 2: destIndex = 1; break;
    347                     case 3: destIndex = srcIndex - 1; break;
    348                     case 4: destIndex = srcIndex + srcLength + 1; break;
    349                     case 5: destIndex = cpCount-1; break;
    350                     case 6: destIndex = cpCount; break;
    351                 }
    352                 if (destIndex<0 || destIndex>cpCount) {
    353                     // filter out bogus test cases.
    354                     continue;
    355                 }
    356 
    357                 nativeStart = nativeMap[srcIndex].nativeIdx;
    358                 nativeLimit = nativeMap[srcIndex+srcLength].nativeIdx;
    359                 nativeDest  = nativeMap[destIndex].nativeIdx;
    360 
    361                 u16Start    = u16Map[srcIndex].nativeIdx;
    362                 u16Limit    = u16Map[srcIndex+srcLength].nativeIdx;
    363                 u16Dest     = u16Map[destIndex].nativeIdx;
    364 
    365                 gFailed = FALSE;
    366                 TestCopyMove(us, ut, FALSE,
    367                     nativeStart, nativeLimit, nativeDest,
    368                     u16Start, u16Limit, u16Dest);
    369 
    370                 TestCopyMove(us, ut, TRUE,
    371                     nativeStart, nativeLimit, nativeDest,
    372                     u16Start, u16Limit, u16Dest);
    373 
    374                 if (gFailed) {
    375                     return;
    376                 }
    377             }
    378 
    379             //
    380             //  Replace tests.
    381             //
    382             UnicodeString fullRepString("This is an arbitrary string that will be used as replacement text");
    383             for (int32_t replStrLen=0; replStrLen<20; replStrLen++) {
    384                 UnicodeString repStr(fullRepString, 0, replStrLen);
    385                 TestReplace(us, ut,
    386                     nativeStart, nativeLimit,
    387                     u16Start, u16Limit,
    388                     repStr);
    389                 if (gFailed) {
    390                     return;
    391                 }
    392             }
    393 
    394         }
    395     }
    396 
    397 }
    398 
    399 //
    400 //   TestCopyMove    run a single test case for utext_copy.
    401 //                   Test cases are created in TestCMR and dispatched here for execution.
    402 //
    403 void UTextTest::TestCopyMove(const UnicodeString &us, UText *ut, UBool move,
    404                     int32_t nativeStart, int32_t nativeLimit, int32_t nativeDest,
    405                     int32_t u16Start, int32_t u16Limit, int32_t u16Dest)
    406 {
    407     UErrorCode      status   = U_ZERO_ERROR;
    408     UText          *targetUT = NULL;
    409     gTestNum++;
    410     gFailed = FALSE;
    411 
    412     //
    413     //  clone the UText.  The test will be run in the cloned copy
    414     //  so that we don't alter the original.
    415     //
    416     targetUT = utext_clone(NULL, ut, TRUE, FALSE, &status);
    417     TEST_SUCCESS(status);
    418     UnicodeString targetUS(us);    // And copy the reference string.
    419 
    420     // do the test operation first in the reference
    421     targetUS.copy(u16Start, u16Limit, u16Dest);
    422     if (move) {
    423         // delete out the source range.
    424         if (u16Limit < u16Dest) {
    425             targetUS.removeBetween(u16Start, u16Limit);
    426         } else {
    427             int32_t amtCopied = u16Limit - u16Start;
    428             targetUS.removeBetween(u16Start+amtCopied, u16Limit+amtCopied);
    429         }
    430     }
    431 
    432     // Do the same operation in the UText under test
    433     utext_copy(targetUT, nativeStart, nativeLimit, nativeDest, move, &status);
    434     if (nativeDest > nativeStart && nativeDest < nativeLimit) {
    435         TEST_ASSERT(status == U_INDEX_OUTOFBOUNDS_ERROR);
    436     } else {
    437         TEST_SUCCESS(status);
    438 
    439         // Compare the results of the two parallel tests
    440         int32_t  usi = 0;    // UnicodeString postion, utf-16 index.
    441         int64_t  uti = 0;    // UText position, native index.
    442         int32_t  cpi;        // char32 position (code point index)
    443         UChar32  usc;        // code point from Unicode String
    444         UChar32  utc;        // code point from UText
    445         utext_setNativeIndex(targetUT, 0);
    446         for (cpi=0; ; cpi++) {
    447             usc = targetUS.char32At(usi);
    448             utc = utext_next32(targetUT);
    449             if (utc < 0) {
    450                 break;
    451             }
    452             TEST_ASSERT(uti == usi);
    453             TEST_ASSERT(utc == usc);
    454             usi = targetUS.moveIndex32(usi, 1);
    455             uti = utext_getNativeIndex(targetUT);
    456             if (gFailed) {
    457                 goto cleanupAndReturn;
    458             }
    459         }
    460         int64_t expectedNativeLength = utext_nativeLength(ut);
    461         if (move == FALSE) {
    462             expectedNativeLength += nativeLimit - nativeStart;
    463         }
    464         uti = utext_getNativeIndex(targetUT);
    465         TEST_ASSERT(uti == expectedNativeLength);
    466     }
    467 
    468 cleanupAndReturn:
    469     utext_close(targetUT);
    470 }
    471 
    472 
    473 //
    474 //  TestReplace   Test a single Replace operation.
    475 //
    476 void UTextTest::TestReplace(
    477             const UnicodeString &us,     // reference UnicodeString in which to do the replace
    478             UText         *ut,                // UnicodeText object under test.
    479             int32_t       nativeStart,        // Range to be replaced, in UText native units.
    480             int32_t       nativeLimit,
    481             int32_t       u16Start,           // Range to be replaced, in UTF-16 units
    482             int32_t       u16Limit,           //    for use in the reference UnicodeString.
    483             const UnicodeString &repStr)      // The replacement string
    484 {
    485     UErrorCode      status   = U_ZERO_ERROR;
    486     UText          *targetUT = NULL;
    487     gTestNum++;
    488     gFailed = FALSE;
    489 
    490     //
    491     //  clone the target UText.  The test will be run in the cloned copy
    492     //  so that we don't alter the original.
    493     //
    494     targetUT = utext_clone(NULL, ut, TRUE, FALSE, &status);
    495     TEST_SUCCESS(status);
    496     UnicodeString targetUS(us);    // And copy the reference string.
    497 
    498     //
    499     // Do the replace operation in the Unicode String, to
    500     //   produce a reference result.
    501     //
    502     targetUS.replace(u16Start, u16Limit-u16Start, repStr);
    503 
    504     //
    505     // Do the replace on the UText under test
    506     //
    507     const UChar *rs = repStr.getBuffer();
    508     int32_t  rsLen = repStr.length();
    509     int32_t actualDelta = utext_replace(targetUT, nativeStart, nativeLimit, rs, rsLen, &status);
    510     int32_t expectedDelta = repStr.length() - (nativeLimit - nativeStart);
    511     TEST_ASSERT(actualDelta == expectedDelta);
    512 
    513     //
    514     // Compare the results
    515     //
    516     int32_t  usi = 0;    // UnicodeString postion, utf-16 index.
    517     int64_t  uti = 0;    // UText position, native index.
    518     int32_t  cpi;        // char32 position (code point index)
    519     UChar32  usc;        // code point from Unicode String
    520     UChar32  utc;        // code point from UText
    521     int64_t  expectedNativeLength = 0;
    522     utext_setNativeIndex(targetUT, 0);
    523     for (cpi=0; ; cpi++) {
    524         usc = targetUS.char32At(usi);
    525         utc = utext_next32(targetUT);
    526         if (utc < 0) {
    527             break;
    528         }
    529         TEST_ASSERT(uti == usi);
    530         TEST_ASSERT(utc == usc);
    531         usi = targetUS.moveIndex32(usi, 1);
    532         uti = utext_getNativeIndex(targetUT);
    533         if (gFailed) {
    534             goto cleanupAndReturn;
    535         }
    536     }
    537     expectedNativeLength = utext_nativeLength(ut) + expectedDelta;
    538     uti = utext_getNativeIndex(targetUT);
    539     TEST_ASSERT(uti == expectedNativeLength);
    540 
    541 cleanupAndReturn:
    542     utext_close(targetUT);
    543 }
    544 
    545 //
    546 //  TestAccess      Test the read only access functions on a UText, including cloning.
    547 //                  The text is accessed in a variety of ways, and compared with
    548 //                  the reference UnicodeString.
    549 //
    550 void UTextTest::TestAccess(const UnicodeString &us, UText *ut, int cpCount, m *cpMap) {
    551     // Run the standard tests on the caller-supplied UText.
    552     TestAccessNoClone(us, ut, cpCount, cpMap);
    553 
    554     // Re-run tests on a shallow clone.
    555     utext_setNativeIndex(ut, 0);
    556     UErrorCode status = U_ZERO_ERROR;
    557     UText *shallowClone = utext_clone(NULL, ut, FALSE /*deep*/, FALSE /*readOnly*/, &status);
    558     TEST_SUCCESS(status);
    559     TestAccessNoClone(us, shallowClone, cpCount, cpMap);
    560 
    561     //
    562     // Rerun again on a deep clone.
    563     // Note that text providers are not required to provide deep cloning,
    564     //   so unsupported errors are ignored.
    565     //
    566     status = U_ZERO_ERROR;
    567     utext_setNativeIndex(shallowClone, 0);
    568     UText *deepClone = utext_clone(NULL, shallowClone, TRUE, FALSE, &status);
    569     utext_close(shallowClone);
    570     if (status != U_UNSUPPORTED_ERROR) {
    571         TEST_SUCCESS(status);
    572         TestAccessNoClone(us, deepClone, cpCount, cpMap);
    573     }
    574     utext_close(deepClone);
    575 }
    576 
    577 
    578 //
    579 //  TestAccessNoClone()    Test the read only access functions on a UText.
    580 //                         The text is accessed in a variety of ways, and compared with
    581 //                         the reference UnicodeString.
    582 //
    583 void UTextTest::TestAccessNoClone(const UnicodeString &us, UText *ut, int cpCount, m *cpMap) {
    584     UErrorCode  status = U_ZERO_ERROR;
    585     gTestNum++;
    586 
    587     //
    588     //  Check the length from the UText
    589     //
    590     int64_t expectedLen = cpMap[cpCount].nativeIdx;
    591     int64_t utlen = utext_nativeLength(ut);
    592     TEST_ASSERT(expectedLen == utlen);
    593 
    594     //
    595     //  Iterate forwards, verify that we get the correct code points
    596     //   at the correct native offsets.
    597     //
    598     int         i = 0;
    599     int64_t     index;
    600     int64_t     expectedIndex = 0;
    601     int64_t     foundIndex = 0;
    602     UChar32     expectedC;
    603     UChar32     foundC;
    604     int64_t     len;
    605 
    606     for (i=0; i<cpCount; i++) {
    607         expectedIndex = cpMap[i].nativeIdx;
    608         foundIndex    = utext_getNativeIndex(ut);
    609         TEST_ASSERT(expectedIndex == foundIndex);
    610         expectedC     = cpMap[i].cp;
    611         foundC        = utext_next32(ut);
    612         TEST_ASSERT(expectedC == foundC);
    613         foundIndex    = utext_getPreviousNativeIndex(ut);
    614         TEST_ASSERT(expectedIndex == foundIndex);
    615         if (gFailed) {
    616             return;
    617         }
    618     }
    619     foundC = utext_next32(ut);
    620     TEST_ASSERT(foundC == U_SENTINEL);
    621 
    622     // Repeat above, using macros
    623     utext_setNativeIndex(ut, 0);
    624     for (i=0; i<cpCount; i++) {
    625         expectedIndex = cpMap[i].nativeIdx;
    626         foundIndex    = UTEXT_GETNATIVEINDEX(ut);
    627         TEST_ASSERT(expectedIndex == foundIndex);
    628         expectedC     = cpMap[i].cp;
    629         foundC        = UTEXT_NEXT32(ut);
    630         TEST_ASSERT(expectedC == foundC);
    631         if (gFailed) {
    632             return;
    633         }
    634     }
    635     foundC = UTEXT_NEXT32(ut);
    636     TEST_ASSERT(foundC == U_SENTINEL);
    637 
    638     //
    639     //  Forward iteration (above) should have left index at the
    640     //   end of the input, which should == length().
    641     //
    642     len = utext_nativeLength(ut);
    643     foundIndex  = utext_getNativeIndex(ut);
    644     TEST_ASSERT(len == foundIndex);
    645 
    646     //
    647     // Iterate backwards over entire test string
    648     //
    649     len = utext_getNativeIndex(ut);
    650     utext_setNativeIndex(ut, len);
    651     for (i=cpCount-1; i>=0; i--) {
    652         expectedC     = cpMap[i].cp;
    653         expectedIndex = cpMap[i].nativeIdx;
    654         int64_t prevIndex = utext_getPreviousNativeIndex(ut);
    655         foundC        = utext_previous32(ut);
    656         foundIndex    = utext_getNativeIndex(ut);
    657         TEST_ASSERT(expectedIndex == foundIndex);
    658         TEST_ASSERT(expectedC == foundC);
    659         TEST_ASSERT(prevIndex == foundIndex);
    660         if (gFailed) {
    661             return;
    662         }
    663     }
    664 
    665     //
    666     //  Backwards iteration, above, should have left our iterator
    667     //   position at zero, and continued backwards iterationshould fail.
    668     //
    669     foundIndex = utext_getNativeIndex(ut);
    670     TEST_ASSERT(foundIndex == 0);
    671     foundIndex = utext_getPreviousNativeIndex(ut);
    672     TEST_ASSERT(foundIndex == 0);
    673 
    674 
    675     foundC = utext_previous32(ut);
    676     TEST_ASSERT(foundC == U_SENTINEL);
    677     foundIndex = utext_getNativeIndex(ut);
    678     TEST_ASSERT(foundIndex == 0);
    679     foundIndex = utext_getPreviousNativeIndex(ut);
    680     TEST_ASSERT(foundIndex == 0);
    681 
    682 
    683     // And again, with the macros
    684     utext_setNativeIndex(ut, len);
    685     for (i=cpCount-1; i>=0; i--) {
    686         expectedC     = cpMap[i].cp;
    687         expectedIndex = cpMap[i].nativeIdx;
    688         foundC        = UTEXT_PREVIOUS32(ut);
    689         foundIndex    = UTEXT_GETNATIVEINDEX(ut);
    690         TEST_ASSERT(expectedIndex == foundIndex);
    691         TEST_ASSERT(expectedC == foundC);
    692         if (gFailed) {
    693             return;
    694         }
    695     }
    696 
    697     //
    698     //  Backwards iteration, above, should have left our iterator
    699     //   position at zero, and continued backwards iterationshould fail.
    700     //
    701     foundIndex = UTEXT_GETNATIVEINDEX(ut);
    702     TEST_ASSERT(foundIndex == 0);
    703 
    704     foundC = UTEXT_PREVIOUS32(ut);
    705     TEST_ASSERT(foundC == U_SENTINEL);
    706     foundIndex = UTEXT_GETNATIVEINDEX(ut);
    707     TEST_ASSERT(foundIndex == 0);
    708     if (gFailed) {
    709         return;
    710     }
    711 
    712     //
    713     //  next32From(), prevous32From(), Iterate in a somewhat random order.
    714     //
    715     int  cpIndex = 0;
    716     for (i=0; i<cpCount; i++) {
    717         cpIndex = (cpIndex + 9973) % cpCount;
    718         index         = cpMap[cpIndex].nativeIdx;
    719         expectedC     = cpMap[cpIndex].cp;
    720         foundC        = utext_next32From(ut, index);
    721         TEST_ASSERT(expectedC == foundC);
    722         if (gFailed) {
    723             return;
    724         }
    725     }
    726 
    727     cpIndex = 0;
    728     for (i=0; i<cpCount; i++) {
    729         cpIndex = (cpIndex + 9973) % cpCount;
    730         index         = cpMap[cpIndex+1].nativeIdx;
    731         expectedC     = cpMap[cpIndex].cp;
    732         foundC        = utext_previous32From(ut, index);
    733         TEST_ASSERT(expectedC == foundC);
    734         if (gFailed) {
    735             return;
    736         }
    737     }
    738 
    739 
    740     //
    741     // moveIndex(int32_t delta);
    742     //
    743 
    744     // Walk through frontwards, incrementing by one
    745     utext_setNativeIndex(ut, 0);
    746     for (i=1; i<=cpCount; i++) {
    747         utext_moveIndex32(ut, 1);
    748         index = utext_getNativeIndex(ut);
    749         expectedIndex = cpMap[i].nativeIdx;
    750         TEST_ASSERT(expectedIndex == index);
    751         index = UTEXT_GETNATIVEINDEX(ut);
    752         TEST_ASSERT(expectedIndex == index);
    753     }
    754 
    755     // Walk through frontwards, incrementing by two
    756     utext_setNativeIndex(ut, 0);
    757     for (i=2; i<cpCount; i+=2) {
    758         utext_moveIndex32(ut, 2);
    759         index = utext_getNativeIndex(ut);
    760         expectedIndex = cpMap[i].nativeIdx;
    761         TEST_ASSERT(expectedIndex == index);
    762         index = UTEXT_GETNATIVEINDEX(ut);
    763         TEST_ASSERT(expectedIndex == index);
    764     }
    765 
    766     // walk through the string backwards, decrementing by one.
    767     i = cpMap[cpCount].nativeIdx;
    768     utext_setNativeIndex(ut, i);
    769     for (i=cpCount; i>=0; i--) {
    770         expectedIndex = cpMap[i].nativeIdx;
    771         index = utext_getNativeIndex(ut);
    772         TEST_ASSERT(expectedIndex == index);
    773         index = UTEXT_GETNATIVEINDEX(ut);
    774         TEST_ASSERT(expectedIndex == index);
    775         utext_moveIndex32(ut, -1);
    776     }
    777 
    778 
    779     // walk through backwards, decrementing by three
    780     i = cpMap[cpCount].nativeIdx;
    781     utext_setNativeIndex(ut, i);
    782     for (i=cpCount; i>=0; i-=3) {
    783         expectedIndex = cpMap[i].nativeIdx;
    784         index = utext_getNativeIndex(ut);
    785         TEST_ASSERT(expectedIndex == index);
    786         index = UTEXT_GETNATIVEINDEX(ut);
    787         TEST_ASSERT(expectedIndex == index);
    788         utext_moveIndex32(ut, -3);
    789     }
    790 
    791 
    792     //
    793     // Extract
    794     //
    795     int bufSize = us.length() + 10;
    796     UChar *buf = new UChar[bufSize];
    797     status = U_ZERO_ERROR;
    798     expectedLen = us.length();
    799     len = utext_extract(ut, 0, utlen, buf, bufSize, &status);
    800     TEST_SUCCESS(status);
    801     TEST_ASSERT(len == expectedLen);
    802     int compareResult = us.compare(buf, -1);
    803     TEST_ASSERT(compareResult == 0);
    804 
    805     status = U_ZERO_ERROR;
    806     len = utext_extract(ut, 0, utlen, NULL, 0, &status);
    807     if (utlen == 0) {
    808         TEST_ASSERT(status == U_STRING_NOT_TERMINATED_WARNING);
    809     } else {
    810         TEST_ASSERT(status == U_BUFFER_OVERFLOW_ERROR);
    811     }
    812     TEST_ASSERT(len == expectedLen);
    813 
    814     status = U_ZERO_ERROR;
    815     u_memset(buf, 0x5555, bufSize);
    816     len = utext_extract(ut, 0, utlen, buf, 1, &status);
    817     if (us.length() == 0) {
    818         TEST_SUCCESS(status);
    819         TEST_ASSERT(buf[0] == 0);
    820     } else {
    821         // Buf len == 1, extracting a single 16 bit value.
    822         // If the data char is supplementary, it doesn't matter whether the buffer remains unchanged,
    823         //   or whether the lead surrogate of the pair is extracted.
    824         //   It's a buffer overflow error in either case.
    825         TEST_ASSERT(buf[0] == us.charAt(0) ||
    826                     buf[0] == 0x5555 && U_IS_SUPPLEMENTARY(us.char32At(0)));
    827         TEST_ASSERT(buf[1] == 0x5555);
    828         if (us.length() == 1) {
    829             TEST_ASSERT(status == U_STRING_NOT_TERMINATED_WARNING);
    830         } else {
    831             TEST_ASSERT(status == U_BUFFER_OVERFLOW_ERROR);
    832         }
    833     }
    834 
    835     delete []buf;
    836 }
    837 
    838 
    839 
    840 //
    841 //  ErrorTest()    Check various error and edge cases.
    842 //
    843 void UTextTest::ErrorTest()
    844 {
    845     // Close of an unitialized UText.  Shouldn't blow up.
    846     {
    847         UText  ut;
    848         memset(&ut, 0, sizeof(UText));
    849         utext_close(&ut);
    850         utext_close(NULL);
    851     }
    852 
    853     // Double-close of a UText.  Shouldn't blow up.  UText should still be usable.
    854     {
    855         UErrorCode status = U_ZERO_ERROR;
    856         UText ut = UTEXT_INITIALIZER;
    857         UnicodeString s("Hello, World");
    858         UText *ut2 = utext_openUnicodeString(&ut, &s, &status);
    859         TEST_SUCCESS(status);
    860         TEST_ASSERT(ut2 == &ut);
    861 
    862         UText *ut3 = utext_close(&ut);
    863         TEST_ASSERT(ut3 == &ut);
    864 
    865         UText *ut4 = utext_close(&ut);
    866         TEST_ASSERT(ut4 == &ut);
    867 
    868         utext_openUnicodeString(&ut, &s, &status);
    869         TEST_SUCCESS(status);
    870         utext_close(&ut);
    871     }
    872 
    873     // Re-use of a UText, chaining through each of the types of UText
    874     //   (If it doesn't blow up, and doesn't leak, it's probably working fine)
    875     {
    876         UErrorCode status = U_ZERO_ERROR;
    877         UText ut = UTEXT_INITIALIZER;
    878         UText  *utp;
    879         UnicodeString s1("Hello, World");
    880         UChar s2[] = {(UChar)0x41, (UChar)0x42, (UChar)0};
    881         const char  *s3 = "\x66\x67\x68";
    882 
    883         utp = utext_openUnicodeString(&ut, &s1, &status);
    884         TEST_SUCCESS(status);
    885         TEST_ASSERT(utp == &ut);
    886 
    887         utp = utext_openConstUnicodeString(&ut, &s1, &status);
    888         TEST_SUCCESS(status);
    889         TEST_ASSERT(utp == &ut);
    890 
    891         utp = utext_openUTF8(&ut, s3, -1, &status);
    892         TEST_SUCCESS(status);
    893         TEST_ASSERT(utp == &ut);
    894 
    895         utp = utext_openUChars(&ut, s2, -1, &status);
    896         TEST_SUCCESS(status);
    897         TEST_ASSERT(utp == &ut);
    898 
    899         utp = utext_close(&ut);
    900         TEST_ASSERT(utp == &ut);
    901 
    902         utp = utext_openUnicodeString(&ut, &s1, &status);
    903         TEST_SUCCESS(status);
    904         TEST_ASSERT(utp == &ut);
    905     }
    906 
    907     // Invalid parameters on open
    908     //
    909     {
    910         UErrorCode status = U_ZERO_ERROR;
    911         UText ut = UTEXT_INITIALIZER;
    912 
    913         utext_openUChars(&ut, NULL, 5, &status);
    914         TEST_ASSERT(status == U_ILLEGAL_ARGUMENT_ERROR);
    915 
    916         status = U_ZERO_ERROR;
    917         utext_openUChars(&ut, NULL, -1, &status);
    918         TEST_ASSERT(status == U_ILLEGAL_ARGUMENT_ERROR);
    919 
    920         status = U_ZERO_ERROR;
    921         utext_openUTF8(&ut, NULL, 4, &status);
    922         TEST_ASSERT(status == U_ILLEGAL_ARGUMENT_ERROR);
    923 
    924         status = U_ZERO_ERROR;
    925         utext_openUTF8(&ut, NULL, -1, &status);
    926         TEST_ASSERT(status == U_ILLEGAL_ARGUMENT_ERROR);
    927     }
    928 
    929     //
    930     //  UTF-8 with malformed sequences.
    931     //    These should come through as the Unicode replacement char, \ufffd
    932     //
    933     {
    934         UErrorCode status = U_ZERO_ERROR;
    935         UText *ut = NULL;
    936         const char *badUTF8 = "\x41\x81\x42\xf0\x81\x81\x43";
    937         UChar32  c;
    938 
    939         ut = utext_openUTF8(NULL, badUTF8, -1, &status);
    940         TEST_SUCCESS(status);
    941         c = utext_char32At(ut, 1);
    942         TEST_ASSERT(c == 0xfffd);
    943         c = utext_char32At(ut, 3);
    944         TEST_ASSERT(c == 0xfffd);
    945         c = utext_char32At(ut, 5);
    946         TEST_ASSERT(c == 0xfffd);
    947         c = utext_char32At(ut, 6);
    948         TEST_ASSERT(c == 0x43);
    949 
    950         UChar buf[10];
    951         int n = utext_extract(ut, 0, 9, buf, 10, &status);
    952         TEST_SUCCESS(status);
    953         TEST_ASSERT(n==5);
    954         TEST_ASSERT(buf[1] == 0xfffd);
    955         TEST_ASSERT(buf[3] == 0xfffd);
    956         TEST_ASSERT(buf[2] == 0x42);
    957         utext_close(ut);
    958     }
    959 
    960 
    961     //
    962     //  isLengthExpensive - does it make the exptected transitions after
    963     //                      getting the length of a nul terminated string?
    964     //
    965     {
    966         UErrorCode status = U_ZERO_ERROR;
    967         UnicodeString sa("Hello, this is a string");
    968         UBool  isExpensive;
    969 
    970         UChar sb[100];
    971         memset(sb, 0x20, sizeof(sb));
    972         sb[99] = 0;
    973 
    974         UText *uta = utext_openUnicodeString(NULL, &sa, &status);
    975         TEST_SUCCESS(status);
    976         isExpensive = utext_isLengthExpensive(uta);
    977         TEST_ASSERT(isExpensive == FALSE);
    978         utext_close(uta);
    979 
    980         UText *utb = utext_openUChars(NULL, sb, -1, &status);
    981         TEST_SUCCESS(status);
    982         isExpensive = utext_isLengthExpensive(utb);
    983         TEST_ASSERT(isExpensive == TRUE);
    984         int64_t  len = utext_nativeLength(utb);
    985         TEST_ASSERT(len == 99);
    986         isExpensive = utext_isLengthExpensive(utb);
    987         TEST_ASSERT(isExpensive == FALSE);
    988         utext_close(utb);
    989     }
    990 
    991     //
    992     // Index to positions not on code point boundaries.
    993     //
    994     {
    995         const char *u8str =         "\xc8\x81\xe1\x82\x83\xf1\x84\x85\x86";
    996         int32_t startMap[] =        {   0,  0,  2,  2,  2,  5,  5,  5,  5,  9,  9};
    997         int32_t nextMap[]  =        {   2,  2,  5,  5,  5,  9,  9,  9,  9,  9,  9};
    998         int32_t prevMap[]  =        {   0,  0,  0,  0,  0,  2,  2,  2,  2,  5,  5};
    999         UChar32  c32Map[] =    {0x201, 0x201, 0x1083, 0x1083, 0x1083, 0x044146, 0x044146, 0x044146, 0x044146, -1, -1};
   1000         UChar32  pr32Map[] =   {    -1,   -1,  0x201,  0x201,  0x201,   0x1083,   0x1083,   0x1083,   0x1083, 0x044146, 0x044146};
   1001 
   1002         // extractLen is the size, in UChars, of what will be extracted between index and index+1.
   1003         //  is zero when both index positions lie within the same code point.
   1004         int32_t  exLen[] =          {   0,  1,   0,  0,  1,  0,  0,  0,  2,  0,  0};
   1005 
   1006 
   1007         UErrorCode status = U_ZERO_ERROR;
   1008         UText *ut = utext_openUTF8(NULL, u8str, -1, &status);
   1009         TEST_SUCCESS(status);
   1010 
   1011         // Check setIndex
   1012         int32_t i;
   1013         int32_t startMapLimit = sizeof(startMap) / sizeof(int32_t);
   1014         for (i=0; i<startMapLimit; i++) {
   1015             utext_setNativeIndex(ut, i);
   1016             int64_t cpIndex = utext_getNativeIndex(ut);
   1017             TEST_ASSERT(cpIndex == startMap[i]);
   1018             cpIndex = UTEXT_GETNATIVEINDEX(ut);
   1019             TEST_ASSERT(cpIndex == startMap[i]);
   1020         }
   1021 
   1022         // Check char32At
   1023         for (i=0; i<startMapLimit; i++) {
   1024             UChar32 c32 = utext_char32At(ut, i);
   1025             TEST_ASSERT(c32 == c32Map[i]);
   1026             int64_t cpIndex = utext_getNativeIndex(ut);
   1027             TEST_ASSERT(cpIndex == startMap[i]);
   1028         }
   1029 
   1030         // Check utext_next32From
   1031         for (i=0; i<startMapLimit; i++) {
   1032             UChar32 c32 = utext_next32From(ut, i);
   1033             TEST_ASSERT(c32 == c32Map[i]);
   1034             int64_t cpIndex = utext_getNativeIndex(ut);
   1035             TEST_ASSERT(cpIndex == nextMap[i]);
   1036         }
   1037 
   1038         // check utext_previous32From
   1039         for (i=0; i<startMapLimit; i++) {
   1040             gTestNum++;
   1041             UChar32 c32 = utext_previous32From(ut, i);
   1042             TEST_ASSERT(c32 == pr32Map[i]);
   1043             int64_t cpIndex = utext_getNativeIndex(ut);
   1044             TEST_ASSERT(cpIndex == prevMap[i]);
   1045         }
   1046 
   1047         // check Extract
   1048         //   Extract from i to i+1, which may be zero or one code points,
   1049         //     depending on whether the indices straddle a cp boundary.
   1050         for (i=0; i<startMapLimit; i++) {
   1051             UChar buf[3];
   1052             status = U_ZERO_ERROR;
   1053             int32_t  extractedLen = utext_extract(ut, i, i+1, buf, 3, &status);
   1054             TEST_SUCCESS(status);
   1055             TEST_ASSERT(extractedLen == exLen[i]);
   1056             if (extractedLen > 0) {
   1057                 UChar32  c32;
   1058                 /* extractedLen-extractedLen == 0 is used to get around a compiler warning. */
   1059                 U16_GET(buf, 0, extractedLen-extractedLen, extractedLen, c32);
   1060                 TEST_ASSERT(c32 == c32Map[i]);
   1061             }
   1062         }
   1063 
   1064         utext_close(ut);
   1065     }
   1066 
   1067 
   1068     {    //  Similar test, with utf16 instead of utf8
   1069          //  TODO:  merge the common parts of these tests.
   1070 
   1071         UnicodeString u16str("\\u1000\\U00011000\\u2000\\U00022000", -1, US_INV);
   1072         int32_t startMap[]  ={ 0,     1,   1,    3,     4,  4,     6,  6};
   1073         int32_t nextMap[]  = { 1,     3,   3,    4,     6,  6,     6,  6};
   1074         int32_t prevMap[]  = { 0,     0,   0,    1,     3,  3,     4,  4};
   1075         UChar32  c32Map[] =  {0x1000, 0x11000, 0x11000, 0x2000,  0x22000, 0x22000, -1, -1};
   1076         UChar32  pr32Map[] = {    -1, 0x1000,  0x1000,  0x11000, 0x2000,  0x2000,   0x22000,   0x22000};
   1077         int32_t  exLen[] =   {   1,  0,   2,  1,  0,  2,  0,  0,};
   1078 
   1079         u16str = u16str.unescape();
   1080         UErrorCode status = U_ZERO_ERROR;
   1081         UText *ut = utext_openUnicodeString(NULL, &u16str, &status);
   1082         TEST_SUCCESS(status);
   1083 
   1084         int32_t startMapLimit = sizeof(startMap) / sizeof(int32_t);
   1085         int i;
   1086         for (i=0; i<startMapLimit; i++) {
   1087             utext_setNativeIndex(ut, i);
   1088             int64_t cpIndex = utext_getNativeIndex(ut);
   1089             TEST_ASSERT(cpIndex == startMap[i]);
   1090         }
   1091 
   1092         // Check char32At
   1093         for (i=0; i<startMapLimit; i++) {
   1094             UChar32 c32 = utext_char32At(ut, i);
   1095             TEST_ASSERT(c32 == c32Map[i]);
   1096             int64_t cpIndex = utext_getNativeIndex(ut);
   1097             TEST_ASSERT(cpIndex == startMap[i]);
   1098         }
   1099 
   1100         // Check utext_next32From
   1101         for (i=0; i<startMapLimit; i++) {
   1102             UChar32 c32 = utext_next32From(ut, i);
   1103             TEST_ASSERT(c32 == c32Map[i]);
   1104             int64_t cpIndex = utext_getNativeIndex(ut);
   1105             TEST_ASSERT(cpIndex == nextMap[i]);
   1106         }
   1107 
   1108         // check utext_previous32From
   1109         for (i=0; i<startMapLimit; i++) {
   1110             UChar32 c32 = utext_previous32From(ut, i);
   1111             TEST_ASSERT(c32 == pr32Map[i]);
   1112             int64_t cpIndex = utext_getNativeIndex(ut);
   1113             TEST_ASSERT(cpIndex == prevMap[i]);
   1114         }
   1115 
   1116         // check Extract
   1117         //   Extract from i to i+1, which may be zero or one code points,
   1118         //     depending on whether the indices straddle a cp boundary.
   1119         for (i=0; i<startMapLimit; i++) {
   1120             UChar buf[3];
   1121             status = U_ZERO_ERROR;
   1122             int32_t  extractedLen = utext_extract(ut, i, i+1, buf, 3, &status);
   1123             TEST_SUCCESS(status);
   1124             TEST_ASSERT(extractedLen == exLen[i]);
   1125             if (extractedLen > 0) {
   1126                 UChar32  c32;
   1127                 /* extractedLen-extractedLen == 0 is used to get around a compiler warning. */
   1128                 U16_GET(buf, 0, extractedLen-extractedLen, extractedLen, c32);
   1129                 TEST_ASSERT(c32 == c32Map[i]);
   1130             }
   1131         }
   1132 
   1133         utext_close(ut);
   1134     }
   1135 
   1136     {    //  Similar test, with UText over Replaceable
   1137          //  TODO:  merge the common parts of these tests.
   1138 
   1139         UnicodeString u16str("\\u1000\\U00011000\\u2000\\U00022000", -1, US_INV);
   1140         int32_t startMap[]  ={ 0,     1,   1,    3,     4,  4,     6,  6};
   1141         int32_t nextMap[]  = { 1,     3,   3,    4,     6,  6,     6,  6};
   1142         int32_t prevMap[]  = { 0,     0,   0,    1,     3,  3,     4,  4};
   1143         UChar32  c32Map[] =  {0x1000, 0x11000, 0x11000, 0x2000,  0x22000, 0x22000, -1, -1};
   1144         UChar32  pr32Map[] = {    -1, 0x1000,  0x1000,  0x11000, 0x2000,  0x2000,   0x22000,   0x22000};
   1145         int32_t  exLen[] =   {   1,  0,   2,  1,  0,  2,  0,  0,};
   1146 
   1147         u16str = u16str.unescape();
   1148         UErrorCode status = U_ZERO_ERROR;
   1149         UText *ut = utext_openReplaceable(NULL, &u16str, &status);
   1150         TEST_SUCCESS(status);
   1151 
   1152         int32_t startMapLimit = sizeof(startMap) / sizeof(int32_t);
   1153         int i;
   1154         for (i=0; i<startMapLimit; i++) {
   1155             utext_setNativeIndex(ut, i);
   1156             int64_t cpIndex = utext_getNativeIndex(ut);
   1157             TEST_ASSERT(cpIndex == startMap[i]);
   1158         }
   1159 
   1160         // Check char32At
   1161         for (i=0; i<startMapLimit; i++) {
   1162             UChar32 c32 = utext_char32At(ut, i);
   1163             TEST_ASSERT(c32 == c32Map[i]);
   1164             int64_t cpIndex = utext_getNativeIndex(ut);
   1165             TEST_ASSERT(cpIndex == startMap[i]);
   1166         }
   1167 
   1168         // Check utext_next32From
   1169         for (i=0; i<startMapLimit; i++) {
   1170             UChar32 c32 = utext_next32From(ut, i);
   1171             TEST_ASSERT(c32 == c32Map[i]);
   1172             int64_t cpIndex = utext_getNativeIndex(ut);
   1173             TEST_ASSERT(cpIndex == nextMap[i]);
   1174         }
   1175 
   1176         // check utext_previous32From
   1177         for (i=0; i<startMapLimit; i++) {
   1178             UChar32 c32 = utext_previous32From(ut, i);
   1179             TEST_ASSERT(c32 == pr32Map[i]);
   1180             int64_t cpIndex = utext_getNativeIndex(ut);
   1181             TEST_ASSERT(cpIndex == prevMap[i]);
   1182         }
   1183 
   1184         // check Extract
   1185         //   Extract from i to i+1, which may be zero or one code points,
   1186         //     depending on whether the indices straddle a cp boundary.
   1187         for (i=0; i<startMapLimit; i++) {
   1188             UChar buf[3];
   1189             status = U_ZERO_ERROR;
   1190             int32_t  extractedLen = utext_extract(ut, i, i+1, buf, 3, &status);
   1191             TEST_SUCCESS(status);
   1192             TEST_ASSERT(extractedLen == exLen[i]);
   1193             if (extractedLen > 0) {
   1194                 UChar32  c32;
   1195                 /* extractedLen-extractedLen == 0 is used to get around a compiler warning. */
   1196                 U16_GET(buf, 0, extractedLen-extractedLen, extractedLen, c32);
   1197                 TEST_ASSERT(c32 == c32Map[i]);
   1198             }
   1199         }
   1200 
   1201         utext_close(ut);
   1202     }
   1203 }
   1204 
   1205 
   1206 void UTextTest::FreezeTest() {
   1207     // Check isWritable() and freeze() behavior.
   1208     //
   1209 
   1210     UnicodeString  ustr("Hello, World.");
   1211     const char u8str[] = {char(0x31), (char)0x32, (char)0x33, 0};
   1212     const UChar u16str[] = {(UChar)0x31, (UChar)0x32, (UChar)0x44, 0};
   1213 
   1214     UErrorCode status = U_ZERO_ERROR;
   1215     UText  *ut        = NULL;
   1216     UText  *ut2       = NULL;
   1217 
   1218     ut = utext_openUTF8(ut, u8str, -1, &status);
   1219     TEST_SUCCESS(status);
   1220     UBool writable = utext_isWritable(ut);
   1221     TEST_ASSERT(writable == FALSE);
   1222     utext_copy(ut, 1, 2, 0, TRUE, &status);
   1223     TEST_ASSERT(status == U_NO_WRITE_PERMISSION);
   1224 
   1225     status = U_ZERO_ERROR;
   1226     ut = utext_openUChars(ut, u16str, -1, &status);
   1227     TEST_SUCCESS(status);
   1228     writable = utext_isWritable(ut);
   1229     TEST_ASSERT(writable == FALSE);
   1230     utext_copy(ut, 1, 2, 0, TRUE, &status);
   1231     TEST_ASSERT(status == U_NO_WRITE_PERMISSION);
   1232 
   1233     status = U_ZERO_ERROR;
   1234     ut = utext_openUnicodeString(ut, &ustr, &status);
   1235     TEST_SUCCESS(status);
   1236     writable = utext_isWritable(ut);
   1237     TEST_ASSERT(writable == TRUE);
   1238     utext_freeze(ut);
   1239     writable = utext_isWritable(ut);
   1240     TEST_ASSERT(writable == FALSE);
   1241     utext_copy(ut, 1, 2, 0, TRUE, &status);
   1242     TEST_ASSERT(status == U_NO_WRITE_PERMISSION);
   1243 
   1244     status = U_ZERO_ERROR;
   1245     ut = utext_openUnicodeString(ut, &ustr, &status);
   1246     TEST_SUCCESS(status);
   1247     ut2 = utext_clone(ut2, ut, FALSE, FALSE, &status);  // clone with readonly = false
   1248     TEST_SUCCESS(status);
   1249     writable = utext_isWritable(ut2);
   1250     TEST_ASSERT(writable == TRUE);
   1251     ut2 = utext_clone(ut2, ut, FALSE, TRUE, &status);  // clone with readonly = true
   1252     TEST_SUCCESS(status);
   1253     writable = utext_isWritable(ut2);
   1254     TEST_ASSERT(writable == FALSE);
   1255     utext_copy(ut2, 1, 2, 0, TRUE, &status);
   1256     TEST_ASSERT(status == U_NO_WRITE_PERMISSION);
   1257 
   1258     status = U_ZERO_ERROR;
   1259     ut = utext_openConstUnicodeString(ut, (const UnicodeString *)&ustr, &status);
   1260     TEST_SUCCESS(status);
   1261     writable = utext_isWritable(ut);
   1262     TEST_ASSERT(writable == FALSE);
   1263     utext_copy(ut, 1, 2, 0, TRUE, &status);
   1264     TEST_ASSERT(status == U_NO_WRITE_PERMISSION);
   1265 
   1266     // Deep Clone of a frozen UText should re-enable writing in the copy.
   1267     status = U_ZERO_ERROR;
   1268     ut = utext_openUnicodeString(ut, &ustr, &status);
   1269     TEST_SUCCESS(status);
   1270     utext_freeze(ut);
   1271     ut2 = utext_clone(ut2, ut, TRUE, FALSE, &status);   // deep clone
   1272     TEST_SUCCESS(status);
   1273     writable = utext_isWritable(ut2);
   1274     TEST_ASSERT(writable == TRUE);
   1275 
   1276 
   1277     // Deep clone of a frozen UText, where the base type is intrinsically non-writable,
   1278     //  should NOT enable writing in the copy.
   1279     status = U_ZERO_ERROR;
   1280     ut = utext_openUChars(ut, u16str, -1, &status);
   1281     TEST_SUCCESS(status);
   1282     utext_freeze(ut);
   1283     ut2 = utext_clone(ut2, ut, TRUE, FALSE, &status);   // deep clone
   1284     TEST_SUCCESS(status);
   1285     writable = utext_isWritable(ut2);
   1286     TEST_ASSERT(writable == FALSE);
   1287 
   1288     // cleanup
   1289     utext_close(ut);
   1290     utext_close(ut2);
   1291 }
   1292 
   1293 
   1294 //
   1295 //  Fragmented UText
   1296 //      A UText type that works with a chunk size of 1.
   1297 //      Intended to test for edge cases.
   1298 //      Input comes from a UnicodeString.
   1299 //
   1300 //       ut.b    the character.  Put into both halves.
   1301 //
   1302 
   1303 U_CDECL_BEGIN
   1304 static UBool U_CALLCONV
   1305 fragTextAccess(UText *ut, int64_t index, UBool forward) {
   1306     const UnicodeString *us = (const UnicodeString *)ut->context;
   1307     UChar  c;
   1308     int32_t length = us->length();
   1309     if (forward && index>=0 && index<length) {
   1310         c = us->charAt((int32_t)index);
   1311         ut->b = c | c<<16;
   1312         ut->chunkOffset = 0;
   1313         ut->chunkLength = 1;
   1314         ut->chunkNativeStart = index;
   1315         ut->chunkNativeLimit = index+1;
   1316         return true;
   1317     }
   1318     if (!forward && index>0 && index <=length) {
   1319         c = us->charAt((int32_t)index-1);
   1320         ut->b = c | c<<16;
   1321         ut->chunkOffset = 1;
   1322         ut->chunkLength = 1;
   1323         ut->chunkNativeStart = index-1;
   1324         ut->chunkNativeLimit = index;
   1325         return true;
   1326     }
   1327     ut->b = 0;
   1328     ut->chunkOffset = 0;
   1329     ut->chunkLength = 0;
   1330     if (index <= 0) {
   1331         ut->chunkNativeStart = 0;
   1332         ut->chunkNativeLimit = 0;
   1333     } else {
   1334         ut->chunkNativeStart = length;
   1335         ut->chunkNativeLimit = length;
   1336     }
   1337     return false;
   1338 }
   1339 
   1340 // Function table to be used with this fragmented text provider.
   1341 //   Initialized in the open function.
   1342 static UTextFuncs  fragmentFuncs;
   1343 
   1344 // Clone function for fragmented text provider.
   1345 //   Didn't really want to provide this, but it's easier to provide it than to keep it
   1346 //   out of the tests.
   1347 //
   1348 UText *
   1349 cloneFragmentedUnicodeString(UText *dest, const UText *src, UBool deep, UErrorCode *status) {
   1350     if (U_FAILURE(*status)) {
   1351         return NULL;
   1352     }
   1353     if (deep) {
   1354         *status = U_UNSUPPORTED_ERROR;
   1355         return NULL;
   1356     }
   1357     dest = utext_openUnicodeString(dest, (UnicodeString *)src->context, status);
   1358     utext_setNativeIndex(dest, utext_getNativeIndex(src));
   1359     return dest;
   1360 }
   1361 
   1362 U_CDECL_END
   1363 
   1364 // Open function for the fragmented text provider.
   1365 UText *
   1366 openFragmentedUnicodeString(UText *ut, UnicodeString *s, UErrorCode *status) {
   1367     ut = utext_openUnicodeString(ut, s, status);
   1368     if (U_FAILURE(*status)) {
   1369         return ut;
   1370     }
   1371 
   1372     // Copy of the function table from the stock UnicodeString UText,
   1373     //   and replace the entry for the access function.
   1374     memcpy(&fragmentFuncs, ut->pFuncs, sizeof(fragmentFuncs));
   1375     fragmentFuncs.access = fragTextAccess;
   1376     fragmentFuncs.clone  = cloneFragmentedUnicodeString;
   1377     ut->pFuncs = &fragmentFuncs;
   1378 
   1379     ut->chunkContents = (UChar *)&ut->b;
   1380     ut->pFuncs->access(ut, 0, TRUE);
   1381     return ut;
   1382 }
   1383 
   1384 // Regression test for Ticket 5560
   1385 //   Clone fails to update chunkContentPointer in the cloned copy.
   1386 //   This is only an issue for UText types that work in a local buffer,
   1387 //      (UTF-8 wrapper, for example)
   1388 //
   1389 //   The test:
   1390 //     1.  Create an inital UText
   1391 //     2.  Deep clone it.  Contents should match original.
   1392 //     3.  Reset original to something different.
   1393 //     4.  Check that clone contents did not change.
   1394 //
   1395 void UTextTest::Ticket5560() {
   1396     /* The following two strings are in UTF-8 even on EBCDIC platforms. */
   1397     static const char s1[] = {0x41,0x42,0x43,0x44,0x45,0x46,0}; /* "ABCDEF" */
   1398     static const char s2[] = {0x31,0x32,0x33,0x34,0x35,0x36,0}; /* "123456" */
   1399 	UErrorCode status = U_ZERO_ERROR;
   1400 
   1401 	UText ut1 = UTEXT_INITIALIZER;
   1402 	UText ut2 = UTEXT_INITIALIZER;
   1403 
   1404 	utext_openUTF8(&ut1, s1, -1, &status);
   1405 	UChar c = utext_next32(&ut1);
   1406 	TEST_ASSERT(c == 0x41);  // c == 'A'
   1407 
   1408 	utext_clone(&ut2, &ut1, TRUE, FALSE, &status);
   1409 	TEST_SUCCESS(status);
   1410     c = utext_next32(&ut2);
   1411 	TEST_ASSERT(c == 0x42);  // c == 'B'
   1412     c = utext_next32(&ut1);
   1413 	TEST_ASSERT(c == 0x42);  // c == 'B'
   1414 
   1415 	utext_openUTF8(&ut1, s2, -1, &status);
   1416 	c = utext_next32(&ut1);
   1417 	TEST_ASSERT(c == 0x31);  // c == '1'
   1418     c = utext_next32(&ut2);
   1419 	TEST_ASSERT(c == 0x43);  // c == 'C'
   1420 
   1421     utext_close(&ut1);
   1422     utext_close(&ut2);
   1423 }
   1424 
   1425 
   1426 // Test for Ticket 6847
   1427 //
   1428 void UTextTest::Ticket6847() {
   1429     const int STRLEN = 90;
   1430     UChar s[STRLEN+1];
   1431     u_memset(s, 0x41, STRLEN);
   1432     s[STRLEN] = 0;
   1433 
   1434     UErrorCode status = U_ZERO_ERROR;
   1435     UText *ut = utext_openUChars(NULL, s, -1, &status);
   1436 
   1437     utext_setNativeIndex(ut, 0);
   1438     int32_t count = 0;
   1439     UChar32 c = 0;
   1440     int32_t nativeIndex = UTEXT_GETNATIVEINDEX(ut);
   1441     TEST_ASSERT(nativeIndex == 0);
   1442     while ((c = utext_next32(ut)) != U_SENTINEL) {
   1443         TEST_ASSERT(c == 0x41);
   1444         TEST_ASSERT(count < STRLEN);
   1445         if (count >= STRLEN) {
   1446             break;
   1447         }
   1448         count++;
   1449         nativeIndex = UTEXT_GETNATIVEINDEX(ut);
   1450         TEST_ASSERT(nativeIndex == count);
   1451     }
   1452     TEST_ASSERT(count == STRLEN);
   1453     nativeIndex = UTEXT_GETNATIVEINDEX(ut);
   1454     TEST_ASSERT(nativeIndex == STRLEN);
   1455     utext_close(ut);
   1456 }
   1457 
   1458