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/ustring.h"
20 #include "unicode/uchriter.h"
25 static UBool gFailed
= FALSE
;
26 static int gTestNum
= 0;
29 UText
*openFragmentedUnicodeString(UText
*ut
, UnicodeString
*s
, UErrorCode
*status
);
31 #define TEST_ASSERT(x) \
32 { if ((x)==FALSE) {errln("Test #%d failure in file %s at line %d\n", gTestNum, __FILE__, __LINE__);\
37 #define TEST_SUCCESS(status) \
38 { if (U_FAILURE(status)) {errln("Test #%d failure in file %s at line %d. Error = \"%s\"\n", \
39 gTestNum, __FILE__, __LINE__, u_errorName(status)); \
43 UTextTest::UTextTest() {
46 UTextTest::~UTextTest() {
51 UTextTest::runIndexedTest(int32_t index
, UBool exec
,
52 const char* &name
, char* /*par*/) {
54 case 0: name
= "TextTest";
55 if (exec
) TextTest(); break;
56 case 1: name
= "ErrorTest";
57 if (exec
) ErrorTest(); break;
58 case 2: name
= "FreezeTest";
59 if (exec
) FreezeTest(); break;
60 case 3: name
= "Ticket5560";
61 if (exec
) Ticket5560(); break;
62 case 4: name
= "Ticket6847";
63 if (exec
) Ticket6847(); break;
64 case 5: name
= "Ticket10562";
65 if (exec
) Ticket10562(); break;
66 case 6: name
= "Ticket10983";
67 if (exec
) Ticket10983(); break;
68 case 7: name
= "Ticket12130";
69 if (exec
) Ticket12130(); break;
70 case 8: name
= "Ticket12888";
71 if (exec
) Ticket12888(); break;
72 default: name
= ""; break;
77 // Quick and dirty random number generator.
78 // (don't use library so that results are portable.
79 static uint32_t m_seed
= 1;
80 static uint32_t m_rand()
82 m_seed
= m_seed
* 1103515245 + 12345;
83 return (uint32_t)(m_seed
/65536) % 32768;
90 // Top Level function for UText testing.
91 // Specifies the strings to be tested, with the acutal testing itself
92 // being carried out in another function, TestString().
94 void UTextTest::TextTest() {
97 TestString("abcd\\U00010001xyz");
100 // Supplementary chars at start or end
101 TestString("\\U00010001");
102 TestString("abc\\U00010001");
103 TestString("\\U00010001abc");
105 // Test simple strings of lengths 1 to 60, looking for glitches at buffer boundaries
107 for (i
=1; i
<60; i
++) {
109 for (j
=0; j
<i
; j
++) {
110 if (j
+0x30 == 0x5c) {
111 // backslash. Needs to be escaped
112 s
.append((UChar
)0x5c);
114 s
.append(UChar(j
+0x30));
119 // Test strings with odd-aligned supplementary chars,
120 // looking for glitches at buffer boundaries
121 for (i
=1; i
<60; i
++) {
123 s
.append((UChar
)0x41);
124 for (j
=0; j
<i
; j
++) {
125 s
.append(UChar32(j
+0x11000));
130 // String of chars of randomly varying size in utf-8 representation.
131 // Exercise the mapping, and the varying sized buffer.
137 UChar32 c4
= 0x11000;
138 for (i
=0; i
<1000; i
++) {
139 int len8
= m_rand()%4
+ 1;
143 // don't put 0 into string (0 terminated strings for some tests)
144 // don't put '\', will cause unescape() to fail.
145 if (c1
==0x5c || c1
==0) {
166 // TestString() Run a suite of UText tests on a string.
167 // The test string is unescaped before use.
169 void UTextTest::TestString(const UnicodeString
&s
) {
174 UErrorCode status
= U_ZERO_ERROR
;
178 UnicodeString sa
= s
.unescape();
182 // Build up a mapping between code points and UTF-16 code unit indexes.
184 m
*cpMap
= new m
[sa
.length() + 1];
186 for (i
=0; i
<sa
.length(); i
=sa
.moveIndex32(i
, 1)) {
188 cpMap
[j
].nativeIdx
= i
;
193 cpMap
[j
].nativeIdx
= i
; // position following the last char in utf-16 string.
196 // UChar * test, null terminated
197 status
= U_ZERO_ERROR
;
198 UChar
*buf
= new UChar
[saLen
+1];
199 sa
.extract(buf
, saLen
+1, status
);
200 TEST_SUCCESS(status
);
201 ut
= utext_openUChars(NULL
, buf
, -1, &status
);
202 TEST_SUCCESS(status
);
203 TestAccess(sa
, ut
, cpCount
, cpMap
);
207 // UChar * test, with length
208 status
= U_ZERO_ERROR
;
209 buf
= new UChar
[saLen
+1];
210 sa
.extract(buf
, saLen
+1, status
);
211 TEST_SUCCESS(status
);
212 ut
= utext_openUChars(NULL
, buf
, saLen
, &status
);
213 TEST_SUCCESS(status
);
214 TestAccess(sa
, ut
, cpCount
, cpMap
);
219 // UnicodeString test
220 status
= U_ZERO_ERROR
;
221 ut
= utext_openUnicodeString(NULL
, &sa
, &status
);
222 TEST_SUCCESS(status
);
223 TestAccess(sa
, ut
, cpCount
, cpMap
);
224 TestCMR(sa
, ut
, cpCount
, cpMap
, cpMap
);
228 // Const UnicodeString test
229 status
= U_ZERO_ERROR
;
230 ut
= utext_openConstUnicodeString(NULL
, &sa
, &status
);
231 TEST_SUCCESS(status
);
232 TestAccess(sa
, ut
, cpCount
, cpMap
);
236 // Replaceable test. (UnicodeString inherits Replaceable)
237 status
= U_ZERO_ERROR
;
238 ut
= utext_openReplaceable(NULL
, &sa
, &status
);
239 TEST_SUCCESS(status
);
240 TestAccess(sa
, ut
, cpCount
, cpMap
);
241 TestCMR(sa
, ut
, cpCount
, cpMap
, cpMap
);
244 // Character Iterator Tests
245 status
= U_ZERO_ERROR
;
246 const UChar
*cbuf
= sa
.getBuffer();
247 CharacterIterator
*ci
= new UCharCharacterIterator(cbuf
, saLen
, status
);
248 TEST_SUCCESS(status
);
249 ut
= utext_openCharacterIterator(NULL
, ci
, &status
);
250 TEST_SUCCESS(status
);
251 TestAccess(sa
, ut
, cpCount
, cpMap
);
256 // Fragmented UnicodeString (Chunk size of one)
258 status
= U_ZERO_ERROR
;
259 ut
= openFragmentedUnicodeString(NULL
, &sa
, &status
);
260 TEST_SUCCESS(status
);
261 TestAccess(sa
, ut
, cpCount
, cpMap
);
268 // Convert the test string from UnicodeString to (char *) in utf-8 format
269 int32_t u8Len
= sa
.extract(0, sa
.length(), NULL
, 0, "utf-8");
270 char *u8String
= new char[u8Len
+ 1];
271 sa
.extract(0, sa
.length(), u8String
, u8Len
+1, "utf-8");
273 // Build up the map of code point indices in the utf-8 string
274 m
* u8Map
= new m
[sa
.length() + 1];
275 i
= 0; // native utf-8 index
276 for (j
=0; j
<cpCount
; j
++) { // code point number
277 u8Map
[j
].nativeIdx
= i
;
278 U8_NEXT(u8String
, i
, u8Len
, c
)
281 u8Map
[cpCount
].nativeIdx
= u8Len
; // position following the last char in utf-8 string.
283 // Do the test itself
284 status
= U_ZERO_ERROR
;
285 ut
= utext_openUTF8(NULL
, u8String
, -1, &status
);
286 TEST_SUCCESS(status
);
287 TestAccess(sa
, ut
, cpCount
, u8Map
);
297 // TestCMR test Copy, Move and Replace operations.
298 // us UnicodeString containing the test text.
299 // ut UText containing the same test text.
300 // cpCount number of code points in the test text.
301 // nativeMap Mapping from code points to native indexes for the UText.
302 // u16Map Mapping from code points to UTF-16 indexes, for use with the UnicodeString.
304 // This function runs a whole series of opertions on each incoming UText.
305 // The UText is deep-cloned prior to each operation, so that the original UText remains unchanged.
307 void UTextTest::TestCMR(const UnicodeString
&us
, UText
*ut
, int cpCount
, m
*nativeMap
, m
*u16Map
) {
308 TEST_ASSERT(utext_isWritable(ut
) == TRUE
);
310 int srcLengthType
; // Loop variables for selecting the postion and length
311 int srcPosType
; // of the block to operate on within the source text.
314 int srcIndex
= 0; // Code Point indexes of the block to operate on for
315 int srcLength
= 0; // a specific test.
317 int destIndex
= 0; // Code point index of the destination for a copy/move test.
319 int32_t nativeStart
= 0; // Native unit indexes for a test.
320 int32_t nativeLimit
= 0;
321 int32_t nativeDest
= 0;
323 int32_t u16Start
= 0; // UTF-16 indexes for a test.
324 int32_t u16Limit
= 0; // used when performing the same operation in a Unicode String
327 // Iterate over a whole series of source index, length and a target indexes.
328 // This is done with code point indexes; these will be later translated to native
329 // indexes using the cpMap.
330 for (srcLengthType
=1; srcLengthType
<=3; srcLengthType
++) {
331 switch (srcLengthType
) {
332 case 1: srcLength
= 1; break;
333 case 2: srcLength
= 5; break;
334 case 3: srcLength
= cpCount
/ 3;
336 for (srcPosType
=1; srcPosType
<=5; srcPosType
++) {
337 switch (srcPosType
) {
338 case 1: srcIndex
= 0; break;
339 case 2: srcIndex
= 1; break;
340 case 3: srcIndex
= cpCount
- srcLength
; break;
341 case 4: srcIndex
= cpCount
- srcLength
- 1; break;
342 case 5: srcIndex
= cpCount
/ 2; break;
344 if (srcIndex
< 0 || srcIndex
+ srcLength
> cpCount
) {
345 // filter out bogus test cases -
346 // those with a source range that falls of an edge of the string.
351 // Copy and move tests.
352 // iterate over a variety of destination positions.
354 for (destPosType
=1; destPosType
<=4; destPosType
++) {
355 switch (destPosType
) {
356 case 1: destIndex
= 0; break;
357 case 2: destIndex
= 1; break;
358 case 3: destIndex
= srcIndex
- 1; break;
359 case 4: destIndex
= srcIndex
+ srcLength
+ 1; break;
360 case 5: destIndex
= cpCount
-1; break;
361 case 6: destIndex
= cpCount
; break;
363 if (destIndex
<0 || destIndex
>cpCount
) {
364 // filter out bogus test cases.
368 nativeStart
= nativeMap
[srcIndex
].nativeIdx
;
369 nativeLimit
= nativeMap
[srcIndex
+srcLength
].nativeIdx
;
370 nativeDest
= nativeMap
[destIndex
].nativeIdx
;
372 u16Start
= u16Map
[srcIndex
].nativeIdx
;
373 u16Limit
= u16Map
[srcIndex
+srcLength
].nativeIdx
;
374 u16Dest
= u16Map
[destIndex
].nativeIdx
;
377 TestCopyMove(us
, ut
, FALSE
,
378 nativeStart
, nativeLimit
, nativeDest
,
379 u16Start
, u16Limit
, u16Dest
);
381 TestCopyMove(us
, ut
, TRUE
,
382 nativeStart
, nativeLimit
, nativeDest
,
383 u16Start
, u16Limit
, u16Dest
);
393 UnicodeString
fullRepString("This is an arbitrary string that will be used as replacement text");
394 for (int32_t replStrLen
=0; replStrLen
<20; replStrLen
++) {
395 UnicodeString
repStr(fullRepString
, 0, replStrLen
);
397 nativeStart
, nativeLimit
,
411 // TestCopyMove run a single test case for utext_copy.
412 // Test cases are created in TestCMR and dispatched here for execution.
414 void UTextTest::TestCopyMove(const UnicodeString
&us
, UText
*ut
, UBool move
,
415 int32_t nativeStart
, int32_t nativeLimit
, int32_t nativeDest
,
416 int32_t u16Start
, int32_t u16Limit
, int32_t u16Dest
)
418 UErrorCode status
= U_ZERO_ERROR
;
419 UText
*targetUT
= NULL
;
424 // clone the UText. The test will be run in the cloned copy
425 // so that we don't alter the original.
427 targetUT
= utext_clone(NULL
, ut
, TRUE
, FALSE
, &status
);
428 TEST_SUCCESS(status
);
429 UnicodeString
targetUS(us
); // And copy the reference string.
431 // do the test operation first in the reference
432 targetUS
.copy(u16Start
, u16Limit
, u16Dest
);
434 // delete out the source range.
435 if (u16Limit
< u16Dest
) {
436 targetUS
.removeBetween(u16Start
, u16Limit
);
438 int32_t amtCopied
= u16Limit
- u16Start
;
439 targetUS
.removeBetween(u16Start
+amtCopied
, u16Limit
+amtCopied
);
443 // Do the same operation in the UText under test
444 utext_copy(targetUT
, nativeStart
, nativeLimit
, nativeDest
, move
, &status
);
445 if (nativeDest
> nativeStart
&& nativeDest
< nativeLimit
) {
446 TEST_ASSERT(status
== U_INDEX_OUTOFBOUNDS_ERROR
);
448 TEST_SUCCESS(status
);
450 // Compare the results of the two parallel tests
451 int32_t usi
= 0; // UnicodeString postion, utf-16 index.
452 int64_t uti
= 0; // UText position, native index.
453 int32_t cpi
; // char32 position (code point index)
454 UChar32 usc
; // code point from Unicode String
455 UChar32 utc
; // code point from UText
456 utext_setNativeIndex(targetUT
, 0);
457 for (cpi
=0; ; cpi
++) {
458 usc
= targetUS
.char32At(usi
);
459 utc
= utext_next32(targetUT
);
463 TEST_ASSERT(uti
== usi
);
464 TEST_ASSERT(utc
== usc
);
465 usi
= targetUS
.moveIndex32(usi
, 1);
466 uti
= utext_getNativeIndex(targetUT
);
468 goto cleanupAndReturn
;
471 int64_t expectedNativeLength
= utext_nativeLength(ut
);
473 expectedNativeLength
+= nativeLimit
- nativeStart
;
475 uti
= utext_getNativeIndex(targetUT
);
476 TEST_ASSERT(uti
== expectedNativeLength
);
480 utext_close(targetUT
);
485 // TestReplace Test a single Replace operation.
487 void UTextTest::TestReplace(
488 const UnicodeString
&us
, // reference UnicodeString in which to do the replace
489 UText
*ut
, // UnicodeText object under test.
490 int32_t nativeStart
, // Range to be replaced, in UText native units.
492 int32_t u16Start
, // Range to be replaced, in UTF-16 units
493 int32_t u16Limit
, // for use in the reference UnicodeString.
494 const UnicodeString
&repStr
) // The replacement string
496 UErrorCode status
= U_ZERO_ERROR
;
497 UText
*targetUT
= NULL
;
502 // clone the target UText. The test will be run in the cloned copy
503 // so that we don't alter the original.
505 targetUT
= utext_clone(NULL
, ut
, TRUE
, FALSE
, &status
);
506 TEST_SUCCESS(status
);
507 UnicodeString
targetUS(us
); // And copy the reference string.
510 // Do the replace operation in the Unicode String, to
511 // produce a reference result.
513 targetUS
.replace(u16Start
, u16Limit
-u16Start
, repStr
);
516 // Do the replace on the UText under test
518 const UChar
*rs
= repStr
.getBuffer();
519 int32_t rsLen
= repStr
.length();
520 int32_t actualDelta
= utext_replace(targetUT
, nativeStart
, nativeLimit
, rs
, rsLen
, &status
);
521 int32_t expectedDelta
= repStr
.length() - (nativeLimit
- nativeStart
);
522 TEST_ASSERT(actualDelta
== expectedDelta
);
525 // Compare the results
527 int32_t usi
= 0; // UnicodeString postion, utf-16 index.
528 int64_t uti
= 0; // UText position, native index.
529 int32_t cpi
; // char32 position (code point index)
530 UChar32 usc
; // code point from Unicode String
531 UChar32 utc
; // code point from UText
532 int64_t expectedNativeLength
= 0;
533 utext_setNativeIndex(targetUT
, 0);
534 for (cpi
=0; ; cpi
++) {
535 usc
= targetUS
.char32At(usi
);
536 utc
= utext_next32(targetUT
);
540 TEST_ASSERT(uti
== usi
);
541 TEST_ASSERT(utc
== usc
);
542 usi
= targetUS
.moveIndex32(usi
, 1);
543 uti
= utext_getNativeIndex(targetUT
);
545 goto cleanupAndReturn
;
548 expectedNativeLength
= utext_nativeLength(ut
) + expectedDelta
;
549 uti
= utext_getNativeIndex(targetUT
);
550 TEST_ASSERT(uti
== expectedNativeLength
);
553 utext_close(targetUT
);
557 // TestAccess Test the read only access functions on a UText, including cloning.
558 // The text is accessed in a variety of ways, and compared with
559 // the reference UnicodeString.
561 void UTextTest::TestAccess(const UnicodeString
&us
, UText
*ut
, int cpCount
, m
*cpMap
) {
562 // Run the standard tests on the caller-supplied UText.
563 TestAccessNoClone(us
, ut
, cpCount
, cpMap
);
565 // Re-run tests on a shallow clone.
566 utext_setNativeIndex(ut
, 0);
567 UErrorCode status
= U_ZERO_ERROR
;
568 UText
*shallowClone
= utext_clone(NULL
, ut
, FALSE
/*deep*/, FALSE
/*readOnly*/, &status
);
569 TEST_SUCCESS(status
);
570 TestAccessNoClone(us
, shallowClone
, cpCount
, cpMap
);
573 // Rerun again on a deep clone.
574 // Note that text providers are not required to provide deep cloning,
575 // so unsupported errors are ignored.
577 status
= U_ZERO_ERROR
;
578 utext_setNativeIndex(shallowClone
, 0);
579 UText
*deepClone
= utext_clone(NULL
, shallowClone
, TRUE
, FALSE
, &status
);
580 utext_close(shallowClone
);
581 if (status
!= U_UNSUPPORTED_ERROR
) {
582 TEST_SUCCESS(status
);
583 TestAccessNoClone(us
, deepClone
, cpCount
, cpMap
);
585 utext_close(deepClone
);
590 // TestAccessNoClone() Test the read only access functions on a UText.
591 // The text is accessed in a variety of ways, and compared with
592 // the reference UnicodeString.
594 void UTextTest::TestAccessNoClone(const UnicodeString
&us
, UText
*ut
, int cpCount
, m
*cpMap
) {
595 UErrorCode status
= U_ZERO_ERROR
;
599 // Check the length from the UText
601 int64_t expectedLen
= cpMap
[cpCount
].nativeIdx
;
602 int64_t utlen
= utext_nativeLength(ut
);
603 TEST_ASSERT(expectedLen
== utlen
);
606 // Iterate forwards, verify that we get the correct code points
607 // at the correct native offsets.
611 int64_t expectedIndex
= 0;
612 int64_t foundIndex
= 0;
617 for (i
=0; i
<cpCount
; i
++) {
618 expectedIndex
= cpMap
[i
].nativeIdx
;
619 foundIndex
= utext_getNativeIndex(ut
);
620 TEST_ASSERT(expectedIndex
== foundIndex
);
621 expectedC
= cpMap
[i
].cp
;
622 foundC
= utext_next32(ut
);
623 TEST_ASSERT(expectedC
== foundC
);
624 foundIndex
= utext_getPreviousNativeIndex(ut
);
625 TEST_ASSERT(expectedIndex
== foundIndex
);
630 foundC
= utext_next32(ut
);
631 TEST_ASSERT(foundC
== U_SENTINEL
);
633 // Repeat above, using macros
634 utext_setNativeIndex(ut
, 0);
635 for (i
=0; i
<cpCount
; i
++) {
636 expectedIndex
= cpMap
[i
].nativeIdx
;
637 foundIndex
= UTEXT_GETNATIVEINDEX(ut
);
638 TEST_ASSERT(expectedIndex
== foundIndex
);
639 expectedC
= cpMap
[i
].cp
;
640 foundC
= UTEXT_NEXT32(ut
);
641 TEST_ASSERT(expectedC
== foundC
);
646 foundC
= UTEXT_NEXT32(ut
);
647 TEST_ASSERT(foundC
== U_SENTINEL
);
650 // Forward iteration (above) should have left index at the
651 // end of the input, which should == length().
653 len
= utext_nativeLength(ut
);
654 foundIndex
= utext_getNativeIndex(ut
);
655 TEST_ASSERT(len
== foundIndex
);
658 // Iterate backwards over entire test string
660 len
= utext_getNativeIndex(ut
);
661 utext_setNativeIndex(ut
, len
);
662 for (i
=cpCount
-1; i
>=0; i
--) {
663 expectedC
= cpMap
[i
].cp
;
664 expectedIndex
= cpMap
[i
].nativeIdx
;
665 int64_t prevIndex
= utext_getPreviousNativeIndex(ut
);
666 foundC
= utext_previous32(ut
);
667 foundIndex
= utext_getNativeIndex(ut
);
668 TEST_ASSERT(expectedIndex
== foundIndex
);
669 TEST_ASSERT(expectedC
== foundC
);
670 TEST_ASSERT(prevIndex
== foundIndex
);
677 // Backwards iteration, above, should have left our iterator
678 // position at zero, and continued backwards iterationshould fail.
680 foundIndex
= utext_getNativeIndex(ut
);
681 TEST_ASSERT(foundIndex
== 0);
682 foundIndex
= utext_getPreviousNativeIndex(ut
);
683 TEST_ASSERT(foundIndex
== 0);
686 foundC
= utext_previous32(ut
);
687 TEST_ASSERT(foundC
== U_SENTINEL
);
688 foundIndex
= utext_getNativeIndex(ut
);
689 TEST_ASSERT(foundIndex
== 0);
690 foundIndex
= utext_getPreviousNativeIndex(ut
);
691 TEST_ASSERT(foundIndex
== 0);
694 // And again, with the macros
695 utext_setNativeIndex(ut
, len
);
696 for (i
=cpCount
-1; i
>=0; i
--) {
697 expectedC
= cpMap
[i
].cp
;
698 expectedIndex
= cpMap
[i
].nativeIdx
;
699 foundC
= UTEXT_PREVIOUS32(ut
);
700 foundIndex
= UTEXT_GETNATIVEINDEX(ut
);
701 TEST_ASSERT(expectedIndex
== foundIndex
);
702 TEST_ASSERT(expectedC
== foundC
);
709 // Backwards iteration, above, should have left our iterator
710 // position at zero, and continued backwards iterationshould fail.
712 foundIndex
= UTEXT_GETNATIVEINDEX(ut
);
713 TEST_ASSERT(foundIndex
== 0);
715 foundC
= UTEXT_PREVIOUS32(ut
);
716 TEST_ASSERT(foundC
== U_SENTINEL
);
717 foundIndex
= UTEXT_GETNATIVEINDEX(ut
);
718 TEST_ASSERT(foundIndex
== 0);
724 // next32From(), prevous32From(), Iterate in a somewhat random order.
727 for (i
=0; i
<cpCount
; i
++) {
728 cpIndex
= (cpIndex
+ 9973) % cpCount
;
729 index
= cpMap
[cpIndex
].nativeIdx
;
730 expectedC
= cpMap
[cpIndex
].cp
;
731 foundC
= utext_next32From(ut
, index
);
732 TEST_ASSERT(expectedC
== foundC
);
739 for (i
=0; i
<cpCount
; i
++) {
740 cpIndex
= (cpIndex
+ 9973) % cpCount
;
741 index
= cpMap
[cpIndex
+1].nativeIdx
;
742 expectedC
= cpMap
[cpIndex
].cp
;
743 foundC
= utext_previous32From(ut
, index
);
744 TEST_ASSERT(expectedC
== foundC
);
752 // moveIndex(int32_t delta);
755 // Walk through frontwards, incrementing by one
756 utext_setNativeIndex(ut
, 0);
757 for (i
=1; i
<=cpCount
; i
++) {
758 utext_moveIndex32(ut
, 1);
759 index
= utext_getNativeIndex(ut
);
760 expectedIndex
= cpMap
[i
].nativeIdx
;
761 TEST_ASSERT(expectedIndex
== index
);
762 index
= UTEXT_GETNATIVEINDEX(ut
);
763 TEST_ASSERT(expectedIndex
== index
);
766 // Walk through frontwards, incrementing by two
767 utext_setNativeIndex(ut
, 0);
768 for (i
=2; i
<cpCount
; i
+=2) {
769 utext_moveIndex32(ut
, 2);
770 index
= utext_getNativeIndex(ut
);
771 expectedIndex
= cpMap
[i
].nativeIdx
;
772 TEST_ASSERT(expectedIndex
== index
);
773 index
= UTEXT_GETNATIVEINDEX(ut
);
774 TEST_ASSERT(expectedIndex
== index
);
777 // walk through the string backwards, decrementing by one.
778 i
= cpMap
[cpCount
].nativeIdx
;
779 utext_setNativeIndex(ut
, i
);
780 for (i
=cpCount
; i
>=0; i
--) {
781 expectedIndex
= cpMap
[i
].nativeIdx
;
782 index
= utext_getNativeIndex(ut
);
783 TEST_ASSERT(expectedIndex
== index
);
784 index
= UTEXT_GETNATIVEINDEX(ut
);
785 TEST_ASSERT(expectedIndex
== index
);
786 utext_moveIndex32(ut
, -1);
790 // walk through backwards, decrementing by three
791 i
= cpMap
[cpCount
].nativeIdx
;
792 utext_setNativeIndex(ut
, i
);
793 for (i
=cpCount
; i
>=0; i
-=3) {
794 expectedIndex
= cpMap
[i
].nativeIdx
;
795 index
= utext_getNativeIndex(ut
);
796 TEST_ASSERT(expectedIndex
== index
);
797 index
= UTEXT_GETNATIVEINDEX(ut
);
798 TEST_ASSERT(expectedIndex
== index
);
799 utext_moveIndex32(ut
, -3);
806 int bufSize
= us
.length() + 10;
807 UChar
*buf
= new UChar
[bufSize
];
808 status
= U_ZERO_ERROR
;
809 expectedLen
= us
.length();
810 len
= utext_extract(ut
, 0, utlen
, buf
, bufSize
, &status
);
811 TEST_SUCCESS(status
);
812 TEST_ASSERT(len
== expectedLen
);
813 int compareResult
= us
.compare(buf
, -1);
814 TEST_ASSERT(compareResult
== 0);
816 status
= U_ZERO_ERROR
;
817 len
= utext_extract(ut
, 0, utlen
, NULL
, 0, &status
);
819 TEST_ASSERT(status
== U_STRING_NOT_TERMINATED_WARNING
);
821 TEST_ASSERT(status
== U_BUFFER_OVERFLOW_ERROR
);
823 TEST_ASSERT(len
== expectedLen
);
825 status
= U_ZERO_ERROR
;
826 u_memset(buf
, 0x5555, bufSize
);
827 len
= utext_extract(ut
, 0, utlen
, buf
, 1, &status
);
828 if (us
.length() == 0) {
829 TEST_SUCCESS(status
);
830 TEST_ASSERT(buf
[0] == 0);
832 // Buf len == 1, extracting a single 16 bit value.
833 // If the data char is supplementary, it doesn't matter whether the buffer remains unchanged,
834 // or whether the lead surrogate of the pair is extracted.
835 // It's a buffer overflow error in either case.
836 TEST_ASSERT(buf
[0] == us
.charAt(0) ||
837 (buf
[0] == 0x5555 && U_IS_SUPPLEMENTARY(us
.char32At(0))));
838 TEST_ASSERT(buf
[1] == 0x5555);
839 if (us
.length() == 1) {
840 TEST_ASSERT(status
== U_STRING_NOT_TERMINATED_WARNING
);
842 TEST_ASSERT(status
== U_BUFFER_OVERFLOW_ERROR
);
850 // ErrorTest() Check various error and edge cases.
852 void UTextTest::ErrorTest()
854 // Close of an unitialized UText. Shouldn't blow up.
857 memset(&ut
, 0, sizeof(UText
));
862 // Double-close of a UText. Shouldn't blow up. UText should still be usable.
864 UErrorCode status
= U_ZERO_ERROR
;
865 UText ut
= UTEXT_INITIALIZER
;
866 UnicodeString
s("Hello, World");
867 UText
*ut2
= utext_openUnicodeString(&ut
, &s
, &status
);
868 TEST_SUCCESS(status
);
869 TEST_ASSERT(ut2
== &ut
);
871 UText
*ut3
= utext_close(&ut
);
872 TEST_ASSERT(ut3
== &ut
);
874 UText
*ut4
= utext_close(&ut
);
875 TEST_ASSERT(ut4
== &ut
);
877 utext_openUnicodeString(&ut
, &s
, &status
);
878 TEST_SUCCESS(status
);
882 // Re-use of a UText, chaining through each of the types of UText
883 // (If it doesn't blow up, and doesn't leak, it's probably working fine)
885 UErrorCode status
= U_ZERO_ERROR
;
886 UText ut
= UTEXT_INITIALIZER
;
888 UnicodeString
s1("Hello, World");
889 UChar s2
[] = {(UChar
)0x41, (UChar
)0x42, (UChar
)0};
890 const char *s3
= "\x66\x67\x68";
892 utp
= utext_openUnicodeString(&ut
, &s1
, &status
);
893 TEST_SUCCESS(status
);
894 TEST_ASSERT(utp
== &ut
);
896 utp
= utext_openConstUnicodeString(&ut
, &s1
, &status
);
897 TEST_SUCCESS(status
);
898 TEST_ASSERT(utp
== &ut
);
900 utp
= utext_openUTF8(&ut
, s3
, -1, &status
);
901 TEST_SUCCESS(status
);
902 TEST_ASSERT(utp
== &ut
);
904 utp
= utext_openUChars(&ut
, s2
, -1, &status
);
905 TEST_SUCCESS(status
);
906 TEST_ASSERT(utp
== &ut
);
908 utp
= utext_close(&ut
);
909 TEST_ASSERT(utp
== &ut
);
911 utp
= utext_openUnicodeString(&ut
, &s1
, &status
);
912 TEST_SUCCESS(status
);
913 TEST_ASSERT(utp
== &ut
);
916 // Invalid parameters on open
919 UErrorCode status
= U_ZERO_ERROR
;
920 UText ut
= UTEXT_INITIALIZER
;
922 utext_openUChars(&ut
, NULL
, 5, &status
);
923 TEST_ASSERT(status
== U_ILLEGAL_ARGUMENT_ERROR
);
925 status
= U_ZERO_ERROR
;
926 utext_openUChars(&ut
, NULL
, -1, &status
);
927 TEST_ASSERT(status
== U_ILLEGAL_ARGUMENT_ERROR
);
929 status
= U_ZERO_ERROR
;
930 utext_openUTF8(&ut
, NULL
, 4, &status
);
931 TEST_ASSERT(status
== U_ILLEGAL_ARGUMENT_ERROR
);
933 status
= U_ZERO_ERROR
;
934 utext_openUTF8(&ut
, NULL
, -1, &status
);
935 TEST_ASSERT(status
== U_ILLEGAL_ARGUMENT_ERROR
);
939 // UTF-8 with malformed sequences.
940 // These should come through as the Unicode replacement char, \ufffd
943 UErrorCode status
= U_ZERO_ERROR
;
945 const char *badUTF8
= "\x41\x81\x42\xf0\x81\x81\x43";
948 ut
= utext_openUTF8(NULL
, badUTF8
, -1, &status
);
949 TEST_SUCCESS(status
);
950 c
= utext_char32At(ut
, 1);
951 TEST_ASSERT(c
== 0xfffd);
952 c
= utext_char32At(ut
, 3);
953 TEST_ASSERT(c
== 0xfffd);
954 c
= utext_char32At(ut
, 5);
955 TEST_ASSERT(c
== 0xfffd);
956 c
= utext_char32At(ut
, 6);
957 TEST_ASSERT(c
== 0x43);
960 int n
= utext_extract(ut
, 0, 9, buf
, 10, &status
);
961 TEST_SUCCESS(status
);
963 TEST_ASSERT(buf
[1] == 0xfffd);
964 TEST_ASSERT(buf
[3] == 0xfffd);
965 TEST_ASSERT(buf
[2] == 0x42);
971 // isLengthExpensive - does it make the exptected transitions after
972 // getting the length of a nul terminated string?
975 UErrorCode status
= U_ZERO_ERROR
;
976 UnicodeString
sa("Hello, this is a string");
980 memset(sb
, 0x20, sizeof(sb
));
983 UText
*uta
= utext_openUnicodeString(NULL
, &sa
, &status
);
984 TEST_SUCCESS(status
);
985 isExpensive
= utext_isLengthExpensive(uta
);
986 TEST_ASSERT(isExpensive
== FALSE
);
989 UText
*utb
= utext_openUChars(NULL
, sb
, -1, &status
);
990 TEST_SUCCESS(status
);
991 isExpensive
= utext_isLengthExpensive(utb
);
992 TEST_ASSERT(isExpensive
== TRUE
);
993 int64_t len
= utext_nativeLength(utb
);
994 TEST_ASSERT(len
== 99);
995 isExpensive
= utext_isLengthExpensive(utb
);
996 TEST_ASSERT(isExpensive
== FALSE
);
1001 // Index to positions not on code point boundaries.
1004 const char *u8str
= "\xc8\x81\xe1\x82\x83\xf1\x84\x85\x86";
1005 int32_t startMap
[] = { 0, 0, 2, 2, 2, 5, 5, 5, 5, 9, 9};
1006 int32_t nextMap
[] = { 2, 2, 5, 5, 5, 9, 9, 9, 9, 9, 9};
1007 int32_t prevMap
[] = { 0, 0, 0, 0, 0, 2, 2, 2, 2, 5, 5};
1008 UChar32 c32Map
[] = {0x201, 0x201, 0x1083, 0x1083, 0x1083, 0x044146, 0x044146, 0x044146, 0x044146, -1, -1};
1009 UChar32 pr32Map
[] = { -1, -1, 0x201, 0x201, 0x201, 0x1083, 0x1083, 0x1083, 0x1083, 0x044146, 0x044146};
1011 // extractLen is the size, in UChars, of what will be extracted between index and index+1.
1012 // is zero when both index positions lie within the same code point.
1013 int32_t exLen
[] = { 0, 1, 0, 0, 1, 0, 0, 0, 2, 0, 0};
1016 UErrorCode status
= U_ZERO_ERROR
;
1017 UText
*ut
= utext_openUTF8(NULL
, u8str
, -1, &status
);
1018 TEST_SUCCESS(status
);
1022 int32_t startMapLimit
= UPRV_LENGTHOF(startMap
);
1023 for (i
=0; i
<startMapLimit
; i
++) {
1024 utext_setNativeIndex(ut
, i
);
1025 int64_t cpIndex
= utext_getNativeIndex(ut
);
1026 TEST_ASSERT(cpIndex
== startMap
[i
]);
1027 cpIndex
= UTEXT_GETNATIVEINDEX(ut
);
1028 TEST_ASSERT(cpIndex
== startMap
[i
]);
1032 for (i
=0; i
<startMapLimit
; i
++) {
1033 UChar32 c32
= utext_char32At(ut
, i
);
1034 TEST_ASSERT(c32
== c32Map
[i
]);
1035 int64_t cpIndex
= utext_getNativeIndex(ut
);
1036 TEST_ASSERT(cpIndex
== startMap
[i
]);
1039 // Check utext_next32From
1040 for (i
=0; i
<startMapLimit
; i
++) {
1041 UChar32 c32
= utext_next32From(ut
, i
);
1042 TEST_ASSERT(c32
== c32Map
[i
]);
1043 int64_t cpIndex
= utext_getNativeIndex(ut
);
1044 TEST_ASSERT(cpIndex
== nextMap
[i
]);
1047 // check utext_previous32From
1048 for (i
=0; i
<startMapLimit
; i
++) {
1050 UChar32 c32
= utext_previous32From(ut
, i
);
1051 TEST_ASSERT(c32
== pr32Map
[i
]);
1052 int64_t cpIndex
= utext_getNativeIndex(ut
);
1053 TEST_ASSERT(cpIndex
== prevMap
[i
]);
1057 // Extract from i to i+1, which may be zero or one code points,
1058 // depending on whether the indices straddle a cp boundary.
1059 for (i
=0; i
<startMapLimit
; i
++) {
1061 status
= U_ZERO_ERROR
;
1062 int32_t extractedLen
= utext_extract(ut
, i
, i
+1, buf
, 3, &status
);
1063 TEST_SUCCESS(status
);
1064 TEST_ASSERT(extractedLen
== exLen
[i
]);
1065 if (extractedLen
> 0) {
1067 /* extractedLen-extractedLen == 0 is used to get around a compiler warning. */
1068 U16_GET(buf
, 0, extractedLen
-extractedLen
, extractedLen
, c32
);
1069 TEST_ASSERT(c32
== c32Map
[i
]);
1077 { // Similar test, with utf16 instead of utf8
1078 // TODO: merge the common parts of these tests.
1080 UnicodeString
u16str("\\u1000\\U00011000\\u2000\\U00022000", -1, US_INV
);
1081 int32_t startMap
[] ={ 0, 1, 1, 3, 4, 4, 6, 6};
1082 int32_t nextMap
[] = { 1, 3, 3, 4, 6, 6, 6, 6};
1083 int32_t prevMap
[] = { 0, 0, 0, 1, 3, 3, 4, 4};
1084 UChar32 c32Map
[] = {0x1000, 0x11000, 0x11000, 0x2000, 0x22000, 0x22000, -1, -1};
1085 UChar32 pr32Map
[] = { -1, 0x1000, 0x1000, 0x11000, 0x2000, 0x2000, 0x22000, 0x22000};
1086 int32_t exLen
[] = { 1, 0, 2, 1, 0, 2, 0, 0,};
1088 u16str
= u16str
.unescape();
1089 UErrorCode status
= U_ZERO_ERROR
;
1090 UText
*ut
= utext_openUnicodeString(NULL
, &u16str
, &status
);
1091 TEST_SUCCESS(status
);
1093 int32_t startMapLimit
= UPRV_LENGTHOF(startMap
);
1095 for (i
=0; i
<startMapLimit
; i
++) {
1096 utext_setNativeIndex(ut
, i
);
1097 int64_t cpIndex
= utext_getNativeIndex(ut
);
1098 TEST_ASSERT(cpIndex
== startMap
[i
]);
1102 for (i
=0; i
<startMapLimit
; i
++) {
1103 UChar32 c32
= utext_char32At(ut
, i
);
1104 TEST_ASSERT(c32
== c32Map
[i
]);
1105 int64_t cpIndex
= utext_getNativeIndex(ut
);
1106 TEST_ASSERT(cpIndex
== startMap
[i
]);
1109 // Check utext_next32From
1110 for (i
=0; i
<startMapLimit
; i
++) {
1111 UChar32 c32
= utext_next32From(ut
, i
);
1112 TEST_ASSERT(c32
== c32Map
[i
]);
1113 int64_t cpIndex
= utext_getNativeIndex(ut
);
1114 TEST_ASSERT(cpIndex
== nextMap
[i
]);
1117 // check utext_previous32From
1118 for (i
=0; i
<startMapLimit
; i
++) {
1119 UChar32 c32
= utext_previous32From(ut
, i
);
1120 TEST_ASSERT(c32
== pr32Map
[i
]);
1121 int64_t cpIndex
= utext_getNativeIndex(ut
);
1122 TEST_ASSERT(cpIndex
== prevMap
[i
]);
1126 // Extract from i to i+1, which may be zero or one code points,
1127 // depending on whether the indices straddle a cp boundary.
1128 for (i
=0; i
<startMapLimit
; i
++) {
1130 status
= U_ZERO_ERROR
;
1131 int32_t extractedLen
= utext_extract(ut
, i
, i
+1, buf
, 3, &status
);
1132 TEST_SUCCESS(status
);
1133 TEST_ASSERT(extractedLen
== exLen
[i
]);
1134 if (extractedLen
> 0) {
1136 /* extractedLen-extractedLen == 0 is used to get around a compiler warning. */
1137 U16_GET(buf
, 0, extractedLen
-extractedLen
, extractedLen
, c32
);
1138 TEST_ASSERT(c32
== c32Map
[i
]);
1145 { // Similar test, with UText over Replaceable
1146 // TODO: merge the common parts of these tests.
1148 UnicodeString
u16str("\\u1000\\U00011000\\u2000\\U00022000", -1, US_INV
);
1149 int32_t startMap
[] ={ 0, 1, 1, 3, 4, 4, 6, 6};
1150 int32_t nextMap
[] = { 1, 3, 3, 4, 6, 6, 6, 6};
1151 int32_t prevMap
[] = { 0, 0, 0, 1, 3, 3, 4, 4};
1152 UChar32 c32Map
[] = {0x1000, 0x11000, 0x11000, 0x2000, 0x22000, 0x22000, -1, -1};
1153 UChar32 pr32Map
[] = { -1, 0x1000, 0x1000, 0x11000, 0x2000, 0x2000, 0x22000, 0x22000};
1154 int32_t exLen
[] = { 1, 0, 2, 1, 0, 2, 0, 0,};
1156 u16str
= u16str
.unescape();
1157 UErrorCode status
= U_ZERO_ERROR
;
1158 UText
*ut
= utext_openReplaceable(NULL
, &u16str
, &status
);
1159 TEST_SUCCESS(status
);
1161 int32_t startMapLimit
= UPRV_LENGTHOF(startMap
);
1163 for (i
=0; i
<startMapLimit
; i
++) {
1164 utext_setNativeIndex(ut
, i
);
1165 int64_t cpIndex
= utext_getNativeIndex(ut
);
1166 TEST_ASSERT(cpIndex
== startMap
[i
]);
1170 for (i
=0; i
<startMapLimit
; i
++) {
1171 UChar32 c32
= utext_char32At(ut
, i
);
1172 TEST_ASSERT(c32
== c32Map
[i
]);
1173 int64_t cpIndex
= utext_getNativeIndex(ut
);
1174 TEST_ASSERT(cpIndex
== startMap
[i
]);
1177 // Check utext_next32From
1178 for (i
=0; i
<startMapLimit
; i
++) {
1179 UChar32 c32
= utext_next32From(ut
, i
);
1180 TEST_ASSERT(c32
== c32Map
[i
]);
1181 int64_t cpIndex
= utext_getNativeIndex(ut
);
1182 TEST_ASSERT(cpIndex
== nextMap
[i
]);
1185 // check utext_previous32From
1186 for (i
=0; i
<startMapLimit
; i
++) {
1187 UChar32 c32
= utext_previous32From(ut
, i
);
1188 TEST_ASSERT(c32
== pr32Map
[i
]);
1189 int64_t cpIndex
= utext_getNativeIndex(ut
);
1190 TEST_ASSERT(cpIndex
== prevMap
[i
]);
1194 // Extract from i to i+1, which may be zero or one code points,
1195 // depending on whether the indices straddle a cp boundary.
1196 for (i
=0; i
<startMapLimit
; i
++) {
1198 status
= U_ZERO_ERROR
;
1199 int32_t extractedLen
= utext_extract(ut
, i
, i
+1, buf
, 3, &status
);
1200 TEST_SUCCESS(status
);
1201 TEST_ASSERT(extractedLen
== exLen
[i
]);
1202 if (extractedLen
> 0) {
1204 /* extractedLen-extractedLen == 0 is used to get around a compiler warning. */
1205 U16_GET(buf
, 0, extractedLen
-extractedLen
, extractedLen
, c32
);
1206 TEST_ASSERT(c32
== c32Map
[i
]);
1215 void UTextTest::FreezeTest() {
1216 // Check isWritable() and freeze() behavior.
1219 UnicodeString
ustr("Hello, World.");
1220 const char u8str
[] = {char(0x31), (char)0x32, (char)0x33, 0};
1221 const UChar u16str
[] = {(UChar
)0x31, (UChar
)0x32, (UChar
)0x44, 0};
1223 UErrorCode status
= U_ZERO_ERROR
;
1227 ut
= utext_openUTF8(ut
, u8str
, -1, &status
);
1228 TEST_SUCCESS(status
);
1229 UBool 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_openUChars(ut
, u16str
, -1, &status
);
1236 TEST_SUCCESS(status
);
1237 writable
= utext_isWritable(ut
);
1238 TEST_ASSERT(writable
== FALSE
);
1239 utext_copy(ut
, 1, 2, 0, TRUE
, &status
);
1240 TEST_ASSERT(status
== U_NO_WRITE_PERMISSION
);
1242 status
= U_ZERO_ERROR
;
1243 ut
= utext_openUnicodeString(ut
, &ustr
, &status
);
1244 TEST_SUCCESS(status
);
1245 writable
= utext_isWritable(ut
);
1246 TEST_ASSERT(writable
== TRUE
);
1248 writable
= utext_isWritable(ut
);
1249 TEST_ASSERT(writable
== FALSE
);
1250 utext_copy(ut
, 1, 2, 0, TRUE
, &status
);
1251 TEST_ASSERT(status
== U_NO_WRITE_PERMISSION
);
1253 status
= U_ZERO_ERROR
;
1254 ut
= utext_openUnicodeString(ut
, &ustr
, &status
);
1255 TEST_SUCCESS(status
);
1256 ut2
= utext_clone(ut2
, ut
, FALSE
, FALSE
, &status
); // clone with readonly = false
1257 TEST_SUCCESS(status
);
1258 writable
= utext_isWritable(ut2
);
1259 TEST_ASSERT(writable
== TRUE
);
1260 ut2
= utext_clone(ut2
, ut
, FALSE
, TRUE
, &status
); // clone with readonly = true
1261 TEST_SUCCESS(status
);
1262 writable
= utext_isWritable(ut2
);
1263 TEST_ASSERT(writable
== FALSE
);
1264 utext_copy(ut2
, 1, 2, 0, TRUE
, &status
);
1265 TEST_ASSERT(status
== U_NO_WRITE_PERMISSION
);
1267 status
= U_ZERO_ERROR
;
1268 ut
= utext_openConstUnicodeString(ut
, (const UnicodeString
*)&ustr
, &status
);
1269 TEST_SUCCESS(status
);
1270 writable
= utext_isWritable(ut
);
1271 TEST_ASSERT(writable
== FALSE
);
1272 utext_copy(ut
, 1, 2, 0, TRUE
, &status
);
1273 TEST_ASSERT(status
== U_NO_WRITE_PERMISSION
);
1275 // Deep Clone of a frozen UText should re-enable writing in the copy.
1276 status
= U_ZERO_ERROR
;
1277 ut
= utext_openUnicodeString(ut
, &ustr
, &status
);
1278 TEST_SUCCESS(status
);
1280 ut2
= utext_clone(ut2
, ut
, TRUE
, FALSE
, &status
); // deep clone
1281 TEST_SUCCESS(status
);
1282 writable
= utext_isWritable(ut2
);
1283 TEST_ASSERT(writable
== TRUE
);
1286 // Deep clone of a frozen UText, where the base type is intrinsically non-writable,
1287 // should NOT enable writing in the copy.
1288 status
= U_ZERO_ERROR
;
1289 ut
= utext_openUChars(ut
, u16str
, -1, &status
);
1290 TEST_SUCCESS(status
);
1292 ut2
= utext_clone(ut2
, ut
, TRUE
, FALSE
, &status
); // deep clone
1293 TEST_SUCCESS(status
);
1294 writable
= utext_isWritable(ut2
);
1295 TEST_ASSERT(writable
== FALSE
);
1305 // A UText type that works with a chunk size of 1.
1306 // Intended to test for edge cases.
1307 // Input comes from a UnicodeString.
1309 // ut.b the character. Put into both halves.
1313 static UBool U_CALLCONV
1314 fragTextAccess(UText
*ut
, int64_t index
, UBool forward
) {
1315 const UnicodeString
*us
= (const UnicodeString
*)ut
->context
;
1317 int32_t length
= us
->length();
1318 if (forward
&& index
>=0 && index
<length
) {
1319 c
= us
->charAt((int32_t)index
);
1321 ut
->chunkOffset
= 0;
1322 ut
->chunkLength
= 1;
1323 ut
->chunkNativeStart
= index
;
1324 ut
->chunkNativeLimit
= index
+1;
1327 if (!forward
&& index
>0 && index
<=length
) {
1328 c
= us
->charAt((int32_t)index
-1);
1330 ut
->chunkOffset
= 1;
1331 ut
->chunkLength
= 1;
1332 ut
->chunkNativeStart
= index
-1;
1333 ut
->chunkNativeLimit
= index
;
1337 ut
->chunkOffset
= 0;
1338 ut
->chunkLength
= 0;
1340 ut
->chunkNativeStart
= 0;
1341 ut
->chunkNativeLimit
= 0;
1343 ut
->chunkNativeStart
= length
;
1344 ut
->chunkNativeLimit
= length
;
1349 // Function table to be used with this fragmented text provider.
1350 // Initialized in the open function.
1351 static UTextFuncs fragmentFuncs
;
1353 // Clone function for fragmented text provider.
1354 // Didn't really want to provide this, but it's easier to provide it than to keep it
1355 // out of the tests.
1358 cloneFragmentedUnicodeString(UText
*dest
, const UText
*src
, UBool deep
, UErrorCode
*status
) {
1359 if (U_FAILURE(*status
)) {
1363 *status
= U_UNSUPPORTED_ERROR
;
1366 dest
= utext_openUnicodeString(dest
, (UnicodeString
*)src
->context
, status
);
1367 utext_setNativeIndex(dest
, utext_getNativeIndex(src
));
1373 // Open function for the fragmented text provider.
1375 openFragmentedUnicodeString(UText
*ut
, UnicodeString
*s
, UErrorCode
*status
) {
1376 ut
= utext_openUnicodeString(ut
, s
, status
);
1377 if (U_FAILURE(*status
)) {
1381 // Copy of the function table from the stock UnicodeString UText,
1382 // and replace the entry for the access function.
1383 memcpy(&fragmentFuncs
, ut
->pFuncs
, sizeof(fragmentFuncs
));
1384 fragmentFuncs
.access
= fragTextAccess
;
1385 fragmentFuncs
.clone
= cloneFragmentedUnicodeString
;
1386 ut
->pFuncs
= &fragmentFuncs
;
1388 ut
->chunkContents
= (UChar
*)&ut
->b
;
1389 ut
->pFuncs
->access(ut
, 0, TRUE
);
1393 // Regression test for Ticket 5560
1394 // Clone fails to update chunkContentPointer in the cloned copy.
1395 // This is only an issue for UText types that work in a local buffer,
1396 // (UTF-8 wrapper, for example)
1399 // 1. Create an inital UText
1400 // 2. Deep clone it. Contents should match original.
1401 // 3. Reset original to something different.
1402 // 4. Check that clone contents did not change.
1404 void UTextTest::Ticket5560() {
1405 /* The following two strings are in UTF-8 even on EBCDIC platforms. */
1406 static const char s1
[] = {0x41,0x42,0x43,0x44,0x45,0x46,0}; /* "ABCDEF" */
1407 static const char s2
[] = {0x31,0x32,0x33,0x34,0x35,0x36,0}; /* "123456" */
1408 UErrorCode status
= U_ZERO_ERROR
;
1410 UText ut1
= UTEXT_INITIALIZER
;
1411 UText ut2
= UTEXT_INITIALIZER
;
1413 utext_openUTF8(&ut1
, s1
, -1, &status
);
1414 UChar c
= utext_next32(&ut1
);
1415 TEST_ASSERT(c
== 0x41); // c == 'A'
1417 utext_clone(&ut2
, &ut1
, TRUE
, FALSE
, &status
);
1418 TEST_SUCCESS(status
);
1419 c
= utext_next32(&ut2
);
1420 TEST_ASSERT(c
== 0x42); // c == 'B'
1421 c
= utext_next32(&ut1
);
1422 TEST_ASSERT(c
== 0x42); // c == 'B'
1424 utext_openUTF8(&ut1
, s2
, -1, &status
);
1425 c
= utext_next32(&ut1
);
1426 TEST_ASSERT(c
== 0x31); // c == '1'
1427 c
= utext_next32(&ut2
);
1428 TEST_ASSERT(c
== 0x43); // c == 'C'
1435 // Test for Ticket 6847
1437 void UTextTest::Ticket6847() {
1438 const int STRLEN
= 90;
1440 u_memset(s
, 0x41, STRLEN
);
1443 UErrorCode status
= U_ZERO_ERROR
;
1444 UText
*ut
= utext_openUChars(NULL
, s
, -1, &status
);
1446 utext_setNativeIndex(ut
, 0);
1449 int64_t nativeIndex
= UTEXT_GETNATIVEINDEX(ut
);
1450 TEST_ASSERT(nativeIndex
== 0);
1451 while ((c
= utext_next32(ut
)) != U_SENTINEL
) {
1452 TEST_ASSERT(c
== 0x41);
1453 TEST_ASSERT(count
< STRLEN
);
1454 if (count
>= STRLEN
) {
1458 nativeIndex
= UTEXT_GETNATIVEINDEX(ut
);
1459 TEST_ASSERT(nativeIndex
== count
);
1461 TEST_ASSERT(count
== STRLEN
);
1462 nativeIndex
= UTEXT_GETNATIVEINDEX(ut
);
1463 TEST_ASSERT(nativeIndex
== STRLEN
);
1468 void UTextTest::Ticket10562() {
1469 // Note: failures show as a heap error when the test is run under valgrind.
1470 UErrorCode status
= U_ZERO_ERROR
;
1472 const char *utf8_string
= "\x41\x41\x41\x41\x41\x41\x41\x41\x41\x41\x41\x41\x41\x41\x41";
1473 UText
*utf8Text
= utext_openUTF8(NULL
, utf8_string
, -1, &status
);
1474 TEST_SUCCESS(status
);
1475 UText
*deepClone
= utext_clone(NULL
, utf8Text
, TRUE
, FALSE
, &status
);
1476 TEST_SUCCESS(status
);
1477 UText
*shallowClone
= utext_clone(NULL
, deepClone
, FALSE
, FALSE
, &status
);
1478 TEST_SUCCESS(status
);
1479 utext_close(shallowClone
);
1480 utext_close(deepClone
);
1481 utext_close(utf8Text
);
1483 status
= U_ZERO_ERROR
;
1484 UnicodeString
usString("Hello, World.");
1485 UText
*usText
= utext_openUnicodeString(NULL
, &usString
, &status
);
1486 TEST_SUCCESS(status
);
1487 UText
*usDeepClone
= utext_clone(NULL
, usText
, TRUE
, FALSE
, &status
);
1488 TEST_SUCCESS(status
);
1489 UText
*usShallowClone
= utext_clone(NULL
, usDeepClone
, FALSE
, FALSE
, &status
);
1490 TEST_SUCCESS(status
);
1491 utext_close(usShallowClone
);
1492 utext_close(usDeepClone
);
1493 utext_close(usText
);
1497 void UTextTest::Ticket10983() {
1498 // Note: failure shows as a seg fault when the defect is present.
1500 UErrorCode status
= U_ZERO_ERROR
;
1501 UnicodeString
s("Hello, World");
1502 UText
*ut
= utext_openConstUnicodeString(NULL
, &s
, &status
);
1503 TEST_SUCCESS(status
);
1505 status
= U_INVALID_STATE_ERROR
;
1506 UText
*cloned
= utext_clone(NULL
, ut
, TRUE
, TRUE
, &status
);
1507 TEST_ASSERT(cloned
== NULL
);
1508 TEST_ASSERT(status
== U_INVALID_STATE_ERROR
);
1513 // Ticket 12130 - extract on a UText wrapping a null terminated UChar * string
1514 // leaves the iteration position set incorrectly when the
1515 // actual string length is not yet known.
1517 // The test text needs to be long enough that UText defers getting the length.
1519 void UTextTest::Ticket12130() {
1520 UErrorCode status
= U_ZERO_ERROR
;
1523 "Fundamentally, computers just deal with numbers. They store letters and other characters "
1524 "by assigning a number for each one. Before Unicode was invented, there were hundreds "
1525 "of different encoding systems for assigning these numbers. No single encoding could "
1526 "contain enough characters: for example, the European Union alone requires several "
1527 "different encodings to cover all its languages. Even for a single language like "
1528 "English no single encoding was adequate for all the letters, punctuation, and technical "
1529 "symbols in common use.";
1531 UnicodeString
str(text8
);
1532 const UChar
*ustr
= str
.getTerminatedBuffer();
1533 UText ut
= UTEXT_INITIALIZER
;
1534 utext_openUChars(&ut
, ustr
, -1, &status
);
1535 UChar extractBuffer
[50];
1537 for (int32_t startIdx
= 0; startIdx
<str
.length(); ++startIdx
) {
1538 int32_t endIdx
= startIdx
+ 20;
1540 u_memset(extractBuffer
, 0, UPRV_LENGTHOF(extractBuffer
));
1541 utext_extract(&ut
, startIdx
, endIdx
, extractBuffer
, UPRV_LENGTHOF(extractBuffer
), &status
);
1542 if (U_FAILURE(status
)) {
1543 errln("%s:%d %s", __FILE__
, __LINE__
, u_errorName(status
));
1546 int64_t ni
= utext_getNativeIndex(&ut
);
1547 int64_t expectedni
= startIdx
+ 20;
1548 if (expectedni
> str
.length()) {
1549 expectedni
= str
.length();
1551 if (expectedni
!= ni
) {
1552 errln("%s:%d utext_getNativeIndex() expected %d, got %d", __FILE__
, __LINE__
, expectedni
, ni
);
1554 if (0 != str
.tempSubString(startIdx
, 20).compare(extractBuffer
)) {
1555 errln("%s:%d utext_extract() failed. expected \"%s\", got \"%s\"",
1556 __FILE__
, __LINE__
, CStr(str
.tempSubString(startIdx
, 20))(), CStr(UnicodeString(extractBuffer
))());
1561 // Similar utext extract, this time with the string length provided to the UText in advance,
1562 // and a buffer of larger than required capacity.
1564 utext_openUChars(&ut
, ustr
, str
.length(), &status
);
1565 for (int32_t startIdx
= 0; startIdx
<str
.length(); ++startIdx
) {
1566 int32_t endIdx
= startIdx
+ 20;
1567 u_memset(extractBuffer
, 0, UPRV_LENGTHOF(extractBuffer
));
1568 utext_extract(&ut
, startIdx
, endIdx
, extractBuffer
, UPRV_LENGTHOF(extractBuffer
), &status
);
1569 if (U_FAILURE(status
)) {
1570 errln("%s:%d %s", __FILE__
, __LINE__
, u_errorName(status
));
1573 int64_t ni
= utext_getNativeIndex(&ut
);
1574 int64_t expectedni
= startIdx
+ 20;
1575 if (expectedni
> str
.length()) {
1576 expectedni
= str
.length();
1578 if (expectedni
!= ni
) {
1579 errln("%s:%d utext_getNativeIndex() expected %d, got %d", __FILE__
, __LINE__
, expectedni
, ni
);
1581 if (0 != str
.tempSubString(startIdx
, 20).compare(extractBuffer
)) {
1582 errln("%s:%d utext_extract() failed. expected \"%s\", got \"%s\"",
1583 __FILE__
, __LINE__
, CStr(str
.tempSubString(startIdx
, 20))(), CStr(UnicodeString(extractBuffer
))());
1589 // Ticket 12888: bad handling of illegal utf-8 containing many instances of the archaic, now illegal,
1590 // six byte utf-8 forms. Original implementation had an assumption that
1591 // there would be at most three utf-8 bytes per UTF-16 code unit.
1592 // The five and six byte sequences map to a single replacement character.
1594 void UTextTest::Ticket12888() {
1595 const char *badString
=
1596 "\xfd\x80\x80\x80\x80\x80\xfd\x80\x80\x80\x80\x80\xfd\x80\x80\x80\x80\x80\xfd\x80\x80\x80\x80\x80"
1597 "\xfd\x80\x80\x80\x80\x80\xfd\x80\x80\x80\x80\x80\xfd\x80\x80\x80\x80\x80\xfd\x80\x80\x80\x80\x80"
1598 "\xfd\x80\x80\x80\x80\x80\xfd\x80\x80\x80\x80\x80\xfd\x80\x80\x80\x80\x80\xfd\x80\x80\x80\x80\x80"
1599 "\xfd\x80\x80\x80\x80\x80\xfd\x80\x80\x80\x80\x80\xfd\x80\x80\x80\x80\x80\xfd\x80\x80\x80\x80\x80"
1600 "\xfd\x80\x80\x80\x80\x80\xfd\x80\x80\x80\x80\x80\xfd\x80\x80\x80\x80\x80\xfd\x80\x80\x80\x80\x80"
1601 "\xfd\x80\x80\x80\x80\x80\xfd\x80\x80\x80\x80\x80\xfd\x80\x80\x80\x80\x80\xfd\x80\x80\x80\x80\x80"
1602 "\xfd\x80\x80\x80\x80\x80\xfd\x80\x80\x80\x80\x80\xfd\x80\x80\x80\x80\x80\xfd\x80\x80\x80\x80\x80"
1603 "\xfd\x80\x80\x80\x80\x80\xfd\x80\x80\x80\x80\x80\xfd\x80\x80\x80\x80\x80\xfd\x80\x80\x80\x80\x80"
1604 "\xfd\x80\x80\x80\x80\x80\xfd\x80\x80\x80\x80\x80\xfd\x80\x80\x80\x80\x80\xfd\x80\x80\x80\x80\x80"
1605 "\xfd\x80\x80\x80\x80\x80\xfd\x80\x80\x80\x80\x80\xfd\x80\x80\x80\x80\x80\xfd\x80\x80\x80\x80\x80"
1606 "\xfd\x80\x80\x80\x80\x80\xfd\x80\x80\x80\x80\x80\xfd\x80\x80\x80\x80\x80\xfd\x80\x80\x80\x80\x80"
1607 "\xfd\x80\x80\x80\x80\x80\xfd\x80\x80\x80\x80\x80\xfd\x80\x80\x80\x80\x80\xfd\x80\x80\x80\x80\x80"
1608 "\xfd\x80\x80\x80\x80\x80\xfd\x80\x80\x80\x80\x80\xfd\x80\x80\x80\x80\x80\xfd\x80\x80\x80\x80\x80"
1609 "\xfd\x80\x80\x80\x80\x80\xfd\x80\x80\x80\x80\x80\xfd\x80\x80\x80\x80\x80\xfd\x80\x80\x80\x80\x80"
1610 "\xfd\x80\x80\x80\x80\x80\xfd\x80\x80\x80\x80\x80\xfd\x80\x80\x80\x80\x80\xfd\x80\x80\x80\x80\x80"
1611 "\xfd\x80\x80\x80\x80\x80\xfd\x80\x80\x80\x80\x80\xfd\x80\x80\x80\x80\x80\xfd\x80\x80\x80\x80\x80"
1612 "\xfd\x80\x80\x80\x80\x80\xfd\x80\x80\x80\x80\x80\xfd\x80\x80\x80\x80\x80\xfd\x80\x80\x80\x80\x80"
1613 "\xfd\x80\x80\x80\x80\x80\xfd\x80\x80\x80\x80\x80\xfd\x80\x80\x80\x80\x80\xfd\x80\x80\x80\x80\x80"
1614 "\xfd\x80\x80\x80\x80\x80\xfd\x80\x80\x80\x80\x80\xfd\x80\x80\x80\x80\x80\xfd\x80\x80\x80\x80\x80"
1615 "\xfd\x80\x80\x80\x80\x80\xfd\x80\x80\x80\x80\x80\xfd\x80\x80\x80\x80\x80\xfd\x80\x80\x80\x80\x80";
1617 UErrorCode status
= U_ZERO_ERROR
;
1618 LocalUTextPointer
ut(utext_openUTF8(NULL
, badString
, -1, &status
));
1619 TEST_SUCCESS(status
);
1621 UChar32 c
= utext_next32(ut
.getAlias());
1622 if (c
== U_SENTINEL
) {
1626 int32_t endIdx
= utext_getNativeIndex(ut
.getAlias());
1627 if (endIdx
!= (int32_t)strlen(badString
)) {
1628 errln("%s:%d expected=%d, actual=%d", __FILE__
, __LINE__
, strlen(badString
), endIdx
);
1632 for (int32_t prevIndex
= endIdx
; prevIndex
>0;) {
1633 UChar32 c
= utext_previous32(ut
.getAlias());
1634 int32_t currentIndex
= utext_getNativeIndex(ut
.getAlias());
1636 errln("%s:%d (expected, actual, index) = (%d, %d, %d)\n",
1637 __FILE__
, __LINE__
, 0xfffd, c
, currentIndex
);
1640 if (currentIndex
!= prevIndex
- 6) {
1641 errln("%s:%d: wrong index. Expected, actual = %d, %d",
1642 __FILE__
, __LINE__
, prevIndex
- 6, currentIndex
);
1645 prevIndex
= currentIndex
;