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