Home | History | Annotate | Download | only in intltest
      1 /********************************************************************
      2  * COPYRIGHT:
      3  * Copyright (c) 2005-2010, 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 <string.h>
     12 #include <stdio.h>
     13 #include <stdlib.h>
     14 #include "unicode/utypes.h"
     15 #include "unicode/utext.h"
     16 #include "unicode/utf8.h"
     17 #include "unicode/ustring.h"
     18 #include "unicode/uchriter.h"
     19 #include "utxttest.h"
     20 
     21 static UBool  gFailed = FALSE;
     22 static int    gTestNum = 0;
     23 
     24 // Forward decl
     25 UText *openFragmentedUnicodeString(UText *ut, UnicodeString *s, UErrorCode *status);
     26 
     27 #define TEST_ASSERT(x) \
     28 { if ((x)==FALSE) {errln("Test #%d failure in file %s at line %d\n", gTestNum, __FILE__, __LINE__);\
     29                      gFailed = TRUE;\
     30    }}
     31 
     32 
     33 #define TEST_SUCCESS(status) \
     34 { if (U_FAILURE(status)) {errln("Test #%d failure in file %s at line %d. Error = \"%s\"\n", \
     35        gTestNum, __FILE__, __LINE__, u_errorName(status)); \
     36        gFailed = TRUE;\
     37    }}
     38 
     39 UTextTest::UTextTest() {
     40 }
     41 
     42 UTextTest::~UTextTest() {
     43 }
     44 
     45 
     46 void
     47 UTextTest::runIndexedTest(int32_t index, UBool exec,
     48                           const char* &name, char* /*par*/) {
     49     switch (index) {
     50         case 0: name = "TextTest";
     51             if (exec) TextTest();    break;
     52         case 1: name = "ErrorTest";
     53             if (exec) ErrorTest();   break;
     54         case 2: name = "FreezeTest";
     55             if (exec) FreezeTest();  break;
     56         case 3: name = "Ticket5560";
     57             if (exec) Ticket5560();  break;
     58         case 4: name = "Ticket6847";
     59             if (exec) Ticket6847();  break;
     60         case 5: name = "ComparisonTest";
     61             if (exec) ComparisonTest(); break;
     62         default: name = "";          break;
     63     }
     64 }
     65 
     66 //
     67 // Quick and dirty random number generator.
     68 //   (don't use library so that results are portable.
     69 static uint32_t m_seed = 1;
     70 static uint32_t m_rand()
     71 {
     72     m_seed = m_seed * 1103515245 + 12345;
     73     return (uint32_t)(m_seed/65536) % 32768;
     74 }
     75 
     76 
     77 //
     78 //   TextTest()
     79 //
     80 //       Top Level function for UText testing.
     81 //       Specifies the strings to be tested, with the acutal testing itself
     82 //       being carried out in another function, TestString().
     83 //
     84 void  UTextTest::TextTest() {
     85     int32_t i, j;
     86 
     87     TestString("abcd\\U00010001xyz");
     88     TestString("");
     89 
     90     // Supplementary chars at start or end
     91     TestString("\\U00010001");
     92     TestString("abc\\U00010001");
     93     TestString("\\U00010001abc");
     94 
     95     // Test simple strings of lengths 1 to 60, looking for glitches at buffer boundaries
     96     UnicodeString s;
     97     for (i=1; i<60; i++) {
     98         s.truncate(0);
     99         for (j=0; j<i; j++) {
    100             if (j+0x30 == 0x5c) {
    101                 // backslash.  Needs to be escaped
    102                 s.append((UChar)0x5c);
    103             }
    104             s.append(UChar(j+0x30));
    105         }
    106         TestString(s);
    107     }
    108 
    109    // Test strings with odd-aligned supplementary chars,
    110    //    looking for glitches at buffer boundaries
    111     for (i=1; i<60; i++) {
    112         s.truncate(0);
    113         s.append((UChar)0x41);
    114         for (j=0; j<i; j++) {
    115             s.append(UChar32(j+0x11000));
    116         }
    117         TestString(s);
    118     }
    119 
    120     // String of chars of randomly varying size in utf-8 representation.
    121     //   Exercise the mapping, and the varying sized buffer.
    122     //
    123     s.truncate(0);
    124     UChar32  c1 = 0;
    125     UChar32  c2 = 0x100;
    126     UChar32  c3 = 0xa000;
    127     UChar32  c4 = 0x11000;
    128     for (i=0; i<1000; i++) {
    129         int len8 = m_rand()%4 + 1;
    130         switch (len8) {
    131             case 1:
    132                 c1 = (c1+1)%0x80;
    133                 // don't put 0 into string (0 terminated strings for some tests)
    134                 // don't put '\', will cause unescape() to fail.
    135                 if (c1==0x5c || c1==0) {
    136                     c1++;
    137                 }
    138                 s.append(c1);
    139                 break;
    140             case 2:
    141                 s.append(c2++);
    142                 break;
    143             case 3:
    144                 s.append(c3++);
    145                 break;
    146             case 4:
    147                 s.append(c4++);
    148                 break;
    149         }
    150     }
    151     TestString(s);
    152 }
    153 
    154 
    155 //
    156 //  TestString()     Run a suite of UText tests on a string.
    157 //                   The test string is unescaped before use.
    158 //
    159 void UTextTest::TestString(const UnicodeString &s) {
    160     int32_t       i;
    161     int32_t       j;
    162     UChar32       c;
    163     int32_t       cpCount = 0;
    164     UErrorCode    status  = U_ZERO_ERROR;
    165     UText        *ut      = NULL;
    166     int32_t       saLen;
    167 
    168     UnicodeString sa = s.unescape();
    169     saLen = sa.length();
    170 
    171     //
    172     // Build up a mapping between code points and UTF-16 code unit indexes.
    173     //
    174     m *cpMap = new m[sa.length() + 1];
    175     j = 0;
    176     for (i=0; i<sa.length(); i=sa.moveIndex32(i, 1)) {
    177         c = sa.char32At(i);
    178         cpMap[j].nativeIdx = i;
    179         cpMap[j].cp = c;
    180         j++;
    181         cpCount++;
    182     }
    183     cpMap[j].nativeIdx = i;   // position following the last char in utf-16 string.
    184 
    185 
    186     // UChar * test, null terminated
    187     status = U_ZERO_ERROR;
    188     UChar *buf = new UChar[saLen+1];
    189     sa.extract(buf, saLen+1, status);
    190     TEST_SUCCESS(status);
    191     ut = utext_openUChars(NULL, buf, -1, &status);
    192     TEST_SUCCESS(status);
    193     TestAccess(sa, ut, cpCount, cpMap);
    194     utext_close(ut);
    195     delete [] buf;
    196 
    197     // UChar * test, with length
    198     status = U_ZERO_ERROR;
    199     buf = new UChar[saLen+1];
    200     sa.extract(buf, saLen+1, status);
    201     TEST_SUCCESS(status);
    202     ut = utext_openUChars(NULL, buf, saLen, &status);
    203     TEST_SUCCESS(status);
    204     TestAccess(sa, ut, cpCount, cpMap);
    205     utext_close(ut);
    206     delete [] buf;
    207 
    208 
    209     // UnicodeString test
    210     status = U_ZERO_ERROR;
    211     ut = utext_openUnicodeString(NULL, &sa, &status);
    212     TEST_SUCCESS(status);
    213     TestAccess(sa, ut, cpCount, cpMap);
    214     TestCMR(sa, ut, cpCount, cpMap, cpMap);
    215     utext_close(ut);
    216 
    217 
    218     // Const UnicodeString test
    219     status = U_ZERO_ERROR;
    220     ut = utext_openConstUnicodeString(NULL, &sa, &status);
    221     TEST_SUCCESS(status);
    222     TestAccess(sa, ut, cpCount, cpMap);
    223     utext_close(ut);
    224 
    225 
    226     // Replaceable test.  (UnicodeString inherits Replaceable)
    227     status = U_ZERO_ERROR;
    228     ut = utext_openReplaceable(NULL, &sa, &status);
    229     TEST_SUCCESS(status);
    230     TestAccess(sa, ut, cpCount, cpMap);
    231     TestCMR(sa, ut, cpCount, cpMap, cpMap);
    232     utext_close(ut);
    233 
    234     // Character Iterator Tests
    235     status = U_ZERO_ERROR;
    236     const UChar *cbuf = sa.getBuffer();
    237     CharacterIterator *ci = new UCharCharacterIterator(cbuf, saLen, status);
    238     TEST_SUCCESS(status);
    239     ut = utext_openCharacterIterator(NULL, ci, &status);
    240     TEST_SUCCESS(status);
    241     TestAccess(sa, ut, cpCount, cpMap);
    242     utext_close(ut);
    243     delete ci;
    244 
    245 
    246     // Fragmented UnicodeString  (Chunk size of one)
    247     //
    248     status = U_ZERO_ERROR;
    249     ut = openFragmentedUnicodeString(NULL, &sa, &status);
    250     TEST_SUCCESS(status);
    251     TestAccess(sa, ut, cpCount, cpMap);
    252     utext_close(ut);
    253 
    254     //
    255     // UTF-8 test
    256     //
    257 
    258     // Convert the test string from UnicodeString to (char *) in utf-8 format
    259     int32_t u8Len = sa.extract(0, sa.length(), NULL, 0, "utf-8");
    260     char *u8String = new char[u8Len + 1];
    261     sa.extract(0, sa.length(), u8String, u8Len+1, "utf-8");
    262 
    263     // Build up the map of code point indices in the utf-8 string
    264     m * u8Map = new m[sa.length() + 1];
    265     i = 0;   // native utf-8 index
    266     for (j=0; j<cpCount ; j++) {  // code point number
    267         u8Map[j].nativeIdx = i;
    268         U8_NEXT(u8String, i, u8Len, c)
    269         u8Map[j].cp = c;
    270     }
    271     u8Map[cpCount].nativeIdx = u8Len;   // position following the last char in utf-8 string.
    272 
    273     // Do the test itself
    274     status = U_ZERO_ERROR;
    275     ut = utext_openUTF8(NULL, u8String, -1, &status);
    276     TEST_SUCCESS(status);
    277     TestAccess(sa, ut, cpCount, u8Map);
    278     utext_close(ut);
    279 
    280 
    281 
    282     delete []cpMap;
    283     delete []u8Map;
    284     delete []u8String;
    285 }
    286 
    287 //  TestCMR   test Copy, Move and Replace operations.
    288 //              us         UnicodeString containing the test text.
    289 //              ut         UText containing the same test text.
    290 //              cpCount    number of code points in the test text.
    291 //              nativeMap  Mapping from code points to native indexes for the UText.
    292 //              u16Map     Mapping from code points to UTF-16 indexes, for use with the UnicodeString.
    293 //
    294 //     This function runs a whole series of opertions on each incoming UText.
    295 //     The UText is deep-cloned prior to each operation, so that the original UText remains unchanged.
    296 //
    297 void UTextTest::TestCMR(const UnicodeString &us, UText *ut, int cpCount, m *nativeMap, m *u16Map) {
    298     TEST_ASSERT(utext_isWritable(ut) == TRUE);
    299 
    300     int  srcLengthType;       // Loop variables for selecting the postion and length
    301     int  srcPosType;          //   of the block to operate on within the source text.
    302     int  destPosType;
    303 
    304     int  srcIndex  = 0;       // Code Point indexes of the block to operate on for
    305     int  srcLength = 0;       //   a specific test.
    306 
    307     int  destIndex = 0;       // Code point index of the destination for a copy/move test.
    308 
    309     int32_t  nativeStart = 0; // Native unit indexes for a test.
    310     int32_t  nativeLimit = 0;
    311     int32_t  nativeDest  = 0;
    312 
    313     int32_t  u16Start    = 0; // UTF-16 indexes for a test.
    314     int32_t  u16Limit    = 0; //   used when performing the same operation in a Unicode String
    315     int32_t  u16Dest     = 0;
    316 
    317     // Iterate over a whole series of source index, length and a target indexes.
    318     // This is done with code point indexes; these will be later translated to native
    319     //   indexes using the cpMap.
    320     for (srcLengthType=1; srcLengthType<=3; srcLengthType++) {
    321         switch (srcLengthType) {
    322             case 1: srcLength = 1; break;
    323             case 2: srcLength = 5; break;
    324             case 3: srcLength = cpCount / 3;
    325         }
    326         for (srcPosType=1; srcPosType<=5; srcPosType++) {
    327             switch (srcPosType) {
    328                 case 1: srcIndex = 0; break;
    329                 case 2: srcIndex = 1; break;
    330                 case 3: srcIndex = cpCount - srcLength; break;
    331                 case 4: srcIndex = cpCount - srcLength - 1; break;
    332                 case 5: srcIndex = cpCount / 2; break;
    333             }
    334             if (srcIndex < 0 || srcIndex + srcLength > cpCount) {
    335                 // filter out bogus test cases -
    336                 //   those with a source range that falls of an edge of the string.
    337                 continue;
    338             }
    339 
    340             //
    341             // Copy and move tests.
    342             //   iterate over a variety of destination positions.
    343             //
    344             for (destPosType=1; destPosType<=4; destPosType++) {
    345                 switch (destPosType) {
    346                     case 1: destIndex = 0; break;
    347                     case 2: destIndex = 1; break;
    348                     case 3: destIndex = srcIndex - 1; break;
    349                     case 4: destIndex = srcIndex + srcLength + 1; break;
    350                     case 5: destIndex = cpCount-1; break;
    351                     case 6: destIndex = cpCount; break;
    352                 }
    353                 if (destIndex<0 || destIndex>cpCount) {
    354                     // filter out bogus test cases.
    355                     continue;
    356                 }
    357 
    358                 nativeStart = nativeMap[srcIndex].nativeIdx;
    359                 nativeLimit = nativeMap[srcIndex+srcLength].nativeIdx;
    360                 nativeDest  = nativeMap[destIndex].nativeIdx;
    361 
    362                 u16Start    = u16Map[srcIndex].nativeIdx;
    363                 u16Limit    = u16Map[srcIndex+srcLength].nativeIdx;
    364                 u16Dest     = u16Map[destIndex].nativeIdx;
    365 
    366                 gFailed = FALSE;
    367                 TestCopyMove(us, ut, FALSE,
    368                     nativeStart, nativeLimit, nativeDest,
    369                     u16Start, u16Limit, u16Dest);
    370 
    371                 TestCopyMove(us, ut, TRUE,
    372                     nativeStart, nativeLimit, nativeDest,
    373                     u16Start, u16Limit, u16Dest);
    374 
    375                 if (gFailed) {
    376                     return;
    377                 }
    378             }
    379 
    380             //
    381             //  Replace tests.
    382             //
    383             UnicodeString fullRepString("This is an arbitrary string that will be used as replacement text");
    384             for (int32_t replStrLen=0; replStrLen<20; replStrLen++) {
    385                 UnicodeString repStr(fullRepString, 0, replStrLen);
    386                 TestReplace(us, ut,
    387                     nativeStart, nativeLimit,
    388                     u16Start, u16Limit,
    389                     repStr);
    390                 if (gFailed) {
    391                     return;
    392                 }
    393             }
    394 
    395         }
    396     }
    397 
    398 }
    399 
    400 //
    401 //   TestCopyMove    run a single test case for utext_copy.
    402 //                   Test cases are created in TestCMR and dispatched here for execution.
    403 //
    404 void UTextTest::TestCopyMove(const UnicodeString &us, UText *ut, UBool move,
    405                     int32_t nativeStart, int32_t nativeLimit, int32_t nativeDest,
    406                     int32_t u16Start, int32_t u16Limit, int32_t u16Dest)
    407 {
    408     UErrorCode      status   = U_ZERO_ERROR;
    409     UText          *targetUT = NULL;
    410     gTestNum++;
    411     gFailed = FALSE;
    412 
    413     //
    414     //  clone the UText.  The test will be run in the cloned copy
    415     //  so that we don't alter the original.
    416     //
    417     targetUT = utext_clone(NULL, ut, TRUE, FALSE, &status);
    418     TEST_SUCCESS(status);
    419     UnicodeString targetUS(us);    // And copy the reference string.
    420 
    421     // do the test operation first in the reference
    422     targetUS.copy(u16Start, u16Limit, u16Dest);
    423     if (move) {
    424         // delete out the source range.
    425         if (u16Limit < u16Dest) {
    426             targetUS.removeBetween(u16Start, u16Limit);
    427         } else {
    428             int32_t amtCopied = u16Limit - u16Start;
    429             targetUS.removeBetween(u16Start+amtCopied, u16Limit+amtCopied);
    430         }
    431     }
    432 
    433     // Do the same operation in the UText under test
    434     utext_copy(targetUT, nativeStart, nativeLimit, nativeDest, move, &status);
    435     if (nativeDest > nativeStart && nativeDest < nativeLimit) {
    436         TEST_ASSERT(status == U_INDEX_OUTOFBOUNDS_ERROR);
    437     } else {
    438         TEST_SUCCESS(status);
    439 
    440         // Compare the results of the two parallel tests
    441         int32_t  usi = 0;    // UnicodeString postion, utf-16 index.
    442         int64_t  uti = 0;    // UText position, native index.
    443         int32_t  cpi;        // char32 position (code point index)
    444         UChar32  usc;        // code point from Unicode String
    445         UChar32  utc;        // code point from UText
    446         utext_setNativeIndex(targetUT, 0);
    447         for (cpi=0; ; cpi++) {
    448             usc = targetUS.char32At(usi);
    449             utc = utext_next32(targetUT);
    450             if (utc < 0) {
    451                 break;
    452             }
    453             TEST_ASSERT(uti == usi);
    454             TEST_ASSERT(utc == usc);
    455             usi = targetUS.moveIndex32(usi, 1);
    456             uti = utext_getNativeIndex(targetUT);
    457             if (gFailed) {
    458                 goto cleanupAndReturn;
    459             }
    460         }
    461         int64_t expectedNativeLength = utext_nativeLength(ut);
    462         if (move == FALSE) {
    463             expectedNativeLength += nativeLimit - nativeStart;
    464         }
    465         uti = utext_getNativeIndex(targetUT);
    466         TEST_ASSERT(uti == expectedNativeLength);
    467     }
    468 
    469 cleanupAndReturn:
    470     utext_close(targetUT);
    471 }
    472 
    473 
    474 //
    475 //  TestReplace   Test a single Replace operation.
    476 //
    477 void UTextTest::TestReplace(
    478             const UnicodeString &us,     // reference UnicodeString in which to do the replace
    479             UText         *ut,                // UnicodeText object under test.
    480             int32_t       nativeStart,        // Range to be replaced, in UText native units.
    481             int32_t       nativeLimit,
    482             int32_t       u16Start,           // Range to be replaced, in UTF-16 units
    483             int32_t       u16Limit,           //    for use in the reference UnicodeString.
    484             const UnicodeString &repStr)      // The replacement string
    485 {
    486     UErrorCode      status   = U_ZERO_ERROR;
    487     UText          *targetUT = NULL;
    488     gTestNum++;
    489     gFailed = FALSE;
    490 
    491     //
    492     //  clone the target UText.  The test will be run in the cloned copy
    493     //  so that we don't alter the original.
    494     //
    495     targetUT = utext_clone(NULL, ut, TRUE, FALSE, &status);
    496     TEST_SUCCESS(status);
    497     UnicodeString targetUS(us);    // And copy the reference string.
    498 
    499     //
    500     // Do the replace operation in the Unicode String, to
    501     //   produce a reference result.
    502     //
    503     targetUS.replace(u16Start, u16Limit-u16Start, repStr);
    504 
    505     //
    506     // Do the replace on the UText under test
    507     //
    508     const UChar *rs = repStr.getBuffer();
    509     int32_t  rsLen = repStr.length();
    510     int32_t actualDelta = utext_replace(targetUT, nativeStart, nativeLimit, rs, rsLen, &status);
    511     int32_t expectedDelta = repStr.length() - (nativeLimit - nativeStart);
    512     TEST_ASSERT(actualDelta == expectedDelta);
    513 
    514     //
    515     // Compare the results
    516     //
    517     int32_t  usi = 0;    // UnicodeString postion, utf-16 index.
    518     int64_t  uti = 0;    // UText position, native index.
    519     int32_t  cpi;        // char32 position (code point index)
    520     UChar32  usc;        // code point from Unicode String
    521     UChar32  utc;        // code point from UText
    522     int64_t  expectedNativeLength = 0;
    523     utext_setNativeIndex(targetUT, 0);
    524     for (cpi=0; ; cpi++) {
    525         usc = targetUS.char32At(usi);
    526         utc = utext_next32(targetUT);
    527         if (utc < 0) {
    528             break;
    529         }
    530         TEST_ASSERT(uti == usi);
    531         TEST_ASSERT(utc == usc);
    532         usi = targetUS.moveIndex32(usi, 1);
    533         uti = utext_getNativeIndex(targetUT);
    534         if (gFailed) {
    535             goto cleanupAndReturn;
    536         }
    537     }
    538     expectedNativeLength = utext_nativeLength(ut) + expectedDelta;
    539     uti = utext_getNativeIndex(targetUT);
    540     TEST_ASSERT(uti == expectedNativeLength);
    541 
    542 cleanupAndReturn:
    543     utext_close(targetUT);
    544 }
    545 
    546 //
    547 //  TestAccess      Test the read only access functions on a UText, including cloning.
    548 //                  The text is accessed in a variety of ways, and compared with
    549 //                  the reference UnicodeString.
    550 //
    551 void UTextTest::TestAccess(const UnicodeString &us, UText *ut, int cpCount, m *cpMap) {
    552     // Run the standard tests on the caller-supplied UText.
    553     TestAccessNoClone(us, ut, cpCount, cpMap);
    554 
    555     // Re-run tests on a shallow clone.
    556     utext_setNativeIndex(ut, 0);
    557     UErrorCode status = U_ZERO_ERROR;
    558     UText *shallowClone = utext_clone(NULL, ut, FALSE /*deep*/, FALSE /*readOnly*/, &status);
    559     TEST_SUCCESS(status);
    560     TestAccessNoClone(us, shallowClone, cpCount, cpMap);
    561 
    562     //
    563     // Rerun again on a deep clone.
    564     // Note that text providers are not required to provide deep cloning,
    565     //   so unsupported errors are ignored.
    566     //
    567     status = U_ZERO_ERROR;
    568     utext_setNativeIndex(shallowClone, 0);
    569     UText *deepClone = utext_clone(NULL, shallowClone, TRUE, FALSE, &status);
    570     utext_close(shallowClone);
    571     if (status != U_UNSUPPORTED_ERROR) {
    572         TEST_SUCCESS(status);
    573         TestAccessNoClone(us, deepClone, cpCount, cpMap);
    574     }
    575     utext_close(deepClone);
    576 }
    577 
    578 
    579 //
    580 //  TestAccessNoClone()    Test the read only access functions on a UText.
    581 //                         The text is accessed in a variety of ways, and compared with
    582 //                         the reference UnicodeString.
    583 //
    584 void UTextTest::TestAccessNoClone(const UnicodeString &us, UText *ut, int cpCount, m *cpMap) {
    585     UErrorCode  status = U_ZERO_ERROR;
    586     gTestNum++;
    587 
    588     //
    589     //  Check the length from the UText
    590     //
    591     int64_t expectedLen = cpMap[cpCount].nativeIdx;
    592     int64_t utlen = utext_nativeLength(ut);
    593     TEST_ASSERT(expectedLen == utlen);
    594 
    595     //
    596     //  Iterate forwards, verify that we get the correct code points
    597     //   at the correct native offsets.
    598     //
    599     int         i = 0;
    600     int64_t     index;
    601     int64_t     expectedIndex = 0;
    602     int64_t     foundIndex = 0;
    603     UChar32     expectedC;
    604     UChar32     foundC;
    605     int64_t     len;
    606 
    607     for (i=0; i<cpCount; i++) {
    608         expectedIndex = cpMap[i].nativeIdx;
    609         foundIndex    = utext_getNativeIndex(ut);
    610         TEST_ASSERT(expectedIndex == foundIndex);
    611         expectedC     = cpMap[i].cp;
    612         foundC        = utext_next32(ut);
    613         TEST_ASSERT(expectedC == foundC);
    614         foundIndex    = utext_getPreviousNativeIndex(ut);
    615         TEST_ASSERT(expectedIndex == foundIndex);
    616         if (gFailed) {
    617             return;
    618         }
    619     }
    620     foundC = utext_next32(ut);
    621     TEST_ASSERT(foundC == U_SENTINEL);
    622 
    623     // Repeat above, using macros
    624     utext_setNativeIndex(ut, 0);
    625     for (i=0; i<cpCount; i++) {
    626         expectedIndex = cpMap[i].nativeIdx;
    627         foundIndex    = UTEXT_GETNATIVEINDEX(ut);
    628         TEST_ASSERT(expectedIndex == foundIndex);
    629         expectedC     = cpMap[i].cp;
    630         foundC        = UTEXT_NEXT32(ut);
    631         TEST_ASSERT(expectedC == foundC);
    632         if (gFailed) {
    633             return;
    634         }
    635     }
    636     foundC = UTEXT_NEXT32(ut);
    637     TEST_ASSERT(foundC == U_SENTINEL);
    638 
    639     //
    640     //  Forward iteration (above) should have left index at the
    641     //   end of the input, which should == length().
    642     //
    643     len = utext_nativeLength(ut);
    644     foundIndex  = utext_getNativeIndex(ut);
    645     TEST_ASSERT(len == foundIndex);
    646 
    647     //
    648     // Iterate backwards over entire test string
    649     //
    650     len = utext_getNativeIndex(ut);
    651     utext_setNativeIndex(ut, len);
    652     for (i=cpCount-1; i>=0; i--) {
    653         expectedC     = cpMap[i].cp;
    654         expectedIndex = cpMap[i].nativeIdx;
    655         int64_t prevIndex = utext_getPreviousNativeIndex(ut);
    656         foundC        = utext_previous32(ut);
    657         foundIndex    = utext_getNativeIndex(ut);
    658         TEST_ASSERT(expectedIndex == foundIndex);
    659         TEST_ASSERT(expectedC == foundC);
    660         TEST_ASSERT(prevIndex == foundIndex);
    661         if (gFailed) {
    662             return;
    663         }
    664     }
    665 
    666     //
    667     //  Backwards iteration, above, should have left our iterator
    668     //   position at zero, and continued backwards iterationshould fail.
    669     //
    670     foundIndex = utext_getNativeIndex(ut);
    671     TEST_ASSERT(foundIndex == 0);
    672     foundIndex = utext_getPreviousNativeIndex(ut);
    673     TEST_ASSERT(foundIndex == 0);
    674 
    675 
    676     foundC = utext_previous32(ut);
    677     TEST_ASSERT(foundC == U_SENTINEL);
    678     foundIndex = utext_getNativeIndex(ut);
    679     TEST_ASSERT(foundIndex == 0);
    680     foundIndex = utext_getPreviousNativeIndex(ut);
    681     TEST_ASSERT(foundIndex == 0);
    682 
    683 
    684     // And again, with the macros
    685     utext_setNativeIndex(ut, len);
    686     for (i=cpCount-1; i>=0; i--) {
    687         expectedC     = cpMap[i].cp;
    688         expectedIndex = cpMap[i].nativeIdx;
    689         foundC        = UTEXT_PREVIOUS32(ut);
    690         foundIndex    = UTEXT_GETNATIVEINDEX(ut);
    691         TEST_ASSERT(expectedIndex == foundIndex);
    692         TEST_ASSERT(expectedC == foundC);
    693         if (gFailed) {
    694             return;
    695         }
    696     }
    697 
    698     //
    699     //  Backwards iteration, above, should have left our iterator
    700     //   position at zero, and continued backwards iterationshould fail.
    701     //
    702     foundIndex = UTEXT_GETNATIVEINDEX(ut);
    703     TEST_ASSERT(foundIndex == 0);
    704 
    705     foundC = UTEXT_PREVIOUS32(ut);
    706     TEST_ASSERT(foundC == U_SENTINEL);
    707     foundIndex = UTEXT_GETNATIVEINDEX(ut);
    708     TEST_ASSERT(foundIndex == 0);
    709     if (gFailed) {
    710         return;
    711     }
    712 
    713     //
    714     //  next32From(), prevous32From(), Iterate in a somewhat random order.
    715     //
    716     int  cpIndex = 0;
    717     for (i=0; i<cpCount; i++) {
    718         cpIndex = (cpIndex + 9973) % cpCount;
    719         index         = cpMap[cpIndex].nativeIdx;
    720         expectedC     = cpMap[cpIndex].cp;
    721         foundC        = utext_next32From(ut, index);
    722         TEST_ASSERT(expectedC == foundC);
    723         if (gFailed) {
    724             return;
    725         }
    726     }
    727 
    728     cpIndex = 0;
    729     for (i=0; i<cpCount; i++) {
    730         cpIndex = (cpIndex + 9973) % cpCount;
    731         index         = cpMap[cpIndex+1].nativeIdx;
    732         expectedC     = cpMap[cpIndex].cp;
    733         foundC        = utext_previous32From(ut, index);
    734         TEST_ASSERT(expectedC == foundC);
    735         if (gFailed) {
    736             return;
    737         }
    738     }
    739 
    740 
    741     //
    742     // moveIndex(int32_t delta);
    743     //
    744 
    745     // Walk through frontwards, incrementing by one
    746     utext_setNativeIndex(ut, 0);
    747     for (i=1; i<=cpCount; i++) {
    748         utext_moveIndex32(ut, 1);
    749         index = utext_getNativeIndex(ut);
    750         expectedIndex = cpMap[i].nativeIdx;
    751         TEST_ASSERT(expectedIndex == index);
    752         index = UTEXT_GETNATIVEINDEX(ut);
    753         TEST_ASSERT(expectedIndex == index);
    754     }
    755 
    756     // Walk through frontwards, incrementing by two
    757     utext_setNativeIndex(ut, 0);
    758     for (i=2; i<cpCount; i+=2) {
    759         utext_moveIndex32(ut, 2);
    760         index = utext_getNativeIndex(ut);
    761         expectedIndex = cpMap[i].nativeIdx;
    762         TEST_ASSERT(expectedIndex == index);
    763         index = UTEXT_GETNATIVEINDEX(ut);
    764         TEST_ASSERT(expectedIndex == index);
    765     }
    766 
    767     // walk through the string backwards, decrementing by one.
    768     i = cpMap[cpCount].nativeIdx;
    769     utext_setNativeIndex(ut, i);
    770     for (i=cpCount; i>=0; i--) {
    771         expectedIndex = cpMap[i].nativeIdx;
    772         index = utext_getNativeIndex(ut);
    773         TEST_ASSERT(expectedIndex == index);
    774         index = UTEXT_GETNATIVEINDEX(ut);
    775         TEST_ASSERT(expectedIndex == index);
    776         utext_moveIndex32(ut, -1);
    777     }
    778 
    779 
    780     // walk through backwards, decrementing by three
    781     i = cpMap[cpCount].nativeIdx;
    782     utext_setNativeIndex(ut, i);
    783     for (i=cpCount; i>=0; i-=3) {
    784         expectedIndex = cpMap[i].nativeIdx;
    785         index = utext_getNativeIndex(ut);
    786         TEST_ASSERT(expectedIndex == index);
    787         index = UTEXT_GETNATIVEINDEX(ut);
    788         TEST_ASSERT(expectedIndex == index);
    789         utext_moveIndex32(ut, -3);
    790     }
    791 
    792 
    793     //
    794     // Extract
    795     //
    796     int bufSize = us.length() + 10;
    797     UChar *buf = new UChar[bufSize];
    798     status = U_ZERO_ERROR;
    799     expectedLen = us.length();
    800     len = utext_extract(ut, 0, utlen, buf, bufSize, &status);
    801     TEST_SUCCESS(status);
    802     TEST_ASSERT(len == expectedLen);
    803     int compareResult = us.compare(buf, -1);
    804     TEST_ASSERT(compareResult == 0);
    805 
    806     status = U_ZERO_ERROR;
    807     len = utext_extract(ut, 0, utlen, NULL, 0, &status);
    808     if (utlen == 0) {
    809         TEST_ASSERT(status == U_STRING_NOT_TERMINATED_WARNING);
    810     } else {
    811         TEST_ASSERT(status == U_BUFFER_OVERFLOW_ERROR);
    812     }
    813     TEST_ASSERT(len == expectedLen);
    814 
    815     status = U_ZERO_ERROR;
    816     u_memset(buf, 0x5555, bufSize);
    817     len = utext_extract(ut, 0, utlen, buf, 1, &status);
    818     if (us.length() == 0) {
    819         TEST_SUCCESS(status);
    820         TEST_ASSERT(buf[0] == 0);
    821     } else {
    822         // Buf len == 1, extracting a single 16 bit value.
    823         // If the data char is supplementary, it doesn't matter whether the buffer remains unchanged,
    824         //   or whether the lead surrogate of the pair is extracted.
    825         //   It's a buffer overflow error in either case.
    826         TEST_ASSERT(buf[0] == us.charAt(0) ||
    827                     (buf[0] == 0x5555 && U_IS_SUPPLEMENTARY(us.char32At(0))));
    828         TEST_ASSERT(buf[1] == 0x5555);
    829         if (us.length() == 1) {
    830             TEST_ASSERT(status == U_STRING_NOT_TERMINATED_WARNING);
    831         } else {
    832             TEST_ASSERT(status == U_BUFFER_OVERFLOW_ERROR);
    833         }
    834     }
    835 
    836     delete []buf;
    837 }
    838 
    839 
    840 //
    841 //  ComparisonTest()    Check the string comparison functions. Based on UnicodeStringTest::TestCompare()
    842 //
    843 void UTextTest::ComparisonTest()
    844 {
    845     UErrorCode status = U_ZERO_ERROR;
    846     UnicodeString   test1Str("this is a test");
    847     UnicodeString   test2Str("this is a test");
    848     UnicodeString   test3Str("this is a test of the emergency broadcast system");
    849     UnicodeString   test4Str("never say, \"this is a test\"!!");
    850 
    851     UText test1 = UTEXT_INITIALIZER;
    852     UText test2 = UTEXT_INITIALIZER;
    853     UText test3 = UTEXT_INITIALIZER;
    854     UText test4 = UTEXT_INITIALIZER;
    855 
    856     UChar        uniChars[] = { 0x74, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73,
    857                                 0x20, 0x61, 0x20, 0x74, 0x65, 0x73, 0x74, 0 };
    858     char            chars[] = { 0x74, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73,
    859                                 0x20, 0x61, 0x20, 0x74, 0x65, 0x73, 0x74, 0 };
    860 
    861     UText uniCharText = UTEXT_INITIALIZER;
    862     UText charText = UTEXT_INITIALIZER;
    863 
    864     utext_openUnicodeString(&test1, &test1Str, &status);
    865     utext_openUnicodeString(&test2, &test2Str, &status);
    866     utext_openUnicodeString(&test3, &test3Str, &status);
    867     utext_openUnicodeString(&test4, &test4Str, &status);
    868 
    869     utext_openUChars(&uniCharText, uniChars, -1, &status);
    870     utext_openUTF8(&charText, chars, -1, &status);
    871 
    872     TEST_SUCCESS(status);
    873 
    874     // test utext_compare(), simple
    875     UTEXT_SETNATIVEINDEX(&test1, 0);
    876     UTEXT_SETNATIVEINDEX(&test2, 0);
    877     if (utext_compare(&test1, -1, &test2, -1) != 0) errln("utext_compare() failed, simple setup");
    878     UTEXT_SETNATIVEINDEX(&test1, 0);
    879     UTEXT_SETNATIVEINDEX(&test3, 0);
    880     if (utext_compare(&test1, -1, &test3, -1) >= 0) errln("utext_compare() failed, simple setup");
    881     UTEXT_SETNATIVEINDEX(&test1, 0);
    882     UTEXT_SETNATIVEINDEX(&test4, 0);
    883     if (utext_compare(&test1, -1, &test4, -1) <= 0) errln("utext_compare() failed, simple setup");
    884 
    885     // test utext_compareNativeLimit(), simple
    886     UTEXT_SETNATIVEINDEX(&test1, 0);
    887     UTEXT_SETNATIVEINDEX(&test2, 0);
    888     if (utext_compareNativeLimit(&test1, -1, &test2, -1) != 0) errln("utext_compareNativeLimit() failed, simple setup");
    889     UTEXT_SETNATIVEINDEX(&test1, 0);
    890     UTEXT_SETNATIVEINDEX(&test3, 0);
    891     if (utext_compareNativeLimit(&test1, -1, &test3, -1) >= 0) errln("utext_compareNativeLimit() failed, simple setup");
    892     UTEXT_SETNATIVEINDEX(&test1, 0);
    893     UTEXT_SETNATIVEINDEX(&test4, 0);
    894     if (utext_compareNativeLimit(&test1, -1, &test4, -1) <= 0) errln("utext_compareNativeLimit() failed, simple setup");
    895 
    896     // test utext_compare(), one explicit length
    897     UTEXT_SETNATIVEINDEX(&test1, 0);
    898     UTEXT_SETNATIVEINDEX(&test2, 0);
    899     if (utext_compare(&test1, 14, &test2, -1) != 0) errln("utext_compare() failed, one explicit length");
    900     UTEXT_SETNATIVEINDEX(&test2, 0);
    901     UTEXT_SETNATIVEINDEX(&test3, 0);
    902     if (utext_compare(&test3, 14, &test2, -1) != 0) errln("utext_compare() failed, one explicit length");
    903     UTEXT_SETNATIVEINDEX(&test2, 0);
    904     UTEXT_SETNATIVEINDEX(&test4, 12);
    905     if (utext_compare(&test4, 14, &test2, -1) != 0) errln("utext_compare() failed, one explicit length and offset");
    906     UTEXT_SETNATIVEINDEX(&test1, 0);
    907     UTEXT_SETNATIVEINDEX(&test3, 0);
    908     if (utext_compare(&test3, 18, &test2, -1) <= 0) errln("utext_compare() failed, one explicit length");
    909 
    910     // test utext_compareNativeLimit(), one explicit length
    911     UTEXT_SETNATIVEINDEX(&test1, 0);
    912     UTEXT_SETNATIVEINDEX(&test2, 0);
    913     if (utext_compareNativeLimit(&test1, 14, &test2, -1) != 0) errln("utext_compareNativeLimit() failed, one explicit length");
    914     UTEXT_SETNATIVEINDEX(&test2, 0);
    915     UTEXT_SETNATIVEINDEX(&test3, 0);
    916     if (utext_compareNativeLimit(&test3, 14, &test2, -1) != 0) errln("utext_compareNativeLimit() failed, one explicit length");
    917     UTEXT_SETNATIVEINDEX(&test2, 0);
    918     UTEXT_SETNATIVEINDEX(&test4, 12);
    919     if (utext_compareNativeLimit(&test4, 26, &test2, -1) != 0) errln("utext_compareNativeLimit() failed, one explicit length and limit");
    920     UTEXT_SETNATIVEINDEX(&test1, 0);
    921     UTEXT_SETNATIVEINDEX(&test3, 0);
    922     if (utext_compareNativeLimit(&test3, 18, &test2, -1) <= 0) errln("utext_compareNativeLimit() failed, one explicit length");
    923 
    924     // test utext_compare(), UChar-based UText
    925     UTEXT_SETNATIVEINDEX(&uniCharText, 0);
    926     UTEXT_SETNATIVEINDEX(&test2, 0);
    927     if (utext_compare(&test2, -1, &uniCharText, -1) != 0) errln("utext_compare() failed, UChar-based UText");
    928     UTEXT_SETNATIVEINDEX(&uniCharText, 0);
    929     UTEXT_SETNATIVEINDEX(&test3, 0);
    930     if (utext_compare(&test3, -1, &uniCharText, -1) <= 0) errln("utext_compare() failed, UChar-based UText");
    931     UTEXT_SETNATIVEINDEX(&uniCharText, 0);
    932     UTEXT_SETNATIVEINDEX(&test4, 0);
    933     if (utext_compare(&test4, -1, &uniCharText, -1) >= 0) errln("utext_compare() failed, UChar-based UText");
    934 
    935     // test utext_compareNativeLimit(), UChar-based UText
    936     UTEXT_SETNATIVEINDEX(&uniCharText, 0);
    937     UTEXT_SETNATIVEINDEX(&test2, 0);
    938     if (utext_compareNativeLimit(&test2, -1, &uniCharText, -1) != 0) errln("utext_compareNativeLimit() failed, UChar-based UText");
    939     UTEXT_SETNATIVEINDEX(&uniCharText, 0);
    940     UTEXT_SETNATIVEINDEX(&test3, 0);
    941     if (utext_compareNativeLimit(&test3, -1, &uniCharText, -1) <= 0) errln("utext_compareNativeLimit() failed, UChar-based UText");
    942     UTEXT_SETNATIVEINDEX(&uniCharText, 0);
    943     UTEXT_SETNATIVEINDEX(&test4, 0);
    944     if (utext_compareNativeLimit(&test4, -1, &uniCharText, -1) >= 0) errln("utext_compareNativeLimit() failed, UChar-based UText");
    945 
    946     // test utext_compare(), UTF8-based UText
    947     UTEXT_SETNATIVEINDEX(&charText, 0);
    948     UTEXT_SETNATIVEINDEX(&test2, 0);
    949     if (utext_compare(&test2, -1, &charText, -1) != 0) errln("utext_compare() failed, UTF8-based UText");
    950     UTEXT_SETNATIVEINDEX(&charText, 0);
    951     UTEXT_SETNATIVEINDEX(&test3, 0);
    952     if (utext_compare(&test3, -1, &charText, -1) <= 0) errln("utext_compare() failed, UTF8-based UText");
    953     UTEXT_SETNATIVEINDEX(&charText, 0);
    954     UTEXT_SETNATIVEINDEX(&test4, 0);
    955     if (utext_compare(&test4, -1, &charText, -1) >= 0) errln("utext_compare() failed, UTF8-based UText");
    956 
    957     // test utext_compareNativeLimit(), UTF8-based UText
    958     UTEXT_SETNATIVEINDEX(&charText, 0);
    959     UTEXT_SETNATIVEINDEX(&test2, 0);
    960     if (utext_compareNativeLimit(&test2, -1, &charText, -1) != 0) errln("utext_compareNativeLimit() failed, UTF8-based UText");
    961     UTEXT_SETNATIVEINDEX(&charText, 0);
    962     UTEXT_SETNATIVEINDEX(&test3, 0);
    963     if (utext_compareNativeLimit(&test3, -1, &charText, -1) <= 0) errln("utext_compareNativeLimit() failed, UTF8-based UText");
    964     UTEXT_SETNATIVEINDEX(&charText, 0);
    965     UTEXT_SETNATIVEINDEX(&test4, 0);
    966     if (utext_compareNativeLimit(&test4, -1, &charText, -1) >= 0) errln("utext_compareNativeLimit() failed, UTF8-based UText");
    967 
    968     // test utext_compare(), length
    969     UTEXT_SETNATIVEINDEX(&test1, 0);
    970     UTEXT_SETNATIVEINDEX(&test2, 0);
    971     if (utext_compare(&test1, -1, &test2, 4) != 0) errln("utext_compare() failed, one length");
    972     UTEXT_SETNATIVEINDEX(&test1, 0);
    973     UTEXT_SETNATIVEINDEX(&test2, 0);
    974     if (utext_compare(&test1, 5, &test2, 4) <= 0) errln("utext_compare() failed, both lengths");
    975 
    976     // test utext_compareNativeLimit(), limit
    977     UTEXT_SETNATIVEINDEX(&test1, 0);
    978     UTEXT_SETNATIVEINDEX(&test2, 0);
    979     if (utext_compareNativeLimit(&test1, -1, &test2, 4) != 0) errln("utext_compareNativeLimit() failed, one limit");
    980     UTEXT_SETNATIVEINDEX(&test1, 0);
    981     UTEXT_SETNATIVEINDEX(&test2, 0);
    982     if (utext_compareNativeLimit(&test1, 5, &test2, 4) <= 0) errln("utext_compareNativeLimit() failed, both limits");
    983 
    984     // test utext_compare(), both explicit offsets and lengths
    985     UTEXT_SETNATIVEINDEX(&test1, 0);
    986     UTEXT_SETNATIVEINDEX(&test2, 0);
    987     if (utext_compare(&test1, 14, &test2, 14) != 0) errln("utext_compare() failed, both explicit offsets and lengths");
    988     UTEXT_SETNATIVEINDEX(&test1, 0);
    989     UTEXT_SETNATIVEINDEX(&test3, 0);
    990     if (utext_compare(&test1, 14, &test3, 14) != 0) errln("utext_compare() failed, both explicit offsets and lengths");
    991     UTEXT_SETNATIVEINDEX(&test1, 0);
    992     UTEXT_SETNATIVEINDEX(&test4, 12);
    993     if (utext_compare(&test1, 14, &test4, 14) != 0) errln("utext_compare() failed, both explicit offsets and lengths");
    994     UTEXT_SETNATIVEINDEX(&test1, 10);
    995     UTEXT_SETNATIVEINDEX(&test2, 0);
    996     if (utext_compare(&test1, 4, &test2, 4) >= 0) errln("utext_compare() failed, both explicit offsets and lengths");
    997     UTEXT_SETNATIVEINDEX(&test1, 10);
    998     UTEXT_SETNATIVEINDEX(&test3, 22);
    999     if (utext_compare(&test1, 4, &test3, 9) <= 0) errln("utext_compare() failed, both explicit offsets and lengths");
   1000     UTEXT_SETNATIVEINDEX(&test1, 10);
   1001     UTEXT_SETNATIVEINDEX(&test4, 22);
   1002     if (utext_compare(&test1, 4, &test4, 4) != 0) errln("utext_compare() failed, both explicit offsets and lengths");
   1003 
   1004     // test utext_compareNativeLimit(), both explicit offsets and limits
   1005     UTEXT_SETNATIVEINDEX(&test1, 0);
   1006     UTEXT_SETNATIVEINDEX(&test2, 0);
   1007     if (utext_compareNativeLimit(&test1, 14, &test2, 14) != 0) errln("utext_compareNativeLimit() failed, both explicit offsets and limits");
   1008     UTEXT_SETNATIVEINDEX(&test1, 0);
   1009     UTEXT_SETNATIVEINDEX(&test3, 0);
   1010     if (utext_compareNativeLimit(&test1, 14, &test3, 14) != 0) errln("utext_compareNativeLimit() failed, both explicit offsets and limits");
   1011     UTEXT_SETNATIVEINDEX(&test1, 0);
   1012     UTEXT_SETNATIVEINDEX(&test4, 12);
   1013     if (utext_compareNativeLimit(&test1, 14, &test4, 26) != 0) errln("utext_compareNativeLimit() failed, both explicit offsets and limits");
   1014     UTEXT_SETNATIVEINDEX(&test1, 10);
   1015     UTEXT_SETNATIVEINDEX(&test2, 0);
   1016     if (utext_compareNativeLimit(&test1, 14, &test2, 4) >= 0) errln("utext_compareNativeLimit() failed, both explicit offsets and limits");
   1017     UTEXT_SETNATIVEINDEX(&test1, 10);
   1018     UTEXT_SETNATIVEINDEX(&test3, 22);
   1019     if (utext_compareNativeLimit(&test1, 14, &test3, 31) <= 0) errln("utext_compareNativeLimit() failed, both explicit offsets and limits");
   1020     UTEXT_SETNATIVEINDEX(&test1, 10);
   1021     UTEXT_SETNATIVEINDEX(&test4, 22);
   1022     if (utext_compareNativeLimit(&test1, 14, &test4, 26) != 0) errln("utext_compareNativeLimit() failed, both explicit offsets and limits");
   1023 
   1024     /* test caseCompare() */
   1025     {
   1026         static const UChar
   1027         _mixed[]=               { 0x61, 0x42, 0x131, 0x3a3, 0xdf,       0x130,       0x49,  0xfb03,           0xd93f, 0xdfff, 0 },
   1028         _otherDefault[]=        { 0x41, 0x62, 0x131, 0x3c3, 0x73, 0x53, 0x69, 0x307, 0x69,  0x46, 0x66, 0x49, 0xd93f, 0xdfff, 0 },
   1029         _otherExcludeSpecialI[]={ 0x41, 0x62, 0x131, 0x3c3, 0x53, 0x73, 0x69,        0x131, 0x66, 0x46, 0x69, 0xd93f, 0xdfff, 0 },
   1030         _different[]=           { 0x41, 0x62, 0x131, 0x3c3, 0x73, 0x53, 0x130,       0x49,  0x46, 0x66, 0x49, 0xd93f, 0xdffd, 0 };
   1031 
   1032         UText
   1033         mixed = UTEXT_INITIALIZER,
   1034         otherDefault = UTEXT_INITIALIZER,
   1035         otherExcludeSpecialI = UTEXT_INITIALIZER,
   1036         different = UTEXT_INITIALIZER;
   1037 
   1038         utext_openUChars(&mixed, _mixed, -1, &status);
   1039         utext_openUChars(&otherDefault, _otherDefault, -1, &status);
   1040         utext_openUChars(&otherExcludeSpecialI, _otherExcludeSpecialI, -1, &status);
   1041         utext_openUChars(&different, _different, -1, &status);
   1042 
   1043         TEST_SUCCESS(status);
   1044 
   1045         int32_t result;
   1046 
   1047         /* test default options */
   1048         UTEXT_SETNATIVEINDEX(&mixed, 0);
   1049         UTEXT_SETNATIVEINDEX(&otherDefault, 0);
   1050         result = utext_caseCompare(&mixed, -1, &otherDefault, -1, U_FOLD_CASE_DEFAULT, &status);
   1051         if (0 != result || U_FAILURE(status)) {
   1052             errln("error: utext_caseCompare (other, default) gives %ld (should be 0) (%s)\n", result, u_errorName(status));
   1053         }
   1054         UTEXT_SETNATIVEINDEX(&mixed, 0);
   1055         UTEXT_SETNATIVEINDEX(&otherDefault, 0);
   1056         result = utext_caseCompareNativeLimit(&mixed, -1, &otherDefault, -1, U_FOLD_CASE_DEFAULT, &status);
   1057         if (0 != result || U_FAILURE(status)) {
   1058             errln("error: utext_caseCompareNativeLimit (other, default) gives %ld (should be 0) (%s)\n", result, u_errorName(status));
   1059         }
   1060 
   1061         /* test excluding special I */
   1062         UTEXT_SETNATIVEINDEX(&mixed, 0);
   1063         UTEXT_SETNATIVEINDEX(&otherExcludeSpecialI, 0);
   1064         result = utext_caseCompare(&mixed, -1, &otherExcludeSpecialI, -1, U_FOLD_CASE_EXCLUDE_SPECIAL_I, &status);
   1065         if (0 != result || U_FAILURE(status)) {
   1066             errln("error: utext_caseCompare (otherExcludeSpecialI, U_FOLD_CASE_EXCLUDE_SPECIAL_I) gives %ld (should be 0) (%s)\n", result, u_errorName(status));
   1067         }
   1068         UTEXT_SETNATIVEINDEX(&mixed, 0);
   1069         UTEXT_SETNATIVEINDEX(&otherExcludeSpecialI, 0);
   1070         result = utext_caseCompareNativeLimit(&mixed, -1, &otherExcludeSpecialI, -1, U_FOLD_CASE_EXCLUDE_SPECIAL_I, &status);
   1071         if (0 != result || U_FAILURE(status)) {
   1072             errln("error: utext_caseCompareNativeLimit (otherExcludeSpecialI, U_FOLD_CASE_EXCLUDE_SPECIAL_I) gives %ld (should be 0) (%s)\n", result, u_errorName(status));
   1073         }
   1074         UTEXT_SETNATIVEINDEX(&mixed, 0);
   1075         UTEXT_SETNATIVEINDEX(&otherDefault, 0);
   1076         result = utext_caseCompare(&mixed, -1, &otherDefault, -1, U_FOLD_CASE_EXCLUDE_SPECIAL_I, &status);
   1077         if (0 == result || U_FAILURE(status)) {
   1078             errln("error: utext_caseCompare (other, U_FOLD_CASE_EXCLUDE_SPECIAL_I) gives %ld (should be nonzero) (%s)\n", result, u_errorName(status));
   1079         }
   1080         UTEXT_SETNATIVEINDEX(&mixed, 0);
   1081         UTEXT_SETNATIVEINDEX(&otherDefault, 0);
   1082         result = utext_caseCompareNativeLimit(&mixed, -1, &otherDefault, -1, U_FOLD_CASE_EXCLUDE_SPECIAL_I, &status);
   1083         if (0 == result || U_FAILURE(status)) {
   1084             errln("error: utext_caseCompareNativeLimit (other, U_FOLD_CASE_EXCLUDE_SPECIAL_I) gives %ld (should be nonzero) (%s)\n", result, u_errorName(status));
   1085         }
   1086 
   1087         /* test against different string */
   1088         UTEXT_SETNATIVEINDEX(&mixed, 0);
   1089         UTEXT_SETNATIVEINDEX(&different, 0);
   1090         result = utext_caseCompare(&mixed, -1, &different, -1, U_FOLD_CASE_DEFAULT, &status);
   1091         if (0 >= result || U_FAILURE(status)) {
   1092             errln("error: utext_caseCompare (different, default) gives %ld (should be positive) (%s)\n", result, u_errorName(status));
   1093         }
   1094         UTEXT_SETNATIVEINDEX(&mixed, 0);
   1095         UTEXT_SETNATIVEINDEX(&different, 0);
   1096         result = utext_caseCompareNativeLimit(&mixed, -1, &different, -1, U_FOLD_CASE_DEFAULT, &status);
   1097         if (0 >= result || U_FAILURE(status)) {
   1098             errln("error: utext_caseCompareNativeLimit (different, default) gives %ld (should be positive) (%s)\n", result, u_errorName(status));
   1099         }
   1100 
   1101         /* test caseCompare() - include the folded sharp s (U+00df) with different lengths */
   1102         UTEXT_SETNATIVEINDEX(&mixed, 1);
   1103         UTEXT_SETNATIVEINDEX(&different, 1);
   1104         result = utext_caseCompare(&mixed, 4, &different, 5, U_FOLD_CASE_DEFAULT, &status);
   1105         if (0 != result || U_FAILURE(status)) {
   1106             errln("error: utext_caseCompare (mixed[1-5), different[1-6), default) gives %ld (should be 0) (%s)\n", result, u_errorName(status));
   1107         }
   1108         UTEXT_SETNATIVEINDEX(&mixed, 1);
   1109         UTEXT_SETNATIVEINDEX(&different, 1);
   1110         result = utext_caseCompareNativeLimit(&mixed, 5, &different, 6, U_FOLD_CASE_DEFAULT, &status);
   1111         if (0 != result || U_FAILURE(status)) {
   1112             errln("error: utext_caseCompareNativeLimit (mixed[1-5), different[1-6), default) gives %ld (should be 0) (%s)\n", result, u_errorName(status));
   1113         }
   1114 
   1115         /* test caseCompare() - stop in the middle of the sharp s (U+00df) */
   1116         UTEXT_SETNATIVEINDEX(&mixed, 1);
   1117         UTEXT_SETNATIVEINDEX(&different, 1);
   1118         result = utext_caseCompare(&mixed, 4, &different, 4, U_FOLD_CASE_DEFAULT, &status);
   1119         if (0 >= result || U_FAILURE(status)) {
   1120             errln("error: utext_caseCompare (mixed[1-5), different[1-5), default) gives %ld (should be positive) (%s)\n", result, u_errorName(status));
   1121         }
   1122         UTEXT_SETNATIVEINDEX(&mixed, 1);
   1123         UTEXT_SETNATIVEINDEX(&different, 1);
   1124         result = utext_caseCompareNativeLimit(&mixed, 5, &different, 5, U_FOLD_CASE_DEFAULT, &status);
   1125         if (0 >= result || U_FAILURE(status)) {
   1126             errln("error: utext_caseCompareNativeLimit (mixed[1-5), different[1-5), default) gives %ld (should be positive) (%s)\n", result, u_errorName(status));
   1127         }
   1128     }
   1129 
   1130     /* test surrogates in comparison */
   1131     {
   1132         static const UChar
   1133         _before[] = { 0x65, 0xd800, 0xd800, 0xdc01, 0x65, 0x00 },
   1134         _after[]  = { 0x65, 0xd800, 0xdc00, 0x65, 0x00 };
   1135 
   1136         UText
   1137         before = UTEXT_INITIALIZER,
   1138         after  = UTEXT_INITIALIZER;
   1139 
   1140         utext_openUChars(&before, _before, -1, &status);
   1141         utext_openUChars(&after, _after, -1, &status);
   1142 
   1143         TEST_SUCCESS(status);
   1144         int32_t result;
   1145 
   1146         UTEXT_SETNATIVEINDEX(&before, 1);
   1147         UTEXT_SETNATIVEINDEX(&after, 1);
   1148         result = utext_compare(&before, -1, &after, -1);
   1149         if (0 <= result || U_FAILURE(status)) {
   1150             errln("error: utext_compare ({ 65, d800, 10001, 65 }, { 65, 10000, 65 }) gives %ld (should be negative) (%s)\n", result, u_errorName(status));
   1151         }
   1152 
   1153         UTEXT_SETNATIVEINDEX(&before, 1);
   1154         UTEXT_SETNATIVEINDEX(&after, 1);
   1155         result = utext_compare(&before, 3, &after, 3);
   1156         if (0 <= result || U_FAILURE(status)) {
   1157             errln("error: utext_compare with lengths ({ 65, d800, 10001, 65 }, { 65, 10000, 65 }) gives %ld (should be negative) (%s)\n", result, u_errorName(status));
   1158         }
   1159 
   1160         UTEXT_SETNATIVEINDEX(&before, 1);
   1161         UTEXT_SETNATIVEINDEX(&after, 1);
   1162         result = utext_caseCompare(&before, -1, &after, -1, U_FOLD_CASE_DEFAULT, &status);
   1163         if (0 <= result || U_FAILURE(status)) {
   1164             errln("error: utext_caseCompare ({ 65, d800, 10001, 65 }, { 65, 10000, 65 }) gives %ld (should be negative) (%s)\n", result, u_errorName(status));
   1165         }
   1166 
   1167         UTEXT_SETNATIVEINDEX(&before, 1);
   1168         UTEXT_SETNATIVEINDEX(&after, 1);
   1169         result = utext_caseCompare(&before, 3, &after, 3, U_FOLD_CASE_DEFAULT, &status);
   1170         if (0 <= result || U_FAILURE(status)) {
   1171             errln("error: utext_caseCompare with lengths ({ 65, d800, 10001, 65 }, { 65, 10000, 65 }) gives %ld (should be negative) (%s)\n", result, u_errorName(status));
   1172         }
   1173 
   1174         utext_close(&before);
   1175         utext_close(&after);
   1176     }
   1177 
   1178     /* test surrogates at end of string */
   1179     {
   1180         static const UChar
   1181         _before[] = { 0x65, 0xd800, 0xd800, 0xdc01, 0x00 },
   1182         _after[]  = { 0x65, 0xd800, 0xdc00, 0x00 };
   1183 
   1184         UText
   1185         before = UTEXT_INITIALIZER,
   1186         after  = UTEXT_INITIALIZER;
   1187 
   1188         utext_openUChars(&before, _before, -1, &status);
   1189         utext_openUChars(&after, _after, -1, &status);
   1190 
   1191         TEST_SUCCESS(status);
   1192         int32_t result;
   1193 
   1194         UTEXT_SETNATIVEINDEX(&before, 1);
   1195         UTEXT_SETNATIVEINDEX(&after, 1);
   1196         result = utext_compare(&before, -1, &after, -1);
   1197         if (0 <= result || U_FAILURE(status)) {
   1198             errln("error: utext_compare ({ 65, d800, 10001 }, { 65, 10000 }) gives %ld (should be negative) (%s)\n", result, u_errorName(status));
   1199         }
   1200 
   1201         UTEXT_SETNATIVEINDEX(&before, 1);
   1202         UTEXT_SETNATIVEINDEX(&after, 1);
   1203         result = utext_caseCompare(&before, -1, &after, -1, U_FOLD_CASE_DEFAULT, &status);
   1204         if (0 <= result || U_FAILURE(status)) {
   1205             errln("error: utext_caseCompare ({ 65, d800, 10001 }, { 65, 10000 }) gives %ld (should be negative) (%s)\n", result, u_errorName(status));
   1206         }
   1207 
   1208         utext_close(&before);
   1209         utext_close(&after);
   1210     }
   1211 
   1212     /* test empty strings */
   1213     {
   1214         UChar zero16 = 0;
   1215         char zero8 = 0;
   1216         UText emptyUChar = UTEXT_INITIALIZER;
   1217         UText emptyUTF8 = UTEXT_INITIALIZER;
   1218         UText nullUChar = UTEXT_INITIALIZER;
   1219         UText nullUTF8 = UTEXT_INITIALIZER;
   1220 
   1221         utext_openUChars(&emptyUChar, &zero16, -1, &status);
   1222         utext_openUTF8(&emptyUTF8, &zero8, -1, &status);
   1223         utext_openUChars(&nullUChar, NULL, 0, &status);
   1224         utext_openUTF8(&nullUTF8, NULL, 0, &status);
   1225 
   1226         if (utext_compare(&emptyUChar, -1, &emptyUTF8, -1) != 0) {
   1227             errln("error: utext_compare(&emptyUChar, -1, &emptyUTF8, -1) != 0");
   1228         }
   1229         if (utext_compare(&emptyUChar, -1, &nullUChar, -1) != 0) {
   1230             errln("error: utext_compare(&emptyUChar, -1, &nullUChar, -1) != 0");
   1231         }
   1232         if (utext_compare(&emptyUChar, -1, &nullUTF8, -1) != 0) {
   1233             errln("error: utext_compare(&emptyUChar, -1, &nullUTF8, -1) != 0");
   1234         }
   1235         if (utext_compare(&emptyUTF8, -1, &nullUChar, -1) != 0) {
   1236             errln("error: utext_compare(&emptyUTF8, -1, &nullUChar, -1) != 0");
   1237         }
   1238         if (utext_compare(&emptyUTF8, -1, &nullUTF8, -1) != 0) {
   1239             errln("error: utext_compare(&emptyUTF8, -1, &nullUTF8, -1) != 0");
   1240         }
   1241         if (utext_compare(&nullUChar, -1, &nullUTF8, -1) != 0) {
   1242             errln("error: utext_compare(&nullUChar, -1, &nullUTF8, -1) != 0");
   1243         }
   1244 
   1245         if (utext_compareNativeLimit(&emptyUChar, -1, &emptyUTF8, -1) != 0) {
   1246             errln("error: utext_compareNativeLimit(&emptyUChar, -1, &emptyUTF8, -1) != 0");
   1247         }
   1248         if (utext_compareNativeLimit(&emptyUChar, -1, &nullUChar, -1) != 0) {
   1249             errln("error: utext_compareNativeLimit(&emptyUChar, -1, &nullUChar, -1) != 0");
   1250         }
   1251         if (utext_compareNativeLimit(&emptyUChar, -1, &nullUTF8, -1) != 0) {
   1252             errln("error: utext_compareNativeLimit(&emptyUChar, -1, &nullUTF8, -1) != 0");
   1253         }
   1254         if (utext_compareNativeLimit(&emptyUTF8, -1, &nullUChar, -1) != 0) {
   1255             errln("error: utext_compareNativeLimit(&emptyUTF8, -1, &nullUChar, -1) != 0");
   1256         }
   1257         if (utext_compareNativeLimit(&emptyUTF8, -1, &nullUTF8, -1) != 0) {
   1258             errln("error: utext_compareNativeLimit(&emptyUTF8, -1, &nullUTF8, -1) != 0");
   1259         }
   1260         if (utext_compareNativeLimit(&nullUChar, -1, &nullUTF8, -1) != 0) {
   1261             errln("error: utext_compareNativeLimit(&nullUChar, -1, &nullUTF8, -1) != 0");
   1262         }
   1263 
   1264         if (utext_caseCompare(&emptyUChar, -1, &emptyUTF8, -1, 0, &status) != 0) {
   1265             errln("error: utext_caseCompare(&emptyUChar, -1, &emptyUTF8, -1, 0, &status) != 0");
   1266         }
   1267         if (utext_caseCompare(&emptyUChar, -1, &nullUChar, -1, 0, &status) != 0) {
   1268             errln("error: utext_caseCompare(&emptyUChar, -1, &nullUChar, -1, 0, &status) != 0");
   1269         }
   1270         if (utext_caseCompare(&emptyUChar, -1, &nullUTF8, -1, 0, &status) != 0) {
   1271             errln("error: utext_caseCompare(&emptyUChar, -1, &nullUTF8, -1, 0, &status) != 0");
   1272         }
   1273         if (utext_caseCompare(&emptyUTF8, -1, &nullUChar, -1, 0, &status) != 0) {
   1274             errln("error: utext_caseCompare(&emptyUTF8, -1, &nullUChar, -1, 0, &status) != 0");
   1275         }
   1276         if (utext_caseCompare(&emptyUTF8, -1, &nullUTF8, -1, 0, &status) != 0) {
   1277             errln("error: utext_caseCompare(&emptyUTF8, -1, &nullUTF8, -1, 0, &status) != 0");
   1278         }
   1279         if (utext_caseCompare(&nullUChar, -1, &nullUTF8, -1, 0, &status) != 0) {
   1280             errln("error: utext_caseCompare(&nullUChar, -1, &nullUTF8, -1, 0, &status) != 0");
   1281         }
   1282 
   1283         if (utext_caseCompareNativeLimit(&emptyUChar, -1, &emptyUTF8, -1, 0, &status) != 0) {
   1284             errln("error: utext_caseCompareNativeLimit(&emptyUChar, -1, &emptyUTF8, -1, 0, &status) != 0");
   1285         }
   1286         if (utext_caseCompareNativeLimit(&emptyUChar, -1, &nullUChar, -1, 0, &status) != 0) {
   1287             errln("error: utext_caseCompareNativeLimit(&emptyUChar, -1, &nullUChar, -1, 0, &status) != 0");
   1288         }
   1289         if (utext_caseCompareNativeLimit(&emptyUChar, -1, &nullUTF8, -1, 0, &status) != 0) {
   1290             errln("error: utext_caseCompareNativeLimit(&emptyUChar, -1, &nullUTF8, -1, 0, &status) != 0");
   1291         }
   1292         if (utext_caseCompareNativeLimit(&emptyUTF8, -1, &nullUChar, -1, 0, &status) != 0) {
   1293             errln("error: utext_caseCompareNativeLimit(&emptyUTF8, -1, &nullUChar, -1, 0, &status) != 0");
   1294         }
   1295         if (utext_caseCompareNativeLimit(&emptyUTF8, -1, &nullUTF8, -1, 0, &status) != 0) {
   1296             errln("error: utext_caseCompareNativeLimit(&emptyUTF8, -1, &nullUTF8, -1, 0, &status) != 0");
   1297         }
   1298         if (utext_caseCompareNativeLimit(&nullUChar, -1, &nullUTF8, -1, 0, &status) != 0) {
   1299             errln("error: utext_caseCompareNativeLimit(&nullUChar, -1, &nullUTF8, -1, 0, &status) != 0");
   1300         }
   1301 
   1302         utext_close(&emptyUChar);
   1303         utext_close(&emptyUTF8);
   1304         utext_close(&nullUChar);
   1305         utext_close(&nullUTF8);
   1306         utext_close(&charText);
   1307         utext_close(&uniCharText);
   1308     }
   1309 }
   1310 
   1311 
   1312 
   1313 //
   1314 //  ErrorTest()    Check various error and edge cases.
   1315 //
   1316 void UTextTest::ErrorTest()
   1317 {
   1318     // Close of an unitialized UText.  Shouldn't blow up.
   1319     {
   1320         UText  ut;
   1321         memset(&ut, 0, sizeof(UText));
   1322         utext_close(&ut);
   1323         utext_close(NULL);
   1324     }
   1325 
   1326     // Double-close of a UText.  Shouldn't blow up.  UText should still be usable.
   1327     {
   1328         UErrorCode status = U_ZERO_ERROR;
   1329         UText ut = UTEXT_INITIALIZER;
   1330         UnicodeString s("Hello, World");
   1331         UText *ut2 = utext_openUnicodeString(&ut, &s, &status);
   1332         TEST_SUCCESS(status);
   1333         TEST_ASSERT(ut2 == &ut);
   1334 
   1335         UText *ut3 = utext_close(&ut);
   1336         TEST_ASSERT(ut3 == &ut);
   1337 
   1338         UText *ut4 = utext_close(&ut);
   1339         TEST_ASSERT(ut4 == &ut);
   1340 
   1341         utext_openUnicodeString(&ut, &s, &status);
   1342         TEST_SUCCESS(status);
   1343         utext_close(&ut);
   1344     }
   1345 
   1346     // Re-use of a UText, chaining through each of the types of UText
   1347     //   (If it doesn't blow up, and doesn't leak, it's probably working fine)
   1348     {
   1349         UErrorCode status = U_ZERO_ERROR;
   1350         UText ut = UTEXT_INITIALIZER;
   1351         UText  *utp;
   1352         UnicodeString s1("Hello, World");
   1353         UChar s2[] = {(UChar)0x41, (UChar)0x42, (UChar)0};
   1354         const char  *s3 = "\x66\x67\x68";
   1355 
   1356         utp = utext_openUnicodeString(&ut, &s1, &status);
   1357         TEST_SUCCESS(status);
   1358         TEST_ASSERT(utp == &ut);
   1359 
   1360         utp = utext_openConstUnicodeString(&ut, &s1, &status);
   1361         TEST_SUCCESS(status);
   1362         TEST_ASSERT(utp == &ut);
   1363 
   1364         utp = utext_openUTF8(&ut, s3, -1, &status);
   1365         TEST_SUCCESS(status);
   1366         TEST_ASSERT(utp == &ut);
   1367 
   1368         utp = utext_openUChars(&ut, s2, -1, &status);
   1369         TEST_SUCCESS(status);
   1370         TEST_ASSERT(utp == &ut);
   1371 
   1372         utp = utext_close(&ut);
   1373         TEST_ASSERT(utp == &ut);
   1374 
   1375         utp = utext_openUnicodeString(&ut, &s1, &status);
   1376         TEST_SUCCESS(status);
   1377         TEST_ASSERT(utp == &ut);
   1378     }
   1379 
   1380     // Invalid parameters on open
   1381     //
   1382     {
   1383         UErrorCode status = U_ZERO_ERROR;
   1384         UText ut = UTEXT_INITIALIZER;
   1385 
   1386         utext_openUChars(&ut, NULL, 5, &status);
   1387         TEST_ASSERT(status == U_ILLEGAL_ARGUMENT_ERROR);
   1388 
   1389         status = U_ZERO_ERROR;
   1390         utext_openUChars(&ut, NULL, -1, &status);
   1391         TEST_ASSERT(status == U_ILLEGAL_ARGUMENT_ERROR);
   1392 
   1393         status = U_ZERO_ERROR;
   1394         utext_openUTF8(&ut, NULL, 4, &status);
   1395         TEST_ASSERT(status == U_ILLEGAL_ARGUMENT_ERROR);
   1396 
   1397         status = U_ZERO_ERROR;
   1398         utext_openUTF8(&ut, NULL, -1, &status);
   1399         TEST_ASSERT(status == U_ILLEGAL_ARGUMENT_ERROR);
   1400     }
   1401 
   1402     //
   1403     //  UTF-8 with malformed sequences.
   1404     //    These should come through as the Unicode replacement char, \ufffd
   1405     //
   1406     {
   1407         UErrorCode status = U_ZERO_ERROR;
   1408         UText *ut = NULL;
   1409         const char *badUTF8 = "\x41\x81\x42\xf0\x81\x81\x43";
   1410         UChar32  c;
   1411 
   1412         ut = utext_openUTF8(NULL, badUTF8, -1, &status);
   1413         TEST_SUCCESS(status);
   1414         c = utext_char32At(ut, 1);
   1415         TEST_ASSERT(c == 0xfffd);
   1416         c = utext_char32At(ut, 3);
   1417         TEST_ASSERT(c == 0xfffd);
   1418         c = utext_char32At(ut, 5);
   1419         TEST_ASSERT(c == 0xfffd);
   1420         c = utext_char32At(ut, 6);
   1421         TEST_ASSERT(c == 0x43);
   1422 
   1423         UChar buf[10];
   1424         int n = utext_extract(ut, 0, 9, buf, 10, &status);
   1425         TEST_SUCCESS(status);
   1426         TEST_ASSERT(n==5);
   1427         TEST_ASSERT(buf[1] == 0xfffd);
   1428         TEST_ASSERT(buf[3] == 0xfffd);
   1429         TEST_ASSERT(buf[2] == 0x42);
   1430         utext_close(ut);
   1431     }
   1432 
   1433 
   1434     //
   1435     //  isLengthExpensive - does it make the exptected transitions after
   1436     //                      getting the length of a nul terminated string?
   1437     //
   1438     {
   1439         UErrorCode status = U_ZERO_ERROR;
   1440         UnicodeString sa("Hello, this is a string");
   1441         UBool  isExpensive;
   1442 
   1443         UChar sb[100];
   1444         memset(sb, 0x20, sizeof(sb));
   1445         sb[99] = 0;
   1446 
   1447         UText *uta = utext_openUnicodeString(NULL, &sa, &status);
   1448         TEST_SUCCESS(status);
   1449         isExpensive = utext_isLengthExpensive(uta);
   1450         TEST_ASSERT(isExpensive == FALSE);
   1451         utext_close(uta);
   1452 
   1453         UText *utb = utext_openUChars(NULL, sb, -1, &status);
   1454         TEST_SUCCESS(status);
   1455         isExpensive = utext_isLengthExpensive(utb);
   1456         TEST_ASSERT(isExpensive == TRUE);
   1457         int64_t  len = utext_nativeLength(utb);
   1458         TEST_ASSERT(len == 99);
   1459         isExpensive = utext_isLengthExpensive(utb);
   1460         TEST_ASSERT(isExpensive == FALSE);
   1461         utext_close(utb);
   1462     }
   1463 
   1464     //
   1465     // Index to positions not on code point boundaries.
   1466     //
   1467     {
   1468         const char *u8str =         "\xc8\x81\xe1\x82\x83\xf1\x84\x85\x86";
   1469         int32_t startMap[] =        {   0,  0,  2,  2,  2,  5,  5,  5,  5,  9,  9};
   1470         int32_t nextMap[]  =        {   2,  2,  5,  5,  5,  9,  9,  9,  9,  9,  9};
   1471         int32_t prevMap[]  =        {   0,  0,  0,  0,  0,  2,  2,  2,  2,  5,  5};
   1472         UChar32  c32Map[] =    {0x201, 0x201, 0x1083, 0x1083, 0x1083, 0x044146, 0x044146, 0x044146, 0x044146, -1, -1};
   1473         UChar32  pr32Map[] =   {    -1,   -1,  0x201,  0x201,  0x201,   0x1083,   0x1083,   0x1083,   0x1083, 0x044146, 0x044146};
   1474 
   1475         // extractLen is the size, in UChars, of what will be extracted between index and index+1.
   1476         //  is zero when both index positions lie within the same code point.
   1477         int32_t  exLen[] =          {   0,  1,   0,  0,  1,  0,  0,  0,  2,  0,  0};
   1478 
   1479 
   1480         UErrorCode status = U_ZERO_ERROR;
   1481         UText *ut = utext_openUTF8(NULL, u8str, -1, &status);
   1482         TEST_SUCCESS(status);
   1483 
   1484         // Check setIndex
   1485         int32_t i;
   1486         int32_t startMapLimit = sizeof(startMap) / sizeof(int32_t);
   1487         for (i=0; i<startMapLimit; i++) {
   1488             utext_setNativeIndex(ut, i);
   1489             int64_t cpIndex = utext_getNativeIndex(ut);
   1490             TEST_ASSERT(cpIndex == startMap[i]);
   1491             cpIndex = UTEXT_GETNATIVEINDEX(ut);
   1492             TEST_ASSERT(cpIndex == startMap[i]);
   1493         }
   1494 
   1495         // Check char32At
   1496         for (i=0; i<startMapLimit; i++) {
   1497             UChar32 c32 = utext_char32At(ut, i);
   1498             TEST_ASSERT(c32 == c32Map[i]);
   1499             int64_t cpIndex = utext_getNativeIndex(ut);
   1500             TEST_ASSERT(cpIndex == startMap[i]);
   1501         }
   1502 
   1503         // Check utext_next32From
   1504         for (i=0; i<startMapLimit; i++) {
   1505             UChar32 c32 = utext_next32From(ut, i);
   1506             TEST_ASSERT(c32 == c32Map[i]);
   1507             int64_t cpIndex = utext_getNativeIndex(ut);
   1508             TEST_ASSERT(cpIndex == nextMap[i]);
   1509         }
   1510 
   1511         // check utext_previous32From
   1512         for (i=0; i<startMapLimit; i++) {
   1513             gTestNum++;
   1514             UChar32 c32 = utext_previous32From(ut, i);
   1515             TEST_ASSERT(c32 == pr32Map[i]);
   1516             int64_t cpIndex = utext_getNativeIndex(ut);
   1517             TEST_ASSERT(cpIndex == prevMap[i]);
   1518         }
   1519 
   1520         // check Extract
   1521         //   Extract from i to i+1, which may be zero or one code points,
   1522         //     depending on whether the indices straddle a cp boundary.
   1523         for (i=0; i<startMapLimit; i++) {
   1524             UChar buf[3];
   1525             status = U_ZERO_ERROR;
   1526             int32_t  extractedLen = utext_extract(ut, i, i+1, buf, 3, &status);
   1527             TEST_SUCCESS(status);
   1528             TEST_ASSERT(extractedLen == exLen[i]);
   1529             if (extractedLen > 0) {
   1530                 UChar32  c32;
   1531                 /* extractedLen-extractedLen == 0 is used to get around a compiler warning. */
   1532                 U16_GET(buf, 0, extractedLen-extractedLen, extractedLen, c32);
   1533                 TEST_ASSERT(c32 == c32Map[i]);
   1534             }
   1535         }
   1536 
   1537         utext_close(ut);
   1538     }
   1539 
   1540 
   1541     {    //  Similar test, with utf16 instead of utf8
   1542          //  TODO:  merge the common parts of these tests.
   1543 
   1544         UnicodeString u16str("\\u1000\\U00011000\\u2000\\U00022000", -1, US_INV);
   1545         int32_t startMap[]  ={ 0,     1,   1,    3,     4,  4,     6,  6};
   1546         int32_t nextMap[]  = { 1,     3,   3,    4,     6,  6,     6,  6};
   1547         int32_t prevMap[]  = { 0,     0,   0,    1,     3,  3,     4,  4};
   1548         UChar32  c32Map[] =  {0x1000, 0x11000, 0x11000, 0x2000,  0x22000, 0x22000, -1, -1};
   1549         UChar32  pr32Map[] = {    -1, 0x1000,  0x1000,  0x11000, 0x2000,  0x2000,   0x22000,   0x22000};
   1550         int32_t  exLen[] =   {   1,  0,   2,  1,  0,  2,  0,  0,};
   1551 
   1552         u16str = u16str.unescape();
   1553         UErrorCode status = U_ZERO_ERROR;
   1554         UText *ut = utext_openUnicodeString(NULL, &u16str, &status);
   1555         TEST_SUCCESS(status);
   1556 
   1557         int32_t startMapLimit = sizeof(startMap) / sizeof(int32_t);
   1558         int i;
   1559         for (i=0; i<startMapLimit; i++) {
   1560             utext_setNativeIndex(ut, i);
   1561             int64_t cpIndex = utext_getNativeIndex(ut);
   1562             TEST_ASSERT(cpIndex == startMap[i]);
   1563         }
   1564 
   1565         // Check char32At
   1566         for (i=0; i<startMapLimit; i++) {
   1567             UChar32 c32 = utext_char32At(ut, i);
   1568             TEST_ASSERT(c32 == c32Map[i]);
   1569             int64_t cpIndex = utext_getNativeIndex(ut);
   1570             TEST_ASSERT(cpIndex == startMap[i]);
   1571         }
   1572 
   1573         // Check utext_next32From
   1574         for (i=0; i<startMapLimit; i++) {
   1575             UChar32 c32 = utext_next32From(ut, i);
   1576             TEST_ASSERT(c32 == c32Map[i]);
   1577             int64_t cpIndex = utext_getNativeIndex(ut);
   1578             TEST_ASSERT(cpIndex == nextMap[i]);
   1579         }
   1580 
   1581         // check utext_previous32From
   1582         for (i=0; i<startMapLimit; i++) {
   1583             UChar32 c32 = utext_previous32From(ut, i);
   1584             TEST_ASSERT(c32 == pr32Map[i]);
   1585             int64_t cpIndex = utext_getNativeIndex(ut);
   1586             TEST_ASSERT(cpIndex == prevMap[i]);
   1587         }
   1588 
   1589         // check Extract
   1590         //   Extract from i to i+1, which may be zero or one code points,
   1591         //     depending on whether the indices straddle a cp boundary.
   1592         for (i=0; i<startMapLimit; i++) {
   1593             UChar buf[3];
   1594             status = U_ZERO_ERROR;
   1595             int32_t  extractedLen = utext_extract(ut, i, i+1, buf, 3, &status);
   1596             TEST_SUCCESS(status);
   1597             TEST_ASSERT(extractedLen == exLen[i]);
   1598             if (extractedLen > 0) {
   1599                 UChar32  c32;
   1600                 /* extractedLen-extractedLen == 0 is used to get around a compiler warning. */
   1601                 U16_GET(buf, 0, extractedLen-extractedLen, extractedLen, c32);
   1602                 TEST_ASSERT(c32 == c32Map[i]);
   1603             }
   1604         }
   1605 
   1606         utext_close(ut);
   1607     }
   1608 
   1609     {    //  Similar test, with UText over Replaceable
   1610          //  TODO:  merge the common parts of these tests.
   1611 
   1612         UnicodeString u16str("\\u1000\\U00011000\\u2000\\U00022000", -1, US_INV);
   1613         int32_t startMap[]  ={ 0,     1,   1,    3,     4,  4,     6,  6};
   1614         int32_t nextMap[]  = { 1,     3,   3,    4,     6,  6,     6,  6};
   1615         int32_t prevMap[]  = { 0,     0,   0,    1,     3,  3,     4,  4};
   1616         UChar32  c32Map[] =  {0x1000, 0x11000, 0x11000, 0x2000,  0x22000, 0x22000, -1, -1};
   1617         UChar32  pr32Map[] = {    -1, 0x1000,  0x1000,  0x11000, 0x2000,  0x2000,   0x22000,   0x22000};
   1618         int32_t  exLen[] =   {   1,  0,   2,  1,  0,  2,  0,  0,};
   1619 
   1620         u16str = u16str.unescape();
   1621         UErrorCode status = U_ZERO_ERROR;
   1622         UText *ut = utext_openReplaceable(NULL, &u16str, &status);
   1623         TEST_SUCCESS(status);
   1624 
   1625         int32_t startMapLimit = sizeof(startMap) / sizeof(int32_t);
   1626         int i;
   1627         for (i=0; i<startMapLimit; i++) {
   1628             utext_setNativeIndex(ut, i);
   1629             int64_t cpIndex = utext_getNativeIndex(ut);
   1630             TEST_ASSERT(cpIndex == startMap[i]);
   1631         }
   1632 
   1633         // Check char32At
   1634         for (i=0; i<startMapLimit; i++) {
   1635             UChar32 c32 = utext_char32At(ut, i);
   1636             TEST_ASSERT(c32 == c32Map[i]);
   1637             int64_t cpIndex = utext_getNativeIndex(ut);
   1638             TEST_ASSERT(cpIndex == startMap[i]);
   1639         }
   1640 
   1641         // Check utext_next32From
   1642         for (i=0; i<startMapLimit; i++) {
   1643             UChar32 c32 = utext_next32From(ut, i);
   1644             TEST_ASSERT(c32 == c32Map[i]);
   1645             int64_t cpIndex = utext_getNativeIndex(ut);
   1646             TEST_ASSERT(cpIndex == nextMap[i]);
   1647         }
   1648 
   1649         // check utext_previous32From
   1650         for (i=0; i<startMapLimit; i++) {
   1651             UChar32 c32 = utext_previous32From(ut, i);
   1652             TEST_ASSERT(c32 == pr32Map[i]);
   1653             int64_t cpIndex = utext_getNativeIndex(ut);
   1654             TEST_ASSERT(cpIndex == prevMap[i]);
   1655         }
   1656 
   1657         // check Extract
   1658         //   Extract from i to i+1, which may be zero or one code points,
   1659         //     depending on whether the indices straddle a cp boundary.
   1660         for (i=0; i<startMapLimit; i++) {
   1661             UChar buf[3];
   1662             status = U_ZERO_ERROR;
   1663             int32_t  extractedLen = utext_extract(ut, i, i+1, buf, 3, &status);
   1664             TEST_SUCCESS(status);
   1665             TEST_ASSERT(extractedLen == exLen[i]);
   1666             if (extractedLen > 0) {
   1667                 UChar32  c32;
   1668                 /* extractedLen-extractedLen == 0 is used to get around a compiler warning. */
   1669                 U16_GET(buf, 0, extractedLen-extractedLen, extractedLen, c32);
   1670                 TEST_ASSERT(c32 == c32Map[i]);
   1671             }
   1672         }
   1673 
   1674         utext_close(ut);
   1675     }
   1676 }
   1677 
   1678 
   1679 void UTextTest::FreezeTest() {
   1680     // Check isWritable() and freeze() behavior.
   1681     //
   1682 
   1683     UnicodeString  ustr("Hello, World.");
   1684     const char u8str[] = {char(0x31), (char)0x32, (char)0x33, 0};
   1685     const UChar u16str[] = {(UChar)0x31, (UChar)0x32, (UChar)0x44, 0};
   1686 
   1687     UErrorCode status = U_ZERO_ERROR;
   1688     UText  *ut        = NULL;
   1689     UText  *ut2       = NULL;
   1690 
   1691     ut = utext_openUTF8(ut, u8str, -1, &status);
   1692     TEST_SUCCESS(status);
   1693     UBool writable = utext_isWritable(ut);
   1694     TEST_ASSERT(writable == FALSE);
   1695     utext_copy(ut, 1, 2, 0, TRUE, &status);
   1696     TEST_ASSERT(status == U_NO_WRITE_PERMISSION);
   1697 
   1698     status = U_ZERO_ERROR;
   1699     ut = utext_openUChars(ut, u16str, -1, &status);
   1700     TEST_SUCCESS(status);
   1701     writable = utext_isWritable(ut);
   1702     TEST_ASSERT(writable == FALSE);
   1703     utext_copy(ut, 1, 2, 0, TRUE, &status);
   1704     TEST_ASSERT(status == U_NO_WRITE_PERMISSION);
   1705 
   1706     status = U_ZERO_ERROR;
   1707     ut = utext_openUnicodeString(ut, &ustr, &status);
   1708     TEST_SUCCESS(status);
   1709     writable = utext_isWritable(ut);
   1710     TEST_ASSERT(writable == TRUE);
   1711     utext_freeze(ut);
   1712     writable = utext_isWritable(ut);
   1713     TEST_ASSERT(writable == FALSE);
   1714     utext_copy(ut, 1, 2, 0, TRUE, &status);
   1715     TEST_ASSERT(status == U_NO_WRITE_PERMISSION);
   1716 
   1717     status = U_ZERO_ERROR;
   1718     ut = utext_openUnicodeString(ut, &ustr, &status);
   1719     TEST_SUCCESS(status);
   1720     ut2 = utext_clone(ut2, ut, FALSE, FALSE, &status);  // clone with readonly = false
   1721     TEST_SUCCESS(status);
   1722     writable = utext_isWritable(ut2);
   1723     TEST_ASSERT(writable == TRUE);
   1724     ut2 = utext_clone(ut2, ut, FALSE, TRUE, &status);  // clone with readonly = true
   1725     TEST_SUCCESS(status);
   1726     writable = utext_isWritable(ut2);
   1727     TEST_ASSERT(writable == FALSE);
   1728     utext_copy(ut2, 1, 2, 0, TRUE, &status);
   1729     TEST_ASSERT(status == U_NO_WRITE_PERMISSION);
   1730 
   1731     status = U_ZERO_ERROR;
   1732     ut = utext_openConstUnicodeString(ut, (const UnicodeString *)&ustr, &status);
   1733     TEST_SUCCESS(status);
   1734     writable = utext_isWritable(ut);
   1735     TEST_ASSERT(writable == FALSE);
   1736     utext_copy(ut, 1, 2, 0, TRUE, &status);
   1737     TEST_ASSERT(status == U_NO_WRITE_PERMISSION);
   1738 
   1739     // Deep Clone of a frozen UText should re-enable writing in the copy.
   1740     status = U_ZERO_ERROR;
   1741     ut = utext_openUnicodeString(ut, &ustr, &status);
   1742     TEST_SUCCESS(status);
   1743     utext_freeze(ut);
   1744     ut2 = utext_clone(ut2, ut, TRUE, FALSE, &status);   // deep clone
   1745     TEST_SUCCESS(status);
   1746     writable = utext_isWritable(ut2);
   1747     TEST_ASSERT(writable == TRUE);
   1748 
   1749 
   1750     // Deep clone of a frozen UText, where the base type is intrinsically non-writable,
   1751     //  should NOT enable writing in the copy.
   1752     status = U_ZERO_ERROR;
   1753     ut = utext_openUChars(ut, u16str, -1, &status);
   1754     TEST_SUCCESS(status);
   1755     utext_freeze(ut);
   1756     ut2 = utext_clone(ut2, ut, TRUE, FALSE, &status);   // deep clone
   1757     TEST_SUCCESS(status);
   1758     writable = utext_isWritable(ut2);
   1759     TEST_ASSERT(writable == FALSE);
   1760 
   1761     // cleanup
   1762     utext_close(ut);
   1763     utext_close(ut2);
   1764 }
   1765 
   1766 
   1767 //
   1768 //  Fragmented UText
   1769 //      A UText type that works with a chunk size of 1.
   1770 //      Intended to test for edge cases.
   1771 //      Input comes from a UnicodeString.
   1772 //
   1773 //       ut.b    the character.  Put into both halves.
   1774 //
   1775 
   1776 U_CDECL_BEGIN
   1777 static UBool U_CALLCONV
   1778 fragTextAccess(UText *ut, int64_t index, UBool forward) {
   1779     const UnicodeString *us = (const UnicodeString *)ut->context;
   1780     UChar  c;
   1781     int32_t length = us->length();
   1782     if (forward && index>=0 && index<length) {
   1783         c = us->charAt((int32_t)index);
   1784         ut->b = c | c<<16;
   1785         ut->chunkOffset = 0;
   1786         ut->chunkLength = 1;
   1787         ut->chunkNativeStart = index;
   1788         ut->chunkNativeLimit = index+1;
   1789         return true;
   1790     }
   1791     if (!forward && index>0 && index <=length) {
   1792         c = us->charAt((int32_t)index-1);
   1793         ut->b = c | c<<16;
   1794         ut->chunkOffset = 1;
   1795         ut->chunkLength = 1;
   1796         ut->chunkNativeStart = index-1;
   1797         ut->chunkNativeLimit = index;
   1798         return true;
   1799     }
   1800     ut->b = 0;
   1801     ut->chunkOffset = 0;
   1802     ut->chunkLength = 0;
   1803     if (index <= 0) {
   1804         ut->chunkNativeStart = 0;
   1805         ut->chunkNativeLimit = 0;
   1806     } else {
   1807         ut->chunkNativeStart = length;
   1808         ut->chunkNativeLimit = length;
   1809     }
   1810     return false;
   1811 }
   1812 
   1813 // Function table to be used with this fragmented text provider.
   1814 //   Initialized in the open function.
   1815 static UTextFuncs  fragmentFuncs;
   1816 
   1817 // Clone function for fragmented text provider.
   1818 //   Didn't really want to provide this, but it's easier to provide it than to keep it
   1819 //   out of the tests.
   1820 //
   1821 UText *
   1822 cloneFragmentedUnicodeString(UText *dest, const UText *src, UBool deep, UErrorCode *status) {
   1823     if (U_FAILURE(*status)) {
   1824         return NULL;
   1825     }
   1826     if (deep) {
   1827         *status = U_UNSUPPORTED_ERROR;
   1828         return NULL;
   1829     }
   1830     dest = utext_openUnicodeString(dest, (UnicodeString *)src->context, status);
   1831     utext_setNativeIndex(dest, utext_getNativeIndex(src));
   1832     return dest;
   1833 }
   1834 
   1835 U_CDECL_END
   1836 
   1837 // Open function for the fragmented text provider.
   1838 UText *
   1839 openFragmentedUnicodeString(UText *ut, UnicodeString *s, UErrorCode *status) {
   1840     ut = utext_openUnicodeString(ut, s, status);
   1841     if (U_FAILURE(*status)) {
   1842         return ut;
   1843     }
   1844 
   1845     // Copy of the function table from the stock UnicodeString UText,
   1846     //   and replace the entry for the access function.
   1847     memcpy(&fragmentFuncs, ut->pFuncs, sizeof(fragmentFuncs));
   1848     fragmentFuncs.access = fragTextAccess;
   1849     fragmentFuncs.clone  = cloneFragmentedUnicodeString;
   1850     ut->pFuncs = &fragmentFuncs;
   1851 
   1852     ut->chunkContents = (UChar *)&ut->b;
   1853     ut->pFuncs->access(ut, 0, TRUE);
   1854     return ut;
   1855 }
   1856 
   1857 // Regression test for Ticket 5560
   1858 //   Clone fails to update chunkContentPointer in the cloned copy.
   1859 //   This is only an issue for UText types that work in a local buffer,
   1860 //      (UTF-8 wrapper, for example)
   1861 //
   1862 //   The test:
   1863 //     1.  Create an inital UText
   1864 //     2.  Deep clone it.  Contents should match original.
   1865 //     3.  Reset original to something different.
   1866 //     4.  Check that clone contents did not change.
   1867 //
   1868 void UTextTest::Ticket5560() {
   1869     /* The following two strings are in UTF-8 even on EBCDIC platforms. */
   1870     static const char s1[] = {0x41,0x42,0x43,0x44,0x45,0x46,0}; /* "ABCDEF" */
   1871     static const char s2[] = {0x31,0x32,0x33,0x34,0x35,0x36,0}; /* "123456" */
   1872 	UErrorCode status = U_ZERO_ERROR;
   1873 
   1874 	UText ut1 = UTEXT_INITIALIZER;
   1875 	UText ut2 = UTEXT_INITIALIZER;
   1876 
   1877 	utext_openUTF8(&ut1, s1, -1, &status);
   1878 	UChar c = utext_next32(&ut1);
   1879 	TEST_ASSERT(c == 0x41);  // c == 'A'
   1880 
   1881 	utext_clone(&ut2, &ut1, TRUE, FALSE, &status);
   1882 	TEST_SUCCESS(status);
   1883     c = utext_next32(&ut2);
   1884 	TEST_ASSERT(c == 0x42);  // c == 'B'
   1885     c = utext_next32(&ut1);
   1886 	TEST_ASSERT(c == 0x42);  // c == 'B'
   1887 
   1888 	utext_openUTF8(&ut1, s2, -1, &status);
   1889 	c = utext_next32(&ut1);
   1890 	TEST_ASSERT(c == 0x31);  // c == '1'
   1891     c = utext_next32(&ut2);
   1892 	TEST_ASSERT(c == 0x43);  // c == 'C'
   1893 
   1894     utext_close(&ut1);
   1895     utext_close(&ut2);
   1896 }
   1897 
   1898 
   1899 // Test for Ticket 6847
   1900 //
   1901 void UTextTest::Ticket6847() {
   1902     const int STRLEN = 90;
   1903     UChar s[STRLEN+1];
   1904     u_memset(s, 0x41, STRLEN);
   1905     s[STRLEN] = 0;
   1906 
   1907     UErrorCode status = U_ZERO_ERROR;
   1908     UText *ut = utext_openUChars(NULL, s, -1, &status);
   1909 
   1910     utext_setNativeIndex(ut, 0);
   1911     int32_t count = 0;
   1912     UChar32 c = 0;
   1913     int32_t nativeIndex = UTEXT_GETNATIVEINDEX(ut);
   1914     TEST_ASSERT(nativeIndex == 0);
   1915     while ((c = utext_next32(ut)) != U_SENTINEL) {
   1916         TEST_ASSERT(c == 0x41);
   1917         TEST_ASSERT(count < STRLEN);
   1918         if (count >= STRLEN) {
   1919             break;
   1920         }
   1921         count++;
   1922         nativeIndex = UTEXT_GETNATIVEINDEX(ut);
   1923         TEST_ASSERT(nativeIndex == count);
   1924     }
   1925     TEST_ASSERT(count == STRLEN);
   1926     nativeIndex = UTEXT_GETNATIVEINDEX(ut);
   1927     TEST_ASSERT(nativeIndex == STRLEN);
   1928     utext_close(ut);
   1929 }
   1930 
   1931