1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
3 /********************************************************************
5 * Copyright (c) 1997-2016, International Business Machines Corporation and
6 * others. All Rights Reserved.
7 ********************************************************************/
11 * IntlTestUtilities is the medium level test class for everything in the directory "utility".
14 #include "unicode/utypes.h"
15 #include "unicode/errorcode.h"
16 #include "unicode/localpointer.h"
35 extern IntlTest
*createBytesTrieTest();
36 static IntlTest
*createLocalPointerTest();
37 extern IntlTest
*createUCharsTrieTest();
38 static IntlTest
*createEnumSetTest();
39 extern IntlTest
*createSimpleFormatterTest();
40 extern IntlTest
*createUnifiedCacheTest();
41 extern IntlTest
*createQuantityFormatterTest();
42 extern IntlTest
*createPluralMapTest();
43 #if !UCONFIG_NO_FORMATTING
44 extern IntlTest
*createStaticUnicodeSetsTest();
48 #define CASE(id, test) case id: \
51 logln(#test "---"); logln(); \
57 void IntlTestUtilities::runIndexedTest( int32_t index
, UBool exec
, const char* &name
, char* par
)
59 if (exec
) logln("TestSuite Utilities: ");
61 CASE(0, MultithreadTest
);
63 CASE(2, UnicodeStringTest
);
65 CASE(4, CharIterTest
);
68 CASE(7, ResourceBundleTest
);
69 CASE(8, NewResourceBundleTest
);
71 CASE(10, UVector32Test
);
72 CASE(11, UVectorTest
);
74 CASE(13, LocaleAliasTest
);
75 CASE(14, UnicodeSetTest
);
76 CASE(15, ErrorCodeTest
);
78 name
= "LocalPointerTest";
80 logln("TestSuite LocalPointerTest---"); logln();
81 LocalPointer
<IntlTest
> test(createLocalPointerTest());
86 name
= "BytesTrieTest";
88 logln("TestSuite BytesTrieTest---"); logln();
89 LocalPointer
<IntlTest
> test(createBytesTrieTest());
94 name
= "UCharsTrieTest";
96 logln("TestSuite UCharsTrieTest---"); logln();
97 LocalPointer
<IntlTest
> test(createUCharsTrieTest());
102 name
= "EnumSetTest";
104 logln("TestSuite EnumSetTest---"); logln();
105 LocalPointer
<IntlTest
> test(createEnumSetTest());
106 callTest(*test
, par
);
110 name
= "SimpleFormatterTest";
112 logln("TestSuite SimpleFormatterTest---"); logln();
113 LocalPointer
<IntlTest
> test(createSimpleFormatterTest());
114 callTest(*test
, par
);
118 name
= "UnifiedCacheTest";
120 logln("TestSuite UnifiedCacheTest---"); logln();
121 LocalPointer
<IntlTest
> test(createUnifiedCacheTest());
122 callTest(*test
, par
);
126 name
= "QuantityFormatterTest";
128 logln("TestSuite QuantityFormatterTest---"); logln();
129 LocalPointer
<IntlTest
> test(createQuantityFormatterTest());
130 callTest(*test
, par
);
134 name
= "PluralMapTest";
136 logln("TestSuite PluralMapTest---"); logln();
137 LocalPointer
<IntlTest
> test(createPluralMapTest());
138 callTest(*test
, par
);
142 name
= "StaticUnicodeSetsTest";
143 #if !UCONFIG_NO_FORMATTING
145 logln("TestSuite StaticUnicodeSetsTest---"); logln();
146 LocalPointer
<IntlTest
> test(createStaticUnicodeSetsTest());
147 callTest(*test
, par
);
151 default: name
= ""; break; //needed to end loop
155 void ErrorCodeTest::runIndexedTest(int32_t index
, UBool exec
, const char* &name
, char* /*par*/) {
156 if (exec
) logln("TestSuite Utilities: ");
158 case 0: name
= "TestErrorCode"; if (exec
) TestErrorCode(); break;
159 case 1: name
= "TestSubclass"; if (exec
) TestSubclass(); break;
160 case 2: name
= "TestIcuTestErrorCode"; if (exec
) TestIcuTestErrorCode(); break;
161 default: name
= ""; break; //needed to end loop
165 static void RefPlusOne(UErrorCode
&code
) { code
=(UErrorCode
)(code
+1); }
166 static void PtrPlusTwo(UErrorCode
*code
) { *code
=(UErrorCode
)(*code
+2); }
168 void ErrorCodeTest::TestErrorCode() {
170 if(errorCode
.get()!=U_ZERO_ERROR
|| !errorCode
.isSuccess() || errorCode
.isFailure()) {
171 errln("ErrorCode did not initialize properly");
174 errorCode
.assertSuccess();
175 if(errorCode
.errorName()!=u_errorName(U_ZERO_ERROR
)) {
176 errln("ErrorCode did not format error message string properly");
178 RefPlusOne(errorCode
);
179 if(errorCode
.get()!=U_ILLEGAL_ARGUMENT_ERROR
|| errorCode
.isSuccess() || !errorCode
.isFailure()) {
180 errln("ErrorCode did not yield a writable reference");
182 PtrPlusTwo(errorCode
);
183 if(errorCode
.get()!=U_INVALID_FORMAT_ERROR
|| errorCode
.isSuccess() || !errorCode
.isFailure()) {
184 errln("ErrorCode did not yield a writable pointer");
186 errorCode
.set(U_PARSE_ERROR
);
187 if(errorCode
.get()!=U_PARSE_ERROR
|| errorCode
.isSuccess() || !errorCode
.isFailure()) {
188 errln("ErrorCode.set() failed");
190 if( errorCode
.reset()!=U_PARSE_ERROR
|| errorCode
.get()!=U_ZERO_ERROR
||
191 !errorCode
.isSuccess() || errorCode
.isFailure()
193 errln("ErrorCode did not reset properly");
197 class MyErrorCode
: public ErrorCode
{
199 MyErrorCode(int32_t &countChecks
, int32_t &countDests
)
200 : checks(countChecks
), dests(countDests
) {}
207 virtual void handleFailure() const {
214 void ErrorCodeTest::TestSubclass() {
215 int32_t countChecks
=0;
216 int32_t countDests
=0;
218 MyErrorCode
errorCode(countChecks
, countDests
);
219 if( errorCode
.get()!=U_ZERO_ERROR
|| !errorCode
.isSuccess() || errorCode
.isFailure() ||
220 countChecks
!=0 || countDests
!=0
222 errln("ErrorCode did not initialize properly");
225 errorCode
.assertSuccess();
227 errln("ErrorCode.assertSuccess() called handleFailure() despite success");
229 RefPlusOne(errorCode
);
230 if(errorCode
.get()!=U_ILLEGAL_ARGUMENT_ERROR
|| errorCode
.isSuccess() || !errorCode
.isFailure()) {
231 errln("ErrorCode did not yield a writable reference");
233 errorCode
.assertSuccess();
235 errln("ErrorCode.assertSuccess() did not handleFailure()");
237 PtrPlusTwo(errorCode
);
238 if(errorCode
.get()!=U_INVALID_FORMAT_ERROR
|| errorCode
.isSuccess() || !errorCode
.isFailure()) {
239 errln("ErrorCode did not yield a writable pointer");
241 errorCode
.assertSuccess();
243 errln("ErrorCode.assertSuccess() did not handleFailure()");
245 errorCode
.set(U_PARSE_ERROR
);
246 if(errorCode
.get()!=U_PARSE_ERROR
|| errorCode
.isSuccess() || !errorCode
.isFailure()) {
247 errln("ErrorCode.set() failed");
249 if( errorCode
.reset()!=U_PARSE_ERROR
|| errorCode
.get()!=U_ZERO_ERROR
||
250 !errorCode
.isSuccess() || errorCode
.isFailure()
252 errln("ErrorCode did not reset properly");
254 errorCode
.assertSuccess();
256 errln("ErrorCode.assertSuccess() called handleFailure() despite success");
260 errln("MyErrorCode destructor detected failure despite success");
262 countChecks
=countDests
=0;
264 MyErrorCode
errorCode(countChecks
, countDests
);
265 errorCode
.set(U_PARSE_ERROR
);
268 errln("MyErrorCode destructor failed to detect failure");
272 class IcuTestErrorCodeTestHelper
: public IntlTest
{
274 void errln( const UnicodeString
&message
) U_OVERRIDE
{
275 test
->assertFalse("Already saw an error", seenError
);
277 test
->assertEquals("Message for Error", expectedErrln
, message
);
278 if (expectedDataErr
) {
279 test
->errln("Got non-data error, but expected data error");
283 void dataerrln( const UnicodeString
&message
) U_OVERRIDE
{
284 test
->assertFalse("Already saw an error", seenError
);
286 test
->assertEquals("Message for Error", expectedErrln
, message
);
287 if (!expectedDataErr
) {
288 test
->errln("Got data error, but expected non-data error");
293 UBool expectedDataErr
;
294 UnicodeString expectedErrln
;
298 void ErrorCodeTest::TestIcuTestErrorCode() {
299 IcuTestErrorCodeTestHelper helper
;
302 // Test destructor message
303 helper
.expectedErrln
= u
"AAA failure: U_ILLEGAL_PAD_POSITION";
304 helper
.expectedDataErr
= FALSE
;
305 helper
.seenError
= FALSE
;
307 IcuTestErrorCode
testStatus(helper
, "AAA");
308 testStatus
.set(U_ILLEGAL_PAD_POSITION
);
310 assertTrue("Should have seen an error", helper
.seenError
);
312 // Test destructor message with scope
313 helper
.expectedErrln
= u
"BBB failure: U_ILLEGAL_PAD_POSITION scope: foo";
314 helper
.expectedDataErr
= FALSE
;
315 helper
.seenError
= FALSE
;
317 IcuTestErrorCode
testStatus(helper
, "BBB");
318 testStatus
.setScope("foo");
319 testStatus
.set(U_ILLEGAL_PAD_POSITION
);
321 assertTrue("Should have seen an error", helper
.seenError
);
323 // Check errIfFailure message with scope
324 helper
.expectedErrln
= u
"CCC failure: U_ILLEGAL_PAD_POSITION scope: foo";
325 helper
.expectedDataErr
= FALSE
;
326 helper
.seenError
= FALSE
;
328 IcuTestErrorCode
testStatus(helper
, "CCC");
329 testStatus
.setScope("foo");
330 testStatus
.set(U_ILLEGAL_PAD_POSITION
);
331 testStatus
.errIfFailureAndReset();
332 assertTrue("Should have seen an error", helper
.seenError
);
333 helper
.seenError
= FALSE
;
334 helper
.expectedErrln
= u
"CCC failure: U_ILLEGAL_CHAR_FOUND scope: foo - 5.4300";
335 testStatus
.set(U_ILLEGAL_CHAR_FOUND
);
336 testStatus
.errIfFailureAndReset("%6.4f", 5.43);
337 assertTrue("Should have seen an error", helper
.seenError
);
340 // Check errDataIfFailure message without scope
341 helper
.expectedErrln
= u
"DDD failure: U_ILLEGAL_PAD_POSITION";
342 helper
.expectedDataErr
= TRUE
;
343 helper
.seenError
= FALSE
;
345 IcuTestErrorCode
testStatus(helper
, "DDD");
346 testStatus
.set(U_ILLEGAL_PAD_POSITION
);
347 testStatus
.errDataIfFailureAndReset();
348 assertTrue("Should have seen an error", helper
.seenError
);
349 helper
.seenError
= FALSE
;
350 helper
.expectedErrln
= u
"DDD failure: U_ILLEGAL_CHAR_FOUND - 5.4300";
351 testStatus
.set(U_ILLEGAL_CHAR_FOUND
);
352 testStatus
.errDataIfFailureAndReset("%6.4f", 5.43);
353 assertTrue("Should have seen an error", helper
.seenError
);
358 class LocalPointerTest
: public IntlTest
{
360 LocalPointerTest() {}
362 void runIndexedTest(int32_t index
, UBool exec
, const char *&name
, char *par
=NULL
);
364 void TestLocalPointer();
365 void TestLocalPointerMoveSwap();
366 void TestLocalArray();
367 void TestLocalArrayMoveSwap();
368 void TestLocalXyzPointer();
369 void TestLocalXyzPointerMoveSwap();
370 void TestLocalXyzPointerNull();
373 static IntlTest
*createLocalPointerTest() {
374 return new LocalPointerTest();
377 void LocalPointerTest::runIndexedTest(int32_t index
, UBool exec
, const char *&name
, char * /*par*/) {
379 logln("TestSuite LocalPointerTest: ");
382 TESTCASE_AUTO(TestLocalPointer
);
383 TESTCASE_AUTO(TestLocalPointerMoveSwap
);
384 TESTCASE_AUTO(TestLocalArray
);
385 TESTCASE_AUTO(TestLocalArrayMoveSwap
);
386 TESTCASE_AUTO(TestLocalXyzPointer
);
387 TESTCASE_AUTO(TestLocalXyzPointerMoveSwap
);
388 TESTCASE_AUTO(TestLocalXyzPointerNull
);
392 // Exercise almost every LocalPointer and LocalPointerBase method.
393 void LocalPointerTest::TestLocalPointer() {
395 LocalPointer
<UnicodeString
> s(new UnicodeString((UChar32
)0x50005));
396 // isNULL(), isValid(), operator==(), operator!=()
397 if(s
.isNull() || !s
.isValid() || s
==NULL
|| !(s
!=NULL
)) {
398 errln("LocalPointer constructor or NULL test failure");
401 // getAlias(), operator->, operator*
402 if(s
.getAlias()->length()!=2 || s
->length()!=2 || (*s
).length()!=2) {
403 errln("LocalPointer access failure");
405 // adoptInstead(), orphan()
406 s
.adoptInstead(new UnicodeString((UChar
)0xfffc));
408 errln("LocalPointer adoptInstead(U+FFFC) failure");
410 UnicodeString
*orphan
=s
.orphan();
411 if(orphan
==NULL
|| orphan
->length()!=1 || s
.isValid() || s
!=NULL
) {
412 errln("LocalPointer orphan() failure");
415 s
.adoptInstead(new UnicodeString());
417 errln("LocalPointer adoptInstead(empty) failure");
420 // LocalPointer(p, errorCode) sets U_MEMORY_ALLOCATION_ERROR if p==NULL.
421 UErrorCode errorCode
= U_ZERO_ERROR
;
422 LocalPointer
<CharString
> csx(new CharString("some chars", errorCode
), errorCode
);
423 if(csx
.isNull() && U_SUCCESS(errorCode
)) {
424 errln("LocalPointer(p, errorCode) failure");
427 errorCode
= U_ZERO_ERROR
;
428 csx
.adoptInsteadAndCheckErrorCode(new CharString("different chars", errorCode
), errorCode
);
429 if(csx
.isNull() && U_SUCCESS(errorCode
)) {
430 errln("adoptInsteadAndCheckErrorCode(p, errorCode) failure");
433 // Incoming failure: Keep the current object and delete the input object.
434 errorCode
= U_ILLEGAL_ARGUMENT_ERROR
;
435 csx
.adoptInsteadAndCheckErrorCode(new CharString("unused", errorCode
), errorCode
);
436 if(csx
.isValid() && strcmp(csx
->data(), "different chars") != 0) {
437 errln("adoptInsteadAndCheckErrorCode(p, U_FAILURE) did not retain the old object");
440 errorCode
= U_ZERO_ERROR
;
441 csx
.adoptInsteadAndCheckErrorCode(NULL
, errorCode
);
442 if(errorCode
!= U_MEMORY_ALLOCATION_ERROR
) {
443 errln("adoptInsteadAndCheckErrorCode(NULL, errorCode) did not set U_MEMORY_ALLOCATION_ERROR");
447 errln("adoptInsteadAndCheckErrorCode(NULL, errorCode) kept the object");
450 errorCode
= U_ZERO_ERROR
;
451 LocalPointer
<CharString
> null(NULL
, errorCode
);
452 if(errorCode
!= U_MEMORY_ALLOCATION_ERROR
) {
453 errln("LocalPointer(NULL, errorCode) did not set U_MEMORY_ALLOCATION_ERROR");
460 void LocalPointerTest::TestLocalPointerMoveSwap() {
461 UnicodeString
*p1
= new UnicodeString((UChar
)0x61);
462 UnicodeString
*p2
= new UnicodeString((UChar
)0x62);
463 LocalPointer
<UnicodeString
> s1(p1
);
464 LocalPointer
<UnicodeString
> s2(p2
);
466 if(s1
.getAlias() != p2
|| s2
.getAlias() != p1
) {
467 errln("LocalPointer.swap() did not swap");
470 if(s1
.getAlias() != p1
|| s2
.getAlias() != p2
) {
471 errln("swap(LocalPointer) did not swap back");
473 LocalPointer
<UnicodeString
> s3
;
475 if(s3
.getAlias() != p1
|| s1
.isValid()) {
476 errln("LocalPointer.moveFrom() did not move");
478 infoln("TestLocalPointerMoveSwap() with rvalue references");
479 s1
= static_cast<LocalPointer
<UnicodeString
> &&>(s3
);
480 if(s1
.getAlias() != p1
|| s3
.isValid()) {
481 errln("LocalPointer move assignment operator did not move");
483 LocalPointer
<UnicodeString
> s4(static_cast<LocalPointer
<UnicodeString
> &&>(s2
));
484 if(s4
.getAlias() != p2
|| s2
.isValid()) {
485 errln("LocalPointer move constructor did not move");
488 // Move self assignment leaves the object valid but in an undefined state.
489 // Do it to make sure there is no crash,
490 // but do not check for any particular resulting value.
495 // Exercise almost every LocalArray method (but not LocalPointerBase).
496 void LocalPointerTest::TestLocalArray() {
498 LocalArray
<UnicodeString
> a(new UnicodeString
[2]);
500 a
[0].append((UChar
)0x61);
501 a
[1].append((UChar32
)0x60006);
502 if(a
[0].length()!=1 || a
[1].length()!=2) {
503 errln("LocalArray access failure");
506 a
.adoptInstead(new UnicodeString
[4]);
507 a
[3].append((UChar
)0x62).append((UChar
)0x63).reverse();
508 if(a
[3].length()!=2 || a
[3][1]!=0x62) {
509 errln("LocalArray adoptInstead() failure");
512 // LocalArray(p, errorCode) sets U_MEMORY_ALLOCATION_ERROR if p==NULL.
513 UErrorCode errorCode
= U_ZERO_ERROR
;
514 LocalArray
<UnicodeString
> ua(new UnicodeString
[3], errorCode
);
515 if(ua
.isNull() && U_SUCCESS(errorCode
)) {
516 errln("LocalArray(p, errorCode) failure");
519 errorCode
= U_ZERO_ERROR
;
520 UnicodeString
*u4
= new UnicodeString
[4];
521 ua
.adoptInsteadAndCheckErrorCode(u4
, errorCode
);
522 if(ua
.isNull() && U_SUCCESS(errorCode
)) {
523 errln("adoptInsteadAndCheckErrorCode(p, errorCode) failure");
526 // Incoming failure: Keep the current object and delete the input object.
527 errorCode
= U_ILLEGAL_ARGUMENT_ERROR
;
528 ua
.adoptInsteadAndCheckErrorCode(new UnicodeString
[5], errorCode
);
529 if(ua
.isValid() && ua
.getAlias() != u4
) {
530 errln("adoptInsteadAndCheckErrorCode(p, U_FAILURE) did not retain the old array");
533 errorCode
= U_ZERO_ERROR
;
534 ua
.adoptInsteadAndCheckErrorCode(NULL
, errorCode
);
535 if(errorCode
!= U_MEMORY_ALLOCATION_ERROR
) {
536 errln("adoptInsteadAndCheckErrorCode(NULL, errorCode) did not set U_MEMORY_ALLOCATION_ERROR");
540 errln("adoptInsteadAndCheckErrorCode(NULL, errorCode) kept the array");
543 errorCode
= U_ZERO_ERROR
;
544 LocalArray
<UnicodeString
> null(NULL
, errorCode
);
545 if(errorCode
!= U_MEMORY_ALLOCATION_ERROR
) {
546 errln("LocalArray(NULL, errorCode) did not set U_MEMORY_ALLOCATION_ERROR");
553 void LocalPointerTest::TestLocalArrayMoveSwap() {
554 UnicodeString
*p1
= new UnicodeString
[2];
555 UnicodeString
*p2
= new UnicodeString
[3];
556 LocalArray
<UnicodeString
> a1(p1
);
557 LocalArray
<UnicodeString
> a2(p2
);
559 if(a1
.getAlias() != p2
|| a2
.getAlias() != p1
) {
560 errln("LocalArray.swap() did not swap");
563 if(a1
.getAlias() != p1
|| a2
.getAlias() != p2
) {
564 errln("swap(LocalArray) did not swap back");
566 LocalArray
<UnicodeString
> a3
;
568 if(a3
.getAlias() != p1
|| a1
.isValid()) {
569 errln("LocalArray.moveFrom() did not move");
571 infoln("TestLocalArrayMoveSwap() with rvalue references");
572 a1
= static_cast<LocalArray
<UnicodeString
> &&>(a3
);
573 if(a1
.getAlias() != p1
|| a3
.isValid()) {
574 errln("LocalArray move assignment operator did not move");
576 LocalArray
<UnicodeString
> a4(static_cast<LocalArray
<UnicodeString
> &&>(a2
));
577 if(a4
.getAlias() != p2
|| a2
.isValid()) {
578 errln("LocalArray move constructor did not move");
581 // Move self assignment leaves the object valid but in an undefined state.
582 // Do it to make sure there is no crash,
583 // but do not check for any particular resulting value.
588 #include "unicode/ucnvsel.h"
589 #include "unicode/ucal.h"
590 #include "unicode/udatpg.h"
591 #include "unicode/uidna.h"
592 #include "unicode/uldnames.h"
593 #include "unicode/umsg.h"
594 #include "unicode/unorm2.h"
595 #include "unicode/uregex.h"
596 #include "unicode/utrans.h"
598 // Use LocalXyzPointer types that are not covered elsewhere in the intltest suite.
599 void LocalPointerTest::TestLocalXyzPointer() {
600 IcuTestErrorCode
errorCode(*this, "TestLocalXyzPointer");
602 static const char *const encoding
="ISO-8859-1";
603 LocalUConverterSelectorPointer
sel(
604 ucnvsel_open(&encoding
, 1, NULL
, UCNV_ROUNDTRIP_SET
, errorCode
));
605 if(errorCode
.errIfFailureAndReset("ucnvsel_open()")) {
609 errln("LocalUConverterSelectorPointer failure");
613 #if !UCONFIG_NO_FORMATTING
614 LocalUCalendarPointer
cal(ucal_open(NULL
, 0, "root", UCAL_GREGORIAN
, errorCode
));
615 if(errorCode
.errDataIfFailureAndReset("ucal_open()")) {
619 errln("LocalUCalendarPointer failure");
623 LocalUDateTimePatternGeneratorPointer
patgen(udatpg_open("root", errorCode
));
624 if(errorCode
.errDataIfFailureAndReset("udatpg_open()")) {
627 if(patgen
.isNull()) {
628 errln("LocalUDateTimePatternGeneratorPointer failure");
632 LocalULocaleDisplayNamesPointer
ldn(uldn_open("de-CH", ULDN_STANDARD_NAMES
, errorCode
));
633 if(errorCode
.errIfFailureAndReset("uldn_open()")) {
637 errln("LocalULocaleDisplayNamesPointer failure");
641 UnicodeString hello
=UNICODE_STRING_SIMPLE("Hello {0}!");
642 LocalUMessageFormatPointer
msg(
643 umsg_open(hello
.getBuffer(), hello
.length(), "root", NULL
, errorCode
));
644 if(errorCode
.errIfFailureAndReset("umsg_open()")) {
648 errln("LocalUMessageFormatPointer failure");
651 #endif /* UCONFIG_NO_FORMATTING */
653 #if !UCONFIG_NO_NORMALIZATION
654 const UNormalizer2
*nfc
=unorm2_getNFCInstance(errorCode
);
656 LocalUNormalizer2Pointer
fn2(unorm2_openFiltered(nfc
, emptySet
.toUSet(), errorCode
));
657 if(errorCode
.errIfFailureAndReset("unorm2_openFiltered()")) {
661 errln("LocalUNormalizer2Pointer failure");
664 #endif /* !UCONFIG_NO_NORMALIZATION */
667 LocalUIDNAPointer
idna(uidna_openUTS46(0, errorCode
));
668 if(errorCode
.errIfFailureAndReset("uidna_openUTS46()")) {
672 errln("LocalUIDNAPointer failure");
675 #endif /* !UCONFIG_NO_IDNA */
677 #if !UCONFIG_NO_REGULAR_EXPRESSIONS
678 UnicodeString pattern
=UNICODE_STRING_SIMPLE("abc|xy+z");
679 LocalURegularExpressionPointer
regex(
680 uregex_open(pattern
.getBuffer(), pattern
.length(), 0, NULL
, errorCode
));
681 if(errorCode
.errIfFailureAndReset("uregex_open()")) {
685 errln("LocalURegularExpressionPointer failure");
688 #endif /* UCONFIG_NO_REGULAR_EXPRESSIONS */
690 #if !UCONFIG_NO_TRANSLITERATION
691 UnicodeString id
=UNICODE_STRING_SIMPLE("Grek-Latn");
692 LocalUTransliteratorPointer
trans(
693 utrans_openU(id
.getBuffer(), id
.length(), UTRANS_FORWARD
, NULL
, 0, NULL
, errorCode
));
694 if(errorCode
.errIfFailureAndReset("utrans_open()")) {
698 errln("LocalUTransliteratorPointer failure");
701 #endif /* !UCONFIG_NO_TRANSLITERATION */
706 void LocalPointerTest::TestLocalXyzPointerMoveSwap() {
707 #if !UCONFIG_NO_NORMALIZATION
708 IcuTestErrorCode
errorCode(*this, "TestLocalXyzPointerMoveSwap");
709 const UNormalizer2
*nfc
=unorm2_getNFCInstance(errorCode
);
710 const UNormalizer2
*nfd
=unorm2_getNFDInstance(errorCode
);
711 if(errorCode
.errIfFailureAndReset("unorm2_getNF[CD]Instance()")) {
715 UNormalizer2
*p1
= unorm2_openFiltered(nfc
, emptySet
.toUSet(), errorCode
);
716 UNormalizer2
*p2
= unorm2_openFiltered(nfd
, emptySet
.toUSet(), errorCode
);
717 LocalUNormalizer2Pointer
f1(p1
);
718 LocalUNormalizer2Pointer
f2(p2
);
719 if(errorCode
.errIfFailureAndReset("unorm2_openFiltered()")) {
722 if(f1
.isNull() || f2
.isNull()) {
723 errln("LocalUNormalizer2Pointer failure");
727 if(f1
.getAlias() != p2
|| f2
.getAlias() != p1
) {
728 errln("LocalUNormalizer2Pointer.swap() did not swap");
731 if(f1
.getAlias() != p1
|| f2
.getAlias() != p2
) {
732 errln("swap(LocalUNormalizer2Pointer) did not swap back");
734 LocalUNormalizer2Pointer f3
;
736 if(f3
.getAlias() != p1
|| f1
.isValid()) {
737 errln("LocalUNormalizer2Pointer.moveFrom() did not move");
739 infoln("TestLocalXyzPointerMoveSwap() with rvalue references");
740 f1
= static_cast<LocalUNormalizer2Pointer
&&>(f3
);
741 if(f1
.getAlias() != p1
|| f3
.isValid()) {
742 errln("LocalUNormalizer2Pointer move assignment operator did not move");
744 LocalUNormalizer2Pointer
f4(static_cast<LocalUNormalizer2Pointer
&&>(f2
));
745 if(f4
.getAlias() != p2
|| f2
.isValid()) {
746 errln("LocalUNormalizer2Pointer move constructor did not move");
748 // Move self assignment leaves the object valid but in an undefined state.
749 // Do it to make sure there is no crash,
750 // but do not check for any particular resulting value.
753 #endif /* !UCONFIG_NO_NORMALIZATION */
756 // Try LocalXyzPointer types with NULL pointers.
757 void LocalPointerTest::TestLocalXyzPointerNull() {
759 IcuTestErrorCode
errorCode(*this, "TestLocalXyzPointerNull/LocalUConverterSelectorPointer");
760 static const char *const encoding
="ISO-8859-1";
761 LocalUConverterSelectorPointer null
;
762 LocalUConverterSelectorPointer
sel(
763 ucnvsel_open(&encoding
, 1, NULL
, UCNV_ROUNDTRIP_SET
, errorCode
));
764 sel
.adoptInstead(NULL
);
766 #if !UCONFIG_NO_FORMATTING
768 IcuTestErrorCode
errorCode(*this, "TestLocalXyzPointerNull/LocalUCalendarPointer");
769 LocalUCalendarPointer null
;
770 LocalUCalendarPointer
cal(ucal_open(NULL
, 0, "root", UCAL_GREGORIAN
, errorCode
));
771 if(!errorCode
.errDataIfFailureAndReset("ucal_open()")) {
772 cal
.adoptInstead(NULL
);
776 IcuTestErrorCode
errorCode(*this, "TestLocalXyzPointerNull/LocalUDateTimePatternGeneratorPointer");
777 LocalUDateTimePatternGeneratorPointer null
;
778 LocalUDateTimePatternGeneratorPointer
patgen(udatpg_open("root", errorCode
));
779 patgen
.adoptInstead(NULL
);
782 IcuTestErrorCode
errorCode(*this, "TestLocalXyzPointerNull/LocalUMessageFormatPointer");
783 UnicodeString hello
=UNICODE_STRING_SIMPLE("Hello {0}!");
784 LocalUMessageFormatPointer null
;
785 LocalUMessageFormatPointer
msg(
786 umsg_open(hello
.getBuffer(), hello
.length(), "root", NULL
, errorCode
));
787 msg
.adoptInstead(NULL
);
789 #endif /* !UCONFIG_NO_FORMATTING */
791 #if !UCONFIG_NO_REGULAR_EXPRESSIONS
793 IcuTestErrorCode
errorCode(*this, "TestLocalXyzPointerNull/LocalURegularExpressionPointer");
794 UnicodeString pattern
=UNICODE_STRING_SIMPLE("abc|xy+z");
795 LocalURegularExpressionPointer null
;
796 LocalURegularExpressionPointer
regex(
797 uregex_open(pattern
.getBuffer(), pattern
.length(), 0, NULL
, errorCode
));
798 if(!errorCode
.errDataIfFailureAndReset("urege_open()")) {
799 regex
.adoptInstead(NULL
);
802 #endif /* !UCONFIG_NO_REGULAR_EXPRESSIONS */
804 #if !UCONFIG_NO_TRANSLITERATION
806 IcuTestErrorCode
errorCode(*this, "TestLocalXyzPointerNull/LocalUTransliteratorPointer");
807 UnicodeString id
=UNICODE_STRING_SIMPLE("Grek-Latn");
808 LocalUTransliteratorPointer null
;
809 LocalUTransliteratorPointer
trans(
810 utrans_openU(id
.getBuffer(), id
.length(), UTRANS_FORWARD
, NULL
, 0, NULL
, errorCode
));
811 if(!errorCode
.errDataIfFailureAndReset("utrans_openU()")) {
812 trans
.adoptInstead(NULL
);
815 #endif /* !UCONFIG_NO_TRANSLITERATION */
820 #include "unicode/enumset.h"
822 class EnumSetTest
: public IntlTest
{
825 virtual void runIndexedTest(int32_t index
, UBool exec
, const char *&name
, char *par
=NULL
);
829 static IntlTest
*createEnumSetTest() {
830 return new EnumSetTest();
833 void EnumSetTest::runIndexedTest(int32_t index
, UBool exec
, const char *&name
, char * /*par*/) {
835 TESTCASE_AUTO(TestEnumSet
);
846 void EnumSetTest::TestEnumSet() {
852 logln("Enum is from [%d..%d]\n", MAX_NONBOOLEAN
+1,
855 TEST_ASSERT_TRUE(flags
.get(THING1
) == FALSE
);
856 TEST_ASSERT_TRUE(flags
.get(THING2
) == FALSE
);
857 TEST_ASSERT_TRUE(flags
.get(THING3
) == FALSE
);
859 logln("get(thing1)=%d, get(thing2)=%d, get(thing3)=%d\n", flags
.get(THING1
), flags
.get(THING2
), flags
.get(THING3
));
860 logln("Value now: %d\n", flags
.getAll());
862 logln("clear -Value now: %d\n", flags
.getAll());
863 logln("get(thing1)=%d, get(thing2)=%d, get(thing3)=%d\n", flags
.get(THING1
), flags
.get(THING2
), flags
.get(THING3
));
864 TEST_ASSERT_TRUE(flags
.get(THING1
) == FALSE
);
865 TEST_ASSERT_TRUE(flags
.get(THING2
) == FALSE
);
866 TEST_ASSERT_TRUE(flags
.get(THING3
) == FALSE
);
868 logln("set THING1 -Value now: %d\n", flags
.getAll());
869 TEST_ASSERT_TRUE(flags
.get(THING1
) == TRUE
);
870 TEST_ASSERT_TRUE(flags
.get(THING2
) == FALSE
);
871 TEST_ASSERT_TRUE(flags
.get(THING3
) == FALSE
);
872 logln("get(thing1)=%d, get(thing2)=%d, get(thing3)=%d\n", flags
.get(THING1
), flags
.get(THING2
), flags
.get(THING3
));
874 logln("set THING3 -Value now: %d\n", flags
.getAll());
875 TEST_ASSERT_TRUE(flags
.get(THING1
) == TRUE
);
876 TEST_ASSERT_TRUE(flags
.get(THING2
) == FALSE
);
877 TEST_ASSERT_TRUE(flags
.get(THING3
) == TRUE
);
878 logln("get(thing1)=%d, get(thing2)=%d, get(thing3)=%d\n", flags
.get(THING1
), flags
.get(THING2
), flags
.get(THING3
));
879 flags
.remove(THING2
);
880 TEST_ASSERT_TRUE(flags
.get(THING1
) == TRUE
);
881 TEST_ASSERT_TRUE(flags
.get(THING2
) == FALSE
);
882 TEST_ASSERT_TRUE(flags
.get(THING3
) == TRUE
);
883 logln("remove THING2 -Value now: %d\n", flags
.getAll());
884 logln("get(thing1)=%d, get(thing2)=%d, get(thing3)=%d\n", flags
.get(THING1
), flags
.get(THING2
), flags
.get(THING3
));
885 flags
.remove(THING1
);
886 TEST_ASSERT_TRUE(flags
.get(THING1
) == FALSE
);
887 TEST_ASSERT_TRUE(flags
.get(THING2
) == FALSE
);
888 TEST_ASSERT_TRUE(flags
.get(THING3
) == TRUE
);
889 logln("remove THING1 -Value now: %d\n", flags
.getAll());
890 logln("get(thing1)=%d, get(thing2)=%d, get(thing3)=%d\n", flags
.get(THING1
), flags
.get(THING2
), flags
.get(THING3
));
893 logln("clear -Value now: %d\n", flags
.getAll());
894 logln("get(thing1)=%d, get(thing2)=%d, get(thing3)=%d\n", flags
.get(THING1
), flags
.get(THING2
), flags
.get(THING3
));
895 TEST_ASSERT_TRUE(flags
.get(THING1
) == FALSE
);
896 TEST_ASSERT_TRUE(flags
.get(THING2
) == FALSE
);
897 TEST_ASSERT_TRUE(flags
.get(THING3
) == FALSE
);