X-Git-Url: https://git.saurik.com/apple/icu.git/blobdiff_plain/374ca955a76ecab1204ca8bfa63ff9238d998416..4388f060552cc537e71e957d32f35e9d75a61233:/icuSources/test/intltest/icusvtst.cpp diff --git a/icuSources/test/intltest/icusvtst.cpp b/icuSources/test/intltest/icusvtst.cpp index 1a25027a..c45c68c9 100644 --- a/icuSources/test/intltest/icusvtst.cpp +++ b/icuSources/test/intltest/icusvtst.cpp @@ -1,18 +1,18 @@ /** ******************************************************************************* - * Copyright (C) 2001-2004, International Business Machines Corporation and * - * others. All Rights Reserved. * - ******************************************************************************* - * + * Copyright (C) 2001-2011, International Business Machines Corporation and + * others. All Rights Reserved. ******************************************************************************* */ +#include // for 'typeid' to work + #include "unicode/utypes.h" #if !UCONFIG_NO_SERVICE #include "icusvtst.h" -#include "iculserv.h" +#include "servloc.h" #include @@ -24,15 +24,24 @@ class WrongListener : public EventListener { class ICUNSubclass : public ICUNotifier { public: - UBool acceptsListener(const EventListener& l) const { + UBool acceptsListener(const EventListener& /*l*/) const { return TRUE; // return l instanceof MyListener; } - virtual void notifyListener(EventListener& l) const { + virtual void notifyListener(EventListener& /*l*/) const { } }; +// This factory does nothing +class LKFSubclass0 : public LocaleKeyFactory { +public: + LKFSubclass0() + : LocaleKeyFactory(VISIBLE, "LKFSubclass0") + { + } +}; + class LKFSubclass : public LocaleKeyFactory { Hashtable table; @@ -62,22 +71,22 @@ class Integer : public UObject { virtual ~Integer() { } - virtual UBool operator==(const UObject& other) const - { - return other.getDynamicClassID() == getStaticClassID() && - _val == ((Integer&)other)._val; - } - public: /** * UObject boilerplate. */ + static UClassID getStaticClassID() { + return (UClassID)&fgClassID; + } + virtual UClassID getDynamicClassID() const { return getStaticClassID(); } - static UClassID getStaticClassID() { - return (UClassID)&fgClassID; + virtual UBool operator==(const UObject& other) const + { + return typeid(*this) == typeid(other) && + _val == ((Integer&)other)._val; } public: @@ -107,8 +116,9 @@ class TestIntegerService : public ICUService { virtual ICUServiceFactory* createSimpleFactory(UObject* obj, const UnicodeString& id, UBool visible, UErrorCode& status) { - if (U_SUCCESS(status) && obj && obj->getDynamicClassID() == Integer::getStaticClassID()) { - return new SimpleFactory((Integer*)obj, id, visible); + Integer* i; + if (U_SUCCESS(status) && obj && (i = dynamic_cast(obj)) != NULL) { + return new SimpleFactory(i, id, visible); } return NULL; } @@ -147,13 +157,15 @@ UnicodeString append(UnicodeString& result, const UObject* obj) if (obj == NULL) { result.append("NULL"); } else { - UClassID id = obj->getDynamicClassID(); - if (id == UnicodeString::getStaticClassID()) { - result.append(*(UnicodeString*)obj); - } else if (id == Locale::getStaticClassID()) { - result.append(((Locale*)obj)->getName()); - } else if (id == Integer::getStaticClassID()) { - sprintf(buffer, "%d", (int)((Integer*)obj)->_val); + const UnicodeString* s; + const Locale* loc; + const Integer* i; + if ((s = dynamic_cast(obj)) != NULL) { + result.append(*s); + } else if ((loc = dynamic_cast(obj)) != NULL) { + result.append(loc->getName()); + } else if ((i = dynamic_cast(obj)) != NULL) { + sprintf(buffer, "%d", (int)i->_val); result.append(buffer); } else { sprintf(buffer, "%p", (const void*)obj); @@ -269,7 +281,7 @@ ICUServiceTest::confirmStringsEqual(const UnicodeString& message, const UnicodeS if (equ) { logln(temp); } else { - errln(temp); + dataerrln(temp); } } @@ -446,7 +458,7 @@ ICUServiceTest::testAPI_One() // should not be able to locate invisible services { UErrorCode status = U_ZERO_ERROR; - UVector ids(uhash_deleteUnicodeString, uhash_compareUnicodeString, status); + UVector ids(uprv_deleteUObject, uhash_compareUnicodeString, status); service.getVisibleIDs(ids, status); UnicodeString target = "en_US_BAR"; confirmBoolean("18) find invisible", !ids.contains(&target)); @@ -460,6 +472,25 @@ ICUServiceTest::testAPI_One() /* ****************************************************************** */ +class TestStringSimpleKeyService : public ICUService { +public: + + virtual ICUServiceFactory* createSimpleFactory(UObject* obj, const UnicodeString& id, UBool visible, UErrorCode& status) + { + // We could put this type check into ICUService itself, but we'd still + // have to implement cloneInstance. Otherwise we could just tell the service + // what the object type is when we create it, and the default implementation + // could handle everything for us. Phooey. + if (obj && dynamic_cast(obj) != NULL) { + return ICUService::createSimpleFactory(obj, id, visible, status); + } + return NULL; + } + + virtual UObject* cloneInstance(UObject* instance) const { + return instance ? new UnicodeString(*(UnicodeString*)instance) : NULL; + } +}; class TestStringService : public ICUService { public: @@ -469,8 +500,9 @@ class TestStringService : public ICUService { virtual ICUServiceFactory* createSimpleFactory(UObject* obj, const UnicodeString& id, UBool visible, UErrorCode& /* status */) { - if (obj && obj->getDynamicClassID() == UnicodeString::getStaticClassID()) { - return new SimpleFactory((UnicodeString*)obj, id, visible); + UnicodeString* s; + if (obj && (s = dynamic_cast(obj)) != NULL) { + return new SimpleFactory(s, id, visible); } return NULL; } @@ -497,14 +529,14 @@ class AnonymousStringFactory : public ICUServiceFactory return result; } - virtual UClassID getDynamicClassID() const { - return getStaticClassID(); - } - static UClassID getStaticClassID() { return (UClassID)&fgClassID; } + virtual UClassID getDynamicClassID() const { + return getStaticClassID(); + } + private: static const char fgClassID; }; @@ -519,7 +551,7 @@ class TestMultipleKeyStringFactory : public ICUServiceFactory { public: TestMultipleKeyStringFactory(const UnicodeString ids[], int32_t count, const UnicodeString& factoryID) : _status(U_ZERO_ERROR) - , _ids(uhash_deleteUnicodeString, uhash_compareUnicodeString, count, _status) + , _ids(uprv_deleteUObject, uhash_compareUnicodeString, count, _status) , _factoryID(factoryID + ": ") { for (int i = 0; i < count; ++i) { @@ -573,14 +605,14 @@ class TestMultipleKeyStringFactory : public ICUServiceFactory { return result; } - virtual UClassID getDynamicClassID() const { - return getStaticClassID(); - } - static UClassID getStaticClassID() { return (UClassID)&fgClassID; } + virtual UClassID getDynamicClassID() const { + return getStaticClassID(); + } + private: static const char fgClassID; }; @@ -648,7 +680,7 @@ ICUServiceTest::testAPI_Two() // iterate over the visual ids returned by the multiple factory { UErrorCode status = U_ZERO_ERROR; - UVector ids(uhash_deleteUnicodeString, uhash_compareUnicodeString, 0, status); + UVector ids(uprv_deleteUObject, uhash_compareUnicodeString, 0, status); service.getVisibleIDs(ids, status); for (int i = 0; i < ids.size(); ++i) { const UnicodeString* id = (const UnicodeString*)ids[i]; @@ -667,8 +699,8 @@ ICUServiceTest::testAPI_Two() // iterate over the display names { UErrorCode status = U_ZERO_ERROR; - UVector names(userv_deleteStringPair, NULL, status); - service.getDisplayNames(names, Locale::getGerman(), status); + UVector names(status); + service.getDisplayNames(names, status); for (int i = 0; i < names.size(); ++i) { const StringPair* pair = (const StringPair*)names[i]; logln(" " + pair->displayName + " --> " + pair->id); @@ -707,13 +739,13 @@ ICUServiceTest::testAPI_Two() // Rad dude's surfer gal 'replaces' Later's surfer gal { UErrorCode status = U_ZERO_ERROR; - UVector names(userv_deleteStringPair, NULL, status); + UVector names(status); service.getDisplayNames(names, Locale("es"), status); for (int i = 0; i < names.size(); ++i) { const StringPair* pair = (const StringPair*)names[i]; logln(" " + pair->displayName + " --> " + pair->id); } - confirmIdentical("26) display names", names.size(), 7); + confirmIdentical("29) display names", names.size(), 7); } // we should get the display name corresponding to the actual id @@ -769,7 +801,7 @@ ICUServiceTest::testAPI_Two() { UErrorCode status = U_ZERO_ERROR; - UVector ids(uhash_deleteUnicodeString, uhash_compareUnicodeString, 0, status); + UVector ids(uprv_deleteUObject, uhash_compareUnicodeString, 0, status); service.getVisibleIDs(ids, status); for (int i = 0; i < ids.size(); ++i) { const UnicodeString* id = (const UnicodeString*)ids[i]; @@ -865,7 +897,7 @@ ICUServiceTest::testRBF() // list all of the resources { UErrorCode status = U_ZERO_ERROR; - UVector ids(uhash_deleteUnicodeString, uhash_compareUnicodeString, 0, status); + UVector ids(uprv_deleteUObject, uhash_compareUnicodeString, 0, status); service.getVisibleIDs(ids, status); logln("all visible ids:"); for (int i = 0; i < ids.size(); ++i) { @@ -878,7 +910,7 @@ ICUServiceTest::testRBF() // this should be fast since the display names were cached. { UErrorCode status = U_ZERO_ERROR; - UVector names(userv_deleteStringPair, NULL, status); + UVector names(status); service.getDisplayNames(names, Locale::getGermany(), status); logln("service display names for de_DE"); for (int i = 0; i < names.size(); ++i) { @@ -904,7 +936,7 @@ ICUServiceTest::testRBF() logln(UnicodeString("\n --- ") + idNames[i] + " ---"); { UErrorCode status = U_ZERO_ERROR; - UVector names(userv_deleteStringPair, NULL, status); + UVector names(status); service.getDisplayNames(names, idNames[i], status); for (int i = 0; i < names.size(); ++i) { const StringPair* pair = (const StringPair*)names[i]; @@ -1014,7 +1046,7 @@ void ICUServiceTest::testLocale() { service.registerInstance(root, "", status); service.registerInstance(german, "de", status); service.registerInstance(germany, Locale::getGermany(), status); - service.registerInstance(japanese, "ja", status); + service.registerInstance(japanese, (UnicodeString)"ja", TRUE, status); service.registerInstance(japan, Locale::getJapan(), status); { @@ -1132,7 +1164,7 @@ void ICUServiceTest::testLocale() { { UErrorCode status = U_ZERO_ERROR; - UVector ids(uhash_deleteUnicodeString, uhash_compareUnicodeString, 0, status); + UVector ids(uprv_deleteUObject, uhash_compareUnicodeString, 0, status); service.getVisibleIDs(ids, status); logln("all visible ids:"); for (int i = 0; i < ids.size(); ++i) { @@ -1144,7 +1176,7 @@ void ICUServiceTest::testLocale() { Locale::setDefault(loc, status); { UErrorCode status = U_ZERO_ERROR; - UVector ids(uhash_deleteUnicodeString, uhash_compareUnicodeString, 0, status); + UVector ids(uprv_deleteUObject, uhash_compareUnicodeString, 0, status); service.getVisibleIDs(ids, status); logln("all visible ids:"); for (int i = 0; i < ids.size(); ++i) { @@ -1225,14 +1257,14 @@ class WrapFactory : public ICUServiceFactory { /** * UObject boilerplate. */ - virtual UClassID getDynamicClassID() const { - return getStaticClassID(); - } - static UClassID getStaticClassID() { return (UClassID)&fgClassID; } + virtual UClassID getDynamicClassID() const { + return getStaticClassID(); + } + private: static const char fgClassID; static UnicodeString* greetingID; @@ -1303,18 +1335,45 @@ void ICUServiceTest::testCoverage() } // ICUService - TestStringService service; - service.registerFactory(sf, status); - - { - UnicodeString* result = (UnicodeString*)service.get("object", status); - if (result) { - logln("object is: " + *result); - delete result; - } else { - errln("could not get object"); - } - } + { + TestStringService service; + service.registerFactory(sf, status); + + { + UnicodeString* result = (UnicodeString*)service.get("object", status); + if (result) { + logln("object is: " + *result); + delete result; + } else { + errln("could not get object"); + } + } + } + } + + // ICUServiceKey + { + UErrorCode status = U_ZERO_ERROR; + UnicodeString* howdy = new UnicodeString("Howdy"); + + TestStringSimpleKeyService service; + service.registerInstance(howdy, "Greetings", status); + { + UnicodeString* result = (UnicodeString*)service.get("Greetings", status); + if (result) { + logln("object is: " + *result); + delete result; + } else { + errln("could not get object"); + } + } + + UVector ids(uprv_deleteUObject, uhash_compareUnicodeString, status); + // yuck, this is awkward to use. All because we pass null in an overload. + // TODO: change this. + UnicodeString str("Greet"); + service.getVisibleIDs(ids, &str, status); + confirmIdentical("no fallback of greet", ids.size(), 0); } // ICULocaleService @@ -1374,15 +1433,40 @@ void ICUServiceTest::testCoverage() } delete obj; delete key; - } + key = LocaleKey::createWithCanonicalFallback(&primary, &fallback, 123, status); + if (U_SUCCESS(status)) { + UnicodeString str; + key->currentDescriptor(str); + key->parsePrefix(str); + if (str != "123") { + errln("did not get expected prefix"); + } + delete key; + } + + // coverage, getSupportedIDs is either overridden or the calling method is + LKFSubclass0 lkFactory; + Hashtable table0; + lkFactory.updateVisibleIDs(table0, status); + if (table0.count() != 0) { + errln("LKF returned non-empty hashtable"); + } -#if 0 - // ResourceBundleFactory - ICUResourceBundleFactory rbf = new ICUResourceBundleFactory(); - logln("RB: " + rbf.create(lkey, null)); - // ICUNotifier + // ResourceBundleFactory + key = LocaleKey::createWithCanonicalFallback(&primary, &fallback, status); + ICUResourceBundleFactory rbf; + UObject* icurb = rbf.create(*key, NULL, status); + if (icurb != NULL) { + logln("got resource bundle for key"); + delete icurb; + } + delete key; + } + + #if 0 + // ICUNotifier ICUNotifier nf = new ICUNSubclass(); try { nf.addListener(null);