1 /********************************************************************
3 * Copyright (c) 2005-2010, 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
= "ComparisonTest";
61 if (exec
) ComparisonTest(); break;
62 default: name
= ""; break;
67 // Quick and dirty random number generator.
68 // (don't use library so that results are portable.
69 static uint32_t m_seed
= 1;
70 static uint32_t m_rand()
72 m_seed
= m_seed
* 1103515245 + 12345;
73 return (uint32_t)(m_seed
/65536) % 32768;
80 // Top Level function for UText testing.
81 // Specifies the strings to be tested, with the acutal testing itself
82 // being carried out in another function, TestString().
84 void UTextTest::TextTest() {
87 TestString("abcd\\U00010001xyz");
90 // Supplementary chars at start or end
91 TestString("\\U00010001");
92 TestString("abc\\U00010001");
93 TestString("\\U00010001abc");
95 // Test simple strings of lengths 1 to 60, looking for glitches at buffer boundaries
97 for (i
=1; i
<60; i
++) {
100 if (j
+0x30 == 0x5c) {
101 // backslash. Needs to be escaped
102 s
.append((UChar
)0x5c);
104 s
.append(UChar(j
+0x30));
109 // Test strings with odd-aligned supplementary chars,
110 // looking for glitches at buffer boundaries
111 for (i
=1; i
<60; i
++) {
113 s
.append((UChar
)0x41);
114 for (j
=0; j
<i
; j
++) {
115 s
.append(UChar32(j
+0x11000));
120 // String of chars of randomly varying size in utf-8 representation.
121 // Exercise the mapping, and the varying sized buffer.
127 UChar32 c4
= 0x11000;
128 for (i
=0; i
<1000; i
++) {
129 int len8
= m_rand()%4
+ 1;
133 // don't put 0 into string (0 terminated strings for some tests)
134 // don't put '\', will cause unescape() to fail.
135 if (c1
==0x5c || c1
==0) {
156 // TestString() Run a suite of UText tests on a string.
157 // The test string is unescaped before use.
159 void UTextTest::TestString(const UnicodeString
&s
) {
164 UErrorCode status
= U_ZERO_ERROR
;
168 UnicodeString sa
= s
.unescape();
172 // Build up a mapping between code points and UTF-16 code unit indexes.
174 m
*cpMap
= new m
[sa
.length() + 1];
176 for (i
=0; i
<sa
.length(); i
=sa
.moveIndex32(i
, 1)) {
178 cpMap
[j
].nativeIdx
= i
;
183 cpMap
[j
].nativeIdx
= i
; // position following the last char in utf-16 string.
186 // UChar * test, null terminated
187 status
= U_ZERO_ERROR
;
188 UChar
*buf
= new UChar
[saLen
+1];
189 sa
.extract(buf
, saLen
+1, status
);
190 TEST_SUCCESS(status
);
191 ut
= utext_openUChars(NULL
, buf
, -1, &status
);
192 TEST_SUCCESS(status
);
193 TestAccess(sa
, ut
, cpCount
, cpMap
);
197 // UChar * test, with length
198 status
= U_ZERO_ERROR
;
199 buf
= new UChar
[saLen
+1];
200 sa
.extract(buf
, saLen
+1, status
);
201 TEST_SUCCESS(status
);
202 ut
= utext_openUChars(NULL
, buf
, saLen
, &status
);
203 TEST_SUCCESS(status
);
204 TestAccess(sa
, ut
, cpCount
, cpMap
);
209 // UnicodeString test
210 status
= U_ZERO_ERROR
;
211 ut
= utext_openUnicodeString(NULL
, &sa
, &status
);
212 TEST_SUCCESS(status
);
213 TestAccess(sa
, ut
, cpCount
, cpMap
);
214 TestCMR(sa
, ut
, cpCount
, cpMap
, cpMap
);
218 // Const UnicodeString test
219 status
= U_ZERO_ERROR
;
220 ut
= utext_openConstUnicodeString(NULL
, &sa
, &status
);
221 TEST_SUCCESS(status
);
222 TestAccess(sa
, ut
, cpCount
, cpMap
);
226 // Replaceable test. (UnicodeString inherits Replaceable)
227 status
= U_ZERO_ERROR
;
228 ut
= utext_openReplaceable(NULL
, &sa
, &status
);
229 TEST_SUCCESS(status
);
230 TestAccess(sa
, ut
, cpCount
, cpMap
);
231 TestCMR(sa
, ut
, cpCount
, cpMap
, cpMap
);
234 // Character Iterator Tests
235 status
= U_ZERO_ERROR
;
236 const UChar
*cbuf
= sa
.getBuffer();
237 CharacterIterator
*ci
= new UCharCharacterIterator(cbuf
, saLen
, status
);
238 TEST_SUCCESS(status
);
239 ut
= utext_openCharacterIterator(NULL
, ci
, &status
);
240 TEST_SUCCESS(status
);
241 TestAccess(sa
, ut
, cpCount
, cpMap
);
246 // Fragmented UnicodeString (Chunk size of one)
248 status
= U_ZERO_ERROR
;
249 ut
= openFragmentedUnicodeString(NULL
, &sa
, &status
);
250 TEST_SUCCESS(status
);
251 TestAccess(sa
, ut
, cpCount
, cpMap
);
258 // Convert the test string from UnicodeString to (char *) in utf-8 format
259 int32_t u8Len
= sa
.extract(0, sa
.length(), NULL
, 0, "utf-8");
260 char *u8String
= new char[u8Len
+ 1];
261 sa
.extract(0, sa
.length(), u8String
, u8Len
+1, "utf-8");
263 // Build up the map of code point indices in the utf-8 string
264 m
* u8Map
= new m
[sa
.length() + 1];
265 i
= 0; // native utf-8 index
266 for (j
=0; j
<cpCount
; j
++) { // code point number
267 u8Map
[j
].nativeIdx
= i
;
268 U8_NEXT(u8String
, i
, u8Len
, c
)
271 u8Map
[cpCount
].nativeIdx
= u8Len
; // position following the last char in utf-8 string.
273 // Do the test itself
274 status
= U_ZERO_ERROR
;
275 ut
= utext_openUTF8(NULL
, u8String
, -1, &status
);
276 TEST_SUCCESS(status
);
277 TestAccess(sa
, ut
, cpCount
, u8Map
);
287 // TestCMR test Copy, Move and Replace operations.
288 // us UnicodeString containing the test text.
289 // ut UText containing the same test text.
290 // cpCount number of code points in the test text.
291 // nativeMap Mapping from code points to native indexes for the UText.
292 // u16Map Mapping from code points to UTF-16 indexes, for use with the UnicodeString.
294 // This function runs a whole series of opertions on each incoming UText.
295 // The UText is deep-cloned prior to each operation, so that the original UText remains unchanged.
297 void UTextTest::TestCMR(const UnicodeString
&us
, UText
*ut
, int cpCount
, m
*nativeMap
, m
*u16Map
) {
298 TEST_ASSERT(utext_isWritable(ut
) == TRUE
);
300 int srcLengthType
; // Loop variables for selecting the postion and length
301 int srcPosType
; // of the block to operate on within the source text.
304 int srcIndex
= 0; // Code Point indexes of the block to operate on for
305 int srcLength
= 0; // a specific test.
307 int destIndex
= 0; // Code point index of the destination for a copy/move test.
309 int32_t nativeStart
= 0; // Native unit indexes for a test.
310 int32_t nativeLimit
= 0;
311 int32_t nativeDest
= 0;
313 int32_t u16Start
= 0; // UTF-16 indexes for a test.
314 int32_t u16Limit
= 0; // used when performing the same operation in a Unicode String
317 // Iterate over a whole series of source index, length and a target indexes.
318 // This is done with code point indexes; these will be later translated to native
319 // indexes using the cpMap.
320 for (srcLengthType
=1; srcLengthType
<=3; srcLengthType
++) {
321 switch (srcLengthType
) {
322 case 1: srcLength
= 1; break;
323 case 2: srcLength
= 5; break;
324 case 3: srcLength
= cpCount
/ 3;
326 for (srcPosType
=1; srcPosType
<=5; srcPosType
++) {
327 switch (srcPosType
) {
328 case 1: srcIndex
= 0; break;
329 case 2: srcIndex
= 1; break;
330 case 3: srcIndex
= cpCount
- srcLength
; break;
331 case 4: srcIndex
= cpCount
- srcLength
- 1; break;
332 case 5: srcIndex
= cpCount
/ 2; break;
334 if (srcIndex
< 0 || srcIndex
+ srcLength
> cpCount
) {
335 // filter out bogus test cases -
336 // those with a source range that falls of an edge of the string.
341 // Copy and move tests.
342 // iterate over a variety of destination positions.
344 for (destPosType
=1; destPosType
<=4; destPosType
++) {
345 switch (destPosType
) {
346 case 1: destIndex
= 0; break;
347 case 2: destIndex
= 1; break;
348 case 3: destIndex
= srcIndex
- 1; break;
349 case 4: destIndex
= srcIndex
+ srcLength
+ 1; break;
350 case 5: destIndex
= cpCount
-1; break;
351 case 6: destIndex
= cpCount
; break;
353 if (destIndex
<0 || destIndex
>cpCount
) {
354 // filter out bogus test cases.
358 nativeStart
= nativeMap
[srcIndex
].nativeIdx
;
359 nativeLimit
= nativeMap
[srcIndex
+srcLength
].nativeIdx
;
360 nativeDest
= nativeMap
[destIndex
].nativeIdx
;
362 u16Start
= u16Map
[srcIndex
].nativeIdx
;
363 u16Limit
= u16Map
[srcIndex
+srcLength
].nativeIdx
;
364 u16Dest
= u16Map
[destIndex
].nativeIdx
;
367 TestCopyMove(us
, ut
, FALSE
,
368 nativeStart
, nativeLimit
, nativeDest
,
369 u16Start
, u16Limit
, u16Dest
);
371 TestCopyMove(us
, ut
, TRUE
,
372 nativeStart
, nativeLimit
, nativeDest
,
373 u16Start
, u16Limit
, u16Dest
);
383 UnicodeString
fullRepString("This is an arbitrary string that will be used as replacement text");
384 for (int32_t replStrLen
=0; replStrLen
<20; replStrLen
++) {
385 UnicodeString
repStr(fullRepString
, 0, replStrLen
);
387 nativeStart
, nativeLimit
,
401 // TestCopyMove run a single test case for utext_copy.
402 // Test cases are created in TestCMR and dispatched here for execution.
404 void UTextTest::TestCopyMove(const UnicodeString
&us
, UText
*ut
, UBool move
,
405 int32_t nativeStart
, int32_t nativeLimit
, int32_t nativeDest
,
406 int32_t u16Start
, int32_t u16Limit
, int32_t u16Dest
)
408 UErrorCode status
= U_ZERO_ERROR
;
409 UText
*targetUT
= NULL
;
414 // clone the UText. The test will be run in the cloned copy
415 // so that we don't alter the original.
417 targetUT
= utext_clone(NULL
, ut
, TRUE
, FALSE
, &status
);
418 TEST_SUCCESS(status
);
419 UnicodeString
targetUS(us
); // And copy the reference string.
421 // do the test operation first in the reference
422 targetUS
.copy(u16Start
, u16Limit
, u16Dest
);
424 // delete out the source range.
425 if (u16Limit
< u16Dest
) {
426 targetUS
.removeBetween(u16Start
, u16Limit
);
428 int32_t amtCopied
= u16Limit
- u16Start
;
429 targetUS
.removeBetween(u16Start
+amtCopied
, u16Limit
+amtCopied
);
433 // Do the same operation in the UText under test
434 utext_copy(targetUT
, nativeStart
, nativeLimit
, nativeDest
, move
, &status
);
435 if (nativeDest
> nativeStart
&& nativeDest
< nativeLimit
) {
436 TEST_ASSERT(status
== U_INDEX_OUTOFBOUNDS_ERROR
);
438 TEST_SUCCESS(status
);
440 // Compare the results of the two parallel tests
441 int32_t usi
= 0; // UnicodeString postion, utf-16 index.
442 int64_t uti
= 0; // UText position, native index.
443 int32_t cpi
; // char32 position (code point index)
444 UChar32 usc
; // code point from Unicode String
445 UChar32 utc
; // code point from UText
446 utext_setNativeIndex(targetUT
, 0);
447 for (cpi
=0; ; cpi
++) {
448 usc
= targetUS
.char32At(usi
);
449 utc
= utext_next32(targetUT
);
453 TEST_ASSERT(uti
== usi
);
454 TEST_ASSERT(utc
== usc
);
455 usi
= targetUS
.moveIndex32(usi
, 1);
456 uti
= utext_getNativeIndex(targetUT
);
458 goto cleanupAndReturn
;
461 int64_t expectedNativeLength
= utext_nativeLength(ut
);
463 expectedNativeLength
+= nativeLimit
- nativeStart
;
465 uti
= utext_getNativeIndex(targetUT
);
466 TEST_ASSERT(uti
== expectedNativeLength
);
470 utext_close(targetUT
);
475 // TestReplace Test a single Replace operation.
477 void UTextTest::TestReplace(
478 const UnicodeString
&us
, // reference UnicodeString in which to do the replace
479 UText
*ut
, // UnicodeText object under test.
480 int32_t nativeStart
, // Range to be replaced, in UText native units.
482 int32_t u16Start
, // Range to be replaced, in UTF-16 units
483 int32_t u16Limit
, // for use in the reference UnicodeString.
484 const UnicodeString
&repStr
) // The replacement string
486 UErrorCode status
= U_ZERO_ERROR
;
487 UText
*targetUT
= NULL
;
492 // clone the target UText. The test will be run in the cloned copy
493 // so that we don't alter the original.
495 targetUT
= utext_clone(NULL
, ut
, TRUE
, FALSE
, &status
);
496 TEST_SUCCESS(status
);
497 UnicodeString
targetUS(us
); // And copy the reference string.
500 // Do the replace operation in the Unicode String, to
501 // produce a reference result.
503 targetUS
.replace(u16Start
, u16Limit
-u16Start
, repStr
);
506 // Do the replace on the UText under test
508 const UChar
*rs
= repStr
.getBuffer();
509 int32_t rsLen
= repStr
.length();
510 int32_t actualDelta
= utext_replace(targetUT
, nativeStart
, nativeLimit
, rs
, rsLen
, &status
);
511 int32_t expectedDelta
= repStr
.length() - (nativeLimit
- nativeStart
);
512 TEST_ASSERT(actualDelta
== expectedDelta
);
515 // Compare the results
517 int32_t usi
= 0; // UnicodeString postion, utf-16 index.
518 int64_t uti
= 0; // UText position, native index.
519 int32_t cpi
; // char32 position (code point index)
520 UChar32 usc
; // code point from Unicode String
521 UChar32 utc
; // code point from UText
522 int64_t expectedNativeLength
= 0;
523 utext_setNativeIndex(targetUT
, 0);
524 for (cpi
=0; ; cpi
++) {
525 usc
= targetUS
.char32At(usi
);
526 utc
= utext_next32(targetUT
);
530 TEST_ASSERT(uti
== usi
);
531 TEST_ASSERT(utc
== usc
);
532 usi
= targetUS
.moveIndex32(usi
, 1);
533 uti
= utext_getNativeIndex(targetUT
);
535 goto cleanupAndReturn
;
538 expectedNativeLength
= utext_nativeLength(ut
) + expectedDelta
;
539 uti
= utext_getNativeIndex(targetUT
);
540 TEST_ASSERT(uti
== expectedNativeLength
);
543 utext_close(targetUT
);
547 // TestAccess Test the read only access functions on a UText, including cloning.
548 // The text is accessed in a variety of ways, and compared with
549 // the reference UnicodeString.
551 void UTextTest::TestAccess(const UnicodeString
&us
, UText
*ut
, int cpCount
, m
*cpMap
) {
552 // Run the standard tests on the caller-supplied UText.
553 TestAccessNoClone(us
, ut
, cpCount
, cpMap
);
555 // Re-run tests on a shallow clone.
556 utext_setNativeIndex(ut
, 0);
557 UErrorCode status
= U_ZERO_ERROR
;
558 UText
*shallowClone
= utext_clone(NULL
, ut
, FALSE
/*deep*/, FALSE
/*readOnly*/, &status
);
559 TEST_SUCCESS(status
);
560 TestAccessNoClone(us
, shallowClone
, cpCount
, cpMap
);
563 // Rerun again on a deep clone.
564 // Note that text providers are not required to provide deep cloning,
565 // so unsupported errors are ignored.
567 status
= U_ZERO_ERROR
;
568 utext_setNativeIndex(shallowClone
, 0);
569 UText
*deepClone
= utext_clone(NULL
, shallowClone
, TRUE
, FALSE
, &status
);
570 utext_close(shallowClone
);
571 if (status
!= U_UNSUPPORTED_ERROR
) {
572 TEST_SUCCESS(status
);
573 TestAccessNoClone(us
, deepClone
, cpCount
, cpMap
);
575 utext_close(deepClone
);
580 // TestAccessNoClone() Test the read only access functions on a UText.
581 // The text is accessed in a variety of ways, and compared with
582 // the reference UnicodeString.
584 void UTextTest::TestAccessNoClone(const UnicodeString
&us
, UText
*ut
, int cpCount
, m
*cpMap
) {
585 UErrorCode status
= U_ZERO_ERROR
;
589 // Check the length from the UText
591 int64_t expectedLen
= cpMap
[cpCount
].nativeIdx
;
592 int64_t utlen
= utext_nativeLength(ut
);
593 TEST_ASSERT(expectedLen
== utlen
);
596 // Iterate forwards, verify that we get the correct code points
597 // at the correct native offsets.
601 int64_t expectedIndex
= 0;
602 int64_t foundIndex
= 0;
607 for (i
=0; i
<cpCount
; i
++) {
608 expectedIndex
= cpMap
[i
].nativeIdx
;
609 foundIndex
= utext_getNativeIndex(ut
);
610 TEST_ASSERT(expectedIndex
== foundIndex
);
611 expectedC
= cpMap
[i
].cp
;
612 foundC
= utext_next32(ut
);
613 TEST_ASSERT(expectedC
== foundC
);
614 foundIndex
= utext_getPreviousNativeIndex(ut
);
615 TEST_ASSERT(expectedIndex
== foundIndex
);
620 foundC
= utext_next32(ut
);
621 TEST_ASSERT(foundC
== U_SENTINEL
);
623 // Repeat above, using macros
624 utext_setNativeIndex(ut
, 0);
625 for (i
=0; i
<cpCount
; i
++) {
626 expectedIndex
= cpMap
[i
].nativeIdx
;
627 foundIndex
= UTEXT_GETNATIVEINDEX(ut
);
628 TEST_ASSERT(expectedIndex
== foundIndex
);
629 expectedC
= cpMap
[i
].cp
;
630 foundC
= UTEXT_NEXT32(ut
);
631 TEST_ASSERT(expectedC
== foundC
);
636 foundC
= UTEXT_NEXT32(ut
);
637 TEST_ASSERT(foundC
== U_SENTINEL
);
640 // Forward iteration (above) should have left index at the
641 // end of the input, which should == length().
643 len
= utext_nativeLength(ut
);
644 foundIndex
= utext_getNativeIndex(ut
);
645 TEST_ASSERT(len
== foundIndex
);
648 // Iterate backwards over entire test string
650 len
= utext_getNativeIndex(ut
);
651 utext_setNativeIndex(ut
, len
);
652 for (i
=cpCount
-1; i
>=0; i
--) {
653 expectedC
= cpMap
[i
].cp
;
654 expectedIndex
= cpMap
[i
].nativeIdx
;
655 int64_t prevIndex
= utext_getPreviousNativeIndex(ut
);
656 foundC
= utext_previous32(ut
);
657 foundIndex
= utext_getNativeIndex(ut
);
658 TEST_ASSERT(expectedIndex
== foundIndex
);
659 TEST_ASSERT(expectedC
== foundC
);
660 TEST_ASSERT(prevIndex
== foundIndex
);
667 // Backwards iteration, above, should have left our iterator
668 // position at zero, and continued backwards iterationshould fail.
670 foundIndex
= utext_getNativeIndex(ut
);
671 TEST_ASSERT(foundIndex
== 0);
672 foundIndex
= utext_getPreviousNativeIndex(ut
);
673 TEST_ASSERT(foundIndex
== 0);
676 foundC
= utext_previous32(ut
);
677 TEST_ASSERT(foundC
== U_SENTINEL
);
678 foundIndex
= utext_getNativeIndex(ut
);
679 TEST_ASSERT(foundIndex
== 0);
680 foundIndex
= utext_getPreviousNativeIndex(ut
);
681 TEST_ASSERT(foundIndex
== 0);
684 // And again, with the macros
685 utext_setNativeIndex(ut
, len
);
686 for (i
=cpCount
-1; i
>=0; i
--) {
687 expectedC
= cpMap
[i
].cp
;
688 expectedIndex
= cpMap
[i
].nativeIdx
;
689 foundC
= UTEXT_PREVIOUS32(ut
);
690 foundIndex
= UTEXT_GETNATIVEINDEX(ut
);
691 TEST_ASSERT(expectedIndex
== foundIndex
);
692 TEST_ASSERT(expectedC
== foundC
);
699 // Backwards iteration, above, should have left our iterator
700 // position at zero, and continued backwards iterationshould fail.
702 foundIndex
= UTEXT_GETNATIVEINDEX(ut
);
703 TEST_ASSERT(foundIndex
== 0);
705 foundC
= UTEXT_PREVIOUS32(ut
);
706 TEST_ASSERT(foundC
== U_SENTINEL
);
707 foundIndex
= UTEXT_GETNATIVEINDEX(ut
);
708 TEST_ASSERT(foundIndex
== 0);
714 // next32From(), prevous32From(), Iterate in a somewhat random order.
717 for (i
=0; i
<cpCount
; i
++) {
718 cpIndex
= (cpIndex
+ 9973) % cpCount
;
719 index
= cpMap
[cpIndex
].nativeIdx
;
720 expectedC
= cpMap
[cpIndex
].cp
;
721 foundC
= utext_next32From(ut
, index
);
722 TEST_ASSERT(expectedC
== foundC
);
729 for (i
=0; i
<cpCount
; i
++) {
730 cpIndex
= (cpIndex
+ 9973) % cpCount
;
731 index
= cpMap
[cpIndex
+1].nativeIdx
;
732 expectedC
= cpMap
[cpIndex
].cp
;
733 foundC
= utext_previous32From(ut
, index
);
734 TEST_ASSERT(expectedC
== foundC
);
742 // moveIndex(int32_t delta);
745 // Walk through frontwards, incrementing by one
746 utext_setNativeIndex(ut
, 0);
747 for (i
=1; i
<=cpCount
; i
++) {
748 utext_moveIndex32(ut
, 1);
749 index
= utext_getNativeIndex(ut
);
750 expectedIndex
= cpMap
[i
].nativeIdx
;
751 TEST_ASSERT(expectedIndex
== index
);
752 index
= UTEXT_GETNATIVEINDEX(ut
);
753 TEST_ASSERT(expectedIndex
== index
);
756 // Walk through frontwards, incrementing by two
757 utext_setNativeIndex(ut
, 0);
758 for (i
=2; i
<cpCount
; i
+=2) {
759 utext_moveIndex32(ut
, 2);
760 index
= utext_getNativeIndex(ut
);
761 expectedIndex
= cpMap
[i
].nativeIdx
;
762 TEST_ASSERT(expectedIndex
== index
);
763 index
= UTEXT_GETNATIVEINDEX(ut
);
764 TEST_ASSERT(expectedIndex
== index
);
767 // walk through the string backwards, decrementing by one.
768 i
= cpMap
[cpCount
].nativeIdx
;
769 utext_setNativeIndex(ut
, i
);
770 for (i
=cpCount
; i
>=0; i
--) {
771 expectedIndex
= cpMap
[i
].nativeIdx
;
772 index
= utext_getNativeIndex(ut
);
773 TEST_ASSERT(expectedIndex
== index
);
774 index
= UTEXT_GETNATIVEINDEX(ut
);
775 TEST_ASSERT(expectedIndex
== index
);
776 utext_moveIndex32(ut
, -1);
780 // walk through backwards, decrementing by three
781 i
= cpMap
[cpCount
].nativeIdx
;
782 utext_setNativeIndex(ut
, i
);
783 for (i
=cpCount
; i
>=0; i
-=3) {
784 expectedIndex
= cpMap
[i
].nativeIdx
;
785 index
= utext_getNativeIndex(ut
);
786 TEST_ASSERT(expectedIndex
== index
);
787 index
= UTEXT_GETNATIVEINDEX(ut
);
788 TEST_ASSERT(expectedIndex
== index
);
789 utext_moveIndex32(ut
, -3);
796 int bufSize
= us
.length() + 10;
797 UChar
*buf
= new UChar
[bufSize
];
798 status
= U_ZERO_ERROR
;
799 expectedLen
= us
.length();
800 len
= utext_extract(ut
, 0, utlen
, buf
, bufSize
, &status
);
801 TEST_SUCCESS(status
);
802 TEST_ASSERT(len
== expectedLen
);
803 int compareResult
= us
.compare(buf
, -1);
804 TEST_ASSERT(compareResult
== 0);
806 status
= U_ZERO_ERROR
;
807 len
= utext_extract(ut
, 0, utlen
, NULL
, 0, &status
);
809 TEST_ASSERT(status
== U_STRING_NOT_TERMINATED_WARNING
);
811 TEST_ASSERT(status
== U_BUFFER_OVERFLOW_ERROR
);
813 TEST_ASSERT(len
== expectedLen
);
815 status
= U_ZERO_ERROR
;
816 u_memset(buf
, 0x5555, bufSize
);
817 len
= utext_extract(ut
, 0, utlen
, buf
, 1, &status
);
818 if (us
.length() == 0) {
819 TEST_SUCCESS(status
);
820 TEST_ASSERT(buf
[0] == 0);
822 // Buf len == 1, extracting a single 16 bit value.
823 // If the data char is supplementary, it doesn't matter whether the buffer remains unchanged,
824 // or whether the lead surrogate of the pair is extracted.
825 // It's a buffer overflow error in either case.
826 TEST_ASSERT(buf
[0] == us
.charAt(0) ||
827 (buf
[0] == 0x5555 && U_IS_SUPPLEMENTARY(us
.char32At(0))));
828 TEST_ASSERT(buf
[1] == 0x5555);
829 if (us
.length() == 1) {
830 TEST_ASSERT(status
== U_STRING_NOT_TERMINATED_WARNING
);
832 TEST_ASSERT(status
== U_BUFFER_OVERFLOW_ERROR
);
841 // ComparisonTest() Check the string comparison functions. Based on UnicodeStringTest::TestCompare()
843 void UTextTest::ComparisonTest()
845 UErrorCode status
= U_ZERO_ERROR
;
846 UnicodeString
test1Str("this is a test");
847 UnicodeString
test2Str("this is a test");
848 UnicodeString
test3Str("this is a test of the emergency broadcast system");
849 UnicodeString
test4Str("never say, \"this is a test\"!!");
851 UText test1
= UTEXT_INITIALIZER
;
852 UText test2
= UTEXT_INITIALIZER
;
853 UText test3
= UTEXT_INITIALIZER
;
854 UText test4
= UTEXT_INITIALIZER
;
856 UChar uniChars
[] = { 0x74, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73,
857 0x20, 0x61, 0x20, 0x74, 0x65, 0x73, 0x74, 0 };
858 char chars
[] = { 0x74, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73,
859 0x20, 0x61, 0x20, 0x74, 0x65, 0x73, 0x74, 0 };
861 UText uniCharText
= UTEXT_INITIALIZER
;
862 UText charText
= UTEXT_INITIALIZER
;
864 utext_openUnicodeString(&test1
, &test1Str
, &status
);
865 utext_openUnicodeString(&test2
, &test2Str
, &status
);
866 utext_openUnicodeString(&test3
, &test3Str
, &status
);
867 utext_openUnicodeString(&test4
, &test4Str
, &status
);
869 utext_openUChars(&uniCharText
, uniChars
, -1, &status
);
870 utext_openUTF8(&charText
, chars
, -1, &status
);
872 TEST_SUCCESS(status
);
874 // test utext_compare(), simple
875 UTEXT_SETNATIVEINDEX(&test1
, 0);
876 UTEXT_SETNATIVEINDEX(&test2
, 0);
877 if (utext_compare(&test1
, -1, &test2
, -1) != 0) errln("utext_compare() failed, simple setup");
878 UTEXT_SETNATIVEINDEX(&test1
, 0);
879 UTEXT_SETNATIVEINDEX(&test3
, 0);
880 if (utext_compare(&test1
, -1, &test3
, -1) >= 0) errln("utext_compare() failed, simple setup");
881 UTEXT_SETNATIVEINDEX(&test1
, 0);
882 UTEXT_SETNATIVEINDEX(&test4
, 0);
883 if (utext_compare(&test1
, -1, &test4
, -1) <= 0) errln("utext_compare() failed, simple setup");
885 // test utext_compareNativeLimit(), simple
886 UTEXT_SETNATIVEINDEX(&test1
, 0);
887 UTEXT_SETNATIVEINDEX(&test2
, 0);
888 if (utext_compareNativeLimit(&test1
, -1, &test2
, -1) != 0) errln("utext_compareNativeLimit() failed, simple setup");
889 UTEXT_SETNATIVEINDEX(&test1
, 0);
890 UTEXT_SETNATIVEINDEX(&test3
, 0);
891 if (utext_compareNativeLimit(&test1
, -1, &test3
, -1) >= 0) errln("utext_compareNativeLimit() failed, simple setup");
892 UTEXT_SETNATIVEINDEX(&test1
, 0);
893 UTEXT_SETNATIVEINDEX(&test4
, 0);
894 if (utext_compareNativeLimit(&test1
, -1, &test4
, -1) <= 0) errln("utext_compareNativeLimit() failed, simple setup");
896 // test utext_compare(), one explicit length
897 UTEXT_SETNATIVEINDEX(&test1
, 0);
898 UTEXT_SETNATIVEINDEX(&test2
, 0);
899 if (utext_compare(&test1
, 14, &test2
, -1) != 0) errln("utext_compare() failed, one explicit length");
900 UTEXT_SETNATIVEINDEX(&test2
, 0);
901 UTEXT_SETNATIVEINDEX(&test3
, 0);
902 if (utext_compare(&test3
, 14, &test2
, -1) != 0) errln("utext_compare() failed, one explicit length");
903 UTEXT_SETNATIVEINDEX(&test2
, 0);
904 UTEXT_SETNATIVEINDEX(&test4
, 12);
905 if (utext_compare(&test4
, 14, &test2
, -1) != 0) errln("utext_compare() failed, one explicit length and offset");
906 UTEXT_SETNATIVEINDEX(&test1
, 0);
907 UTEXT_SETNATIVEINDEX(&test3
, 0);
908 if (utext_compare(&test3
, 18, &test2
, -1) <= 0) errln("utext_compare() failed, one explicit length");
910 // test utext_compareNativeLimit(), one explicit length
911 UTEXT_SETNATIVEINDEX(&test1
, 0);
912 UTEXT_SETNATIVEINDEX(&test2
, 0);
913 if (utext_compareNativeLimit(&test1
, 14, &test2
, -1) != 0) errln("utext_compareNativeLimit() failed, one explicit length");
914 UTEXT_SETNATIVEINDEX(&test2
, 0);
915 UTEXT_SETNATIVEINDEX(&test3
, 0);
916 if (utext_compareNativeLimit(&test3
, 14, &test2
, -1) != 0) errln("utext_compareNativeLimit() failed, one explicit length");
917 UTEXT_SETNATIVEINDEX(&test2
, 0);
918 UTEXT_SETNATIVEINDEX(&test4
, 12);
919 if (utext_compareNativeLimit(&test4
, 26, &test2
, -1) != 0) errln("utext_compareNativeLimit() failed, one explicit length and limit");
920 UTEXT_SETNATIVEINDEX(&test1
, 0);
921 UTEXT_SETNATIVEINDEX(&test3
, 0);
922 if (utext_compareNativeLimit(&test3
, 18, &test2
, -1) <= 0) errln("utext_compareNativeLimit() failed, one explicit length");
924 // test utext_compare(), UChar-based UText
925 UTEXT_SETNATIVEINDEX(&uniCharText
, 0);
926 UTEXT_SETNATIVEINDEX(&test2
, 0);
927 if (utext_compare(&test2
, -1, &uniCharText
, -1) != 0) errln("utext_compare() failed, UChar-based UText");
928 UTEXT_SETNATIVEINDEX(&uniCharText
, 0);
929 UTEXT_SETNATIVEINDEX(&test3
, 0);
930 if (utext_compare(&test3
, -1, &uniCharText
, -1) <= 0) errln("utext_compare() failed, UChar-based UText");
931 UTEXT_SETNATIVEINDEX(&uniCharText
, 0);
932 UTEXT_SETNATIVEINDEX(&test4
, 0);
933 if (utext_compare(&test4
, -1, &uniCharText
, -1) >= 0) errln("utext_compare() failed, UChar-based UText");
935 // test utext_compareNativeLimit(), UChar-based UText
936 UTEXT_SETNATIVEINDEX(&uniCharText
, 0);
937 UTEXT_SETNATIVEINDEX(&test2
, 0);
938 if (utext_compareNativeLimit(&test2
, -1, &uniCharText
, -1) != 0) errln("utext_compareNativeLimit() failed, UChar-based UText");
939 UTEXT_SETNATIVEINDEX(&uniCharText
, 0);
940 UTEXT_SETNATIVEINDEX(&test3
, 0);
941 if (utext_compareNativeLimit(&test3
, -1, &uniCharText
, -1) <= 0) errln("utext_compareNativeLimit() failed, UChar-based UText");
942 UTEXT_SETNATIVEINDEX(&uniCharText
, 0);
943 UTEXT_SETNATIVEINDEX(&test4
, 0);
944 if (utext_compareNativeLimit(&test4
, -1, &uniCharText
, -1) >= 0) errln("utext_compareNativeLimit() failed, UChar-based UText");
946 // test utext_compare(), UTF8-based UText
947 UTEXT_SETNATIVEINDEX(&charText
, 0);
948 UTEXT_SETNATIVEINDEX(&test2
, 0);
949 if (utext_compare(&test2
, -1, &charText
, -1) != 0) errln("utext_compare() failed, UTF8-based UText");
950 UTEXT_SETNATIVEINDEX(&charText
, 0);
951 UTEXT_SETNATIVEINDEX(&test3
, 0);
952 if (utext_compare(&test3
, -1, &charText
, -1) <= 0) errln("utext_compare() failed, UTF8-based UText");
953 UTEXT_SETNATIVEINDEX(&charText
, 0);
954 UTEXT_SETNATIVEINDEX(&test4
, 0);
955 if (utext_compare(&test4
, -1, &charText
, -1) >= 0) errln("utext_compare() failed, UTF8-based UText");
957 // test utext_compareNativeLimit(), UTF8-based UText
958 UTEXT_SETNATIVEINDEX(&charText
, 0);
959 UTEXT_SETNATIVEINDEX(&test2
, 0);
960 if (utext_compareNativeLimit(&test2
, -1, &charText
, -1) != 0) errln("utext_compareNativeLimit() failed, UTF8-based UText");
961 UTEXT_SETNATIVEINDEX(&charText
, 0);
962 UTEXT_SETNATIVEINDEX(&test3
, 0);
963 if (utext_compareNativeLimit(&test3
, -1, &charText
, -1) <= 0) errln("utext_compareNativeLimit() failed, UTF8-based UText");
964 UTEXT_SETNATIVEINDEX(&charText
, 0);
965 UTEXT_SETNATIVEINDEX(&test4
, 0);
966 if (utext_compareNativeLimit(&test4
, -1, &charText
, -1) >= 0) errln("utext_compareNativeLimit() failed, UTF8-based UText");
968 // test utext_compare(), length
969 UTEXT_SETNATIVEINDEX(&test1
, 0);
970 UTEXT_SETNATIVEINDEX(&test2
, 0);
971 if (utext_compare(&test1
, -1, &test2
, 4) != 0) errln("utext_compare() failed, one length");
972 UTEXT_SETNATIVEINDEX(&test1
, 0);
973 UTEXT_SETNATIVEINDEX(&test2
, 0);
974 if (utext_compare(&test1
, 5, &test2
, 4) <= 0) errln("utext_compare() failed, both lengths");
976 // test utext_compareNativeLimit(), limit
977 UTEXT_SETNATIVEINDEX(&test1
, 0);
978 UTEXT_SETNATIVEINDEX(&test2
, 0);
979 if (utext_compareNativeLimit(&test1
, -1, &test2
, 4) != 0) errln("utext_compareNativeLimit() failed, one limit");
980 UTEXT_SETNATIVEINDEX(&test1
, 0);
981 UTEXT_SETNATIVEINDEX(&test2
, 0);
982 if (utext_compareNativeLimit(&test1
, 5, &test2
, 4) <= 0) errln("utext_compareNativeLimit() failed, both limits");
984 // test utext_compare(), both explicit offsets and lengths
985 UTEXT_SETNATIVEINDEX(&test1
, 0);
986 UTEXT_SETNATIVEINDEX(&test2
, 0);
987 if (utext_compare(&test1
, 14, &test2
, 14) != 0) errln("utext_compare() failed, both explicit offsets and lengths");
988 UTEXT_SETNATIVEINDEX(&test1
, 0);
989 UTEXT_SETNATIVEINDEX(&test3
, 0);
990 if (utext_compare(&test1
, 14, &test3
, 14) != 0) errln("utext_compare() failed, both explicit offsets and lengths");
991 UTEXT_SETNATIVEINDEX(&test1
, 0);
992 UTEXT_SETNATIVEINDEX(&test4
, 12);
993 if (utext_compare(&test1
, 14, &test4
, 14) != 0) errln("utext_compare() failed, both explicit offsets and lengths");
994 UTEXT_SETNATIVEINDEX(&test1
, 10);
995 UTEXT_SETNATIVEINDEX(&test2
, 0);
996 if (utext_compare(&test1
, 4, &test2
, 4) >= 0) errln("utext_compare() failed, both explicit offsets and lengths");
997 UTEXT_SETNATIVEINDEX(&test1
, 10);
998 UTEXT_SETNATIVEINDEX(&test3
, 22);
999 if (utext_compare(&test1
, 4, &test3
, 9) <= 0) errln("utext_compare() failed, both explicit offsets and lengths");
1000 UTEXT_SETNATIVEINDEX(&test1
, 10);
1001 UTEXT_SETNATIVEINDEX(&test4
, 22);
1002 if (utext_compare(&test1
, 4, &test4
, 4) != 0) errln("utext_compare() failed, both explicit offsets and lengths");
1004 // test utext_compareNativeLimit(), both explicit offsets and limits
1005 UTEXT_SETNATIVEINDEX(&test1
, 0);
1006 UTEXT_SETNATIVEINDEX(&test2
, 0);
1007 if (utext_compareNativeLimit(&test1
, 14, &test2
, 14) != 0) errln("utext_compareNativeLimit() failed, both explicit offsets and limits");
1008 UTEXT_SETNATIVEINDEX(&test1
, 0);
1009 UTEXT_SETNATIVEINDEX(&test3
, 0);
1010 if (utext_compareNativeLimit(&test1
, 14, &test3
, 14) != 0) errln("utext_compareNativeLimit() failed, both explicit offsets and limits");
1011 UTEXT_SETNATIVEINDEX(&test1
, 0);
1012 UTEXT_SETNATIVEINDEX(&test4
, 12);
1013 if (utext_compareNativeLimit(&test1
, 14, &test4
, 26) != 0) errln("utext_compareNativeLimit() failed, both explicit offsets and limits");
1014 UTEXT_SETNATIVEINDEX(&test1
, 10);
1015 UTEXT_SETNATIVEINDEX(&test2
, 0);
1016 if (utext_compareNativeLimit(&test1
, 14, &test2
, 4) >= 0) errln("utext_compareNativeLimit() failed, both explicit offsets and limits");
1017 UTEXT_SETNATIVEINDEX(&test1
, 10);
1018 UTEXT_SETNATIVEINDEX(&test3
, 22);
1019 if (utext_compareNativeLimit(&test1
, 14, &test3
, 31) <= 0) errln("utext_compareNativeLimit() failed, both explicit offsets and limits");
1020 UTEXT_SETNATIVEINDEX(&test1
, 10);
1021 UTEXT_SETNATIVEINDEX(&test4
, 22);
1022 if (utext_compareNativeLimit(&test1
, 14, &test4
, 26) != 0) errln("utext_compareNativeLimit() failed, both explicit offsets and limits");
1024 /* test caseCompare() */
1027 _mixed
[]= { 0x61, 0x42, 0x131, 0x3a3, 0xdf, 0x130, 0x49, 0xfb03, 0xd93f, 0xdfff, 0 },
1028 _otherDefault
[]= { 0x41, 0x62, 0x131, 0x3c3, 0x73, 0x53, 0x69, 0x307, 0x69, 0x46, 0x66, 0x49, 0xd93f, 0xdfff, 0 },
1029 _otherExcludeSpecialI
[]={ 0x41, 0x62, 0x131, 0x3c3, 0x53, 0x73, 0x69, 0x131, 0x66, 0x46, 0x69, 0xd93f, 0xdfff, 0 },
1030 _different
[]= { 0x41, 0x62, 0x131, 0x3c3, 0x73, 0x53, 0x130, 0x49, 0x46, 0x66, 0x49, 0xd93f, 0xdffd, 0 };
1033 mixed
= UTEXT_INITIALIZER
,
1034 otherDefault
= UTEXT_INITIALIZER
,
1035 otherExcludeSpecialI
= UTEXT_INITIALIZER
,
1036 different
= UTEXT_INITIALIZER
;
1038 utext_openUChars(&mixed
, _mixed
, -1, &status
);
1039 utext_openUChars(&otherDefault
, _otherDefault
, -1, &status
);
1040 utext_openUChars(&otherExcludeSpecialI
, _otherExcludeSpecialI
, -1, &status
);
1041 utext_openUChars(&different
, _different
, -1, &status
);
1043 TEST_SUCCESS(status
);
1047 /* test default options */
1048 UTEXT_SETNATIVEINDEX(&mixed
, 0);
1049 UTEXT_SETNATIVEINDEX(&otherDefault
, 0);
1050 result
= utext_caseCompare(&mixed
, -1, &otherDefault
, -1, U_FOLD_CASE_DEFAULT
, &status
);
1051 if (0 != result
|| U_FAILURE(status
)) {
1052 errln("error: utext_caseCompare (other, default) gives %ld (should be 0) (%s)\n", result
, u_errorName(status
));
1054 UTEXT_SETNATIVEINDEX(&mixed
, 0);
1055 UTEXT_SETNATIVEINDEX(&otherDefault
, 0);
1056 result
= utext_caseCompareNativeLimit(&mixed
, -1, &otherDefault
, -1, U_FOLD_CASE_DEFAULT
, &status
);
1057 if (0 != result
|| U_FAILURE(status
)) {
1058 errln("error: utext_caseCompareNativeLimit (other, default) gives %ld (should be 0) (%s)\n", result
, u_errorName(status
));
1061 /* test excluding special I */
1062 UTEXT_SETNATIVEINDEX(&mixed
, 0);
1063 UTEXT_SETNATIVEINDEX(&otherExcludeSpecialI
, 0);
1064 result
= utext_caseCompare(&mixed
, -1, &otherExcludeSpecialI
, -1, U_FOLD_CASE_EXCLUDE_SPECIAL_I
, &status
);
1065 if (0 != result
|| U_FAILURE(status
)) {
1066 errln("error: utext_caseCompare (otherExcludeSpecialI, U_FOLD_CASE_EXCLUDE_SPECIAL_I) gives %ld (should be 0) (%s)\n", result
, u_errorName(status
));
1068 UTEXT_SETNATIVEINDEX(&mixed
, 0);
1069 UTEXT_SETNATIVEINDEX(&otherExcludeSpecialI
, 0);
1070 result
= utext_caseCompareNativeLimit(&mixed
, -1, &otherExcludeSpecialI
, -1, U_FOLD_CASE_EXCLUDE_SPECIAL_I
, &status
);
1071 if (0 != result
|| U_FAILURE(status
)) {
1072 errln("error: utext_caseCompareNativeLimit (otherExcludeSpecialI, U_FOLD_CASE_EXCLUDE_SPECIAL_I) gives %ld (should be 0) (%s)\n", result
, u_errorName(status
));
1074 UTEXT_SETNATIVEINDEX(&mixed
, 0);
1075 UTEXT_SETNATIVEINDEX(&otherDefault
, 0);
1076 result
= utext_caseCompare(&mixed
, -1, &otherDefault
, -1, U_FOLD_CASE_EXCLUDE_SPECIAL_I
, &status
);
1077 if (0 == result
|| U_FAILURE(status
)) {
1078 errln("error: utext_caseCompare (other, U_FOLD_CASE_EXCLUDE_SPECIAL_I) gives %ld (should be nonzero) (%s)\n", result
, u_errorName(status
));
1080 UTEXT_SETNATIVEINDEX(&mixed
, 0);
1081 UTEXT_SETNATIVEINDEX(&otherDefault
, 0);
1082 result
= utext_caseCompareNativeLimit(&mixed
, -1, &otherDefault
, -1, U_FOLD_CASE_EXCLUDE_SPECIAL_I
, &status
);
1083 if (0 == result
|| U_FAILURE(status
)) {
1084 errln("error: utext_caseCompareNativeLimit (other, U_FOLD_CASE_EXCLUDE_SPECIAL_I) gives %ld (should be nonzero) (%s)\n", result
, u_errorName(status
));
1087 /* test against different string */
1088 UTEXT_SETNATIVEINDEX(&mixed
, 0);
1089 UTEXT_SETNATIVEINDEX(&different
, 0);
1090 result
= utext_caseCompare(&mixed
, -1, &different
, -1, U_FOLD_CASE_DEFAULT
, &status
);
1091 if (0 >= result
|| U_FAILURE(status
)) {
1092 errln("error: utext_caseCompare (different, default) gives %ld (should be positive) (%s)\n", result
, u_errorName(status
));
1094 UTEXT_SETNATIVEINDEX(&mixed
, 0);
1095 UTEXT_SETNATIVEINDEX(&different
, 0);
1096 result
= utext_caseCompareNativeLimit(&mixed
, -1, &different
, -1, U_FOLD_CASE_DEFAULT
, &status
);
1097 if (0 >= result
|| U_FAILURE(status
)) {
1098 errln("error: utext_caseCompareNativeLimit (different, default) gives %ld (should be positive) (%s)\n", result
, u_errorName(status
));
1101 /* test caseCompare() - include the folded sharp s (U+00df) with different lengths */
1102 UTEXT_SETNATIVEINDEX(&mixed
, 1);
1103 UTEXT_SETNATIVEINDEX(&different
, 1);
1104 result
= utext_caseCompare(&mixed
, 4, &different
, 5, U_FOLD_CASE_DEFAULT
, &status
);
1105 if (0 != result
|| U_FAILURE(status
)) {
1106 errln("error: utext_caseCompare (mixed[1-5), different[1-6), default) gives %ld (should be 0) (%s)\n", result
, u_errorName(status
));
1108 UTEXT_SETNATIVEINDEX(&mixed
, 1);
1109 UTEXT_SETNATIVEINDEX(&different
, 1);
1110 result
= utext_caseCompareNativeLimit(&mixed
, 5, &different
, 6, U_FOLD_CASE_DEFAULT
, &status
);
1111 if (0 != result
|| U_FAILURE(status
)) {
1112 errln("error: utext_caseCompareNativeLimit (mixed[1-5), different[1-6), default) gives %ld (should be 0) (%s)\n", result
, u_errorName(status
));
1115 /* test caseCompare() - stop in the middle of the sharp s (U+00df) */
1116 UTEXT_SETNATIVEINDEX(&mixed
, 1);
1117 UTEXT_SETNATIVEINDEX(&different
, 1);
1118 result
= utext_caseCompare(&mixed
, 4, &different
, 4, U_FOLD_CASE_DEFAULT
, &status
);
1119 if (0 >= result
|| U_FAILURE(status
)) {
1120 errln("error: utext_caseCompare (mixed[1-5), different[1-5), default) gives %ld (should be positive) (%s)\n", result
, u_errorName(status
));
1122 UTEXT_SETNATIVEINDEX(&mixed
, 1);
1123 UTEXT_SETNATIVEINDEX(&different
, 1);
1124 result
= utext_caseCompareNativeLimit(&mixed
, 5, &different
, 5, U_FOLD_CASE_DEFAULT
, &status
);
1125 if (0 >= result
|| U_FAILURE(status
)) {
1126 errln("error: utext_caseCompareNativeLimit (mixed[1-5), different[1-5), default) gives %ld (should be positive) (%s)\n", result
, u_errorName(status
));
1130 /* test surrogates in comparison */
1133 _before
[] = { 0x65, 0xd800, 0xd800, 0xdc01, 0x65, 0x00 },
1134 _after
[] = { 0x65, 0xd800, 0xdc00, 0x65, 0x00 };
1137 before
= UTEXT_INITIALIZER
,
1138 after
= UTEXT_INITIALIZER
;
1140 utext_openUChars(&before
, _before
, -1, &status
);
1141 utext_openUChars(&after
, _after
, -1, &status
);
1143 TEST_SUCCESS(status
);
1146 UTEXT_SETNATIVEINDEX(&before
, 1);
1147 UTEXT_SETNATIVEINDEX(&after
, 1);
1148 result
= utext_compare(&before
, -1, &after
, -1);
1149 if (0 <= result
|| U_FAILURE(status
)) {
1150 errln("error: utext_compare ({ 65, d800, 10001, 65 }, { 65, 10000, 65 }) gives %ld (should be negative) (%s)\n", result
, u_errorName(status
));
1153 UTEXT_SETNATIVEINDEX(&before
, 1);
1154 UTEXT_SETNATIVEINDEX(&after
, 1);
1155 result
= utext_compare(&before
, 3, &after
, 3);
1156 if (0 <= result
|| U_FAILURE(status
)) {
1157 errln("error: utext_compare with lengths ({ 65, d800, 10001, 65 }, { 65, 10000, 65 }) gives %ld (should be negative) (%s)\n", result
, u_errorName(status
));
1160 UTEXT_SETNATIVEINDEX(&before
, 1);
1161 UTEXT_SETNATIVEINDEX(&after
, 1);
1162 result
= utext_caseCompare(&before
, -1, &after
, -1, U_FOLD_CASE_DEFAULT
, &status
);
1163 if (0 <= result
|| U_FAILURE(status
)) {
1164 errln("error: utext_caseCompare ({ 65, d800, 10001, 65 }, { 65, 10000, 65 }) gives %ld (should be negative) (%s)\n", result
, u_errorName(status
));
1167 UTEXT_SETNATIVEINDEX(&before
, 1);
1168 UTEXT_SETNATIVEINDEX(&after
, 1);
1169 result
= utext_caseCompare(&before
, 3, &after
, 3, U_FOLD_CASE_DEFAULT
, &status
);
1170 if (0 <= result
|| U_FAILURE(status
)) {
1171 errln("error: utext_caseCompare with lengths ({ 65, d800, 10001, 65 }, { 65, 10000, 65 }) gives %ld (should be negative) (%s)\n", result
, u_errorName(status
));
1174 utext_close(&before
);
1175 utext_close(&after
);
1178 /* test surrogates at end of string */
1181 _before
[] = { 0x65, 0xd800, 0xd800, 0xdc01, 0x00 },
1182 _after
[] = { 0x65, 0xd800, 0xdc00, 0x00 };
1185 before
= UTEXT_INITIALIZER
,
1186 after
= UTEXT_INITIALIZER
;
1188 utext_openUChars(&before
, _before
, -1, &status
);
1189 utext_openUChars(&after
, _after
, -1, &status
);
1191 TEST_SUCCESS(status
);
1194 UTEXT_SETNATIVEINDEX(&before
, 1);
1195 UTEXT_SETNATIVEINDEX(&after
, 1);
1196 result
= utext_compare(&before
, -1, &after
, -1);
1197 if (0 <= result
|| U_FAILURE(status
)) {
1198 errln("error: utext_compare ({ 65, d800, 10001 }, { 65, 10000 }) gives %ld (should be negative) (%s)\n", result
, u_errorName(status
));
1201 UTEXT_SETNATIVEINDEX(&before
, 1);
1202 UTEXT_SETNATIVEINDEX(&after
, 1);
1203 result
= utext_caseCompare(&before
, -1, &after
, -1, U_FOLD_CASE_DEFAULT
, &status
);
1204 if (0 <= result
|| U_FAILURE(status
)) {
1205 errln("error: utext_caseCompare ({ 65, d800, 10001 }, { 65, 10000 }) gives %ld (should be negative) (%s)\n", result
, u_errorName(status
));
1208 utext_close(&before
);
1209 utext_close(&after
);
1212 /* test empty strings */
1216 UText emptyUChar
= UTEXT_INITIALIZER
;
1217 UText emptyUTF8
= UTEXT_INITIALIZER
;
1218 UText nullUChar
= UTEXT_INITIALIZER
;
1219 UText nullUTF8
= UTEXT_INITIALIZER
;
1221 utext_openUChars(&emptyUChar
, &zero16
, -1, &status
);
1222 utext_openUTF8(&emptyUTF8
, &zero8
, -1, &status
);
1223 utext_openUChars(&nullUChar
, NULL
, 0, &status
);
1224 utext_openUTF8(&nullUTF8
, NULL
, 0, &status
);
1226 if (utext_compare(&emptyUChar
, -1, &emptyUTF8
, -1) != 0) {
1227 errln("error: utext_compare(&emptyUChar, -1, &emptyUTF8, -1) != 0");
1229 if (utext_compare(&emptyUChar
, -1, &nullUChar
, -1) != 0) {
1230 errln("error: utext_compare(&emptyUChar, -1, &nullUChar, -1) != 0");
1232 if (utext_compare(&emptyUChar
, -1, &nullUTF8
, -1) != 0) {
1233 errln("error: utext_compare(&emptyUChar, -1, &nullUTF8, -1) != 0");
1235 if (utext_compare(&emptyUTF8
, -1, &nullUChar
, -1) != 0) {
1236 errln("error: utext_compare(&emptyUTF8, -1, &nullUChar, -1) != 0");
1238 if (utext_compare(&emptyUTF8
, -1, &nullUTF8
, -1) != 0) {
1239 errln("error: utext_compare(&emptyUTF8, -1, &nullUTF8, -1) != 0");
1241 if (utext_compare(&nullUChar
, -1, &nullUTF8
, -1) != 0) {
1242 errln("error: utext_compare(&nullUChar, -1, &nullUTF8, -1) != 0");
1245 if (utext_compareNativeLimit(&emptyUChar
, -1, &emptyUTF8
, -1) != 0) {
1246 errln("error: utext_compareNativeLimit(&emptyUChar, -1, &emptyUTF8, -1) != 0");
1248 if (utext_compareNativeLimit(&emptyUChar
, -1, &nullUChar
, -1) != 0) {
1249 errln("error: utext_compareNativeLimit(&emptyUChar, -1, &nullUChar, -1) != 0");
1251 if (utext_compareNativeLimit(&emptyUChar
, -1, &nullUTF8
, -1) != 0) {
1252 errln("error: utext_compareNativeLimit(&emptyUChar, -1, &nullUTF8, -1) != 0");
1254 if (utext_compareNativeLimit(&emptyUTF8
, -1, &nullUChar
, -1) != 0) {
1255 errln("error: utext_compareNativeLimit(&emptyUTF8, -1, &nullUChar, -1) != 0");
1257 if (utext_compareNativeLimit(&emptyUTF8
, -1, &nullUTF8
, -1) != 0) {
1258 errln("error: utext_compareNativeLimit(&emptyUTF8, -1, &nullUTF8, -1) != 0");
1260 if (utext_compareNativeLimit(&nullUChar
, -1, &nullUTF8
, -1) != 0) {
1261 errln("error: utext_compareNativeLimit(&nullUChar, -1, &nullUTF8, -1) != 0");
1264 if (utext_caseCompare(&emptyUChar
, -1, &emptyUTF8
, -1, 0, &status
) != 0) {
1265 errln("error: utext_caseCompare(&emptyUChar, -1, &emptyUTF8, -1, 0, &status) != 0");
1267 if (utext_caseCompare(&emptyUChar
, -1, &nullUChar
, -1, 0, &status
) != 0) {
1268 errln("error: utext_caseCompare(&emptyUChar, -1, &nullUChar, -1, 0, &status) != 0");
1270 if (utext_caseCompare(&emptyUChar
, -1, &nullUTF8
, -1, 0, &status
) != 0) {
1271 errln("error: utext_caseCompare(&emptyUChar, -1, &nullUTF8, -1, 0, &status) != 0");
1273 if (utext_caseCompare(&emptyUTF8
, -1, &nullUChar
, -1, 0, &status
) != 0) {
1274 errln("error: utext_caseCompare(&emptyUTF8, -1, &nullUChar, -1, 0, &status) != 0");
1276 if (utext_caseCompare(&emptyUTF8
, -1, &nullUTF8
, -1, 0, &status
) != 0) {
1277 errln("error: utext_caseCompare(&emptyUTF8, -1, &nullUTF8, -1, 0, &status) != 0");
1279 if (utext_caseCompare(&nullUChar
, -1, &nullUTF8
, -1, 0, &status
) != 0) {
1280 errln("error: utext_caseCompare(&nullUChar, -1, &nullUTF8, -1, 0, &status) != 0");
1283 if (utext_caseCompareNativeLimit(&emptyUChar
, -1, &emptyUTF8
, -1, 0, &status
) != 0) {
1284 errln("error: utext_caseCompareNativeLimit(&emptyUChar, -1, &emptyUTF8, -1, 0, &status) != 0");
1286 if (utext_caseCompareNativeLimit(&emptyUChar
, -1, &nullUChar
, -1, 0, &status
) != 0) {
1287 errln("error: utext_caseCompareNativeLimit(&emptyUChar, -1, &nullUChar, -1, 0, &status) != 0");
1289 if (utext_caseCompareNativeLimit(&emptyUChar
, -1, &nullUTF8
, -1, 0, &status
) != 0) {
1290 errln("error: utext_caseCompareNativeLimit(&emptyUChar, -1, &nullUTF8, -1, 0, &status) != 0");
1292 if (utext_caseCompareNativeLimit(&emptyUTF8
, -1, &nullUChar
, -1, 0, &status
) != 0) {
1293 errln("error: utext_caseCompareNativeLimit(&emptyUTF8, -1, &nullUChar, -1, 0, &status) != 0");
1295 if (utext_caseCompareNativeLimit(&emptyUTF8
, -1, &nullUTF8
, -1, 0, &status
) != 0) {
1296 errln("error: utext_caseCompareNativeLimit(&emptyUTF8, -1, &nullUTF8, -1, 0, &status) != 0");
1298 if (utext_caseCompareNativeLimit(&nullUChar
, -1, &nullUTF8
, -1, 0, &status
) != 0) {
1299 errln("error: utext_caseCompareNativeLimit(&nullUChar, -1, &nullUTF8, -1, 0, &status) != 0");
1302 utext_close(&emptyUChar
);
1303 utext_close(&emptyUTF8
);
1304 utext_close(&nullUChar
);
1305 utext_close(&nullUTF8
);
1306 utext_close(&charText
);
1307 utext_close(&uniCharText
);
1314 // ErrorTest() Check various error and edge cases.
1316 void UTextTest::ErrorTest()
1318 // Close of an unitialized UText. Shouldn't blow up.
1321 memset(&ut
, 0, sizeof(UText
));
1326 // Double-close of a UText. Shouldn't blow up. UText should still be usable.
1328 UErrorCode status
= U_ZERO_ERROR
;
1329 UText ut
= UTEXT_INITIALIZER
;
1330 UnicodeString
s("Hello, World");
1331 UText
*ut2
= utext_openUnicodeString(&ut
, &s
, &status
);
1332 TEST_SUCCESS(status
);
1333 TEST_ASSERT(ut2
== &ut
);
1335 UText
*ut3
= utext_close(&ut
);
1336 TEST_ASSERT(ut3
== &ut
);
1338 UText
*ut4
= utext_close(&ut
);
1339 TEST_ASSERT(ut4
== &ut
);
1341 utext_openUnicodeString(&ut
, &s
, &status
);
1342 TEST_SUCCESS(status
);
1346 // Re-use of a UText, chaining through each of the types of UText
1347 // (If it doesn't blow up, and doesn't leak, it's probably working fine)
1349 UErrorCode status
= U_ZERO_ERROR
;
1350 UText ut
= UTEXT_INITIALIZER
;
1352 UnicodeString
s1("Hello, World");
1353 UChar s2
[] = {(UChar
)0x41, (UChar
)0x42, (UChar
)0};
1354 const char *s3
= "\x66\x67\x68";
1356 utp
= utext_openUnicodeString(&ut
, &s1
, &status
);
1357 TEST_SUCCESS(status
);
1358 TEST_ASSERT(utp
== &ut
);
1360 utp
= utext_openConstUnicodeString(&ut
, &s1
, &status
);
1361 TEST_SUCCESS(status
);
1362 TEST_ASSERT(utp
== &ut
);
1364 utp
= utext_openUTF8(&ut
, s3
, -1, &status
);
1365 TEST_SUCCESS(status
);
1366 TEST_ASSERT(utp
== &ut
);
1368 utp
= utext_openUChars(&ut
, s2
, -1, &status
);
1369 TEST_SUCCESS(status
);
1370 TEST_ASSERT(utp
== &ut
);
1372 utp
= utext_close(&ut
);
1373 TEST_ASSERT(utp
== &ut
);
1375 utp
= utext_openUnicodeString(&ut
, &s1
, &status
);
1376 TEST_SUCCESS(status
);
1377 TEST_ASSERT(utp
== &ut
);
1380 // Invalid parameters on open
1383 UErrorCode status
= U_ZERO_ERROR
;
1384 UText ut
= UTEXT_INITIALIZER
;
1386 utext_openUChars(&ut
, NULL
, 5, &status
);
1387 TEST_ASSERT(status
== U_ILLEGAL_ARGUMENT_ERROR
);
1389 status
= U_ZERO_ERROR
;
1390 utext_openUChars(&ut
, NULL
, -1, &status
);
1391 TEST_ASSERT(status
== U_ILLEGAL_ARGUMENT_ERROR
);
1393 status
= U_ZERO_ERROR
;
1394 utext_openUTF8(&ut
, NULL
, 4, &status
);
1395 TEST_ASSERT(status
== U_ILLEGAL_ARGUMENT_ERROR
);
1397 status
= U_ZERO_ERROR
;
1398 utext_openUTF8(&ut
, NULL
, -1, &status
);
1399 TEST_ASSERT(status
== U_ILLEGAL_ARGUMENT_ERROR
);
1403 // UTF-8 with malformed sequences.
1404 // These should come through as the Unicode replacement char, \ufffd
1407 UErrorCode status
= U_ZERO_ERROR
;
1409 const char *badUTF8
= "\x41\x81\x42\xf0\x81\x81\x43";
1412 ut
= utext_openUTF8(NULL
, badUTF8
, -1, &status
);
1413 TEST_SUCCESS(status
);
1414 c
= utext_char32At(ut
, 1);
1415 TEST_ASSERT(c
== 0xfffd);
1416 c
= utext_char32At(ut
, 3);
1417 TEST_ASSERT(c
== 0xfffd);
1418 c
= utext_char32At(ut
, 5);
1419 TEST_ASSERT(c
== 0xfffd);
1420 c
= utext_char32At(ut
, 6);
1421 TEST_ASSERT(c
== 0x43);
1424 int n
= utext_extract(ut
, 0, 9, buf
, 10, &status
);
1425 TEST_SUCCESS(status
);
1427 TEST_ASSERT(buf
[1] == 0xfffd);
1428 TEST_ASSERT(buf
[3] == 0xfffd);
1429 TEST_ASSERT(buf
[2] == 0x42);
1435 // isLengthExpensive - does it make the exptected transitions after
1436 // getting the length of a nul terminated string?
1439 UErrorCode status
= U_ZERO_ERROR
;
1440 UnicodeString
sa("Hello, this is a string");
1444 memset(sb
, 0x20, sizeof(sb
));
1447 UText
*uta
= utext_openUnicodeString(NULL
, &sa
, &status
);
1448 TEST_SUCCESS(status
);
1449 isExpensive
= utext_isLengthExpensive(uta
);
1450 TEST_ASSERT(isExpensive
== FALSE
);
1453 UText
*utb
= utext_openUChars(NULL
, sb
, -1, &status
);
1454 TEST_SUCCESS(status
);
1455 isExpensive
= utext_isLengthExpensive(utb
);
1456 TEST_ASSERT(isExpensive
== TRUE
);
1457 int64_t len
= utext_nativeLength(utb
);
1458 TEST_ASSERT(len
== 99);
1459 isExpensive
= utext_isLengthExpensive(utb
);
1460 TEST_ASSERT(isExpensive
== FALSE
);
1465 // Index to positions not on code point boundaries.
1468 const char *u8str
= "\xc8\x81\xe1\x82\x83\xf1\x84\x85\x86";
1469 int32_t startMap
[] = { 0, 0, 2, 2, 2, 5, 5, 5, 5, 9, 9};
1470 int32_t nextMap
[] = { 2, 2, 5, 5, 5, 9, 9, 9, 9, 9, 9};
1471 int32_t prevMap
[] = { 0, 0, 0, 0, 0, 2, 2, 2, 2, 5, 5};
1472 UChar32 c32Map
[] = {0x201, 0x201, 0x1083, 0x1083, 0x1083, 0x044146, 0x044146, 0x044146, 0x044146, -1, -1};
1473 UChar32 pr32Map
[] = { -1, -1, 0x201, 0x201, 0x201, 0x1083, 0x1083, 0x1083, 0x1083, 0x044146, 0x044146};
1475 // extractLen is the size, in UChars, of what will be extracted between index and index+1.
1476 // is zero when both index positions lie within the same code point.
1477 int32_t exLen
[] = { 0, 1, 0, 0, 1, 0, 0, 0, 2, 0, 0};
1480 UErrorCode status
= U_ZERO_ERROR
;
1481 UText
*ut
= utext_openUTF8(NULL
, u8str
, -1, &status
);
1482 TEST_SUCCESS(status
);
1486 int32_t startMapLimit
= sizeof(startMap
) / sizeof(int32_t);
1487 for (i
=0; i
<startMapLimit
; i
++) {
1488 utext_setNativeIndex(ut
, i
);
1489 int64_t cpIndex
= utext_getNativeIndex(ut
);
1490 TEST_ASSERT(cpIndex
== startMap
[i
]);
1491 cpIndex
= UTEXT_GETNATIVEINDEX(ut
);
1492 TEST_ASSERT(cpIndex
== startMap
[i
]);
1496 for (i
=0; i
<startMapLimit
; i
++) {
1497 UChar32 c32
= utext_char32At(ut
, i
);
1498 TEST_ASSERT(c32
== c32Map
[i
]);
1499 int64_t cpIndex
= utext_getNativeIndex(ut
);
1500 TEST_ASSERT(cpIndex
== startMap
[i
]);
1503 // Check utext_next32From
1504 for (i
=0; i
<startMapLimit
; i
++) {
1505 UChar32 c32
= utext_next32From(ut
, i
);
1506 TEST_ASSERT(c32
== c32Map
[i
]);
1507 int64_t cpIndex
= utext_getNativeIndex(ut
);
1508 TEST_ASSERT(cpIndex
== nextMap
[i
]);
1511 // check utext_previous32From
1512 for (i
=0; i
<startMapLimit
; i
++) {
1514 UChar32 c32
= utext_previous32From(ut
, i
);
1515 TEST_ASSERT(c32
== pr32Map
[i
]);
1516 int64_t cpIndex
= utext_getNativeIndex(ut
);
1517 TEST_ASSERT(cpIndex
== prevMap
[i
]);
1521 // Extract from i to i+1, which may be zero or one code points,
1522 // depending on whether the indices straddle a cp boundary.
1523 for (i
=0; i
<startMapLimit
; i
++) {
1525 status
= U_ZERO_ERROR
;
1526 int32_t extractedLen
= utext_extract(ut
, i
, i
+1, buf
, 3, &status
);
1527 TEST_SUCCESS(status
);
1528 TEST_ASSERT(extractedLen
== exLen
[i
]);
1529 if (extractedLen
> 0) {
1531 /* extractedLen-extractedLen == 0 is used to get around a compiler warning. */
1532 U16_GET(buf
, 0, extractedLen
-extractedLen
, extractedLen
, c32
);
1533 TEST_ASSERT(c32
== c32Map
[i
]);
1541 { // Similar test, with utf16 instead of utf8
1542 // TODO: merge the common parts of these tests.
1544 UnicodeString
u16str("\\u1000\\U00011000\\u2000\\U00022000", -1, US_INV
);
1545 int32_t startMap
[] ={ 0, 1, 1, 3, 4, 4, 6, 6};
1546 int32_t nextMap
[] = { 1, 3, 3, 4, 6, 6, 6, 6};
1547 int32_t prevMap
[] = { 0, 0, 0, 1, 3, 3, 4, 4};
1548 UChar32 c32Map
[] = {0x1000, 0x11000, 0x11000, 0x2000, 0x22000, 0x22000, -1, -1};
1549 UChar32 pr32Map
[] = { -1, 0x1000, 0x1000, 0x11000, 0x2000, 0x2000, 0x22000, 0x22000};
1550 int32_t exLen
[] = { 1, 0, 2, 1, 0, 2, 0, 0,};
1552 u16str
= u16str
.unescape();
1553 UErrorCode status
= U_ZERO_ERROR
;
1554 UText
*ut
= utext_openUnicodeString(NULL
, &u16str
, &status
);
1555 TEST_SUCCESS(status
);
1557 int32_t startMapLimit
= sizeof(startMap
) / sizeof(int32_t);
1559 for (i
=0; i
<startMapLimit
; i
++) {
1560 utext_setNativeIndex(ut
, i
);
1561 int64_t cpIndex
= utext_getNativeIndex(ut
);
1562 TEST_ASSERT(cpIndex
== startMap
[i
]);
1566 for (i
=0; i
<startMapLimit
; i
++) {
1567 UChar32 c32
= utext_char32At(ut
, i
);
1568 TEST_ASSERT(c32
== c32Map
[i
]);
1569 int64_t cpIndex
= utext_getNativeIndex(ut
);
1570 TEST_ASSERT(cpIndex
== startMap
[i
]);
1573 // Check utext_next32From
1574 for (i
=0; i
<startMapLimit
; i
++) {
1575 UChar32 c32
= utext_next32From(ut
, i
);
1576 TEST_ASSERT(c32
== c32Map
[i
]);
1577 int64_t cpIndex
= utext_getNativeIndex(ut
);
1578 TEST_ASSERT(cpIndex
== nextMap
[i
]);
1581 // check utext_previous32From
1582 for (i
=0; i
<startMapLimit
; i
++) {
1583 UChar32 c32
= utext_previous32From(ut
, i
);
1584 TEST_ASSERT(c32
== pr32Map
[i
]);
1585 int64_t cpIndex
= utext_getNativeIndex(ut
);
1586 TEST_ASSERT(cpIndex
== prevMap
[i
]);
1590 // Extract from i to i+1, which may be zero or one code points,
1591 // depending on whether the indices straddle a cp boundary.
1592 for (i
=0; i
<startMapLimit
; i
++) {
1594 status
= U_ZERO_ERROR
;
1595 int32_t extractedLen
= utext_extract(ut
, i
, i
+1, buf
, 3, &status
);
1596 TEST_SUCCESS(status
);
1597 TEST_ASSERT(extractedLen
== exLen
[i
]);
1598 if (extractedLen
> 0) {
1600 /* extractedLen-extractedLen == 0 is used to get around a compiler warning. */
1601 U16_GET(buf
, 0, extractedLen
-extractedLen
, extractedLen
, c32
);
1602 TEST_ASSERT(c32
== c32Map
[i
]);
1609 { // Similar test, with UText over Replaceable
1610 // TODO: merge the common parts of these tests.
1612 UnicodeString
u16str("\\u1000\\U00011000\\u2000\\U00022000", -1, US_INV
);
1613 int32_t startMap
[] ={ 0, 1, 1, 3, 4, 4, 6, 6};
1614 int32_t nextMap
[] = { 1, 3, 3, 4, 6, 6, 6, 6};
1615 int32_t prevMap
[] = { 0, 0, 0, 1, 3, 3, 4, 4};
1616 UChar32 c32Map
[] = {0x1000, 0x11000, 0x11000, 0x2000, 0x22000, 0x22000, -1, -1};
1617 UChar32 pr32Map
[] = { -1, 0x1000, 0x1000, 0x11000, 0x2000, 0x2000, 0x22000, 0x22000};
1618 int32_t exLen
[] = { 1, 0, 2, 1, 0, 2, 0, 0,};
1620 u16str
= u16str
.unescape();
1621 UErrorCode status
= U_ZERO_ERROR
;
1622 UText
*ut
= utext_openReplaceable(NULL
, &u16str
, &status
);
1623 TEST_SUCCESS(status
);
1625 int32_t startMapLimit
= sizeof(startMap
) / sizeof(int32_t);
1627 for (i
=0; i
<startMapLimit
; i
++) {
1628 utext_setNativeIndex(ut
, i
);
1629 int64_t cpIndex
= utext_getNativeIndex(ut
);
1630 TEST_ASSERT(cpIndex
== startMap
[i
]);
1634 for (i
=0; i
<startMapLimit
; i
++) {
1635 UChar32 c32
= utext_char32At(ut
, i
);
1636 TEST_ASSERT(c32
== c32Map
[i
]);
1637 int64_t cpIndex
= utext_getNativeIndex(ut
);
1638 TEST_ASSERT(cpIndex
== startMap
[i
]);
1641 // Check utext_next32From
1642 for (i
=0; i
<startMapLimit
; i
++) {
1643 UChar32 c32
= utext_next32From(ut
, i
);
1644 TEST_ASSERT(c32
== c32Map
[i
]);
1645 int64_t cpIndex
= utext_getNativeIndex(ut
);
1646 TEST_ASSERT(cpIndex
== nextMap
[i
]);
1649 // check utext_previous32From
1650 for (i
=0; i
<startMapLimit
; i
++) {
1651 UChar32 c32
= utext_previous32From(ut
, i
);
1652 TEST_ASSERT(c32
== pr32Map
[i
]);
1653 int64_t cpIndex
= utext_getNativeIndex(ut
);
1654 TEST_ASSERT(cpIndex
== prevMap
[i
]);
1658 // Extract from i to i+1, which may be zero or one code points,
1659 // depending on whether the indices straddle a cp boundary.
1660 for (i
=0; i
<startMapLimit
; i
++) {
1662 status
= U_ZERO_ERROR
;
1663 int32_t extractedLen
= utext_extract(ut
, i
, i
+1, buf
, 3, &status
);
1664 TEST_SUCCESS(status
);
1665 TEST_ASSERT(extractedLen
== exLen
[i
]);
1666 if (extractedLen
> 0) {
1668 /* extractedLen-extractedLen == 0 is used to get around a compiler warning. */
1669 U16_GET(buf
, 0, extractedLen
-extractedLen
, extractedLen
, c32
);
1670 TEST_ASSERT(c32
== c32Map
[i
]);
1679 void UTextTest::FreezeTest() {
1680 // Check isWritable() and freeze() behavior.
1683 UnicodeString
ustr("Hello, World.");
1684 const char u8str
[] = {char(0x31), (char)0x32, (char)0x33, 0};
1685 const UChar u16str
[] = {(UChar
)0x31, (UChar
)0x32, (UChar
)0x44, 0};
1687 UErrorCode status
= U_ZERO_ERROR
;
1691 ut
= utext_openUTF8(ut
, u8str
, -1, &status
);
1692 TEST_SUCCESS(status
);
1693 UBool writable
= utext_isWritable(ut
);
1694 TEST_ASSERT(writable
== FALSE
);
1695 utext_copy(ut
, 1, 2, 0, TRUE
, &status
);
1696 TEST_ASSERT(status
== U_NO_WRITE_PERMISSION
);
1698 status
= U_ZERO_ERROR
;
1699 ut
= utext_openUChars(ut
, u16str
, -1, &status
);
1700 TEST_SUCCESS(status
);
1701 writable
= utext_isWritable(ut
);
1702 TEST_ASSERT(writable
== FALSE
);
1703 utext_copy(ut
, 1, 2, 0, TRUE
, &status
);
1704 TEST_ASSERT(status
== U_NO_WRITE_PERMISSION
);
1706 status
= U_ZERO_ERROR
;
1707 ut
= utext_openUnicodeString(ut
, &ustr
, &status
);
1708 TEST_SUCCESS(status
);
1709 writable
= utext_isWritable(ut
);
1710 TEST_ASSERT(writable
== TRUE
);
1712 writable
= utext_isWritable(ut
);
1713 TEST_ASSERT(writable
== FALSE
);
1714 utext_copy(ut
, 1, 2, 0, TRUE
, &status
);
1715 TEST_ASSERT(status
== U_NO_WRITE_PERMISSION
);
1717 status
= U_ZERO_ERROR
;
1718 ut
= utext_openUnicodeString(ut
, &ustr
, &status
);
1719 TEST_SUCCESS(status
);
1720 ut2
= utext_clone(ut2
, ut
, FALSE
, FALSE
, &status
); // clone with readonly = false
1721 TEST_SUCCESS(status
);
1722 writable
= utext_isWritable(ut2
);
1723 TEST_ASSERT(writable
== TRUE
);
1724 ut2
= utext_clone(ut2
, ut
, FALSE
, TRUE
, &status
); // clone with readonly = true
1725 TEST_SUCCESS(status
);
1726 writable
= utext_isWritable(ut2
);
1727 TEST_ASSERT(writable
== FALSE
);
1728 utext_copy(ut2
, 1, 2, 0, TRUE
, &status
);
1729 TEST_ASSERT(status
== U_NO_WRITE_PERMISSION
);
1731 status
= U_ZERO_ERROR
;
1732 ut
= utext_openConstUnicodeString(ut
, (const UnicodeString
*)&ustr
, &status
);
1733 TEST_SUCCESS(status
);
1734 writable
= utext_isWritable(ut
);
1735 TEST_ASSERT(writable
== FALSE
);
1736 utext_copy(ut
, 1, 2, 0, TRUE
, &status
);
1737 TEST_ASSERT(status
== U_NO_WRITE_PERMISSION
);
1739 // Deep Clone of a frozen UText should re-enable writing in the copy.
1740 status
= U_ZERO_ERROR
;
1741 ut
= utext_openUnicodeString(ut
, &ustr
, &status
);
1742 TEST_SUCCESS(status
);
1744 ut2
= utext_clone(ut2
, ut
, TRUE
, FALSE
, &status
); // deep clone
1745 TEST_SUCCESS(status
);
1746 writable
= utext_isWritable(ut2
);
1747 TEST_ASSERT(writable
== TRUE
);
1750 // Deep clone of a frozen UText, where the base type is intrinsically non-writable,
1751 // should NOT enable writing in the copy.
1752 status
= U_ZERO_ERROR
;
1753 ut
= utext_openUChars(ut
, u16str
, -1, &status
);
1754 TEST_SUCCESS(status
);
1756 ut2
= utext_clone(ut2
, ut
, TRUE
, FALSE
, &status
); // deep clone
1757 TEST_SUCCESS(status
);
1758 writable
= utext_isWritable(ut2
);
1759 TEST_ASSERT(writable
== FALSE
);
1769 // A UText type that works with a chunk size of 1.
1770 // Intended to test for edge cases.
1771 // Input comes from a UnicodeString.
1773 // ut.b the character. Put into both halves.
1777 static UBool U_CALLCONV
1778 fragTextAccess(UText
*ut
, int64_t index
, UBool forward
) {
1779 const UnicodeString
*us
= (const UnicodeString
*)ut
->context
;
1781 int32_t length
= us
->length();
1782 if (forward
&& index
>=0 && index
<length
) {
1783 c
= us
->charAt((int32_t)index
);
1785 ut
->chunkOffset
= 0;
1786 ut
->chunkLength
= 1;
1787 ut
->chunkNativeStart
= index
;
1788 ut
->chunkNativeLimit
= index
+1;
1791 if (!forward
&& index
>0 && index
<=length
) {
1792 c
= us
->charAt((int32_t)index
-1);
1794 ut
->chunkOffset
= 1;
1795 ut
->chunkLength
= 1;
1796 ut
->chunkNativeStart
= index
-1;
1797 ut
->chunkNativeLimit
= index
;
1801 ut
->chunkOffset
= 0;
1802 ut
->chunkLength
= 0;
1804 ut
->chunkNativeStart
= 0;
1805 ut
->chunkNativeLimit
= 0;
1807 ut
->chunkNativeStart
= length
;
1808 ut
->chunkNativeLimit
= length
;
1813 // Function table to be used with this fragmented text provider.
1814 // Initialized in the open function.
1815 static UTextFuncs fragmentFuncs
;
1817 // Clone function for fragmented text provider.
1818 // Didn't really want to provide this, but it's easier to provide it than to keep it
1819 // out of the tests.
1822 cloneFragmentedUnicodeString(UText
*dest
, const UText
*src
, UBool deep
, UErrorCode
*status
) {
1823 if (U_FAILURE(*status
)) {
1827 *status
= U_UNSUPPORTED_ERROR
;
1830 dest
= utext_openUnicodeString(dest
, (UnicodeString
*)src
->context
, status
);
1831 utext_setNativeIndex(dest
, utext_getNativeIndex(src
));
1837 // Open function for the fragmented text provider.
1839 openFragmentedUnicodeString(UText
*ut
, UnicodeString
*s
, UErrorCode
*status
) {
1840 ut
= utext_openUnicodeString(ut
, s
, status
);
1841 if (U_FAILURE(*status
)) {
1845 // Copy of the function table from the stock UnicodeString UText,
1846 // and replace the entry for the access function.
1847 memcpy(&fragmentFuncs
, ut
->pFuncs
, sizeof(fragmentFuncs
));
1848 fragmentFuncs
.access
= fragTextAccess
;
1849 fragmentFuncs
.clone
= cloneFragmentedUnicodeString
;
1850 ut
->pFuncs
= &fragmentFuncs
;
1852 ut
->chunkContents
= (UChar
*)&ut
->b
;
1853 ut
->pFuncs
->access(ut
, 0, TRUE
);
1857 // Regression test for Ticket 5560
1858 // Clone fails to update chunkContentPointer in the cloned copy.
1859 // This is only an issue for UText types that work in a local buffer,
1860 // (UTF-8 wrapper, for example)
1863 // 1. Create an inital UText
1864 // 2. Deep clone it. Contents should match original.
1865 // 3. Reset original to something different.
1866 // 4. Check that clone contents did not change.
1868 void UTextTest::Ticket5560() {
1869 /* The following two strings are in UTF-8 even on EBCDIC platforms. */
1870 static const char s1
[] = {0x41,0x42,0x43,0x44,0x45,0x46,0}; /* "ABCDEF" */
1871 static const char s2
[] = {0x31,0x32,0x33,0x34,0x35,0x36,0}; /* "123456" */
1872 UErrorCode status
= U_ZERO_ERROR
;
1874 UText ut1
= UTEXT_INITIALIZER
;
1875 UText ut2
= UTEXT_INITIALIZER
;
1877 utext_openUTF8(&ut1
, s1
, -1, &status
);
1878 UChar c
= utext_next32(&ut1
);
1879 TEST_ASSERT(c
== 0x41); // c == 'A'
1881 utext_clone(&ut2
, &ut1
, TRUE
, FALSE
, &status
);
1882 TEST_SUCCESS(status
);
1883 c
= utext_next32(&ut2
);
1884 TEST_ASSERT(c
== 0x42); // c == 'B'
1885 c
= utext_next32(&ut1
);
1886 TEST_ASSERT(c
== 0x42); // c == 'B'
1888 utext_openUTF8(&ut1
, s2
, -1, &status
);
1889 c
= utext_next32(&ut1
);
1890 TEST_ASSERT(c
== 0x31); // c == '1'
1891 c
= utext_next32(&ut2
);
1892 TEST_ASSERT(c
== 0x43); // c == 'C'
1899 // Test for Ticket 6847
1901 void UTextTest::Ticket6847() {
1902 const int STRLEN
= 90;
1904 u_memset(s
, 0x41, STRLEN
);
1907 UErrorCode status
= U_ZERO_ERROR
;
1908 UText
*ut
= utext_openUChars(NULL
, s
, -1, &status
);
1910 utext_setNativeIndex(ut
, 0);
1913 int32_t nativeIndex
= UTEXT_GETNATIVEINDEX(ut
);
1914 TEST_ASSERT(nativeIndex
== 0);
1915 while ((c
= utext_next32(ut
)) != U_SENTINEL
) {
1916 TEST_ASSERT(c
== 0x41);
1917 TEST_ASSERT(count
< STRLEN
);
1918 if (count
>= STRLEN
) {
1922 nativeIndex
= UTEXT_GETNATIVEINDEX(ut
);
1923 TEST_ASSERT(nativeIndex
== count
);
1925 TEST_ASSERT(count
== STRLEN
);
1926 nativeIndex
= UTEXT_GETNATIVEINDEX(ut
);
1927 TEST_ASSERT(nativeIndex
== STRLEN
);