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