1 /********************************************************************
3 * Copyright (c) 2005-2013, International Business Machines Corporation and
4 * others. All Rights Reserved.
5 ********************************************************************/
6 /************************************************************************
7 * Tests for the UText and UTextIterator text abstraction classses
9 ************************************************************************/
14 #include "unicode/utypes.h"
15 #include "unicode/utext.h"
16 #include "unicode/utf8.h"
17 #include "unicode/ustring.h"
18 #include "unicode/uchriter.h"
21 static UBool gFailed
= FALSE
;
22 static int gTestNum
= 0;
25 UText
*openFragmentedUnicodeString(UText
*ut
, UnicodeString
*s
, UErrorCode
*status
);
27 #define TEST_ASSERT(x) \
28 { if ((x)==FALSE) {errln("Test #%d failure in file %s at line %d\n", gTestNum, __FILE__, __LINE__);\
33 #define TEST_SUCCESS(status) \
34 { if (U_FAILURE(status)) {errln("Test #%d failure in file %s at line %d. Error = \"%s\"\n", \
35 gTestNum, __FILE__, __LINE__, u_errorName(status)); \
39 UTextTest::UTextTest() {
42 UTextTest::~UTextTest() {
47 UTextTest::runIndexedTest(int32_t index
, UBool exec
,
48 const char* &name
, char* /*par*/) {
50 case 0: name
= "TextTest";
51 if (exec
) TextTest(); break;
52 case 1: name
= "ErrorTest";
53 if (exec
) ErrorTest(); break;
54 case 2: name
= "FreezeTest";
55 if (exec
) FreezeTest(); break;
56 case 3: name
= "Ticket5560";
57 if (exec
) Ticket5560(); break;
58 case 4: name
= "Ticket6847";
59 if (exec
) Ticket6847(); break;
60 case 5: name
= "Ticket10562";
61 if (exec
) Ticket10562(); break;
62 default: name
= ""; break;
67 // Quick and dirty random number generator.
68 // (don't use library so that results are portable.
69 static uint32_t m_seed
= 1;
70 static uint32_t m_rand()
72 m_seed
= m_seed
* 1103515245 + 12345;
73 return (uint32_t)(m_seed
/65536) % 32768;
80 // Top Level function for UText testing.
81 // Specifies the strings to be tested, with the acutal testing itself
82 // being carried out in another function, TestString().
84 void UTextTest::TextTest() {
87 TestString("abcd\\U00010001xyz");
90 // Supplementary chars at start or end
91 TestString("\\U00010001");
92 TestString("abc\\U00010001");
93 TestString("\\U00010001abc");
95 // Test simple strings of lengths 1 to 60, looking for glitches at buffer boundaries
97 for (i
=1; i
<60; i
++) {
100 if (j
+0x30 == 0x5c) {
101 // backslash. Needs to be escaped
102 s
.append((UChar
)0x5c);
104 s
.append(UChar(j
+0x30));
109 // Test strings with odd-aligned supplementary chars,
110 // looking for glitches at buffer boundaries
111 for (i
=1; i
<60; i
++) {
113 s
.append((UChar
)0x41);
114 for (j
=0; j
<i
; j
++) {
115 s
.append(UChar32(j
+0x11000));
120 // String of chars of randomly varying size in utf-8 representation.
121 // Exercise the mapping, and the varying sized buffer.
127 UChar32 c4
= 0x11000;
128 for (i
=0; i
<1000; i
++) {
129 int len8
= m_rand()%4
+ 1;
133 // don't put 0 into string (0 terminated strings for some tests)
134 // don't put '\', will cause unescape() to fail.
135 if (c1
==0x5c || c1
==0) {
156 // TestString() Run a suite of UText tests on a string.
157 // The test string is unescaped before use.
159 void UTextTest::TestString(const UnicodeString
&s
) {
164 UErrorCode status
= U_ZERO_ERROR
;
168 UnicodeString sa
= s
.unescape();
172 // Build up a mapping between code points and UTF-16 code unit indexes.
174 m
*cpMap
= new m
[sa
.length() + 1];
176 for (i
=0; i
<sa
.length(); i
=sa
.moveIndex32(i
, 1)) {
178 cpMap
[j
].nativeIdx
= i
;
183 cpMap
[j
].nativeIdx
= i
; // position following the last char in utf-16 string.
186 // UChar * test, null terminated
187 status
= U_ZERO_ERROR
;
188 UChar
*buf
= new UChar
[saLen
+1];
189 sa
.extract(buf
, saLen
+1, status
);
190 TEST_SUCCESS(status
);
191 ut
= utext_openUChars(NULL
, buf
, -1, &status
);
192 TEST_SUCCESS(status
);
193 TestAccess(sa
, ut
, cpCount
, cpMap
);
197 // UChar * test, with length
198 status
= U_ZERO_ERROR
;
199 buf
= new UChar
[saLen
+1];
200 sa
.extract(buf
, saLen
+1, status
);
201 TEST_SUCCESS(status
);
202 ut
= utext_openUChars(NULL
, buf
, saLen
, &status
);
203 TEST_SUCCESS(status
);
204 TestAccess(sa
, ut
, cpCount
, cpMap
);
209 // UnicodeString test
210 status
= U_ZERO_ERROR
;
211 ut
= utext_openUnicodeString(NULL
, &sa
, &status
);
212 TEST_SUCCESS(status
);
213 TestAccess(sa
, ut
, cpCount
, cpMap
);
214 TestCMR(sa
, ut
, cpCount
, cpMap
, cpMap
);
218 // Const UnicodeString test
219 status
= U_ZERO_ERROR
;
220 ut
= utext_openConstUnicodeString(NULL
, &sa
, &status
);
221 TEST_SUCCESS(status
);
222 TestAccess(sa
, ut
, cpCount
, cpMap
);
226 // Replaceable test. (UnicodeString inherits Replaceable)
227 status
= U_ZERO_ERROR
;
228 ut
= utext_openReplaceable(NULL
, &sa
, &status
);
229 TEST_SUCCESS(status
);
230 TestAccess(sa
, ut
, cpCount
, cpMap
);
231 TestCMR(sa
, ut
, cpCount
, cpMap
, cpMap
);
234 // Character Iterator Tests
235 status
= U_ZERO_ERROR
;
236 const UChar
*cbuf
= sa
.getBuffer();
237 CharacterIterator
*ci
= new UCharCharacterIterator(cbuf
, saLen
, status
);
238 TEST_SUCCESS(status
);
239 ut
= utext_openCharacterIterator(NULL
, ci
, &status
);
240 TEST_SUCCESS(status
);
241 TestAccess(sa
, ut
, cpCount
, cpMap
);
246 // Fragmented UnicodeString (Chunk size of one)
248 status
= U_ZERO_ERROR
;
249 ut
= openFragmentedUnicodeString(NULL
, &sa
, &status
);
250 TEST_SUCCESS(status
);
251 TestAccess(sa
, ut
, cpCount
, cpMap
);
258 // Convert the test string from UnicodeString to (char *) in utf-8 format
259 int32_t u8Len
= sa
.extract(0, sa
.length(), NULL
, 0, "utf-8");
260 char *u8String
= new char[u8Len
+ 1];
261 sa
.extract(0, sa
.length(), u8String
, u8Len
+1, "utf-8");
263 // Build up the map of code point indices in the utf-8 string
264 m
* u8Map
= new m
[sa
.length() + 1];
265 i
= 0; // native utf-8 index
266 for (j
=0; j
<cpCount
; j
++) { // code point number
267 u8Map
[j
].nativeIdx
= i
;
268 U8_NEXT(u8String
, i
, u8Len
, c
)
271 u8Map
[cpCount
].nativeIdx
= u8Len
; // position following the last char in utf-8 string.
273 // Do the test itself
274 status
= U_ZERO_ERROR
;
275 ut
= utext_openUTF8(NULL
, u8String
, -1, &status
);
276 TEST_SUCCESS(status
);
277 TestAccess(sa
, ut
, cpCount
, u8Map
);
287 // TestCMR test Copy, Move and Replace operations.
288 // us UnicodeString containing the test text.
289 // ut UText containing the same test text.
290 // cpCount number of code points in the test text.
291 // nativeMap Mapping from code points to native indexes for the UText.
292 // u16Map Mapping from code points to UTF-16 indexes, for use with the UnicodeString.
294 // This function runs a whole series of opertions on each incoming UText.
295 // The UText is deep-cloned prior to each operation, so that the original UText remains unchanged.
297 void UTextTest::TestCMR(const UnicodeString
&us
, UText
*ut
, int cpCount
, m
*nativeMap
, m
*u16Map
) {
298 TEST_ASSERT(utext_isWritable(ut
) == TRUE
);
300 int srcLengthType
; // Loop variables for selecting the postion and length
301 int srcPosType
; // of the block to operate on within the source text.
304 int srcIndex
= 0; // Code Point indexes of the block to operate on for
305 int srcLength
= 0; // a specific test.
307 int destIndex
= 0; // Code point index of the destination for a copy/move test.
309 int32_t nativeStart
= 0; // Native unit indexes for a test.
310 int32_t nativeLimit
= 0;
311 int32_t nativeDest
= 0;
313 int32_t u16Start
= 0; // UTF-16 indexes for a test.
314 int32_t u16Limit
= 0; // used when performing the same operation in a Unicode String
317 // Iterate over a whole series of source index, length and a target indexes.
318 // This is done with code point indexes; these will be later translated to native
319 // indexes using the cpMap.
320 for (srcLengthType
=1; srcLengthType
<=3; srcLengthType
++) {
321 switch (srcLengthType
) {
322 case 1: srcLength
= 1; break;
323 case 2: srcLength
= 5; break;
324 case 3: srcLength
= cpCount
/ 3;
326 for (srcPosType
=1; srcPosType
<=5; srcPosType
++) {
327 switch (srcPosType
) {
328 case 1: srcIndex
= 0; break;
329 case 2: srcIndex
= 1; break;
330 case 3: srcIndex
= cpCount
- srcLength
; break;
331 case 4: srcIndex
= cpCount
- srcLength
- 1; break;
332 case 5: srcIndex
= cpCount
/ 2; break;
334 if (srcIndex
< 0 || srcIndex
+ srcLength
> cpCount
) {
335 // filter out bogus test cases -
336 // those with a source range that falls of an edge of the string.
341 // Copy and move tests.
342 // iterate over a variety of destination positions.
344 for (destPosType
=1; destPosType
<=4; destPosType
++) {
345 switch (destPosType
) {
346 case 1: destIndex
= 0; break;
347 case 2: destIndex
= 1; break;
348 case 3: destIndex
= srcIndex
- 1; break;
349 case 4: destIndex
= srcIndex
+ srcLength
+ 1; break;
350 case 5: destIndex
= cpCount
-1; break;
351 case 6: destIndex
= cpCount
; break;
353 if (destIndex
<0 || destIndex
>cpCount
) {
354 // filter out bogus test cases.
358 nativeStart
= nativeMap
[srcIndex
].nativeIdx
;
359 nativeLimit
= nativeMap
[srcIndex
+srcLength
].nativeIdx
;
360 nativeDest
= nativeMap
[destIndex
].nativeIdx
;
362 u16Start
= u16Map
[srcIndex
].nativeIdx
;
363 u16Limit
= u16Map
[srcIndex
+srcLength
].nativeIdx
;
364 u16Dest
= u16Map
[destIndex
].nativeIdx
;
367 TestCopyMove(us
, ut
, FALSE
,
368 nativeStart
, nativeLimit
, nativeDest
,
369 u16Start
, u16Limit
, u16Dest
);
371 TestCopyMove(us
, ut
, TRUE
,
372 nativeStart
, nativeLimit
, nativeDest
,
373 u16Start
, u16Limit
, u16Dest
);
383 UnicodeString
fullRepString("This is an arbitrary string that will be used as replacement text");
384 for (int32_t replStrLen
=0; replStrLen
<20; replStrLen
++) {
385 UnicodeString
repStr(fullRepString
, 0, replStrLen
);
387 nativeStart
, nativeLimit
,
401 // TestCopyMove run a single test case for utext_copy.
402 // Test cases are created in TestCMR and dispatched here for execution.
404 void UTextTest::TestCopyMove(const UnicodeString
&us
, UText
*ut
, UBool move
,
405 int32_t nativeStart
, int32_t nativeLimit
, int32_t nativeDest
,
406 int32_t u16Start
, int32_t u16Limit
, int32_t u16Dest
)
408 UErrorCode status
= U_ZERO_ERROR
;
409 UText
*targetUT
= NULL
;
414 // clone the UText. The test will be run in the cloned copy
415 // so that we don't alter the original.
417 targetUT
= utext_clone(NULL
, ut
, TRUE
, FALSE
, &status
);
418 TEST_SUCCESS(status
);
419 UnicodeString
targetUS(us
); // And copy the reference string.
421 // do the test operation first in the reference
422 targetUS
.copy(u16Start
, u16Limit
, u16Dest
);
424 // delete out the source range.
425 if (u16Limit
< u16Dest
) {
426 targetUS
.removeBetween(u16Start
, u16Limit
);
428 int32_t amtCopied
= u16Limit
- u16Start
;
429 targetUS
.removeBetween(u16Start
+amtCopied
, u16Limit
+amtCopied
);
433 // Do the same operation in the UText under test
434 utext_copy(targetUT
, nativeStart
, nativeLimit
, nativeDest
, move
, &status
);
435 if (nativeDest
> nativeStart
&& nativeDest
< nativeLimit
) {
436 TEST_ASSERT(status
== U_INDEX_OUTOFBOUNDS_ERROR
);
438 TEST_SUCCESS(status
);
440 // Compare the results of the two parallel tests
441 int32_t usi
= 0; // UnicodeString postion, utf-16 index.
442 int64_t uti
= 0; // UText position, native index.
443 int32_t cpi
; // char32 position (code point index)
444 UChar32 usc
; // code point from Unicode String
445 UChar32 utc
; // code point from UText
446 utext_setNativeIndex(targetUT
, 0);
447 for (cpi
=0; ; cpi
++) {
448 usc
= targetUS
.char32At(usi
);
449 utc
= utext_next32(targetUT
);
453 TEST_ASSERT(uti
== usi
);
454 TEST_ASSERT(utc
== usc
);
455 usi
= targetUS
.moveIndex32(usi
, 1);
456 uti
= utext_getNativeIndex(targetUT
);
458 goto cleanupAndReturn
;
461 int64_t expectedNativeLength
= utext_nativeLength(ut
);
463 expectedNativeLength
+= nativeLimit
- nativeStart
;
465 uti
= utext_getNativeIndex(targetUT
);
466 TEST_ASSERT(uti
== expectedNativeLength
);
470 utext_close(targetUT
);
475 // TestReplace Test a single Replace operation.
477 void UTextTest::TestReplace(
478 const UnicodeString
&us
, // reference UnicodeString in which to do the replace
479 UText
*ut
, // UnicodeText object under test.
480 int32_t nativeStart
, // Range to be replaced, in UText native units.
482 int32_t u16Start
, // Range to be replaced, in UTF-16 units
483 int32_t u16Limit
, // for use in the reference UnicodeString.
484 const UnicodeString
&repStr
) // The replacement string
486 UErrorCode status
= U_ZERO_ERROR
;
487 UText
*targetUT
= NULL
;
492 // clone the target UText. The test will be run in the cloned copy
493 // so that we don't alter the original.
495 targetUT
= utext_clone(NULL
, ut
, TRUE
, FALSE
, &status
);
496 TEST_SUCCESS(status
);
497 UnicodeString
targetUS(us
); // And copy the reference string.
500 // Do the replace operation in the Unicode String, to
501 // produce a reference result.
503 targetUS
.replace(u16Start
, u16Limit
-u16Start
, repStr
);
506 // Do the replace on the UText under test
508 const UChar
*rs
= repStr
.getBuffer();
509 int32_t rsLen
= repStr
.length();
510 int32_t actualDelta
= utext_replace(targetUT
, nativeStart
, nativeLimit
, rs
, rsLen
, &status
);
511 int32_t expectedDelta
= repStr
.length() - (nativeLimit
- nativeStart
);
512 TEST_ASSERT(actualDelta
== expectedDelta
);
515 // Compare the results
517 int32_t usi
= 0; // UnicodeString postion, utf-16 index.
518 int64_t uti
= 0; // UText position, native index.
519 int32_t cpi
; // char32 position (code point index)
520 UChar32 usc
; // code point from Unicode String
521 UChar32 utc
; // code point from UText
522 int64_t expectedNativeLength
= 0;
523 utext_setNativeIndex(targetUT
, 0);
524 for (cpi
=0; ; cpi
++) {
525 usc
= targetUS
.char32At(usi
);
526 utc
= utext_next32(targetUT
);
530 TEST_ASSERT(uti
== usi
);
531 TEST_ASSERT(utc
== usc
);
532 usi
= targetUS
.moveIndex32(usi
, 1);
533 uti
= utext_getNativeIndex(targetUT
);
535 goto cleanupAndReturn
;
538 expectedNativeLength
= utext_nativeLength(ut
) + expectedDelta
;
539 uti
= utext_getNativeIndex(targetUT
);
540 TEST_ASSERT(uti
== expectedNativeLength
);
543 utext_close(targetUT
);
547 // TestAccess Test the read only access functions on a UText, including cloning.
548 // The text is accessed in a variety of ways, and compared with
549 // the reference UnicodeString.
551 void UTextTest::TestAccess(const UnicodeString
&us
, UText
*ut
, int cpCount
, m
*cpMap
) {
552 // Run the standard tests on the caller-supplied UText.
553 TestAccessNoClone(us
, ut
, cpCount
, cpMap
);
555 // Re-run tests on a shallow clone.
556 utext_setNativeIndex(ut
, 0);
557 UErrorCode status
= U_ZERO_ERROR
;
558 UText
*shallowClone
= utext_clone(NULL
, ut
, FALSE
/*deep*/, FALSE
/*readOnly*/, &status
);
559 TEST_SUCCESS(status
);
560 TestAccessNoClone(us
, shallowClone
, cpCount
, cpMap
);
563 // Rerun again on a deep clone.
564 // Note that text providers are not required to provide deep cloning,
565 // so unsupported errors are ignored.
567 status
= U_ZERO_ERROR
;
568 utext_setNativeIndex(shallowClone
, 0);
569 UText
*deepClone
= utext_clone(NULL
, shallowClone
, TRUE
, FALSE
, &status
);
570 utext_close(shallowClone
);
571 if (status
!= U_UNSUPPORTED_ERROR
) {
572 TEST_SUCCESS(status
);
573 TestAccessNoClone(us
, deepClone
, cpCount
, cpMap
);
575 utext_close(deepClone
);
580 // TestAccessNoClone() Test the read only access functions on a UText.
581 // The text is accessed in a variety of ways, and compared with
582 // the reference UnicodeString.
584 void UTextTest::TestAccessNoClone(const UnicodeString
&us
, UText
*ut
, int cpCount
, m
*cpMap
) {
585 UErrorCode status
= U_ZERO_ERROR
;
589 // Check the length from the UText
591 int64_t expectedLen
= cpMap
[cpCount
].nativeIdx
;
592 int64_t utlen
= utext_nativeLength(ut
);
593 TEST_ASSERT(expectedLen
== utlen
);
596 // Iterate forwards, verify that we get the correct code points
597 // at the correct native offsets.
601 int64_t expectedIndex
= 0;
602 int64_t foundIndex
= 0;
607 for (i
=0; i
<cpCount
; i
++) {
608 expectedIndex
= cpMap
[i
].nativeIdx
;
609 foundIndex
= utext_getNativeIndex(ut
);
610 TEST_ASSERT(expectedIndex
== foundIndex
);
611 expectedC
= cpMap
[i
].cp
;
612 foundC
= utext_next32(ut
);
613 TEST_ASSERT(expectedC
== foundC
);
614 foundIndex
= utext_getPreviousNativeIndex(ut
);
615 TEST_ASSERT(expectedIndex
== foundIndex
);
620 foundC
= utext_next32(ut
);
621 TEST_ASSERT(foundC
== U_SENTINEL
);
623 // Repeat above, using macros
624 utext_setNativeIndex(ut
, 0);
625 for (i
=0; i
<cpCount
; i
++) {
626 expectedIndex
= cpMap
[i
].nativeIdx
;
627 foundIndex
= UTEXT_GETNATIVEINDEX(ut
);
628 TEST_ASSERT(expectedIndex
== foundIndex
);
629 expectedC
= cpMap
[i
].cp
;
630 foundC
= UTEXT_NEXT32(ut
);
631 TEST_ASSERT(expectedC
== foundC
);
636 foundC
= UTEXT_NEXT32(ut
);
637 TEST_ASSERT(foundC
== U_SENTINEL
);
640 // Forward iteration (above) should have left index at the
641 // end of the input, which should == length().
643 len
= utext_nativeLength(ut
);
644 foundIndex
= utext_getNativeIndex(ut
);
645 TEST_ASSERT(len
== foundIndex
);
648 // Iterate backwards over entire test string
650 len
= utext_getNativeIndex(ut
);
651 utext_setNativeIndex(ut
, len
);
652 for (i
=cpCount
-1; i
>=0; i
--) {
653 expectedC
= cpMap
[i
].cp
;
654 expectedIndex
= cpMap
[i
].nativeIdx
;
655 int64_t prevIndex
= utext_getPreviousNativeIndex(ut
);
656 foundC
= utext_previous32(ut
);
657 foundIndex
= utext_getNativeIndex(ut
);
658 TEST_ASSERT(expectedIndex
== foundIndex
);
659 TEST_ASSERT(expectedC
== foundC
);
660 TEST_ASSERT(prevIndex
== foundIndex
);
667 // Backwards iteration, above, should have left our iterator
668 // position at zero, and continued backwards iterationshould fail.
670 foundIndex
= utext_getNativeIndex(ut
);
671 TEST_ASSERT(foundIndex
== 0);
672 foundIndex
= utext_getPreviousNativeIndex(ut
);
673 TEST_ASSERT(foundIndex
== 0);
676 foundC
= utext_previous32(ut
);
677 TEST_ASSERT(foundC
== U_SENTINEL
);
678 foundIndex
= utext_getNativeIndex(ut
);
679 TEST_ASSERT(foundIndex
== 0);
680 foundIndex
= utext_getPreviousNativeIndex(ut
);
681 TEST_ASSERT(foundIndex
== 0);
684 // And again, with the macros
685 utext_setNativeIndex(ut
, len
);
686 for (i
=cpCount
-1; i
>=0; i
--) {
687 expectedC
= cpMap
[i
].cp
;
688 expectedIndex
= cpMap
[i
].nativeIdx
;
689 foundC
= UTEXT_PREVIOUS32(ut
);
690 foundIndex
= UTEXT_GETNATIVEINDEX(ut
);
691 TEST_ASSERT(expectedIndex
== foundIndex
);
692 TEST_ASSERT(expectedC
== foundC
);
699 // Backwards iteration, above, should have left our iterator
700 // position at zero, and continued backwards iterationshould fail.
702 foundIndex
= UTEXT_GETNATIVEINDEX(ut
);
703 TEST_ASSERT(foundIndex
== 0);
705 foundC
= UTEXT_PREVIOUS32(ut
);
706 TEST_ASSERT(foundC
== U_SENTINEL
);
707 foundIndex
= UTEXT_GETNATIVEINDEX(ut
);
708 TEST_ASSERT(foundIndex
== 0);
714 // next32From(), prevous32From(), Iterate in a somewhat random order.
717 for (i
=0; i
<cpCount
; i
++) {
718 cpIndex
= (cpIndex
+ 9973) % cpCount
;
719 index
= cpMap
[cpIndex
].nativeIdx
;
720 expectedC
= cpMap
[cpIndex
].cp
;
721 foundC
= utext_next32From(ut
, index
);
722 TEST_ASSERT(expectedC
== foundC
);
729 for (i
=0; i
<cpCount
; i
++) {
730 cpIndex
= (cpIndex
+ 9973) % cpCount
;
731 index
= cpMap
[cpIndex
+1].nativeIdx
;
732 expectedC
= cpMap
[cpIndex
].cp
;
733 foundC
= utext_previous32From(ut
, index
);
734 TEST_ASSERT(expectedC
== foundC
);
742 // moveIndex(int32_t delta);
745 // Walk through frontwards, incrementing by one
746 utext_setNativeIndex(ut
, 0);
747 for (i
=1; i
<=cpCount
; i
++) {
748 utext_moveIndex32(ut
, 1);
749 index
= utext_getNativeIndex(ut
);
750 expectedIndex
= cpMap
[i
].nativeIdx
;
751 TEST_ASSERT(expectedIndex
== index
);
752 index
= UTEXT_GETNATIVEINDEX(ut
);
753 TEST_ASSERT(expectedIndex
== index
);
756 // Walk through frontwards, incrementing by two
757 utext_setNativeIndex(ut
, 0);
758 for (i
=2; i
<cpCount
; i
+=2) {
759 utext_moveIndex32(ut
, 2);
760 index
= utext_getNativeIndex(ut
);
761 expectedIndex
= cpMap
[i
].nativeIdx
;
762 TEST_ASSERT(expectedIndex
== index
);
763 index
= UTEXT_GETNATIVEINDEX(ut
);
764 TEST_ASSERT(expectedIndex
== index
);
767 // walk through the string backwards, decrementing by one.
768 i
= cpMap
[cpCount
].nativeIdx
;
769 utext_setNativeIndex(ut
, i
);
770 for (i
=cpCount
; i
>=0; i
--) {
771 expectedIndex
= cpMap
[i
].nativeIdx
;
772 index
= utext_getNativeIndex(ut
);
773 TEST_ASSERT(expectedIndex
== index
);
774 index
= UTEXT_GETNATIVEINDEX(ut
);
775 TEST_ASSERT(expectedIndex
== index
);
776 utext_moveIndex32(ut
, -1);
780 // walk through backwards, decrementing by three
781 i
= cpMap
[cpCount
].nativeIdx
;
782 utext_setNativeIndex(ut
, i
);
783 for (i
=cpCount
; i
>=0; i
-=3) {
784 expectedIndex
= cpMap
[i
].nativeIdx
;
785 index
= utext_getNativeIndex(ut
);
786 TEST_ASSERT(expectedIndex
== index
);
787 index
= UTEXT_GETNATIVEINDEX(ut
);
788 TEST_ASSERT(expectedIndex
== index
);
789 utext_moveIndex32(ut
, -3);
796 int bufSize
= us
.length() + 10;
797 UChar
*buf
= new UChar
[bufSize
];
798 status
= U_ZERO_ERROR
;
799 expectedLen
= us
.length();
800 len
= utext_extract(ut
, 0, utlen
, buf
, bufSize
, &status
);
801 TEST_SUCCESS(status
);
802 TEST_ASSERT(len
== expectedLen
);
803 int compareResult
= us
.compare(buf
, -1);
804 TEST_ASSERT(compareResult
== 0);
806 status
= U_ZERO_ERROR
;
807 len
= utext_extract(ut
, 0, utlen
, NULL
, 0, &status
);
809 TEST_ASSERT(status
== U_STRING_NOT_TERMINATED_WARNING
);
811 TEST_ASSERT(status
== U_BUFFER_OVERFLOW_ERROR
);
813 TEST_ASSERT(len
== expectedLen
);
815 status
= U_ZERO_ERROR
;
816 u_memset(buf
, 0x5555, bufSize
);
817 len
= utext_extract(ut
, 0, utlen
, buf
, 1, &status
);
818 if (us
.length() == 0) {
819 TEST_SUCCESS(status
);
820 TEST_ASSERT(buf
[0] == 0);
822 // Buf len == 1, extracting a single 16 bit value.
823 // If the data char is supplementary, it doesn't matter whether the buffer remains unchanged,
824 // or whether the lead surrogate of the pair is extracted.
825 // It's a buffer overflow error in either case.
826 TEST_ASSERT(buf
[0] == us
.charAt(0) ||
827 (buf
[0] == 0x5555 && U_IS_SUPPLEMENTARY(us
.char32At(0))));
828 TEST_ASSERT(buf
[1] == 0x5555);
829 if (us
.length() == 1) {
830 TEST_ASSERT(status
== U_STRING_NOT_TERMINATED_WARNING
);
832 TEST_ASSERT(status
== U_BUFFER_OVERFLOW_ERROR
);
840 // ErrorTest() Check various error and edge cases.
842 void UTextTest::ErrorTest()
844 // Close of an unitialized UText. Shouldn't blow up.
847 memset(&ut
, 0, sizeof(UText
));
852 // Double-close of a UText. Shouldn't blow up. UText should still be usable.
854 UErrorCode status
= U_ZERO_ERROR
;
855 UText ut
= UTEXT_INITIALIZER
;
856 UnicodeString
s("Hello, World");
857 UText
*ut2
= utext_openUnicodeString(&ut
, &s
, &status
);
858 TEST_SUCCESS(status
);
859 TEST_ASSERT(ut2
== &ut
);
861 UText
*ut3
= utext_close(&ut
);
862 TEST_ASSERT(ut3
== &ut
);
864 UText
*ut4
= utext_close(&ut
);
865 TEST_ASSERT(ut4
== &ut
);
867 utext_openUnicodeString(&ut
, &s
, &status
);
868 TEST_SUCCESS(status
);
872 // Re-use of a UText, chaining through each of the types of UText
873 // (If it doesn't blow up, and doesn't leak, it's probably working fine)
875 UErrorCode status
= U_ZERO_ERROR
;
876 UText ut
= UTEXT_INITIALIZER
;
878 UnicodeString
s1("Hello, World");
879 UChar s2
[] = {(UChar
)0x41, (UChar
)0x42, (UChar
)0};
880 const char *s3
= "\x66\x67\x68";
882 utp
= utext_openUnicodeString(&ut
, &s1
, &status
);
883 TEST_SUCCESS(status
);
884 TEST_ASSERT(utp
== &ut
);
886 utp
= utext_openConstUnicodeString(&ut
, &s1
, &status
);
887 TEST_SUCCESS(status
);
888 TEST_ASSERT(utp
== &ut
);
890 utp
= utext_openUTF8(&ut
, s3
, -1, &status
);
891 TEST_SUCCESS(status
);
892 TEST_ASSERT(utp
== &ut
);
894 utp
= utext_openUChars(&ut
, s2
, -1, &status
);
895 TEST_SUCCESS(status
);
896 TEST_ASSERT(utp
== &ut
);
898 utp
= utext_close(&ut
);
899 TEST_ASSERT(utp
== &ut
);
901 utp
= utext_openUnicodeString(&ut
, &s1
, &status
);
902 TEST_SUCCESS(status
);
903 TEST_ASSERT(utp
== &ut
);
906 // Invalid parameters on open
909 UErrorCode status
= U_ZERO_ERROR
;
910 UText ut
= UTEXT_INITIALIZER
;
912 utext_openUChars(&ut
, NULL
, 5, &status
);
913 TEST_ASSERT(status
== U_ILLEGAL_ARGUMENT_ERROR
);
915 status
= U_ZERO_ERROR
;
916 utext_openUChars(&ut
, NULL
, -1, &status
);
917 TEST_ASSERT(status
== U_ILLEGAL_ARGUMENT_ERROR
);
919 status
= U_ZERO_ERROR
;
920 utext_openUTF8(&ut
, NULL
, 4, &status
);
921 TEST_ASSERT(status
== U_ILLEGAL_ARGUMENT_ERROR
);
923 status
= U_ZERO_ERROR
;
924 utext_openUTF8(&ut
, NULL
, -1, &status
);
925 TEST_ASSERT(status
== U_ILLEGAL_ARGUMENT_ERROR
);
929 // UTF-8 with malformed sequences.
930 // These should come through as the Unicode replacement char, \ufffd
933 UErrorCode status
= U_ZERO_ERROR
;
935 const char *badUTF8
= "\x41\x81\x42\xf0\x81\x81\x43";
938 ut
= utext_openUTF8(NULL
, badUTF8
, -1, &status
);
939 TEST_SUCCESS(status
);
940 c
= utext_char32At(ut
, 1);
941 TEST_ASSERT(c
== 0xfffd);
942 c
= utext_char32At(ut
, 3);
943 TEST_ASSERT(c
== 0xfffd);
944 c
= utext_char32At(ut
, 5);
945 TEST_ASSERT(c
== 0xfffd);
946 c
= utext_char32At(ut
, 6);
947 TEST_ASSERT(c
== 0x43);
950 int n
= utext_extract(ut
, 0, 9, buf
, 10, &status
);
951 TEST_SUCCESS(status
);
953 TEST_ASSERT(buf
[1] == 0xfffd);
954 TEST_ASSERT(buf
[3] == 0xfffd);
955 TEST_ASSERT(buf
[2] == 0x42);
961 // isLengthExpensive - does it make the exptected transitions after
962 // getting the length of a nul terminated string?
965 UErrorCode status
= U_ZERO_ERROR
;
966 UnicodeString
sa("Hello, this is a string");
970 memset(sb
, 0x20, sizeof(sb
));
973 UText
*uta
= utext_openUnicodeString(NULL
, &sa
, &status
);
974 TEST_SUCCESS(status
);
975 isExpensive
= utext_isLengthExpensive(uta
);
976 TEST_ASSERT(isExpensive
== FALSE
);
979 UText
*utb
= utext_openUChars(NULL
, sb
, -1, &status
);
980 TEST_SUCCESS(status
);
981 isExpensive
= utext_isLengthExpensive(utb
);
982 TEST_ASSERT(isExpensive
== TRUE
);
983 int64_t len
= utext_nativeLength(utb
);
984 TEST_ASSERT(len
== 99);
985 isExpensive
= utext_isLengthExpensive(utb
);
986 TEST_ASSERT(isExpensive
== FALSE
);
991 // Index to positions not on code point boundaries.
994 const char *u8str
= "\xc8\x81\xe1\x82\x83\xf1\x84\x85\x86";
995 int32_t startMap
[] = { 0, 0, 2, 2, 2, 5, 5, 5, 5, 9, 9};
996 int32_t nextMap
[] = { 2, 2, 5, 5, 5, 9, 9, 9, 9, 9, 9};
997 int32_t prevMap
[] = { 0, 0, 0, 0, 0, 2, 2, 2, 2, 5, 5};
998 UChar32 c32Map
[] = {0x201, 0x201, 0x1083, 0x1083, 0x1083, 0x044146, 0x044146, 0x044146, 0x044146, -1, -1};
999 UChar32 pr32Map
[] = { -1, -1, 0x201, 0x201, 0x201, 0x1083, 0x1083, 0x1083, 0x1083, 0x044146, 0x044146};
1001 // extractLen is the size, in UChars, of what will be extracted between index and index+1.
1002 // is zero when both index positions lie within the same code point.
1003 int32_t exLen
[] = { 0, 1, 0, 0, 1, 0, 0, 0, 2, 0, 0};
1006 UErrorCode status
= U_ZERO_ERROR
;
1007 UText
*ut
= utext_openUTF8(NULL
, u8str
, -1, &status
);
1008 TEST_SUCCESS(status
);
1012 int32_t startMapLimit
= sizeof(startMap
) / sizeof(int32_t);
1013 for (i
=0; i
<startMapLimit
; i
++) {
1014 utext_setNativeIndex(ut
, i
);
1015 int64_t cpIndex
= utext_getNativeIndex(ut
);
1016 TEST_ASSERT(cpIndex
== startMap
[i
]);
1017 cpIndex
= UTEXT_GETNATIVEINDEX(ut
);
1018 TEST_ASSERT(cpIndex
== startMap
[i
]);
1022 for (i
=0; i
<startMapLimit
; i
++) {
1023 UChar32 c32
= utext_char32At(ut
, i
);
1024 TEST_ASSERT(c32
== c32Map
[i
]);
1025 int64_t cpIndex
= utext_getNativeIndex(ut
);
1026 TEST_ASSERT(cpIndex
== startMap
[i
]);
1029 // Check utext_next32From
1030 for (i
=0; i
<startMapLimit
; i
++) {
1031 UChar32 c32
= utext_next32From(ut
, i
);
1032 TEST_ASSERT(c32
== c32Map
[i
]);
1033 int64_t cpIndex
= utext_getNativeIndex(ut
);
1034 TEST_ASSERT(cpIndex
== nextMap
[i
]);
1037 // check utext_previous32From
1038 for (i
=0; i
<startMapLimit
; i
++) {
1040 UChar32 c32
= utext_previous32From(ut
, i
);
1041 TEST_ASSERT(c32
== pr32Map
[i
]);
1042 int64_t cpIndex
= utext_getNativeIndex(ut
);
1043 TEST_ASSERT(cpIndex
== prevMap
[i
]);
1047 // Extract from i to i+1, which may be zero or one code points,
1048 // depending on whether the indices straddle a cp boundary.
1049 for (i
=0; i
<startMapLimit
; i
++) {
1051 status
= U_ZERO_ERROR
;
1052 int32_t extractedLen
= utext_extract(ut
, i
, i
+1, buf
, 3, &status
);
1053 TEST_SUCCESS(status
);
1054 TEST_ASSERT(extractedLen
== exLen
[i
]);
1055 if (extractedLen
> 0) {
1057 /* extractedLen-extractedLen == 0 is used to get around a compiler warning. */
1058 U16_GET(buf
, 0, extractedLen
-extractedLen
, extractedLen
, c32
);
1059 TEST_ASSERT(c32
== c32Map
[i
]);
1067 { // Similar test, with utf16 instead of utf8
1068 // TODO: merge the common parts of these tests.
1070 UnicodeString
u16str("\\u1000\\U00011000\\u2000\\U00022000", -1, US_INV
);
1071 int32_t startMap
[] ={ 0, 1, 1, 3, 4, 4, 6, 6};
1072 int32_t nextMap
[] = { 1, 3, 3, 4, 6, 6, 6, 6};
1073 int32_t prevMap
[] = { 0, 0, 0, 1, 3, 3, 4, 4};
1074 UChar32 c32Map
[] = {0x1000, 0x11000, 0x11000, 0x2000, 0x22000, 0x22000, -1, -1};
1075 UChar32 pr32Map
[] = { -1, 0x1000, 0x1000, 0x11000, 0x2000, 0x2000, 0x22000, 0x22000};
1076 int32_t exLen
[] = { 1, 0, 2, 1, 0, 2, 0, 0,};
1078 u16str
= u16str
.unescape();
1079 UErrorCode status
= U_ZERO_ERROR
;
1080 UText
*ut
= utext_openUnicodeString(NULL
, &u16str
, &status
);
1081 TEST_SUCCESS(status
);
1083 int32_t startMapLimit
= sizeof(startMap
) / sizeof(int32_t);
1085 for (i
=0; i
<startMapLimit
; i
++) {
1086 utext_setNativeIndex(ut
, i
);
1087 int64_t cpIndex
= utext_getNativeIndex(ut
);
1088 TEST_ASSERT(cpIndex
== startMap
[i
]);
1092 for (i
=0; i
<startMapLimit
; i
++) {
1093 UChar32 c32
= utext_char32At(ut
, i
);
1094 TEST_ASSERT(c32
== c32Map
[i
]);
1095 int64_t cpIndex
= utext_getNativeIndex(ut
);
1096 TEST_ASSERT(cpIndex
== startMap
[i
]);
1099 // Check utext_next32From
1100 for (i
=0; i
<startMapLimit
; i
++) {
1101 UChar32 c32
= utext_next32From(ut
, i
);
1102 TEST_ASSERT(c32
== c32Map
[i
]);
1103 int64_t cpIndex
= utext_getNativeIndex(ut
);
1104 TEST_ASSERT(cpIndex
== nextMap
[i
]);
1107 // check utext_previous32From
1108 for (i
=0; i
<startMapLimit
; i
++) {
1109 UChar32 c32
= utext_previous32From(ut
, i
);
1110 TEST_ASSERT(c32
== pr32Map
[i
]);
1111 int64_t cpIndex
= utext_getNativeIndex(ut
);
1112 TEST_ASSERT(cpIndex
== prevMap
[i
]);
1116 // Extract from i to i+1, which may be zero or one code points,
1117 // depending on whether the indices straddle a cp boundary.
1118 for (i
=0; i
<startMapLimit
; i
++) {
1120 status
= U_ZERO_ERROR
;
1121 int32_t extractedLen
= utext_extract(ut
, i
, i
+1, buf
, 3, &status
);
1122 TEST_SUCCESS(status
);
1123 TEST_ASSERT(extractedLen
== exLen
[i
]);
1124 if (extractedLen
> 0) {
1126 /* extractedLen-extractedLen == 0 is used to get around a compiler warning. */
1127 U16_GET(buf
, 0, extractedLen
-extractedLen
, extractedLen
, c32
);
1128 TEST_ASSERT(c32
== c32Map
[i
]);
1135 { // Similar test, with UText over Replaceable
1136 // TODO: merge the common parts of these tests.
1138 UnicodeString
u16str("\\u1000\\U00011000\\u2000\\U00022000", -1, US_INV
);
1139 int32_t startMap
[] ={ 0, 1, 1, 3, 4, 4, 6, 6};
1140 int32_t nextMap
[] = { 1, 3, 3, 4, 6, 6, 6, 6};
1141 int32_t prevMap
[] = { 0, 0, 0, 1, 3, 3, 4, 4};
1142 UChar32 c32Map
[] = {0x1000, 0x11000, 0x11000, 0x2000, 0x22000, 0x22000, -1, -1};
1143 UChar32 pr32Map
[] = { -1, 0x1000, 0x1000, 0x11000, 0x2000, 0x2000, 0x22000, 0x22000};
1144 int32_t exLen
[] = { 1, 0, 2, 1, 0, 2, 0, 0,};
1146 u16str
= u16str
.unescape();
1147 UErrorCode status
= U_ZERO_ERROR
;
1148 UText
*ut
= utext_openReplaceable(NULL
, &u16str
, &status
);
1149 TEST_SUCCESS(status
);
1151 int32_t startMapLimit
= sizeof(startMap
) / sizeof(int32_t);
1153 for (i
=0; i
<startMapLimit
; i
++) {
1154 utext_setNativeIndex(ut
, i
);
1155 int64_t cpIndex
= utext_getNativeIndex(ut
);
1156 TEST_ASSERT(cpIndex
== startMap
[i
]);
1160 for (i
=0; i
<startMapLimit
; i
++) {
1161 UChar32 c32
= utext_char32At(ut
, i
);
1162 TEST_ASSERT(c32
== c32Map
[i
]);
1163 int64_t cpIndex
= utext_getNativeIndex(ut
);
1164 TEST_ASSERT(cpIndex
== startMap
[i
]);
1167 // Check utext_next32From
1168 for (i
=0; i
<startMapLimit
; i
++) {
1169 UChar32 c32
= utext_next32From(ut
, i
);
1170 TEST_ASSERT(c32
== c32Map
[i
]);
1171 int64_t cpIndex
= utext_getNativeIndex(ut
);
1172 TEST_ASSERT(cpIndex
== nextMap
[i
]);
1175 // check utext_previous32From
1176 for (i
=0; i
<startMapLimit
; i
++) {
1177 UChar32 c32
= utext_previous32From(ut
, i
);
1178 TEST_ASSERT(c32
== pr32Map
[i
]);
1179 int64_t cpIndex
= utext_getNativeIndex(ut
);
1180 TEST_ASSERT(cpIndex
== prevMap
[i
]);
1184 // Extract from i to i+1, which may be zero or one code points,
1185 // depending on whether the indices straddle a cp boundary.
1186 for (i
=0; i
<startMapLimit
; i
++) {
1188 status
= U_ZERO_ERROR
;
1189 int32_t extractedLen
= utext_extract(ut
, i
, i
+1, buf
, 3, &status
);
1190 TEST_SUCCESS(status
);
1191 TEST_ASSERT(extractedLen
== exLen
[i
]);
1192 if (extractedLen
> 0) {
1194 /* extractedLen-extractedLen == 0 is used to get around a compiler warning. */
1195 U16_GET(buf
, 0, extractedLen
-extractedLen
, extractedLen
, c32
);
1196 TEST_ASSERT(c32
== c32Map
[i
]);
1205 void UTextTest::FreezeTest() {
1206 // Check isWritable() and freeze() behavior.
1209 UnicodeString
ustr("Hello, World.");
1210 const char u8str
[] = {char(0x31), (char)0x32, (char)0x33, 0};
1211 const UChar u16str
[] = {(UChar
)0x31, (UChar
)0x32, (UChar
)0x44, 0};
1213 UErrorCode status
= U_ZERO_ERROR
;
1217 ut
= utext_openUTF8(ut
, u8str
, -1, &status
);
1218 TEST_SUCCESS(status
);
1219 UBool writable
= utext_isWritable(ut
);
1220 TEST_ASSERT(writable
== FALSE
);
1221 utext_copy(ut
, 1, 2, 0, TRUE
, &status
);
1222 TEST_ASSERT(status
== U_NO_WRITE_PERMISSION
);
1224 status
= U_ZERO_ERROR
;
1225 ut
= utext_openUChars(ut
, u16str
, -1, &status
);
1226 TEST_SUCCESS(status
);
1227 writable
= utext_isWritable(ut
);
1228 TEST_ASSERT(writable
== FALSE
);
1229 utext_copy(ut
, 1, 2, 0, TRUE
, &status
);
1230 TEST_ASSERT(status
== U_NO_WRITE_PERMISSION
);
1232 status
= U_ZERO_ERROR
;
1233 ut
= utext_openUnicodeString(ut
, &ustr
, &status
);
1234 TEST_SUCCESS(status
);
1235 writable
= utext_isWritable(ut
);
1236 TEST_ASSERT(writable
== TRUE
);
1238 writable
= utext_isWritable(ut
);
1239 TEST_ASSERT(writable
== FALSE
);
1240 utext_copy(ut
, 1, 2, 0, TRUE
, &status
);
1241 TEST_ASSERT(status
== U_NO_WRITE_PERMISSION
);
1243 status
= U_ZERO_ERROR
;
1244 ut
= utext_openUnicodeString(ut
, &ustr
, &status
);
1245 TEST_SUCCESS(status
);
1246 ut2
= utext_clone(ut2
, ut
, FALSE
, FALSE
, &status
); // clone with readonly = false
1247 TEST_SUCCESS(status
);
1248 writable
= utext_isWritable(ut2
);
1249 TEST_ASSERT(writable
== TRUE
);
1250 ut2
= utext_clone(ut2
, ut
, FALSE
, TRUE
, &status
); // clone with readonly = true
1251 TEST_SUCCESS(status
);
1252 writable
= utext_isWritable(ut2
);
1253 TEST_ASSERT(writable
== FALSE
);
1254 utext_copy(ut2
, 1, 2, 0, TRUE
, &status
);
1255 TEST_ASSERT(status
== U_NO_WRITE_PERMISSION
);
1257 status
= U_ZERO_ERROR
;
1258 ut
= utext_openConstUnicodeString(ut
, (const UnicodeString
*)&ustr
, &status
);
1259 TEST_SUCCESS(status
);
1260 writable
= utext_isWritable(ut
);
1261 TEST_ASSERT(writable
== FALSE
);
1262 utext_copy(ut
, 1, 2, 0, TRUE
, &status
);
1263 TEST_ASSERT(status
== U_NO_WRITE_PERMISSION
);
1265 // Deep Clone of a frozen UText should re-enable writing in the copy.
1266 status
= U_ZERO_ERROR
;
1267 ut
= utext_openUnicodeString(ut
, &ustr
, &status
);
1268 TEST_SUCCESS(status
);
1270 ut2
= utext_clone(ut2
, ut
, TRUE
, FALSE
, &status
); // deep clone
1271 TEST_SUCCESS(status
);
1272 writable
= utext_isWritable(ut2
);
1273 TEST_ASSERT(writable
== TRUE
);
1276 // Deep clone of a frozen UText, where the base type is intrinsically non-writable,
1277 // should NOT enable writing in the copy.
1278 status
= U_ZERO_ERROR
;
1279 ut
= utext_openUChars(ut
, u16str
, -1, &status
);
1280 TEST_SUCCESS(status
);
1282 ut2
= utext_clone(ut2
, ut
, TRUE
, FALSE
, &status
); // deep clone
1283 TEST_SUCCESS(status
);
1284 writable
= utext_isWritable(ut2
);
1285 TEST_ASSERT(writable
== FALSE
);
1295 // A UText type that works with a chunk size of 1.
1296 // Intended to test for edge cases.
1297 // Input comes from a UnicodeString.
1299 // ut.b the character. Put into both halves.
1303 static UBool U_CALLCONV
1304 fragTextAccess(UText
*ut
, int64_t index
, UBool forward
) {
1305 const UnicodeString
*us
= (const UnicodeString
*)ut
->context
;
1307 int32_t length
= us
->length();
1308 if (forward
&& index
>=0 && index
<length
) {
1309 c
= us
->charAt((int32_t)index
);
1311 ut
->chunkOffset
= 0;
1312 ut
->chunkLength
= 1;
1313 ut
->chunkNativeStart
= index
;
1314 ut
->chunkNativeLimit
= index
+1;
1317 if (!forward
&& index
>0 && index
<=length
) {
1318 c
= us
->charAt((int32_t)index
-1);
1320 ut
->chunkOffset
= 1;
1321 ut
->chunkLength
= 1;
1322 ut
->chunkNativeStart
= index
-1;
1323 ut
->chunkNativeLimit
= index
;
1327 ut
->chunkOffset
= 0;
1328 ut
->chunkLength
= 0;
1330 ut
->chunkNativeStart
= 0;
1331 ut
->chunkNativeLimit
= 0;
1333 ut
->chunkNativeStart
= length
;
1334 ut
->chunkNativeLimit
= length
;
1339 // Function table to be used with this fragmented text provider.
1340 // Initialized in the open function.
1341 static UTextFuncs fragmentFuncs
;
1343 // Clone function for fragmented text provider.
1344 // Didn't really want to provide this, but it's easier to provide it than to keep it
1345 // out of the tests.
1348 cloneFragmentedUnicodeString(UText
*dest
, const UText
*src
, UBool deep
, UErrorCode
*status
) {
1349 if (U_FAILURE(*status
)) {
1353 *status
= U_UNSUPPORTED_ERROR
;
1356 dest
= utext_openUnicodeString(dest
, (UnicodeString
*)src
->context
, status
);
1357 utext_setNativeIndex(dest
, utext_getNativeIndex(src
));
1363 // Open function for the fragmented text provider.
1365 openFragmentedUnicodeString(UText
*ut
, UnicodeString
*s
, UErrorCode
*status
) {
1366 ut
= utext_openUnicodeString(ut
, s
, status
);
1367 if (U_FAILURE(*status
)) {
1371 // Copy of the function table from the stock UnicodeString UText,
1372 // and replace the entry for the access function.
1373 memcpy(&fragmentFuncs
, ut
->pFuncs
, sizeof(fragmentFuncs
));
1374 fragmentFuncs
.access
= fragTextAccess
;
1375 fragmentFuncs
.clone
= cloneFragmentedUnicodeString
;
1376 ut
->pFuncs
= &fragmentFuncs
;
1378 ut
->chunkContents
= (UChar
*)&ut
->b
;
1379 ut
->pFuncs
->access(ut
, 0, TRUE
);
1383 // Regression test for Ticket 5560
1384 // Clone fails to update chunkContentPointer in the cloned copy.
1385 // This is only an issue for UText types that work in a local buffer,
1386 // (UTF-8 wrapper, for example)
1389 // 1. Create an inital UText
1390 // 2. Deep clone it. Contents should match original.
1391 // 3. Reset original to something different.
1392 // 4. Check that clone contents did not change.
1394 void UTextTest::Ticket5560() {
1395 /* The following two strings are in UTF-8 even on EBCDIC platforms. */
1396 static const char s1
[] = {0x41,0x42,0x43,0x44,0x45,0x46,0}; /* "ABCDEF" */
1397 static const char s2
[] = {0x31,0x32,0x33,0x34,0x35,0x36,0}; /* "123456" */
1398 UErrorCode status
= U_ZERO_ERROR
;
1400 UText ut1
= UTEXT_INITIALIZER
;
1401 UText ut2
= UTEXT_INITIALIZER
;
1403 utext_openUTF8(&ut1
, s1
, -1, &status
);
1404 UChar c
= utext_next32(&ut1
);
1405 TEST_ASSERT(c
== 0x41); // c == 'A'
1407 utext_clone(&ut2
, &ut1
, TRUE
, FALSE
, &status
);
1408 TEST_SUCCESS(status
);
1409 c
= utext_next32(&ut2
);
1410 TEST_ASSERT(c
== 0x42); // c == 'B'
1411 c
= utext_next32(&ut1
);
1412 TEST_ASSERT(c
== 0x42); // c == 'B'
1414 utext_openUTF8(&ut1
, s2
, -1, &status
);
1415 c
= utext_next32(&ut1
);
1416 TEST_ASSERT(c
== 0x31); // c == '1'
1417 c
= utext_next32(&ut2
);
1418 TEST_ASSERT(c
== 0x43); // c == 'C'
1425 // Test for Ticket 6847
1427 void UTextTest::Ticket6847() {
1428 const int STRLEN
= 90;
1430 u_memset(s
, 0x41, STRLEN
);
1433 UErrorCode status
= U_ZERO_ERROR
;
1434 UText
*ut
= utext_openUChars(NULL
, s
, -1, &status
);
1436 utext_setNativeIndex(ut
, 0);
1439 int64_t nativeIndex
= UTEXT_GETNATIVEINDEX(ut
);
1440 TEST_ASSERT(nativeIndex
== 0);
1441 while ((c
= utext_next32(ut
)) != U_SENTINEL
) {
1442 TEST_ASSERT(c
== 0x41);
1443 TEST_ASSERT(count
< STRLEN
);
1444 if (count
>= STRLEN
) {
1448 nativeIndex
= UTEXT_GETNATIVEINDEX(ut
);
1449 TEST_ASSERT(nativeIndex
== count
);
1451 TEST_ASSERT(count
== STRLEN
);
1452 nativeIndex
= UTEXT_GETNATIVEINDEX(ut
);
1453 TEST_ASSERT(nativeIndex
== STRLEN
);
1458 void UTextTest::Ticket10562() {
1459 // Note: failures show as a heap error when the test is run under valgrind.
1460 UErrorCode status
= U_ZERO_ERROR
;
1462 const char *utf8_string
= "\x41\x41\x41\x41\x41\x41\x41\x41\x41\x41\x41\x41\x41\x41\x41";
1463 UText
*utf8Text
= utext_openUTF8(NULL
, utf8_string
, -1, &status
);
1464 TEST_SUCCESS(status
);
1465 UText
*deepClone
= utext_clone(NULL
, utf8Text
, TRUE
, FALSE
, &status
);
1466 TEST_SUCCESS(status
);
1467 UText
*shallowClone
= utext_clone(NULL
, deepClone
, FALSE
, FALSE
, &status
);
1468 TEST_SUCCESS(status
);
1469 utext_close(shallowClone
);
1470 utext_close(deepClone
);
1471 utext_close(utf8Text
);
1473 status
= U_ZERO_ERROR
;
1474 UnicodeString
usString("Hello, World.");
1475 UText
*usText
= utext_openUnicodeString(NULL
, &usString
, &status
);
1476 TEST_SUCCESS(status
);
1477 UText
*usDeepClone
= utext_clone(NULL
, usText
, TRUE
, FALSE
, &status
);
1478 TEST_SUCCESS(status
);
1479 UText
*usShallowClone
= utext_clone(NULL
, usDeepClone
, FALSE
, FALSE
, &status
);
1480 TEST_SUCCESS(status
);
1481 utext_close(usShallowClone
);
1482 utext_close(usDeepClone
);
1483 utext_close(usText
);