Home | History | Annotate | Download | only in intltest
      1 /********************************************************************
      2  * COPYRIGHT:
      3  * Copyright (c) 1997-2010, International Business Machines Corporation and
      4  * others. All Rights Reserved.
      5  ********************************************************************/
      6 
      7 
      8 /**
      9  * IntlTestUtilities is the medium level test class for everything in the directory "utility".
     10  */
     11 
     12 #include "unicode/utypes.h"
     13 #include "unicode/errorcode.h"
     14 #include "unicode/localpointer.h"
     15 #include "itutil.h"
     16 #include "strtest.h"
     17 #include "loctest.h"
     18 #include "citrtest.h"
     19 #include "ustrtest.h"
     20 #include "ucdtest.h"
     21 #include "restest.h"
     22 #include "restsnew.h"
     23 #include "tsmthred.h"
     24 #include "tsputil.h"
     25 #include "uobjtest.h"
     26 #include "utxttest.h"
     27 #include "v32test.h"
     28 #include "uvectest.h"
     29 #include "aliastst.h"
     30 #include "usettest.h"
     31 
     32 static IntlTest *createLocalPointerTest();
     33 
     34 #define CASE(id, test) case id:                               \
     35                           name = #test;                       \
     36                           if (exec) {                         \
     37                               logln(#test "---"); logln();    \
     38                               test t;                         \
     39                               callTest(t, par);               \
     40                           }                                   \
     41                           break
     42 
     43 void IntlTestUtilities::runIndexedTest( int32_t index, UBool exec, const char* &name, char* par )
     44 {
     45     if (exec) logln("TestSuite Utilities: ");
     46     switch (index) {
     47         CASE(0, MultithreadTest);
     48         CASE(1, StringTest);
     49         CASE(2, UnicodeStringTest);
     50         CASE(3, LocaleTest);
     51         CASE(4, CharIterTest);
     52         CASE(5, UObjectTest);
     53         CASE(6, UnicodeTest);
     54         CASE(7, ResourceBundleTest);
     55         CASE(8, NewResourceBundleTest);
     56         CASE(9, PUtilTest);
     57         CASE(10, UVector32Test);
     58         CASE(11, UVectorTest);
     59         CASE(12, UTextTest);
     60         CASE(13, LocaleAliasTest);
     61         CASE(14, UnicodeSetTest);
     62         CASE(15, ErrorCodeTest);
     63         case 16:
     64             name = "LocalPointerTest";
     65             if (exec) {
     66                 logln("TestSuite LocalPointerTest---"); logln();
     67                 LocalPointer<IntlTest> test(createLocalPointerTest());
     68                 callTest(*test, par);
     69             }
     70             break;
     71         default: name = ""; break; //needed to end loop
     72     }
     73 }
     74 
     75 void ErrorCodeTest::runIndexedTest(int32_t index, UBool exec, const char* &name, char* /*par*/) {
     76     if (exec) logln("TestSuite Utilities: ");
     77     switch (index) {
     78         case 0: name = "TestErrorCode"; if (exec) TestErrorCode(); break;
     79         case 1: name = "TestSubclass"; if (exec) TestSubclass(); break;
     80         default: name = ""; break; //needed to end loop
     81     }
     82 }
     83 
     84 static void RefPlusOne(UErrorCode &code) { code=(UErrorCode)(code+1); }
     85 static void PtrPlusTwo(UErrorCode *code) { *code=(UErrorCode)(*code+2); }
     86 
     87 void ErrorCodeTest::TestErrorCode() {
     88     ErrorCode errorCode;
     89     if(errorCode.get()!=U_ZERO_ERROR || !errorCode.isSuccess() || errorCode.isFailure()) {
     90         errln("ErrorCode did not initialize properly");
     91         return;
     92     }
     93     errorCode.assertSuccess();
     94     if(errorCode.errorName()!=u_errorName(U_ZERO_ERROR)) {
     95         errln("ErrorCode did not format error message string properly");
     96     }
     97     RefPlusOne(errorCode);
     98     if(errorCode.get()!=U_ILLEGAL_ARGUMENT_ERROR || errorCode.isSuccess() || !errorCode.isFailure()) {
     99         errln("ErrorCode did not yield a writable reference");
    100     }
    101     PtrPlusTwo(errorCode);
    102     if(errorCode.get()!=U_INVALID_FORMAT_ERROR || errorCode.isSuccess() || !errorCode.isFailure()) {
    103         errln("ErrorCode did not yield a writable pointer");
    104     }
    105     errorCode.set(U_PARSE_ERROR);
    106     if(errorCode.get()!=U_PARSE_ERROR || errorCode.isSuccess() || !errorCode.isFailure()) {
    107         errln("ErrorCode.set() failed");
    108     }
    109     if( errorCode.reset()!=U_PARSE_ERROR || errorCode.get()!=U_ZERO_ERROR ||
    110         !errorCode.isSuccess() || errorCode.isFailure()
    111     ) {
    112         errln("ErrorCode did not reset properly");
    113     }
    114 }
    115 
    116 class MyErrorCode: public ErrorCode {
    117 public:
    118     MyErrorCode(int32_t &countChecks, int32_t &countDests)
    119         : checks(countChecks), dests(countDests) {}
    120     ~MyErrorCode() {
    121         if(isFailure()) {
    122             ++dests;
    123         }
    124     }
    125 private:
    126     virtual void handleFailure() const {
    127         ++checks;
    128     }
    129     int32_t &checks;
    130     int32_t &dests;
    131 };
    132 
    133 void ErrorCodeTest::TestSubclass() {
    134     int32_t countChecks=0;
    135     int32_t countDests=0;
    136     {
    137         MyErrorCode errorCode(countChecks, countDests);
    138         if( errorCode.get()!=U_ZERO_ERROR || !errorCode.isSuccess() || errorCode.isFailure() ||
    139             countChecks!=0 || countDests!=0
    140         ) {
    141             errln("ErrorCode did not initialize properly");
    142             return;
    143         }
    144         errorCode.assertSuccess();
    145         if(countChecks!=0) {
    146             errln("ErrorCode.assertSuccess() called handleFailure() despite success");
    147         }
    148         RefPlusOne(errorCode);
    149         if(errorCode.get()!=U_ILLEGAL_ARGUMENT_ERROR || errorCode.isSuccess() || !errorCode.isFailure()) {
    150             errln("ErrorCode did not yield a writable reference");
    151         }
    152         errorCode.assertSuccess();
    153         if(countChecks!=1) {
    154             errln("ErrorCode.assertSuccess() did not handleFailure()");
    155         }
    156         PtrPlusTwo(errorCode);
    157         if(errorCode.get()!=U_INVALID_FORMAT_ERROR || errorCode.isSuccess() || !errorCode.isFailure()) {
    158             errln("ErrorCode did not yield a writable pointer");
    159         }
    160         errorCode.assertSuccess();
    161         if(countChecks!=2) {
    162             errln("ErrorCode.assertSuccess() did not handleFailure()");
    163         }
    164         errorCode.set(U_PARSE_ERROR);
    165         if(errorCode.get()!=U_PARSE_ERROR || errorCode.isSuccess() || !errorCode.isFailure()) {
    166             errln("ErrorCode.set() failed");
    167         }
    168         if( errorCode.reset()!=U_PARSE_ERROR || errorCode.get()!=U_ZERO_ERROR ||
    169             !errorCode.isSuccess() || errorCode.isFailure()
    170         ) {
    171             errln("ErrorCode did not reset properly");
    172         }
    173         errorCode.assertSuccess();
    174         if(countChecks!=2) {
    175             errln("ErrorCode.assertSuccess() called handleFailure() despite success");
    176         }
    177     }
    178     if(countDests!=0) {
    179         errln("MyErrorCode destructor detected failure despite success");
    180     }
    181     countChecks=countDests=0;
    182     {
    183         MyErrorCode errorCode(countChecks, countDests);
    184         errorCode.set(U_PARSE_ERROR);
    185     }
    186     if(countDests!=1) {
    187         errln("MyErrorCode destructor failed to detect failure");
    188     }
    189 }
    190 
    191 class LocalPointerTest : public IntlTest {
    192 public:
    193     LocalPointerTest() {}
    194 
    195     void runIndexedTest(int32_t index, UBool exec, const char *&name, char *par=NULL);
    196 
    197     void TestLocalPointer();
    198     void TestLocalArray();
    199     void TestLocalXyzPointer();
    200     void TestLocalXyzPointerNull();
    201 };
    202 
    203 static IntlTest *createLocalPointerTest() {
    204     return new LocalPointerTest();
    205 }
    206 
    207 void LocalPointerTest::runIndexedTest(int32_t index, UBool exec, const char *&name, char * /*par*/) {
    208     if(exec) {
    209         logln("TestSuite LocalPointerTest: ");
    210     }
    211     switch (index) {
    212         TESTCASE(0, TestLocalPointer);
    213         TESTCASE(1, TestLocalArray);
    214         TESTCASE(2, TestLocalXyzPointer);
    215         TESTCASE(3, TestLocalXyzPointerNull);
    216         default:
    217             name="";
    218             break; // needed to end the loop
    219     }
    220 }
    221 
    222 // Exercise every LocalPointer and LocalPointerBase method.
    223 void LocalPointerTest::TestLocalPointer() {
    224     // constructor
    225     LocalPointer<UnicodeString> s(new UnicodeString((UChar32)0x50005));
    226     // isNULL(), isValid(), operator==(), operator!=()
    227     if(s.isNull() || !s.isValid() || s==NULL || !(s!=NULL)) {
    228         errln("LocalPointer constructor or NULL test failure");
    229         return;
    230     }
    231     // getAlias(), operator->, operator*
    232     if(s.getAlias()->length()!=2 || s->length()!=2 || (*s).length()!=2) {
    233         errln("LocalPointer access failure");
    234     }
    235     // adoptInstead(), orphan()
    236     s.adoptInstead(new UnicodeString((UChar)0xfffc));
    237     if(s->length()!=1) {
    238         errln("LocalPointer adoptInstead(U+FFFC) failure");
    239     }
    240     UnicodeString *orphan=s.orphan();
    241     if(orphan==NULL || orphan->length()!=1 || s.isValid() || s!=NULL) {
    242         errln("LocalPointer orphan() failure");
    243     }
    244     delete orphan;
    245     // destructor
    246     s.adoptInstead(new UnicodeString());
    247     if(s->length()!=0) {
    248         errln("LocalPointer adoptInstead(empty) failure");
    249     }
    250 }
    251 
    252 // Exercise every LocalArray method (but not LocalPointerBase).
    253 void LocalPointerTest::TestLocalArray() {
    254     // constructor
    255     LocalArray<UnicodeString> a(new UnicodeString[2]);
    256     // operator[]()
    257     a[0].append((UChar)0x61);
    258     a[1].append((UChar32)0x60006);
    259     if(a[0].length()!=1 || a[1].length()!=2) {
    260         errln("LocalArray access failure");
    261     }
    262     // adoptInstead()
    263     a.adoptInstead(new UnicodeString[4]);
    264     a[3].append((UChar)0x62).append((UChar)0x63).reverse();
    265     if(a[3].length()!=2 || a[3][1]!=0x62) {
    266         errln("LocalArray adoptInstead() failure");
    267     }
    268     // destructor
    269 }
    270 
    271 #include "unicode/ucnvsel.h"
    272 #include "unicode/ucal.h"
    273 #include "unicode/udatpg.h"
    274 #include "unicode/uidna.h"
    275 #include "unicode/uldnames.h"
    276 #include "unicode/umsg.h"
    277 #include "unicode/unorm2.h"
    278 #include "unicode/uregex.h"
    279 #include "unicode/utrans.h"
    280 
    281 // Use LocalXyzPointer types that are not covered elsewhere in the intltest suite.
    282 void LocalPointerTest::TestLocalXyzPointer() {
    283     IcuTestErrorCode errorCode(*this, "TestLocalXyzPointer");
    284 
    285     static const char *const encoding="ISO-8859-1";
    286     LocalUConverterSelectorPointer sel(
    287         ucnvsel_open(&encoding, 1, NULL, UCNV_ROUNDTRIP_SET, errorCode));
    288     if(errorCode.logIfFailureAndReset("ucnvsel_open()")) {
    289         return;
    290     }
    291     if(sel.isNull()) {
    292         errln("LocalUConverterSelectorPointer failure");
    293         return;
    294     }
    295 
    296 #if !UCONFIG_NO_FORMATTING
    297     LocalUCalendarPointer cal(ucal_open(NULL, 0, "root", UCAL_GREGORIAN, errorCode));
    298     if(errorCode.logDataIfFailureAndReset("ucal_open()")) {
    299         return;
    300     }
    301     if(cal.isNull()) {
    302         errln("LocalUCalendarPointer failure");
    303         return;
    304     }
    305 
    306     LocalUDateTimePatternGeneratorPointer patgen(udatpg_open("root", errorCode));
    307     if(errorCode.logIfFailureAndReset("udatpg_open()")) {
    308         return;
    309     }
    310     if(patgen.isNull()) {
    311         errln("LocalUDateTimePatternGeneratorPointer failure");
    312         return;
    313     }
    314 
    315     LocalULocaleDisplayNamesPointer ldn(uldn_open("de-CH", ULDN_STANDARD_NAMES, errorCode));
    316     if(errorCode.logIfFailureAndReset("uldn_open()")) {
    317         return;
    318     }
    319     if(ldn.isNull()) {
    320         errln("LocalULocaleDisplayNamesPointer failure");
    321         return;
    322     }
    323 
    324     UnicodeString hello=UNICODE_STRING_SIMPLE("Hello {0}!");
    325     LocalUMessageFormatPointer msg(
    326         umsg_open(hello.getBuffer(), hello.length(), "root", NULL, errorCode));
    327     if(errorCode.logIfFailureAndReset("umsg_open()")) {
    328         return;
    329     }
    330     if(msg.isNull()) {
    331         errln("LocalUMessageFormatPointer failure");
    332         return;
    333     }
    334 #endif  /* UCONFIG_NO_FORMATTING  */
    335 
    336 #if !UCONFIG_NO_NORMALIZATION
    337     const UNormalizer2 *nfc=unorm2_getInstance(NULL, "nfc", UNORM2_COMPOSE, errorCode);
    338     UnicodeSet emptySet;
    339     LocalUNormalizer2Pointer fn2(unorm2_openFiltered(nfc, emptySet.toUSet(), errorCode));
    340     if(errorCode.logIfFailureAndReset("unorm2_openFiltered()")) {
    341         return;
    342     }
    343     if(fn2.isNull()) {
    344         errln("LocalUNormalizer2Pointer failure");
    345         return;
    346     }
    347 #endif /* !UCONFIG_NO_NORMALIZATION */
    348 
    349 #if !UCONFIG_NO_IDNA
    350     LocalUIDNAPointer idna(uidna_openUTS46(0, errorCode));
    351     if(errorCode.logIfFailureAndReset("uidna_openUTS46()")) {
    352         return;
    353     }
    354     if(idna.isNull()) {
    355         errln("LocalUIDNAPointer failure");
    356         return;
    357     }
    358 #endif  /* !UCONFIG_NO_IDNA */
    359 
    360 #if !UCONFIG_NO_REGULAR_EXPRESSIONS
    361     UnicodeString pattern=UNICODE_STRING_SIMPLE("abc|xy+z");
    362     LocalURegularExpressionPointer regex(
    363         uregex_open(pattern.getBuffer(), pattern.length(), 0, NULL, errorCode));
    364     if(errorCode.logIfFailureAndReset("uregex_open()")) {
    365         return;
    366     }
    367     if(regex.isNull()) {
    368         errln("LocalURegularExpressionPointer failure");
    369         return;
    370     }
    371 #endif /* UCONFIG_NO_REGULAR_EXPRESSIONS */
    372 
    373 #if !UCONFIG_NO_TRANSLITERATION
    374     UnicodeString id=UNICODE_STRING_SIMPLE("Grek-Latn");
    375     LocalUTransliteratorPointer trans(
    376         utrans_openU(id.getBuffer(), id.length(), UTRANS_FORWARD, NULL, 0, NULL, errorCode));
    377     if(errorCode.logIfFailureAndReset("utrans_open()")) {
    378         return;
    379     }
    380     if(trans.isNull()) {
    381         errln("LocalUTransliteratorPointer failure");
    382         return;
    383     }
    384 #endif /* !UCONFIG_NO_TRANSLITERATION */
    385 
    386     // destructors
    387 }
    388 
    389 // Try LocalXyzPointer types with NULL pointers.
    390 void LocalPointerTest::TestLocalXyzPointerNull() {
    391     {
    392         IcuTestErrorCode errorCode(*this, "TestLocalXyzPointerNull/LocalUConverterSelectorPointer");
    393         static const char *const encoding="ISO-8859-1";
    394         LocalUConverterSelectorPointer null;
    395         LocalUConverterSelectorPointer sel(
    396             ucnvsel_open(&encoding, 1, NULL, UCNV_ROUNDTRIP_SET, errorCode));
    397         sel.adoptInstead(NULL);
    398     }
    399 #if !UCONFIG_NO_FORMATTING
    400     {
    401         IcuTestErrorCode errorCode(*this, "TestLocalXyzPointerNull/LocalUCalendarPointer");
    402         LocalUCalendarPointer null;
    403         LocalUCalendarPointer cal(ucal_open(NULL, 0, "root", UCAL_GREGORIAN, errorCode));
    404         if(!errorCode.logDataIfFailureAndReset("ucal_open()")) {
    405             cal.adoptInstead(NULL);
    406         }
    407     }
    408     {
    409         IcuTestErrorCode errorCode(*this, "TestLocalXyzPointerNull/LocalUDateTimePatternGeneratorPointer");
    410         LocalUDateTimePatternGeneratorPointer null;
    411         LocalUDateTimePatternGeneratorPointer patgen(udatpg_open("root", errorCode));
    412         patgen.adoptInstead(NULL);
    413     }
    414     {
    415         IcuTestErrorCode errorCode(*this, "TestLocalXyzPointerNull/LocalUMessageFormatPointer");
    416         UnicodeString hello=UNICODE_STRING_SIMPLE("Hello {0}!");
    417         LocalUMessageFormatPointer null;
    418         LocalUMessageFormatPointer msg(
    419             umsg_open(hello.getBuffer(), hello.length(), "root", NULL, errorCode));
    420         msg.adoptInstead(NULL);
    421     }
    422 #endif /* !UCONFIG_NO_FORMATTING */
    423 
    424 #if !UCONFIG_NO_REGULAR_EXPRESSIONS
    425     {
    426         IcuTestErrorCode errorCode(*this, "TestLocalXyzPointerNull/LocalURegularExpressionPointer");
    427         UnicodeString pattern=UNICODE_STRING_SIMPLE("abc|xy+z");
    428         LocalURegularExpressionPointer null;
    429         LocalURegularExpressionPointer regex(
    430             uregex_open(pattern.getBuffer(), pattern.length(), 0, NULL, errorCode));
    431         if(!errorCode.logDataIfFailureAndReset("urege_open()")) {
    432             regex.adoptInstead(NULL);
    433         }
    434     }
    435 #endif /* !UCONFIG_NO_REGULAR_EXPRESSIONS */
    436 
    437 #if !UCONFIG_NO_TRANSLITERATION
    438     {
    439         IcuTestErrorCode errorCode(*this, "TestLocalXyzPointerNull/LocalUTransliteratorPointer");
    440         UnicodeString id=UNICODE_STRING_SIMPLE("Grek-Latn");
    441         LocalUTransliteratorPointer null;
    442         LocalUTransliteratorPointer trans(
    443             utrans_openU(id.getBuffer(), id.length(), UTRANS_FORWARD, NULL, 0, NULL, errorCode));
    444         if(!errorCode.logDataIfFailureAndReset("utrans_openU()")) {
    445             trans.adoptInstead(NULL);
    446         }
    447     }
    448 #endif /* !UCONFIG_NO_TRANSLITERATION */
    449 
    450 }
    451