1 /********************************************************************
3 * Copyright (c) 2005-2014, 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 case 6: name
= "Ticket10983";
63 if (exec
) Ticket10983(); break;
64 default: name
= ""; break;
69 // Quick and dirty random number generator.
70 // (don't use library so that results are portable.
71 static uint32_t m_seed
= 1;
72 static uint32_t m_rand()
74 m_seed
= m_seed
* 1103515245 + 12345;
75 return (uint32_t)(m_seed
/65536) % 32768;
82 // Top Level function for UText testing.
83 // Specifies the strings to be tested, with the acutal testing itself
84 // being carried out in another function, TestString().
86 void UTextTest::TextTest() {
89 TestString("abcd\\U00010001xyz");
92 // Supplementary chars at start or end
93 TestString("\\U00010001");
94 TestString("abc\\U00010001");
95 TestString("\\U00010001abc");
97 // Test simple strings of lengths 1 to 60, looking for glitches at buffer boundaries
99 for (i
=1; i
<60; i
++) {
101 for (j
=0; j
<i
; j
++) {
102 if (j
+0x30 == 0x5c) {
103 // backslash. Needs to be escaped
104 s
.append((UChar
)0x5c);
106 s
.append(UChar(j
+0x30));
111 // Test strings with odd-aligned supplementary chars,
112 // looking for glitches at buffer boundaries
113 for (i
=1; i
<60; i
++) {
115 s
.append((UChar
)0x41);
116 for (j
=0; j
<i
; j
++) {
117 s
.append(UChar32(j
+0x11000));
122 // String of chars of randomly varying size in utf-8 representation.
123 // Exercise the mapping, and the varying sized buffer.
129 UChar32 c4
= 0x11000;
130 for (i
=0; i
<1000; i
++) {
131 int len8
= m_rand()%4
+ 1;
135 // don't put 0 into string (0 terminated strings for some tests)
136 // don't put '\', will cause unescape() to fail.
137 if (c1
==0x5c || c1
==0) {
158 // TestString() Run a suite of UText tests on a string.
159 // The test string is unescaped before use.
161 void UTextTest::TestString(const UnicodeString
&s
) {
166 UErrorCode status
= U_ZERO_ERROR
;
170 UnicodeString sa
= s
.unescape();
174 // Build up a mapping between code points and UTF-16 code unit indexes.
176 m
*cpMap
= new m
[sa
.length() + 1];
178 for (i
=0; i
<sa
.length(); i
=sa
.moveIndex32(i
, 1)) {
180 cpMap
[j
].nativeIdx
= i
;
185 cpMap
[j
].nativeIdx
= i
; // position following the last char in utf-16 string.
188 // UChar * test, null terminated
189 status
= U_ZERO_ERROR
;
190 UChar
*buf
= new UChar
[saLen
+1];
191 sa
.extract(buf
, saLen
+1, status
);
192 TEST_SUCCESS(status
);
193 ut
= utext_openUChars(NULL
, buf
, -1, &status
);
194 TEST_SUCCESS(status
);
195 TestAccess(sa
, ut
, cpCount
, cpMap
);
199 // UChar * test, with length
200 status
= U_ZERO_ERROR
;
201 buf
= new UChar
[saLen
+1];
202 sa
.extract(buf
, saLen
+1, status
);
203 TEST_SUCCESS(status
);
204 ut
= utext_openUChars(NULL
, buf
, saLen
, &status
);
205 TEST_SUCCESS(status
);
206 TestAccess(sa
, ut
, cpCount
, cpMap
);
211 // UnicodeString test
212 status
= U_ZERO_ERROR
;
213 ut
= utext_openUnicodeString(NULL
, &sa
, &status
);
214 TEST_SUCCESS(status
);
215 TestAccess(sa
, ut
, cpCount
, cpMap
);
216 TestCMR(sa
, ut
, cpCount
, cpMap
, cpMap
);
220 // Const UnicodeString test
221 status
= U_ZERO_ERROR
;
222 ut
= utext_openConstUnicodeString(NULL
, &sa
, &status
);
223 TEST_SUCCESS(status
);
224 TestAccess(sa
, ut
, cpCount
, cpMap
);
228 // Replaceable test. (UnicodeString inherits Replaceable)
229 status
= U_ZERO_ERROR
;
230 ut
= utext_openReplaceable(NULL
, &sa
, &status
);
231 TEST_SUCCESS(status
);
232 TestAccess(sa
, ut
, cpCount
, cpMap
);
233 TestCMR(sa
, ut
, cpCount
, cpMap
, cpMap
);
236 // Character Iterator Tests
237 status
= U_ZERO_ERROR
;
238 const UChar
*cbuf
= sa
.getBuffer();
239 CharacterIterator
*ci
= new UCharCharacterIterator(cbuf
, saLen
, status
);
240 TEST_SUCCESS(status
);
241 ut
= utext_openCharacterIterator(NULL
, ci
, &status
);
242 TEST_SUCCESS(status
);
243 TestAccess(sa
, ut
, cpCount
, cpMap
);
248 // Fragmented UnicodeString (Chunk size of one)
250 status
= U_ZERO_ERROR
;
251 ut
= openFragmentedUnicodeString(NULL
, &sa
, &status
);
252 TEST_SUCCESS(status
);
253 TestAccess(sa
, ut
, cpCount
, cpMap
);
260 // Convert the test string from UnicodeString to (char *) in utf-8 format
261 int32_t u8Len
= sa
.extract(0, sa
.length(), NULL
, 0, "utf-8");
262 char *u8String
= new char[u8Len
+ 1];
263 sa
.extract(0, sa
.length(), u8String
, u8Len
+1, "utf-8");
265 // Build up the map of code point indices in the utf-8 string
266 m
* u8Map
= new m
[sa
.length() + 1];
267 i
= 0; // native utf-8 index
268 for (j
=0; j
<cpCount
; j
++) { // code point number
269 u8Map
[j
].nativeIdx
= i
;
270 U8_NEXT(u8String
, i
, u8Len
, c
)
273 u8Map
[cpCount
].nativeIdx
= u8Len
; // position following the last char in utf-8 string.
275 // Do the test itself
276 status
= U_ZERO_ERROR
;
277 ut
= utext_openUTF8(NULL
, u8String
, -1, &status
);
278 TEST_SUCCESS(status
);
279 TestAccess(sa
, ut
, cpCount
, u8Map
);
289 // TestCMR test Copy, Move and Replace operations.
290 // us UnicodeString containing the test text.
291 // ut UText containing the same test text.
292 // cpCount number of code points in the test text.
293 // nativeMap Mapping from code points to native indexes for the UText.
294 // u16Map Mapping from code points to UTF-16 indexes, for use with the UnicodeString.
296 // This function runs a whole series of opertions on each incoming UText.
297 // The UText is deep-cloned prior to each operation, so that the original UText remains unchanged.
299 void UTextTest::TestCMR(const UnicodeString
&us
, UText
*ut
, int cpCount
, m
*nativeMap
, m
*u16Map
) {
300 TEST_ASSERT(utext_isWritable(ut
) == TRUE
);
302 int srcLengthType
; // Loop variables for selecting the postion and length
303 int srcPosType
; // of the block to operate on within the source text.
306 int srcIndex
= 0; // Code Point indexes of the block to operate on for
307 int srcLength
= 0; // a specific test.
309 int destIndex
= 0; // Code point index of the destination for a copy/move test.
311 int32_t nativeStart
= 0; // Native unit indexes for a test.
312 int32_t nativeLimit
= 0;
313 int32_t nativeDest
= 0;
315 int32_t u16Start
= 0; // UTF-16 indexes for a test.
316 int32_t u16Limit
= 0; // used when performing the same operation in a Unicode String
319 // Iterate over a whole series of source index, length and a target indexes.
320 // This is done with code point indexes; these will be later translated to native
321 // indexes using the cpMap.
322 for (srcLengthType
=1; srcLengthType
<=3; srcLengthType
++) {
323 switch (srcLengthType
) {
324 case 1: srcLength
= 1; break;
325 case 2: srcLength
= 5; break;
326 case 3: srcLength
= cpCount
/ 3;
328 for (srcPosType
=1; srcPosType
<=5; srcPosType
++) {
329 switch (srcPosType
) {
330 case 1: srcIndex
= 0; break;
331 case 2: srcIndex
= 1; break;
332 case 3: srcIndex
= cpCount
- srcLength
; break;
333 case 4: srcIndex
= cpCount
- srcLength
- 1; break;
334 case 5: srcIndex
= cpCount
/ 2; break;
336 if (srcIndex
< 0 || srcIndex
+ srcLength
> cpCount
) {
337 // filter out bogus test cases -
338 // those with a source range that falls of an edge of the string.
343 // Copy and move tests.
344 // iterate over a variety of destination positions.
346 for (destPosType
=1; destPosType
<=4; destPosType
++) {
347 switch (destPosType
) {
348 case 1: destIndex
= 0; break;
349 case 2: destIndex
= 1; break;
350 case 3: destIndex
= srcIndex
- 1; break;
351 case 4: destIndex
= srcIndex
+ srcLength
+ 1; break;
352 case 5: destIndex
= cpCount
-1; break;
353 case 6: destIndex
= cpCount
; break;
355 if (destIndex
<0 || destIndex
>cpCount
) {
356 // filter out bogus test cases.
360 nativeStart
= nativeMap
[srcIndex
].nativeIdx
;
361 nativeLimit
= nativeMap
[srcIndex
+srcLength
].nativeIdx
;
362 nativeDest
= nativeMap
[destIndex
].nativeIdx
;
364 u16Start
= u16Map
[srcIndex
].nativeIdx
;
365 u16Limit
= u16Map
[srcIndex
+srcLength
].nativeIdx
;
366 u16Dest
= u16Map
[destIndex
].nativeIdx
;
369 TestCopyMove(us
, ut
, FALSE
,
370 nativeStart
, nativeLimit
, nativeDest
,
371 u16Start
, u16Limit
, u16Dest
);
373 TestCopyMove(us
, ut
, TRUE
,
374 nativeStart
, nativeLimit
, nativeDest
,
375 u16Start
, u16Limit
, u16Dest
);
385 UnicodeString
fullRepString("This is an arbitrary string that will be used as replacement text");
386 for (int32_t replStrLen
=0; replStrLen
<20; replStrLen
++) {
387 UnicodeString
repStr(fullRepString
, 0, replStrLen
);
389 nativeStart
, nativeLimit
,
403 // TestCopyMove run a single test case for utext_copy.
404 // Test cases are created in TestCMR and dispatched here for execution.
406 void UTextTest::TestCopyMove(const UnicodeString
&us
, UText
*ut
, UBool move
,
407 int32_t nativeStart
, int32_t nativeLimit
, int32_t nativeDest
,
408 int32_t u16Start
, int32_t u16Limit
, int32_t u16Dest
)
410 UErrorCode status
= U_ZERO_ERROR
;
411 UText
*targetUT
= NULL
;
416 // clone the UText. The test will be run in the cloned copy
417 // so that we don't alter the original.
419 targetUT
= utext_clone(NULL
, ut
, TRUE
, FALSE
, &status
);
420 TEST_SUCCESS(status
);
421 UnicodeString
targetUS(us
); // And copy the reference string.
423 // do the test operation first in the reference
424 targetUS
.copy(u16Start
, u16Limit
, u16Dest
);
426 // delete out the source range.
427 if (u16Limit
< u16Dest
) {
428 targetUS
.removeBetween(u16Start
, u16Limit
);
430 int32_t amtCopied
= u16Limit
- u16Start
;
431 targetUS
.removeBetween(u16Start
+amtCopied
, u16Limit
+amtCopied
);
435 // Do the same operation in the UText under test
436 utext_copy(targetUT
, nativeStart
, nativeLimit
, nativeDest
, move
, &status
);
437 if (nativeDest
> nativeStart
&& nativeDest
< nativeLimit
) {
438 TEST_ASSERT(status
== U_INDEX_OUTOFBOUNDS_ERROR
);
440 TEST_SUCCESS(status
);
442 // Compare the results of the two parallel tests
443 int32_t usi
= 0; // UnicodeString postion, utf-16 index.
444 int64_t uti
= 0; // UText position, native index.
445 int32_t cpi
; // char32 position (code point index)
446 UChar32 usc
; // code point from Unicode String
447 UChar32 utc
; // code point from UText
448 utext_setNativeIndex(targetUT
, 0);
449 for (cpi
=0; ; cpi
++) {
450 usc
= targetUS
.char32At(usi
);
451 utc
= utext_next32(targetUT
);
455 TEST_ASSERT(uti
== usi
);
456 TEST_ASSERT(utc
== usc
);
457 usi
= targetUS
.moveIndex32(usi
, 1);
458 uti
= utext_getNativeIndex(targetUT
);
460 goto cleanupAndReturn
;
463 int64_t expectedNativeLength
= utext_nativeLength(ut
);
465 expectedNativeLength
+= nativeLimit
- nativeStart
;
467 uti
= utext_getNativeIndex(targetUT
);
468 TEST_ASSERT(uti
== expectedNativeLength
);
472 utext_close(targetUT
);
477 // TestReplace Test a single Replace operation.
479 void UTextTest::TestReplace(
480 const UnicodeString
&us
, // reference UnicodeString in which to do the replace
481 UText
*ut
, // UnicodeText object under test.
482 int32_t nativeStart
, // Range to be replaced, in UText native units.
484 int32_t u16Start
, // Range to be replaced, in UTF-16 units
485 int32_t u16Limit
, // for use in the reference UnicodeString.
486 const UnicodeString
&repStr
) // The replacement string
488 UErrorCode status
= U_ZERO_ERROR
;
489 UText
*targetUT
= NULL
;
494 // clone the target UText. The test will be run in the cloned copy
495 // so that we don't alter the original.
497 targetUT
= utext_clone(NULL
, ut
, TRUE
, FALSE
, &status
);
498 TEST_SUCCESS(status
);
499 UnicodeString
targetUS(us
); // And copy the reference string.
502 // Do the replace operation in the Unicode String, to
503 // produce a reference result.
505 targetUS
.replace(u16Start
, u16Limit
-u16Start
, repStr
);
508 // Do the replace on the UText under test
510 const UChar
*rs
= repStr
.getBuffer();
511 int32_t rsLen
= repStr
.length();
512 int32_t actualDelta
= utext_replace(targetUT
, nativeStart
, nativeLimit
, rs
, rsLen
, &status
);
513 int32_t expectedDelta
= repStr
.length() - (nativeLimit
- nativeStart
);
514 TEST_ASSERT(actualDelta
== expectedDelta
);
517 // Compare the results
519 int32_t usi
= 0; // UnicodeString postion, utf-16 index.
520 int64_t uti
= 0; // UText position, native index.
521 int32_t cpi
; // char32 position (code point index)
522 UChar32 usc
; // code point from Unicode String
523 UChar32 utc
; // code point from UText
524 int64_t expectedNativeLength
= 0;
525 utext_setNativeIndex(targetUT
, 0);
526 for (cpi
=0; ; cpi
++) {
527 usc
= targetUS
.char32At(usi
);
528 utc
= utext_next32(targetUT
);
532 TEST_ASSERT(uti
== usi
);
533 TEST_ASSERT(utc
== usc
);
534 usi
= targetUS
.moveIndex32(usi
, 1);
535 uti
= utext_getNativeIndex(targetUT
);
537 goto cleanupAndReturn
;
540 expectedNativeLength
= utext_nativeLength(ut
) + expectedDelta
;
541 uti
= utext_getNativeIndex(targetUT
);
542 TEST_ASSERT(uti
== expectedNativeLength
);
545 utext_close(targetUT
);
549 // TestAccess Test the read only access functions on a UText, including cloning.
550 // The text is accessed in a variety of ways, and compared with
551 // the reference UnicodeString.
553 void UTextTest::TestAccess(const UnicodeString
&us
, UText
*ut
, int cpCount
, m
*cpMap
) {
554 // Run the standard tests on the caller-supplied UText.
555 TestAccessNoClone(us
, ut
, cpCount
, cpMap
);
557 // Re-run tests on a shallow clone.
558 utext_setNativeIndex(ut
, 0);
559 UErrorCode status
= U_ZERO_ERROR
;
560 UText
*shallowClone
= utext_clone(NULL
, ut
, FALSE
/*deep*/, FALSE
/*readOnly*/, &status
);
561 TEST_SUCCESS(status
);
562 TestAccessNoClone(us
, shallowClone
, cpCount
, cpMap
);
565 // Rerun again on a deep clone.
566 // Note that text providers are not required to provide deep cloning,
567 // so unsupported errors are ignored.
569 status
= U_ZERO_ERROR
;
570 utext_setNativeIndex(shallowClone
, 0);
571 UText
*deepClone
= utext_clone(NULL
, shallowClone
, TRUE
, FALSE
, &status
);
572 utext_close(shallowClone
);
573 if (status
!= U_UNSUPPORTED_ERROR
) {
574 TEST_SUCCESS(status
);
575 TestAccessNoClone(us
, deepClone
, cpCount
, cpMap
);
577 utext_close(deepClone
);
582 // TestAccessNoClone() Test the read only access functions on a UText.
583 // The text is accessed in a variety of ways, and compared with
584 // the reference UnicodeString.
586 void UTextTest::TestAccessNoClone(const UnicodeString
&us
, UText
*ut
, int cpCount
, m
*cpMap
) {
587 UErrorCode status
= U_ZERO_ERROR
;
591 // Check the length from the UText
593 int64_t expectedLen
= cpMap
[cpCount
].nativeIdx
;
594 int64_t utlen
= utext_nativeLength(ut
);
595 TEST_ASSERT(expectedLen
== utlen
);
598 // Iterate forwards, verify that we get the correct code points
599 // at the correct native offsets.
603 int64_t expectedIndex
= 0;
604 int64_t foundIndex
= 0;
609 for (i
=0; i
<cpCount
; i
++) {
610 expectedIndex
= cpMap
[i
].nativeIdx
;
611 foundIndex
= utext_getNativeIndex(ut
);
612 TEST_ASSERT(expectedIndex
== foundIndex
);
613 expectedC
= cpMap
[i
].cp
;
614 foundC
= utext_next32(ut
);
615 TEST_ASSERT(expectedC
== foundC
);
616 foundIndex
= utext_getPreviousNativeIndex(ut
);
617 TEST_ASSERT(expectedIndex
== foundIndex
);
622 foundC
= utext_next32(ut
);
623 TEST_ASSERT(foundC
== U_SENTINEL
);
625 // Repeat above, using macros
626 utext_setNativeIndex(ut
, 0);
627 for (i
=0; i
<cpCount
; i
++) {
628 expectedIndex
= cpMap
[i
].nativeIdx
;
629 foundIndex
= UTEXT_GETNATIVEINDEX(ut
);
630 TEST_ASSERT(expectedIndex
== foundIndex
);
631 expectedC
= cpMap
[i
].cp
;
632 foundC
= UTEXT_NEXT32(ut
);
633 TEST_ASSERT(expectedC
== foundC
);
638 foundC
= UTEXT_NEXT32(ut
);
639 TEST_ASSERT(foundC
== U_SENTINEL
);
642 // Forward iteration (above) should have left index at the
643 // end of the input, which should == length().
645 len
= utext_nativeLength(ut
);
646 foundIndex
= utext_getNativeIndex(ut
);
647 TEST_ASSERT(len
== foundIndex
);
650 // Iterate backwards over entire test string
652 len
= utext_getNativeIndex(ut
);
653 utext_setNativeIndex(ut
, len
);
654 for (i
=cpCount
-1; i
>=0; i
--) {
655 expectedC
= cpMap
[i
].cp
;
656 expectedIndex
= cpMap
[i
].nativeIdx
;
657 int64_t prevIndex
= utext_getPreviousNativeIndex(ut
);
658 foundC
= utext_previous32(ut
);
659 foundIndex
= utext_getNativeIndex(ut
);
660 TEST_ASSERT(expectedIndex
== foundIndex
);
661 TEST_ASSERT(expectedC
== foundC
);
662 TEST_ASSERT(prevIndex
== foundIndex
);
669 // Backwards iteration, above, should have left our iterator
670 // position at zero, and continued backwards iterationshould fail.
672 foundIndex
= utext_getNativeIndex(ut
);
673 TEST_ASSERT(foundIndex
== 0);
674 foundIndex
= utext_getPreviousNativeIndex(ut
);
675 TEST_ASSERT(foundIndex
== 0);
678 foundC
= utext_previous32(ut
);
679 TEST_ASSERT(foundC
== U_SENTINEL
);
680 foundIndex
= utext_getNativeIndex(ut
);
681 TEST_ASSERT(foundIndex
== 0);
682 foundIndex
= utext_getPreviousNativeIndex(ut
);
683 TEST_ASSERT(foundIndex
== 0);
686 // And again, with the macros
687 utext_setNativeIndex(ut
, len
);
688 for (i
=cpCount
-1; i
>=0; i
--) {
689 expectedC
= cpMap
[i
].cp
;
690 expectedIndex
= cpMap
[i
].nativeIdx
;
691 foundC
= UTEXT_PREVIOUS32(ut
);
692 foundIndex
= UTEXT_GETNATIVEINDEX(ut
);
693 TEST_ASSERT(expectedIndex
== foundIndex
);
694 TEST_ASSERT(expectedC
== foundC
);
701 // Backwards iteration, above, should have left our iterator
702 // position at zero, and continued backwards iterationshould fail.
704 foundIndex
= UTEXT_GETNATIVEINDEX(ut
);
705 TEST_ASSERT(foundIndex
== 0);
707 foundC
= UTEXT_PREVIOUS32(ut
);
708 TEST_ASSERT(foundC
== U_SENTINEL
);
709 foundIndex
= UTEXT_GETNATIVEINDEX(ut
);
710 TEST_ASSERT(foundIndex
== 0);
716 // next32From(), prevous32From(), Iterate in a somewhat random order.
719 for (i
=0; i
<cpCount
; i
++) {
720 cpIndex
= (cpIndex
+ 9973) % cpCount
;
721 index
= cpMap
[cpIndex
].nativeIdx
;
722 expectedC
= cpMap
[cpIndex
].cp
;
723 foundC
= utext_next32From(ut
, index
);
724 TEST_ASSERT(expectedC
== foundC
);
731 for (i
=0; i
<cpCount
; i
++) {
732 cpIndex
= (cpIndex
+ 9973) % cpCount
;
733 index
= cpMap
[cpIndex
+1].nativeIdx
;
734 expectedC
= cpMap
[cpIndex
].cp
;
735 foundC
= utext_previous32From(ut
, index
);
736 TEST_ASSERT(expectedC
== foundC
);
744 // moveIndex(int32_t delta);
747 // Walk through frontwards, incrementing by one
748 utext_setNativeIndex(ut
, 0);
749 for (i
=1; i
<=cpCount
; i
++) {
750 utext_moveIndex32(ut
, 1);
751 index
= utext_getNativeIndex(ut
);
752 expectedIndex
= cpMap
[i
].nativeIdx
;
753 TEST_ASSERT(expectedIndex
== index
);
754 index
= UTEXT_GETNATIVEINDEX(ut
);
755 TEST_ASSERT(expectedIndex
== index
);
758 // Walk through frontwards, incrementing by two
759 utext_setNativeIndex(ut
, 0);
760 for (i
=2; i
<cpCount
; i
+=2) {
761 utext_moveIndex32(ut
, 2);
762 index
= utext_getNativeIndex(ut
);
763 expectedIndex
= cpMap
[i
].nativeIdx
;
764 TEST_ASSERT(expectedIndex
== index
);
765 index
= UTEXT_GETNATIVEINDEX(ut
);
766 TEST_ASSERT(expectedIndex
== index
);
769 // walk through the string backwards, decrementing by one.
770 i
= cpMap
[cpCount
].nativeIdx
;
771 utext_setNativeIndex(ut
, i
);
772 for (i
=cpCount
; i
>=0; i
--) {
773 expectedIndex
= cpMap
[i
].nativeIdx
;
774 index
= utext_getNativeIndex(ut
);
775 TEST_ASSERT(expectedIndex
== index
);
776 index
= UTEXT_GETNATIVEINDEX(ut
);
777 TEST_ASSERT(expectedIndex
== index
);
778 utext_moveIndex32(ut
, -1);
782 // walk through backwards, decrementing by three
783 i
= cpMap
[cpCount
].nativeIdx
;
784 utext_setNativeIndex(ut
, i
);
785 for (i
=cpCount
; i
>=0; i
-=3) {
786 expectedIndex
= cpMap
[i
].nativeIdx
;
787 index
= utext_getNativeIndex(ut
);
788 TEST_ASSERT(expectedIndex
== index
);
789 index
= UTEXT_GETNATIVEINDEX(ut
);
790 TEST_ASSERT(expectedIndex
== index
);
791 utext_moveIndex32(ut
, -3);
798 int bufSize
= us
.length() + 10;
799 UChar
*buf
= new UChar
[bufSize
];
800 status
= U_ZERO_ERROR
;
801 expectedLen
= us
.length();
802 len
= utext_extract(ut
, 0, utlen
, buf
, bufSize
, &status
);
803 TEST_SUCCESS(status
);
804 TEST_ASSERT(len
== expectedLen
);
805 int compareResult
= us
.compare(buf
, -1);
806 TEST_ASSERT(compareResult
== 0);
808 status
= U_ZERO_ERROR
;
809 len
= utext_extract(ut
, 0, utlen
, NULL
, 0, &status
);
811 TEST_ASSERT(status
== U_STRING_NOT_TERMINATED_WARNING
);
813 TEST_ASSERT(status
== U_BUFFER_OVERFLOW_ERROR
);
815 TEST_ASSERT(len
== expectedLen
);
817 status
= U_ZERO_ERROR
;
818 u_memset(buf
, 0x5555, bufSize
);
819 len
= utext_extract(ut
, 0, utlen
, buf
, 1, &status
);
820 if (us
.length() == 0) {
821 TEST_SUCCESS(status
);
822 TEST_ASSERT(buf
[0] == 0);
824 // Buf len == 1, extracting a single 16 bit value.
825 // If the data char is supplementary, it doesn't matter whether the buffer remains unchanged,
826 // or whether the lead surrogate of the pair is extracted.
827 // It's a buffer overflow error in either case.
828 TEST_ASSERT(buf
[0] == us
.charAt(0) ||
829 (buf
[0] == 0x5555 && U_IS_SUPPLEMENTARY(us
.char32At(0))));
830 TEST_ASSERT(buf
[1] == 0x5555);
831 if (us
.length() == 1) {
832 TEST_ASSERT(status
== U_STRING_NOT_TERMINATED_WARNING
);
834 TEST_ASSERT(status
== U_BUFFER_OVERFLOW_ERROR
);
842 // ErrorTest() Check various error and edge cases.
844 void UTextTest::ErrorTest()
846 // Close of an unitialized UText. Shouldn't blow up.
849 memset(&ut
, 0, sizeof(UText
));
854 // Double-close of a UText. Shouldn't blow up. UText should still be usable.
856 UErrorCode status
= U_ZERO_ERROR
;
857 UText ut
= UTEXT_INITIALIZER
;
858 UnicodeString
s("Hello, World");
859 UText
*ut2
= utext_openUnicodeString(&ut
, &s
, &status
);
860 TEST_SUCCESS(status
);
861 TEST_ASSERT(ut2
== &ut
);
863 UText
*ut3
= utext_close(&ut
);
864 TEST_ASSERT(ut3
== &ut
);
866 UText
*ut4
= utext_close(&ut
);
867 TEST_ASSERT(ut4
== &ut
);
869 utext_openUnicodeString(&ut
, &s
, &status
);
870 TEST_SUCCESS(status
);
874 // Re-use of a UText, chaining through each of the types of UText
875 // (If it doesn't blow up, and doesn't leak, it's probably working fine)
877 UErrorCode status
= U_ZERO_ERROR
;
878 UText ut
= UTEXT_INITIALIZER
;
880 UnicodeString
s1("Hello, World");
881 UChar s2
[] = {(UChar
)0x41, (UChar
)0x42, (UChar
)0};
882 const char *s3
= "\x66\x67\x68";
884 utp
= utext_openUnicodeString(&ut
, &s1
, &status
);
885 TEST_SUCCESS(status
);
886 TEST_ASSERT(utp
== &ut
);
888 utp
= utext_openConstUnicodeString(&ut
, &s1
, &status
);
889 TEST_SUCCESS(status
);
890 TEST_ASSERT(utp
== &ut
);
892 utp
= utext_openUTF8(&ut
, s3
, -1, &status
);
893 TEST_SUCCESS(status
);
894 TEST_ASSERT(utp
== &ut
);
896 utp
= utext_openUChars(&ut
, s2
, -1, &status
);
897 TEST_SUCCESS(status
);
898 TEST_ASSERT(utp
== &ut
);
900 utp
= utext_close(&ut
);
901 TEST_ASSERT(utp
== &ut
);
903 utp
= utext_openUnicodeString(&ut
, &s1
, &status
);
904 TEST_SUCCESS(status
);
905 TEST_ASSERT(utp
== &ut
);
908 // Invalid parameters on open
911 UErrorCode status
= U_ZERO_ERROR
;
912 UText ut
= UTEXT_INITIALIZER
;
914 utext_openUChars(&ut
, NULL
, 5, &status
);
915 TEST_ASSERT(status
== U_ILLEGAL_ARGUMENT_ERROR
);
917 status
= U_ZERO_ERROR
;
918 utext_openUChars(&ut
, NULL
, -1, &status
);
919 TEST_ASSERT(status
== U_ILLEGAL_ARGUMENT_ERROR
);
921 status
= U_ZERO_ERROR
;
922 utext_openUTF8(&ut
, NULL
, 4, &status
);
923 TEST_ASSERT(status
== U_ILLEGAL_ARGUMENT_ERROR
);
925 status
= U_ZERO_ERROR
;
926 utext_openUTF8(&ut
, NULL
, -1, &status
);
927 TEST_ASSERT(status
== U_ILLEGAL_ARGUMENT_ERROR
);
931 // UTF-8 with malformed sequences.
932 // These should come through as the Unicode replacement char, \ufffd
935 UErrorCode status
= U_ZERO_ERROR
;
937 const char *badUTF8
= "\x41\x81\x42\xf0\x81\x81\x43";
940 ut
= utext_openUTF8(NULL
, badUTF8
, -1, &status
);
941 TEST_SUCCESS(status
);
942 c
= utext_char32At(ut
, 1);
943 TEST_ASSERT(c
== 0xfffd);
944 c
= utext_char32At(ut
, 3);
945 TEST_ASSERT(c
== 0xfffd);
946 c
= utext_char32At(ut
, 5);
947 TEST_ASSERT(c
== 0xfffd);
948 c
= utext_char32At(ut
, 6);
949 TEST_ASSERT(c
== 0x43);
952 int n
= utext_extract(ut
, 0, 9, buf
, 10, &status
);
953 TEST_SUCCESS(status
);
955 TEST_ASSERT(buf
[1] == 0xfffd);
956 TEST_ASSERT(buf
[3] == 0xfffd);
957 TEST_ASSERT(buf
[2] == 0x42);
963 // isLengthExpensive - does it make the exptected transitions after
964 // getting the length of a nul terminated string?
967 UErrorCode status
= U_ZERO_ERROR
;
968 UnicodeString
sa("Hello, this is a string");
972 memset(sb
, 0x20, sizeof(sb
));
975 UText
*uta
= utext_openUnicodeString(NULL
, &sa
, &status
);
976 TEST_SUCCESS(status
);
977 isExpensive
= utext_isLengthExpensive(uta
);
978 TEST_ASSERT(isExpensive
== FALSE
);
981 UText
*utb
= utext_openUChars(NULL
, sb
, -1, &status
);
982 TEST_SUCCESS(status
);
983 isExpensive
= utext_isLengthExpensive(utb
);
984 TEST_ASSERT(isExpensive
== TRUE
);
985 int64_t len
= utext_nativeLength(utb
);
986 TEST_ASSERT(len
== 99);
987 isExpensive
= utext_isLengthExpensive(utb
);
988 TEST_ASSERT(isExpensive
== FALSE
);
993 // Index to positions not on code point boundaries.
996 const char *u8str
= "\xc8\x81\xe1\x82\x83\xf1\x84\x85\x86";
997 int32_t startMap
[] = { 0, 0, 2, 2, 2, 5, 5, 5, 5, 9, 9};
998 int32_t nextMap
[] = { 2, 2, 5, 5, 5, 9, 9, 9, 9, 9, 9};
999 int32_t prevMap
[] = { 0, 0, 0, 0, 0, 2, 2, 2, 2, 5, 5};
1000 UChar32 c32Map
[] = {0x201, 0x201, 0x1083, 0x1083, 0x1083, 0x044146, 0x044146, 0x044146, 0x044146, -1, -1};
1001 UChar32 pr32Map
[] = { -1, -1, 0x201, 0x201, 0x201, 0x1083, 0x1083, 0x1083, 0x1083, 0x044146, 0x044146};
1003 // extractLen is the size, in UChars, of what will be extracted between index and index+1.
1004 // is zero when both index positions lie within the same code point.
1005 int32_t exLen
[] = { 0, 1, 0, 0, 1, 0, 0, 0, 2, 0, 0};
1008 UErrorCode status
= U_ZERO_ERROR
;
1009 UText
*ut
= utext_openUTF8(NULL
, u8str
, -1, &status
);
1010 TEST_SUCCESS(status
);
1014 int32_t startMapLimit
= sizeof(startMap
) / sizeof(int32_t);
1015 for (i
=0; i
<startMapLimit
; i
++) {
1016 utext_setNativeIndex(ut
, i
);
1017 int64_t cpIndex
= utext_getNativeIndex(ut
);
1018 TEST_ASSERT(cpIndex
== startMap
[i
]);
1019 cpIndex
= UTEXT_GETNATIVEINDEX(ut
);
1020 TEST_ASSERT(cpIndex
== startMap
[i
]);
1024 for (i
=0; i
<startMapLimit
; i
++) {
1025 UChar32 c32
= utext_char32At(ut
, i
);
1026 TEST_ASSERT(c32
== c32Map
[i
]);
1027 int64_t cpIndex
= utext_getNativeIndex(ut
);
1028 TEST_ASSERT(cpIndex
== startMap
[i
]);
1031 // Check utext_next32From
1032 for (i
=0; i
<startMapLimit
; i
++) {
1033 UChar32 c32
= utext_next32From(ut
, i
);
1034 TEST_ASSERT(c32
== c32Map
[i
]);
1035 int64_t cpIndex
= utext_getNativeIndex(ut
);
1036 TEST_ASSERT(cpIndex
== nextMap
[i
]);
1039 // check utext_previous32From
1040 for (i
=0; i
<startMapLimit
; i
++) {
1042 UChar32 c32
= utext_previous32From(ut
, i
);
1043 TEST_ASSERT(c32
== pr32Map
[i
]);
1044 int64_t cpIndex
= utext_getNativeIndex(ut
);
1045 TEST_ASSERT(cpIndex
== prevMap
[i
]);
1049 // Extract from i to i+1, which may be zero or one code points,
1050 // depending on whether the indices straddle a cp boundary.
1051 for (i
=0; i
<startMapLimit
; i
++) {
1053 status
= U_ZERO_ERROR
;
1054 int32_t extractedLen
= utext_extract(ut
, i
, i
+1, buf
, 3, &status
);
1055 TEST_SUCCESS(status
);
1056 TEST_ASSERT(extractedLen
== exLen
[i
]);
1057 if (extractedLen
> 0) {
1059 /* extractedLen-extractedLen == 0 is used to get around a compiler warning. */
1060 U16_GET(buf
, 0, extractedLen
-extractedLen
, extractedLen
, c32
);
1061 TEST_ASSERT(c32
== c32Map
[i
]);
1069 { // Similar test, with utf16 instead of utf8
1070 // TODO: merge the common parts of these tests.
1072 UnicodeString
u16str("\\u1000\\U00011000\\u2000\\U00022000", -1, US_INV
);
1073 int32_t startMap
[] ={ 0, 1, 1, 3, 4, 4, 6, 6};
1074 int32_t nextMap
[] = { 1, 3, 3, 4, 6, 6, 6, 6};
1075 int32_t prevMap
[] = { 0, 0, 0, 1, 3, 3, 4, 4};
1076 UChar32 c32Map
[] = {0x1000, 0x11000, 0x11000, 0x2000, 0x22000, 0x22000, -1, -1};
1077 UChar32 pr32Map
[] = { -1, 0x1000, 0x1000, 0x11000, 0x2000, 0x2000, 0x22000, 0x22000};
1078 int32_t exLen
[] = { 1, 0, 2, 1, 0, 2, 0, 0,};
1080 u16str
= u16str
.unescape();
1081 UErrorCode status
= U_ZERO_ERROR
;
1082 UText
*ut
= utext_openUnicodeString(NULL
, &u16str
, &status
);
1083 TEST_SUCCESS(status
);
1085 int32_t startMapLimit
= sizeof(startMap
) / sizeof(int32_t);
1087 for (i
=0; i
<startMapLimit
; i
++) {
1088 utext_setNativeIndex(ut
, i
);
1089 int64_t cpIndex
= utext_getNativeIndex(ut
);
1090 TEST_ASSERT(cpIndex
== startMap
[i
]);
1094 for (i
=0; i
<startMapLimit
; i
++) {
1095 UChar32 c32
= utext_char32At(ut
, i
);
1096 TEST_ASSERT(c32
== c32Map
[i
]);
1097 int64_t cpIndex
= utext_getNativeIndex(ut
);
1098 TEST_ASSERT(cpIndex
== startMap
[i
]);
1101 // Check utext_next32From
1102 for (i
=0; i
<startMapLimit
; i
++) {
1103 UChar32 c32
= utext_next32From(ut
, i
);
1104 TEST_ASSERT(c32
== c32Map
[i
]);
1105 int64_t cpIndex
= utext_getNativeIndex(ut
);
1106 TEST_ASSERT(cpIndex
== nextMap
[i
]);
1109 // check utext_previous32From
1110 for (i
=0; i
<startMapLimit
; i
++) {
1111 UChar32 c32
= utext_previous32From(ut
, i
);
1112 TEST_ASSERT(c32
== pr32Map
[i
]);
1113 int64_t cpIndex
= utext_getNativeIndex(ut
);
1114 TEST_ASSERT(cpIndex
== prevMap
[i
]);
1118 // Extract from i to i+1, which may be zero or one code points,
1119 // depending on whether the indices straddle a cp boundary.
1120 for (i
=0; i
<startMapLimit
; i
++) {
1122 status
= U_ZERO_ERROR
;
1123 int32_t extractedLen
= utext_extract(ut
, i
, i
+1, buf
, 3, &status
);
1124 TEST_SUCCESS(status
);
1125 TEST_ASSERT(extractedLen
== exLen
[i
]);
1126 if (extractedLen
> 0) {
1128 /* extractedLen-extractedLen == 0 is used to get around a compiler warning. */
1129 U16_GET(buf
, 0, extractedLen
-extractedLen
, extractedLen
, c32
);
1130 TEST_ASSERT(c32
== c32Map
[i
]);
1137 { // Similar test, with UText over Replaceable
1138 // TODO: merge the common parts of these tests.
1140 UnicodeString
u16str("\\u1000\\U00011000\\u2000\\U00022000", -1, US_INV
);
1141 int32_t startMap
[] ={ 0, 1, 1, 3, 4, 4, 6, 6};
1142 int32_t nextMap
[] = { 1, 3, 3, 4, 6, 6, 6, 6};
1143 int32_t prevMap
[] = { 0, 0, 0, 1, 3, 3, 4, 4};
1144 UChar32 c32Map
[] = {0x1000, 0x11000, 0x11000, 0x2000, 0x22000, 0x22000, -1, -1};
1145 UChar32 pr32Map
[] = { -1, 0x1000, 0x1000, 0x11000, 0x2000, 0x2000, 0x22000, 0x22000};
1146 int32_t exLen
[] = { 1, 0, 2, 1, 0, 2, 0, 0,};
1148 u16str
= u16str
.unescape();
1149 UErrorCode status
= U_ZERO_ERROR
;
1150 UText
*ut
= utext_openReplaceable(NULL
, &u16str
, &status
);
1151 TEST_SUCCESS(status
);
1153 int32_t startMapLimit
= sizeof(startMap
) / sizeof(int32_t);
1155 for (i
=0; i
<startMapLimit
; i
++) {
1156 utext_setNativeIndex(ut
, i
);
1157 int64_t cpIndex
= utext_getNativeIndex(ut
);
1158 TEST_ASSERT(cpIndex
== startMap
[i
]);
1162 for (i
=0; i
<startMapLimit
; i
++) {
1163 UChar32 c32
= utext_char32At(ut
, i
);
1164 TEST_ASSERT(c32
== c32Map
[i
]);
1165 int64_t cpIndex
= utext_getNativeIndex(ut
);
1166 TEST_ASSERT(cpIndex
== startMap
[i
]);
1169 // Check utext_next32From
1170 for (i
=0; i
<startMapLimit
; i
++) {
1171 UChar32 c32
= utext_next32From(ut
, i
);
1172 TEST_ASSERT(c32
== c32Map
[i
]);
1173 int64_t cpIndex
= utext_getNativeIndex(ut
);
1174 TEST_ASSERT(cpIndex
== nextMap
[i
]);
1177 // check utext_previous32From
1178 for (i
=0; i
<startMapLimit
; i
++) {
1179 UChar32 c32
= utext_previous32From(ut
, i
);
1180 TEST_ASSERT(c32
== pr32Map
[i
]);
1181 int64_t cpIndex
= utext_getNativeIndex(ut
);
1182 TEST_ASSERT(cpIndex
== prevMap
[i
]);
1186 // Extract from i to i+1, which may be zero or one code points,
1187 // depending on whether the indices straddle a cp boundary.
1188 for (i
=0; i
<startMapLimit
; i
++) {
1190 status
= U_ZERO_ERROR
;
1191 int32_t extractedLen
= utext_extract(ut
, i
, i
+1, buf
, 3, &status
);
1192 TEST_SUCCESS(status
);
1193 TEST_ASSERT(extractedLen
== exLen
[i
]);
1194 if (extractedLen
> 0) {
1196 /* extractedLen-extractedLen == 0 is used to get around a compiler warning. */
1197 U16_GET(buf
, 0, extractedLen
-extractedLen
, extractedLen
, c32
);
1198 TEST_ASSERT(c32
== c32Map
[i
]);
1207 void UTextTest::FreezeTest() {
1208 // Check isWritable() and freeze() behavior.
1211 UnicodeString
ustr("Hello, World.");
1212 const char u8str
[] = {char(0x31), (char)0x32, (char)0x33, 0};
1213 const UChar u16str
[] = {(UChar
)0x31, (UChar
)0x32, (UChar
)0x44, 0};
1215 UErrorCode status
= U_ZERO_ERROR
;
1219 ut
= utext_openUTF8(ut
, u8str
, -1, &status
);
1220 TEST_SUCCESS(status
);
1221 UBool writable
= utext_isWritable(ut
);
1222 TEST_ASSERT(writable
== FALSE
);
1223 utext_copy(ut
, 1, 2, 0, TRUE
, &status
);
1224 TEST_ASSERT(status
== U_NO_WRITE_PERMISSION
);
1226 status
= U_ZERO_ERROR
;
1227 ut
= utext_openUChars(ut
, u16str
, -1, &status
);
1228 TEST_SUCCESS(status
);
1229 writable
= utext_isWritable(ut
);
1230 TEST_ASSERT(writable
== FALSE
);
1231 utext_copy(ut
, 1, 2, 0, TRUE
, &status
);
1232 TEST_ASSERT(status
== U_NO_WRITE_PERMISSION
);
1234 status
= U_ZERO_ERROR
;
1235 ut
= utext_openUnicodeString(ut
, &ustr
, &status
);
1236 TEST_SUCCESS(status
);
1237 writable
= utext_isWritable(ut
);
1238 TEST_ASSERT(writable
== TRUE
);
1240 writable
= utext_isWritable(ut
);
1241 TEST_ASSERT(writable
== FALSE
);
1242 utext_copy(ut
, 1, 2, 0, TRUE
, &status
);
1243 TEST_ASSERT(status
== U_NO_WRITE_PERMISSION
);
1245 status
= U_ZERO_ERROR
;
1246 ut
= utext_openUnicodeString(ut
, &ustr
, &status
);
1247 TEST_SUCCESS(status
);
1248 ut2
= utext_clone(ut2
, ut
, FALSE
, FALSE
, &status
); // clone with readonly = false
1249 TEST_SUCCESS(status
);
1250 writable
= utext_isWritable(ut2
);
1251 TEST_ASSERT(writable
== TRUE
);
1252 ut2
= utext_clone(ut2
, ut
, FALSE
, TRUE
, &status
); // clone with readonly = true
1253 TEST_SUCCESS(status
);
1254 writable
= utext_isWritable(ut2
);
1255 TEST_ASSERT(writable
== FALSE
);
1256 utext_copy(ut2
, 1, 2, 0, TRUE
, &status
);
1257 TEST_ASSERT(status
== U_NO_WRITE_PERMISSION
);
1259 status
= U_ZERO_ERROR
;
1260 ut
= utext_openConstUnicodeString(ut
, (const UnicodeString
*)&ustr
, &status
);
1261 TEST_SUCCESS(status
);
1262 writable
= utext_isWritable(ut
);
1263 TEST_ASSERT(writable
== FALSE
);
1264 utext_copy(ut
, 1, 2, 0, TRUE
, &status
);
1265 TEST_ASSERT(status
== U_NO_WRITE_PERMISSION
);
1267 // Deep Clone of a frozen UText should re-enable writing in the copy.
1268 status
= U_ZERO_ERROR
;
1269 ut
= utext_openUnicodeString(ut
, &ustr
, &status
);
1270 TEST_SUCCESS(status
);
1272 ut2
= utext_clone(ut2
, ut
, TRUE
, FALSE
, &status
); // deep clone
1273 TEST_SUCCESS(status
);
1274 writable
= utext_isWritable(ut2
);
1275 TEST_ASSERT(writable
== TRUE
);
1278 // Deep clone of a frozen UText, where the base type is intrinsically non-writable,
1279 // should NOT enable writing in the copy.
1280 status
= U_ZERO_ERROR
;
1281 ut
= utext_openUChars(ut
, u16str
, -1, &status
);
1282 TEST_SUCCESS(status
);
1284 ut2
= utext_clone(ut2
, ut
, TRUE
, FALSE
, &status
); // deep clone
1285 TEST_SUCCESS(status
);
1286 writable
= utext_isWritable(ut2
);
1287 TEST_ASSERT(writable
== FALSE
);
1297 // A UText type that works with a chunk size of 1.
1298 // Intended to test for edge cases.
1299 // Input comes from a UnicodeString.
1301 // ut.b the character. Put into both halves.
1305 static UBool U_CALLCONV
1306 fragTextAccess(UText
*ut
, int64_t index
, UBool forward
) {
1307 const UnicodeString
*us
= (const UnicodeString
*)ut
->context
;
1309 int32_t length
= us
->length();
1310 if (forward
&& index
>=0 && index
<length
) {
1311 c
= us
->charAt((int32_t)index
);
1313 ut
->chunkOffset
= 0;
1314 ut
->chunkLength
= 1;
1315 ut
->chunkNativeStart
= index
;
1316 ut
->chunkNativeLimit
= index
+1;
1319 if (!forward
&& index
>0 && index
<=length
) {
1320 c
= us
->charAt((int32_t)index
-1);
1322 ut
->chunkOffset
= 1;
1323 ut
->chunkLength
= 1;
1324 ut
->chunkNativeStart
= index
-1;
1325 ut
->chunkNativeLimit
= index
;
1329 ut
->chunkOffset
= 0;
1330 ut
->chunkLength
= 0;
1332 ut
->chunkNativeStart
= 0;
1333 ut
->chunkNativeLimit
= 0;
1335 ut
->chunkNativeStart
= length
;
1336 ut
->chunkNativeLimit
= length
;
1341 // Function table to be used with this fragmented text provider.
1342 // Initialized in the open function.
1343 static UTextFuncs fragmentFuncs
;
1345 // Clone function for fragmented text provider.
1346 // Didn't really want to provide this, but it's easier to provide it than to keep it
1347 // out of the tests.
1350 cloneFragmentedUnicodeString(UText
*dest
, const UText
*src
, UBool deep
, UErrorCode
*status
) {
1351 if (U_FAILURE(*status
)) {
1355 *status
= U_UNSUPPORTED_ERROR
;
1358 dest
= utext_openUnicodeString(dest
, (UnicodeString
*)src
->context
, status
);
1359 utext_setNativeIndex(dest
, utext_getNativeIndex(src
));
1365 // Open function for the fragmented text provider.
1367 openFragmentedUnicodeString(UText
*ut
, UnicodeString
*s
, UErrorCode
*status
) {
1368 ut
= utext_openUnicodeString(ut
, s
, status
);
1369 if (U_FAILURE(*status
)) {
1373 // Copy of the function table from the stock UnicodeString UText,
1374 // and replace the entry for the access function.
1375 memcpy(&fragmentFuncs
, ut
->pFuncs
, sizeof(fragmentFuncs
));
1376 fragmentFuncs
.access
= fragTextAccess
;
1377 fragmentFuncs
.clone
= cloneFragmentedUnicodeString
;
1378 ut
->pFuncs
= &fragmentFuncs
;
1380 ut
->chunkContents
= (UChar
*)&ut
->b
;
1381 ut
->pFuncs
->access(ut
, 0, TRUE
);
1385 // Regression test for Ticket 5560
1386 // Clone fails to update chunkContentPointer in the cloned copy.
1387 // This is only an issue for UText types that work in a local buffer,
1388 // (UTF-8 wrapper, for example)
1391 // 1. Create an inital UText
1392 // 2. Deep clone it. Contents should match original.
1393 // 3. Reset original to something different.
1394 // 4. Check that clone contents did not change.
1396 void UTextTest::Ticket5560() {
1397 /* The following two strings are in UTF-8 even on EBCDIC platforms. */
1398 static const char s1
[] = {0x41,0x42,0x43,0x44,0x45,0x46,0}; /* "ABCDEF" */
1399 static const char s2
[] = {0x31,0x32,0x33,0x34,0x35,0x36,0}; /* "123456" */
1400 UErrorCode status
= U_ZERO_ERROR
;
1402 UText ut1
= UTEXT_INITIALIZER
;
1403 UText ut2
= UTEXT_INITIALIZER
;
1405 utext_openUTF8(&ut1
, s1
, -1, &status
);
1406 UChar c
= utext_next32(&ut1
);
1407 TEST_ASSERT(c
== 0x41); // c == 'A'
1409 utext_clone(&ut2
, &ut1
, TRUE
, FALSE
, &status
);
1410 TEST_SUCCESS(status
);
1411 c
= utext_next32(&ut2
);
1412 TEST_ASSERT(c
== 0x42); // c == 'B'
1413 c
= utext_next32(&ut1
);
1414 TEST_ASSERT(c
== 0x42); // c == 'B'
1416 utext_openUTF8(&ut1
, s2
, -1, &status
);
1417 c
= utext_next32(&ut1
);
1418 TEST_ASSERT(c
== 0x31); // c == '1'
1419 c
= utext_next32(&ut2
);
1420 TEST_ASSERT(c
== 0x43); // c == 'C'
1427 // Test for Ticket 6847
1429 void UTextTest::Ticket6847() {
1430 const int STRLEN
= 90;
1432 u_memset(s
, 0x41, STRLEN
);
1435 UErrorCode status
= U_ZERO_ERROR
;
1436 UText
*ut
= utext_openUChars(NULL
, s
, -1, &status
);
1438 utext_setNativeIndex(ut
, 0);
1441 int64_t nativeIndex
= UTEXT_GETNATIVEINDEX(ut
);
1442 TEST_ASSERT(nativeIndex
== 0);
1443 while ((c
= utext_next32(ut
)) != U_SENTINEL
) {
1444 TEST_ASSERT(c
== 0x41);
1445 TEST_ASSERT(count
< STRLEN
);
1446 if (count
>= STRLEN
) {
1450 nativeIndex
= UTEXT_GETNATIVEINDEX(ut
);
1451 TEST_ASSERT(nativeIndex
== count
);
1453 TEST_ASSERT(count
== STRLEN
);
1454 nativeIndex
= UTEXT_GETNATIVEINDEX(ut
);
1455 TEST_ASSERT(nativeIndex
== STRLEN
);
1460 void UTextTest::Ticket10562() {
1461 // Note: failures show as a heap error when the test is run under valgrind.
1462 UErrorCode status
= U_ZERO_ERROR
;
1464 const char *utf8_string
= "\x41\x41\x41\x41\x41\x41\x41\x41\x41\x41\x41\x41\x41\x41\x41";
1465 UText
*utf8Text
= utext_openUTF8(NULL
, utf8_string
, -1, &status
);
1466 TEST_SUCCESS(status
);
1467 UText
*deepClone
= utext_clone(NULL
, utf8Text
, TRUE
, FALSE
, &status
);
1468 TEST_SUCCESS(status
);
1469 UText
*shallowClone
= utext_clone(NULL
, deepClone
, FALSE
, FALSE
, &status
);
1470 TEST_SUCCESS(status
);
1471 utext_close(shallowClone
);
1472 utext_close(deepClone
);
1473 utext_close(utf8Text
);
1475 status
= U_ZERO_ERROR
;
1476 UnicodeString
usString("Hello, World.");
1477 UText
*usText
= utext_openUnicodeString(NULL
, &usString
, &status
);
1478 TEST_SUCCESS(status
);
1479 UText
*usDeepClone
= utext_clone(NULL
, usText
, TRUE
, FALSE
, &status
);
1480 TEST_SUCCESS(status
);
1481 UText
*usShallowClone
= utext_clone(NULL
, usDeepClone
, FALSE
, FALSE
, &status
);
1482 TEST_SUCCESS(status
);
1483 utext_close(usShallowClone
);
1484 utext_close(usDeepClone
);
1485 utext_close(usText
);
1489 void UTextTest::Ticket10983() {
1490 // Note: failure shows as a seg fault when the defect is present.
1492 UErrorCode status
= U_ZERO_ERROR
;
1493 UnicodeString
s("Hello, World");
1494 UText
*ut
= utext_openConstUnicodeString(NULL
, &s
, &status
);
1495 TEST_SUCCESS(status
);
1497 status
= U_INVALID_STATE_ERROR
;
1498 UText
*cloned
= utext_clone(NULL
, ut
, TRUE
, TRUE
, &status
);
1499 TEST_ASSERT(cloned
== NULL
);
1500 TEST_ASSERT(status
== U_INVALID_STATE_ERROR
);