]> git.saurik.com Git - apple/icu.git/blobdiff - icuSources/test/intltest/uobjtest.cpp
ICU-511.34.tar.gz
[apple/icu.git] / icuSources / test / intltest / uobjtest.cpp
index 2f44e16454d0da87181aad99f74f1edd518723b4..e607f2f32b0170585024a60db0d7a7ffedf7c8d8 100644 (file)
@@ -1,19 +1,31 @@
 /********************************************************************
- * COPYRIGHT: 
- * Copyright (c) 2002-2008, International Business Machines Corporation and
+ * COPYRIGHT:
+ * Copyright (c) 1997-2012, International Business Machines Corporation and
  * others. All Rights Reserved.
+ * Copyright (C) 2010 , Yahoo! Inc.
  ********************************************************************/
 
+#include <stdio.h>
+#include <string.h>
+#include "utypeinfo.h"  // for 'typeid' to work
+
 #include "uobjtest.h"
 #include "cmemory.h" // UAlignedMemory
-#include <string.h>
-#include <stdio.h>
 
 /**
- * 
  * Test for UObject, currently only the classID.
  *
  * Usage
+ *   TESTCLASSID_NONE_DEFAULT(Foo)
+ *      --  Foo is expected to not support "poor man's RTTI".
+ *          Beginning with ICU 4.6, we only use compiler RTTI in new class hierarchies.
+ *
+ *   TESTCLASSID_NONE_CTOR(Foo, (1, 2, 3, status))
+ *      -- Combines TESTCLASSID_NONE_DEFAULT() and TESTCLASSID_CTOR().
+ *
+ *   TESTCLASSID_NONE_FACTORY(Foo, (1, 2, 3, status))
+ *      -- Combines TESTCLASSID_NONE_DEFAULT() and TESTCLASSID_FACTORY().
+ *
  *   TESTCLASSID_ABSTRACT(Bar)
  *      --  Bar is expected to be abstract. Only the static ID will be tested.
  *
  *      'status' will be tested & reset. This only tests uniqueness.
  */
 
