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 extern IntlTest
*createLocaleMatcherTest();
39 static IntlTest
*createLocalPointerTest();
40 extern IntlTest
*createUCharsTrieTest();
41 static IntlTest
*createEnumSetTest();
42 extern IntlTest
*createSimpleFormatterTest();
43 extern IntlTest
*createUnifiedCacheTest();
44 extern IntlTest
*createQuantityFormatterTest();
45 extern IntlTest
*createPluralMapTest();
46 #if !UCONFIG_NO_FORMATTING
47 extern IntlTest
*createStaticUnicodeSetsTest();
50 void IntlTestUtilities::runIndexedTest( int32_t index
, UBool exec
, const char* &name
, char* par
)
52 if (exec
) logln("TestSuite Utilities: ");
54 TESTCASE_AUTO_CLASS(MultithreadTest
);
55 TESTCASE_AUTO_CLASS(StringTest
);
56 TESTCASE_AUTO_CLASS(UnicodeStringTest
);
57 TESTCASE_AUTO_CLASS(LocaleTest
);
58 TESTCASE_AUTO_CLASS(CharIterTest
);
59 TESTCASE_AUTO_CLASS(UObjectTest
);
60 TESTCASE_AUTO_CLASS(UnicodeTest
);
61 TESTCASE_AUTO_CLASS(ResourceBundleTest
);
62 TESTCASE_AUTO_CLASS(NewResourceBundleTest
);
63 TESTCASE_AUTO_CLASS(PUtilTest
);
64 TESTCASE_AUTO_CLASS(UVector32Test
);
65 TESTCASE_AUTO_CLASS(UVectorTest
);
66 TESTCASE_AUTO_CLASS(UTextTest
);
67 TESTCASE_AUTO_CLASS(LocaleAliasTest
);
68 TESTCASE_AUTO_CLASS(UnicodeSetTest
);
69 TESTCASE_AUTO_CLASS(ErrorCodeTest
);
70 TESTCASE_AUTO_CREATE_CLASS(LocalPointerTest
);
71 TESTCASE_AUTO_CREATE_CLASS(BytesTrieTest
);
72 TESTCASE_AUTO_CREATE_CLASS(UCharsTrieTest
);
73 TESTCASE_AUTO_CREATE_CLASS(EnumSetTest
);
74 TESTCASE_AUTO_CREATE_CLASS(SimpleFormatterTest
);
75 TESTCASE_AUTO_CREATE_CLASS(UnifiedCacheTest
);
76 TESTCASE_AUTO_CREATE_CLASS(QuantityFormatterTest
);
77 TESTCASE_AUTO_CREATE_CLASS(PluralMapTest
);
78 #if !UCONFIG_NO_FORMATTING
79 TESTCASE_AUTO_CREATE_CLASS(StaticUnicodeSetsTest
);
81 TESTCASE_AUTO_CLASS(LocaleBuilderTest
);
82 TESTCASE_AUTO_CREATE_CLASS(LocaleMatcherTest
);
86 void ErrorCodeTest::runIndexedTest(int32_t index
, UBool exec
, const char* &name
, char* /*par*/) {
87 if (exec
) logln("TestSuite Utilities: ");
89 case 0: name
= "TestErrorCode"; if (exec
) TestErrorCode(); break;
90 case 1: name
= "TestSubclass"; if (exec
) TestSubclass(); break;
91 case 2: name
= "TestIcuTestErrorCode"; if (exec
) TestIcuTestErrorCode(); break;
92 default: name
= ""; break; //needed to end loop
96 static void RefPlusOne(UErrorCode
&code
) { code
=(UErrorCode
)(code
+1); }
97 static void PtrPlusTwo(UErrorCode
*code
) { *code
=(UErrorCode
)(*code
+2); }
99 void ErrorCodeTest::TestErrorCode() {
101 if(errorCode
.get()!=U_ZERO_ERROR
|| !errorCode
.isSuccess() || errorCode
.isFailure()) {
102 errln("ErrorCode did not initialize properly");
105 errorCode
.assertSuccess();
106 if(errorCode
.errorName()!=u_errorName(U_ZERO_ERROR
)) {
107 errln("ErrorCode did not format error message string properly");
109 RefPlusOne(errorCode
);
110 if(errorCode
.get()!=U_ILLEGAL_ARGUMENT_ERROR
|| errorCode
.isSuccess() || !errorCode
.isFailure()) {
111 errln("ErrorCode did not yield a writable reference");
113 PtrPlusTwo(errorCode
);
114 if(errorCode
.get()!=U_INVALID_FORMAT_ERROR
|| errorCode
.isSuccess() || !errorCode
.isFailure()) {
115 errln("ErrorCode did not yield a writable pointer");
117 errorCode
.set(U_PARSE_ERROR
);
118 if(errorCode
.get()!=U_PARSE_ERROR
|| errorCode
.isSuccess() || !errorCode
.isFailure()) {
119 errln("ErrorCode.set() failed");
121 if( errorCode
.reset()!=U_PARSE_ERROR
|| errorCode
.get()!=U_ZERO_ERROR
||
122 !errorCode
.isSuccess() || errorCode
.isFailure()
124 errln("ErrorCode did not reset properly");
128 class MyErrorCode
: public ErrorCode
{
130 MyErrorCode(int32_t &countChecks
, int32_t &countDests
)
131 : checks(countChecks
), dests(countDests
) {}
138 virtual void handleFailure() const {
145 void ErrorCodeTest::TestSubclass() {
146 int32_t countChecks
=0;
147 int32_t countDests
=0;
149 MyErrorCode
errorCode(countChecks
, countDests
);
150 if( errorCode
.get()!=U_ZERO_ERROR
|| !errorCode
.isSuccess() || errorCode
.isFailure() ||
151 countChecks
!=0 || countDests
!=0
153 errln("ErrorCode did not initialize properly");
156 errorCode
.assertSuccess();
158 errln("ErrorCode.assertSuccess() called handleFailure() despite success");
160 RefPlusOne(errorCode
);
161 if(errorCode
.get()!=U_ILLEGAL_ARGUMENT_ERROR
|| errorCode
.isSuccess() || !errorCode
.isFailure()) {
162 errln("ErrorCode did not yield a writable reference");
164 errorCode
.assertSuccess();
166 errln("ErrorCode.assertSuccess() did not handleFailure()");
168 PtrPlusTwo(errorCode
);
169 if(errorCode
.get()!=U_INVALID_FORMAT_ERROR
|| errorCode
.isSuccess() || !errorCode
.isFailure()) {
170 errln("ErrorCode did not yield a writable pointer");
172 errorCode
.assertSuccess();
174 errln("ErrorCode.assertSuccess() did not handleFailure()");
176 errorCode
.set(U_PARSE_ERROR
);
177 if(errorCode
.get()!=U_PARSE_ERROR
|| errorCode
.isSuccess() || !errorCode
.isFailure()) {
178 errln("ErrorCode.set() failed");
180 if( errorCode
.reset()!=U_PARSE_ERROR
|| errorCode
.get()!=U_ZERO_ERROR
||
181 !errorCode
.isSuccess() || errorCode
.isFailure()
183 errln("ErrorCode did not reset properly");
185 errorCode
.assertSuccess();
187 errln("ErrorCode.assertSuccess() called handleFailure() despite success");
191 errln("MyErrorCode destructor detected failure despite success");
193 countChecks
=countDests
=0;
195 MyErrorCode
errorCode(countChecks
, countDests
);
196 errorCode
.set(U_PARSE_ERROR
);
199 errln("MyErrorCode destructor failed to detect failure");
203 class IcuTestErrorCodeTestHelper
: public IntlTest
{
205 void errln( const UnicodeString
&message
) U_OVERRIDE
{
206 test
->assertFalse("Already saw an error", seenError
);
208 test
->assertEquals("Message for Error", expectedErrln
, message
);
209 if (expectedDataErr
) {
210 test
->errln("Got non-data error, but expected data error");
214 void dataerrln( const UnicodeString
&message
) U_OVERRIDE
{
215 test
->assertFalse("Already saw an error", seenError
);
217 test
->assertEquals("Message for Error", expectedErrln
, message
);
218 if (!expectedDataErr
) {
219 test
->errln("Got data error, but expected non-data error");
224 UBool expectedDataErr
;
225 UnicodeString expectedErrln
;
229 void ErrorCodeTest::TestIcuTestErrorCode() {
230 IcuTestErrorCodeTestHelper helper
;
233 // Test destructor message
234 helper
.expectedErrln
= u
"AAA destructor: expected success but got error: U_ILLEGAL_PAD_POSITION";
235 helper
.expectedDataErr
= FALSE
;
236 helper
.seenError
= FALSE
;
238 IcuTestErrorCode
testStatus(helper
, "AAA");
239 testStatus
.set(U_ILLEGAL_PAD_POSITION
);
241 assertTrue("Should have seen an error", helper
.seenError
);
243 // Test destructor message with scope
244 helper
.expectedErrln
= u
"BBB destructor: expected success but got error: U_ILLEGAL_PAD_POSITION scope: foo";
245 helper
.expectedDataErr
= FALSE
;
246 helper
.seenError
= FALSE
;
248 IcuTestErrorCode
testStatus(helper
, "BBB");
249 testStatus
.setScope("foo");
250 testStatus
.set(U_ILLEGAL_PAD_POSITION
);
252 assertTrue("Should have seen an error", helper
.seenError
);
254 // Check errIfFailure message with scope
255 helper
.expectedErrln
= u
"CCC expected success but got error: U_ILLEGAL_PAD_POSITION scope: foo";
256 helper
.expectedDataErr
= FALSE
;
257 helper
.seenError
= FALSE
;
259 IcuTestErrorCode
testStatus(helper
, "CCC");
260 testStatus
.setScope("foo");
261 testStatus
.set(U_ILLEGAL_PAD_POSITION
);
262 testStatus
.errIfFailureAndReset();
263 assertTrue("Should have seen an error", helper
.seenError
);
264 helper
.seenError
= FALSE
;
265 helper
.expectedErrln
= u
"CCC expected success but got error: U_ILLEGAL_CHAR_FOUND scope: foo - 5.4300";
266 testStatus
.set(U_ILLEGAL_CHAR_FOUND
);
267 testStatus
.errIfFailureAndReset("%6.4f", 5.43);
268 assertTrue("Should have seen an error", helper
.seenError
);
271 // Check errDataIfFailure message without scope
272 helper
.expectedErrln
= u
"DDD data: expected success but got error: U_ILLEGAL_PAD_POSITION";
273 helper
.expectedDataErr
= TRUE
;
274 helper
.seenError
= FALSE
;
276 IcuTestErrorCode
testStatus(helper
, "DDD");
277 testStatus
.set(U_ILLEGAL_PAD_POSITION
);
278 testStatus
.errDataIfFailureAndReset();
279 assertTrue("Should have seen an error", helper
.seenError
);
280 helper
.seenError
= FALSE
;
281 helper
.expectedErrln
= u
"DDD data: expected success but got error: U_ILLEGAL_CHAR_FOUND - 5.4300";
282 testStatus
.set(U_ILLEGAL_CHAR_FOUND
);
283 testStatus
.errDataIfFailureAndReset("%6.4f", 5.43);
284 assertTrue("Should have seen an error", helper
.seenError
);
287 // Check expectFailure
288 helper
.expectedErrln
= u
"EEE expected: U_ILLEGAL_CHAR_FOUND but got error: U_ILLEGAL_PAD_POSITION";
289 helper
.expectedDataErr
= FALSE
;
290 helper
.seenError
= FALSE
;
292 IcuTestErrorCode
testStatus(helper
, "EEE");
293 testStatus
.set(U_ILLEGAL_PAD_POSITION
);
294 testStatus
.expectErrorAndReset(U_ILLEGAL_PAD_POSITION
);
295 assertFalse("Should NOT have seen an error", helper
.seenError
);
296 testStatus
.set(U_ILLEGAL_PAD_POSITION
);
297 testStatus
.expectErrorAndReset(U_ILLEGAL_CHAR_FOUND
);
298 assertTrue("Should have seen an error", helper
.seenError
);
299 helper
.seenError
= FALSE
;
300 helper
.expectedErrln
= u
"EEE expected: U_ILLEGAL_CHAR_FOUND but got error: U_ZERO_ERROR scope: scopety scope - 5.4300";
301 testStatus
.setScope("scopety scope");
302 testStatus
.set(U_ILLEGAL_PAD_POSITION
);
303 testStatus
.expectErrorAndReset(U_ILLEGAL_PAD_POSITION
, "%6.4f", 5.43);
304 assertFalse("Should NOT have seen an error", helper
.seenError
);
305 testStatus
.expectErrorAndReset(U_ILLEGAL_CHAR_FOUND
, "%6.4f", 5.43);
306 assertTrue("Should have seen an error", helper
.seenError
);
311 class LocalPointerTest
: public IntlTest
{
313 LocalPointerTest() {}
315 void runIndexedTest(int32_t index
, UBool exec
, const char *&name
, char *par
=NULL
);
317 void TestLocalPointer();
318 void TestLocalPointerMoveSwap();
319 void TestLocalPointerStdUniquePtr();
320 void TestLocalArray();
321 void TestLocalArrayMoveSwap();
322 void TestLocalArrayStdUniquePtr();
323 void TestLocalXyzPointer();
324 void TestLocalXyzPointerMoveSwap();
325 void TestLocalXyzPointerNull();
326 void TestLocalXyzStdUniquePtr();
329 static IntlTest
*createLocalPointerTest() {
330 return new LocalPointerTest();
333 void LocalPointerTest::runIndexedTest(int32_t index
, UBool exec
, const char *&name
, char * /*par*/) {
335 logln("TestSuite LocalPointerTest: ");
338 TESTCASE_AUTO(TestLocalPointer
);
339 TESTCASE_AUTO(TestLocalPointerMoveSwap
);
340 TESTCASE_AUTO(TestLocalPointerStdUniquePtr
);
341 TESTCASE_AUTO(TestLocalArray
);
342 TESTCASE_AUTO(TestLocalArrayMoveSwap
);
343 TESTCASE_AUTO(TestLocalArrayStdUniquePtr
);
344 TESTCASE_AUTO(TestLocalXyzPointer
);
345 TESTCASE_AUTO(TestLocalXyzPointerMoveSwap
);
346 TESTCASE_AUTO(TestLocalXyzPointerNull
);
347 TESTCASE_AUTO(TestLocalXyzStdUniquePtr
);
351 // Exercise almost every LocalPointer and LocalPointerBase method.
352 void LocalPointerTest::TestLocalPointer() {
354 LocalPointer
<UnicodeString
> s(new UnicodeString((UChar32
)0x50005));
355 // isNULL(), isValid(), operator==(), operator!=()
356 if(s
.isNull() || !s
.isValid() || s
==NULL
|| !(s
!=NULL
)) {
357 errln("LocalPointer constructor or NULL test failure");
360 // getAlias(), operator->, operator*
361 if(s
.getAlias()->length()!=2 || s
->length()!=2 || (*s
).length()!=2) {
362 errln("LocalPointer access failure");
364 // adoptInstead(), orphan()
365 s
.adoptInstead(new UnicodeString((UChar
)0xfffc));
367 errln("LocalPointer adoptInstead(U+FFFC) failure");
369 UnicodeString
*orphan
=s
.orphan();
370 if(orphan
==NULL
|| orphan
->length()!=1 || s
.isValid() || s
!=NULL
) {
371 errln("LocalPointer orphan() failure");
374 s
.adoptInstead(new UnicodeString());
376 errln("LocalPointer adoptInstead(empty) failure");
379 // LocalPointer(p, errorCode) sets U_MEMORY_ALLOCATION_ERROR if p==NULL.
380 UErrorCode errorCode
= U_ZERO_ERROR
;
381 LocalPointer
<CharString
> csx(new CharString("some chars", errorCode
), errorCode
);
382 if(csx
.isNull() && U_SUCCESS(errorCode
)) {
383 errln("LocalPointer(p, errorCode) failure");
386 errorCode
= U_ZERO_ERROR
;
387 csx
.adoptInsteadAndCheckErrorCode(new CharString("different chars", errorCode
), errorCode
);
388 if(csx
.isNull() && U_SUCCESS(errorCode
)) {
389 errln("adoptInsteadAndCheckErrorCode(p, errorCode) failure");
392 // Incoming failure: Keep the current object and delete the input object.
393 errorCode
= U_ILLEGAL_ARGUMENT_ERROR
;
394 csx
.adoptInsteadAndCheckErrorCode(new CharString("unused", errorCode
), errorCode
);
395 if(csx
.isValid() && strcmp(csx
->data(), "different chars") != 0) {
396 errln("adoptInsteadAndCheckErrorCode(p, U_FAILURE) did not retain the old object");
399 errorCode
= U_ZERO_ERROR
;
400 csx
.adoptInsteadAndCheckErrorCode(NULL
, errorCode
);
401 if(errorCode
!= U_MEMORY_ALLOCATION_ERROR
) {
402 errln("adoptInsteadAndCheckErrorCode(NULL, errorCode) did not set U_MEMORY_ALLOCATION_ERROR");
406 errln("adoptInsteadAndCheckErrorCode(NULL, errorCode) kept the object");
409 errorCode
= U_ZERO_ERROR
;
410 LocalPointer
<CharString
> null(NULL
, errorCode
);
411 if(errorCode
!= U_MEMORY_ALLOCATION_ERROR
) {
412 errln("LocalPointer(NULL, errorCode) did not set U_MEMORY_ALLOCATION_ERROR");
419 // Try to avoid clang -Wself-move warnings from s1 = std::move(s1);
421 void moveFrom(T
&dest
, T
&src
) {
422 dest
= std::move(src
);
425 void LocalPointerTest::TestLocalPointerMoveSwap() {
426 UnicodeString
*p1
= new UnicodeString((UChar
)0x61);
427 UnicodeString
*p2
= new UnicodeString((UChar
)0x62);
428 LocalPointer
<UnicodeString
> s1(p1
);
429 LocalPointer
<UnicodeString
> s2(p2
);
431 if(s1
.getAlias() != p2
|| s2
.getAlias() != p1
) {
432 errln("LocalPointer.swap() did not swap");
435 if(s1
.getAlias() != p1
|| s2
.getAlias() != p2
) {
436 errln("swap(LocalPointer) did not swap back");
438 LocalPointer
<UnicodeString
> s3
;
440 if(s3
.getAlias() != p1
|| s1
.isValid()) {
441 errln("LocalPointer = std::move() did not move");
443 infoln("TestLocalPointerMoveSwap() with rvalue references");
444 s1
= static_cast<LocalPointer
<UnicodeString
> &&>(s3
);
445 if(s1
.getAlias() != p1
|| s3
.isValid()) {
446 errln("LocalPointer move assignment operator did not move");
448 LocalPointer
<UnicodeString
> s4(static_cast<LocalPointer
<UnicodeString
> &&>(s2
));
449 if(s4
.getAlias() != p2
|| s2
.isValid()) {
450 errln("LocalPointer move constructor did not move");
453 // Move self assignment leaves the object valid but in an undefined state.
454 // Do it to make sure there is no crash,
455 // but do not check for any particular resulting value.
460 void LocalPointerTest::TestLocalPointerStdUniquePtr() {
461 auto* ptr
= new UnicodeString((UChar32
)0x50005);
462 // Implicit conversion operator
463 std::unique_ptr
<UnicodeString
> s
= LocalPointer
<UnicodeString
>(ptr
);
464 // Explicit move constructor
465 LocalPointer
<UnicodeString
> s2(std::move(s
));
466 // Conversion operator should also work with std::move
468 // Back again with move assignment
470 assertTrue("Pointer should remain the same", ptr
== s2
.getAlias());
473 // Exercise almost every LocalArray method (but not LocalPointerBase).
474 void LocalPointerTest::TestLocalArray() {
476 LocalArray
<UnicodeString
> a(new UnicodeString
[2]);
478 a
[0].append((UChar
)0x61);
479 a
[1].append((UChar32
)0x60006);
480 if(a
[0].length()!=1 || a
[1].length()!=2) {
481 errln("LocalArray access failure");
484 a
.adoptInstead(new UnicodeString
[4]);
485 a
[3].append((UChar
)0x62).append((UChar
)0x63).reverse();
486 if(a
[3].length()!=2 || a
[3][1]!=0x62) {
487 errln("LocalArray adoptInstead() failure");
490 // LocalArray(p, errorCode) sets U_MEMORY_ALLOCATION_ERROR if p==NULL.
491 UErrorCode errorCode
= U_ZERO_ERROR
;
492 LocalArray
<UnicodeString
> ua(new UnicodeString
[3], errorCode
);
493 if(ua
.isNull() && U_SUCCESS(errorCode
)) {
494 errln("LocalArray(p, errorCode) failure");
497 errorCode
= U_ZERO_ERROR
;
498 UnicodeString
*u4
= new UnicodeString
[4];
499 ua
.adoptInsteadAndCheckErrorCode(u4
, errorCode
);
500 if(ua
.isNull() && U_SUCCESS(errorCode
)) {
501 errln("adoptInsteadAndCheckErrorCode(p, errorCode) failure");
504 // Incoming failure: Keep the current object and delete the input object.
505 errorCode
= U_ILLEGAL_ARGUMENT_ERROR
;
506 ua
.adoptInsteadAndCheckErrorCode(new UnicodeString
[5], errorCode
);
507 if(ua
.isValid() && ua
.getAlias() != u4
) {
508 errln("adoptInsteadAndCheckErrorCode(p, U_FAILURE) did not retain the old array");
511 errorCode
= U_ZERO_ERROR
;
512 ua
.adoptInsteadAndCheckErrorCode(NULL
, errorCode
);
513 if(errorCode
!= U_MEMORY_ALLOCATION_ERROR
) {
514 errln("adoptInsteadAndCheckErrorCode(NULL, errorCode) did not set U_MEMORY_ALLOCATION_ERROR");
518 errln("adoptInsteadAndCheckErrorCode(NULL, errorCode) kept the array");
521 errorCode
= U_ZERO_ERROR
;
522 LocalArray
<UnicodeString
> null(NULL
, errorCode
);
523 if(errorCode
!= U_MEMORY_ALLOCATION_ERROR
) {
524 errln("LocalArray(NULL, errorCode) did not set U_MEMORY_ALLOCATION_ERROR");
531 void LocalPointerTest::TestLocalArrayMoveSwap() {
532 UnicodeString
*p1
= new UnicodeString
[2];
533 UnicodeString
*p2
= new UnicodeString
[3];
534 LocalArray
<UnicodeString
> a1(p1
);
535 LocalArray
<UnicodeString
> a2(p2
);
537 if(a1
.getAlias() != p2
|| a2
.getAlias() != p1
) {
538 errln("LocalArray.swap() did not swap");
541 if(a1
.getAlias() != p1
|| a2
.getAlias() != p2
) {
542 errln("swap(LocalArray) did not swap back");
544 LocalArray
<UnicodeString
> a3
;
546 if(a3
.getAlias() != p1
|| a1
.isValid()) {
547 errln("LocalArray = std::move() did not move");
549 infoln("TestLocalArrayMoveSwap() with rvalue references");
550 a1
= static_cast<LocalArray
<UnicodeString
> &&>(a3
);
551 if(a1
.getAlias() != p1
|| a3
.isValid()) {
552 errln("LocalArray move assignment operator did not move");
554 LocalArray
<UnicodeString
> a4(static_cast<LocalArray
<UnicodeString
> &&>(a2
));
555 if(a4
.getAlias() != p2
|| a2
.isValid()) {
556 errln("LocalArray move constructor did not move");
559 // Move self assignment leaves the object valid but in an undefined state.
560 // Do it to make sure there is no crash,
561 // but do not check for any particular resulting value.
566 void LocalPointerTest::TestLocalArrayStdUniquePtr() {
567 auto* ptr
= new UnicodeString
[2];
568 // Implicit conversion operator
569 std::unique_ptr
<UnicodeString
[]> a
= LocalArray
<UnicodeString
>(ptr
);
570 // Explicit move constructor
571 LocalArray
<UnicodeString
> a2(std::move(a
));
572 // Conversion operator should also work with std::move
574 // Back again with move assignment
576 assertTrue("Pointer should remain the same", ptr
== a2
.getAlias());
579 #include "unicode/ucnvsel.h"
580 #include "unicode/ucal.h"
581 #include "unicode/udatpg.h"
582 #include "unicode/uidna.h"
583 #include "unicode/uldnames.h"
584 #include "unicode/umsg.h"
585 #include "unicode/unorm2.h"
586 #include "unicode/uregex.h"
587 #include "unicode/utrans.h"
588 #include "unicode/uformattedvalue.h"
590 // Use LocalXyzPointer types that are not covered elsewhere in the intltest suite.
591 void LocalPointerTest::TestLocalXyzPointer() {
592 IcuTestErrorCode
errorCode(*this, "TestLocalXyzPointer");
594 static const char *const encoding
="ISO-8859-1";
595 LocalUConverterSelectorPointer
sel(
596 ucnvsel_open(&encoding
, 1, NULL
, UCNV_ROUNDTRIP_SET
, errorCode
));
597 if(errorCode
.errIfFailureAndReset("ucnvsel_open()")) {
601 errln("LocalUConverterSelectorPointer failure");
605 #if !UCONFIG_NO_FORMATTING
606 LocalUCalendarPointer
cal(ucal_open(NULL
, 0, "root", UCAL_GREGORIAN
, errorCode
));
607 if(errorCode
.errDataIfFailureAndReset("ucal_open()")) {
611 errln("LocalUCalendarPointer failure");
615 LocalUDateTimePatternGeneratorPointer
patgen(udatpg_open("root", errorCode
));
616 if(errorCode
.errDataIfFailureAndReset("udatpg_open()")) {
619 if(patgen
.isNull()) {
620 errln("LocalUDateTimePatternGeneratorPointer failure");
624 LocalULocaleDisplayNamesPointer
ldn(uldn_open("de-CH", ULDN_STANDARD_NAMES
, errorCode
));
625 if(errorCode
.errIfFailureAndReset("uldn_open()")) {
629 errln("LocalULocaleDisplayNamesPointer failure");
633 UnicodeString hello
=UNICODE_STRING_SIMPLE("Hello {0}!");
634 LocalUMessageFormatPointer
msg(
635 umsg_open(hello
.getBuffer(), hello
.length(), "root", NULL
, errorCode
));
636 if(errorCode
.errIfFailureAndReset("umsg_open()")) {
640 errln("LocalUMessageFormatPointer failure");
643 #endif /* UCONFIG_NO_FORMATTING */
645 #if !UCONFIG_NO_NORMALIZATION
646 const UNormalizer2
*nfc
=unorm2_getNFCInstance(errorCode
);
648 LocalUNormalizer2Pointer
fn2(unorm2_openFiltered(nfc
, emptySet
.toUSet(), errorCode
));
649 if(errorCode
.errIfFailureAndReset("unorm2_openFiltered()")) {
653 errln("LocalUNormalizer2Pointer failure");
656 #endif /* !UCONFIG_NO_NORMALIZATION */
659 LocalUIDNAPointer
idna(uidna_openUTS46(0, errorCode
));
660 if(errorCode
.errIfFailureAndReset("uidna_openUTS46()")) {
664 errln("LocalUIDNAPointer failure");
667 #endif /* !UCONFIG_NO_IDNA */
669 #if !UCONFIG_NO_REGULAR_EXPRESSIONS
670 UnicodeString pattern
=UNICODE_STRING_SIMPLE("abc|xy+z");
671 LocalURegularExpressionPointer
regex(
672 uregex_open(pattern
.getBuffer(), pattern
.length(), 0, NULL
, errorCode
));
673 if(errorCode
.errIfFailureAndReset("uregex_open()")) {
677 errln("LocalURegularExpressionPointer failure");
680 #endif /* UCONFIG_NO_REGULAR_EXPRESSIONS */
682 #if !UCONFIG_NO_TRANSLITERATION
683 UnicodeString id
=UNICODE_STRING_SIMPLE("Grek-Latn");
684 LocalUTransliteratorPointer
trans(
685 utrans_openU(id
.getBuffer(), id
.length(), UTRANS_FORWARD
, NULL
, 0, NULL
, errorCode
));
686 if(errorCode
.errIfFailureAndReset("utrans_open()")) {
690 errln("LocalUTransliteratorPointer failure");
693 #endif /* !UCONFIG_NO_TRANSLITERATION */
698 void LocalPointerTest::TestLocalXyzPointerMoveSwap() {
699 #if !UCONFIG_NO_NORMALIZATION
700 IcuTestErrorCode
errorCode(*this, "TestLocalXyzPointerMoveSwap");
701 const UNormalizer2
*nfc
=unorm2_getNFCInstance(errorCode
);
702 const UNormalizer2
*nfd
=unorm2_getNFDInstance(errorCode
);
703 if(errorCode
.errIfFailureAndReset("unorm2_getNF[CD]Instance()")) {
707 UNormalizer2
*p1
= unorm2_openFiltered(nfc
, emptySet
.toUSet(), errorCode
);
708 UNormalizer2
*p2
= unorm2_openFiltered(nfd
, emptySet
.toUSet(), errorCode
);
709 LocalUNormalizer2Pointer
f1(p1
);
710 LocalUNormalizer2Pointer
f2(p2
);
711 if(errorCode
.errIfFailureAndReset("unorm2_openFiltered()")) {
714 if(f1
.isNull() || f2
.isNull()) {
715 errln("LocalUNormalizer2Pointer failure");
719 if(f1
.getAlias() != p2
|| f2
.getAlias() != p1
) {
720 errln("LocalUNormalizer2Pointer.swap() did not swap");
723 if(f1
.getAlias() != p1
|| f2
.getAlias() != p2
) {
724 errln("swap(LocalUNormalizer2Pointer) did not swap back");
726 LocalUNormalizer2Pointer f3
;
728 if(f3
.getAlias() != p1
|| f1
.isValid()) {
729 errln("LocalUNormalizer2Pointer = std::move() did not move");
731 infoln("TestLocalXyzPointerMoveSwap() with rvalue references");
732 f1
= static_cast<LocalUNormalizer2Pointer
&&>(f3
);
733 if(f1
.getAlias() != p1
|| f3
.isValid()) {
734 errln("LocalUNormalizer2Pointer move assignment operator did not move");
736 LocalUNormalizer2Pointer
f4(static_cast<LocalUNormalizer2Pointer
&&>(f2
));
737 if(f4
.getAlias() != p2
|| f2
.isValid()) {
738 errln("LocalUNormalizer2Pointer move constructor did not move");
740 // Move self assignment leaves the object valid but in an undefined state.
741 // Do it to make sure there is no crash,
742 // but do not check for any particular resulting value.
745 #endif /* !UCONFIG_NO_NORMALIZATION */
748 // Try LocalXyzPointer types with NULL pointers.
749 void LocalPointerTest::TestLocalXyzPointerNull() {
751 IcuTestErrorCode
errorCode(*this, "TestLocalXyzPointerNull/LocalUConverterSelectorPointer");
752 static const char *const encoding
="ISO-8859-1";
753 LocalUConverterSelectorPointer null
;
754 LocalUConverterSelectorPointer
sel(
755 ucnvsel_open(&encoding
, 1, NULL
, UCNV_ROUNDTRIP_SET
, errorCode
));
756 sel
.adoptInstead(NULL
);
758 #if !UCONFIG_NO_FORMATTING
760 IcuTestErrorCode
errorCode(*this, "TestLocalXyzPointerNull/LocalUCalendarPointer");
761 LocalUCalendarPointer null
;
762 LocalUCalendarPointer
cal(ucal_open(NULL
, 0, "root", UCAL_GREGORIAN
, errorCode
));
763 if(!errorCode
.errDataIfFailureAndReset("ucal_open()")) {
764 cal
.adoptInstead(NULL
);
768 IcuTestErrorCode
errorCode(*this, "TestLocalXyzPointerNull/LocalUDateTimePatternGeneratorPointer");
769 LocalUDateTimePatternGeneratorPointer null
;
770 LocalUDateTimePatternGeneratorPointer
patgen(udatpg_open("root", errorCode
));
771 patgen
.adoptInstead(NULL
);
774 IcuTestErrorCode
errorCode(*this, "TestLocalXyzPointerNull/LocalUMessageFormatPointer");
775 UnicodeString hello
=UNICODE_STRING_SIMPLE("Hello {0}!");
776 LocalUMessageFormatPointer null
;
777 LocalUMessageFormatPointer
msg(
778 umsg_open(hello
.getBuffer(), hello
.length(), "root", NULL
, errorCode
));
779 msg
.adoptInstead(NULL
);
781 #endif /* !UCONFIG_NO_FORMATTING */
783 #if !UCONFIG_NO_REGULAR_EXPRESSIONS
785 IcuTestErrorCode
errorCode(*this, "TestLocalXyzPointerNull/LocalURegularExpressionPointer");
786 UnicodeString pattern
=UNICODE_STRING_SIMPLE("abc|xy+z");
787 LocalURegularExpressionPointer null
;
788 LocalURegularExpressionPointer
regex(
789 uregex_open(pattern
.getBuffer(), pattern
.length(), 0, NULL
, errorCode
));
790 if(!errorCode
.errDataIfFailureAndReset("urege_open()")) {
791 regex
.adoptInstead(NULL
);
794 #endif /* !UCONFIG_NO_REGULAR_EXPRESSIONS */
796 #if !UCONFIG_NO_TRANSLITERATION
798 IcuTestErrorCode
errorCode(*this, "TestLocalXyzPointerNull/LocalUTransliteratorPointer");
799 UnicodeString id
=UNICODE_STRING_SIMPLE("Grek-Latn");
800 LocalUTransliteratorPointer null
;
801 LocalUTransliteratorPointer
trans(
802 utrans_openU(id
.getBuffer(), id
.length(), UTRANS_FORWARD
, NULL
, 0, NULL
, errorCode
));
803 if(!errorCode
.errDataIfFailureAndReset("utrans_openU()")) {
804 trans
.adoptInstead(NULL
);
807 #endif /* !UCONFIG_NO_TRANSLITERATION */
811 void LocalPointerTest::TestLocalXyzStdUniquePtr() {
812 IcuTestErrorCode
status(*this, "TestLocalXyzStdUniquePtr");
813 #if !UCONFIG_NO_FORMATTING
814 auto* ptr
= ucfpos_open(status
);
815 // Implicit conversion operator
816 std::unique_ptr
<UConstrainedFieldPosition
, void(*)(UConstrainedFieldPosition
*)> a
=
817 LocalUConstrainedFieldPositionPointer(ptr
);
818 // Explicit move constructor
819 LocalUConstrainedFieldPositionPointer
a2(std::move(a
));
820 // Conversion operator should also work with std::move
822 // Back again with move assignment
824 assertTrue("Pointer should remain the same", ptr
== a2
.getAlias());
825 #endif // UCONFIG_NO_FORMATTING
829 #include "unicode/enumset.h"
831 class EnumSetTest
: public IntlTest
{
834 virtual void runIndexedTest(int32_t index
, UBool exec
, const char *&name
, char *par
=NULL
);
838 static IntlTest
*createEnumSetTest() {
839 return new EnumSetTest();
842 void EnumSetTest::runIndexedTest(int32_t index
, UBool exec
, const char *&name
, char * /*par*/) {
844 TESTCASE_AUTO(TestEnumSet
);
855 void EnumSetTest::TestEnumSet() {
861 logln("Enum is from [%d..%d]\n", MAX_NONBOOLEAN
+1,
864 assertFalse(WHERE
, flags
.get(THING1
));
865 assertFalse(WHERE
, flags
.get(THING2
));
866 assertFalse(WHERE
, flags
.get(THING3
));
868 logln("get(thing1)=%d, get(thing2)=%d, get(thing3)=%d\n", flags
.get(THING1
), flags
.get(THING2
), flags
.get(THING3
));
869 logln("Value now: %d\n", flags
.getAll());
871 logln("clear -Value now: %d\n", flags
.getAll());
872 logln("get(thing1)=%d, get(thing2)=%d, get(thing3)=%d\n", flags
.get(THING1
), flags
.get(THING2
), flags
.get(THING3
));
873 assertFalse(WHERE
, flags
.get(THING1
));
874 assertFalse(WHERE
, flags
.get(THING2
));
875 assertFalse(WHERE
, flags
.get(THING3
));
877 logln("set THING1 -Value now: %d\n", flags
.getAll());
878 assertTrue(WHERE
, flags
.get(THING1
));
879 assertFalse(WHERE
, flags
.get(THING2
));
880 assertFalse(WHERE
, flags
.get(THING3
));
881 logln("get(thing1)=%d, get(thing2)=%d, get(thing3)=%d\n", flags
.get(THING1
), flags
.get(THING2
), flags
.get(THING3
));
883 logln("set THING3 -Value now: %d\n", flags
.getAll());
884 assertTrue(WHERE
, flags
.get(THING1
));
885 assertFalse(WHERE
, flags
.get(THING2
));
886 assertTrue(WHERE
, flags
.get(THING3
));
887 logln("get(thing1)=%d, get(thing2)=%d, get(thing3)=%d\n", flags
.get(THING1
), flags
.get(THING2
), flags
.get(THING3
));
888 flags
.remove(THING2
);
889 assertTrue(WHERE
, flags
.get(THING1
));
890 assertFalse(WHERE
, flags
.get(THING2
));
891 assertTrue(WHERE
, flags
.get(THING3
));
892 logln("remove THING2 -Value now: %d\n", flags
.getAll());
893 logln("get(thing1)=%d, get(thing2)=%d, get(thing3)=%d\n", flags
.get(THING1
), flags
.get(THING2
), flags
.get(THING3
));
894 flags
.remove(THING1
);
895 assertFalse(WHERE
, flags
.get(THING1
));
896 assertFalse(WHERE
, flags
.get(THING2
));
897 assertTrue(WHERE
, flags
.get(THING3
));
898 logln("remove THING1 -Value now: %d\n", flags
.getAll());
899 logln("get(thing1)=%d, get(thing2)=%d, get(thing3)=%d\n", flags
.get(THING1
), flags
.get(THING2
), flags
.get(THING3
));
902 logln("clear -Value now: %d\n", flags
.getAll());
903 logln("get(thing1)=%d, get(thing2)=%d, get(thing3)=%d\n", flags
.get(THING1
), flags
.get(THING2
), flags
.get(THING3
));
904 assertFalse(WHERE
, flags
.get(THING1
));
905 assertFalse(WHERE
, flags
.get(THING2
));
906 assertFalse(WHERE
, flags
.get(THING3
));