X-Git-Url: https://git.saurik.com/apple/icu.git/blobdiff_plain/374ca955a76ecab1204ca8bfa63ff9238d998416..b25be06635768807f8f693286fa73bb2297bb06c:/icuSources/test/intltest/uobjtest.cpp?ds=inline diff --git a/icuSources/test/intltest/uobjtest.cpp b/icuSources/test/intltest/uobjtest.cpp index 8354871c..efbf9523 100644 --- a/icuSources/test/intltest/uobjtest.cpp +++ b/icuSources/test/intltest/uobjtest.cpp @@ -1,19 +1,31 @@ /******************************************************************** * COPYRIGHT: - * Copyright (c) 2002-2004, International Business Machines Corporation and + * Copyright (c) 1997-2010, International Business Machines Corporation and * others. All Rights Reserved. + * Copyright (C) 2010 , Yahoo! Inc. ********************************************************************/ +#include +#include +#include // for 'typeid' to work + #include "uobjtest.h" #include "cmemory.h" // UAlignedMemory -#include -#include /** - * * 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. * @@ -26,91 +38,165 @@ * * TESTCLASSID_FACTORY(Foo, fooCreateFunction(status) ) * -- call fooCreateFunction. 'status' will be tested & reset + * + * TESTCLASSID_FACTORY_HIDDEN(class, factory) + * -- call factory. Class is not available from a header. + * '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_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 -UClassID ids[MAX_CLASS_ID]; -const char *ids_factory[MAX_CLASS_ID]; -const char *ids_class[MAX_CLASS_ID]; -uint32_t ids_count = 0; +static UClassID ids[MAX_CLASS_ID]; +static const char *ids_factory[MAX_CLASS_ID]; +static const char *ids_class[MAX_CLASS_ID]; +static uint32_t ids_count = 0; UObject *UObjectTest::testClass(UObject *obj, const char *className, const char *factory, UClassID staticID) { - uint32_t i; - UnicodeString what = UnicodeString(className) + " * x= " + UnicodeString(factory?factory:" ABSTRACT ") + "; "; - UClassID dynamicID = NULL; - - if(ids_count >= MAX_CLASS_ID) { - char count[100]; - sprintf(count, " (currently %d) ", MAX_CLASS_ID); - errln("FAIL: Fatal: Ran out of IDs! Increase MAX_CLASS_ID." + UnicodeString(count) + what); - return obj; - } - - if(obj) { - dynamicID = obj->getDynamicClassID(); - } - - { - char tmp[500]; - sprintf(tmp, " [static=%p, dynamic=%p] ", staticID, dynamicID); - logln(what + tmp); - } - - if(staticID == NULL) { - errln( "FAIL: staticID == NULL!" + what); - } - - if(factory != NULL) { /* NULL factory means: abstract */ - if(!obj) { - errln( "FAIL: ==NULL!" + what); - return obj; + uint32_t i; + UnicodeString what = UnicodeString(className) + " * x= " + UnicodeString(factory?factory:" ABSTRACT ") + "; "; + UClassID dynamicID = NULL; + + if(ids_count >= MAX_CLASS_ID) { + char count[100]; + sprintf(count, " (currently %d) ", MAX_CLASS_ID); + errln("FAIL: Fatal: Ran out of IDs! Increase MAX_CLASS_ID." + UnicodeString(count) + what); + return obj; } - if(dynamicID == NULL) { - errln("FAIL: dynamicID == NULL!" + what); + if(obj) { + dynamicID = obj->getDynamicClassID(); } - - if(dynamicID != staticID) { - errln("FAIL: dynamicID != staticID!" + what ); + + { + char tmp[500]; + sprintf(tmp, " [static=%p, dynamic=%p] ", staticID, dynamicID); + logln(what + tmp); } - } - // Bail out if static ID is null - if(staticID == NULL) { - return obj; - } - - for(i=0;igetDynamicClassID(); + + { + 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 // @@ -121,11 +207,6 @@ UObject *UObjectTest::testClass(UObject *obj, #include "unicode/utypes.h" -// Things we Patch -#define protected public /* to access private factory function */ -#include "iculserv.h" -#undef protected - // Internal Things (woo) #include "cpdtrans.h" #include "rbt.h" @@ -135,8 +216,9 @@ UObject *UObjectTest::testClass(UObject *obj, #include "digitlst.h" #include "esctrn.h" #include "funcrepl.h" -#include "icunotif.h" -#include "icuserv.h" +#include "servnotf.h" +#include "serv.h" +#include "servloc.h" #include "name2uni.h" #include "nfsubs.h" #include "nortrans.h" @@ -150,7 +232,21 @@ UObject *UObjectTest::testClass(UObject *obj, #include "unesctrn.h" #include "uni2name.h" #include "uvector.h" +#include "uvectr32.h" +#include "currfmt.h" +#include "buddhcal.h" #include "islamcal.h" +#include "japancal.h" +#include "hebrwcal.h" +#include "persncal.h" +#include "taiwncal.h" +#include "indiancal.h" +#include "chnsecal.h" +#include "windtfmt.h" +#include "winnmfmt.h" +#include "ustrenum.h" +#include "olsontz.h" +#include "reldtfmt.h" // External Things #include "unicode/brkiter.h" @@ -160,20 +256,27 @@ UObject *UObjectTest::testClass(UObject *obj, #include "unicode/choicfmt.h" #include "unicode/coleitr.h" #include "unicode/coll.h" +#include "unicode/curramt.h" #include "unicode/datefmt.h" -#include "unicode/dbbi.h" #include "unicode/dcfmtsym.h" #include "unicode/decimfmt.h" #include "unicode/dtfmtsym.h" +#include "unicode/dtptngen.h" #include "unicode/fieldpos.h" #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" @@ -200,27 +303,41 @@ UObject *UObjectTest::testClass(UObject *obj, #define UOBJTEST_TEST_INTERNALS 0 /* do NOT test Internal things - their functions aren't exported on Win32 */ +#if !UCONFIG_NO_SERVICE +/* The whole purpose of this class is to expose the constructor, and gain access to the superclasses RTTI. */ +class TestLocaleKeyFactory : public LocaleKeyFactory { +public: + TestLocaleKeyFactory(int32_t coverage) : LocaleKeyFactory(coverage) {} +}; +#endif + void UObjectTest::testIDs() { ids_count = 0; + UErrorCode status = U_ZERO_ERROR; + static const UChar SMALL_STR[] = {0x51, 0x51, 0x51, 0}; // "QQQ" -#if !UCONFIG_NO_TRANSLITERATION +#if !UCONFIG_NO_TRANSLITERATION || !UCONFIG_NO_FORMATTING UParseError parseError; #endif - UErrorCode status = U_ZERO_ERROR; - - //TESTCLASSID_DEFAULT(AbbreviatedUnicodeSetIterator); - //TESTCLASSID_DEFAULT(AnonymousStringFactory); - - #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 TESTCLASSID_DEFAULT(CollationKey); + TESTCLASSID_FACTORY(UStringEnumeration, Collator::getKeywords(status)); + //TESTCLASSID_FACTORY_HIDDEN(CollationLocaleListEnumeration, Collator::getAvailableLocales()); #endif //TESTCLASSID_FACTORY(CompoundTransliterator, Transliterator::createInstance(UnicodeString("Any-Jex;Hangul-Jamo"), UTRANS_FORWARD, parseError, status)); @@ -228,30 +345,50 @@ void UObjectTest::testIDs() /* TESTCLASSID_FACTORY(NFSubstitution, NFSubstitution::makeSubstitution(8, */ /* TESTCLASSID_DEFAULT(DigitList); UMemory but not UObject*/ TESTCLASSID_ABSTRACT(NumberFormat); + TESTCLASSID_CTOR(RuleBasedNumberFormat, (UnicodeString("%default: -x: minus >>;"), parseError, status)); + TESTCLASSID_CTOR(ChoiceFormat, (UNICODE_STRING_SIMPLE("0#are no files|1#is one file|1b;"), status)); + TESTCLASSID_FACTORY(RuleBasedTransliterator, Transliterator::createFromRules(UnicodeString("abcd"),UnicodeString("a>b;"),UTRANS_FORWARD,parseError,status)); TESTCLASSID_TRANSLIT(TitlecaseTransliterator, "Title"); TESTCLASSID_TRANSLIT(UnescapeTransliterator, "Hex-Any"); TESTCLASSID_TRANSLIT(UnicodeNameTransliterator, "Any-Name"); TESTCLASSID_TRANSLIT(UppercaseTransliterator, "Upper"); + TESTCLASSID_ABSTRACT(CaseMapTransliterator); + TESTCLASSID_ABSTRACT(Transliterator); + TESTCLASSID_FACTORY_HIDDEN(TransliteratorRegistry::Enumeration, Transliterator::getAvailableIDs(status)); + +#if UOBJTEST_TEST_INTERNALS + TESTCLASSID_CTOR(Quantifier, (NULL, 0, 0)); + TESTCLASSID_CTOR(FunctionReplacer, (NULL,NULL)); + TESTCLASSID_CTOR(StringMatcher, (UnicodeString("x"), 0,0,0,TransliterationRuleData(status))); + TESTCLASSID_CTOR(StringReplacer,(UnicodeString(),new TransliterationRuleData(status))); +#endif #endif TESTCLASSID_FACTORY(Locale, new Locale("123")); - - //TESTCLASSID_DEFAULT(Normalizer); + TESTCLASSID_FACTORY_HIDDEN(KeywordEnumeration, Locale("@a=b").createKeywords(status)); //TESTCLASSID_DEFAULT(NumeratorSubstitution); #if !UCONFIG_NO_TRANSLITERATION TESTCLASSID_DEFAULT(ParsePosition); - //TESTCLASSID_DEFAULT(Quantifier); #endif @@ -292,22 +437,13 @@ void UObjectTest::testIDs() //TESTCLASSID_DEFAULT(StringReplacer); //TESTCLASSID_DEFAULT(StringSearch); - //TESTCLASSID_DEFAULT(TempSearch); //TESTCLASSID_DEFAULT(TestMultipleKeyStringFactory); //TESTCLASSID_DEFAULT(TestReplaceable); #if !UCONFIG_NO_FORMATTING TESTCLASSID_ABSTRACT(TimeZone); -#endif - -#if !UCONFIG_NO_TRANSLITERATION - TESTCLASSID_FACTORY(TitlecaseTransliterator, Transliterator::createInstance(UnicodeString("Any-Title"), UTRANS_FORWARD, parseError, status)); - TESTCLASSID_ABSTRACT(Transliterator); - -#if UOBJTEST_TEST_INTERNALS - TESTCLASSID_CTOR(StringMatcher, (UnicodeString("x"), 0,0,0,TransliterationRuleData(status))); - TESTCLASSID_CTOR(StringReplacer,(UnicodeString(),new TransliterationRuleData(status))); -#endif + TESTCLASSID_FACTORY(OlsonTimeZone, TimeZone::createTimeZone(UnicodeString("America/Los_Angeles"))); + TESTCLASSID_FACTORY_HIDDEN(KeywordEnumeration, TimeZone::createEnumeration()); #endif TESTCLASSID_DEFAULT(UnicodeString); @@ -317,17 +453,21 @@ void UObjectTest::testIDs() TESTCLASSID_CTOR(UnicodeSetIterator,(UnicodeSet(0,1))); TESTCLASSID_CTOR(UStack, (status)); TESTCLASSID_CTOR(UVector, (status)); + TESTCLASSID_CTOR(UVector32, (status)); #if !UCONFIG_NO_SERVICE TESTCLASSID_CTOR(SimpleFactory, (NULL, UnicodeString("foo"))); TESTCLASSID_DEFAULT(EventListener); -#if UOBJTEST_TEST_INTERNALS TESTCLASSID_DEFAULT(ICUResourceBundleFactory); - //TESTCLASSID_DEFAULT(Key); // does ont exist? - TESTCLASSID_CTOR(LocaleKey, (UnicodeString("baz"), UnicodeString("bat"), NULL, 92)); - TESTCLASSID_CTOR(LocaleKeyFactory, (42)); + //TESTCLASSID_DEFAULT(Key); // does not exist? + UnicodeString baz("baz"); + UnicodeString bat("bat"); + TESTCLASSID_FACTORY(LocaleKey, LocaleKey::createWithCanonicalFallback(&baz, &bat, LocaleKey::KIND_ANY, status)); TESTCLASSID_CTOR(SimpleLocaleKeyFactory, (NULL, UnicodeString("bar"), 8, 12) ); -#endif + TESTCLASSID_CTOR(TestLocaleKeyFactory, (42)); // Test replacement for LocaleKeyFactory +//#if UOBJTEST_TEST_INTERNALS +// TESTCLASSID_CTOR(LocaleKeyFactory, (42)); +//#endif #endif #if UOBJTEST_DUMP_IDS @@ -344,12 +484,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) { @@ -386,7 +530,18 @@ 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]; + if ( !pa[0].isEmpty() || !pa[1].isEmpty()){ + errln("constructor used with array new did not work right"); + } + delete [] pa; #endif // try to call the compiler-generated UMemory::operator=(class UMemory const &) @@ -394,17 +549,48 @@ void UObjectTest::testUMemory() { m=n; } -/* --------------- */ +void UObjectTest::TestMFCCompatibility() { +#if U_HAVE_DEBUG_LOCATION_NEW + /* Make sure that it compiles with MFC's debuggable new usage. */ + UnicodeString *str = new(__FILE__, __LINE__) UnicodeString(); + str->append((UChar)0x0040); // Is it usable? + if(str->charAt(0) != 0x0040) { + errln("debug new doesn't work."); + } + UnicodeString::operator delete(str, __FILE__, __LINE__); +#endif +} -#define CASE(id,test) case id: name = #test; if (exec) { logln(#test "---"); logln((UnicodeString)""); test(); } break; +void UObjectTest::TestCompilerRTTI() { + 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(nf) == NULL || dynamic_cast(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; +} +/* --------------- */ void UObjectTest::runIndexedTest( int32_t index, UBool exec, const char* &name, char* /* par */ ) { switch (index) { - CASE(0, testIDs); - CASE(1, testUMemory); + TESTCASE(0, testIDs); + TESTCASE(1, testUMemory); + TESTCASE(2, TestMFCCompatibility); + TESTCASE(3, TestCompilerRTTI); default: name = ""; break; //needed to end loop }