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 ********************************************************************/
12 * IntlTestUtilities is the medium level test class for everything in the directory "utility".
15 #include "unicode/utypes.h"
16 #include "unicode/errorcode.h"
17 #include "unicode/localpointer.h"
22 #include "localebuildertest.h"
37 extern IntlTest
*createBytesTrieTest();
38 static IntlTest
*createLocalPointerTest();
39 extern IntlTest
*createUCharsTrieTest();
40 static IntlTest
*createEnumSetTest();
41 extern IntlTest
*createSimpleFormatterTest();
42 extern IntlTest
*createUnifiedCacheTest();
43 extern IntlTest
*createQuantityFormatterTest();
44 extern IntlTest
*createPluralMapTest();
45 #if !UCONFIG_NO_FORMATTING
46 extern IntlTest
*createStaticUnicodeSetsTest();
50 #define CASE(id, test) case id: \
53 logln(#test "---"); logln(); \
59 void IntlTestUtilities::runIndexedTest( int32_t index
, UBool exec
, const char* &name
, char* par
)
61 if (exec
) logln("TestSuite Utilities: ");
63 CASE(0, MultithreadTest
);
65 CASE(2, UnicodeStringTest
);
67 CASE(4, CharIterTest
);
70 CASE(7, ResourceBundleTest
);
71 CASE(8, NewResourceBundleTest
);
73 CASE(10, UVector32Test
);
74 CASE(11, UVectorTest
);
76 CASE(13, LocaleAliasTest
);
77 CASE(14, UnicodeSetTest
);
78 CASE(15, ErrorCodeTest
);
80 name
= "LocalPointerTest";
82 logln("TestSuite LocalPointerTest---"); logln();
83 LocalPointer
<IntlTest
> test(createLocalPointerTest());
88 name
= "BytesTrieTest";
90 logln("TestSuite BytesTrieTest---"); logln();
91 LocalPointer
<IntlTest
> test(createBytesTrieTest());
96 name
= "UCharsTrieTest";
98 logln("TestSuite UCharsTrieTest---"); logln();
99 LocalPointer
<IntlTest
> test(createUCharsTrieTest());
100 callTest(*test
, par
);
104 name
= "EnumSetTest";
106 logln("TestSuite EnumSetTest---"); logln();
107 LocalPointer
<IntlTest
> test(createEnumSetTest());
108 callTest(*test
, par
);
112 name
= "SimpleFormatterTest";
114 logln("TestSuite SimpleFormatterTest---"); logln();
115 LocalPointer
<IntlTest
> test(createSimpleFormatterTest());
116 callTest(*test
, par
);
120 name
= "UnifiedCacheTest";
122 logln("TestSuite UnifiedCacheTest---"); logln();
123 LocalPointer
<IntlTest
> test(createUnifiedCacheTest());
124 callTest(*test
, par
);
128 name
= "QuantityFormatterTest";
130 logln("TestSuite QuantityFormatterTest---"); logln();
131 LocalPointer
<IntlTest
> test(createQuantityFormatterTest());
132 callTest(*test
, par
);
136 name
= "PluralMapTest";
138 logln("TestSuite PluralMapTest---"); logln();
139 LocalPointer
<IntlTest
> test(createPluralMapTest());
140 callTest(*test
, par
);
144 name
= "StaticUnicodeSetsTest";
145 #if !UCONFIG_NO_FORMATTING
147 logln("TestSuite StaticUnicodeSetsTest---"); logln();
148 LocalPointer
<IntlTest
> test(createStaticUnicodeSetsTest());
149 callTest(*test
, par
);
153 CASE(25, LocaleBuilderTest
);
154 default: name
= ""; break; //needed to end loop
158 void ErrorCodeTest::runIndexedTest(int32_t index
, UBool exec
, const char* &name
, char* /*par*/) {
159 if (exec
) logln("TestSuite Utilities: ");
161 case 0: name
= "TestErrorCode"; if (exec
) TestErrorCode(); break;
162 case 1: name
= "TestSubclass"; if (exec
) TestSubclass(); break;
163 case 2: name
= "TestIcuTestErrorCode"; if (exec
) TestIcuTestErrorCode(); break;
164 default: name
= ""; break; //needed to end loop
168 static void RefPlusOne(UErrorCode
&code
) { code
=(UErrorCode
)(code
+1); }
169 static void PtrPlusTwo(UErrorCode
*code
) { *code
=(UErrorCode
)(*code
+2); }
171 void ErrorCodeTest::TestErrorCode() {
173 if(errorCode
.get()!=U_ZERO_ERROR
|| !errorCode
.isSuccess() || errorCode
.isFailure()) {
174 errln("ErrorCode did not initialize properly");
177 errorCode
.assertSuccess();
178 if(errorCode
.errorName()!=u_errorName(U_ZERO_ERROR
)) {
179 errln("ErrorCode did not format error message string properly");
181 RefPlusOne(errorCode
);
182 if(errorCode
.get()!=U_ILLEGAL_ARGUMENT_ERROR
|| errorCode
.isSuccess() || !errorCode
.isFailure()) {
183 errln("ErrorCode did not yield a writable reference");
185 PtrPlusTwo(errorCode
);
186 if(errorCode
.get()!=U_INVALID_FORMAT_ERROR
|| errorCode
.isSuccess() || !errorCode
.isFailure()) {
187 errln("ErrorCode did not yield a writable pointer");
189 errorCode
.set(U_PARSE_ERROR
);
190 if(errorCode
.get()!=U_PARSE_ERROR
|| errorCode
.isSuccess() || !errorCode
.isFailure()) {
191 errln("ErrorCode.set() failed");
193 if( errorCode
.reset()!=U_PARSE_ERROR
|| errorCode
.get()!=U_ZERO_ERROR
||
194 !errorCode
.isSuccess() || errorCode
.isFailure()
196 errln("ErrorCode did not reset properly");
200 class MyErrorCode
: public ErrorCode
{
202 MyErrorCode(int32_t &countChecks
, int32_t &countDests
)
203 : checks(countChecks
), dests(countDests
) {}
210 virtual void handleFailure() const {
217 void ErrorCodeTest::TestSubclass() {
218 int32_t countChecks
=0;
219 int32_t countDests
=0;
221 MyErrorCode
errorCode(countChecks
, countDests
);
222 if( errorCode
.get()!=U_ZERO_ERROR
|| !errorCode
.isSuccess() || errorCode
.isFailure() ||
223 countChecks
!=0 || countDests
!=0
225 errln("ErrorCode did not initialize properly");
228 errorCode
.assertSuccess();
230 errln("ErrorCode.assertSuccess() called handleFailure() despite success");
232 RefPlusOne(errorCode
);
233 if(errorCode
.get()!=U_ILLEGAL_ARGUMENT_ERROR
|| errorCode
.isSuccess() || !errorCode
.isFailure()) {
234 errln("ErrorCode did not yield a writable reference");
236 errorCode
.assertSuccess();
238 errln("ErrorCode.assertSuccess() did not handleFailure()");
240 PtrPlusTwo(errorCode
);
241 if(errorCode
.get()!=U_INVALID_FORMAT_ERROR
|| errorCode
.isSuccess() || !errorCode
.isFailure()) {
242 errln("ErrorCode did not yield a writable pointer");
244 errorCode
.assertSuccess();
246 errln("ErrorCode.assertSuccess() did not handleFailure()");
248 errorCode
.set(U_PARSE_ERROR
);
249 if(errorCode
.get()!=U_PARSE_ERROR
|| errorCode
.isSuccess() || !errorCode
.isFailure()) {
250 errln("ErrorCode.set() failed");
252 if( errorCode
.reset()!=U_PARSE_ERROR
|| errorCode
.get()!=U_ZERO_ERROR
||
253 !errorCode
.isSuccess() || errorCode
.isFailure()
255 errln("ErrorCode did not reset properly");
257 errorCode
.assertSuccess();
259 errln("ErrorCode.assertSuccess() called handleFailure() despite success");
263 errln("MyErrorCode destructor detected failure despite success");
265 countChecks
=countDests
=0;
267 MyErrorCode
errorCode(countChecks
, countDests
);
268 errorCode
.set(U_PARSE_ERROR
);
271 errln("MyErrorCode destructor failed to detect failure");
275 class IcuTestErrorCodeTestHelper
: public IntlTest
{
277 void errln( const UnicodeString
&message
) U_OVERRIDE
{
278 test
->assertFalse("Already saw an error", seenError
);
280 test
->assertEquals("Message for Error", expectedErrln
, message
);
281 if (expectedDataErr
) {
282 test
->errln("Got non-data error, but expected data error");
286 void dataerrln( const UnicodeString
&message
) U_OVERRIDE
{
287 test
->assertFalse("Already saw an error", seenError
);
289 test
->assertEquals("Message for Error", expectedErrln
, message
);
290 if (!expectedDataErr
) {
291 test
->errln("Got data error, but expected non-data error");
296 UBool expectedDataErr
;
297 UnicodeString expectedErrln
;
301 void ErrorCodeTest::TestIcuTestErrorCode() {
302 IcuTestErrorCodeTestHelper helper
;
305 // Test destructor message
306 helper
.expectedErrln
= u
"AAA destructor: expected success but got error: U_ILLEGAL_PAD_POSITION";
307 helper
.expectedDataErr
= FALSE
;
308 helper
.seenError
= FALSE
;
310 IcuTestErrorCode
testStatus(helper
, "AAA");
311 testStatus
.set(U_ILLEGAL_PAD_POSITION
);
313 assertTrue("Should have seen an error", helper
.seenError
);
315 // Test destructor message with scope
316 helper
.expectedErrln
= u
"BBB destructor: expected success but got error: U_ILLEGAL_PAD_POSITION scope: foo";
317 helper
.expectedDataErr
= FALSE
;
318 helper
.seenError
= FALSE
;
320 IcuTestErrorCode
testStatus(helper
, "BBB");
321 testStatus
.setScope("foo");
322 testStatus
.set(U_ILLEGAL_PAD_POSITION
);
324 assertTrue("Should have seen an error", helper
.seenError
);
326 // Check errIfFailure message with scope
327 helper
.expectedErrln
= u
"CCC expected success but got error: U_ILLEGAL_PAD_POSITION scope: foo";
328 helper
.expectedDataErr
= FALSE
;
329 helper
.seenError
= FALSE
;
331 IcuTestErrorCode
testStatus(helper
, "CCC");
332 testStatus
.setScope("foo");
333 testStatus
.set(U_ILLEGAL_PAD_POSITION
);
334 testStatus
.errIfFailureAndReset();
335 assertTrue("Should have seen an error", helper
.seenError
);
336 helper
.seenError
= FALSE
;
337 helper
.expectedErrln
= u
"CCC expected success but got error: U_ILLEGAL_CHAR_FOUND scope: foo - 5.4300";
338 testStatus
.set(U_ILLEGAL_CHAR_FOUND
);
339 testStatus
.errIfFailureAndReset("%6.4f", 5.43);
340 assertTrue("Should have seen an error", helper
.seenError
);
343 // Check errDataIfFailure message without scope
344 helper
.expectedErrln
= u
"DDD data: expected success but got error: U_ILLEGAL_PAD_POSITION";
345 helper
.expectedDataErr
= TRUE
;
346 helper
.seenError
= FALSE
;
348 IcuTestErrorCode
testStatus(helper
, "DDD");
349 testStatus
.set(U_ILLEGAL_PAD_POSITION
);
350 testStatus
.errDataIfFailureAndReset();
351 assertTrue("Should have seen an error", helper
.seenError
);
352 helper
.seenError
= FALSE
;
353 helper
.expectedErrln
= u
"DDD data: expected success but got error: U_ILLEGAL_CHAR_FOUND - 5.4300";
354 testStatus
.set(U_ILLEGAL_CHAR_FOUND
);
355 testStatus
.errDataIfFailureAndReset("%6.4f", 5.43);
356 assertTrue("Should have seen an error", helper
.seenError
);
359 // Check expectFailure
360 helper
.expectedErrln
= u
"EEE expected: U_ILLEGAL_CHAR_FOUND but got error: U_ILLEGAL_PAD_POSITION";
361 helper
.expectedDataErr
= FALSE
;
362 helper
.seenError
= FALSE
;
364 IcuTestErrorCode
testStatus(helper
, "EEE");
365 testStatus
.set(U_ILLEGAL_PAD_POSITION
);
366 testStatus
.expectErrorAndReset(U_ILLEGAL_PAD_POSITION
);
367 assertFalse("Should NOT have seen an error", helper
.seenError
);
368 testStatus
.set(U_ILLEGAL_PAD_POSITION
);
369 testStatus
.expectErrorAndReset(U_ILLEGAL_CHAR_FOUND
);
370 assertTrue("Should have seen an error", helper
.seenError
);
371 helper
.seenError
= FALSE
;
372 helper
.expectedErrln
= u
"EEE expected: U_ILLEGAL_CHAR_FOUND but got error: U_ZERO_ERROR scope: scopety scope - 5.4300";
373 testStatus
.setScope("scopety scope");
374 testStatus
.set(U_ILLEGAL_PAD_POSITION
);
375 testStatus
.expectErrorAndReset(U_ILLEGAL_PAD_POSITION
, "%6.4f", 5.43);
376 assertFalse("Should NOT have seen an error", helper
.seenError
);
377 testStatus
.expectErrorAndReset(U_ILLEGAL_CHAR_FOUND
, "%6.4f", 5.43);
378 assertTrue("Should have seen an error", helper
.seenError
);
383 class LocalPointerTest
: public IntlTest
{
385 LocalPointerTest() {}
387 void runIndexedTest(int32_t index
, UBool exec
, const char *&name
, char *par
=NULL
);
389 void TestLocalPointer();
390 void TestLocalPointerMoveSwap();
391 void TestLocalPointerStdUniquePtr();
392 void TestLocalArray();
393 void TestLocalArrayMoveSwap();
394 void TestLocalArrayStdUniquePtr();
395 void TestLocalXyzPointer();
396 void TestLocalXyzPointerMoveSwap();
397 void TestLocalXyzPointerNull();
398 void TestLocalXyzStdUniquePtr();
401 static IntlTest
*createLocalPointerTest() {
402 return new LocalPointerTest();
405 void LocalPointerTest::runIndexedTest(int32_t index
, UBool exec
, const char *&name
, char * /*par*/) {
407 logln("TestSuite LocalPointerTest: ");
410 TESTCASE_AUTO(TestLocalPointer
);
411 TESTCASE_AUTO(TestLocalPointerMoveSwap
);
412 TESTCASE_AUTO(TestLocalPointerStdUniquePtr
);
413 TESTCASE_AUTO(TestLocalArray
);
414 TESTCASE_AUTO(TestLocalArrayMoveSwap
);
415 TESTCASE_AUTO(TestLocalArrayStdUniquePtr
);
416 TESTCASE_AUTO(TestLocalXyzPointer
);
417 TESTCASE_AUTO(TestLocalXyzPointerMoveSwap
);
418 TESTCASE_AUTO(TestLocalXyzPointerNull
);
419 TESTCASE_AUTO(TestLocalXyzStdUniquePtr
);
423 // Exercise almost every LocalPointer and LocalPointerBase method.
424 void LocalPointerTest::TestLocalPointer() {
426 LocalPointer
<UnicodeString
> s(new UnicodeString((UChar32
)0x50005));
427 // isNULL(), isValid(), operator==(), operator!=()
428 if(s
.isNull() || !s
.isValid() || s
==NULL
|| !(s
!=NULL
)) {
429 errln("LocalPointer constructor or NULL test failure");
432 // getAlias(), operator->, operator*
433 if(s
.getAlias()->length()!=2 || s
->length()!=2 || (*s
).length()!=2) {
434 errln("LocalPointer access failure");
436 // adoptInstead(), orphan()
437 s
.adoptInstead(new UnicodeString((UChar
)0xfffc));
439 errln("LocalPointer adoptInstead(U+FFFC) failure");
441 UnicodeString
*orphan
=s
.orphan();
442 if(orphan
==NULL
|| orphan
->length()!=1 || s
.isValid() || s
!=NULL
) {
443 errln("LocalPointer orphan() failure");
446 s
.adoptInstead(new UnicodeString());
448 errln("LocalPointer adoptInstead(empty) failure");
451 // LocalPointer(p, errorCode) sets U_MEMORY_ALLOCATION_ERROR if p==NULL.
452 UErrorCode errorCode
= U_ZERO_ERROR
;
453 LocalPointer
<CharString
> csx(new CharString("some chars", errorCode
), errorCode
);
454 if(csx
.isNull() && U_SUCCESS(errorCode
)) {
455 errln("LocalPointer(p, errorCode) failure");
458 errorCode
= U_ZERO_ERROR
;
459 csx
.adoptInsteadAndCheckErrorCode(new CharString("different chars", errorCode
), errorCode
);
460 if(csx
.isNull() && U_SUCCESS(errorCode
)) {
461 errln("adoptInsteadAndCheckErrorCode(p, errorCode) failure");
464 // Incoming failure: Keep the current object and delete the input object.
465 errorCode
= U_ILLEGAL_ARGUMENT_ERROR
;
466 csx
.adoptInsteadAndCheckErrorCode(new CharString("unused", errorCode
), errorCode
);
467 if(csx
.isValid() && strcmp(csx
->data(), "different chars") != 0) {
468 errln("adoptInsteadAndCheckErrorCode(p, U_FAILURE) did not retain the old object");
471 errorCode
= U_ZERO_ERROR
;
472 csx
.adoptInsteadAndCheckErrorCode(NULL
, errorCode
);
473 if(errorCode
!= U_MEMORY_ALLOCATION_ERROR
) {
474 errln("adoptInsteadAndCheckErrorCode(NULL, errorCode) did not set U_MEMORY_ALLOCATION_ERROR");
478 errln("adoptInsteadAndCheckErrorCode(NULL, errorCode) kept the object");
481 errorCode
= U_ZERO_ERROR
;
482 LocalPointer
<CharString
> null(NULL
, errorCode
);
483 if(errorCode
!= U_MEMORY_ALLOCATION_ERROR
) {
484 errln("LocalPointer(NULL, errorCode) did not set U_MEMORY_ALLOCATION_ERROR");
491 // Try to avoid clang -Wself-move warnings from s1 = std::move(s1);
493 void moveFrom(T
&dest
, T
&src
) {
494 dest
= std::move(src
);
497 void LocalPointerTest::TestLocalPointerMoveSwap() {
498 UnicodeString
*p1
= new UnicodeString((UChar
)0x61);
499 UnicodeString
*p2
= new UnicodeString((UChar
)0x62);
500 LocalPointer
<UnicodeString
> s1(p1
);
501 LocalPointer
<UnicodeString
> s2(p2
);
503 if(s1
.getAlias() != p2
|| s2
.getAlias() != p1
) {
504 errln("LocalPointer.swap() did not swap");
507 if(s1
.getAlias() != p1
|| s2
.getAlias() != p2
) {
508 errln("swap(LocalPointer) did not swap back");
510 LocalPointer
<UnicodeString
> s3
;
512 if(s3
.getAlias() != p1
|| s1
.isValid()) {
513 errln("LocalPointer = std::move() did not move");
515 infoln("TestLocalPointerMoveSwap() with rvalue references");
516 s1
= static_cast<LocalPointer
<UnicodeString
> &&>(s3
);
517 if(s1
.getAlias() != p1
|| s3
.isValid()) {
518 errln("LocalPointer move assignment operator did not move");
520 LocalPointer
<UnicodeString
> s4(static_cast<LocalPointer
<UnicodeString
> &&>(s2
));
521 if(s4
.getAlias() != p2
|| s2
.isValid()) {
522 errln("LocalPointer move constructor did not move");
525 // Move self assignment leaves the object valid but in an undefined state.
526 // Do it to make sure there is no crash,
527 // but do not check for any particular resulting value.
532 void LocalPointerTest::TestLocalPointerStdUniquePtr() {
533 auto* ptr
= new UnicodeString((UChar32
)0x50005);
534 // Implicit conversion operator
535 std::unique_ptr
<UnicodeString
> s
= LocalPointer
<UnicodeString
>(ptr
);
536 // Explicit move constructor
537 LocalPointer
<UnicodeString
> s2(std::move(s
));
538 // Conversion operator should also work with std::move
540 // Back again with move assignment
542 assertTrue("Pointer should remain the same", ptr
== s2
.getAlias());
545 // Exercise almost every LocalArray method (but not LocalPointerBase).
546 void LocalPointerTest::TestLocalArray() {
548 LocalArray
<UnicodeString
> a(new UnicodeString
[2]);
550 a
[0].append((UChar
)0x61);
551 a
[1].append((UChar32
)0x60006);
552 if(a
[0].length()!=1 || a
[1].length()!=2) {
553 errln("LocalArray access failure");
556 a
.adoptInstead(new UnicodeString
[4]);
557 a
[3].append((UChar
)0x62).append((UChar
)0x63).reverse();
558 if(a
[3].length()!=2 || a
[3][1]!=0x62) {
559 errln("LocalArray adoptInstead() failure");
562 // LocalArray(p, errorCode) sets U_MEMORY_ALLOCATION_ERROR if p==NULL.
563 UErrorCode errorCode
= U_ZERO_ERROR
;
564 LocalArray
<UnicodeString
> ua(new UnicodeString
[3], errorCode
);
565 if(ua
.isNull() && U_SUCCESS(errorCode
)) {
566 errln("LocalArray(p, errorCode) failure");
569 errorCode
= U_ZERO_ERROR
;
570 UnicodeString
*u4
= new UnicodeString
[4];
571 ua
.adoptInsteadAndCheckErrorCode(u4
, errorCode
);
572 if(ua
.isNull() && U_SUCCESS(errorCode
)) {
573 errln("adoptInsteadAndCheckErrorCode(p, errorCode) failure");
576 // Incoming failure: Keep the current object and delete the input object.
577 errorCode
= U_ILLEGAL_ARGUMENT_ERROR
;
578 ua
.adoptInsteadAndCheckErrorCode(new UnicodeString
[5], errorCode
);
579 if(ua
.isValid() && ua
.getAlias() != u4
) {
580 errln("adoptInsteadAndCheckErrorCode(p, U_FAILURE) did not retain the old array");
583 errorCode
= U_ZERO_ERROR
;
584 ua
.adoptInsteadAndCheckErrorCode(NULL
, errorCode
);
585 if(errorCode
!= U_MEMORY_ALLOCATION_ERROR
) {
586 errln("adoptInsteadAndCheckErrorCode(NULL, errorCode) did not set U_MEMORY_ALLOCATION_ERROR");
590 errln("adoptInsteadAndCheckErrorCode(NULL, errorCode) kept the array");
593 errorCode
= U_ZERO_ERROR
;
594 LocalArray
<UnicodeString
> null(NULL
, errorCode
);
595 if(errorCode
!= U_MEMORY_ALLOCATION_ERROR
) {
596 errln("LocalArray(NULL, errorCode) did not set U_MEMORY_ALLOCATION_ERROR");
603 void LocalPointerTest::TestLocalArrayMoveSwap() {
604 UnicodeString
*p1
= new UnicodeString
[2];
605 UnicodeString
*p2
= new UnicodeString
[3];
606 LocalArray
<UnicodeString
> a1(p1
);
607 LocalArray
<UnicodeString
> a2(p2
);
609 if(a1
.getAlias() != p2
|| a2
.getAlias() != p1
) {
610 errln("LocalArray.swap() did not swap");
613 if(a1
.getAlias() != p1
|| a2
.getAlias() != p2
) {
614 errln("swap(LocalArray) did not swap back");
616 LocalArray
<UnicodeString
> a3
;
618 if(a3
.getAlias() != p1
|| a1
.isValid()) {
619 errln("LocalArray = std::move() did not move");
621 infoln("TestLocalArrayMoveSwap() with rvalue references");
622 a1
= static_cast<LocalArray
<UnicodeString
> &&>(a3
);
623 if(a1
.getAlias() != p1
|| a3
.isValid()) {
624 errln("LocalArray move assignment operator did not move");
626 LocalArray
<UnicodeString
> a4(static_cast<LocalArray
<UnicodeString
> &&>(a2
));
627 if(a4
.getAlias() != p2
|| a2
.isValid()) {
628 errln("LocalArray move constructor did not move");
631 // Move self assignment leaves the object valid but in an undefined state.
632 // Do it to make sure there is no crash,
633 // but do not check for any particular resulting value.
638 void LocalPointerTest::TestLocalArrayStdUniquePtr() {
639 auto* ptr
= new UnicodeString
[2];
640 // Implicit conversion operator
641 std::unique_ptr
<UnicodeString
[]> a
= LocalArray
<UnicodeString
>(ptr
);
642 // Explicit move constructor
643 LocalArray
<UnicodeString
> a2(std::move(a
));
644 // Conversion operator should also work with std::move
646 // Back again with move assignment
648 assertTrue("Pointer should remain the same", ptr
== a2
.getAlias());
651 #include "unicode/ucnvsel.h"
652 #include "unicode/ucal.h"
653 #include "unicode/udatpg.h"
654 #include "unicode/uidna.h"
655 #include "unicode/uldnames.h"
656 #include "unicode/umsg.h"
657 #include "unicode/unorm2.h"
658 #include "unicode/uregex.h"
659 #include "unicode/utrans.h"
660 #include "unicode/uformattedvalue.h"
662 // Use LocalXyzPointer types that are not covered elsewhere in the intltest suite.
663 void LocalPointerTest::TestLocalXyzPointer() {
664 IcuTestErrorCode
errorCode(*this, "TestLocalXyzPointer");
666 static const char *const encoding
="ISO-8859-1";
667 LocalUConverterSelectorPointer
sel(
668 ucnvsel_open(&encoding
, 1, NULL
, UCNV_ROUNDTRIP_SET
, errorCode
));
669 if(errorCode
.errIfFailureAndReset("ucnvsel_open()")) {
673 errln("LocalUConverterSelectorPointer failure");
677 #if !UCONFIG_NO_FORMATTING
678 LocalUCalendarPointer
cal(ucal_open(NULL
, 0, "root", UCAL_GREGORIAN
, errorCode
));
679 if(errorCode
.errDataIfFailureAndReset("ucal_open()")) {
683 errln("LocalUCalendarPointer failure");
687 LocalUDateTimePatternGeneratorPointer
patgen(udatpg_open("root", errorCode
));
688 if(errorCode
.errDataIfFailureAndReset("udatpg_open()")) {
691 if(patgen
.isNull()) {
692 errln("LocalUDateTimePatternGeneratorPointer failure");
696 LocalULocaleDisplayNamesPointer
ldn(uldn_open("de-CH", ULDN_STANDARD_NAMES
, errorCode
));
697 if(errorCode
.errIfFailureAndReset("uldn_open()")) {
701 errln("LocalULocaleDisplayNamesPointer failure");
705 UnicodeString hello
=UNICODE_STRING_SIMPLE("Hello {0}!");
706 LocalUMessageFormatPointer
msg(
707 umsg_open(hello
.getBuffer(), hello
.length(), "root", NULL
, errorCode
));
708 if(errorCode
.errIfFailureAndReset("umsg_open()")) {
712 errln("LocalUMessageFormatPointer failure");
715 #endif /* UCONFIG_NO_FORMATTING */
717 #if !UCONFIG_NO_NORMALIZATION
718 const UNormalizer2
*nfc
=unorm2_getNFCInstance(errorCode
);
720 LocalUNormalizer2Pointer
fn2(unorm2_openFiltered(nfc
, emptySet
.toUSet(), errorCode
));
721 if(errorCode
.errIfFailureAndReset("unorm2_openFiltered()")) {
725 errln("LocalUNormalizer2Pointer failure");
728 #endif /* !UCONFIG_NO_NORMALIZATION */
731 LocalUIDNAPointer
idna(uidna_openUTS46(0, errorCode
));
732 if(errorCode
.errIfFailureAndReset("uidna_openUTS46()")) {
736 errln("LocalUIDNAPointer failure");
739 #endif /* !UCONFIG_NO_IDNA */
741 #if !UCONFIG_NO_REGULAR_EXPRESSIONS
742 UnicodeString pattern
=UNICODE_STRING_SIMPLE("abc|xy+z");
743 LocalURegularExpressionPointer
regex(
744 uregex_open(pattern
.getBuffer(), pattern
.length(), 0, NULL
, errorCode
));
745 if(errorCode
.errIfFailureAndReset("uregex_open()")) {
749 errln("LocalURegularExpressionPointer failure");
752 #endif /* UCONFIG_NO_REGULAR_EXPRESSIONS */
754 #if !UCONFIG_NO_TRANSLITERATION
755 UnicodeString id
=UNICODE_STRING_SIMPLE("Grek-Latn");
756 LocalUTransliteratorPointer
trans(
757 utrans_openU(id
.getBuffer(), id
.length(), UTRANS_FORWARD
, NULL
, 0, NULL
, errorCode
));
758 if(errorCode
.errIfFailureAndReset("utrans_open()")) {
762 errln("LocalUTransliteratorPointer failure");
765 #endif /* !UCONFIG_NO_TRANSLITERATION */
770 void LocalPointerTest::TestLocalXyzPointerMoveSwap() {
771 #if !UCONFIG_NO_NORMALIZATION
772 IcuTestErrorCode
errorCode(*this, "TestLocalXyzPointerMoveSwap");
773 const UNormalizer2
*nfc
=unorm2_getNFCInstance(errorCode
);
774 const UNormalizer2
*nfd
=unorm2_getNFDInstance(errorCode
);
775 if(errorCode
.errIfFailureAndReset("unorm2_getNF[CD]Instance()")) {
779 UNormalizer2
*p1
= unorm2_openFiltered(nfc
, emptySet
.toUSet(), errorCode
);
780 UNormalizer2
*p2
= unorm2_openFiltered(nfd
, emptySet
.toUSet(), errorCode
);
781 LocalUNormalizer2Pointer
f1(p1
);
782 LocalUNormalizer2Pointer
f2(p2
);
783 if(errorCode
.errIfFailureAndReset("unorm2_openFiltered()")) {
786 if(f1
.isNull() || f2
.isNull()) {
787 errln("LocalUNormalizer2Pointer failure");
791 if(f1
.getAlias() != p2
|| f2
.getAlias() != p1
) {
792 errln("LocalUNormalizer2Pointer.swap() did not swap");
795 if(f1
.getAlias() != p1
|| f2
.getAlias() != p2
) {
796 errln("swap(LocalUNormalizer2Pointer) did not swap back");
798 LocalUNormalizer2Pointer f3
;
800 if(f3
.getAlias() != p1
|| f1
.isValid()) {
801 errln("LocalUNormalizer2Pointer = std::move() did not move");
803 infoln("TestLocalXyzPointerMoveSwap() with rvalue references");
804 f1
= static_cast<LocalUNormalizer2Pointer
&&>(f3
);
805 if(f1
.getAlias() != p1
|| f3
.isValid()) {
806 errln("LocalUNormalizer2Pointer move assignment operator did not move");
808 LocalUNormalizer2Pointer
f4(static_cast<LocalUNormalizer2Pointer
&&>(f2
));
809 if(f4
.getAlias() != p2
|| f2
.isValid()) {
810 errln("LocalUNormalizer2Pointer move constructor did not move");
812 // Move self assignment leaves the object valid but in an undefined state.
813 // Do it to make sure there is no crash,
814 // but do not check for any particular resulting value.
817 #endif /* !UCONFIG_NO_NORMALIZATION */
820 // Try LocalXyzPointer types with NULL pointers.
821 void LocalPointerTest::TestLocalXyzPointerNull() {
823 IcuTestErrorCode
errorCode(*this, "TestLocalXyzPointerNull/LocalUConverterSelectorPointer");
824 static const char *const encoding
="ISO-8859-1";
825 LocalUConverterSelectorPointer null
;
826 LocalUConverterSelectorPointer
sel(
827 ucnvsel_open(&encoding
, 1, NULL
, UCNV_ROUNDTRIP_SET
, errorCode
));
828 sel
.adoptInstead(NULL
);
830 #if !UCONFIG_NO_FORMATTING
832 IcuTestErrorCode
errorCode(*this, "TestLocalXyzPointerNull/LocalUCalendarPointer");
833 LocalUCalendarPointer null
;
834 LocalUCalendarPointer
cal(ucal_open(NULL
, 0, "root", UCAL_GREGORIAN
, errorCode
));
835 if(!errorCode
.errDataIfFailureAndReset("ucal_open()")) {
836 cal
.adoptInstead(NULL
);
840 IcuTestErrorCode
errorCode(*this, "TestLocalXyzPointerNull/LocalUDateTimePatternGeneratorPointer");
841 LocalUDateTimePatternGeneratorPointer null
;
842 LocalUDateTimePatternGeneratorPointer
patgen(udatpg_open("root", errorCode
));
843 patgen
.adoptInstead(NULL
);
846 IcuTestErrorCode
errorCode(*this, "TestLocalXyzPointerNull/LocalUMessageFormatPointer");
847 UnicodeString hello
=UNICODE_STRING_SIMPLE("Hello {0}!");
848 LocalUMessageFormatPointer null
;
849 LocalUMessageFormatPointer
msg(
850 umsg_open(hello
.getBuffer(), hello
.length(), "root", NULL
, errorCode
));
851 msg
.adoptInstead(NULL
);
853 #endif /* !UCONFIG_NO_FORMATTING */
855 #if !UCONFIG_NO_REGULAR_EXPRESSIONS
857 IcuTestErrorCode
errorCode(*this, "TestLocalXyzPointerNull/LocalURegularExpressionPointer");
858 UnicodeString pattern
=UNICODE_STRING_SIMPLE("abc|xy+z");
859 LocalURegularExpressionPointer null
;
860 LocalURegularExpressionPointer
regex(
861 uregex_open(pattern
.getBuffer(), pattern
.length(), 0, NULL
, errorCode
));
862 if(!errorCode
.errDataIfFailureAndReset("urege_open()")) {
863 regex
.adoptInstead(NULL
);
866 #endif /* !UCONFIG_NO_REGULAR_EXPRESSIONS */
868 #if !UCONFIG_NO_TRANSLITERATION
870 IcuTestErrorCode
errorCode(*this, "TestLocalXyzPointerNull/LocalUTransliteratorPointer");
871 UnicodeString id
=UNICODE_STRING_SIMPLE("Grek-Latn");
872 LocalUTransliteratorPointer null
;
873 LocalUTransliteratorPointer
trans(
874 utrans_openU(id
.getBuffer(), id
.length(), UTRANS_FORWARD
, NULL
, 0, NULL
, errorCode
));
875 if(!errorCode
.errDataIfFailureAndReset("utrans_openU()")) {
876 trans
.adoptInstead(NULL
);
879 #endif /* !UCONFIG_NO_TRANSLITERATION */
883 void LocalPointerTest::TestLocalXyzStdUniquePtr() {
884 IcuTestErrorCode
status(*this, "TestLocalXyzStdUniquePtr");
885 #if !UCONFIG_NO_FORMATTING
886 auto* ptr
= ucfpos_open(status
);
887 // Implicit conversion operator
888 std::unique_ptr
<UConstrainedFieldPosition
, void(*)(UConstrainedFieldPosition
*)> a
=
889 LocalUConstrainedFieldPositionPointer(ptr
);
890 // Explicit move constructor
891 LocalUConstrainedFieldPositionPointer
a2(std::move(a
));
892 // Conversion operator should also work with std::move
894 // Back again with move assignment
896 assertTrue("Pointer should remain the same", ptr
== a2
.getAlias());
897 #endif // UCONFIG_NO_FORMATTING
901 #include "unicode/enumset.h"
903 class EnumSetTest
: public IntlTest
{
906 virtual void runIndexedTest(int32_t index
, UBool exec
, const char *&name
, char *par
=NULL
);
910 static IntlTest
*createEnumSetTest() {
911 return new EnumSetTest();
914 void EnumSetTest::runIndexedTest(int32_t index
, UBool exec
, const char *&name
, char * /*par*/) {
916 TESTCASE_AUTO(TestEnumSet
);
927 void EnumSetTest::TestEnumSet() {
933 logln("Enum is from [%d..%d]\n", MAX_NONBOOLEAN
+1,
936 assertFalse(WHERE
, flags
.get(THING1
));
937 assertFalse(WHERE
, flags
.get(THING2
));
938 assertFalse(WHERE
, flags
.get(THING3
));
940 logln("get(thing1)=%d, get(thing2)=%d, get(thing3)=%d\n", flags
.get(THING1
), flags
.get(THING2
), flags
.get(THING3
));
941 logln("Value now: %d\n", flags
.getAll());
943 logln("clear -Value now: %d\n", flags
.getAll());
944 logln("get(thing1)=%d, get(thing2)=%d, get(thing3)=%d\n", flags
.get(THING1
), flags
.get(THING2
), flags
.get(THING3
));
945 assertFalse(WHERE
, flags
.get(THING1
));
946 assertFalse(WHERE
, flags
.get(THING2
));
947 assertFalse(WHERE
, flags
.get(THING3
));
949 logln("set THING1 -Value now: %d\n", flags
.getAll());
950 assertTrue(WHERE
, flags
.get(THING1
));
951 assertFalse(WHERE
, flags
.get(THING2
));
952 assertFalse(WHERE
, flags
.get(THING3
));
953 logln("get(thing1)=%d, get(thing2)=%d, get(thing3)=%d\n", flags
.get(THING1
), flags
.get(THING2
), flags
.get(THING3
));
955 logln("set THING3 -Value now: %d\n", flags
.getAll());
956 assertTrue(WHERE
, flags
.get(THING1
));
957 assertFalse(WHERE
, flags
.get(THING2
));
958 assertTrue(WHERE
, flags
.get(THING3
));
959 logln("get(thing1)=%d, get(thing2)=%d, get(thing3)=%d\n", flags
.get(THING1
), flags
.get(THING2
), flags
.get(THING3
));
960 flags
.remove(THING2
);
961 assertTrue(WHERE
, flags
.get(THING1
));
962 assertFalse(WHERE
, flags
.get(THING2
));
963 assertTrue(WHERE
, flags
.get(THING3
));
964 logln("remove THING2 -Value now: %d\n", flags
.getAll());
965 logln("get(thing1)=%d, get(thing2)=%d, get(thing3)=%d\n", flags
.get(THING1
), flags
.get(THING2
), flags
.get(THING3
));
966 flags
.remove(THING1
);
967 assertFalse(WHERE
, flags
.get(THING1
));
968 assertFalse(WHERE
, flags
.get(THING2
));
969 assertTrue(WHERE
, flags
.get(THING3
));
970 logln("remove THING1 -Value now: %d\n", flags
.getAll());
971 logln("get(thing1)=%d, get(thing2)=%d, get(thing3)=%d\n", flags
.get(THING1
), flags
.get(THING2
), flags
.get(THING3
));
974 logln("clear -Value now: %d\n", flags
.getAll());
975 logln("get(thing1)=%d, get(thing2)=%d, get(thing3)=%d\n", flags
.get(THING1
), flags
.get(THING2
), flags
.get(THING3
));
976 assertFalse(WHERE
, flags
.get(THING1
));
977 assertFalse(WHERE
, flags
.get(THING2
));
978 assertFalse(WHERE
, flags
.get(THING3
));