1 /********************************************************************
3 * Copyright (c) 2005-2009, International Business Machines Corporation and
4 * others. All Rights Reserved.
5 ********************************************************************/
6 /************************************************************************
7 * Tests for the UText and UTextIterator text abstraction classses
9 ************************************************************************/
11 #include "unicode/utypes.h"
16 #include <unicode/utext.h>
17 #include <unicode/utf8.h>
18 #include <unicode/ustring.h>
19 #include <unicode/uchriter.h>
22 static UBool gFailed
= FALSE
;
23 static int gTestNum
= 0;
26 UText
*openFragmentedUnicodeString(UText
*ut
, UnicodeString
*s
, UErrorCode
*status
);
28 #define TEST_ASSERT(x) \
29 { if ((x)==FALSE) {errln("Test #%d failure in file %s at line %d\n", gTestNum, __FILE__, __LINE__);\
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)); \
40 UTextTest::UTextTest() {
43 UTextTest::~UTextTest() {
48 UTextTest::runIndexedTest(int32_t index
, UBool exec
,
49 const char* &name
, char* /*par*/) {
51 case 0: name
= "TextTest";
52 if (exec
) TextTest(); break;
53 case 1: name
= "ErrorTest";
54 if (exec
) ErrorTest(); break;
55 case 2: name
= "FreezeTest";
56 if (exec
) FreezeTest(); break;
57 case 3: name
= "Ticket5560";
58 if (exec
) Ticket5560(); break;
59 case 4: name
= "Ticket6847";
60 if (exec
) Ticket6847(); break;
61 default: name
= ""; break;
66 // Quick and dirty random number generator.
67 // (don't use library so that results are portable.
68 static uint32_t m_seed
= 1;
69 static uint32_t m_rand()
71 m_seed
= m_seed
* 1103515245 + 12345;
72 return (uint32_t)(m_seed
/65536) % 32768;
79 // Top Level function for UText testing.
80 // Specifies the strings to be tested, with the acutal testing itself
81 // being carried out in another function, TestString().
83 void UTextTest::TextTest() {
86 TestString("abcd\\U00010001xyz");
89 // Supplementary chars at start or end
90 TestString("\\U00010001");
91 TestString("abc\\U00010001");
92 TestString("\\U00010001abc");
94 // Test simple strings of lengths 1 to 60, looking for glitches at buffer boundaries
96 for (i
=1; i
<60; i
++) {
100 // backslash. Needs to be escaped
101 s
.append((UChar
)0x5c);
103 s
.append(UChar(j
+0x30));
108 // Test strings with odd-aligned supplementary chars,
109 // looking for glitches at buffer boundaries
110 for (i
=1; i
<60; i
++) {
112 s
.append((UChar
)0x41);
113 for (j
=0; j
<i
; j
++) {
114 s
.append(UChar32(j
+0x11000));
119 // String of chars of randomly varying size in utf-8 representation.
120 // Exercise the mapping, and the varying sized buffer.
126 UChar32 c4
= 0x11000;
127 for (i
=0; i
<1000; i
++) {
128 int len8
= m_rand()%4
+ 1;
132 // don't put 0 into string (0 terminated strings for some tests)
133 // don't put '\', will cause unescape() to fail.
134 if (c1
==0x5c || c1
==0) {
155 // TestString() Run a suite of UText tests on a string.
156 // The test string is unescaped before use.
158 void UTextTest::TestString(const UnicodeString
&s
) {
163 UErrorCode status
= U_ZERO_ERROR
;
167 UnicodeString sa
= s
.unescape();
171 // Build up a mapping between code points and UTF-16 code unit indexes.
173 m
*cpMap
= new m
[sa
.length() + 1];
175 for (i
=0; i
<sa
.length(); i
=sa
.moveIndex32(i
, 1)) {
177 cpMap
[j
].nativeIdx
= i
;
182 cpMap
[j
].nativeIdx
= i
; // position following the last char in utf-16 string.
185 // UChar * test, null terminated
186 status
= U_ZERO_ERROR
;
187 UChar
*buf
= new UChar
[saLen
+1];
188 sa
.extract(buf
, saLen
+1, status
);
189 TEST_SUCCESS(status
);
190 ut
= utext_openUChars(NULL
, buf
, -1, &status
);
191 TEST_SUCCESS(status
);
192 TestAccess(sa
, ut
, cpCount
, cpMap
);
196 // UChar * test, with length
197 status
= U_ZERO_ERROR
;
198 buf
= new UChar
[saLen
+1];
199 sa
.extract(buf
, saLen
+1, status
);
200 TEST_SUCCESS(status
);
201 ut
= utext_openUChars(NULL
, buf
, saLen
, &status
);
202 TEST_SUCCESS(status
);
203 TestAccess(sa
, ut
, cpCount
, cpMap
);
208 // UnicodeString test
209 status
= U_ZERO_ERROR
;
210 ut
= utext_openUnicodeString(NULL
, &sa
, &status
);
211 TEST_SUCCESS(status
);
212 TestAccess(sa
, ut
, cpCount
, cpMap
);
213 TestCMR(sa
, ut
, cpCount
, cpMap
, cpMap
);
217 // Const UnicodeString test
218 status
= U_ZERO_ERROR
;
219 ut
= utext_openConstUnicodeString(NULL
, &sa
, &status
);
220 TEST_SUCCESS(status
);
221 TestAccess(sa
, ut
, cpCount
, cpMap
);
225 // Replaceable test. (UnicodeString inherits Replaceable)
226 status
= U_ZERO_ERROR
;
227 ut
= utext_openReplaceable(NULL
, &sa
, &status
);
228 TEST_SUCCESS(status
);
229 TestAccess(sa
, ut
, cpCount
, cpMap
);
230 TestCMR(sa
, ut
, cpCount
, cpMap
, cpMap
);
233 // Character Iterator Tests
234 status
= U_ZERO_ERROR
;
235 const UChar
*cbuf
= sa
.getBuffer();
236 CharacterIterator
*ci
= new UCharCharacterIterator(cbuf
, saLen
, status
);
237 TEST_SUCCESS(status
);
238 ut
= utext_openCharacterIterator(NULL
, ci
, &status
);
239 TEST_SUCCESS(status
);
240 TestAccess(sa
, ut
, cpCount
, cpMap
);
245 // Fragmented UnicodeString (Chunk size of one)
247 status
= U_ZERO_ERROR
;
248 ut
= openFragmentedUnicodeString(NULL
, &sa
, &status
);
249 TEST_SUCCESS(status
);
250 TestAccess(sa
, ut
, cpCount
, cpMap
);
257 // Convert the test string from UnicodeString to (char *) in utf-8 format
258 int32_t u8Len
= sa
.extract(0, sa
.length(), NULL
, 0, "utf-8");
259 char *u8String
= new char[u8Len
+ 1];
260 sa
.extract(0, sa
.length(), u8String
, u8Len
+1, "utf-8");
262 // Build up the map of code point indices in the utf-8 string
263 m
* u8Map
= new m
[sa
.length() + 1];
264 i
= 0; // native utf-8 index
265 for (j
=0; j
<cpCount
; j
++) { // code point number
266 u8Map
[j
].nativeIdx
= i
;
267 U8_NEXT(u8String
, i
, u8Len
, c
)
270 u8Map
[cpCount
].nativeIdx
= u8Len
; // position following the last char in utf-8 string.
272 // Do the test itself
273 status
= U_ZERO_ERROR
;
274 ut
= utext_openUTF8(NULL
, u8String
, -1, &status
);
275 TEST_SUCCESS(status
);
276 TestAccess(sa
, ut
, cpCount
, u8Map
);
286 // TestCMR test Copy, Move and Replace operations.
287 // us UnicodeString containing the test text.
288 // ut UText containing the same test text.
289 // cpCount number of code points in the test text.
290 // nativeMap Mapping from code points to native indexes for the UText.
291 // u16Map Mapping from code points to UTF-16 indexes, for use with the UnicodeString.
293 // This function runs a whole series of opertions on each incoming UText.
294 // The UText is deep-cloned prior to each operation, so that the original UText remains unchanged.
296 void UTextTest::TestCMR(const UnicodeString
&us
, UText
*ut
, int cpCount
, m
*nativeMap
, m
*u16Map
) {
297 TEST_ASSERT(utext_isWritable(ut
) == TRUE
);
299 int srcLengthType
; // Loop variables for selecting the postion and length
300 int srcPosType
; // of the block to operate on within the source text.
303 int srcIndex
= 0; // Code Point indexes of the block to operate on for
304 int srcLength
= 0; // a specific test.
306 int destIndex
= 0; // Code point index of the destination for a copy/move test.
308 int32_t nativeStart
= 0; // Native unit indexes for a test.
309 int32_t nativeLimit
= 0;
310 int32_t nativeDest
= 0;
312 int32_t u16Start
= 0; // UTF-16 indexes for a test.
313 int32_t u16Limit
= 0; // used when performing the same operation in a Unicode String
316 // Iterate over a whole series of source index, length and a target indexes.
317 // This is done with code point indexes; these will be later translated to native
318 // indexes using the cpMap.
319 for (srcLengthType
=1; srcLengthType
<=3; srcLengthType
++) {
320 switch (srcLengthType
) {
321 case 1: srcLength
= 1; break;
322 case 2: srcLength
= 5; break;
323 case 3: srcLength
= cpCount
/ 3;
325 for (srcPosType
=1; srcPosType
<=5; srcPosType
++) {
326 switch (srcPosType
) {
327 case 1: srcIndex
= 0; break;
328 case 2: srcIndex
= 1; break;
329 case 3: srcIndex
= cpCount
- srcLength
; break;
330 case 4: srcIndex
= cpCount
- srcLength
- 1; break;
331 case 5: srcIndex
= cpCount
/ 2; break;
333 if (srcIndex
< 0 || srcIndex
+ srcLength
> cpCount
) {
334 // filter out bogus test cases -
335 // those with a source range that falls of an edge of the string.
340 // Copy and move tests.
341 // iterate over a variety of destination positions.
343 for (destPosType
=1; destPosType
<=4; destPosType
++) {
344 switch (destPosType
) {
345 case 1: destIndex
= 0; break;
346 case 2: destIndex
= 1; break;
347 case 3: destIndex
= srcIndex
- 1; break;
348 case 4: destIndex
= srcIndex
+ srcLength
+ 1; break;
349 case 5: destIndex
= cpCount
-1; break;
350 case 6: destIndex
= cpCount
; break;
352 if (destIndex
<0 || destIndex
>cpCount
) {
353 // filter out bogus test cases.
357 nativeStart
= nativeMap
[srcIndex
].nativeIdx
;
358 nativeLimit
= nativeMap
[srcIndex
+srcLength
].nativeIdx
;
359 nativeDest
= nativeMap
[destIndex
].nativeIdx
;
361 u16Start
= u16Map
[srcIndex
].nativeIdx
;
362 u16Limit
= u16Map
[srcIndex
+srcLength
].nativeIdx
;
363 u16Dest
= u16Map
[destIndex
].nativeIdx
;
366 TestCopyMove(us
, ut
, FALSE
,
367 nativeStart
, nativeLimit
, nativeDest
,
368 u16Start
, u16Limit
, u16Dest
);
370 TestCopyMove(us
, ut
, TRUE
,
371 nativeStart
, nativeLimit
, nativeDest
,
372 u16Start
, u16Limit
, u16Dest
);
382 UnicodeString
fullRepString("This is an arbitrary string that will be used as replacement text");
383 for (int32_t replStrLen
=0; replStrLen
<20; replStrLen
++) {
384 UnicodeString
repStr(fullRepString
, 0, replStrLen
);
386 nativeStart
, nativeLimit
,
400 // TestCopyMove run a single test case for utext_copy.
401 // Test cases are created in TestCMR and dispatched here for execution.
403 void UTextTest::TestCopyMove(const UnicodeString
&us
, UText
*ut
, UBool move
,
404 int32_t nativeStart
, int32_t nativeLimit
, int32_t nativeDest
,
405 int32_t u16Start
, int32_t u16Limit
, int32_t u16Dest
)
407 UErrorCode status
= U_ZERO_ERROR
;
408 UText
*targetUT
= NULL
;
413 // clone the UText. The test will be run in the cloned copy
414 // so that we don't alter the original.
416 targetUT
= utext_clone(NULL
, ut
, TRUE
, FALSE
, &status
);
417 TEST_SUCCESS(status
);
418 UnicodeString
targetUS(us
); // And copy the reference string.
420 // do the test operation first in the reference
421 targetUS
.copy(u16Start
, u16Limit
, u16Dest
);
423 // delete out the source range.
424 if (u16Limit
< u16Dest
) {
425 targetUS
.removeBetween(u16Start
, u16Limit
);
427 int32_t amtCopied
= u16Limit
- u16Start
;
428 targetUS
.removeBetween(u16Start
+amtCopied
, u16Limit
+amtCopied
);
432 // Do the same operation in the UText under test
433 utext_copy(targetUT
, nativeStart
, nativeLimit
, nativeDest
, move
, &status
);
434 if (nativeDest
> nativeStart
&& nativeDest
< nativeLimit
) {
435 TEST_ASSERT(status
== U_INDEX_OUTOFBOUNDS_ERROR
);
437 TEST_SUCCESS(status
);
439 // Compare the results of the two parallel tests
440 int32_t usi
= 0; // UnicodeString postion, utf-16 index.
441 int64_t uti
= 0; // UText position, native index.
442 int32_t cpi
; // char32 position (code point index)
443 UChar32 usc
; // code point from Unicode String
444 UChar32 utc
; // code point from UText
445 utext_setNativeIndex(targetUT
, 0);
446 for (cpi
=0; ; cpi
++) {
447 usc
= targetUS
.char32At(usi
);
448 utc
= utext_next32(targetUT
);
452 TEST_ASSERT(uti
== usi
);
453 TEST_ASSERT(utc
== usc
);
454 usi
= targetUS
.moveIndex32(usi
, 1);
455 uti
= utext_getNativeIndex(targetUT
);
457 goto cleanupAndReturn
;
460 int64_t expectedNativeLength
= utext_nativeLength(ut
);
462 expectedNativeLength
+= nativeLimit
- nativeStart
;
464 uti
= utext_getNativeIndex(targetUT
);
465 TEST_ASSERT(uti
== expectedNativeLength
);
469 utext_close(targetUT
);
474 // TestReplace Test a single Replace operation.
476 void UTextTest::TestReplace(
477 const UnicodeString
&us
, // reference UnicodeString in which to do the replace
478 UText
*ut
, // UnicodeText object under test.
479 int32_t nativeStart
, // Range to be replaced, in UText native units.
481 int32_t u16Start
, // Range to be replaced, in UTF-16 units
482 int32_t u16Limit
, // for use in the reference UnicodeString.
483 const UnicodeString
&repStr
) // The replacement string
485 UErrorCode status
= U_ZERO_ERROR
;
486 UText
*targetUT
= NULL
;
491 // clone the target UText. The test will be run in the cloned copy
492 // so that we don't alter the original.
494 targetUT
= utext_clone(NULL
, ut
, TRUE
, FALSE
, &status
);
495 TEST_SUCCESS(status
);
496 UnicodeString
targetUS(us
); // And copy the reference string.
499 // Do the replace operation in the Unicode String, to
500 // produce a reference result.
502 targetUS
.replace(u16Start
, u16Limit
-u16Start
, repStr
);
505 // Do the replace on the UText under test
507 const UChar
*rs
= repStr
.getBuffer();
508 int32_t rsLen
= repStr
.length();
509 int32_t actualDelta
= utext_replace(targetUT
, nativeStart
, nativeLimit
, rs
, rsLen
, &status
);
510 int32_t expectedDelta
= repStr
.length() - (nativeLimit
- nativeStart
);
511 TEST_ASSERT(actualDelta
== expectedDelta
);
514 // Compare the results
516 int32_t usi
= 0; // UnicodeString postion, utf-16 index.
517 int64_t uti
= 0; // UText position, native index.
518 int32_t cpi
; // char32 position (code point index)
519 UChar32 usc
; // code point from Unicode String
520 UChar32 utc
; // code point from UText
521 int64_t expectedNativeLength
= 0;
522 utext_setNativeIndex(targetUT
, 0);
523 for (cpi
=0; ; cpi
++) {
524 usc
= targetUS
.char32At(usi
);
525 utc
= utext_next32(targetUT
);
529 TEST_ASSERT(uti
== usi
);
530 TEST_ASSERT(utc
== usc
);
531 usi
= targetUS
.moveIndex32(usi
, 1);
532 uti
= utext_getNativeIndex(targetUT
);
534 goto cleanupAndReturn
;
537 expectedNativeLength
= utext_nativeLength(ut
) + expectedDelta
;
538 uti
= utext_getNativeIndex(targetUT
);
539 TEST_ASSERT(uti
== expectedNativeLength
);
542 utext_close(targetUT
);
546 // TestAccess Test the read only access functions on a UText, including cloning.
547 // The text is accessed in a variety of ways, and compared with
548 // the reference UnicodeString.
550 void UTextTest::TestAccess(const UnicodeString
&us
, UText
*ut
, int cpCount
, m
*cpMap
) {
551 // Run the standard tests on the caller-supplied UText.
552 TestAccessNoClone(us
, ut
, cpCount
, cpMap
);
554 // Re-run tests on a shallow clone.
555 utext_setNativeIndex(ut
, 0);
556 UErrorCode status
= U_ZERO_ERROR
;
557 UText
*shallowClone
= utext_clone(NULL
, ut
, FALSE
/*deep*/, FALSE
/*readOnly*/, &status
);
558 TEST_SUCCESS(status
);
559 TestAccessNoClone(us
, shallowClone
, cpCount
, cpMap
);
562 // Rerun again on a deep clone.
563 // Note that text providers are not required to provide deep cloning,
564 // so unsupported errors are ignored.
566 status
= U_ZERO_ERROR
;
567 utext_setNativeIndex(shallowClone
, 0);
568 UText
*deepClone
= utext_clone(NULL
, shallowClone
, TRUE
, FALSE
, &status
);
569 utext_close(shallowClone
);
570 if (status
!= U_UNSUPPORTED_ERROR
) {
571 TEST_SUCCESS(status
);
572 TestAccessNoClone(us
, deepClone
, cpCount
, cpMap
);
574 utext_close(deepClone
);
579 // TestAccessNoClone() Test the read only access functions on a UText.
580 // The text is accessed in a variety of ways, and compared with
581 // the reference UnicodeString.
583 void UTextTest::TestAccessNoClone(const UnicodeString
&us
, UText
*ut
, int cpCount
, m
*cpMap
) {
584 UErrorCode status
= U_ZERO_ERROR
;
588 // Check the length from the UText
590 int64_t expectedLen
= cpMap
[cpCount
].nativeIdx
;
591 int64_t utlen
= utext_nativeLength(ut
);
592 TEST_ASSERT(expectedLen
== utlen
);
595 // Iterate forwards, verify that we get the correct code points
596 // at the correct native offsets.
600 int64_t expectedIndex
= 0;
601 int64_t foundIndex
= 0;
606 for (i
=0; i
<cpCount
; i
++) {
607 expectedIndex
= cpMap
[i
].nativeIdx
;
608 foundIndex
= utext_getNativeIndex(ut
);
609 TEST_ASSERT(expectedIndex
== foundIndex
);
610 expectedC
= cpMap
[i
].cp
;
611 foundC
= utext_next32(ut
);
612 TEST_ASSERT(expectedC
== foundC
);
613 foundIndex
= utext_getPreviousNativeIndex(ut
);
614 TEST_ASSERT(expectedIndex
== foundIndex
);
619 foundC
= utext_next32(ut
);
620 TEST_ASSERT(foundC
== U_SENTINEL
);
622 // Repeat above, using macros
623 utext_setNativeIndex(ut
, 0);
624 for (i
=0; i
<cpCount
; i
++) {
625 expectedIndex
= cpMap
[i
].nativeIdx
;
626 foundIndex
= UTEXT_GETNATIVEINDEX(ut
);
627 TEST_ASSERT(expectedIndex
== foundIndex
);
628 expectedC
= cpMap
[i
].cp
;
629 foundC
= UTEXT_NEXT32(ut
);
630 TEST_ASSERT(expectedC
== foundC
);
635 foundC
= UTEXT_NEXT32(ut
);
636 TEST_ASSERT(foundC
== U_SENTINEL
);
639 // Forward iteration (above) should have left index at the
640 // end of the input, which should == length().
642 len
= utext_nativeLength(ut
);
643 foundIndex
= utext_getNativeIndex(ut
);
644 TEST_ASSERT(len
== foundIndex
);
647 // Iterate backwards over entire test string
649 len
= utext_getNativeIndex(ut
);
650 utext_setNativeIndex(ut
, len
);
651 for (i
=cpCount
-1; i
>=0; i
--) {
652 expectedC
= cpMap
[i
].cp
;
653 expectedIndex
= cpMap
[i
].nativeIdx
;
654 int64_t prevIndex
= utext_getPreviousNativeIndex(ut
);
655 foundC
= utext_previous32(ut
);
656 foundIndex
= utext_getNativeIndex(ut
);
657 TEST_ASSERT(expectedIndex
== foundIndex
);
658 TEST_ASSERT(expectedC
== foundC
);
659 TEST_ASSERT(prevIndex
== foundIndex
);
666 // Backwards iteration, above, should have left our iterator
667 // position at zero, and continued backwards iterationshould fail.
669 foundIndex
= utext_getNativeIndex(ut
);
670 TEST_ASSERT(foundIndex
== 0);
671 foundIndex
= utext_getPreviousNativeIndex(ut
);
672 TEST_ASSERT(foundIndex
== 0);
675 foundC
= utext_previous32(ut
);
676 TEST_ASSERT(foundC
== U_SENTINEL
);
677 foundIndex
= utext_getNativeIndex(ut
);
678 TEST_ASSERT(foundIndex
== 0);
679 foundIndex
= utext_getPreviousNativeIndex(ut
);
680 TEST_ASSERT(foundIndex
== 0);
683 // And again, with the macros
684 utext_setNativeIndex(ut
, len
);
685 for (i
=cpCount
-1; i
>=0; i
--) {
686 expectedC
= cpMap
[i
].cp
;
687 expectedIndex
= cpMap
[i
].nativeIdx
;
688 foundC
= UTEXT_PREVIOUS32(ut
);
689 foundIndex
= UTEXT_GETNATIVEINDEX(ut
);
690 TEST_ASSERT(expectedIndex
== foundIndex
);
691 TEST_ASSERT(expectedC
== foundC
);
698 // Backwards iteration, above, should have left our iterator
699 // position at zero, and continued backwards iterationshould fail.
701 foundIndex
= UTEXT_GETNATIVEINDEX(ut
);
702 TEST_ASSERT(foundIndex
== 0);
704 foundC
= UTEXT_PREVIOUS32(ut
);
705 TEST_ASSERT(foundC
== U_SENTINEL
);
706 foundIndex
= UTEXT_GETNATIVEINDEX(ut
);
707 TEST_ASSERT(foundIndex
== 0);
713 // next32From(), prevous32From(), Iterate in a somewhat random order.
716 for (i
=0; i
<cpCount
; i
++) {
717 cpIndex
= (cpIndex
+ 9973) % cpCount
;
718 index
= cpMap
[cpIndex
].nativeIdx
;
719 expectedC
= cpMap
[cpIndex
].cp
;
720 foundC
= utext_next32From(ut
, index
);
721 TEST_ASSERT(expectedC
== foundC
);
728 for (i
=0; i
<cpCount
; i
++) {
729 cpIndex
= (cpIndex
+ 9973) % cpCount
;
730 index
= cpMap
[cpIndex
+1].nativeIdx
;
731 expectedC
= cpMap
[cpIndex
].cp
;
732 foundC
= utext_previous32From(ut
, index
);
733 TEST_ASSERT(expectedC
== foundC
);
741 // moveIndex(int32_t delta);
744 // Walk through frontwards, incrementing by one
745 utext_setNativeIndex(ut
, 0);
746 for (i
=1; i
<=cpCount
; i
++) {
747 utext_moveIndex32(ut
, 1);
748 index
= utext_getNativeIndex(ut
);
749 expectedIndex
= cpMap
[i
].nativeIdx
;
750 TEST_ASSERT(expectedIndex
== index
);
751 index
= UTEXT_GETNATIVEINDEX(ut
);
752 TEST_ASSERT(expectedIndex
== index
);
755 // Walk through frontwards, incrementing by two
756 utext_setNativeIndex(ut
, 0);
757 for (i
=2; i
<cpCount
; i
+=2) {
758 utext_moveIndex32(ut
, 2);
759 index
= utext_getNativeIndex(ut
);
760 expectedIndex
= cpMap
[i
].nativeIdx
;
761 TEST_ASSERT(expectedIndex
== index
);
762 index
= UTEXT_GETNATIVEINDEX(ut
);
763 TEST_ASSERT(expectedIndex
== index
);
766 // walk through the string backwards, decrementing by one.
767 i
= cpMap
[cpCount
].nativeIdx
;
768 utext_setNativeIndex(ut
, i
);
769 for (i
=cpCount
; i
>=0; i
--) {
770 expectedIndex
= cpMap
[i
].nativeIdx
;
771 index
= utext_getNativeIndex(ut
);
772 TEST_ASSERT(expectedIndex
== index
);
773 index
= UTEXT_GETNATIVEINDEX(ut
);
774 TEST_ASSERT(expectedIndex
== index
);
775 utext_moveIndex32(ut
, -1);
779 // walk through backwards, decrementing by three
780 i
= cpMap
[cpCount
].nativeIdx
;
781 utext_setNativeIndex(ut
, i
);
782 for (i
=cpCount
; i
>=0; i
-=3) {
783 expectedIndex
= cpMap
[i
].nativeIdx
;
784 index
= utext_getNativeIndex(ut
);
785 TEST_ASSERT(expectedIndex
== index
);
786 index
= UTEXT_GETNATIVEINDEX(ut
);
787 TEST_ASSERT(expectedIndex
== index
);
788 utext_moveIndex32(ut
, -3);
795 int bufSize
= us
.length() + 10;
796 UChar
*buf
= new UChar
[bufSize
];
797 status
= U_ZERO_ERROR
;
798 expectedLen
= us
.length();
799 len
= utext_extract(ut
, 0, utlen
, buf
, bufSize
, &status
);
800 TEST_SUCCESS(status
);
801 TEST_ASSERT(len
== expectedLen
);
802 int compareResult
= us
.compare(buf
, -1);
803 TEST_ASSERT(compareResult
== 0);
805 status
= U_ZERO_ERROR
;
806 len
= utext_extract(ut
, 0, utlen
, NULL
, 0, &status
);
808 TEST_ASSERT(status
== U_STRING_NOT_TERMINATED_WARNING
);
810 TEST_ASSERT(status
== U_BUFFER_OVERFLOW_ERROR
);
812 TEST_ASSERT(len
== expectedLen
);
814 status
= U_ZERO_ERROR
;
815 u_memset(buf
, 0x5555, bufSize
);
816 len
= utext_extract(ut
, 0, utlen
, buf
, 1, &status
);
817 if (us
.length() == 0) {
818 TEST_SUCCESS(status
);
819 TEST_ASSERT(buf
[0] == 0);
821 // Buf len == 1, extracting a single 16 bit value.
822 // If the data char is supplementary, it doesn't matter whether the buffer remains unchanged,
823 // or whether the lead surrogate of the pair is extracted.
824 // It's a buffer overflow error in either case.
825 TEST_ASSERT(buf
[0] == us
.charAt(0) ||
826 buf
[0] == 0x5555 && U_IS_SUPPLEMENTARY(us
.char32At(0)));
827 TEST_ASSERT(buf
[1] == 0x5555);
828 if (us
.length() == 1) {
829 TEST_ASSERT(status
== U_STRING_NOT_TERMINATED_WARNING
);
831 TEST_ASSERT(status
== U_BUFFER_OVERFLOW_ERROR
);
841 // ErrorTest() Check various error and edge cases.
843 void UTextTest::ErrorTest()
845 // Close of an unitialized UText. Shouldn't blow up.
848 memset(&ut
, 0, sizeof(UText
));
853 // Double-close of a UText. Shouldn't blow up. UText should still be usable.
855 UErrorCode status
= U_ZERO_ERROR
;
856 UText ut
= UTEXT_INITIALIZER
;
857 UnicodeString
s("Hello, World");
858 UText
*ut2
= utext_openUnicodeString(&ut
, &s
, &status
);
859 TEST_SUCCESS(status
);
860 TEST_ASSERT(ut2
== &ut
);
862 UText
*ut3
= utext_close(&ut
);
863 TEST_ASSERT(ut3
== &ut
);
865 UText
*ut4
= utext_close(&ut
);
866 TEST_ASSERT(ut4
== &ut
);
868 utext_openUnicodeString(&ut
, &s
, &status
);
869 TEST_SUCCESS(status
);
873 // Re-use of a UText, chaining through each of the types of UText
874 // (If it doesn't blow up, and doesn't leak, it's probably working fine)
876 UErrorCode status
= U_ZERO_ERROR
;
877 UText ut
= UTEXT_INITIALIZER
;
879 UnicodeString
s1("Hello, World");
880 UChar s2
[] = {(UChar
)0x41, (UChar
)0x42, (UChar
)0};
881 const char *s3
= "\x66\x67\x68";
883 utp
= utext_openUnicodeString(&ut
, &s1
, &status
);
884 TEST_SUCCESS(status
);
885 TEST_ASSERT(utp
== &ut
);
887 utp
= utext_openConstUnicodeString(&ut
, &s1
, &status
);
888 TEST_SUCCESS(status
);
889 TEST_ASSERT(utp
== &ut
);
891 utp
= utext_openUTF8(&ut
, s3
, -1, &status
);
892 TEST_SUCCESS(status
);
893 TEST_ASSERT(utp
== &ut
);
895 utp
= utext_openUChars(&ut
, s2
, -1, &status
);
896 TEST_SUCCESS(status
);
897 TEST_ASSERT(utp
== &ut
);
899 utp
= utext_close(&ut
);
900 TEST_ASSERT(utp
== &ut
);
902 utp
= utext_openUnicodeString(&ut
, &s1
, &status
);
903 TEST_SUCCESS(status
);
904 TEST_ASSERT(utp
== &ut
);
908 // UTF-8 with malformed sequences.
909 // These should come through as the Unicode replacement char, \ufffd
912 UErrorCode status
= U_ZERO_ERROR
;
914 const char *badUTF8
= "\x41\x81\x42\xf0\x81\x81\x43";
917 ut
= utext_openUTF8(NULL
, badUTF8
, -1, &status
);
918 TEST_SUCCESS(status
);
919 c
= utext_char32At(ut
, 1);
920 TEST_ASSERT(c
== 0xfffd);
921 c
= utext_char32At(ut
, 3);
922 TEST_ASSERT(c
== 0xfffd);
923 c
= utext_char32At(ut
, 5);
924 TEST_ASSERT(c
== 0xfffd);
925 c
= utext_char32At(ut
, 6);
926 TEST_ASSERT(c
== 0x43);
929 int n
= utext_extract(ut
, 0, 9, buf
, 10, &status
);
930 TEST_SUCCESS(status
);
932 TEST_ASSERT(buf
[1] == 0xfffd);
933 TEST_ASSERT(buf
[3] == 0xfffd);
934 TEST_ASSERT(buf
[2] == 0x42);
940 // isLengthExpensive - does it make the exptected transitions after
941 // getting the length of a nul terminated string?
944 UErrorCode status
= U_ZERO_ERROR
;
945 UnicodeString
sa("Hello, this is a string");
949 memset(sb
, 0x20, sizeof(sb
));
952 UText
*uta
= utext_openUnicodeString(NULL
, &sa
, &status
);
953 TEST_SUCCESS(status
);
954 isExpensive
= utext_isLengthExpensive(uta
);
955 TEST_ASSERT(isExpensive
== FALSE
);
958 UText
*utb
= utext_openUChars(NULL
, sb
, -1, &status
);
959 TEST_SUCCESS(status
);
960 isExpensive
= utext_isLengthExpensive(utb
);
961 TEST_ASSERT(isExpensive
== TRUE
);
962 int64_t len
= utext_nativeLength(utb
);
963 TEST_ASSERT(len
== 99);
964 isExpensive
= utext_isLengthExpensive(utb
);
965 TEST_ASSERT(isExpensive
== FALSE
);
970 // Index to positions not on code point boundaries.
973 const char *u8str
= "\xc8\x81\xe1\x82\x83\xf1\x84\x85\x86";
974 int32_t startMap
[] = { 0, 0, 2, 2, 2, 5, 5, 5, 5, 9, 9};
975 int32_t nextMap
[] = { 2, 2, 5, 5, 5, 9, 9, 9, 9, 9, 9};
976 int32_t prevMap
[] = { 0, 0, 0, 0, 0, 2, 2, 2, 2, 5, 5};
977 UChar32 c32Map
[] = {0x201, 0x201, 0x1083, 0x1083, 0x1083, 0x044146, 0x044146, 0x044146, 0x044146, -1, -1};
978 UChar32 pr32Map
[] = { -1, -1, 0x201, 0x201, 0x201, 0x1083, 0x1083, 0x1083, 0x1083, 0x044146, 0x044146};
980 // extractLen is the size, in UChars, of what will be extracted between index and index+1.
981 // is zero when both index positions lie within the same code point.
982 int32_t exLen
[] = { 0, 1, 0, 0, 1, 0, 0, 0, 2, 0, 0};
985 UErrorCode status
= U_ZERO_ERROR
;
986 UText
*ut
= utext_openUTF8(NULL
, u8str
, -1, &status
);
987 TEST_SUCCESS(status
);
991 int32_t startMapLimit
= sizeof(startMap
) / sizeof(int32_t);
992 for (i
=0; i
<startMapLimit
; i
++) {
993 utext_setNativeIndex(ut
, i
);
994 int64_t cpIndex
= utext_getNativeIndex(ut
);
995 TEST_ASSERT(cpIndex
== startMap
[i
]);
996 cpIndex
= UTEXT_GETNATIVEINDEX(ut
);
997 TEST_ASSERT(cpIndex
== startMap
[i
]);
1001 for (i
=0; i
<startMapLimit
; i
++) {
1002 UChar32 c32
= utext_char32At(ut
, i
);
1003 TEST_ASSERT(c32
== c32Map
[i
]);
1004 int64_t cpIndex
= utext_getNativeIndex(ut
);
1005 TEST_ASSERT(cpIndex
== startMap
[i
]);
1008 // Check utext_next32From
1009 for (i
=0; i
<startMapLimit
; i
++) {
1010 UChar32 c32
= utext_next32From(ut
, i
);
1011 TEST_ASSERT(c32
== c32Map
[i
]);
1012 int64_t cpIndex
= utext_getNativeIndex(ut
);
1013 TEST_ASSERT(cpIndex
== nextMap
[i
]);
1016 // check utext_previous32From
1017 for (i
=0; i
<startMapLimit
; i
++) {
1019 UChar32 c32
= utext_previous32From(ut
, i
);
1020 TEST_ASSERT(c32
== pr32Map
[i
]);
1021 int64_t cpIndex
= utext_getNativeIndex(ut
);
1022 TEST_ASSERT(cpIndex
== prevMap
[i
]);
1026 // Extract from i to i+1, which may be zero or one code points,
1027 // depending on whether the indices straddle a cp boundary.
1028 for (i
=0; i
<startMapLimit
; i
++) {
1030 status
= U_ZERO_ERROR
;
1031 int32_t extractedLen
= utext_extract(ut
, i
, i
+1, buf
, 3, &status
);
1032 TEST_SUCCESS(status
);
1033 TEST_ASSERT(extractedLen
== exLen
[i
]);
1034 if (extractedLen
> 0) {
1036 /* extractedLen-extractedLen == 0 is used to get around a compiler warning. */
1037 U16_GET(buf
, 0, extractedLen
-extractedLen
, extractedLen
, c32
);
1038 TEST_ASSERT(c32
== c32Map
[i
]);
1046 { // Similar test, with utf16 instead of utf8
1047 // TODO: merge the common parts of these tests.
1049 UnicodeString
u16str("\\u1000\\U00011000\\u2000\\U00022000", -1, US_INV
);
1050 int32_t startMap
[] ={ 0, 1, 1, 3, 4, 4, 6, 6};
1051 int32_t nextMap
[] = { 1, 3, 3, 4, 6, 6, 6, 6};
1052 int32_t prevMap
[] = { 0, 0, 0, 1, 3, 3, 4, 4};
1053 UChar32 c32Map
[] = {0x1000, 0x11000, 0x11000, 0x2000, 0x22000, 0x22000, -1, -1};
1054 UChar32 pr32Map
[] = { -1, 0x1000, 0x1000, 0x11000, 0x2000, 0x2000, 0x22000, 0x22000};
1055 int32_t exLen
[] = { 1, 0, 2, 1, 0, 2, 0, 0,};
1057 u16str
= u16str
.unescape();
1058 UErrorCode status
= U_ZERO_ERROR
;
1059 UText
*ut
= utext_openUnicodeString(NULL
, &u16str
, &status
);
1060 TEST_SUCCESS(status
);
1062 int32_t startMapLimit
= sizeof(startMap
) / sizeof(int32_t);
1064 for (i
=0; i
<startMapLimit
; i
++) {
1065 utext_setNativeIndex(ut
, i
);
1066 int64_t cpIndex
= utext_getNativeIndex(ut
);
1067 TEST_ASSERT(cpIndex
== startMap
[i
]);
1071 for (i
=0; i
<startMapLimit
; i
++) {
1072 UChar32 c32
= utext_char32At(ut
, i
);
1073 TEST_ASSERT(c32
== c32Map
[i
]);
1074 int64_t cpIndex
= utext_getNativeIndex(ut
);
1075 TEST_ASSERT(cpIndex
== startMap
[i
]);
1078 // Check utext_next32From
1079 for (i
=0; i
<startMapLimit
; i
++) {
1080 UChar32 c32
= utext_next32From(ut
, i
);
1081 TEST_ASSERT(c32
== c32Map
[i
]);
1082 int64_t cpIndex
= utext_getNativeIndex(ut
);
1083 TEST_ASSERT(cpIndex
== nextMap
[i
]);
1086 // check utext_previous32From
1087 for (i
=0; i
<startMapLimit
; i
++) {
1088 UChar32 c32
= utext_previous32From(ut
, i
);
1089 TEST_ASSERT(c32
== pr32Map
[i
]);
1090 int64_t cpIndex
= utext_getNativeIndex(ut
);
1091 TEST_ASSERT(cpIndex
== prevMap
[i
]);
1095 // Extract from i to i+1, which may be zero or one code points,
1096 // depending on whether the indices straddle a cp boundary.
1097 for (i
=0; i
<startMapLimit
; i
++) {
1099 status
= U_ZERO_ERROR
;
1100 int32_t extractedLen
= utext_extract(ut
, i
, i
+1, buf
, 3, &status
);
1101 TEST_SUCCESS(status
);
1102 TEST_ASSERT(extractedLen
== exLen
[i
]);
1103 if (extractedLen
> 0) {
1105 /* extractedLen-extractedLen == 0 is used to get around a compiler warning. */
1106 U16_GET(buf
, 0, extractedLen
-extractedLen
, extractedLen
, c32
);
1107 TEST_ASSERT(c32
== c32Map
[i
]);
1114 { // Similar test, with UText over Replaceable
1115 // TODO: merge the common parts of these tests.
1117 UnicodeString
u16str("\\u1000\\U00011000\\u2000\\U00022000", -1, US_INV
);
1118 int32_t startMap
[] ={ 0, 1, 1, 3, 4, 4, 6, 6};
1119 int32_t nextMap
[] = { 1, 3, 3, 4, 6, 6, 6, 6};
1120 int32_t prevMap
[] = { 0, 0, 0, 1, 3, 3, 4, 4};
1121 UChar32 c32Map
[] = {0x1000, 0x11000, 0x11000, 0x2000, 0x22000, 0x22000, -1, -1};
1122 UChar32 pr32Map
[] = { -1, 0x1000, 0x1000, 0x11000, 0x2000, 0x2000, 0x22000, 0x22000};
1123 int32_t exLen
[] = { 1, 0, 2, 1, 0, 2, 0, 0,};
1125 u16str
= u16str
.unescape();
1126 UErrorCode status
= U_ZERO_ERROR
;
1127 UText
*ut
= utext_openReplaceable(NULL
, &u16str
, &status
);
1128 TEST_SUCCESS(status
);
1130 int32_t startMapLimit
= sizeof(startMap
) / sizeof(int32_t);
1132 for (i
=0; i
<startMapLimit
; i
++) {
1133 utext_setNativeIndex(ut
, i
);
1134 int64_t cpIndex
= utext_getNativeIndex(ut
);
1135 TEST_ASSERT(cpIndex
== startMap
[i
]);
1139 for (i
=0; i
<startMapLimit
; i
++) {
1140 UChar32 c32
= utext_char32At(ut
, i
);
1141 TEST_ASSERT(c32
== c32Map
[i
]);
1142 int64_t cpIndex
= utext_getNativeIndex(ut
);
1143 TEST_ASSERT(cpIndex
== startMap
[i
]);
1146 // Check utext_next32From
1147 for (i
=0; i
<startMapLimit
; i
++) {
1148 UChar32 c32
= utext_next32From(ut
, i
);
1149 TEST_ASSERT(c32
== c32Map
[i
]);
1150 int64_t cpIndex
= utext_getNativeIndex(ut
);
1151 TEST_ASSERT(cpIndex
== nextMap
[i
]);
1154 // check utext_previous32From
1155 for (i
=0; i
<startMapLimit
; i
++) {
1156 UChar32 c32
= utext_previous32From(ut
, i
);
1157 TEST_ASSERT(c32
== pr32Map
[i
]);
1158 int64_t cpIndex
= utext_getNativeIndex(ut
);
1159 TEST_ASSERT(cpIndex
== prevMap
[i
]);
1163 // Extract from i to i+1, which may be zero or one code points,
1164 // depending on whether the indices straddle a cp boundary.
1165 for (i
=0; i
<startMapLimit
; i
++) {
1167 status
= U_ZERO_ERROR
;
1168 int32_t extractedLen
= utext_extract(ut
, i
, i
+1, buf
, 3, &status
);
1169 TEST_SUCCESS(status
);
1170 TEST_ASSERT(extractedLen
== exLen
[i
]);
1171 if (extractedLen
> 0) {
1173 /* extractedLen-extractedLen == 0 is used to get around a compiler warning. */
1174 U16_GET(buf
, 0, extractedLen
-extractedLen
, extractedLen
, c32
);
1175 TEST_ASSERT(c32
== c32Map
[i
]);
1184 void UTextTest::FreezeTest() {
1185 // Check isWritable() and freeze() behavior.
1188 UnicodeString
ustr("Hello, World.");
1189 const char u8str
[] = {char(0x31), (char)0x32, (char)0x33, 0};
1190 const UChar u16str
[] = {(UChar
)0x31, (UChar
)0x32, (UChar
)0x44, 0};
1192 UErrorCode status
= U_ZERO_ERROR
;
1196 ut
= utext_openUTF8(ut
, u8str
, -1, &status
);
1197 TEST_SUCCESS(status
);
1198 UBool writable
= utext_isWritable(ut
);
1199 TEST_ASSERT(writable
== FALSE
);
1200 utext_copy(ut
, 1, 2, 0, TRUE
, &status
);
1201 TEST_ASSERT(status
== U_NO_WRITE_PERMISSION
);
1203 status
= U_ZERO_ERROR
;
1204 ut
= utext_openUChars(ut
, u16str
, -1, &status
);
1205 TEST_SUCCESS(status
);
1206 writable
= utext_isWritable(ut
);
1207 TEST_ASSERT(writable
== FALSE
);
1208 utext_copy(ut
, 1, 2, 0, TRUE
, &status
);
1209 TEST_ASSERT(status
== U_NO_WRITE_PERMISSION
);
1211 status
= U_ZERO_ERROR
;
1212 ut
= utext_openUnicodeString(ut
, &ustr
, &status
);
1213 TEST_SUCCESS(status
);
1214 writable
= utext_isWritable(ut
);
1215 TEST_ASSERT(writable
== TRUE
);
1217 writable
= utext_isWritable(ut
);
1218 TEST_ASSERT(writable
== FALSE
);
1219 utext_copy(ut
, 1, 2, 0, TRUE
, &status
);
1220 TEST_ASSERT(status
== U_NO_WRITE_PERMISSION
);
1222 status
= U_ZERO_ERROR
;
1223 ut
= utext_openUnicodeString(ut
, &ustr
, &status
);
1224 TEST_SUCCESS(status
);
1225 ut2
= utext_clone(ut2
, ut
, FALSE
, FALSE
, &status
); // clone with readonly = false
1226 TEST_SUCCESS(status
);
1227 writable
= utext_isWritable(ut2
);
1228 TEST_ASSERT(writable
== TRUE
);
1229 ut2
= utext_clone(ut2
, ut
, FALSE
, TRUE
, &status
); // clone with readonly = true
1230 TEST_SUCCESS(status
);
1231 writable
= utext_isWritable(ut2
);
1232 TEST_ASSERT(writable
== FALSE
);
1233 utext_copy(ut2
, 1, 2, 0, TRUE
, &status
);
1234 TEST_ASSERT(status
== U_NO_WRITE_PERMISSION
);
1236 status
= U_ZERO_ERROR
;
1237 ut
= utext_openConstUnicodeString(ut
, (const UnicodeString
*)&ustr
, &status
);
1238 TEST_SUCCESS(status
);
1239 writable
= utext_isWritable(ut
);
1240 TEST_ASSERT(writable
== FALSE
);
1241 utext_copy(ut
, 1, 2, 0, TRUE
, &status
);
1242 TEST_ASSERT(status
== U_NO_WRITE_PERMISSION
);
1244 // Deep Clone of a frozen UText should re-enable writing in the copy.
1245 status
= U_ZERO_ERROR
;
1246 ut
= utext_openUnicodeString(ut
, &ustr
, &status
);
1247 TEST_SUCCESS(status
);
1249 ut2
= utext_clone(ut2
, ut
, TRUE
, FALSE
, &status
); // deep clone
1250 TEST_SUCCESS(status
);
1251 writable
= utext_isWritable(ut2
);
1252 TEST_ASSERT(writable
== TRUE
);
1255 // Deep clone of a frozen UText, where the base type is intrinsically non-writable,
1256 // should NOT enable writing in the copy.
1257 status
= U_ZERO_ERROR
;
1258 ut
= utext_openUChars(ut
, u16str
, -1, &status
);
1259 TEST_SUCCESS(status
);
1261 ut2
= utext_clone(ut2
, ut
, TRUE
, FALSE
, &status
); // deep clone
1262 TEST_SUCCESS(status
);
1263 writable
= utext_isWritable(ut2
);
1264 TEST_ASSERT(writable
== FALSE
);
1274 // A UText type that works with a chunk size of 1.
1275 // Intended to test for edge cases.
1276 // Input comes from a UnicodeString.
1278 // ut.b the character. Put into both halves.
1282 static UBool U_CALLCONV
1283 fragTextAccess(UText
*ut
, int64_t index
, UBool forward
) {
1284 const UnicodeString
*us
= (const UnicodeString
*)ut
->context
;
1286 int32_t length
= us
->length();
1287 if (forward
&& index
>=0 && index
<length
) {
1288 c
= us
->charAt((int32_t)index
);
1290 ut
->chunkOffset
= 0;
1291 ut
->chunkLength
= 1;
1292 ut
->chunkNativeStart
= index
;
1293 ut
->chunkNativeLimit
= index
+1;
1296 if (!forward
&& index
>0 && index
<=length
) {
1297 c
= us
->charAt((int32_t)index
-1);
1299 ut
->chunkOffset
= 1;
1300 ut
->chunkLength
= 1;
1301 ut
->chunkNativeStart
= index
-1;
1302 ut
->chunkNativeLimit
= index
;
1306 ut
->chunkOffset
= 0;
1307 ut
->chunkLength
= 0;
1309 ut
->chunkNativeStart
= 0;
1310 ut
->chunkNativeLimit
= 0;
1312 ut
->chunkNativeStart
= length
;
1313 ut
->chunkNativeLimit
= length
;
1318 // Function table to be used with this fragmented text provider.
1319 // Initialized in the open function.
1320 static UTextFuncs fragmentFuncs
;
1322 // Clone function for fragmented text provider.
1323 // Didn't really want to provide this, but it's easier to provide it than to keep it
1324 // out of the tests.
1327 cloneFragmentedUnicodeString(UText
*dest
, const UText
*src
, UBool deep
, UErrorCode
*status
) {
1328 if (U_FAILURE(*status
)) {
1332 *status
= U_UNSUPPORTED_ERROR
;
1335 dest
= utext_openUnicodeString(dest
, (UnicodeString
*)src
->context
, status
);
1336 utext_setNativeIndex(dest
, utext_getNativeIndex(src
));
1342 // Open function for the fragmented text provider.
1344 openFragmentedUnicodeString(UText
*ut
, UnicodeString
*s
, UErrorCode
*status
) {
1345 ut
= utext_openUnicodeString(ut
, s
, status
);
1346 if (U_FAILURE(*status
)) {
1350 // Copy of the function table from the stock UnicodeString UText,
1351 // and replace the entry for the access function.
1352 memcpy(&fragmentFuncs
, ut
->pFuncs
, sizeof(fragmentFuncs
));
1353 fragmentFuncs
.access
= fragTextAccess
;
1354 fragmentFuncs
.clone
= cloneFragmentedUnicodeString
;
1355 ut
->pFuncs
= &fragmentFuncs
;
1357 ut
->chunkContents
= (UChar
*)&ut
->b
;
1358 ut
->pFuncs
->access(ut
, 0, TRUE
);
1362 // Regression test for Ticket 5560
1363 // Clone fails to update chunkContentPointer in the cloned copy.
1364 // This is only an issue for UText types that work in a local buffer,
1365 // (UTF-8 wrapper, for example)
1368 // 1. Create an inital UText
1369 // 2. Deep clone it. Contents should match original.
1370 // 3. Reset original to something different.
1371 // 4. Check that clone contents did not change.
1373 void UTextTest::Ticket5560() {
1374 /* The following two strings are in UTF-8 even on EBCDIC platforms. */
1375 static const char s1
[] = {0x41,0x42,0x43,0x44,0x45,0x46,0}; /* "ABCDEF" */
1376 static const char s2
[] = {0x31,0x32,0x33,0x34,0x35,0x36,0}; /* "123456" */
1377 UErrorCode status
= U_ZERO_ERROR
;
1379 UText ut1
= UTEXT_INITIALIZER
;
1380 UText ut2
= UTEXT_INITIALIZER
;
1382 utext_openUTF8(&ut1
, s1
, -1, &status
);
1383 UChar c
= utext_next32(&ut1
);
1384 TEST_ASSERT(c
== 0x41); // c == 'A'
1386 utext_clone(&ut2
, &ut1
, TRUE
, FALSE
, &status
);
1387 TEST_SUCCESS(status
);
1388 c
= utext_next32(&ut2
);
1389 TEST_ASSERT(c
== 0x42); // c == 'B'
1390 c
= utext_next32(&ut1
);
1391 TEST_ASSERT(c
== 0x42); // c == 'B'
1393 utext_openUTF8(&ut1
, s2
, -1, &status
);
1394 c
= utext_next32(&ut1
);
1395 TEST_ASSERT(c
== 0x31); // c == '1'
1396 c
= utext_next32(&ut2
);
1397 TEST_ASSERT(c
== 0x43); // c == 'C'
1404 // Test for Ticket 6847
1406 void UTextTest::Ticket6847() {
1407 const int STRLEN
= 90;
1409 u_memset(s
, 0x41, STRLEN
);
1412 UErrorCode status
= U_ZERO_ERROR
;
1413 UText
*ut
= utext_openUChars(NULL
, s
, -1, &status
);
1415 utext_setNativeIndex(ut
, 0);
1418 int32_t nativeIndex
= UTEXT_GETNATIVEINDEX(ut
);
1419 TEST_ASSERT(nativeIndex
== 0);
1420 while ((c
= utext_next32(ut
)) != U_SENTINEL
) {
1421 TEST_ASSERT(c
== 0x41);
1422 TEST_ASSERT(count
< STRLEN
);
1423 if (count
>= STRLEN
) {
1427 nativeIndex
= UTEXT_GETNATIVEINDEX(ut
);
1428 TEST_ASSERT(nativeIndex
== count
);
1430 TEST_ASSERT(count
== STRLEN
);
1431 nativeIndex
= UTEXT_GETNATIVEINDEX(ut
);
1432 TEST_ASSERT(nativeIndex
== STRLEN
);