]> git.saurik.com Git - apple/icu.git/blob - icuSources/test/intltest/utxttest.cpp
ICU-400.42.tar.gz
[apple/icu.git] / icuSources / test / intltest / utxttest.cpp
1 /********************************************************************
2 * COPYRIGHT:
3 * Copyright (c) 2005-2009, International Business Machines Corporation and
4 * others. All Rights Reserved.
5 ********************************************************************/
6 /************************************************************************
7 * Tests for the UText and UTextIterator text abstraction classses
8 *
9 ************************************************************************/
10
11 #include "unicode/utypes.h"
12
13 #include <string.h>
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <unicode/utext.h>
17 #include <unicode/utf8.h>
18 #include <unicode/ustring.h>
19 #include <unicode/uchriter.h>
20 #include "utxttest.h"
21
22 static UBool gFailed = FALSE;
23 static int gTestNum = 0;
24
25 // Forward decl
26 UText *openFragmentedUnicodeString(UText *ut, UnicodeString *s, UErrorCode *status);
27
28 #define TEST_ASSERT(x) \
29 { if ((x)==FALSE) {errln("Test #%d failure in file %s at line %d\n", gTestNum, __FILE__, __LINE__);\
30 gFailed = TRUE;\
31 }}
32
33
34 #define TEST_SUCCESS(status) \
35 { if (U_FAILURE(status)) {errln("Test #%d failure in file %s at line %d. Error = \"%s\"\n", \
36 gTestNum, __FILE__, __LINE__, u_errorName(status)); \
37 gFailed = TRUE;\
38 }}
39
40 UTextTest::UTextTest() {
41 }
42
43 UTextTest::~UTextTest() {
44 }
45
46
47 void
48 UTextTest::runIndexedTest(int32_t index, UBool exec,
49 const char* &name, char* /*par*/) {
50 switch (index) {
51 case 0: name = "TextTest";
52 if (exec) TextTest(); break;
53 case 1: name = "ErrorTest";
54 if (exec) ErrorTest(); break;
55 case 2: name = "FreezeTest";
56 if (exec) FreezeTest(); break;
57 case 3: name = "Ticket5560";
58 if (exec) Ticket5560(); break;
59 case 4: name = "Ticket6847";
60 if (exec) Ticket6847(); break;
61 default: name = ""; break;
62 }
63 }
64
65 //
66 // Quick and dirty random number generator.
67 // (don't use library so that results are portable.
68 static uint32_t m_seed = 1;
69 static uint32_t m_rand()
70 {
71 m_seed = m_seed * 1103515245 + 12345;
72 return (uint32_t)(m_seed/65536) % 32768;
73 }
74
75
76 //
77 // TextTest()
78 //
79 // Top Level function for UText testing.
80 // Specifies the strings to be tested, with the acutal testing itself
81 // being carried out in another function, TestString().
82 //
83 void UTextTest::TextTest() {
84 int32_t i, j;
85
86 TestString("abcd\\U00010001xyz");
87 TestString("");
88
89 // Supplementary chars at start or end
90 TestString("\\U00010001");
91 TestString("abc\\U00010001");
92 TestString("\\U00010001abc");
93
94 // Test simple strings of lengths 1 to 60, looking for glitches at buffer boundaries
95 UnicodeString s;
96 for (i=1; i<60; i++) {
97 s.truncate(0);
98 for (j=0; j<i; j++) {
99 if (j+0x30 == 0x5c) {
100 // backslash. Needs to be escaped
101 s.append((UChar)0x5c);
102 }
103 s.append(UChar(j+0x30));
104 }
105 TestString(s);
106 }
107
108 // Test strings with odd-aligned supplementary chars,
109 // looking for glitches at buffer boundaries
110 for (i=1; i<60; i++) {
111 s.truncate(0);
112 s.append((UChar)0x41);
113 for (j=0; j<i; j++) {
114 s.append(UChar32(j+0x11000));
115 }
116 TestString(s);
117 }
118
119 // String of chars of randomly varying size in utf-8 representation.
120 // Exercise the mapping, and the varying sized buffer.
121 //
122 s.truncate(0);
123 UChar32 c1 = 0;
124 UChar32 c2 = 0x100;
125 UChar32 c3 = 0xa000;
126 UChar32 c4 = 0x11000;
127 for (i=0; i<1000; i++) {
128 int len8 = m_rand()%4 + 1;
129 switch (len8) {
130 case 1:
131 c1 = (c1+1)%0x80;
132 // don't put 0 into string (0 terminated strings for some tests)
133 // don't put '\', will cause unescape() to fail.
134 if (c1==0x5c || c1==0) {
135 c1++;
136 }
137 s.append(c1);
138 break;
139 case 2:
140 s.append(c2++);
141 break;
142 case 3:
143 s.append(c3++);
144 break;
145 case 4:
146 s.append(c4++);
147 break;
148 }
149 }
150 TestString(s);
151 }
152
153
154 //
155 // TestString() Run a suite of UText tests on a string.
156 // The test string is unescaped before use.
157 //
158 void UTextTest::TestString(const UnicodeString &s) {
159 int32_t i;
160 int32_t j;
161 UChar32 c;
162 int32_t cpCount = 0;
163 UErrorCode status = U_ZERO_ERROR;
164 UText *ut = NULL;
165 int32_t saLen;
166
167 UnicodeString sa = s.unescape();
168 saLen = sa.length();
169
170 //
171 // Build up a mapping between code points and UTF-16 code unit indexes.
172 //
173 m *cpMap = new m[sa.length() + 1];
174 j = 0;
175 for (i=0; i<sa.length(); i=sa.moveIndex32(i, 1)) {
176 c = sa.char32At(i);
177 cpMap[j].nativeIdx = i;
178 cpMap[j].cp = c;
179 j++;
180 cpCount++;
181 }
182 cpMap[j].nativeIdx = i; // position following the last char in utf-16 string.
183
184
185 // UChar * test, null terminated
186 status = U_ZERO_ERROR;
187 UChar *buf = new UChar[saLen+1];
188 sa.extract(buf, saLen+1, status);
189 TEST_SUCCESS(status);
190 ut = utext_openUChars(NULL, buf, -1, &status);
191 TEST_SUCCESS(status);
192 TestAccess(sa, ut, cpCount, cpMap);
193 utext_close(ut);
194 delete [] buf;
195
196 // UChar * test, with length
197 status = U_ZERO_ERROR;
198 buf = new UChar[saLen+1];
199 sa.extract(buf, saLen+1, status);
200 TEST_SUCCESS(status);
201 ut = utext_openUChars(NULL, buf, saLen, &status);
202 TEST_SUCCESS(status);
203 TestAccess(sa, ut, cpCount, cpMap);
204 utext_close(ut);
205 delete [] buf;
206
207
208 // UnicodeString test
209 status = U_ZERO_ERROR;
210 ut = utext_openUnicodeString(NULL, &sa, &status);
211 TEST_SUCCESS(status);
212 TestAccess(sa, ut, cpCount, cpMap);
213 TestCMR(sa, ut, cpCount, cpMap, cpMap);
214 utext_close(ut);
215
216
217 // Const UnicodeString test
218 status = U_ZERO_ERROR;
219 ut = utext_openConstUnicodeString(NULL, &sa, &status);
220 TEST_SUCCESS(status);
221 TestAccess(sa, ut, cpCount, cpMap);
222 utext_close(ut);
223
224
225 // Replaceable test. (UnicodeString inherits Replaceable)
226 status = U_ZERO_ERROR;
227 ut = utext_openReplaceable(NULL, &sa, &status);
228 TEST_SUCCESS(status);
229 TestAccess(sa, ut, cpCount, cpMap);
230 TestCMR(sa, ut, cpCount, cpMap, cpMap);
231 utext_close(ut);
232
233 // Character Iterator Tests
234 status = U_ZERO_ERROR;
235 const UChar *cbuf = sa.getBuffer();
236 CharacterIterator *ci = new UCharCharacterIterator(cbuf, saLen, status);
237 TEST_SUCCESS(status);
238 ut = utext_openCharacterIterator(NULL, ci, &status);
239 TEST_SUCCESS(status);
240 TestAccess(sa, ut, cpCount, cpMap);
241 utext_close(ut);
242 delete ci;
243
244
245 // Fragmented UnicodeString (Chunk size of one)
246 //
247 status = U_ZERO_ERROR;
248 ut = openFragmentedUnicodeString(NULL, &sa, &status);
249 TEST_SUCCESS(status);
250 TestAccess(sa, ut, cpCount, cpMap);
251 utext_close(ut);
252
253 //
254 // UTF-8 test
255 //
256
257 // Convert the test string from UnicodeString to (char *) in utf-8 format
258 int32_t u8Len = sa.extract(0, sa.length(), NULL, 0, "utf-8");
259 char *u8String = new char[u8Len + 1];
260 sa.extract(0, sa.length(), u8String, u8Len+1, "utf-8");
261
262 // Build up the map of code point indices in the utf-8 string
263 m * u8Map = new m[sa.length() + 1];
264 i = 0; // native utf-8 index
265 for (j=0; j<cpCount ; j++) { // code point number
266 u8Map[j].nativeIdx = i;
267 U8_NEXT(u8String, i, u8Len, c)
268 u8Map[j].cp = c;
269 }
270 u8Map[cpCount].nativeIdx = u8Len; // position following the last char in utf-8 string.
271
272 // Do the test itself
273 status = U_ZERO_ERROR;
274 ut = utext_openUTF8(NULL, u8String, -1, &status);
275 TEST_SUCCESS(status);
276 TestAccess(sa, ut, cpCount, u8Map);
277 utext_close(ut);
278
279
280
281 delete []cpMap;
282 delete []u8Map;
283 delete []u8String;
284 }
285
286 // TestCMR test Copy, Move and Replace operations.
287 // us UnicodeString containing the test text.
288 // ut UText containing the same test text.
289 // cpCount number of code points in the test text.
290 // nativeMap Mapping from code points to native indexes for the UText.
291 // u16Map Mapping from code points to UTF-16 indexes, for use with the UnicodeString.
292 //
293 // This function runs a whole series of opertions on each incoming UText.
294 // The UText is deep-cloned prior to each operation, so that the original UText remains unchanged.
295 //
296 void UTextTest::TestCMR(const UnicodeString &us, UText *ut, int cpCount, m *nativeMap, m *u16Map) {
297 TEST_ASSERT(utext_isWritable(ut) == TRUE);
298
299 int srcLengthType; // Loop variables for selecting the postion and length
300 int srcPosType; // of the block to operate on within the source text.
301 int destPosType;
302
303 int srcIndex = 0; // Code Point indexes of the block to operate on for
304 int srcLength = 0; // a specific test.
305
306 int destIndex = 0; // Code point index of the destination for a copy/move test.
307
308 int32_t nativeStart = 0; // Native unit indexes for a test.
309 int32_t nativeLimit = 0;
310 int32_t nativeDest = 0;
311
312 int32_t u16Start = 0; // UTF-16 indexes for a test.
313 int32_t u16Limit = 0; // used when performing the same operation in a Unicode String
314 int32_t u16Dest = 0;
315
316 // Iterate over a whole series of source index, length and a target indexes.
317 // This is done with code point indexes; these will be later translated to native
318 // indexes using the cpMap.
319 for (srcLengthType=1; srcLengthType<=3; srcLengthType++) {
320 switch (srcLengthType) {
321 case 1: srcLength = 1; break;
322 case 2: srcLength = 5; break;
323 case 3: srcLength = cpCount / 3;
324 }
325 for (srcPosType=1; srcPosType<=5; srcPosType++) {
326 switch (srcPosType) {
327 case 1: srcIndex = 0; break;
328 case 2: srcIndex = 1; break;
329 case 3: srcIndex = cpCount - srcLength; break;
330 case 4: srcIndex = cpCount - srcLength - 1; break;
331 case 5: srcIndex = cpCount / 2; break;
332 }
333 if (srcIndex < 0 || srcIndex + srcLength > cpCount) {
334 // filter out bogus test cases -
335 // those with a source range that falls of an edge of the string.
336 continue;
337 }
338
339 //
340 // Copy and move tests.
341 // iterate over a variety of destination positions.
342 //
343 for (destPosType=1; destPosType<=4; destPosType++) {
344 switch (destPosType) {
345 case 1: destIndex = 0; break;
346 case 2: destIndex = 1; break;
347 case 3: destIndex = srcIndex - 1; break;
348 case 4: destIndex = srcIndex + srcLength + 1; break;
349 case 5: destIndex = cpCount-1; break;
350 case 6: destIndex = cpCount; break;
351 }
352 if (destIndex<0 || destIndex>cpCount) {
353 // filter out bogus test cases.
354 continue;
355 }
356
357 nativeStart = nativeMap[srcIndex].nativeIdx;
358 nativeLimit = nativeMap[srcIndex+srcLength].nativeIdx;
359 nativeDest = nativeMap[destIndex].nativeIdx;
360
361 u16Start = u16Map[srcIndex].nativeIdx;
362 u16Limit = u16Map[srcIndex+srcLength].nativeIdx;
363 u16Dest = u16Map[destIndex].nativeIdx;
364
365 gFailed = FALSE;
366 TestCopyMove(us, ut, FALSE,
367 nativeStart, nativeLimit, nativeDest,
368 u16Start, u16Limit, u16Dest);
369
370 TestCopyMove(us, ut, TRUE,
371 nativeStart, nativeLimit, nativeDest,
372 u16Start, u16Limit, u16Dest);
373
374 if (gFailed) {
375 return;
376 }
377 }
378
379 //
380 // Replace tests.
381 //
382 UnicodeString fullRepString("This is an arbitrary string that will be used as replacement text");
383 for (int32_t replStrLen=0; replStrLen<20; replStrLen++) {
384 UnicodeString repStr(fullRepString, 0, replStrLen);
385 TestReplace(us, ut,
386 nativeStart, nativeLimit,
387 u16Start, u16Limit,
388 repStr);
389 if (gFailed) {
390 return;
391 }
392 }
393
394 }
395 }
396
397 }
398
399 //
400 // TestCopyMove run a single test case for utext_copy.
401 // Test cases are created in TestCMR and dispatched here for execution.
402 //
403 void UTextTest::TestCopyMove(const UnicodeString &us, UText *ut, UBool move,
404 int32_t nativeStart, int32_t nativeLimit, int32_t nativeDest,
405 int32_t u16Start, int32_t u16Limit, int32_t u16Dest)
406 {
407 UErrorCode status = U_ZERO_ERROR;
408 UText *targetUT = NULL;
409 gTestNum++;
410 gFailed = FALSE;
411
412 //
413 // clone the UText. The test will be run in the cloned copy
414 // so that we don't alter the original.
415 //
416 targetUT = utext_clone(NULL, ut, TRUE, FALSE, &status);
417 TEST_SUCCESS(status);
418 UnicodeString targetUS(us); // And copy the reference string.
419
420 // do the test operation first in the reference
421 targetUS.copy(u16Start, u16Limit, u16Dest);
422 if (move) {
423 // delete out the source range.
424 if (u16Limit < u16Dest) {
425 targetUS.removeBetween(u16Start, u16Limit);
426 } else {
427 int32_t amtCopied = u16Limit - u16Start;
428 targetUS.removeBetween(u16Start+amtCopied, u16Limit+amtCopied);
429 }
430 }
431
432 // Do the same operation in the UText under test
433 utext_copy(targetUT, nativeStart, nativeLimit, nativeDest, move, &status);
434 if (nativeDest > nativeStart && nativeDest < nativeLimit) {
435 TEST_ASSERT(status == U_INDEX_OUTOFBOUNDS_ERROR);
436 } else {
437 TEST_SUCCESS(status);
438
439 // Compare the results of the two parallel tests
440 int32_t usi = 0; // UnicodeString postion, utf-16 index.
441 int64_t uti = 0; // UText position, native index.
442 int32_t cpi; // char32 position (code point index)
443 UChar32 usc; // code point from Unicode String
444 UChar32 utc; // code point from UText
445 utext_setNativeIndex(targetUT, 0);
446 for (cpi=0; ; cpi++) {
447 usc = targetUS.char32At(usi);
448 utc = utext_next32(targetUT);
449 if (utc < 0) {
450 break;
451 }
452 TEST_ASSERT(uti == usi);
453 TEST_ASSERT(utc == usc);
454 usi = targetUS.moveIndex32(usi, 1);
455 uti = utext_getNativeIndex(targetUT);
456 if (gFailed) {
457 goto cleanupAndReturn;
458 }
459 }
460 int64_t expectedNativeLength = utext_nativeLength(ut);
461 if (move == FALSE) {
462 expectedNativeLength += nativeLimit - nativeStart;
463 }
464 uti = utext_getNativeIndex(targetUT);
465 TEST_ASSERT(uti == expectedNativeLength);
466 }
467
468 cleanupAndReturn:
469 utext_close(targetUT);
470 }
471
472
473 //
474 // TestReplace Test a single Replace operation.
475 //
476 void UTextTest::TestReplace(
477 const UnicodeString &us, // reference UnicodeString in which to do the replace
478 UText *ut, // UnicodeText object under test.
479 int32_t nativeStart, // Range to be replaced, in UText native units.
480 int32_t nativeLimit,
481 int32_t u16Start, // Range to be replaced, in UTF-16 units
482 int32_t u16Limit, // for use in the reference UnicodeString.
483 const UnicodeString &repStr) // The replacement string
484 {
485 UErrorCode status = U_ZERO_ERROR;
486 UText *targetUT = NULL;
487 gTestNum++;
488 gFailed = FALSE;
489
490 //
491 // clone the target UText. The test will be run in the cloned copy
492 // so that we don't alter the original.
493 //
494 targetUT = utext_clone(NULL, ut, TRUE, FALSE, &status);
495 TEST_SUCCESS(status);
496 UnicodeString targetUS(us); // And copy the reference string.
497
498 //
499 // Do the replace operation in the Unicode String, to
500 // produce a reference result.
501 //
502 targetUS.replace(u16Start, u16Limit-u16Start, repStr);
503
504 //
505 // Do the replace on the UText under test
506 //
507 const UChar *rs = repStr.getBuffer();
508 int32_t rsLen = repStr.length();
509 int32_t actualDelta = utext_replace(targetUT, nativeStart, nativeLimit, rs, rsLen, &status);
510 int32_t expectedDelta = repStr.length() - (nativeLimit - nativeStart);
511 TEST_ASSERT(actualDelta == expectedDelta);
512
513 //
514 // Compare the results
515 //
516 int32_t usi = 0; // UnicodeString postion, utf-16 index.
517 int64_t uti = 0; // UText position, native index.
518 int32_t cpi; // char32 position (code point index)
519 UChar32 usc; // code point from Unicode String
520 UChar32 utc; // code point from UText
521 int64_t expectedNativeLength = 0;
522 utext_setNativeIndex(targetUT, 0);
523 for (cpi=0; ; cpi++) {
524 usc = targetUS.char32At(usi);
525 utc = utext_next32(targetUT);
526 if (utc < 0) {
527 break;
528 }
529 TEST_ASSERT(uti == usi);
530 TEST_ASSERT(utc == usc);
531 usi = targetUS.moveIndex32(usi, 1);
532 uti = utext_getNativeIndex(targetUT);
533 if (gFailed) {
534 goto cleanupAndReturn;
535 }
536 }
537 expectedNativeLength = utext_nativeLength(ut) + expectedDelta;
538 uti = utext_getNativeIndex(targetUT);
539 TEST_ASSERT(uti == expectedNativeLength);
540
541 cleanupAndReturn:
542 utext_close(targetUT);
543 }
544
545 //
546 // TestAccess Test the read only access functions on a UText, including cloning.
547 // The text is accessed in a variety of ways, and compared with
548 // the reference UnicodeString.
549 //
550 void UTextTest::TestAccess(const UnicodeString &us, UText *ut, int cpCount, m *cpMap) {
551 // Run the standard tests on the caller-supplied UText.
552 TestAccessNoClone(us, ut, cpCount, cpMap);
553
554 // Re-run tests on a shallow clone.
555 utext_setNativeIndex(ut, 0);
556 UErrorCode status = U_ZERO_ERROR;
557 UText *shallowClone = utext_clone(NULL, ut, FALSE /*deep*/, FALSE /*readOnly*/, &status);
558 TEST_SUCCESS(status);
559 TestAccessNoClone(us, shallowClone, cpCount, cpMap);
560
561 //
562 // Rerun again on a deep clone.
563 // Note that text providers are not required to provide deep cloning,
564 // so unsupported errors are ignored.
565 //
566 status = U_ZERO_ERROR;
567 utext_setNativeIndex(shallowClone, 0);
568 UText *deepClone = utext_clone(NULL, shallowClone, TRUE, FALSE, &status);
569 utext_close(shallowClone);
570 if (status != U_UNSUPPORTED_ERROR) {
571 TEST_SUCCESS(status);
572 TestAccessNoClone(us, deepClone, cpCount, cpMap);
573 }
574 utext_close(deepClone);
575 }
576
577
578 //
579 // TestAccessNoClone() Test the read only access functions on a UText.
580 // The text is accessed in a variety of ways, and compared with
581 // the reference UnicodeString.
582 //
583 void UTextTest::TestAccessNoClone(const UnicodeString &us, UText *ut, int cpCount, m *cpMap) {
584 UErrorCode status = U_ZERO_ERROR;
585 gTestNum++;
586
587 //
588 // Check the length from the UText
589 //
590 int64_t expectedLen = cpMap[cpCount].nativeIdx;
591 int64_t utlen = utext_nativeLength(ut);
592 TEST_ASSERT(expectedLen == utlen);
593
594 //
595 // Iterate forwards, verify that we get the correct code points
596 // at the correct native offsets.
597 //
598 int i = 0;
599 int64_t index;
600 int64_t expectedIndex = 0;
601 int64_t foundIndex = 0;
602 UChar32 expectedC;
603 UChar32 foundC;
604 int64_t len;
605
606 for (i=0; i<cpCount; i++) {
607 expectedIndex = cpMap[i].nativeIdx;
608 foundIndex = utext_getNativeIndex(ut);
609 TEST_ASSERT(expectedIndex == foundIndex);
610 expectedC = cpMap[i].cp;
611 foundC = utext_next32(ut);
612 TEST_ASSERT(expectedC == foundC);
613 foundIndex = utext_getPreviousNativeIndex(ut);
614 TEST_ASSERT(expectedIndex == foundIndex);
615 if (gFailed) {
616 return;
617 }
618 }
619 foundC = utext_next32(ut);
620 TEST_ASSERT(foundC == U_SENTINEL);
621
622 // Repeat above, using macros
623 utext_setNativeIndex(ut, 0);
624 for (i=0; i<cpCount; i++) {
625 expectedIndex = cpMap[i].nativeIdx;
626 foundIndex = UTEXT_GETNATIVEINDEX(ut);
627 TEST_ASSERT(expectedIndex == foundIndex);
628 expectedC = cpMap[i].cp;
629 foundC = UTEXT_NEXT32(ut);
630 TEST_ASSERT(expectedC == foundC);
631 if (gFailed) {
632 return;
633 }
634 }
635 foundC = UTEXT_NEXT32(ut);
636 TEST_ASSERT(foundC == U_SENTINEL);
637
638 //
639 // Forward iteration (above) should have left index at the
640 // end of the input, which should == length().
641 //
642 len = utext_nativeLength(ut);
643 foundIndex = utext_getNativeIndex(ut);
644 TEST_ASSERT(len == foundIndex);
645
646 //
647 // Iterate backwards over entire test string
648 //
649 len = utext_getNativeIndex(ut);
650 utext_setNativeIndex(ut, len);
651 for (i=cpCount-1; i>=0; i--) {
652 expectedC = cpMap[i].cp;
653 expectedIndex = cpMap[i].nativeIdx;
654 int64_t prevIndex = utext_getPreviousNativeIndex(ut);
655 foundC = utext_previous32(ut);
656 foundIndex = utext_getNativeIndex(ut);
657 TEST_ASSERT(expectedIndex == foundIndex);
658 TEST_ASSERT(expectedC == foundC);
659 TEST_ASSERT(prevIndex == foundIndex);
660 if (gFailed) {
661 return;
662 }
663 }
664
665 //
666 // Backwards iteration, above, should have left our iterator
667 // position at zero, and continued backwards iterationshould fail.
668 //
669 foundIndex = utext_getNativeIndex(ut);
670 TEST_ASSERT(foundIndex == 0);
671 foundIndex = utext_getPreviousNativeIndex(ut);
672 TEST_ASSERT(foundIndex == 0);
673
674
675 foundC = utext_previous32(ut);
676 TEST_ASSERT(foundC == U_SENTINEL);
677 foundIndex = utext_getNativeIndex(ut);
678 TEST_ASSERT(foundIndex == 0);
679 foundIndex = utext_getPreviousNativeIndex(ut);
680 TEST_ASSERT(foundIndex == 0);
681
682
683 // And again, with the macros
684 utext_setNativeIndex(ut, len);
685 for (i=cpCount-1; i>=0; i--) {
686 expectedC = cpMap[i].cp;
687 expectedIndex = cpMap[i].nativeIdx;
688 foundC = UTEXT_PREVIOUS32(ut);
689 foundIndex = UTEXT_GETNATIVEINDEX(ut);
690 TEST_ASSERT(expectedIndex == foundIndex);
691 TEST_ASSERT(expectedC == foundC);
692 if (gFailed) {
693 return;
694 }
695 }
696
697 //
698 // Backwards iteration, above, should have left our iterator
699 // position at zero, and continued backwards iterationshould fail.
700 //
701 foundIndex = UTEXT_GETNATIVEINDEX(ut);
702 TEST_ASSERT(foundIndex == 0);
703
704 foundC = UTEXT_PREVIOUS32(ut);
705 TEST_ASSERT(foundC == U_SENTINEL);
706 foundIndex = UTEXT_GETNATIVEINDEX(ut);
707 TEST_ASSERT(foundIndex == 0);
708 if (gFailed) {
709 return;
710 }
711
712 //
713 // next32From(), prevous32From(), Iterate in a somewhat random order.
714 //
715 int cpIndex = 0;
716 for (i=0; i<cpCount; i++) {
717 cpIndex = (cpIndex + 9973) % cpCount;
718 index = cpMap[cpIndex].nativeIdx;
719 expectedC = cpMap[cpIndex].cp;
720 foundC = utext_next32From(ut, index);
721 TEST_ASSERT(expectedC == foundC);
722 if (gFailed) {
723 return;
724 }
725 }
726
727 cpIndex = 0;
728 for (i=0; i<cpCount; i++) {
729 cpIndex = (cpIndex + 9973) % cpCount;
730 index = cpMap[cpIndex+1].nativeIdx;
731 expectedC = cpMap[cpIndex].cp;
732 foundC = utext_previous32From(ut, index);
733 TEST_ASSERT(expectedC == foundC);
734 if (gFailed) {
735 return;
736 }
737 }
738
739
740 //
741 // moveIndex(int32_t delta);
742 //
743
744 // Walk through frontwards, incrementing by one
745 utext_setNativeIndex(ut, 0);
746 for (i=1; i<=cpCount; i++) {
747 utext_moveIndex32(ut, 1);
748 index = utext_getNativeIndex(ut);
749 expectedIndex = cpMap[i].nativeIdx;
750 TEST_ASSERT(expectedIndex == index);
751 index = UTEXT_GETNATIVEINDEX(ut);
752 TEST_ASSERT(expectedIndex == index);
753 }
754
755 // Walk through frontwards, incrementing by two
756 utext_setNativeIndex(ut, 0);
757 for (i=2; i<cpCount; i+=2) {
758 utext_moveIndex32(ut, 2);
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);
764 }
765
766 // walk through the string backwards, decrementing by one.
767 i = cpMap[cpCount].nativeIdx;
768 utext_setNativeIndex(ut, i);
769 for (i=cpCount; i>=0; i--) {
770 expectedIndex = cpMap[i].nativeIdx;
771 index = utext_getNativeIndex(ut);
772 TEST_ASSERT(expectedIndex == index);
773 index = UTEXT_GETNATIVEINDEX(ut);
774 TEST_ASSERT(expectedIndex == index);
775 utext_moveIndex32(ut, -1);
776 }
777
778
779 // walk through backwards, decrementing by three
780 i = cpMap[cpCount].nativeIdx;
781 utext_setNativeIndex(ut, i);
782 for (i=cpCount; i>=0; i-=3) {
783 expectedIndex = cpMap[i].nativeIdx;
784 index = utext_getNativeIndex(ut);
785 TEST_ASSERT(expectedIndex == index);
786 index = UTEXT_GETNATIVEINDEX(ut);
787 TEST_ASSERT(expectedIndex == index);
788 utext_moveIndex32(ut, -3);
789 }
790
791
792 //
793 // Extract
794 //
795 int bufSize = us.length() + 10;
796 UChar *buf = new UChar[bufSize];
797 status = U_ZERO_ERROR;
798 expectedLen = us.length();
799 len = utext_extract(ut, 0, utlen, buf, bufSize, &status);
800 TEST_SUCCESS(status);
801 TEST_ASSERT(len == expectedLen);
802 int compareResult = us.compare(buf, -1);
803 TEST_ASSERT(compareResult == 0);
804
805 status = U_ZERO_ERROR;
806 len = utext_extract(ut, 0, utlen, NULL, 0, &status);
807 if (utlen == 0) {
808 TEST_ASSERT(status == U_STRING_NOT_TERMINATED_WARNING);
809 } else {
810 TEST_ASSERT(status == U_BUFFER_OVERFLOW_ERROR);
811 }
812 TEST_ASSERT(len == expectedLen);
813
814 status = U_ZERO_ERROR;
815 u_memset(buf, 0x5555, bufSize);
816 len = utext_extract(ut, 0, utlen, buf, 1, &status);
817 if (us.length() == 0) {
818 TEST_SUCCESS(status);
819 TEST_ASSERT(buf[0] == 0);
820 } else {
821 // Buf len == 1, extracting a single 16 bit value.
822 // If the data char is supplementary, it doesn't matter whether the buffer remains unchanged,
823 // or whether the lead surrogate of the pair is extracted.
824 // It's a buffer overflow error in either case.
825 TEST_ASSERT(buf[0] == us.charAt(0) ||
826 buf[0] == 0x5555 && U_IS_SUPPLEMENTARY(us.char32At(0)));
827 TEST_ASSERT(buf[1] == 0x5555);
828 if (us.length() == 1) {
829 TEST_ASSERT(status == U_STRING_NOT_TERMINATED_WARNING);
830 } else {
831 TEST_ASSERT(status == U_BUFFER_OVERFLOW_ERROR);
832 }
833 }
834
835 delete []buf;
836 }
837
838
839
840 //
841 // ErrorTest() Check various error and edge cases.
842 //
843 void UTextTest::ErrorTest()
844 {
845 // Close of an unitialized UText. Shouldn't blow up.
846 {
847 UText ut;
848 memset(&ut, 0, sizeof(UText));
849 utext_close(&ut);
850 utext_close(NULL);
851 }
852
853 // Double-close of a UText. Shouldn't blow up. UText should still be usable.
854 {
855 UErrorCode status = U_ZERO_ERROR;
856 UText ut = UTEXT_INITIALIZER;
857 UnicodeString s("Hello, World");
858 UText *ut2 = utext_openUnicodeString(&ut, &s, &status);
859 TEST_SUCCESS(status);
860 TEST_ASSERT(ut2 == &ut);
861
862 UText *ut3 = utext_close(&ut);
863 TEST_ASSERT(ut3 == &ut);
864
865 UText *ut4 = utext_close(&ut);
866 TEST_ASSERT(ut4 == &ut);
867
868 utext_openUnicodeString(&ut, &s, &status);
869 TEST_SUCCESS(status);
870 utext_close(&ut);
871 }
872
873 // Re-use of a UText, chaining through each of the types of UText
874 // (If it doesn't blow up, and doesn't leak, it's probably working fine)
875 {
876 UErrorCode status = U_ZERO_ERROR;
877 UText ut = UTEXT_INITIALIZER;
878 UText *utp;
879 UnicodeString s1("Hello, World");
880 UChar s2[] = {(UChar)0x41, (UChar)0x42, (UChar)0};
881 const char *s3 = "\x66\x67\x68";
882
883 utp = utext_openUnicodeString(&ut, &s1, &status);
884 TEST_SUCCESS(status);
885 TEST_ASSERT(utp == &ut);
886
887 utp = utext_openConstUnicodeString(&ut, &s1, &status);
888 TEST_SUCCESS(status);
889 TEST_ASSERT(utp == &ut);
890
891 utp = utext_openUTF8(&ut, s3, -1, &status);
892 TEST_SUCCESS(status);
893 TEST_ASSERT(utp == &ut);
894
895 utp = utext_openUChars(&ut, s2, -1, &status);
896 TEST_SUCCESS(status);
897 TEST_ASSERT(utp == &ut);
898
899 utp = utext_close(&ut);
900 TEST_ASSERT(utp == &ut);
901
902 utp = utext_openUnicodeString(&ut, &s1, &status);
903 TEST_SUCCESS(status);
904 TEST_ASSERT(utp == &ut);
905 }
906
907 //
908 // UTF-8 with malformed sequences.
909 // These should come through as the Unicode replacement char, \ufffd
910 //
911 {
912 UErrorCode status = U_ZERO_ERROR;
913 UText *ut = NULL;
914 const char *badUTF8 = "\x41\x81\x42\xf0\x81\x81\x43";
915 UChar32 c;
916
917 ut = utext_openUTF8(NULL, badUTF8, -1, &status);
918 TEST_SUCCESS(status);
919 c = utext_char32At(ut, 1);
920 TEST_ASSERT(c == 0xfffd);
921 c = utext_char32At(ut, 3);
922 TEST_ASSERT(c == 0xfffd);
923 c = utext_char32At(ut, 5);
924 TEST_ASSERT(c == 0xfffd);
925 c = utext_char32At(ut, 6);
926 TEST_ASSERT(c == 0x43);
927
928 UChar buf[10];
929 int n = utext_extract(ut, 0, 9, buf, 10, &status);
930 TEST_SUCCESS(status);
931 TEST_ASSERT(n==5);
932 TEST_ASSERT(buf[1] == 0xfffd);
933 TEST_ASSERT(buf[3] == 0xfffd);
934 TEST_ASSERT(buf[2] == 0x42);
935 utext_close(ut);
936 }
937
938
939 //
940 // isLengthExpensive - does it make the exptected transitions after
941 // getting the length of a nul terminated string?
942 //
943 {
944 UErrorCode status = U_ZERO_ERROR;
945 UnicodeString sa("Hello, this is a string");
946 UBool isExpensive;
947
948 UChar sb[100];
949 memset(sb, 0x20, sizeof(sb));
950 sb[99] = 0;
951
952 UText *uta = utext_openUnicodeString(NULL, &sa, &status);
953 TEST_SUCCESS(status);
954 isExpensive = utext_isLengthExpensive(uta);
955 TEST_ASSERT(isExpensive == FALSE);
956 utext_close(uta);
957
958 UText *utb = utext_openUChars(NULL, sb, -1, &status);
959 TEST_SUCCESS(status);
960 isExpensive = utext_isLengthExpensive(utb);
961 TEST_ASSERT(isExpensive == TRUE);
962 int64_t len = utext_nativeLength(utb);
963 TEST_ASSERT(len == 99);
964 isExpensive = utext_isLengthExpensive(utb);
965 TEST_ASSERT(isExpensive == FALSE);
966 utext_close(utb);
967 }
968
969 //
970 // Index to positions not on code point boundaries.
971 //
972 {
973 const char *u8str = "\xc8\x81\xe1\x82\x83\xf1\x84\x85\x86";
974 int32_t startMap[] = { 0, 0, 2, 2, 2, 5, 5, 5, 5, 9, 9};
975 int32_t nextMap[] = { 2, 2, 5, 5, 5, 9, 9, 9, 9, 9, 9};
976 int32_t prevMap[] = { 0, 0, 0, 0, 0, 2, 2, 2, 2, 5, 5};
977 UChar32 c32Map[] = {0x201, 0x201, 0x1083, 0x1083, 0x1083, 0x044146, 0x044146, 0x044146, 0x044146, -1, -1};
978 UChar32 pr32Map[] = { -1, -1, 0x201, 0x201, 0x201, 0x1083, 0x1083, 0x1083, 0x1083, 0x044146, 0x044146};
979
980 // extractLen is the size, in UChars, of what will be extracted between index and index+1.
981 // is zero when both index positions lie within the same code point.
982 int32_t exLen[] = { 0, 1, 0, 0, 1, 0, 0, 0, 2, 0, 0};
983
984
985 UErrorCode status = U_ZERO_ERROR;
986 UText *ut = utext_openUTF8(NULL, u8str, -1, &status);
987 TEST_SUCCESS(status);
988
989 // Check setIndex
990 int32_t i;
991 int32_t startMapLimit = sizeof(startMap) / sizeof(int32_t);
992 for (i=0; i<startMapLimit; i++) {
993 utext_setNativeIndex(ut, i);
994 int64_t cpIndex = utext_getNativeIndex(ut);
995 TEST_ASSERT(cpIndex == startMap[i]);
996 cpIndex = UTEXT_GETNATIVEINDEX(ut);
997 TEST_ASSERT(cpIndex == startMap[i]);
998 }
999
1000 // Check char32At
1001 for (i=0; i<startMapLimit; i++) {
1002 UChar32 c32 = utext_char32At(ut, i);
1003 TEST_ASSERT(c32 == c32Map[i]);
1004 int64_t cpIndex = utext_getNativeIndex(ut);
1005 TEST_ASSERT(cpIndex == startMap[i]);
1006 }
1007
1008 // Check utext_next32From
1009 for (i=0; i<startMapLimit; i++) {
1010 UChar32 c32 = utext_next32From(ut, i);
1011 TEST_ASSERT(c32 == c32Map[i]);
1012 int64_t cpIndex = utext_getNativeIndex(ut);
1013 TEST_ASSERT(cpIndex == nextMap[i]);
1014 }
1015
1016 // check utext_previous32From
1017 for (i=0; i<startMapLimit; i++) {
1018 gTestNum++;
1019 UChar32 c32 = utext_previous32From(ut, i);
1020 TEST_ASSERT(c32 == pr32Map[i]);
1021 int64_t cpIndex = utext_getNativeIndex(ut);
1022 TEST_ASSERT(cpIndex == prevMap[i]);
1023 }
1024
1025 // check Extract
1026 // Extract from i to i+1, which may be zero or one code points,
1027 // depending on whether the indices straddle a cp boundary.
1028 for (i=0; i<startMapLimit; i++) {
1029 UChar buf[3];
1030 status = U_ZERO_ERROR;
1031 int32_t extractedLen = utext_extract(ut, i, i+1, buf, 3, &status);
1032 TEST_SUCCESS(status);
1033 TEST_ASSERT(extractedLen == exLen[i]);
1034 if (extractedLen > 0) {
1035 UChar32 c32;
1036 /* extractedLen-extractedLen == 0 is used to get around a compiler warning. */
1037 U16_GET(buf, 0, extractedLen-extractedLen, extractedLen, c32);
1038 TEST_ASSERT(c32 == c32Map[i]);
1039 }
1040 }
1041
1042 utext_close(ut);
1043 }
1044
1045
1046 { // Similar test, with utf16 instead of utf8
1047 // TODO: merge the common parts of these tests.
1048
1049 UnicodeString u16str("\\u1000\\U00011000\\u2000\\U00022000", -1, US_INV);
1050 int32_t startMap[] ={ 0, 1, 1, 3, 4, 4, 6, 6};
1051 int32_t nextMap[] = { 1, 3, 3, 4, 6, 6, 6, 6};
1052 int32_t prevMap[] = { 0, 0, 0, 1, 3, 3, 4, 4};
1053 UChar32 c32Map[] = {0x1000, 0x11000, 0x11000, 0x2000, 0x22000, 0x22000, -1, -1};
1054 UChar32 pr32Map[] = { -1, 0x1000, 0x1000, 0x11000, 0x2000, 0x2000, 0x22000, 0x22000};
1055 int32_t exLen[] = { 1, 0, 2, 1, 0, 2, 0, 0,};
1056
1057 u16str = u16str.unescape();
1058 UErrorCode status = U_ZERO_ERROR;
1059 UText *ut = utext_openUnicodeString(NULL, &u16str, &status);
1060 TEST_SUCCESS(status);
1061
1062 int32_t startMapLimit = sizeof(startMap) / sizeof(int32_t);
1063 int i;
1064 for (i=0; i<startMapLimit; i++) {
1065 utext_setNativeIndex(ut, i);
1066 int64_t cpIndex = utext_getNativeIndex(ut);
1067 TEST_ASSERT(cpIndex == startMap[i]);
1068 }
1069
1070 // Check char32At
1071 for (i=0; i<startMapLimit; i++) {
1072 UChar32 c32 = utext_char32At(ut, i);
1073 TEST_ASSERT(c32 == c32Map[i]);
1074 int64_t cpIndex = utext_getNativeIndex(ut);
1075 TEST_ASSERT(cpIndex == startMap[i]);
1076 }
1077
1078 // Check utext_next32From
1079 for (i=0; i<startMapLimit; i++) {
1080 UChar32 c32 = utext_next32From(ut, i);
1081 TEST_ASSERT(c32 == c32Map[i]);
1082 int64_t cpIndex = utext_getNativeIndex(ut);
1083 TEST_ASSERT(cpIndex == nextMap[i]);
1084 }
1085
1086 // check utext_previous32From
1087 for (i=0; i<startMapLimit; i++) {
1088 UChar32 c32 = utext_previous32From(ut, i);
1089 TEST_ASSERT(c32 == pr32Map[i]);
1090 int64_t cpIndex = utext_getNativeIndex(ut);
1091 TEST_ASSERT(cpIndex == prevMap[i]);
1092 }
1093
1094 // check Extract
1095 // Extract from i to i+1, which may be zero or one code points,
1096 // depending on whether the indices straddle a cp boundary.
1097 for (i=0; i<startMapLimit; i++) {
1098 UChar buf[3];
1099 status = U_ZERO_ERROR;
1100 int32_t extractedLen = utext_extract(ut, i, i+1, buf, 3, &status);
1101 TEST_SUCCESS(status);
1102 TEST_ASSERT(extractedLen == exLen[i]);
1103 if (extractedLen > 0) {
1104 UChar32 c32;
1105 /* extractedLen-extractedLen == 0 is used to get around a compiler warning. */
1106 U16_GET(buf, 0, extractedLen-extractedLen, extractedLen, c32);
1107 TEST_ASSERT(c32 == c32Map[i]);
1108 }
1109 }
1110
1111 utext_close(ut);
1112 }
1113
1114 { // Similar test, with UText over Replaceable
1115 // TODO: merge the common parts of these tests.
1116
1117 UnicodeString u16str("\\u1000\\U00011000\\u2000\\U00022000", -1, US_INV);
1118 int32_t startMap[] ={ 0, 1, 1, 3, 4, 4, 6, 6};
1119 int32_t nextMap[] = { 1, 3, 3, 4, 6, 6, 6, 6};
1120 int32_t prevMap[] = { 0, 0, 0, 1, 3, 3, 4, 4};
1121 UChar32 c32Map[] = {0x1000, 0x11000, 0x11000, 0x2000, 0x22000, 0x22000, -1, -1};
1122 UChar32 pr32Map[] = { -1, 0x1000, 0x1000, 0x11000, 0x2000, 0x2000, 0x22000, 0x22000};
1123 int32_t exLen[] = { 1, 0, 2, 1, 0, 2, 0, 0,};
1124
1125 u16str = u16str.unescape();
1126 UErrorCode status = U_ZERO_ERROR;
1127 UText *ut = utext_openReplaceable(NULL, &u16str, &status);
1128 TEST_SUCCESS(status);
1129
1130 int32_t startMapLimit = sizeof(startMap) / sizeof(int32_t);
1131 int i;
1132 for (i=0; i<startMapLimit; i++) {
1133 utext_setNativeIndex(ut, i);
1134 int64_t cpIndex = utext_getNativeIndex(ut);
1135 TEST_ASSERT(cpIndex == startMap[i]);
1136 }
1137
1138 // Check char32At
1139 for (i=0; i<startMapLimit; i++) {
1140 UChar32 c32 = utext_char32At(ut, i);
1141 TEST_ASSERT(c32 == c32Map[i]);
1142 int64_t cpIndex = utext_getNativeIndex(ut);
1143 TEST_ASSERT(cpIndex == startMap[i]);
1144 }
1145
1146 // Check utext_next32From
1147 for (i=0; i<startMapLimit; i++) {
1148 UChar32 c32 = utext_next32From(ut, i);
1149 TEST_ASSERT(c32 == c32Map[i]);
1150 int64_t cpIndex = utext_getNativeIndex(ut);
1151 TEST_ASSERT(cpIndex == nextMap[i]);
1152 }
1153
1154 // check utext_previous32From
1155 for (i=0; i<startMapLimit; i++) {
1156 UChar32 c32 = utext_previous32From(ut, i);
1157 TEST_ASSERT(c32 == pr32Map[i]);
1158 int64_t cpIndex = utext_getNativeIndex(ut);
1159 TEST_ASSERT(cpIndex == prevMap[i]);
1160 }
1161
1162 // check Extract
1163 // Extract from i to i+1, which may be zero or one code points,
1164 // depending on whether the indices straddle a cp boundary.
1165 for (i=0; i<startMapLimit; i++) {
1166 UChar buf[3];
1167 status = U_ZERO_ERROR;
1168 int32_t extractedLen = utext_extract(ut, i, i+1, buf, 3, &status);
1169 TEST_SUCCESS(status);
1170 TEST_ASSERT(extractedLen == exLen[i]);
1171 if (extractedLen > 0) {
1172 UChar32 c32;
1173 /* extractedLen-extractedLen == 0 is used to get around a compiler warning. */
1174 U16_GET(buf, 0, extractedLen-extractedLen, extractedLen, c32);
1175 TEST_ASSERT(c32 == c32Map[i]);
1176 }
1177 }
1178
1179 utext_close(ut);
1180 }
1181 }
1182
1183
1184 void UTextTest::FreezeTest() {
1185 // Check isWritable() and freeze() behavior.
1186 //
1187
1188 UnicodeString ustr("Hello, World.");
1189 const char u8str[] = {char(0x31), (char)0x32, (char)0x33, 0};
1190 const UChar u16str[] = {(UChar)0x31, (UChar)0x32, (UChar)0x44, 0};
1191
1192 UErrorCode status = U_ZERO_ERROR;
1193 UText *ut = NULL;
1194 UText *ut2 = NULL;
1195
1196 ut = utext_openUTF8(ut, u8str, -1, &status);
1197 TEST_SUCCESS(status);
1198 UBool writable = utext_isWritable(ut);
1199 TEST_ASSERT(writable == FALSE);
1200 utext_copy(ut, 1, 2, 0, TRUE, &status);
1201 TEST_ASSERT(status == U_NO_WRITE_PERMISSION);
1202
1203 status = U_ZERO_ERROR;
1204 ut = utext_openUChars(ut, u16str, -1, &status);
1205 TEST_SUCCESS(status);
1206 writable = utext_isWritable(ut);
1207 TEST_ASSERT(writable == FALSE);
1208 utext_copy(ut, 1, 2, 0, TRUE, &status);
1209 TEST_ASSERT(status == U_NO_WRITE_PERMISSION);
1210
1211 status = U_ZERO_ERROR;
1212 ut = utext_openUnicodeString(ut, &ustr, &status);
1213 TEST_SUCCESS(status);
1214 writable = utext_isWritable(ut);
1215 TEST_ASSERT(writable == TRUE);
1216 utext_freeze(ut);
1217 writable = utext_isWritable(ut);
1218 TEST_ASSERT(writable == FALSE);
1219 utext_copy(ut, 1, 2, 0, TRUE, &status);
1220 TEST_ASSERT(status == U_NO_WRITE_PERMISSION);
1221
1222 status = U_ZERO_ERROR;
1223 ut = utext_openUnicodeString(ut, &ustr, &status);
1224 TEST_SUCCESS(status);
1225 ut2 = utext_clone(ut2, ut, FALSE, FALSE, &status); // clone with readonly = false
1226 TEST_SUCCESS(status);
1227 writable = utext_isWritable(ut2);
1228 TEST_ASSERT(writable == TRUE);
1229 ut2 = utext_clone(ut2, ut, FALSE, TRUE, &status); // clone with readonly = true
1230 TEST_SUCCESS(status);
1231 writable = utext_isWritable(ut2);
1232 TEST_ASSERT(writable == FALSE);
1233 utext_copy(ut2, 1, 2, 0, TRUE, &status);
1234 TEST_ASSERT(status == U_NO_WRITE_PERMISSION);
1235
1236 status = U_ZERO_ERROR;
1237 ut = utext_openConstUnicodeString(ut, (const UnicodeString *)&ustr, &status);
1238 TEST_SUCCESS(status);
1239 writable = utext_isWritable(ut);
1240 TEST_ASSERT(writable == FALSE);
1241 utext_copy(ut, 1, 2, 0, TRUE, &status);
1242 TEST_ASSERT(status == U_NO_WRITE_PERMISSION);
1243
1244 // Deep Clone of a frozen UText should re-enable writing in the copy.
1245 status = U_ZERO_ERROR;
1246 ut = utext_openUnicodeString(ut, &ustr, &status);
1247 TEST_SUCCESS(status);
1248 utext_freeze(ut);
1249 ut2 = utext_clone(ut2, ut, TRUE, FALSE, &status); // deep clone
1250 TEST_SUCCESS(status);
1251 writable = utext_isWritable(ut2);
1252 TEST_ASSERT(writable == TRUE);
1253
1254
1255 // Deep clone of a frozen UText, where the base type is intrinsically non-writable,
1256 // should NOT enable writing in the copy.
1257 status = U_ZERO_ERROR;
1258 ut = utext_openUChars(ut, u16str, -1, &status);
1259 TEST_SUCCESS(status);
1260 utext_freeze(ut);
1261 ut2 = utext_clone(ut2, ut, TRUE, FALSE, &status); // deep clone
1262 TEST_SUCCESS(status);
1263 writable = utext_isWritable(ut2);
1264 TEST_ASSERT(writable == FALSE);
1265
1266 // cleanup
1267 utext_close(ut);
1268 utext_close(ut2);
1269 }
1270
1271
1272 //
1273 // Fragmented UText
1274 // A UText type that works with a chunk size of 1.
1275 // Intended to test for edge cases.
1276 // Input comes from a UnicodeString.
1277 //
1278 // ut.b the character. Put into both halves.
1279 //
1280
1281 U_CDECL_BEGIN
1282 static UBool U_CALLCONV
1283 fragTextAccess(UText *ut, int64_t index, UBool forward) {
1284 const UnicodeString *us = (const UnicodeString *)ut->context;
1285 UChar c;
1286 int32_t length = us->length();
1287 if (forward && index>=0 && index<length) {
1288 c = us->charAt((int32_t)index);
1289 ut->b = c | c<<16;
1290 ut->chunkOffset = 0;
1291 ut->chunkLength = 1;
1292 ut->chunkNativeStart = index;
1293 ut->chunkNativeLimit = index+1;
1294 return true;
1295 }
1296 if (!forward && index>0 && index <=length) {
1297 c = us->charAt((int32_t)index-1);
1298 ut->b = c | c<<16;
1299 ut->chunkOffset = 1;
1300 ut->chunkLength = 1;
1301 ut->chunkNativeStart = index-1;
1302 ut->chunkNativeLimit = index;
1303 return true;
1304 }
1305 ut->b = 0;
1306 ut->chunkOffset = 0;
1307 ut->chunkLength = 0;
1308 if (index <= 0) {
1309 ut->chunkNativeStart = 0;
1310 ut->chunkNativeLimit = 0;
1311 } else {
1312 ut->chunkNativeStart = length;
1313 ut->chunkNativeLimit = length;
1314 }
1315 return false;
1316 }
1317
1318 // Function table to be used with this fragmented text provider.
1319 // Initialized in the open function.
1320 static UTextFuncs fragmentFuncs;
1321
1322 // Clone function for fragmented text provider.
1323 // Didn't really want to provide this, but it's easier to provide it than to keep it
1324 // out of the tests.
1325 //
1326 UText *
1327 cloneFragmentedUnicodeString(UText *dest, const UText *src, UBool deep, UErrorCode *status) {
1328 if (U_FAILURE(*status)) {
1329 return NULL;
1330 }
1331 if (deep) {
1332 *status = U_UNSUPPORTED_ERROR;
1333 return NULL;
1334 }
1335 dest = utext_openUnicodeString(dest, (UnicodeString *)src->context, status);
1336 utext_setNativeIndex(dest, utext_getNativeIndex(src));
1337 return dest;
1338 }
1339
1340 U_CDECL_END
1341
1342 // Open function for the fragmented text provider.
1343 UText *
1344 openFragmentedUnicodeString(UText *ut, UnicodeString *s, UErrorCode *status) {
1345 ut = utext_openUnicodeString(ut, s, status);
1346 if (U_FAILURE(*status)) {
1347 return ut;
1348 }
1349
1350 // Copy of the function table from the stock UnicodeString UText,
1351 // and replace the entry for the access function.
1352 memcpy(&fragmentFuncs, ut->pFuncs, sizeof(fragmentFuncs));
1353 fragmentFuncs.access = fragTextAccess;
1354 fragmentFuncs.clone = cloneFragmentedUnicodeString;
1355 ut->pFuncs = &fragmentFuncs;
1356
1357 ut->chunkContents = (UChar *)&ut->b;
1358 ut->pFuncs->access(ut, 0, TRUE);
1359 return ut;
1360 }
1361
1362 // Regression test for Ticket 5560
1363 // Clone fails to update chunkContentPointer in the cloned copy.
1364 // This is only an issue for UText types that work in a local buffer,
1365 // (UTF-8 wrapper, for example)
1366 //
1367 // The test:
1368 // 1. Create an inital UText
1369 // 2. Deep clone it. Contents should match original.
1370 // 3. Reset original to something different.
1371 // 4. Check that clone contents did not change.
1372 //
1373 void UTextTest::Ticket5560() {
1374 /* The following two strings are in UTF-8 even on EBCDIC platforms. */
1375 static const char s1[] = {0x41,0x42,0x43,0x44,0x45,0x46,0}; /* "ABCDEF" */
1376 static const char s2[] = {0x31,0x32,0x33,0x34,0x35,0x36,0}; /* "123456" */
1377 UErrorCode status = U_ZERO_ERROR;
1378
1379 UText ut1 = UTEXT_INITIALIZER;
1380 UText ut2 = UTEXT_INITIALIZER;
1381
1382 utext_openUTF8(&ut1, s1, -1, &status);
1383 UChar c = utext_next32(&ut1);
1384 TEST_ASSERT(c == 0x41); // c == 'A'
1385
1386 utext_clone(&ut2, &ut1, TRUE, FALSE, &status);
1387 TEST_SUCCESS(status);
1388 c = utext_next32(&ut2);
1389 TEST_ASSERT(c == 0x42); // c == 'B'
1390 c = utext_next32(&ut1);
1391 TEST_ASSERT(c == 0x42); // c == 'B'
1392
1393 utext_openUTF8(&ut1, s2, -1, &status);
1394 c = utext_next32(&ut1);
1395 TEST_ASSERT(c == 0x31); // c == '1'
1396 c = utext_next32(&ut2);
1397 TEST_ASSERT(c == 0x43); // c == 'C'
1398
1399 utext_close(&ut1);
1400 utext_close(&ut2);
1401 }
1402
1403
1404 // Test for Ticket 6847
1405 //
1406 void UTextTest::Ticket6847() {
1407 const int STRLEN = 90;
1408 UChar s[STRLEN+1];
1409 u_memset(s, 0x41, STRLEN);
1410 s[STRLEN] = 0;
1411
1412 UErrorCode status = U_ZERO_ERROR;
1413 UText *ut = utext_openUChars(NULL, s, -1, &status);
1414
1415 utext_setNativeIndex(ut, 0);
1416 int32_t count = 0;
1417 UChar32 c = 0;
1418 int32_t nativeIndex = UTEXT_GETNATIVEINDEX(ut);
1419 TEST_ASSERT(nativeIndex == 0);
1420 while ((c = utext_next32(ut)) != U_SENTINEL) {
1421 TEST_ASSERT(c == 0x41);
1422 TEST_ASSERT(count < STRLEN);
1423 if (count >= STRLEN) {
1424 break;
1425 }
1426 count++;
1427 nativeIndex = UTEXT_GETNATIVEINDEX(ut);
1428 TEST_ASSERT(nativeIndex == count);
1429 }
1430 TEST_ASSERT(count == STRLEN);
1431 nativeIndex = UTEXT_GETNATIVEINDEX(ut);
1432 TEST_ASSERT(nativeIndex == STRLEN);
1433 utext_close(ut);
1434 }
1435