/******************************************************************** * COPYRIGHT: * Copyright (c) 1997-2010, International Business Machines Corporation and * others. All Rights Reserved. * Copyright (C) 2010 , Yahoo! Inc. ********************************************************************/ #include "uobjtest.h" #include "cmemory.h" // UAlignedMemory #include #include /** * * Test for UObject, currently only the classID. * * Usage * TESTCLASSID_ABSTRACT(Bar) * -- Bar is expected to be abstract. Only the static ID will be tested. * * TESTCLASSID_DEFAULT(Foo) * -- Foo will be default-constructed. * * TESTCLASSID_CTOR(Foo, (1, 2, 3, status)) * -- Second argument is (parenthesized) constructor argument. * Will be called as: new Foo ( 1, 2, 3, status) [status is tested] * * 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)) { 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 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) { dataerrln("FAIL: staticID == NULL! " + what); } if(factory != NULL) { /* NULL factory means: abstract */ if(!obj) { dataerrln( "FAIL: ==NULL! " + what); return obj; } if(dynamicID == NULL) { errln("FAIL: dynamicID == NULL!" + what); } if(dynamicID != staticID) { dataerrln("FAIL: dynamicID != staticID! " + what); } } // Bail out if static ID is null. Error message was already printed. if(staticID == NULL) { return obj; } for(i=0;i>;"), parseError, status)); TESTCLASSID_CTOR(ChoiceFormat, (UNICODE_STRING_SIMPLE("0#are no files|1#is one file|1b;"),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_FACTORY_HIDDEN(KeywordEnumeration, Locale("@a=b").createKeywords(status)); //TESTCLASSID_DEFAULT(Normalizer); //TESTCLASSID_DEFAULT(NumeratorSubstitution); #if !UCONFIG_NO_TRANSLITERATION TESTCLASSID_DEFAULT(ParsePosition); #endif // NO_REG_EX //TESTCLASSID_DEFAULT(RegexCompile); //TESTCLASSID_DEFAULT(RegexMatcher); //TESTCLASSID_DEFAULT(RegexPattern); //TESTCLASSID_DEFAULT(ReplaceableGlue); TESTCLASSID_FACTORY(ResourceBundle, new ResourceBundle(UnicodeString(), status) ); //TESTCLASSID_DEFAULT(RuleBasedTransliterator); //TESTCLASSID_DEFAULT(SimpleFwdCharIterator); //TESTCLASSID_DEFAULT(StringReplacer); //TESTCLASSID_DEFAULT(StringSearch); //TESTCLASSID_DEFAULT(TestMultipleKeyStringFactory); //TESTCLASSID_DEFAULT(TestReplaceable); #if !UCONFIG_NO_FORMATTING TESTCLASSID_ABSTRACT(TimeZone); TESTCLASSID_FACTORY(OlsonTimeZone, TimeZone::createTimeZone(UnicodeString("America/Los_Angeles"))); TESTCLASSID_FACTORY_HIDDEN(KeywordEnumeration, TimeZone::createEnumeration()); #endif TESTCLASSID_DEFAULT(UnicodeString); TESTCLASSID_CTOR(UnicodeSet, (0, 1)); TESTCLASSID_ABSTRACT(UnicodeFilter); TESTCLASSID_ABSTRACT(UnicodeFunctor); 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); TESTCLASSID_DEFAULT(ICUResourceBundleFactory); //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) ); TESTCLASSID_CTOR(TestLocaleKeyFactory, (42)); // Test replacement for LocaleKeyFactory //#if UOBJTEST_TEST_INTERNALS // TESTCLASSID_CTOR(LocaleKeyFactory, (42)); //#endif #endif #if UOBJTEST_DUMP_IDS int i; for(i=0;ilength()!=len || p->charAt(0)!=0x20ac || p->charAt(len-1)!=0x20ac) { errln("constructor used with placement new did not work right"); } /* * It is not possible to simply say * delete(p, stackMemory); * which results in a call to the normal, non-placement delete operator. * * Via a search on google.com for "c++ placement delete" I found * http://cpptips.hyperformix.com/cpptips/placement_del3 * which says: * * TITLE: using placement delete * * (Newsgroups: comp.std.c++, 27 Aug 97) * * ISJ: isj@image.dk * * > I do not completely understand how placement works on operator delete. * > ... * There is no delete-expression which will invoke a placement * form of operator delete. You can still call the function * explicitly. Example: * ... * // destroy object and delete space manually * p->~T(); * operator delete(p, 12); * * ... so that's what I am doing here. * markus 20031216 */ // destroy object and delete space manually p->~UnicodeString(); UnicodeString::operator delete(p, stackMemory); // 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 &) UMemory m, n; 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::runIndexedTest( int32_t index, UBool exec, const char* &name, char* /* par */ ) { switch (index) { CASE(0, testIDs); CASE(1, testUMemory); CASE(2, TestMFCCompatibility); default: name = ""; break; //needed to end loop } }