1 /******************************************************************** 
   3  * Copyright (c) 1997-2012, 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" 
  32 extern IntlTest 
*createBytesTrieTest(); 
  33 static IntlTest 
*createLocalPointerTest(); 
  34 extern IntlTest 
*createUCharsTrieTest(); 
  35 static IntlTest 
*createEnumSetTest(); 
  37 #define CASE(id, test) case id:                               \ 
  40                               logln(#test "---"); logln();    \ 
  46 void IntlTestUtilities::runIndexedTest( int32_t index
, UBool exec
, const char* &name
, char* par 
) 
  48     if (exec
) logln("TestSuite Utilities: "); 
  50         CASE(0, MultithreadTest
); 
  52         CASE(2, UnicodeStringTest
); 
  54         CASE(4, CharIterTest
); 
  57         CASE(7, ResourceBundleTest
); 
  58         CASE(8, NewResourceBundleTest
); 
  60         CASE(10, UVector32Test
); 
  61         CASE(11, UVectorTest
); 
  63         CASE(13, LocaleAliasTest
); 
  64         CASE(14, UnicodeSetTest
); 
  65         CASE(15, ErrorCodeTest
); 
  67             name 
= "LocalPointerTest"; 
  69                 logln("TestSuite LocalPointerTest---"); logln(); 
  70                 LocalPointer
<IntlTest
> test(createLocalPointerTest()); 
  75             name 
= "BytesTrieTest"; 
  77                 logln("TestSuite BytesTrieTest---"); logln(); 
  78                 LocalPointer
<IntlTest
> test(createBytesTrieTest()); 
  83             name 
= "UCharsTrieTest"; 
  85                 logln("TestSuite UCharsTrieTest---"); logln(); 
  86                 LocalPointer
<IntlTest
> test(createUCharsTrieTest()); 
  93                 logln("TestSuite EnumSetTest---"); logln(); 
  94                 LocalPointer
<IntlTest
> test(createEnumSetTest()); 
  98         default: name 
= ""; break; //needed to end loop 
 102 void ErrorCodeTest::runIndexedTest(int32_t index
, UBool exec
, const char* &name
, char* /*par*/) { 
 103     if (exec
) logln("TestSuite Utilities: "); 
 105         case 0: name 
= "TestErrorCode"; if (exec
) TestErrorCode(); break; 
 106         case 1: name 
= "TestSubclass"; if (exec
) TestSubclass(); break; 
 107         default: name 
= ""; break; //needed to end loop 
 111 static void RefPlusOne(UErrorCode 
&code
) { code
=(UErrorCode
)(code
+1); } 
 112 static void PtrPlusTwo(UErrorCode 
*code
) { *code
=(UErrorCode
)(*code
+2); } 
 114 void ErrorCodeTest::TestErrorCode() { 
 116     if(errorCode
.get()!=U_ZERO_ERROR 
|| !errorCode
.isSuccess() || errorCode
.isFailure()) { 
 117         errln("ErrorCode did not initialize properly"); 
 120     errorCode
.assertSuccess(); 
 121     if(errorCode
.errorName()!=u_errorName(U_ZERO_ERROR
)) { 
 122         errln("ErrorCode did not format error message string properly"); 
 124     RefPlusOne(errorCode
); 
 125     if(errorCode
.get()!=U_ILLEGAL_ARGUMENT_ERROR 
|| errorCode
.isSuccess() || !errorCode
.isFailure()) { 
 126         errln("ErrorCode did not yield a writable reference"); 
 128     PtrPlusTwo(errorCode
); 
 129     if(errorCode
.get()!=U_INVALID_FORMAT_ERROR 
|| errorCode
.isSuccess() || !errorCode
.isFailure()) { 
 130         errln("ErrorCode did not yield a writable pointer"); 
 132     errorCode
.set(U_PARSE_ERROR
); 
 133     if(errorCode
.get()!=U_PARSE_ERROR 
|| errorCode
.isSuccess() || !errorCode
.isFailure()) { 
 134         errln("ErrorCode.set() failed"); 
 136     if( errorCode
.reset()!=U_PARSE_ERROR 
|| errorCode
.get()!=U_ZERO_ERROR 
|| 
 137         !errorCode
.isSuccess() || errorCode
.isFailure() 
 139         errln("ErrorCode did not reset properly"); 
 143 class MyErrorCode
: public ErrorCode 
{ 
 145     MyErrorCode(int32_t &countChecks
, int32_t &countDests
) 
 146         : checks(countChecks
), dests(countDests
) {} 
 153     virtual void handleFailure() const { 
 160 void ErrorCodeTest::TestSubclass() { 
 161     int32_t countChecks
=0; 
 162     int32_t countDests
=0; 
 164         MyErrorCode 
errorCode(countChecks
, countDests
); 
 165         if( errorCode
.get()!=U_ZERO_ERROR 
|| !errorCode
.isSuccess() || errorCode
.isFailure() || 
 166             countChecks
!=0 || countDests
!=0 
 168             errln("ErrorCode did not initialize properly"); 
 171         errorCode
.assertSuccess(); 
 173             errln("ErrorCode.assertSuccess() called handleFailure() despite success"); 
 175         RefPlusOne(errorCode
); 
 176         if(errorCode
.get()!=U_ILLEGAL_ARGUMENT_ERROR 
|| errorCode
.isSuccess() || !errorCode
.isFailure()) { 
 177             errln("ErrorCode did not yield a writable reference"); 
 179         errorCode
.assertSuccess(); 
 181             errln("ErrorCode.assertSuccess() did not handleFailure()"); 
 183         PtrPlusTwo(errorCode
); 
 184         if(errorCode
.get()!=U_INVALID_FORMAT_ERROR 
|| errorCode
.isSuccess() || !errorCode
.isFailure()) { 
 185             errln("ErrorCode did not yield a writable pointer"); 
 187         errorCode
.assertSuccess(); 
 189             errln("ErrorCode.assertSuccess() did not handleFailure()"); 
 191         errorCode
.set(U_PARSE_ERROR
); 
 192         if(errorCode
.get()!=U_PARSE_ERROR 
|| errorCode
.isSuccess() || !errorCode
.isFailure()) { 
 193             errln("ErrorCode.set() failed"); 
 195         if( errorCode
.reset()!=U_PARSE_ERROR 
|| errorCode
.get()!=U_ZERO_ERROR 
|| 
 196             !errorCode
.isSuccess() || errorCode
.isFailure() 
 198             errln("ErrorCode did not reset properly"); 
 200         errorCode
.assertSuccess(); 
 202             errln("ErrorCode.assertSuccess() called handleFailure() despite success"); 
 206         errln("MyErrorCode destructor detected failure despite success"); 
 208     countChecks
=countDests
=0; 
 210         MyErrorCode 
errorCode(countChecks
, countDests
); 
 211         errorCode
.set(U_PARSE_ERROR
); 
 214         errln("MyErrorCode destructor failed to detect failure"); 
 218 class LocalPointerTest 
: public IntlTest 
{ 
 220     LocalPointerTest() {} 
 222     void runIndexedTest(int32_t index
, UBool exec
, const char *&name
, char *par
=NULL
); 
 224     void TestLocalPointer(); 
 225     void TestLocalArray(); 
 226     void TestLocalXyzPointer(); 
 227     void TestLocalXyzPointerNull(); 
 230 static IntlTest 
*createLocalPointerTest() { 
 231     return new LocalPointerTest(); 
 234 void LocalPointerTest::runIndexedTest(int32_t index
, UBool exec
, const char *&name
, char * /*par*/) { 
 236         logln("TestSuite LocalPointerTest: "); 
 239         TESTCASE(0, TestLocalPointer
); 
 240         TESTCASE(1, TestLocalArray
); 
 241         TESTCASE(2, TestLocalXyzPointer
); 
 242         TESTCASE(3, TestLocalXyzPointerNull
); 
 245             break; // needed to end the loop 
 249 // Exercise every LocalPointer and LocalPointerBase method. 
 250 void LocalPointerTest::TestLocalPointer() { 
 252     LocalPointer
<UnicodeString
> s(new UnicodeString((UChar32
)0x50005)); 
 253     // isNULL(), isValid(), operator==(), operator!=() 
 254     if(s
.isNull() || !s
.isValid() || s
==NULL 
|| !(s
!=NULL
)) { 
 255         errln("LocalPointer constructor or NULL test failure"); 
 258     // getAlias(), operator->, operator* 
 259     if(s
.getAlias()->length()!=2 || s
->length()!=2 || (*s
).length()!=2) { 
 260         errln("LocalPointer access failure"); 
 262     // adoptInstead(), orphan() 
 263     s
.adoptInstead(new UnicodeString((UChar
)0xfffc)); 
 265         errln("LocalPointer adoptInstead(U+FFFC) failure"); 
 267     UnicodeString 
*orphan
=s
.orphan(); 
 268     if(orphan
==NULL 
|| orphan
->length()!=1 || s
.isValid() || s
!=NULL
) { 
 269         errln("LocalPointer orphan() failure"); 
 273     s
.adoptInstead(new UnicodeString()); 
 275         errln("LocalPointer adoptInstead(empty) failure"); 
 279 // Exercise every LocalArray method (but not LocalPointerBase). 
 280 void LocalPointerTest::TestLocalArray() { 
 282     LocalArray
<UnicodeString
> a(new UnicodeString
[2]); 
 284     a
[0].append((UChar
)0x61); 
 285     a
[1].append((UChar32
)0x60006); 
 286     if(a
[0].length()!=1 || a
[1].length()!=2) { 
 287         errln("LocalArray access failure"); 
 290     a
.adoptInstead(new UnicodeString
[4]); 
 291     a
[3].append((UChar
)0x62).append((UChar
)0x63).reverse(); 
 292     if(a
[3].length()!=2 || a
[3][1]!=0x62) { 
 293         errln("LocalArray adoptInstead() failure"); 
 298 #include "unicode/ucnvsel.h" 
 299 #include "unicode/ucal.h" 
 300 #include "unicode/udatpg.h" 
 301 #include "unicode/uidna.h" 
 302 #include "unicode/uldnames.h" 
 303 #include "unicode/umsg.h" 
 304 #include "unicode/unorm2.h" 
 305 #include "unicode/uregex.h" 
 306 #include "unicode/utrans.h" 
 308 // Use LocalXyzPointer types that are not covered elsewhere in the intltest suite. 
 309 void LocalPointerTest::TestLocalXyzPointer() { 
 310     IcuTestErrorCode 
errorCode(*this, "TestLocalXyzPointer"); 
 312     static const char *const encoding
="ISO-8859-1"; 
 313     LocalUConverterSelectorPointer 
sel( 
 314         ucnvsel_open(&encoding
, 1, NULL
, UCNV_ROUNDTRIP_SET
, errorCode
)); 
 315     if(errorCode
.logIfFailureAndReset("ucnvsel_open()")) { 
 319         errln("LocalUConverterSelectorPointer failure"); 
 323 #if !UCONFIG_NO_FORMATTING 
 324     LocalUCalendarPointer 
cal(ucal_open(NULL
, 0, "root", UCAL_GREGORIAN
, errorCode
)); 
 325     if(errorCode
.logDataIfFailureAndReset("ucal_open()")) { 
 329         errln("LocalUCalendarPointer failure"); 
 333     LocalUDateTimePatternGeneratorPointer 
patgen(udatpg_open("root", errorCode
)); 
 334     if(errorCode
.logDataIfFailureAndReset("udatpg_open()")) { 
 337     if(patgen
.isNull()) { 
 338         errln("LocalUDateTimePatternGeneratorPointer failure"); 
 342     LocalULocaleDisplayNamesPointer 
ldn(uldn_open("de-CH", ULDN_STANDARD_NAMES
, errorCode
)); 
 343     if(errorCode
.logIfFailureAndReset("uldn_open()")) { 
 347         errln("LocalULocaleDisplayNamesPointer failure"); 
 351     UnicodeString hello
=UNICODE_STRING_SIMPLE("Hello {0}!"); 
 352     LocalUMessageFormatPointer 
msg( 
 353         umsg_open(hello
.getBuffer(), hello
.length(), "root", NULL
, errorCode
)); 
 354     if(errorCode
.logIfFailureAndReset("umsg_open()")) { 
 358         errln("LocalUMessageFormatPointer failure"); 
 361 #endif  /* UCONFIG_NO_FORMATTING  */ 
 363 #if !UCONFIG_NO_NORMALIZATION 
 364     const UNormalizer2 
*nfc
=unorm2_getNFCInstance(errorCode
); 
 366     LocalUNormalizer2Pointer 
fn2(unorm2_openFiltered(nfc
, emptySet
.toUSet(), errorCode
)); 
 367     if(errorCode
.logIfFailureAndReset("unorm2_openFiltered()")) { 
 371         errln("LocalUNormalizer2Pointer failure"); 
 374 #endif /* !UCONFIG_NO_NORMALIZATION */ 
 377     LocalUIDNAPointer 
idna(uidna_openUTS46(0, errorCode
)); 
 378     if(errorCode
.logIfFailureAndReset("uidna_openUTS46()")) { 
 382         errln("LocalUIDNAPointer failure"); 
 385 #endif  /* !UCONFIG_NO_IDNA */ 
 387 #if !UCONFIG_NO_REGULAR_EXPRESSIONS 
 388     UnicodeString pattern
=UNICODE_STRING_SIMPLE("abc|xy+z"); 
 389     LocalURegularExpressionPointer 
regex( 
 390         uregex_open(pattern
.getBuffer(), pattern
.length(), 0, NULL
, errorCode
)); 
 391     if(errorCode
.logIfFailureAndReset("uregex_open()")) { 
 395         errln("LocalURegularExpressionPointer failure"); 
 398 #endif /* UCONFIG_NO_REGULAR_EXPRESSIONS */ 
 400 #if !UCONFIG_NO_TRANSLITERATION 
 401     UnicodeString id
=UNICODE_STRING_SIMPLE("Grek-Latn"); 
 402     LocalUTransliteratorPointer 
trans( 
 403         utrans_openU(id
.getBuffer(), id
.length(), UTRANS_FORWARD
, NULL
, 0, NULL
, errorCode
)); 
 404     if(errorCode
.logIfFailureAndReset("utrans_open()")) { 
 408         errln("LocalUTransliteratorPointer failure"); 
 411 #endif /* !UCONFIG_NO_TRANSLITERATION */ 
 416 // Try LocalXyzPointer types with NULL pointers. 
 417 void LocalPointerTest::TestLocalXyzPointerNull() { 
 419         IcuTestErrorCode 
errorCode(*this, "TestLocalXyzPointerNull/LocalUConverterSelectorPointer"); 
 420         static const char *const encoding
="ISO-8859-1"; 
 421         LocalUConverterSelectorPointer null
; 
 422         LocalUConverterSelectorPointer 
sel( 
 423             ucnvsel_open(&encoding
, 1, NULL
, UCNV_ROUNDTRIP_SET
, errorCode
)); 
 424         sel
.adoptInstead(NULL
); 
 426 #if !UCONFIG_NO_FORMATTING 
 428         IcuTestErrorCode 
errorCode(*this, "TestLocalXyzPointerNull/LocalUCalendarPointer"); 
 429         LocalUCalendarPointer null
; 
 430         LocalUCalendarPointer 
cal(ucal_open(NULL
, 0, "root", UCAL_GREGORIAN
, errorCode
)); 
 431         if(!errorCode
.logDataIfFailureAndReset("ucal_open()")) { 
 432             cal
.adoptInstead(NULL
); 
 436         IcuTestErrorCode 
errorCode(*this, "TestLocalXyzPointerNull/LocalUDateTimePatternGeneratorPointer"); 
 437         LocalUDateTimePatternGeneratorPointer null
; 
 438         LocalUDateTimePatternGeneratorPointer 
patgen(udatpg_open("root", errorCode
)); 
 439         patgen
.adoptInstead(NULL
); 
 442         IcuTestErrorCode 
errorCode(*this, "TestLocalXyzPointerNull/LocalUMessageFormatPointer"); 
 443         UnicodeString hello
=UNICODE_STRING_SIMPLE("Hello {0}!"); 
 444         LocalUMessageFormatPointer null
; 
 445         LocalUMessageFormatPointer 
msg( 
 446             umsg_open(hello
.getBuffer(), hello
.length(), "root", NULL
, errorCode
)); 
 447         msg
.adoptInstead(NULL
); 
 449 #endif /* !UCONFIG_NO_FORMATTING */ 
 451 #if !UCONFIG_NO_REGULAR_EXPRESSIONS 
 453         IcuTestErrorCode 
errorCode(*this, "TestLocalXyzPointerNull/LocalURegularExpressionPointer"); 
 454         UnicodeString pattern
=UNICODE_STRING_SIMPLE("abc|xy+z"); 
 455         LocalURegularExpressionPointer null
; 
 456         LocalURegularExpressionPointer 
regex( 
 457             uregex_open(pattern
.getBuffer(), pattern
.length(), 0, NULL
, errorCode
)); 
 458         if(!errorCode
.logDataIfFailureAndReset("urege_open()")) { 
 459             regex
.adoptInstead(NULL
); 
 462 #endif /* !UCONFIG_NO_REGULAR_EXPRESSIONS */ 
 464 #if !UCONFIG_NO_TRANSLITERATION 
 466         IcuTestErrorCode 
errorCode(*this, "TestLocalXyzPointerNull/LocalUTransliteratorPointer"); 
 467         UnicodeString id
=UNICODE_STRING_SIMPLE("Grek-Latn"); 
 468         LocalUTransliteratorPointer null
; 
 469         LocalUTransliteratorPointer 
trans( 
 470             utrans_openU(id
.getBuffer(), id
.length(), UTRANS_FORWARD
, NULL
, 0, NULL
, errorCode
)); 
 471         if(!errorCode
.logDataIfFailureAndReset("utrans_openU()")) { 
 472             trans
.adoptInstead(NULL
); 
 475 #endif /* !UCONFIG_NO_TRANSLITERATION */ 
 480 #include "unicode/enumset.h" 
 482 class EnumSetTest 
: public IntlTest 
{ 
 485   virtual void runIndexedTest(int32_t index
, UBool exec
, const char *&name
, char *par
=NULL
); 
 489 static IntlTest 
*createEnumSetTest() { 
 490     return new EnumSetTest(); 
 493 void EnumSetTest::runIndexedTest(int32_t index
, UBool exec
, const char *&name
, char * /*par*/) { 
 495   TESTCASE_AUTO(TestEnumSet
); 
 506 void EnumSetTest::TestEnumSet() { 
 512     logln("Enum is from [%d..%d]\n", MAX_NONBOOLEAN
+1, 
 515     TEST_ASSERT_TRUE(flags
.get(THING1
) == FALSE
); 
 516     TEST_ASSERT_TRUE(flags
.get(THING2
) == FALSE
); 
 517     TEST_ASSERT_TRUE(flags
.get(THING3
) == FALSE
); 
 519     logln("get(thing1)=%d, get(thing2)=%d, get(thing3)=%d\n",          flags
.get(THING1
),          flags
.get(THING2
),          flags
.get(THING3
)); 
 520     logln("Value now: %d\n", flags
.getAll()); 
 522     logln("clear -Value now: %d\n", flags
.getAll()); 
 523     logln("get(thing1)=%d, get(thing2)=%d, get(thing3)=%d\n",          flags
.get(THING1
),          flags
.get(THING2
),          flags
.get(THING3
)); 
 524     TEST_ASSERT_TRUE(flags
.get(THING1
) == FALSE
); 
 525     TEST_ASSERT_TRUE(flags
.get(THING2
) == FALSE
); 
 526     TEST_ASSERT_TRUE(flags
.get(THING3
) == FALSE
); 
 528     logln("set THING1 -Value now: %d\n", flags
.getAll()); 
 529     TEST_ASSERT_TRUE(flags
.get(THING1
) == TRUE
); 
 530     TEST_ASSERT_TRUE(flags
.get(THING2
) == FALSE
); 
 531     TEST_ASSERT_TRUE(flags
.get(THING3
) == FALSE
); 
 532     logln("get(thing1)=%d, get(thing2)=%d, get(thing3)=%d\n",          flags
.get(THING1
),          flags
.get(THING2
),          flags
.get(THING3
)); 
 534     logln("set THING3 -Value now: %d\n", flags
.getAll()); 
 535     TEST_ASSERT_TRUE(flags
.get(THING1
) == TRUE
); 
 536     TEST_ASSERT_TRUE(flags
.get(THING2
) == FALSE
); 
 537     TEST_ASSERT_TRUE(flags
.get(THING3
) == TRUE
); 
 538     logln("get(thing1)=%d, get(thing2)=%d, get(thing3)=%d\n",          flags
.get(THING1
),          flags
.get(THING2
),          flags
.get(THING3
)); 
 539     flags
.remove(THING2
); 
 540     TEST_ASSERT_TRUE(flags
.get(THING1
) == TRUE
); 
 541     TEST_ASSERT_TRUE(flags
.get(THING2
) == FALSE
); 
 542     TEST_ASSERT_TRUE(flags
.get(THING3
) == TRUE
); 
 543     logln("remove THING2 -Value now: %d\n", flags
.getAll()); 
 544     logln("get(thing1)=%d, get(thing2)=%d, get(thing3)=%d\n",          flags
.get(THING1
),          flags
.get(THING2
),          flags
.get(THING3
)); 
 545     flags
.remove(THING1
); 
 546     TEST_ASSERT_TRUE(flags
.get(THING1
) == FALSE
); 
 547     TEST_ASSERT_TRUE(flags
.get(THING2
) == FALSE
); 
 548     TEST_ASSERT_TRUE(flags
.get(THING3
) == TRUE
); 
 549     logln("remove THING1 -Value now: %d\n", flags
.getAll()); 
 550     logln("get(thing1)=%d, get(thing2)=%d, get(thing3)=%d\n",          flags
.get(THING1
),          flags
.get(THING2
),          flags
.get(THING3
)); 
 553     logln("clear -Value now: %d\n", flags
.getAll()); 
 554     logln("get(thing1)=%d, get(thing2)=%d, get(thing3)=%d\n",          flags
.get(THING1
),          flags
.get(THING2
),          flags
.get(THING3
)); 
 555     TEST_ASSERT_TRUE(flags
.get(THING1
) == FALSE
); 
 556     TEST_ASSERT_TRUE(flags
.get(THING2
) == FALSE
); 
 557     TEST_ASSERT_TRUE(flags
.get(THING3
) == FALSE
);