1 /********************************************************************
3 * Copyright (c) 1997-2014, International Business Machines Corporation and
4 * others. All Rights Reserved.
5 ********************************************************************/
9 * IntlTestUtilities is the medium level test class for everything in the directory "utility".
12 #include "unicode/utypes.h"
13 #include "unicode/errorcode.h"
14 #include "unicode/localpointer.h"
33 extern IntlTest
*createBytesTrieTest();
34 static IntlTest
*createLocalPointerTest();
35 extern IntlTest
*createUCharsTrieTest();
36 static IntlTest
*createEnumSetTest();
37 extern IntlTest
*createSimplePatternFormatterTest();
38 extern IntlTest
*createUnifiedCacheTest();
39 extern IntlTest
*createQuantityFormatterTest();
41 #define CASE(id, test) case id: \
44 logln(#test "---"); logln(); \
50 void IntlTestUtilities::runIndexedTest( int32_t index
, UBool exec
, const char* &name
, char* par
)
52 if (exec
) logln("TestSuite Utilities: ");
54 CASE(0, MultithreadTest
);
56 CASE(2, UnicodeStringTest
);
58 CASE(4, CharIterTest
);
61 CASE(7, ResourceBundleTest
);
62 CASE(8, NewResourceBundleTest
);
64 CASE(10, UVector32Test
);
65 CASE(11, UVectorTest
);
67 CASE(13, LocaleAliasTest
);
68 CASE(14, UnicodeSetTest
);
69 CASE(15, ErrorCodeTest
);
71 name
= "LocalPointerTest";
73 logln("TestSuite LocalPointerTest---"); logln();
74 LocalPointer
<IntlTest
> test(createLocalPointerTest());
79 name
= "BytesTrieTest";
81 logln("TestSuite BytesTrieTest---"); logln();
82 LocalPointer
<IntlTest
> test(createBytesTrieTest());
87 name
= "UCharsTrieTest";
89 logln("TestSuite UCharsTrieTest---"); logln();
90 LocalPointer
<IntlTest
> test(createUCharsTrieTest());
97 logln("TestSuite EnumSetTest---"); logln();
98 LocalPointer
<IntlTest
> test(createEnumSetTest());
103 name
= "SimplePatternFormatterTest";
105 logln("TestSuite SimplePatternFormatterTest---"); logln();
106 LocalPointer
<IntlTest
> test(createSimplePatternFormatterTest());
107 callTest(*test
, par
);
111 name
= "UnifiedCacheTest";
113 logln("TestSuite UnifiedCacheTest---"); logln();
114 LocalPointer
<IntlTest
> test(createUnifiedCacheTest());
115 callTest(*test
, par
);
119 name
= "QuantityFormatterTest";
121 logln("TestSuite QuantityFormatterTest---"); logln();
122 LocalPointer
<IntlTest
> test(createQuantityFormatterTest());
123 callTest(*test
, par
);
126 default: name
= ""; break; //needed to end loop
130 void ErrorCodeTest::runIndexedTest(int32_t index
, UBool exec
, const char* &name
, char* /*par*/) {
131 if (exec
) logln("TestSuite Utilities: ");
133 case 0: name
= "TestErrorCode"; if (exec
) TestErrorCode(); break;
134 case 1: name
= "TestSubclass"; if (exec
) TestSubclass(); break;
135 default: name
= ""; break; //needed to end loop
139 static void RefPlusOne(UErrorCode
&code
) { code
=(UErrorCode
)(code
+1); }
140 static void PtrPlusTwo(UErrorCode
*code
) { *code
=(UErrorCode
)(*code
+2); }
142 void ErrorCodeTest::TestErrorCode() {
144 if(errorCode
.get()!=U_ZERO_ERROR
|| !errorCode
.isSuccess() || errorCode
.isFailure()) {
145 errln("ErrorCode did not initialize properly");
148 errorCode
.assertSuccess();
149 if(errorCode
.errorName()!=u_errorName(U_ZERO_ERROR
)) {
150 errln("ErrorCode did not format error message string properly");
152 RefPlusOne(errorCode
);
153 if(errorCode
.get()!=U_ILLEGAL_ARGUMENT_ERROR
|| errorCode
.isSuccess() || !errorCode
.isFailure()) {
154 errln("ErrorCode did not yield a writable reference");
156 PtrPlusTwo(errorCode
);
157 if(errorCode
.get()!=U_INVALID_FORMAT_ERROR
|| errorCode
.isSuccess() || !errorCode
.isFailure()) {
158 errln("ErrorCode did not yield a writable pointer");
160 errorCode
.set(U_PARSE_ERROR
);
161 if(errorCode
.get()!=U_PARSE_ERROR
|| errorCode
.isSuccess() || !errorCode
.isFailure()) {
162 errln("ErrorCode.set() failed");
164 if( errorCode
.reset()!=U_PARSE_ERROR
|| errorCode
.get()!=U_ZERO_ERROR
||
165 !errorCode
.isSuccess() || errorCode
.isFailure()
167 errln("ErrorCode did not reset properly");
171 class MyErrorCode
: public ErrorCode
{
173 MyErrorCode(int32_t &countChecks
, int32_t &countDests
)
174 : checks(countChecks
), dests(countDests
) {}
181 virtual void handleFailure() const {
188 void ErrorCodeTest::TestSubclass() {
189 int32_t countChecks
=0;
190 int32_t countDests
=0;
192 MyErrorCode
errorCode(countChecks
, countDests
);
193 if( errorCode
.get()!=U_ZERO_ERROR
|| !errorCode
.isSuccess() || errorCode
.isFailure() ||
194 countChecks
!=0 || countDests
!=0
196 errln("ErrorCode did not initialize properly");
199 errorCode
.assertSuccess();
201 errln("ErrorCode.assertSuccess() called handleFailure() despite success");
203 RefPlusOne(errorCode
);
204 if(errorCode
.get()!=U_ILLEGAL_ARGUMENT_ERROR
|| errorCode
.isSuccess() || !errorCode
.isFailure()) {
205 errln("ErrorCode did not yield a writable reference");
207 errorCode
.assertSuccess();
209 errln("ErrorCode.assertSuccess() did not handleFailure()");
211 PtrPlusTwo(errorCode
);
212 if(errorCode
.get()!=U_INVALID_FORMAT_ERROR
|| errorCode
.isSuccess() || !errorCode
.isFailure()) {
213 errln("ErrorCode did not yield a writable pointer");
215 errorCode
.assertSuccess();
217 errln("ErrorCode.assertSuccess() did not handleFailure()");
219 errorCode
.set(U_PARSE_ERROR
);
220 if(errorCode
.get()!=U_PARSE_ERROR
|| errorCode
.isSuccess() || !errorCode
.isFailure()) {
221 errln("ErrorCode.set() failed");
223 if( errorCode
.reset()!=U_PARSE_ERROR
|| errorCode
.get()!=U_ZERO_ERROR
||
224 !errorCode
.isSuccess() || errorCode
.isFailure()
226 errln("ErrorCode did not reset properly");
228 errorCode
.assertSuccess();
230 errln("ErrorCode.assertSuccess() called handleFailure() despite success");
234 errln("MyErrorCode destructor detected failure despite success");
236 countChecks
=countDests
=0;
238 MyErrorCode
errorCode(countChecks
, countDests
);
239 errorCode
.set(U_PARSE_ERROR
);
242 errln("MyErrorCode destructor failed to detect failure");
246 class LocalPointerTest
: public IntlTest
{
248 LocalPointerTest() {}
250 void runIndexedTest(int32_t index
, UBool exec
, const char *&name
, char *par
=NULL
);
252 void TestLocalPointer();
253 void TestLocalArray();
254 void TestLocalXyzPointer();
255 void TestLocalXyzPointerNull();
258 static IntlTest
*createLocalPointerTest() {
259 return new LocalPointerTest();
262 void LocalPointerTest::runIndexedTest(int32_t index
, UBool exec
, const char *&name
, char * /*par*/) {
264 logln("TestSuite LocalPointerTest: ");
267 TESTCASE_AUTO(TestLocalPointer
);
268 TESTCASE_AUTO(TestLocalArray
);
269 TESTCASE_AUTO(TestLocalXyzPointer
);
270 TESTCASE_AUTO(TestLocalXyzPointerNull
);
274 // Exercise every LocalPointer and LocalPointerBase method.
275 void LocalPointerTest::TestLocalPointer() {
277 LocalPointer
<UnicodeString
> s(new UnicodeString((UChar32
)0x50005));
278 // isNULL(), isValid(), operator==(), operator!=()
279 if(s
.isNull() || !s
.isValid() || s
==NULL
|| !(s
!=NULL
)) {
280 errln("LocalPointer constructor or NULL test failure");
283 // getAlias(), operator->, operator*
284 if(s
.getAlias()->length()!=2 || s
->length()!=2 || (*s
).length()!=2) {
285 errln("LocalPointer access failure");
287 // adoptInstead(), orphan()
288 s
.adoptInstead(new UnicodeString((UChar
)0xfffc));
290 errln("LocalPointer adoptInstead(U+FFFC) failure");
292 UnicodeString
*orphan
=s
.orphan();
293 if(orphan
==NULL
|| orphan
->length()!=1 || s
.isValid() || s
!=NULL
) {
294 errln("LocalPointer orphan() failure");
298 s
.adoptInstead(new UnicodeString());
300 errln("LocalPointer adoptInstead(empty) failure");
303 // LocalPointer(p, errorCode) sets U_MEMORY_ALLOCATION_ERROR if p==NULL.
304 UErrorCode errorCode
= U_ZERO_ERROR
;
305 LocalPointer
<CharString
> cs(new CharString("some chars", errorCode
), errorCode
);
306 if(cs
.isNull() && U_SUCCESS(errorCode
)) {
307 errln("LocalPointer(p, errorCode) failure");
310 errorCode
= U_ZERO_ERROR
;
311 cs
.adoptInsteadAndCheckErrorCode(new CharString("different chars", errorCode
), errorCode
);
312 if(cs
.isNull() && U_SUCCESS(errorCode
)) {
313 errln("adoptInsteadAndCheckErrorCode(p, errorCode) failure");
316 // Incoming failure: Keep the current object and delete the input object.
317 errorCode
= U_ILLEGAL_ARGUMENT_ERROR
;
318 cs
.adoptInsteadAndCheckErrorCode(new CharString("unused", errorCode
), errorCode
);
319 if(cs
.isValid() && strcmp(cs
->data(), "different chars") != 0) {
320 errln("adoptInsteadAndCheckErrorCode(p, U_FAILURE) did not retain the old object");
323 errorCode
= U_ZERO_ERROR
;
324 cs
.adoptInsteadAndCheckErrorCode(NULL
, errorCode
);
325 if(errorCode
!= U_MEMORY_ALLOCATION_ERROR
) {
326 errln("adoptInsteadAndCheckErrorCode(NULL, errorCode) did not set U_MEMORY_ALLOCATION_ERROR");
330 errln("adoptInsteadAndCheckErrorCode(NULL, errorCode) kept the object");
333 errorCode
= U_ZERO_ERROR
;
334 LocalPointer
<CharString
> null(NULL
, errorCode
);
335 if(errorCode
!= U_MEMORY_ALLOCATION_ERROR
) {
336 errln("LocalPointer(NULL, errorCode) did not set U_MEMORY_ALLOCATION_ERROR");
341 // Exercise every LocalArray method (but not LocalPointerBase).
342 void LocalPointerTest::TestLocalArray() {
344 LocalArray
<UnicodeString
> a(new UnicodeString
[2]);
346 a
[0].append((UChar
)0x61);
347 a
[1].append((UChar32
)0x60006);
348 if(a
[0].length()!=1 || a
[1].length()!=2) {
349 errln("LocalArray access failure");
352 a
.adoptInstead(new UnicodeString
[4]);
353 a
[3].append((UChar
)0x62).append((UChar
)0x63).reverse();
354 if(a
[3].length()!=2 || a
[3][1]!=0x62) {
355 errln("LocalArray adoptInstead() failure");
360 #include "unicode/ucnvsel.h"
361 #include "unicode/ucal.h"
362 #include "unicode/udatpg.h"
363 #include "unicode/uidna.h"
364 #include "unicode/uldnames.h"
365 #include "unicode/umsg.h"
366 #include "unicode/unorm2.h"
367 #include "unicode/uregex.h"
368 #include "unicode/utrans.h"
370 // Use LocalXyzPointer types that are not covered elsewhere in the intltest suite.
371 void LocalPointerTest::TestLocalXyzPointer() {
372 IcuTestErrorCode
errorCode(*this, "TestLocalXyzPointer");
374 static const char *const encoding
="ISO-8859-1";
375 LocalUConverterSelectorPointer
sel(
376 ucnvsel_open(&encoding
, 1, NULL
, UCNV_ROUNDTRIP_SET
, errorCode
));
377 if(errorCode
.logIfFailureAndReset("ucnvsel_open()")) {
381 errln("LocalUConverterSelectorPointer failure");
385 #if !UCONFIG_NO_FORMATTING
386 LocalUCalendarPointer
cal(ucal_open(NULL
, 0, "root", UCAL_GREGORIAN
, errorCode
));
387 if(errorCode
.logDataIfFailureAndReset("ucal_open()")) {
391 errln("LocalUCalendarPointer failure");
395 LocalUDateTimePatternGeneratorPointer
patgen(udatpg_open("root", errorCode
));
396 if(errorCode
.logDataIfFailureAndReset("udatpg_open()")) {
399 if(patgen
.isNull()) {
400 errln("LocalUDateTimePatternGeneratorPointer failure");
404 LocalULocaleDisplayNamesPointer
ldn(uldn_open("de-CH", ULDN_STANDARD_NAMES
, errorCode
));
405 if(errorCode
.logIfFailureAndReset("uldn_open()")) {
409 errln("LocalULocaleDisplayNamesPointer failure");
413 UnicodeString hello
=UNICODE_STRING_SIMPLE("Hello {0}!");
414 LocalUMessageFormatPointer
msg(
415 umsg_open(hello
.getBuffer(), hello
.length(), "root", NULL
, errorCode
));
416 if(errorCode
.logIfFailureAndReset("umsg_open()")) {
420 errln("LocalUMessageFormatPointer failure");
423 #endif /* UCONFIG_NO_FORMATTING */
425 #if !UCONFIG_NO_NORMALIZATION
426 const UNormalizer2
*nfc
=unorm2_getNFCInstance(errorCode
);
428 LocalUNormalizer2Pointer
fn2(unorm2_openFiltered(nfc
, emptySet
.toUSet(), errorCode
));
429 if(errorCode
.logIfFailureAndReset("unorm2_openFiltered()")) {
433 errln("LocalUNormalizer2Pointer failure");
436 #endif /* !UCONFIG_NO_NORMALIZATION */
439 LocalUIDNAPointer
idna(uidna_openUTS46(0, errorCode
));
440 if(errorCode
.logIfFailureAndReset("uidna_openUTS46()")) {
444 errln("LocalUIDNAPointer failure");
447 #endif /* !UCONFIG_NO_IDNA */
449 #if !UCONFIG_NO_REGULAR_EXPRESSIONS
450 UnicodeString pattern
=UNICODE_STRING_SIMPLE("abc|xy+z");
451 LocalURegularExpressionPointer
regex(
452 uregex_open(pattern
.getBuffer(), pattern
.length(), 0, NULL
, errorCode
));
453 if(errorCode
.logIfFailureAndReset("uregex_open()")) {
457 errln("LocalURegularExpressionPointer failure");
460 #endif /* UCONFIG_NO_REGULAR_EXPRESSIONS */
462 #if !UCONFIG_NO_TRANSLITERATION
463 UnicodeString id
=UNICODE_STRING_SIMPLE("Grek-Latn");
464 LocalUTransliteratorPointer
trans(
465 utrans_openU(id
.getBuffer(), id
.length(), UTRANS_FORWARD
, NULL
, 0, NULL
, errorCode
));
466 if(errorCode
.logIfFailureAndReset("utrans_open()")) {
470 errln("LocalUTransliteratorPointer failure");
473 #endif /* !UCONFIG_NO_TRANSLITERATION */
478 // Try LocalXyzPointer types with NULL pointers.
479 void LocalPointerTest::TestLocalXyzPointerNull() {
481 IcuTestErrorCode
errorCode(*this, "TestLocalXyzPointerNull/LocalUConverterSelectorPointer");
482 static const char *const encoding
="ISO-8859-1";
483 LocalUConverterSelectorPointer null
;
484 LocalUConverterSelectorPointer
sel(
485 ucnvsel_open(&encoding
, 1, NULL
, UCNV_ROUNDTRIP_SET
, errorCode
));
486 sel
.adoptInstead(NULL
);
488 #if !UCONFIG_NO_FORMATTING
490 IcuTestErrorCode
errorCode(*this, "TestLocalXyzPointerNull/LocalUCalendarPointer");
491 LocalUCalendarPointer null
;
492 LocalUCalendarPointer
cal(ucal_open(NULL
, 0, "root", UCAL_GREGORIAN
, errorCode
));
493 if(!errorCode
.logDataIfFailureAndReset("ucal_open()")) {
494 cal
.adoptInstead(NULL
);
498 IcuTestErrorCode
errorCode(*this, "TestLocalXyzPointerNull/LocalUDateTimePatternGeneratorPointer");
499 LocalUDateTimePatternGeneratorPointer null
;
500 LocalUDateTimePatternGeneratorPointer
patgen(udatpg_open("root", errorCode
));
501 patgen
.adoptInstead(NULL
);
504 IcuTestErrorCode
errorCode(*this, "TestLocalXyzPointerNull/LocalUMessageFormatPointer");
505 UnicodeString hello
=UNICODE_STRING_SIMPLE("Hello {0}!");
506 LocalUMessageFormatPointer null
;
507 LocalUMessageFormatPointer
msg(
508 umsg_open(hello
.getBuffer(), hello
.length(), "root", NULL
, errorCode
));
509 msg
.adoptInstead(NULL
);
511 #endif /* !UCONFIG_NO_FORMATTING */
513 #if !UCONFIG_NO_REGULAR_EXPRESSIONS
515 IcuTestErrorCode
errorCode(*this, "TestLocalXyzPointerNull/LocalURegularExpressionPointer");
516 UnicodeString pattern
=UNICODE_STRING_SIMPLE("abc|xy+z");
517 LocalURegularExpressionPointer null
;
518 LocalURegularExpressionPointer
regex(
519 uregex_open(pattern
.getBuffer(), pattern
.length(), 0, NULL
, errorCode
));
520 if(!errorCode
.logDataIfFailureAndReset("urege_open()")) {
521 regex
.adoptInstead(NULL
);
524 #endif /* !UCONFIG_NO_REGULAR_EXPRESSIONS */
526 #if !UCONFIG_NO_TRANSLITERATION
528 IcuTestErrorCode
errorCode(*this, "TestLocalXyzPointerNull/LocalUTransliteratorPointer");
529 UnicodeString id
=UNICODE_STRING_SIMPLE("Grek-Latn");
530 LocalUTransliteratorPointer null
;
531 LocalUTransliteratorPointer
trans(
532 utrans_openU(id
.getBuffer(), id
.length(), UTRANS_FORWARD
, NULL
, 0, NULL
, errorCode
));
533 if(!errorCode
.logDataIfFailureAndReset("utrans_openU()")) {
534 trans
.adoptInstead(NULL
);
537 #endif /* !UCONFIG_NO_TRANSLITERATION */
542 #include "unicode/enumset.h"
544 class EnumSetTest
: public IntlTest
{
547 virtual void runIndexedTest(int32_t index
, UBool exec
, const char *&name
, char *par
=NULL
);
551 static IntlTest
*createEnumSetTest() {
552 return new EnumSetTest();
555 void EnumSetTest::runIndexedTest(int32_t index
, UBool exec
, const char *&name
, char * /*par*/) {
557 TESTCASE_AUTO(TestEnumSet
);
568 void EnumSetTest::TestEnumSet() {
574 logln("Enum is from [%d..%d]\n", MAX_NONBOOLEAN
+1,
577 TEST_ASSERT_TRUE(flags
.get(THING1
) == FALSE
);
578 TEST_ASSERT_TRUE(flags
.get(THING2
) == FALSE
);
579 TEST_ASSERT_TRUE(flags
.get(THING3
) == FALSE
);
581 logln("get(thing1)=%d, get(thing2)=%d, get(thing3)=%d\n", flags
.get(THING1
), flags
.get(THING2
), flags
.get(THING3
));
582 logln("Value now: %d\n", flags
.getAll());
584 logln("clear -Value now: %d\n", flags
.getAll());
585 logln("get(thing1)=%d, get(thing2)=%d, get(thing3)=%d\n", flags
.get(THING1
), flags
.get(THING2
), flags
.get(THING3
));
586 TEST_ASSERT_TRUE(flags
.get(THING1
) == FALSE
);
587 TEST_ASSERT_TRUE(flags
.get(THING2
) == FALSE
);
588 TEST_ASSERT_TRUE(flags
.get(THING3
) == FALSE
);
590 logln("set THING1 -Value now: %d\n", flags
.getAll());
591 TEST_ASSERT_TRUE(flags
.get(THING1
) == TRUE
);
592 TEST_ASSERT_TRUE(flags
.get(THING2
) == FALSE
);
593 TEST_ASSERT_TRUE(flags
.get(THING3
) == FALSE
);
594 logln("get(thing1)=%d, get(thing2)=%d, get(thing3)=%d\n", flags
.get(THING1
), flags
.get(THING2
), flags
.get(THING3
));
596 logln("set THING3 -Value now: %d\n", flags
.getAll());
597 TEST_ASSERT_TRUE(flags
.get(THING1
) == TRUE
);
598 TEST_ASSERT_TRUE(flags
.get(THING2
) == FALSE
);
599 TEST_ASSERT_TRUE(flags
.get(THING3
) == TRUE
);
600 logln("get(thing1)=%d, get(thing2)=%d, get(thing3)=%d\n", flags
.get(THING1
), flags
.get(THING2
), flags
.get(THING3
));
601 flags
.remove(THING2
);
602 TEST_ASSERT_TRUE(flags
.get(THING1
) == TRUE
);
603 TEST_ASSERT_TRUE(flags
.get(THING2
) == FALSE
);
604 TEST_ASSERT_TRUE(flags
.get(THING3
) == TRUE
);
605 logln("remove THING2 -Value now: %d\n", flags
.getAll());
606 logln("get(thing1)=%d, get(thing2)=%d, get(thing3)=%d\n", flags
.get(THING1
), flags
.get(THING2
), flags
.get(THING3
));
607 flags
.remove(THING1
);
608 TEST_ASSERT_TRUE(flags
.get(THING1
) == FALSE
);
609 TEST_ASSERT_TRUE(flags
.get(THING2
) == FALSE
);
610 TEST_ASSERT_TRUE(flags
.get(THING3
) == TRUE
);
611 logln("remove THING1 -Value now: %d\n", flags
.getAll());
612 logln("get(thing1)=%d, get(thing2)=%d, get(thing3)=%d\n", flags
.get(THING1
), flags
.get(THING2
), flags
.get(THING3
));
615 logln("clear -Value now: %d\n", flags
.getAll());
616 logln("get(thing1)=%d, get(thing2)=%d, get(thing3)=%d\n", flags
.get(THING1
), flags
.get(THING2
), flags
.get(THING3
));
617 TEST_ASSERT_TRUE(flags
.get(THING1
) == FALSE
);
618 TEST_ASSERT_TRUE(flags
.get(THING2
) == FALSE
);
619 TEST_ASSERT_TRUE(flags
.get(THING3
) == FALSE
);