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