-
-#define TESTCLASSID_FACTORY(c, f) { delete testClass(f, #c, #f, c ::getStaticClassID()); if(U_FAILURE(status)) { errln(UnicodeString(#c " - " #f " - got err status ") + UnicodeString(u_errorName(status))); status = U_ZERO_ERROR; } }
-#define TESTCLASSID_TRANSLIT(c, t) { delete testClass(Transliterator::createInstance(UnicodeString(t), UTRANS_FORWARD,parseError,status), #c, "Transliterator: " #t, c ::getStaticClassID()); if(U_FAILURE(status)) { errln(UnicodeString(#c " - Transliterator: " #t " - got err status ") + UnicodeString(u_errorName(status))); status = U_ZERO_ERROR; } }
-#define TESTCLASSID_CTOR(c, x) { delete testClass(new c x, #c, "new " #c #x, c ::getStaticClassID()); if(U_FAILURE(status)) { errln(UnicodeString(#c " - new " #x " - got err status ") + UnicodeString(u_errorName(status))); status = U_ZERO_ERROR; } }
-#define TESTCLASSID_DEFAULT(c) delete testClass(new c, #c, "new " #c , c::getStaticClassID())
-#define TESTCLASSID_ABSTRACT(c) testClass(NULL, #c, NULL, c::getStaticClassID())
-#define TESTCLASSID_FACTORY_HIDDEN(c, f) {UObject *objVar = f; delete testClass(objVar, #c, #f, objVar!=NULL? objVar->getDynamicClassID(): NULL); if(U_FAILURE(status)) { errln(UnicodeString(#c " - " #f " - got err status ") + UnicodeString(u_errorName(status))); status = U_ZERO_ERROR; } }
+#define TESTCLASSID_NONE_DEFAULT(c) \
+    delete testClassNoClassID(new c, #c, "new " #c)
+#define TESTCLASSID_NONE_CTOR(c, x) { \
+    delete testClassNoClassID(new c x, #c, "new " #c #x); \
+    if(U_FAILURE(status)) { \
+        dataerrln(UnicodeString(#c " - new " #x " - got err status ") + UnicodeString(u_errorName(status))); \
+        status = U_ZERO_ERROR; \
+    } \
+}
+#define TESTCLASSID_NONE_FACTORY(c, f) { \
+    delete testClassNoClassID(f, #c, #f); \
+    if(U_FAILURE(status)) { \
+        dataerrln(UnicodeString(#c " - " #f " - got err status ") + UnicodeString(u_errorName(status))); \
+        status = U_ZERO_ERROR; \
+    } \
+}
+#define TESTCLASSID_FACTORY(c, f) { \
+    delete testClass(f, #c, #f, c ::getStaticClassID()); \
+    if(U_FAILURE(status)) { \
+        dataerrln(UnicodeString(#c " - " #f " - got err status ") + UnicodeString(u_errorName(status))); \
+        status = U_ZERO_ERROR; \
+    } \
+}
+#define TESTCLASSID_TRANSLIT(c, t) { \
+    delete testClass(Transliterator::createInstance(UnicodeString(t), UTRANS_FORWARD,parseError,status), #c, "Transliterator: " #t, c ::getStaticClassID()); \
+    if(U_FAILURE(status)) { \
+        dataerrln(UnicodeString(#c " - Transliterator: " #t " - got err status ") + UnicodeString(u_errorName(status))); \
+        status = U_ZERO_ERROR; \
+    } \
+}
+#define TESTCLASSID_CTOR(c, x) { \
+    delete testClass(new c x, #c, "new " #c #x, c ::getStaticClassID()); \
+    if(U_FAILURE(status)) { \
+        dataerrln(UnicodeString(#c " - new " #x " - got err status ") + UnicodeString(u_errorName(status))); \
+        status = U_ZERO_ERROR; \
+    } \
+}
+#define TESTCLASSID_DEFAULT(c) \
+    delete testClass(new c, #c, "new " #c , c::getStaticClassID())
+#define TESTCLASSID_ABSTRACT(c) \
+    testClass(NULL, #c, NULL, c::getStaticClassID())
+#define TESTCLASSID_FACTORY_HIDDEN(c, f) { \
+    UObject *objVar = f; \
+    delete testClass(objVar, #c, #f, objVar!=NULL? objVar->getDynamicClassID(): NULL); \
+    if(U_FAILURE(status)) { \
+        dataerrln(UnicodeString(#c " - " #f " - got err status ")  + UnicodeString(u_errorName(status))); \
+        status = U_ZERO_ERROR; \
+    } \
+}
 
 #define MAX_CLASS_ID 200
 
@@ -73,12 +127,12 @@ UObject *UObjectTest::testClass(UObject *obj,
     }
 
     if(staticID == NULL) {
-        errln("FAIL: staticID == NULL!" + what);
+        dataerrln("FAIL: staticID == NULL! " + what);
     }
 
     if(factory != NULL) {  /* NULL factory means: abstract */
         if(!obj) {
-            errln( "FAIL: ==NULL!" + what);
+            dataerrln( "FAIL: ==NULL! " + what);
             return obj;
         }
 
@@ -87,7 +141,7 @@ UObject *UObjectTest::testClass(UObject *obj,
         }
 
         if(dynamicID != staticID) {
-            errln("FAIL: dynamicID != staticID!" + what );
+            dataerrln("FAIL: dynamicID != staticID! " + what);
         }
     }
 
@@ -116,6 +170,33 @@ UObject *UObjectTest::testClass(UObject *obj,
     return obj;
 }
 
+UObject *UObjectTest::testClassNoClassID(UObject *obj, const char *className, const char *factory)
+{
+    if (!obj) {
+        return NULL;
+    }
+    UnicodeString what = UnicodeString(className) + " * x= " + UnicodeString(factory?factory:" ABSTRACT ") + "; ";
+    UClassID dynamicID = obj->getDynamicClassID();
+
+    {
+        char tmp[500];
+        sprintf(tmp, " [dynamic=%p] ", dynamicID);
+        logln(what + tmp);
+    }
+
+    if(factory != NULL) {  /* NULL factory means: abstract */
+        if(!obj) {
+            dataerrln( "FAIL: ==NULL! " + what);
+            return obj;
+        }
+
+        if(dynamicID != NULL) {
+            errln("FAIL: dynamicID != NULL! for non-poor-man's-RTTI " + what);
+        }
+    }
+
+    return obj;
+}
 
 // begin actual #includes for things to be tested
 // 
@@ -168,6 +249,8 @@ UObject *UObjectTest::testClass(UObject *obj,
 #include "reldtfmt.h"
 
 // External Things
+#include "unicode/appendable.h"
+#include "unicode/alphaindex.h"
 #include "unicode/brkiter.h"
 #include "unicode/calendar.h"
 #include "unicode/caniter.h"
@@ -185,13 +268,17 @@ UObject *UObjectTest::testClass(UObject *obj,
 #include "unicode/fmtable.h"
 #include "unicode/format.h"
 #include "unicode/gregocal.h"
+#include "unicode/idna.h"
+#include "unicode/locdspnm.h"
 #include "unicode/locid.h"
 #include "unicode/msgfmt.h"
 #include "unicode/normlzr.h"
+#include "unicode/normalizer2.h"
 #include "unicode/numfmt.h"
 #include "unicode/parsepos.h"
 #include "unicode/plurrule.h"
 #include "unicode/plurfmt.h"
+#include "unicode/selfmt.h"
 #include "unicode/rbbi.h"
 #include "unicode/rbnf.h"
 #include "unicode/regex.h"
@@ -205,6 +292,7 @@ UObject *UObjectTest::testClass(UObject *obj,
 #include "unicode/timezone.h"
 #include "unicode/translit.h"
 #include "unicode/uchriter.h"
+#include "unicode/uloc.h"
 #include "unicode/unifilt.h"
 #include "unicode/unifunct.h"
 #include "unicode/uniset.h"
@@ -226,11 +314,18 @@ public:
 };
 #endif
 
+// Appendable is abstract; we define a subclass to verify that there is no "poor man's RTTI".
+class DummyAppendable : public Appendable {
+public:
+    virtual UBool appendCodeUnit(UChar /*c*/) { return TRUE; }
+};
+
 void UObjectTest::testIDs()
 {
     ids_count = 0;
     UErrorCode status = U_ZERO_ERROR;
-    static const UChar SMALL_STR[] = {0x51, 0x51, 0x51, 0}; // "QQQ"
+
+    TESTCLASSID_NONE_CTOR(UObject, ());
 
 #if !UCONFIG_NO_TRANSLITERATION || !UCONFIG_NO_FORMATTING
     UParseError parseError;
@@ -238,7 +333,15 @@ void UObjectTest::testIDs()
    
 
 #if !UCONFIG_NO_NORMALIZATION
+    UnicodeString emptyString;
+    TESTCLASSID_CTOR(Normalizer, (emptyString, UNORM_NONE));
+    const Normalizer2 *noNormalizer2 = NULL;
+    UnicodeSet emptySet;
+    TESTCLASSID_NONE_CTOR(FilteredNormalizer2, (*noNormalizer2, emptySet));
     TESTCLASSID_FACTORY(CanonicalIterator, new CanonicalIterator(UnicodeString("abc"), status));
+#endif
+#if !UCONFIG_NO_IDNA
+    TESTCLASSID_NONE_FACTORY(IDNA, IDNA::createUTS46Instance(0, status));
 #endif
     //TESTCLASSID_DEFAULT(CollationElementIterator);
 #if !UCONFIG_NO_COLLATION
@@ -258,13 +361,17 @@ void UObjectTest::testIDs()
     TESTCLASSID_CTOR(DateFormatSymbols, (status));
     TESTCLASSID_CTOR(PluralFormat, (status));
     TESTCLASSID_CTOR(PluralRules, (status));
+    TESTCLASSID_CTOR(SelectFormat, (UnicodeString("feminine {feminineVerbValue} other{otherVerbValue}"), status) );
     TESTCLASSID_FACTORY(DateTimePatternGenerator, DateTimePatternGenerator::createInstance(status));
     TESTCLASSID_FACTORY(RelativeDateFormat, DateFormat::createDateInstance(DateFormat::kFullRelative, Locale::getUS()));
     TESTCLASSID_CTOR(DecimalFormatSymbols, (status));
     TESTCLASSID_DEFAULT(FieldPosition);
     TESTCLASSID_DEFAULT(Formattable);
+
+    static const UChar SMALL_STR[] = {0x51, 0x51, 0x51, 0}; // "QQQ"
     TESTCLASSID_CTOR(CurrencyAmount, (1.0, SMALL_STR, status));
     TESTCLASSID_CTOR(CurrencyUnit, (SMALL_STR, status));
+    TESTCLASSID_NONE_FACTORY(LocaleDisplayNames, LocaleDisplayNames::createInstance("de"));
     TESTCLASSID_FACTORY_HIDDEN(CurrencyFormat, MeasureFormat::createCurrencyFormat(Locale::getUS(), status));
     TESTCLASSID_FACTORY(GregorianCalendar, Calendar::createInstance(Locale("@calendar=gregorian"), status));
     TESTCLASSID_FACTORY(BuddhistCalendar, Calendar::createInstance(Locale("@calendar=buddhist"), status));
@@ -275,13 +382,15 @@ void UObjectTest::testIDs()
     TESTCLASSID_FACTORY(IndianCalendar, Calendar::createInstance(Locale("@calendar=indian"), status));
     TESTCLASSID_FACTORY(ChineseCalendar, Calendar::createInstance(Locale("@calendar=chinese"), status));
     TESTCLASSID_FACTORY(TaiwanCalendar, Calendar::createInstance(Locale("@calendar=roc"), status));
-#ifdef U_WINDOWS
+#if U_PLATFORM_HAS_WIN32_API
     TESTCLASSID_FACTORY(Win32DateFormat, DateFormat::createDateInstance(DateFormat::kFull, Locale("@compat=host")));
+#if U_PLATFORM_USES_ONLY_WIN32_API
     TESTCLASSID_FACTORY(Win32NumberFormat, NumberFormat::createInstance(Locale("@compat=host"), status));
 #endif
 #endif
+#endif
 
-#if !UCONFIG_NO_BREAK_ITERATION
+#if !UCONFIG_NO_BREAK_ITERATION && !UCONFIG_NO_FILE_IO
     /* TESTCLASSID_ABSTRACT(BreakIterator); No staticID!  */
     TESTCLASSID_FACTORY(RuleBasedBreakIterator, BreakIterator::createLineInstance("mt",status));
     //TESTCLASSID_FACTORY(DictionaryBasedBreakIterator, BreakIterator::createLineInstance("th",status));
@@ -321,8 +430,6 @@ void UObjectTest::testIDs()
         
     TESTCLASSID_FACTORY(Locale, new Locale("123"));
     TESTCLASSID_FACTORY_HIDDEN(KeywordEnumeration, Locale("@a=b").createKeywords(status));
-    
-    //TESTCLASSID_DEFAULT(Normalizer);
 
     //TESTCLASSID_DEFAULT(NumeratorSubstitution);
     
@@ -352,7 +459,8 @@ void UObjectTest::testIDs()
     TESTCLASSID_FACTORY(OlsonTimeZone, TimeZone::createTimeZone(UnicodeString("America/Los_Angeles")));
     TESTCLASSID_FACTORY_HIDDEN(KeywordEnumeration, TimeZone::createEnumeration());
 #endif
-    
+
+    TESTCLASSID_NONE_DEFAULT(DummyAppendable);
     TESTCLASSID_DEFAULT(UnicodeString);
     TESTCLASSID_CTOR(UnicodeSet, (0, 1));
     TESTCLASSID_ABSTRACT(UnicodeFilter);
@@ -377,6 +485,10 @@ void UObjectTest::testIDs()
 //#endif
 #endif
 
+#if !UCONFIG_NO_COLLATION && !UCONFIG_NO_NORMALIZATION
+    TESTCLASSID_NONE_CTOR(AlphabeticIndex, (Locale::getEnglish(), status));
+#endif
+
 #if UOBJTEST_DUMP_IDS
     int i;
     for(i=0;i<ids_count;i++) {
@@ -391,12 +503,16 @@ void UObjectTest::testIDs()
 void UObjectTest::testUMemory() {
     // additional tests for code coverage
 #if U_OVERRIDE_CXX_ALLOCATION && U_HAVE_PLACEMENT_NEW
-    UAlignedMemory stackMemory[sizeof(UnicodeString)/sizeof(UAlignedMemory)+1];
+    union {
+        UAlignedMemory   align_;
+        char             bytes_[sizeof(UnicodeString)];
+    } stackMemory;
+    char *bytes = stackMemory.bytes_;
     UnicodeString *p;
     enum { len=20 };
 
-    p=new(stackMemory) UnicodeString(len, (UChar32)0x20ac, len);
-    if((void *)p!=(void *)stackMemory) {
+    p=new(bytes) UnicodeString(len, (UChar32)0x20ac, len);
+    if((void *)p!=(void *)bytes) {
         errln("placement new did not place the object at the expected address");
     }
     if(p->length()!=len || p->charAt(0)!=0x20ac || p->charAt(len-1)!=0x20ac) {
@@ -433,7 +549,11 @@ void UObjectTest::testUMemory() {
      */
     // destroy object and delete space manually
     p->~UnicodeString(); 
-    UnicodeString::operator delete(p, stackMemory); 
+
+    // You normally wouldn't call an operator delete for object placed on the
+    // stack with a placement new().
+    // This overload of delete is a nop, and is called here for code coverage purposes.
+    UnicodeString::operator delete(p, bytes); 
 
     // Jitterbug 4452, for coverage
     UnicodeString *pa = new UnicodeString[2];
@@ -460,18 +580,38 @@ void UObjectTest::TestMFCCompatibility() {
 #endif
 }
 
-/* --------------- */
-
-#define CASE(id,test) case id: name = #test; if (exec) { logln(#test "---"); logln((UnicodeString)""); test(); } break;
+void UObjectTest::TestCompilerRTTI() {
+#if !UCONFIG_NO_FORMATTING
+    UErrorCode errorCode = U_ZERO_ERROR;
+    NumberFormat *nf = NumberFormat::createInstance("de", errorCode);
+    if (U_FAILURE(errorCode)) {
+        dataerrln("NumberFormat::createInstance(de) failed - %s", u_errorName(errorCode));
+        return;
+    }
+    if (dynamic_cast<DecimalFormat *>(nf) == NULL || dynamic_cast<ChoiceFormat *>(nf) != NULL) {
+        errln("dynamic_cast<>(NumberFormat) failed");
+    }
+    UnicodeSet emptySet;
+    if (&typeid(*nf) == NULL || typeid(*nf) == typeid(UObject) || typeid(*nf) == typeid(Format) ||
+        typeid(*nf) != typeid(DecimalFormat) || typeid(*nf) == typeid(ChoiceFormat) ||
+        typeid(*nf) == typeid(emptySet)
+    ) {
+        errln("typeid(NumberFormat) failed");
+    }
+    delete nf;
+#endif
+}
 
+/* --------------- */
 
 void UObjectTest::runIndexedTest( int32_t index, UBool exec, const char* &name, char* /* par */ )
 {
     switch (index) {
 
-    CASE(0, testIDs);
-    CASE(1, testUMemory);
-    CASE(2, TestMFCCompatibility);
+    TESTCASE(0, testIDs);
+    TESTCASE(1, testUMemory);
+    TESTCASE(2, TestMFCCompatibility);
+    TESTCASE(3, TestCompilerRTTI);
 
     default: name = ""; break; //needed to end loop
     }