1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
3 /********************************************************************
5 * Copyright (c) 2005-2016, International Business Machines Corporation and
6 * others. All Rights Reserved.
7 ********************************************************************/
8 /************************************************************************
9 * Tests for the UText and UTextIterator text abstraction classses
11 ************************************************************************/
16 #include "unicode/utypes.h"
17 #include "unicode/utext.h"
18 #include "unicode/utf8.h"
19 #include "unicode/utf16.h"
20 #include "unicode/ustring.h"
21 #include "unicode/uchriter.h"
26 static UBool gFailed
= FALSE
;
27 static int gTestNum
= 0;
30 UText
*openFragmentedUnicodeString(UText
*ut
, UnicodeString
*s
, UErrorCode
*status
);
32 #define TEST_ASSERT(x) UPRV_BLOCK_MACRO_BEGIN { \
34 errln("Test #%d failure in file %s at line %d\n", gTestNum, __FILE__, __LINE__); \
37 } UPRV_BLOCK_MACRO_END
40 #define TEST_SUCCESS(status) UPRV_BLOCK_MACRO_BEGIN { \
41 if (U_FAILURE(status)) { \
42 errln("Test #%d failure in file %s at line %d. Error = \"%s\"\n", \
43 gTestNum, __FILE__, __LINE__, u_errorName(status)); \
46 } UPRV_BLOCK_MACRO_END
48 UTextTest::UTextTest() {
51 UTextTest::~UTextTest() {
56 UTextTest::runIndexedTest(int32_t index
, UBool exec
,
57 const char* &name
, char* /*par*/) {
59 TESTCASE_AUTO(TextTest
);
60 TESTCASE_AUTO(ErrorTest
);
61 TESTCASE_AUTO(FreezeTest
);
62 TESTCASE_AUTO(Ticket5560
);
63 TESTCASE_AUTO(Ticket6847
);
64 TESTCASE_AUTO(Ticket10562
);
65 TESTCASE_AUTO(Ticket10983
);
66 TESTCASE_AUTO(Ticket12130
);
67 TESTCASE_AUTO(Ticket13344
);
72 // Quick and dirty random number generator.
73 // (don't use library so that results are portable.
74 static uint32_t m_seed
= 1;
75 static uint32_t m_rand()
77 m_seed
= m_seed
* 1103515245 + 12345;
78 return (uint32_t)(m_seed
/65536) % 32768;
85 // Top Level function for UText testing.
86 // Specifies the strings to be tested, with the acutal testing itself
87 // being carried out in another function, TestString().
89 void UTextTest::TextTest() {
92 TestString("abcd\\U00010001xyz");
95 // Supplementary chars at start or end
96 TestString("\\U00010001");
97 TestString("abc\\U00010001");
98 TestString("\\U00010001abc");
100 // Test simple strings of lengths 1 to 60, looking for glitches at buffer boundaries
102 for (i
=1; i
<60; i
++) {
104 for (j
=0; j
<i
; j
++) {
105 if (j
+0x30 == 0x5c) {
106 // backslash. Needs to be escaped
107 s
.append((UChar
)0x5c);
109 s
.append(UChar(j
+0x30));
114 // Test strings with odd-aligned supplementary chars,
115 // looking for glitches at buffer boundaries
116 for (i
=1; i
<60; i
++) {
118 s
.append((UChar
)0x41);
119 for (j
=0; j
<i
; j
++) {
120 s
.append(UChar32(j
+0x11000));
125 // String of chars of randomly varying size in utf-8 representation.
126 // Exercise the mapping, and the varying sized buffer.
132 UChar32 c4
= 0x11000;
133 for (i
=0; i
<1000; i
++) {
134 int len8
= m_rand()%4
+ 1;
138 // don't put 0 into string (0 terminated strings for some tests)
139 // don't put '\', will cause unescape() to fail.
140 if (c1
==0x5c || c1
==0) {
161 // TestString() Run a suite of UText tests on a string.
162 // The test string is unescaped before use.
164 void UTextTest::TestString(const UnicodeString
&s
) {
169 UErrorCode status
= U_ZERO_ERROR
;
173 UnicodeString sa
= s
.unescape();
177 // Build up a mapping between code points and UTF-16 code unit indexes.
179 m
*cpMap
= new m
[sa
.length() + 1];
181 for (i
=0; i
<sa
.length(); i
=sa
.moveIndex32(i
, 1)) {
183 cpMap
[j
].nativeIdx
= i
;
188 cpMap
[j
].nativeIdx
= i
; // position following the last char in utf-16 string.
191 // UChar * test, null terminated
192 status
= U_ZERO_ERROR
;
193 UChar
*buf
= new UChar
[saLen
+1];
194 sa
.extract(buf
, saLen
+1, status
);
195 TEST_SUCCESS(status
);
196 ut
= utext_openUChars(NULL
, buf
, -1, &status
);
197 TEST_SUCCESS(status
);
198 TestAccess(sa
, ut
, cpCount
, cpMap
);
202 // UChar * test, with length
203 status
= U_ZERO_ERROR
;
204 buf
= new UChar
[saLen
+1];
205 sa
.extract(buf
, saLen
+1, status
);
206 TEST_SUCCESS(status
);
207 ut
= utext_openUChars(NULL
, buf
, saLen
, &status
);
208 TEST_SUCCESS(status
);
209 TestAccess(sa
, ut
, cpCount
, cpMap
);
214 // UnicodeString test
215 status
= U_ZERO_ERROR
;
216 ut
= utext_openUnicodeString(NULL
, &sa
, &status
);
217 TEST_SUCCESS(status
);
218 TestAccess(sa
, ut
, cpCount
, cpMap
);
219 TestCMR(sa
, ut
, cpCount
, cpMap
, cpMap
);
223 // Const UnicodeString test
224 status
= U_ZERO_ERROR
;
225 ut
= utext_openConstUnicodeString(NULL
, &sa
, &status
);
226 TEST_SUCCESS(status
);
227 TestAccess(sa
, ut
, cpCount
, cpMap
);
231 // Replaceable test. (UnicodeString inherits Replaceable)
232 status
= U_ZERO_ERROR
;
233 ut
= utext_openReplaceable(NULL
, &sa
, &status
);
234 TEST_SUCCESS(status
);
235 TestAccess(sa
, ut
, cpCount
, cpMap
);
236 TestCMR(sa
, ut
, cpCount
, cpMap
, cpMap
);
239 // Character Iterator Tests
240 status
= U_ZERO_ERROR
;
241 const UChar
*cbuf
= sa
.getBuffer();
242 CharacterIterator
*ci
= new UCharCharacterIterator(cbuf
, saLen
, status
);
243 TEST_SUCCESS(status
);
244 ut
= utext_openCharacterIterator(NULL
, ci
, &status
);
245 TEST_SUCCESS(status
);
246 TestAccess(sa
, ut
, cpCount
, cpMap
);
251 // Fragmented UnicodeString (Chunk size of one)
253 status
= U_ZERO_ERROR
;
254 ut
= openFragmentedUnicodeString(NULL
, &sa
, &status
);
255 TEST_SUCCESS(status
);
256 TestAccess(sa
, ut
, cpCount
, cpMap
);
263 // Convert the test string from UnicodeString to (char *) in utf-8 format
264 int32_t u8Len
= sa
.extract(0, sa
.length(), NULL
, 0, "utf-8");
265 char *u8String
= new char[u8Len
+ 1];
266 sa
.extract(0, sa
.length(), u8String
, u8Len
+1, "utf-8");
268 // Build up the map of code point indices in the utf-8 string
269 m
* u8Map
= new m
[sa
.length() + 1];
270 i
= 0; // native utf-8 index
271 for (j
=0; j
<cpCount
; j
++) { // code point number
272 u8Map
[j
].nativeIdx
= i
;
273 U8_NEXT(u8String
, i
, u8Len
, c
);
276 u8Map
[cpCount
].nativeIdx
= u8Len
; // position following the last char in utf-8 string.
278 // Do the test itself
279 status
= U_ZERO_ERROR
;
280 ut
= utext_openUTF8(NULL
, u8String
, -1, &status
);
281 TEST_SUCCESS(status
);
282 TestAccess(sa
, ut
, cpCount
, u8Map
);
292 // TestCMR test Copy, Move and Replace operations.
293 // us UnicodeString containing the test text.
294 // ut UText containing the same test text.
295 // cpCount number of code points in the test text.
296 // nativeMap Mapping from code points to native indexes for the UText.
297 // u16Map Mapping from code points to UTF-16 indexes, for use with the UnicodeString.
299 // This function runs a whole series of opertions on each incoming UText.
300 // The UText is deep-cloned prior to each operation, so that the original UText remains unchanged.
302 void UTextTest::TestCMR(const UnicodeString
&us
, UText
*ut
, int cpCount
, m
*nativeMap
, m
*u16Map
) {
303 TEST_ASSERT(utext_isWritable(ut
) == TRUE
);
305 int srcLengthType
; // Loop variables for selecting the postion and length
306 int srcPosType
; // of the block to operate on within the source text.
309 int srcIndex
= 0; // Code Point indexes of the block to operate on for
310 int srcLength
= 0; // a specific test.
312 int destIndex
= 0; // Code point index of the destination for a copy/move test.
314 int32_t nativeStart
= 0; // Native unit indexes for a test.
315 int32_t nativeLimit
= 0;
316 int32_t nativeDest
= 0;
318 int32_t u16Start
= 0; // UTF-16 indexes for a test.
319 int32_t u16Limit
= 0; // used when performing the same operation in a Unicode String
322 // Iterate over a whole series of source index, length and a target indexes.
323 // This is done with code point indexes; these will be later translated to native
324 // indexes using the cpMap.
325 for (srcLengthType
=1; srcLengthType
<=3; srcLengthType
++) {
326 switch (srcLengthType
) {
327 case 1: srcLength
= 1; break;
328 case 2: srcLength
= 5; break;
329 case 3: srcLength
= cpCount
/ 3;
331 for (srcPosType
=1; srcPosType
<=5; srcPosType
++) {
332 switch (srcPosType
) {
333 case 1: srcIndex
= 0; break;
334 case 2: srcIndex
= 1; break;
335 case 3: srcIndex
= cpCount
- srcLength
; break;
336 case 4: srcIndex
= cpCount
- srcLength
- 1; break;
337 case 5: srcIndex
= cpCount
/ 2; break;
339 if (srcIndex
< 0 || srcIndex
+ srcLength
> cpCount
) {
340 // filter out bogus test cases -
341 // those with a source range that falls of an edge of the string.
346 // Copy and move tests.
347 // iterate over a variety of destination positions.
349 for (destPosType
=1; destPosType
<=4; destPosType
++) {
350 switch (destPosType
) {
351 case 1: destIndex
= 0; break;
352 case 2: destIndex
= 1; break;
353 case 3: destIndex
= srcIndex
- 1; break;
354 case 4: destIndex
= srcIndex
+ srcLength
+ 1; break;
355 case 5: destIndex
= cpCount
-1; break;
356 case 6: destIndex
= cpCount
; break;
358 if (destIndex
<0 || destIndex
>cpCount
) {
359 // filter out bogus test cases.
363 nativeStart
= nativeMap
[srcIndex
].nativeIdx
;
364 nativeLimit
= nativeMap
[srcIndex
+srcLength
].nativeIdx
;
365 nativeDest
= nativeMap
[destIndex
].nativeIdx
;
367 u16Start
= u16Map
[srcIndex
].nativeIdx
;
368 u16Limit
= u16Map
[srcIndex
+srcLength
].nativeIdx
;
369 u16Dest
= u16Map
[destIndex
].nativeIdx
;
372 TestCopyMove(us
, ut
, FALSE
,
373 nativeStart
, nativeLimit
, nativeDest
,
374 u16Start
, u16Limit
, u16Dest
);
376 TestCopyMove(us
, ut
, TRUE
,
377 nativeStart
, nativeLimit
, nativeDest
,
378 u16Start
, u16Limit
, u16Dest
);
388 UnicodeString
fullRepString("This is an arbitrary string that will be used as replacement text");
389 for (int32_t replStrLen
=0; replStrLen
<20; replStrLen
++) {
390 UnicodeString
repStr(fullRepString
, 0, replStrLen
);
392 nativeStart
, nativeLimit
,
406 // TestCopyMove run a single test case for utext_copy.
407 // Test cases are created in TestCMR and dispatched here for execution.
409 void UTextTest::TestCopyMove(const UnicodeString
&us
, UText
*ut
, UBool move
,
410 int32_t nativeStart
, int32_t nativeLimit
, int32_t nativeDest
,
411 int32_t u16Start
, int32_t u16Limit
, int32_t u16Dest
)
413 UErrorCode status
= U_ZERO_ERROR
;
414 UText
*targetUT
= NULL
;
419 // clone the UText. The test will be run in the cloned copy
420 // so that we don't alter the original.
422 targetUT
= utext_clone(NULL
, ut
, TRUE
, FALSE
, &status
);
423 TEST_SUCCESS(status
);
424 UnicodeString
targetUS(us
); // And copy the reference string.
426 // do the test operation first in the reference
427 targetUS
.copy(u16Start
, u16Limit
, u16Dest
);
429 // delete out the source range.
430 if (u16Limit
< u16Dest
) {
431 targetUS
.removeBetween(u16Start
, u16Limit
);
433 int32_t amtCopied
= u16Limit
- u16Start
;
434 targetUS
.removeBetween(u16Start
+amtCopied
, u16Limit
+amtCopied
);
438 // Do the same operation in the UText under test
439 utext_copy(targetUT
, nativeStart
, nativeLimit
, nativeDest
, move
, &status
);
440 if (nativeDest
> nativeStart
&& nativeDest
< nativeLimit
) {
441 TEST_ASSERT(status
== U_INDEX_OUTOFBOUNDS_ERROR
);
443 TEST_SUCCESS(status
);
445 // Compare the results of the two parallel tests
446 int32_t usi
= 0; // UnicodeString postion, utf-16 index.
447 int64_t uti
= 0; // UText position, native index.
448 int32_t cpi
; // char32 position (code point index)
449 UChar32 usc
; // code point from Unicode String
450 UChar32 utc
; // code point from UText
451 utext_setNativeIndex(targetUT
, 0);
452 for (cpi
=0; ; cpi
++) {
453 usc
= targetUS
.char32At(usi
);
454 utc
= utext_next32(targetUT
);
458 TEST_ASSERT(uti
== usi
);
459 TEST_ASSERT(utc
== usc
);
460 usi
= targetUS
.moveIndex32(usi
, 1);
461 uti
= utext_getNativeIndex(targetUT
);
463 goto cleanupAndReturn
;
466 int64_t expectedNativeLength
= utext_nativeLength(ut
);
468 expectedNativeLength
+= nativeLimit
- nativeStart
;
470 uti
= utext_getNativeIndex(targetUT
);
471 TEST_ASSERT(uti
== expectedNativeLength
);
475 utext_close(targetUT
);
480 // TestReplace Test a single Replace operation.
482 void UTextTest::TestReplace(
483 const UnicodeString
&us
, // reference UnicodeString in which to do the replace
484 UText
*ut
, // UnicodeText object under test.
485 int32_t nativeStart
, // Range to be replaced, in UText native units.
487 int32_t u16Start
, // Range to be replaced, in UTF-16 units
488 int32_t u16Limit
, // for use in the reference UnicodeString.
489 const UnicodeString
&repStr
) // The replacement string
491 UErrorCode status
= U_ZERO_ERROR
;
492 UText
*targetUT
= NULL
;
497 // clone the target UText. The test will be run in the cloned copy
498 // so that we don't alter the original.
500 targetUT
= utext_clone(NULL
, ut
, TRUE
, FALSE
, &status
);
501 TEST_SUCCESS(status
);
502 UnicodeString
targetUS(us
); // And copy the reference string.
505 // Do the replace operation in the Unicode String, to
506 // produce a reference result.
508 targetUS
.replace(u16Start
, u16Limit
-u16Start
, repStr
);
511 // Do the replace on the UText under test
513 const UChar
*rs
= repStr
.getBuffer();
514 int32_t rsLen
= repStr
.length();
515 int32_t actualDelta
= utext_replace(targetUT
, nativeStart
, nativeLimit
, rs
, rsLen
, &status
);
516 int32_t expectedDelta
= repStr
.length() - (nativeLimit
- nativeStart
);
517 TEST_ASSERT(actualDelta
== expectedDelta
);
520 // Compare the results
522 int32_t usi
= 0; // UnicodeString postion, utf-16 index.
523 int64_t uti
= 0; // UText position, native index.
524 int32_t cpi
; // char32 position (code point index)
525 UChar32 usc
; // code point from Unicode String
526 UChar32 utc
; // code point from UText
527 int64_t expectedNativeLength
= 0;
528 utext_setNativeIndex(targetUT
, 0);
529 for (cpi
=0; ; cpi
++) {
530 usc
= targetUS
.char32At(usi
);
531 utc
= utext_next32(targetUT
);
535 TEST_ASSERT(uti
== usi
);
536 TEST_ASSERT(utc
== usc
);
537 usi
= targetUS
.moveIndex32(usi
, 1);
538 uti
= utext_getNativeIndex(targetUT
);
540 goto cleanupAndReturn
;
543 expectedNativeLength
= utext_nativeLength(ut
) + expectedDelta
;
544 uti
= utext_getNativeIndex(targetUT
);
545 TEST_ASSERT(uti
== expectedNativeLength
);
548 utext_close(targetUT
);
552 // TestAccess Test the read only access functions on a UText, including cloning.
553 // The text is accessed in a variety of ways, and compared with
554 // the reference UnicodeString.
556 void UTextTest::TestAccess(const UnicodeString
&us
, UText
*ut
, int cpCount
, m
*cpMap
) {
557 // Run the standard tests on the caller-supplied UText.
558 TestAccessNoClone(us
, ut
, cpCount
, cpMap
);
560 // Re-run tests on a shallow clone.
561 utext_setNativeIndex(ut
, 0);
562 UErrorCode status
= U_ZERO_ERROR
;
563 UText
*shallowClone
= utext_clone(NULL
, ut
, FALSE
/*deep*/, FALSE
/*readOnly*/, &status
);
564 TEST_SUCCESS(status
);
565 TestAccessNoClone(us
, shallowClone
, cpCount
, cpMap
);
568 // Rerun again on a deep clone.
569 // Note that text providers are not required to provide deep cloning,
570 // so unsupported errors are ignored.
572 status
= U_ZERO_ERROR
;
573 utext_setNativeIndex(shallowClone
, 0);
574 UText
*deepClone
= utext_clone(NULL
, shallowClone
, TRUE
, FALSE
, &status
);
575 utext_close(shallowClone
);
576 if (status
!= U_UNSUPPORTED_ERROR
) {
577 TEST_SUCCESS(status
);
578 TestAccessNoClone(us
, deepClone
, cpCount
, cpMap
);
580 utext_close(deepClone
);
585 // TestAccessNoClone() Test the read only access functions on a UText.
586 // The text is accessed in a variety of ways, and compared with
587 // the reference UnicodeString.
589 void UTextTest::TestAccessNoClone(const UnicodeString
&us
, UText
*ut
, int cpCount
, m
*cpMap
) {
590 UErrorCode status
= U_ZERO_ERROR
;
594 // Check the length from the UText
596 int64_t expectedLen
= cpMap
[cpCount
].nativeIdx
;
597 int64_t utlen
= utext_nativeLength(ut
);
598 TEST_ASSERT(expectedLen
== utlen
);
601 // Iterate forwards, verify that we get the correct code points
602 // at the correct native offsets.
606 int64_t expectedIndex
= 0;
607 int64_t foundIndex
= 0;
612 for (i
=0; i
<cpCount
; i
++) {
613 expectedIndex
= cpMap
[i
].nativeIdx
;
614 foundIndex
= utext_getNativeIndex(ut
);
615 TEST_ASSERT(expectedIndex
== foundIndex
);
616 expectedC
= cpMap
[i
].cp
;
617 foundC
= utext_next32(ut
);
618 TEST_ASSERT(expectedC
== foundC
);
619 foundIndex
= utext_getPreviousNativeIndex(ut
);
620 TEST_ASSERT(expectedIndex
== foundIndex
);
625 foundC
= utext_next32(ut
);
626 TEST_ASSERT(foundC
== U_SENTINEL
);
628 // Repeat above, using macros
629 utext_setNativeIndex(ut
, 0);
630 for (i
=0; i
<cpCount
; i
++) {
631 expectedIndex
= cpMap
[i
].nativeIdx
;
632 foundIndex
= UTEXT_GETNATIVEINDEX(ut
);
633 TEST_ASSERT(expectedIndex
== foundIndex
);
634 expectedC
= cpMap
[i
].cp
;
635 foundC
= UTEXT_NEXT32(ut
);
636 TEST_ASSERT(expectedC
== foundC
);
641 foundC
= UTEXT_NEXT32(ut
);
642 TEST_ASSERT(foundC
== U_SENTINEL
);
645 // Forward iteration (above) should have left index at the
646 // end of the input, which should == length().
648 len
= utext_nativeLength(ut
);
649 foundIndex
= utext_getNativeIndex(ut
);
650 TEST_ASSERT(len
== foundIndex
);
653 // Iterate backwards over entire test string
655 len
= utext_getNativeIndex(ut
);
656 utext_setNativeIndex(ut
, len
);
657 for (i
=cpCount
-1; i
>=0; i
--) {
658 expectedC
= cpMap
[i
].cp
;
659 expectedIndex
= cpMap
[i
].nativeIdx
;
660 int64_t prevIndex
= utext_getPreviousNativeIndex(ut
);
661 foundC
= utext_previous32(ut
);
662 foundIndex
= utext_getNativeIndex(ut
);
663 TEST_ASSERT(expectedIndex
== foundIndex
);
664 TEST_ASSERT(expectedC
== foundC
);
665 TEST_ASSERT(prevIndex
== foundIndex
);
672 // Backwards iteration, above, should have left our iterator
673 // position at zero, and continued backwards iterationshould fail.
675 foundIndex
= utext_getNativeIndex(ut
);
676 TEST_ASSERT(foundIndex
== 0);
677 foundIndex
= utext_getPreviousNativeIndex(ut
);
678 TEST_ASSERT(foundIndex
== 0);
681 foundC
= utext_previous32(ut
);
682 TEST_ASSERT(foundC
== U_SENTINEL
);
683 foundIndex
= utext_getNativeIndex(ut
);
684 TEST_ASSERT(foundIndex
== 0);
685 foundIndex
= utext_getPreviousNativeIndex(ut
);
686 TEST_ASSERT(foundIndex
== 0);
689 // And again, with the macros
690 utext_setNativeIndex(ut
, len
);
691 for (i
=cpCount
-1; i
>=0; i
--) {
692 expectedC
= cpMap
[i
].cp
;
693 expectedIndex
= cpMap
[i
].nativeIdx
;
694 foundC
= UTEXT_PREVIOUS32(ut
);
695 foundIndex
= UTEXT_GETNATIVEINDEX(ut
);
696 TEST_ASSERT(expectedIndex
== foundIndex
);
697 TEST_ASSERT(expectedC
== foundC
);
704 // Backwards iteration, above, should have left our iterator
705 // position at zero, and continued backwards iterationshould fail.
707 foundIndex
= UTEXT_GETNATIVEINDEX(ut
);
708 TEST_ASSERT(foundIndex
== 0);
710 foundC
= UTEXT_PREVIOUS32(ut
);
711 TEST_ASSERT(foundC
== U_SENTINEL
);
712 foundIndex
= UTEXT_GETNATIVEINDEX(ut
);
713 TEST_ASSERT(foundIndex
== 0);
719 // next32From(), prevous32From(), Iterate in a somewhat random order.
722 for (i
=0; i
<cpCount
; i
++) {
723 cpIndex
= (cpIndex
+ 9973) % cpCount
;
724 index
= cpMap
[cpIndex
].nativeIdx
;
725 expectedC
= cpMap
[cpIndex
].cp
;
726 foundC
= utext_next32From(ut
, index
);
727 TEST_ASSERT(expectedC
== foundC
);
734 for (i
=0; i
<cpCount
; i
++) {
735 cpIndex
= (cpIndex
+ 9973) % cpCount
;
736 index
= cpMap
[cpIndex
+1].nativeIdx
;
737 expectedC
= cpMap
[cpIndex
].cp
;
738 foundC
= utext_previous32From(ut
, index
);
739 TEST_ASSERT(expectedC
== foundC
);
747 // moveIndex(int32_t delta);
750 // Walk through frontwards, incrementing by one
751 utext_setNativeIndex(ut
, 0);
752 for (i
=1; i
<=cpCount
; i
++) {
753 utext_moveIndex32(ut
, 1);
754 index
= utext_getNativeIndex(ut
);
755 expectedIndex
= cpMap
[i
].nativeIdx
;
756 TEST_ASSERT(expectedIndex
== index
);
757 index
= UTEXT_GETNATIVEINDEX(ut
);
758 TEST_ASSERT(expectedIndex
== index
);
761 // Walk through frontwards, incrementing by two
762 utext_setNativeIndex(ut
, 0);
763 for (i
=2; i
<cpCount
; i
+=2) {
764 utext_moveIndex32(ut
, 2);
765 index
= utext_getNativeIndex(ut
);
766 expectedIndex
= cpMap
[i
].nativeIdx
;
767 TEST_ASSERT(expectedIndex
== index
);
768 index
= UTEXT_GETNATIVEINDEX(ut
);
769 TEST_ASSERT(expectedIndex
== index
);
772 // walk through the string backwards, decrementing by one.
773 i
= cpMap
[cpCount
].nativeIdx
;
774 utext_setNativeIndex(ut
, i
);
775 for (i
=cpCount
; i
>=0; i
--) {
776 expectedIndex
= cpMap
[i
].nativeIdx
;
777 index
= utext_getNativeIndex(ut
);
778 TEST_ASSERT(expectedIndex
== index
);
779 index
= UTEXT_GETNATIVEINDEX(ut
);
780 TEST_ASSERT(expectedIndex
== index
);
781 utext_moveIndex32(ut
, -1);
785 // walk through backwards, decrementing by three
786 i
= cpMap
[cpCount
].nativeIdx
;
787 utext_setNativeIndex(ut
, i
);
788 for (i
=cpCount
; i
>=0; i
-=3) {
789 expectedIndex
= cpMap
[i
].nativeIdx
;
790 index
= utext_getNativeIndex(ut
);
791 TEST_ASSERT(expectedIndex
== index
);
792 index
= UTEXT_GETNATIVEINDEX(ut
);
793 TEST_ASSERT(expectedIndex
== index
);
794 utext_moveIndex32(ut
, -3);
801 int bufSize
= us
.length() + 10;
802 UChar
*buf
= new UChar
[bufSize
];
803 status
= U_ZERO_ERROR
;
804 expectedLen
= us
.length();
805 len
= utext_extract(ut
, 0, utlen
, buf
, bufSize
, &status
);
806 TEST_SUCCESS(status
);
807 TEST_ASSERT(len
== expectedLen
);
808 int compareResult
= us
.compare(buf
, -1);
809 TEST_ASSERT(compareResult
== 0);
811 status
= U_ZERO_ERROR
;
812 len
= utext_extract(ut
, 0, utlen
, NULL
, 0, &status
);
814 TEST_ASSERT(status
== U_STRING_NOT_TERMINATED_WARNING
);
816 TEST_ASSERT(status
== U_BUFFER_OVERFLOW_ERROR
);
818 TEST_ASSERT(len
== expectedLen
);
820 status
= U_ZERO_ERROR
;
821 u_memset(buf
, 0x5555, bufSize
);
822 len
= utext_extract(ut
, 0, utlen
, buf
, 1, &status
);
823 if (us
.length() == 0) {
824 TEST_SUCCESS(status
);
825 TEST_ASSERT(buf
[0] == 0);
827 // Buf len == 1, extracting a single 16 bit value.
828 // If the data char is supplementary, it doesn't matter whether the buffer remains unchanged,
829 // or whether the lead surrogate of the pair is extracted.
830 // It's a buffer overflow error in either case.
831 TEST_ASSERT(buf
[0] == us
.charAt(0) ||
832 (buf
[0] == 0x5555 && U_IS_SUPPLEMENTARY(us
.char32At(0))));
833 TEST_ASSERT(buf
[1] == 0x5555);
834 if (us
.length() == 1) {
835 TEST_ASSERT(status
== U_STRING_NOT_TERMINATED_WARNING
);
837 TEST_ASSERT(status
== U_BUFFER_OVERFLOW_ERROR
);
845 // ErrorTest() Check various error and edge cases.
847 void UTextTest::ErrorTest()
849 // Close of an unitialized UText. Shouldn't blow up.
852 memset(&ut
, 0, sizeof(UText
));
857 // Double-close of a UText. Shouldn't blow up. UText should still be usable.
859 UErrorCode status
= U_ZERO_ERROR
;
860 UText ut
= UTEXT_INITIALIZER
;
861 UnicodeString
s("Hello, World");
862 UText
*ut2
= utext_openUnicodeString(&ut
, &s
, &status
);
863 TEST_SUCCESS(status
);
864 TEST_ASSERT(ut2
== &ut
);
866 UText
*ut3
= utext_close(&ut
);
867 TEST_ASSERT(ut3
== &ut
);
869 UText
*ut4
= utext_close(&ut
);
870 TEST_ASSERT(ut4
== &ut
);
872 utext_openUnicodeString(&ut
, &s
, &status
);
873 TEST_SUCCESS(status
);
877 // Re-use of a UText, chaining through each of the types of UText
878 // (If it doesn't blow up, and doesn't leak, it's probably working fine)
880 UErrorCode status
= U_ZERO_ERROR
;
881 UText ut
= UTEXT_INITIALIZER
;
883 UnicodeString
s1("Hello, World");
884 UChar s2
[] = {(UChar
)0x41, (UChar
)0x42, (UChar
)0};
885 const char *s3
= "\x66\x67\x68";
887 utp
= utext_openUnicodeString(&ut
, &s1
, &status
);
888 TEST_SUCCESS(status
);
889 TEST_ASSERT(utp
== &ut
);
891 utp
= utext_openConstUnicodeString(&ut
, &s1
, &status
);
892 TEST_SUCCESS(status
);
893 TEST_ASSERT(utp
== &ut
);
895 utp
= utext_openUTF8(&ut
, s3
, -1, &status
);
896 TEST_SUCCESS(status
);
897 TEST_ASSERT(utp
== &ut
);
899 utp
= utext_openUChars(&ut
, s2
, -1, &status
);
900 TEST_SUCCESS(status
);
901 TEST_ASSERT(utp
== &ut
);
903 utp
= utext_close(&ut
);
904 TEST_ASSERT(utp
== &ut
);
906 utp
= utext_openUnicodeString(&ut
, &s1
, &status
);
907 TEST_SUCCESS(status
);
908 TEST_ASSERT(utp
== &ut
);
911 // Invalid parameters on open
914 UErrorCode status
= U_ZERO_ERROR
;
915 UText ut
= UTEXT_INITIALIZER
;
917 utext_openUChars(&ut
, NULL
, 5, &status
);
918 TEST_ASSERT(status
== U_ILLEGAL_ARGUMENT_ERROR
);
920 status
= U_ZERO_ERROR
;
921 utext_openUChars(&ut
, NULL
, -1, &status
);
922 TEST_ASSERT(status
== U_ILLEGAL_ARGUMENT_ERROR
);
924 status
= U_ZERO_ERROR
;
925 utext_openUTF8(&ut
, NULL
, 4, &status
);
926 TEST_ASSERT(status
== U_ILLEGAL_ARGUMENT_ERROR
);
928 status
= U_ZERO_ERROR
;
929 utext_openUTF8(&ut
, NULL
, -1, &status
);
930 TEST_ASSERT(status
== U_ILLEGAL_ARGUMENT_ERROR
);
934 // UTF-8 with malformed sequences.
935 // These should come through as the Unicode replacement char, \ufffd
938 UErrorCode status
= U_ZERO_ERROR
;
940 const char *badUTF8
= "\x41\x81\x42\xf0\x81\x81\x43";
943 ut
= utext_openUTF8(NULL
, badUTF8
, -1, &status
);
944 TEST_SUCCESS(status
);
945 c
= utext_char32At(ut
, 1);
946 TEST_ASSERT(c
== 0xfffd);
947 c
= utext_char32At(ut
, 3);
948 TEST_ASSERT(c
== 0xfffd);
949 c
= utext_char32At(ut
, 5);
950 TEST_ASSERT(c
== 0xfffd);
951 c
= utext_char32At(ut
, 6);
952 TEST_ASSERT(c
== 0x43);
955 int n
= utext_extract(ut
, 0, 9, buf
, 10, &status
);
956 TEST_SUCCESS(status
);
958 TEST_ASSERT(buf
[0] == 0x41);
959 TEST_ASSERT(buf
[1] == 0xfffd);
960 TEST_ASSERT(buf
[2] == 0x42);
961 TEST_ASSERT(buf
[3] == 0xfffd);
962 TEST_ASSERT(buf
[4] == 0xfffd);
963 TEST_ASSERT(buf
[5] == 0xfffd);
964 TEST_ASSERT(buf
[6] == 0x43);
970 // isLengthExpensive - does it make the exptected transitions after
971 // getting the length of a nul terminated string?
974 UErrorCode status
= U_ZERO_ERROR
;
975 UnicodeString
sa("Hello, this is a string");
979 memset(sb
, 0x20, sizeof(sb
));
982 UText
*uta
= utext_openUnicodeString(NULL
, &sa
, &status
);
983 TEST_SUCCESS(status
);
984 isExpensive
= utext_isLengthExpensive(uta
);
985 TEST_ASSERT(isExpensive
== FALSE
);
988 UText
*utb
= utext_openUChars(NULL
, sb
, -1, &status
);
989 TEST_SUCCESS(status
);
990 isExpensive
= utext_isLengthExpensive(utb
);
991 TEST_ASSERT(isExpensive
== TRUE
);
992 int64_t len
= utext_nativeLength(utb
);
993 TEST_ASSERT(len
== 99);
994 isExpensive
= utext_isLengthExpensive(utb
);
995 TEST_ASSERT(isExpensive
== FALSE
);
1000 // Index to positions not on code point boundaries.
1003 const char *u8str
= "\xc8\x81\xe1\x82\x83\xf1\x84\x85\x86";
1004 int32_t startMap
[] = { 0, 0, 2, 2, 2, 5, 5, 5, 5, 9, 9};
1005 int32_t nextMap
[] = { 2, 2, 5, 5, 5, 9, 9, 9, 9, 9, 9};
1006 int32_t prevMap
[] = { 0, 0, 0, 0, 0, 2, 2, 2, 2, 5, 5};
1007 UChar32 c32Map
[] = {0x201, 0x201, 0x1083, 0x1083, 0x1083, 0x044146, 0x044146, 0x044146, 0x044146, -1, -1};
1008 UChar32 pr32Map
[] = { -1, -1, 0x201, 0x201, 0x201, 0x1083, 0x1083, 0x1083, 0x1083, 0x044146, 0x044146};
1010 // extractLen is the size, in UChars, of what will be extracted between index and index+1.
1011 // is zero when both index positions lie within the same code point.
1012 int32_t exLen
[] = { 0, 1, 0, 0, 1, 0, 0, 0, 2, 0, 0};
1015 UErrorCode status
= U_ZERO_ERROR
;
1016 UText
*ut
= utext_openUTF8(NULL
, u8str
, -1, &status
);
1017 TEST_SUCCESS(status
);
1021 int32_t startMapLimit
= UPRV_LENGTHOF(startMap
);
1022 for (i
=0; i
<startMapLimit
; i
++) {
1023 utext_setNativeIndex(ut
, i
);
1024 int64_t cpIndex
= utext_getNativeIndex(ut
);
1025 TEST_ASSERT(cpIndex
== startMap
[i
]);
1026 cpIndex
= UTEXT_GETNATIVEINDEX(ut
);
1027 TEST_ASSERT(cpIndex
== startMap
[i
]);
1031 for (i
=0; i
<startMapLimit
; i
++) {
1032 UChar32 c32
= utext_char32At(ut
, i
);
1033 TEST_ASSERT(c32
== c32Map
[i
]);
1034 int64_t cpIndex
= utext_getNativeIndex(ut
);
1035 TEST_ASSERT(cpIndex
== startMap
[i
]);
1038 // Check utext_next32From
1039 for (i
=0; i
<startMapLimit
; i
++) {
1040 UChar32 c32
= utext_next32From(ut
, i
);
1041 TEST_ASSERT(c32
== c32Map
[i
]);
1042 int64_t cpIndex
= utext_getNativeIndex(ut
);
1043 TEST_ASSERT(cpIndex
== nextMap
[i
]);
1046 // check utext_previous32From
1047 for (i
=0; i
<startMapLimit
; i
++) {
1049 UChar32 c32
= utext_previous32From(ut
, i
);
1050 TEST_ASSERT(c32
== pr32Map
[i
]);
1051 int64_t cpIndex
= utext_getNativeIndex(ut
);
1052 TEST_ASSERT(cpIndex
== prevMap
[i
]);
1056 // Extract from i to i+1, which may be zero or one code points,
1057 // depending on whether the indices straddle a cp boundary.
1058 for (i
=0; i
<startMapLimit
; i
++) {
1060 status
= U_ZERO_ERROR
;
1061 int32_t extractedLen
= utext_extract(ut
, i
, i
+1, buf
, 3, &status
);
1062 TEST_SUCCESS(status
);
1063 TEST_ASSERT(extractedLen
== exLen
[i
]);
1064 if (extractedLen
> 0) {
1066 /* extractedLen-extractedLen == 0 is used to get around a compiler warning. */
1067 U16_GET(buf
, 0, extractedLen
-extractedLen
, extractedLen
, c32
);
1068 TEST_ASSERT(c32
== c32Map
[i
]);
1076 { // Similar test, with utf16 instead of utf8
1077 // TODO: merge the common parts of these tests.
1079 UnicodeString
u16str("\\u1000\\U00011000\\u2000\\U00022000", -1, US_INV
);
1080 int32_t startMap
[] ={ 0, 1, 1, 3, 4, 4, 6, 6};
1081 int32_t nextMap
[] = { 1, 3, 3, 4, 6, 6, 6, 6};
1082 int32_t prevMap
[] = { 0, 0, 0, 1, 3, 3, 4, 4};
1083 UChar32 c32Map
[] = {0x1000, 0x11000, 0x11000, 0x2000, 0x22000, 0x22000, -1, -1};
1084 UChar32 pr32Map
[] = { -1, 0x1000, 0x1000, 0x11000, 0x2000, 0x2000, 0x22000, 0x22000};
1085 int32_t exLen
[] = { 1, 0, 2, 1, 0, 2, 0, 0,};
1087 u16str
= u16str
.unescape();
1088 UErrorCode status
= U_ZERO_ERROR
;
1089 UText
*ut
= utext_openUnicodeString(NULL
, &u16str
, &status
);
1090 TEST_SUCCESS(status
);
1092 int32_t startMapLimit
= UPRV_LENGTHOF(startMap
);
1094 for (i
=0; i
<startMapLimit
; i
++) {
1095 utext_setNativeIndex(ut
, i
);
1096 int64_t cpIndex
= utext_getNativeIndex(ut
);
1097 TEST_ASSERT(cpIndex
== startMap
[i
]);
1101 for (i
=0; i
<startMapLimit
; i
++) {
1102 UChar32 c32
= utext_char32At(ut
, i
);
1103 TEST_ASSERT(c32
== c32Map
[i
]);
1104 int64_t cpIndex
= utext_getNativeIndex(ut
);
1105 TEST_ASSERT(cpIndex
== startMap
[i
]);
1108 // Check utext_next32From
1109 for (i
=0; i
<startMapLimit
; i
++) {
1110 UChar32 c32
= utext_next32From(ut
, i
);
1111 TEST_ASSERT(c32
== c32Map
[i
]);
1112 int64_t cpIndex
= utext_getNativeIndex(ut
);
1113 TEST_ASSERT(cpIndex
== nextMap
[i
]);
1116 // check utext_previous32From
1117 for (i
=0; i
<startMapLimit
; i
++) {
1118 UChar32 c32
= utext_previous32From(ut
, i
);
1119 TEST_ASSERT(c32
== pr32Map
[i
]);
1120 int64_t cpIndex
= utext_getNativeIndex(ut
);
1121 TEST_ASSERT(cpIndex
== prevMap
[i
]);
1125 // Extract from i to i+1, which may be zero or one code points,
1126 // depending on whether the indices straddle a cp boundary.
1127 for (i
=0; i
<startMapLimit
; i
++) {
1129 status
= U_ZERO_ERROR
;
1130 int32_t extractedLen
= utext_extract(ut
, i
, i
+1, buf
, 3, &status
);
1131 TEST_SUCCESS(status
);
1132 TEST_ASSERT(extractedLen
== exLen
[i
]);
1133 if (extractedLen
> 0) {
1135 /* extractedLen-extractedLen == 0 is used to get around a compiler warning. */
1136 U16_GET(buf
, 0, extractedLen
-extractedLen
, extractedLen
, c32
);
1137 TEST_ASSERT(c32
== c32Map
[i
]);
1144 { // Similar test, with UText over Replaceable
1145 // TODO: merge the common parts of these tests.
1147 UnicodeString
u16str("\\u1000\\U00011000\\u2000\\U00022000", -1, US_INV
);
1148 int32_t startMap
[] ={ 0, 1, 1, 3, 4, 4, 6, 6};
1149 int32_t nextMap
[] = { 1, 3, 3, 4, 6, 6, 6, 6};
1150 int32_t prevMap
[] = { 0, 0, 0, 1, 3, 3, 4, 4};
1151 UChar32 c32Map
[] = {0x1000, 0x11000, 0x11000, 0x2000, 0x22000, 0x22000, -1, -1};
1152 UChar32 pr32Map
[] = { -1, 0x1000, 0x1000, 0x11000, 0x2000, 0x2000, 0x22000, 0x22000};
1153 int32_t exLen
[] = { 1, 0, 2, 1, 0, 2, 0, 0,};
1155 u16str
= u16str
.unescape();
1156 UErrorCode status
= U_ZERO_ERROR
;
1157 UText
*ut
= utext_openReplaceable(NULL
, &u16str
, &status
);
1158 TEST_SUCCESS(status
);
1160 int32_t startMapLimit
= UPRV_LENGTHOF(startMap
);
1162 for (i
=0; i
<startMapLimit
; i
++) {
1163 utext_setNativeIndex(ut
, i
);
1164 int64_t cpIndex
= utext_getNativeIndex(ut
);
1165 TEST_ASSERT(cpIndex
== startMap
[i
]);
1169 for (i
=0; i
<startMapLimit
; i
++) {
1170 UChar32 c32
= utext_char32At(ut
, i
);
1171 TEST_ASSERT(c32
== c32Map
[i
]);
1172 int64_t cpIndex
= utext_getNativeIndex(ut
);
1173 TEST_ASSERT(cpIndex
== startMap
[i
]);
1176 // Check utext_next32From
1177 for (i
=0; i
<startMapLimit
; i
++) {
1178 UChar32 c32
= utext_next32From(ut
, i
);
1179 TEST_ASSERT(c32
== c32Map
[i
]);
1180 int64_t cpIndex
= utext_getNativeIndex(ut
);
1181 TEST_ASSERT(cpIndex
== nextMap
[i
]);
1184 // check utext_previous32From
1185 for (i
=0; i
<startMapLimit
; i
++) {
1186 UChar32 c32
= utext_previous32From(ut
, i
);
1187 TEST_ASSERT(c32
== pr32Map
[i
]);
1188 int64_t cpIndex
= utext_getNativeIndex(ut
);
1189 TEST_ASSERT(cpIndex
== prevMap
[i
]);
1193 // Extract from i to i+1, which may be zero or one code points,
1194 // depending on whether the indices straddle a cp boundary.
1195 for (i
=0; i
<startMapLimit
; i
++) {
1197 status
= U_ZERO_ERROR
;
1198 int32_t extractedLen
= utext_extract(ut
, i
, i
+1, buf
, 3, &status
);
1199 TEST_SUCCESS(status
);
1200 TEST_ASSERT(extractedLen
== exLen
[i
]);
1201 if (extractedLen
> 0) {
1203 /* extractedLen-extractedLen == 0 is used to get around a compiler warning. */
1204 U16_GET(buf
, 0, extractedLen
-extractedLen
, extractedLen
, c32
);
1205 TEST_ASSERT(c32
== c32Map
[i
]);
1214 void UTextTest::FreezeTest() {
1215 // Check isWritable() and freeze() behavior.
1218 UnicodeString
ustr("Hello, World.");
1219 const char u8str
[] = {char(0x31), (char)0x32, (char)0x33, 0};
1220 const UChar u16str
[] = {(UChar
)0x31, (UChar
)0x32, (UChar
)0x44, 0};
1222 UErrorCode status
= U_ZERO_ERROR
;
1226 ut
= utext_openUTF8(ut
, u8str
, -1, &status
);
1227 TEST_SUCCESS(status
);
1228 UBool writable
= utext_isWritable(ut
);
1229 TEST_ASSERT(writable
== FALSE
);
1230 utext_copy(ut
, 1, 2, 0, TRUE
, &status
);
1231 TEST_ASSERT(status
== U_NO_WRITE_PERMISSION
);
1233 status
= U_ZERO_ERROR
;
1234 ut
= utext_openUChars(ut
, u16str
, -1, &status
);
1235 TEST_SUCCESS(status
);
1236 writable
= utext_isWritable(ut
);
1237 TEST_ASSERT(writable
== FALSE
);
1238 utext_copy(ut
, 1, 2, 0, TRUE
, &status
);
1239 TEST_ASSERT(status
== U_NO_WRITE_PERMISSION
);
1241 status
= U_ZERO_ERROR
;
1242 ut
= utext_openUnicodeString(ut
, &ustr
, &status
);
1243 TEST_SUCCESS(status
);
1244 writable
= utext_isWritable(ut
);
1245 TEST_ASSERT(writable
== TRUE
);
1247 writable
= utext_isWritable(ut
);
1248 TEST_ASSERT(writable
== FALSE
);
1249 utext_copy(ut
, 1, 2, 0, TRUE
, &status
);
1250 TEST_ASSERT(status
== U_NO_WRITE_PERMISSION
);
1252 status
= U_ZERO_ERROR
;
1253 ut
= utext_openUnicodeString(ut
, &ustr
, &status
);
1254 TEST_SUCCESS(status
);
1255 ut2
= utext_clone(ut2
, ut
, FALSE
, FALSE
, &status
); // clone with readonly = false
1256 TEST_SUCCESS(status
);
1257 writable
= utext_isWritable(ut2
);
1258 TEST_ASSERT(writable
== TRUE
);
1259 ut2
= utext_clone(ut2
, ut
, FALSE
, TRUE
, &status
); // clone with readonly = true
1260 TEST_SUCCESS(status
);
1261 writable
= utext_isWritable(ut2
);
1262 TEST_ASSERT(writable
== FALSE
);
1263 utext_copy(ut2
, 1, 2, 0, TRUE
, &status
);
1264 TEST_ASSERT(status
== U_NO_WRITE_PERMISSION
);
1266 status
= U_ZERO_ERROR
;
1267 ut
= utext_openConstUnicodeString(ut
, (const UnicodeString
*)&ustr
, &status
);
1268 TEST_SUCCESS(status
);
1269 writable
= utext_isWritable(ut
);
1270 TEST_ASSERT(writable
== FALSE
);
1271 utext_copy(ut
, 1, 2, 0, TRUE
, &status
);
1272 TEST_ASSERT(status
== U_NO_WRITE_PERMISSION
);
1274 // Deep Clone of a frozen UText should re-enable writing in the copy.
1275 status
= U_ZERO_ERROR
;
1276 ut
= utext_openUnicodeString(ut
, &ustr
, &status
);
1277 TEST_SUCCESS(status
);
1279 ut2
= utext_clone(ut2
, ut
, TRUE
, FALSE
, &status
); // deep clone
1280 TEST_SUCCESS(status
);
1281 writable
= utext_isWritable(ut2
);
1282 TEST_ASSERT(writable
== TRUE
);
1285 // Deep clone of a frozen UText, where the base type is intrinsically non-writable,
1286 // should NOT enable writing in the copy.
1287 status
= U_ZERO_ERROR
;
1288 ut
= utext_openUChars(ut
, u16str
, -1, &status
);
1289 TEST_SUCCESS(status
);
1291 ut2
= utext_clone(ut2
, ut
, TRUE
, FALSE
, &status
); // deep clone
1292 TEST_SUCCESS(status
);
1293 writable
= utext_isWritable(ut2
);
1294 TEST_ASSERT(writable
== FALSE
);
1304 // A UText type that works with a chunk size of 1.
1305 // Intended to test for edge cases.
1306 // Input comes from a UnicodeString.
1308 // ut.b the character. Put into both halves.
1312 static UBool U_CALLCONV
1313 fragTextAccess(UText
*ut
, int64_t index
, UBool forward
) {
1314 const UnicodeString
*us
= (const UnicodeString
*)ut
->context
;
1316 int32_t length
= us
->length();
1317 if (forward
&& index
>=0 && index
<length
) {
1318 c
= us
->charAt((int32_t)index
);
1320 ut
->chunkOffset
= 0;
1321 ut
->chunkLength
= 1;
1322 ut
->chunkNativeStart
= index
;
1323 ut
->chunkNativeLimit
= index
+1;
1326 if (!forward
&& index
>0 && index
<=length
) {
1327 c
= us
->charAt((int32_t)index
-1);
1329 ut
->chunkOffset
= 1;
1330 ut
->chunkLength
= 1;
1331 ut
->chunkNativeStart
= index
-1;
1332 ut
->chunkNativeLimit
= index
;
1336 ut
->chunkOffset
= 0;
1337 ut
->chunkLength
= 0;
1339 ut
->chunkNativeStart
= 0;
1340 ut
->chunkNativeLimit
= 0;
1342 ut
->chunkNativeStart
= length
;
1343 ut
->chunkNativeLimit
= length
;
1348 // Function table to be used with this fragmented text provider.
1349 // Initialized in the open function.
1350 static UTextFuncs fragmentFuncs
;
1352 // Clone function for fragmented text provider.
1353 // Didn't really want to provide this, but it's easier to provide it than to keep it
1354 // out of the tests.
1357 cloneFragmentedUnicodeString(UText
*dest
, const UText
*src
, UBool deep
, UErrorCode
*status
) {
1358 if (U_FAILURE(*status
)) {
1362 *status
= U_UNSUPPORTED_ERROR
;
1365 dest
= utext_openUnicodeString(dest
, (UnicodeString
*)src
->context
, status
);
1366 utext_setNativeIndex(dest
, utext_getNativeIndex(src
));
1372 // Open function for the fragmented text provider.
1374 openFragmentedUnicodeString(UText
*ut
, UnicodeString
*s
, UErrorCode
*status
) {
1375 ut
= utext_openUnicodeString(ut
, s
, status
);
1376 if (U_FAILURE(*status
)) {
1380 // Copy of the function table from the stock UnicodeString UText,
1381 // and replace the entry for the access function.
1382 memcpy(&fragmentFuncs
, ut
->pFuncs
, sizeof(fragmentFuncs
));
1383 fragmentFuncs
.access
= fragTextAccess
;
1384 fragmentFuncs
.clone
= cloneFragmentedUnicodeString
;
1385 ut
->pFuncs
= &fragmentFuncs
;
1387 ut
->chunkContents
= (UChar
*)&ut
->b
;
1388 ut
->pFuncs
->access(ut
, 0, TRUE
);
1392 // Regression test for Ticket 5560
1393 // Clone fails to update chunkContentPointer in the cloned copy.
1394 // This is only an issue for UText types that work in a local buffer,
1395 // (UTF-8 wrapper, for example)
1398 // 1. Create an inital UText
1399 // 2. Deep clone it. Contents should match original.
1400 // 3. Reset original to something different.
1401 // 4. Check that clone contents did not change.
1403 void UTextTest::Ticket5560() {
1404 /* The following two strings are in UTF-8 even on EBCDIC platforms. */
1405 static const char s1
[] = {0x41,0x42,0x43,0x44,0x45,0x46,0}; /* "ABCDEF" */
1406 static const char s2
[] = {0x31,0x32,0x33,0x34,0x35,0x36,0}; /* "123456" */
1407 UErrorCode status
= U_ZERO_ERROR
;
1409 UText ut1
= UTEXT_INITIALIZER
;
1410 UText ut2
= UTEXT_INITIALIZER
;
1412 utext_openUTF8(&ut1
, s1
, -1, &status
);
1413 UChar c
= utext_next32(&ut1
);
1414 TEST_ASSERT(c
== 0x41); // c == 'A'
1416 utext_clone(&ut2
, &ut1
, TRUE
, FALSE
, &status
);
1417 TEST_SUCCESS(status
);
1418 c
= utext_next32(&ut2
);
1419 TEST_ASSERT(c
== 0x42); // c == 'B'
1420 c
= utext_next32(&ut1
);
1421 TEST_ASSERT(c
== 0x42); // c == 'B'
1423 utext_openUTF8(&ut1
, s2
, -1, &status
);
1424 c
= utext_next32(&ut1
);
1425 TEST_ASSERT(c
== 0x31); // c == '1'
1426 c
= utext_next32(&ut2
);
1427 TEST_ASSERT(c
== 0x43); // c == 'C'
1434 // Test for Ticket 6847
1436 void UTextTest::Ticket6847() {
1437 const int STRLEN
= 90;
1439 u_memset(s
, 0x41, STRLEN
);
1442 UErrorCode status
= U_ZERO_ERROR
;
1443 UText
*ut
= utext_openUChars(NULL
, s
, -1, &status
);
1445 utext_setNativeIndex(ut
, 0);
1448 int64_t nativeIndex
= UTEXT_GETNATIVEINDEX(ut
);
1449 TEST_ASSERT(nativeIndex
== 0);
1450 while ((c
= utext_next32(ut
)) != U_SENTINEL
) {
1451 TEST_ASSERT(c
== 0x41);
1452 TEST_ASSERT(count
< STRLEN
);
1453 if (count
>= STRLEN
) {
1457 nativeIndex
= UTEXT_GETNATIVEINDEX(ut
);
1458 TEST_ASSERT(nativeIndex
== count
);
1460 TEST_ASSERT(count
== STRLEN
);
1461 nativeIndex
= UTEXT_GETNATIVEINDEX(ut
);
1462 TEST_ASSERT(nativeIndex
== STRLEN
);
1467 void UTextTest::Ticket10562() {
1468 // Note: failures show as a heap error when the test is run under valgrind.
1469 UErrorCode status
= U_ZERO_ERROR
;
1471 const char *utf8_string
= "\x41\x41\x41\x41\x41\x41\x41\x41\x41\x41\x41\x41\x41\x41\x41";
1472 UText
*utf8Text
= utext_openUTF8(NULL
, utf8_string
, -1, &status
);
1473 TEST_SUCCESS(status
);
1474 UText
*deepClone
= utext_clone(NULL
, utf8Text
, TRUE
, FALSE
, &status
);
1475 TEST_SUCCESS(status
);
1476 UText
*shallowClone
= utext_clone(NULL
, deepClone
, FALSE
, FALSE
, &status
);
1477 TEST_SUCCESS(status
);
1478 utext_close(shallowClone
);
1479 utext_close(deepClone
);
1480 utext_close(utf8Text
);
1482 status
= U_ZERO_ERROR
;
1483 UnicodeString
usString("Hello, World.");
1484 UText
*usText
= utext_openUnicodeString(NULL
, &usString
, &status
);
1485 TEST_SUCCESS(status
);
1486 UText
*usDeepClone
= utext_clone(NULL
, usText
, TRUE
, FALSE
, &status
);
1487 TEST_SUCCESS(status
);
1488 UText
*usShallowClone
= utext_clone(NULL
, usDeepClone
, FALSE
, FALSE
, &status
);
1489 TEST_SUCCESS(status
);
1490 utext_close(usShallowClone
);
1491 utext_close(usDeepClone
);
1492 utext_close(usText
);
1496 void UTextTest::Ticket10983() {
1497 // Note: failure shows as a seg fault when the defect is present.
1499 UErrorCode status
= U_ZERO_ERROR
;
1500 UnicodeString
s("Hello, World");
1501 UText
*ut
= utext_openConstUnicodeString(NULL
, &s
, &status
);
1502 TEST_SUCCESS(status
);
1504 status
= U_INVALID_STATE_ERROR
;
1505 UText
*cloned
= utext_clone(NULL
, ut
, TRUE
, TRUE
, &status
);
1506 TEST_ASSERT(cloned
== NULL
);
1507 TEST_ASSERT(status
== U_INVALID_STATE_ERROR
);
1512 // Ticket 12130 - extract on a UText wrapping a null terminated UChar * string
1513 // leaves the iteration position set incorrectly when the
1514 // actual string length is not yet known.
1516 // The test text needs to be long enough that UText defers getting the length.
1518 void UTextTest::Ticket12130() {
1519 UErrorCode status
= U_ZERO_ERROR
;
1522 "Fundamentally, computers just deal with numbers. They store letters and other characters "
1523 "by assigning a number for each one. Before Unicode was invented, there were hundreds "
1524 "of different encoding systems for assigning these numbers. No single encoding could "
1525 "contain enough characters: for example, the European Union alone requires several "
1526 "different encodings to cover all its languages. Even for a single language like "
1527 "English no single encoding was adequate for all the letters, punctuation, and technical "
1528 "symbols in common use.";
1530 UnicodeString
str(text8
);
1531 const UChar
*ustr
= str
.getTerminatedBuffer();
1532 UText ut
= UTEXT_INITIALIZER
;
1533 utext_openUChars(&ut
, ustr
, -1, &status
);
1534 UChar extractBuffer
[50];
1536 for (int32_t startIdx
= 0; startIdx
<str
.length(); ++startIdx
) {
1537 int32_t endIdx
= startIdx
+ 20;
1539 u_memset(extractBuffer
, 0, UPRV_LENGTHOF(extractBuffer
));
1540 utext_extract(&ut
, startIdx
, endIdx
, extractBuffer
, UPRV_LENGTHOF(extractBuffer
), &status
);
1541 if (U_FAILURE(status
)) {
1542 errln("%s:%d %s", __FILE__
, __LINE__
, u_errorName(status
));
1545 int64_t ni
= utext_getNativeIndex(&ut
);
1546 int64_t expectedni
= startIdx
+ 20;
1547 if (expectedni
> str
.length()) {
1548 expectedni
= str
.length();
1550 if (expectedni
!= ni
) {
1551 errln("%s:%d utext_getNativeIndex() expected %d, got %d", __FILE__
, __LINE__
, expectedni
, ni
);
1553 if (0 != str
.tempSubString(startIdx
, 20).compare(extractBuffer
)) {
1554 errln("%s:%d utext_extract() failed. expected \"%s\", got \"%s\"",
1555 __FILE__
, __LINE__
, CStr(str
.tempSubString(startIdx
, 20))(), CStr(UnicodeString(extractBuffer
))());
1560 // Similar utext extract, this time with the string length provided to the UText in advance,
1561 // and a buffer of larger than required capacity.
1563 utext_openUChars(&ut
, ustr
, str
.length(), &status
);
1564 for (int32_t startIdx
= 0; startIdx
<str
.length(); ++startIdx
) {
1565 int32_t endIdx
= startIdx
+ 20;
1566 u_memset(extractBuffer
, 0, UPRV_LENGTHOF(extractBuffer
));
1567 utext_extract(&ut
, startIdx
, endIdx
, extractBuffer
, UPRV_LENGTHOF(extractBuffer
), &status
);
1568 if (U_FAILURE(status
)) {
1569 errln("%s:%d %s", __FILE__
, __LINE__
, u_errorName(status
));
1572 int64_t ni
= utext_getNativeIndex(&ut
);
1573 int64_t expectedni
= startIdx
+ 20;
1574 if (expectedni
> str
.length()) {
1575 expectedni
= str
.length();
1577 if (expectedni
!= ni
) {
1578 errln("%s:%d utext_getNativeIndex() expected %d, got %d", __FILE__
, __LINE__
, expectedni
, ni
);
1580 if (0 != str
.tempSubString(startIdx
, 20).compare(extractBuffer
)) {
1581 errln("%s:%d utext_extract() failed. expected \"%s\", got \"%s\"",
1582 __FILE__
, __LINE__
, CStr(str
.tempSubString(startIdx
, 20))(), CStr(UnicodeString(extractBuffer
))());
1588 // Ticket 13344 The macro form of UTEXT_SETNATIVEINDEX failed when target was a trail surrogate
1589 // of a supplementary character.
1591 void UTextTest::Ticket13344() {
1592 UErrorCode status
= U_ZERO_ERROR
;
1593 const char16_t *str
= u
"abc\U0010abcd xyz";
1594 LocalUTextPointer
ut(utext_openUChars(NULL
, str
, -1, &status
));
1596 assertSuccess("UTextTest::Ticket13344-status", status
);
1597 UTEXT_SETNATIVEINDEX(ut
.getAlias(), 3);
1598 assertEquals("UTextTest::Ticket13344-lead", (int64_t)3, utext_getNativeIndex(ut
.getAlias()));
1599 UTEXT_SETNATIVEINDEX(ut
.getAlias(), 4);
1600 assertEquals("UTextTest::Ticket13344-trail", (int64_t)3, utext_getNativeIndex(ut
.getAlias()));
1601 UTEXT_SETNATIVEINDEX(ut
.getAlias(), 5);
1602 assertEquals("UTextTest::Ticket13344-bmp", (int64_t)5, utext_getNativeIndex(ut
.getAlias()));
1604 utext_setNativeIndex(ut
.getAlias(), 3);
1605 assertEquals("UTextTest::Ticket13344-lead-2", (int64_t)3, utext_getNativeIndex(ut
.getAlias()));
1606 utext_setNativeIndex(ut
.getAlias(), 4);
1607 assertEquals("UTextTest::Ticket13344-trail-2", (int64_t)3, utext_getNativeIndex(ut
.getAlias()));
1608 utext_setNativeIndex(ut
.getAlias(), 5);
1609 assertEquals("UTextTest::Ticket13344-bmp-2", (int64_t)5, utext_getNativeIndex(ut
.getAlias()));