1 // Copyright (C) 2016 and later: Unicode, Inc. and others. 2 // License & terms of use: http://www.unicode.org/copyright.html 3 /******************************************************************** 4 * COPYRIGHT: 5 * Copyright (c) 1997-2012, International Business Machines Corporation and 6 * others. All Rights Reserved. 7 * Copyright (C) 2010 , Yahoo! Inc. 8 ********************************************************************/ 9 10 #include <stdio.h> 11 #include <string.h> 12 #include "utypeinfo.h" // for 'typeid' to work 13 14 #include "uobjtest.h" 15 #include "cmemory.h" // UAlignedMemory 16 17 /** 18 * Test for UObject, currently only the classID. 19 * 20 * Usage 21 * TESTCLASSID_NONE_DEFAULT(Foo) 22 * -- Foo is expected to not support "poor man's RTTI". 23 * Beginning with ICU 4.6, we only use compiler RTTI in new class hierarchies. 24 * 25 * TESTCLASSID_NONE_CTOR(Foo, (1, 2, 3, status)) 26 * -- Combines TESTCLASSID_NONE_DEFAULT() and TESTCLASSID_CTOR(). 27 * 28 * TESTCLASSID_NONE_FACTORY(Foo, (1, 2, 3, status)) 29 * -- Combines TESTCLASSID_NONE_DEFAULT() and TESTCLASSID_FACTORY(). 30 * 31 * TESTCLASSID_ABSTRACT(Bar) 32 * -- Bar is expected to be abstract. Only the static ID will be tested. 33 * 34 * TESTCLASSID_DEFAULT(Foo) 35 * -- Foo will be default-constructed. 36 * 37 * TESTCLASSID_CTOR(Foo, (1, 2, 3, status)) 38 * -- Second argument is (parenthesized) constructor argument. 39 * Will be called as: new Foo ( 1, 2, 3, status) [status is tested] 40 * 41 * TESTCLASSID_FACTORY(Foo, fooCreateFunction(status) ) 42 * -- call fooCreateFunction. 'status' will be tested & reset 43 * 44 * TESTCLASSID_FACTORY_HIDDEN(class, factory) 45 * -- call factory. Class is not available from a header. 46 * 'status' will be tested & reset. This only tests uniqueness. 47 */ 48 49 #define TESTCLASSID_NONE_DEFAULT(c) \ 50 delete testClassNoClassID(new c, #c, "new " #c) 51 #define TESTCLASSID_NONE_CTOR(c, x) { \ 52 delete testClassNoClassID(new c x, #c, "new " #c #x); \ 53 if(U_FAILURE(status)) { \ 54 dataerrln(UnicodeString(#c " - new " #x " - got err status ") + UnicodeString(u_errorName(status))); \ 55 status = U_ZERO_ERROR; \ 56 } \ 57 } 58 #define TESTCLASSID_NONE_FACTORY(c, f) { \ 59 delete testClassNoClassID(f, #c, #f); \ 60 if(U_FAILURE(status)) { \ 61 dataerrln(UnicodeString(#c " - " #f " - got err status ") + UnicodeString(u_errorName(status))); \ 62 status = U_ZERO_ERROR; \ 63 } \ 64 } 65 #define TESTCLASSID_FACTORY(c, f) { \ 66 delete testClass(f, #c, #f, c ::getStaticClassID()); \ 67 if(U_FAILURE(status)) { \ 68 dataerrln(UnicodeString(#c " - " #f " - got err status ") + UnicodeString(u_errorName(status))); \ 69 status = U_ZERO_ERROR; \ 70 } \ 71 } 72 #define TESTCLASSID_TRANSLIT(c, t) { \ 73 delete testClass(Transliterator::createInstance(UnicodeString(t), UTRANS_FORWARD,parseError,status), #c, "Transliterator: " #t, c ::getStaticClassID()); \ 74 if(U_FAILURE(status)) { \ 75 dataerrln(UnicodeString(#c " - Transliterator: " #t " - got err status ") + UnicodeString(u_errorName(status))); \ 76 status = U_ZERO_ERROR; \ 77 } \ 78 } 79 #define TESTCLASSID_CTOR(c, x) { \ 80 delete testClass(new c x, #c, "new " #c #x, c ::getStaticClassID()); \ 81 if(U_FAILURE(status)) { \ 82 dataerrln(UnicodeString(#c " - new " #x " - got err status ") + UnicodeString(u_errorName(status))); \ 83 status = U_ZERO_ERROR; \ 84 } \ 85 } 86 #define TESTCLASSID_DEFAULT(c) \ 87 delete testClass(new c, #c, "new " #c , c::getStaticClassID()) 88 #define TESTCLASSID_ABSTRACT(c) \ 89 testClass(NULL, #c, NULL, c::getStaticClassID()) 90 #define TESTCLASSID_FACTORY_HIDDEN(c, f) { \ 91 UObject *objVar = f; \ 92 delete testClass(objVar, #c, #f, objVar!=NULL? objVar->getDynamicClassID(): NULL); \ 93 if(U_FAILURE(status)) { \ 94 dataerrln(UnicodeString(#c " - " #f " - got err status ") + UnicodeString(u_errorName(status))); \ 95 status = U_ZERO_ERROR; \ 96 } \ 97 } 98 99 #define MAX_CLASS_ID 200 100 101 static UClassID ids[MAX_CLASS_ID]; 102 static const char *ids_factory[MAX_CLASS_ID]; 103 static const char *ids_class[MAX_CLASS_ID]; 104 static uint32_t ids_count = 0; 105 106 UObject *UObjectTest::testClass(UObject *obj, 107 const char *className, const char *factory, 108 UClassID staticID) 109 { 110 uint32_t i; 111 UnicodeString what = UnicodeString(className) + " * x= " + UnicodeString(factory?factory:" ABSTRACT ") + "; "; 112 UClassID dynamicID = NULL; 113 114 if(ids_count >= MAX_CLASS_ID) { 115 char count[100]; 116 sprintf(count, " (currently %d) ", MAX_CLASS_ID); 117 errln("FAIL: Fatal: Ran out of IDs! Increase MAX_CLASS_ID." + UnicodeString(count) + what); 118 return obj; 119 } 120 121 if(obj) { 122 dynamicID = obj->getDynamicClassID(); 123 } 124 125 { 126 char tmp[500]; 127 sprintf(tmp, " [static=%p, dynamic=%p] ", staticID, dynamicID); 128 logln(what + tmp); 129 } 130 131 if(staticID == NULL) { 132 dataerrln("FAIL: staticID == NULL! " + what); 133 } 134 135 if(factory != NULL) { /* NULL factory means: abstract */ 136 if(!obj) { 137 dataerrln( "FAIL: ==NULL! " + what); 138 return obj; 139 } 140 141 if(dynamicID == NULL) { 142 errln("FAIL: dynamicID == NULL!" + what); 143 } 144 145 if(dynamicID != staticID) { 146 dataerrln("FAIL: dynamicID != staticID! " + what); 147 } 148 } 149 150 // Bail out if static ID is null. Error message was already printed. 151 if(staticID == NULL) { 152 return obj; 153 } 154 155 for(i=0;i<ids_count;i++) { 156 if(staticID == ids[i]) { 157 if(!strcmp(ids_class[i], className)) { 158 logln("OK: ID found is the same as " + UnicodeString(ids_class[i]) + UnicodeString(" *y= ") + ids_factory[i] + what); 159 return obj; 160 } else { 161 errln("FAIL: ID is the same as " + UnicodeString(ids_class[i]) + UnicodeString(" *y= ") + ids_factory[i] + what); 162 return obj; 163 } 164 } 165 } 166 167 ids[ids_count] = staticID; 168 ids_factory[ids_count] = factory; 169 ids_class[ids_count] = className; 170 ids_count++; 171 172 return obj; 173 } 174 175 UObject *UObjectTest::testClassNoClassID(UObject *obj, const char *className, const char *factory) 176 { 177 if (!obj) { 178 return NULL; 179 } 180 UnicodeString what = UnicodeString(className) + " * x= " + UnicodeString(factory?factory:" ABSTRACT ") + "; "; 181 UClassID dynamicID = obj->getDynamicClassID(); 182 183 { 184 char tmp[500]; 185 sprintf(tmp, " [dynamic=%p] ", dynamicID); 186 logln(what + tmp); 187 } 188 189 if(factory != NULL) { /* NULL factory means: abstract */ 190 if(!obj) { 191 dataerrln( "FAIL: ==NULL! " + what); 192 return obj; 193 } 194 195 if(dynamicID != NULL) { 196 errln("FAIL: dynamicID != NULL! for non-poor-man's-RTTI " + what); 197 } 198 } 199 200 return obj; 201 } 202 203 // begin actual #includes for things to be tested 204 // 205 // The following script will generate the #includes needed here: 206 // 207 // find common i18n -name '*.h' -print | xargs fgrep ClassID | cut -d: -f1 | cut -d\/ -f2- | sort | uniq | sed -e 's%.*%#include "&"%' 208 209 210 #include "unicode/utypes.h" 211 212 // Internal Things (woo) 213 #include "cpdtrans.h" 214 #include "rbt.h" 215 #include "rbt_data.h" 216 #include "nultrans.h" 217 #include "anytrans.h" 218 #include "digitlst.h" 219 #include "esctrn.h" 220 #include "funcrepl.h" 221 #include "servnotf.h" 222 #include "serv.h" 223 #include "servloc.h" 224 #include "name2uni.h" 225 #include "nfsubs.h" 226 #include "nortrans.h" 227 #include "quant.h" 228 #include "remtrans.h" 229 #include "strmatch.h" 230 #include "strrepl.h" 231 #include "titletrn.h" 232 #include "tolowtrn.h" 233 #include "toupptrn.h" 234 #include "unesctrn.h" 235 #include "uni2name.h" 236 #include "uvector.h" 237 #include "uvectr32.h" 238 #include "currfmt.h" 239 #include "buddhcal.h" 240 #include "islamcal.h" 241 #include "japancal.h" 242 #include "hebrwcal.h" 243 #include "persncal.h" 244 #include "taiwncal.h" 245 #include "indiancal.h" 246 #include "chnsecal.h" 247 #include "windtfmt.h" 248 #include "winnmfmt.h" 249 #include "ustrenum.h" 250 #include "olsontz.h" 251 #include "reldtfmt.h" 252 253 // External Things 254 #include "unicode/appendable.h" 255 #include "unicode/alphaindex.h" 256 #include "unicode/brkiter.h" 257 #include "unicode/calendar.h" 258 #include "unicode/caniter.h" 259 #include "unicode/chariter.h" 260 #include "unicode/choicfmt.h" 261 #include "unicode/coleitr.h" 262 #include "unicode/coll.h" 263 #include "unicode/curramt.h" 264 #include "unicode/datefmt.h" 265 #include "unicode/dcfmtsym.h" 266 #include "unicode/decimfmt.h" 267 #include "unicode/dtfmtsym.h" 268 #include "unicode/dtptngen.h" 269 #include "unicode/fieldpos.h" 270 #include "unicode/fmtable.h" 271 #include "unicode/format.h" 272 #include "unicode/gregocal.h" 273 #include "unicode/idna.h" 274 #include "unicode/locdspnm.h" 275 #include "unicode/locid.h" 276 #include "unicode/msgfmt.h" 277 #include "unicode/normlzr.h" 278 #include "unicode/normalizer2.h" 279 #include "unicode/numfmt.h" 280 #include "unicode/parsepos.h" 281 #include "unicode/plurrule.h" 282 #include "unicode/plurfmt.h" 283 #include "unicode/selfmt.h" 284 #include "unicode/rbbi.h" 285 #include "unicode/rbnf.h" 286 #include "unicode/regex.h" 287 #include "unicode/resbund.h" 288 #include "unicode/schriter.h" 289 #include "unicode/simpletz.h" 290 #include "unicode/smpdtfmt.h" 291 #include "unicode/sortkey.h" 292 #include "unicode/stsearch.h" 293 #include "unicode/tblcoll.h" 294 #include "unicode/timezone.h" 295 #include "unicode/translit.h" 296 #include "unicode/uchriter.h" 297 #include "unicode/uloc.h" 298 #include "unicode/unifilt.h" 299 #include "unicode/unifunct.h" 300 #include "unicode/uniset.h" 301 #include "unicode/unistr.h" 302 #include "unicode/uobject.h" 303 #include "unicode/usetiter.h" 304 //#include "unicode/bidi.h" 305 //#include "unicode/convert.h" 306 307 // END includes ============================================================= 308 309 #define UOBJTEST_TEST_INTERNALS 0 /* do NOT test Internal things - their functions aren't exported on Win32 */ 310 311 #if !UCONFIG_NO_SERVICE 312 /* The whole purpose of this class is to expose the constructor, and gain access to the superclasses RTTI. */ 313 class TestLocaleKeyFactory : public LocaleKeyFactory { 314 public: 315 TestLocaleKeyFactory(int32_t coverage) : LocaleKeyFactory(coverage) {} 316 }; 317 #endif 318 319 // Appendable is abstract; we define a subclass to verify that there is no "poor man's RTTI". 320 class DummyAppendable : public Appendable { 321 public: 322 virtual UBool appendCodeUnit(UChar /*c*/) { return TRUE; } 323 }; 324 325 void UObjectTest::testIDs() 326 { 327 ids_count = 0; 328 UErrorCode status = U_ZERO_ERROR; 329 330 TESTCLASSID_NONE_CTOR(UObject, ()); 331 332 #if !UCONFIG_NO_TRANSLITERATION || !UCONFIG_NO_FORMATTING 333 UParseError parseError; 334 #endif 335 336 337 #if !UCONFIG_NO_NORMALIZATION 338 UnicodeString emptyString; 339 TESTCLASSID_CTOR(Normalizer, (emptyString, UNORM_NONE)); 340 const Normalizer2 *noNormalizer2 = NULL; 341 UnicodeSet emptySet; 342 TESTCLASSID_NONE_CTOR(FilteredNormalizer2, (*noNormalizer2, emptySet)); 343 TESTCLASSID_FACTORY(CanonicalIterator, new CanonicalIterator(UnicodeString("abc"), status)); 344 #endif 345 #if !UCONFIG_NO_IDNA 346 TESTCLASSID_NONE_FACTORY(IDNA, IDNA::createUTS46Instance(0, status)); 347 #endif 348 //TESTCLASSID_DEFAULT(CollationElementIterator); 349 #if !UCONFIG_NO_COLLATION 350 TESTCLASSID_DEFAULT(CollationKey); 351 TESTCLASSID_FACTORY(UStringEnumeration, Collator::getKeywords(status)); 352 //TESTCLASSID_FACTORY_HIDDEN(CollationLocaleListEnumeration, Collator::getAvailableLocales()); 353 #endif 354 //TESTCLASSID_FACTORY(CompoundTransliterator, Transliterator::createInstance(UnicodeString("Any-Jex;Hangul-Jamo"), UTRANS_FORWARD, parseError, status)); 355 356 #if !UCONFIG_NO_FORMATTING 357 /* TESTCLASSID_FACTORY(NFSubstitution, NFSubstitution::makeSubstitution(8, */ 358 /* TESTCLASSID_DEFAULT(DigitList); UMemory but not UObject*/ 359 TESTCLASSID_ABSTRACT(NumberFormat); 360 TESTCLASSID_CTOR(RuleBasedNumberFormat, (UnicodeString("%default: -x: minus >>;"), parseError, status)); 361 TESTCLASSID_CTOR(ChoiceFormat, (UNICODE_STRING_SIMPLE("0#are no files|1#is one file|1<are many files"), status)); 362 TESTCLASSID_CTOR(MessageFormat, (UnicodeString(), status)); 363 TESTCLASSID_CTOR(DateFormatSymbols, (status)); 364 TESTCLASSID_CTOR(PluralFormat, (status)); 365 TESTCLASSID_CTOR(PluralRules, (status)); 366 TESTCLASSID_CTOR(SelectFormat, (UnicodeString("feminine {feminineVerbValue} other{otherVerbValue}"), status) ); 367 TESTCLASSID_FACTORY(DateTimePatternGenerator, DateTimePatternGenerator::createInstance(status)); 368 TESTCLASSID_FACTORY(RelativeDateFormat, DateFormat::createDateInstance(DateFormat::kFullRelative, Locale::getUS())); 369 TESTCLASSID_CTOR(DecimalFormatSymbols, (status)); 370 TESTCLASSID_DEFAULT(FieldPosition); 371 TESTCLASSID_DEFAULT(Formattable); 372 373 static const UChar SMALL_STR[] = {0x51, 0x51, 0x51, 0}; // "QQQ" 374 TESTCLASSID_CTOR(CurrencyAmount, (1.0, SMALL_STR, status)); 375 TESTCLASSID_CTOR(CurrencyUnit, (SMALL_STR, status)); 376 TESTCLASSID_NONE_FACTORY(LocaleDisplayNames, LocaleDisplayNames::createInstance("de")); 377 TESTCLASSID_FACTORY_HIDDEN(CurrencyFormat, MeasureFormat::createCurrencyFormat(Locale::getUS(), status)); 378 TESTCLASSID_FACTORY(GregorianCalendar, Calendar::createInstance(Locale("@calendar=gregorian"), status)); 379 TESTCLASSID_FACTORY(BuddhistCalendar, Calendar::createInstance(Locale("@calendar=buddhist"), status)); 380 TESTCLASSID_FACTORY(IslamicCalendar, Calendar::createInstance(Locale("@calendar=islamic"), status)); 381 TESTCLASSID_FACTORY(JapaneseCalendar, Calendar::createInstance(Locale("@calendar=japanese"), status)); 382 TESTCLASSID_FACTORY(HebrewCalendar, Calendar::createInstance(Locale("@calendar=hebrew"), status)); 383 TESTCLASSID_FACTORY(PersianCalendar, Calendar::createInstance(Locale("@calendar=persian"), status)); 384 TESTCLASSID_FACTORY(IndianCalendar, Calendar::createInstance(Locale("@calendar=indian"), status)); 385 TESTCLASSID_FACTORY(ChineseCalendar, Calendar::createInstance(Locale("@calendar=chinese"), status)); 386 TESTCLASSID_FACTORY(TaiwanCalendar, Calendar::createInstance(Locale("@calendar=roc"), status)); 387 #if U_PLATFORM_HAS_WIN32_API 388 TESTCLASSID_FACTORY(Win32DateFormat, DateFormat::createDateInstance(DateFormat::kFull, Locale("@compat=host"))); 389 #if U_PLATFORM_USES_ONLY_WIN32_API 390 TESTCLASSID_FACTORY(Win32NumberFormat, NumberFormat::createInstance(Locale("@compat=host"), status)); 391 #endif 392 #endif 393 #endif 394 395 #if !UCONFIG_NO_BREAK_ITERATION && !UCONFIG_NO_FILE_IO 396 /* TESTCLASSID_ABSTRACT(BreakIterator); No staticID! */ 397 TESTCLASSID_FACTORY(RuleBasedBreakIterator, BreakIterator::createLineInstance("mt",status)); 398 //TESTCLASSID_FACTORY(DictionaryBasedBreakIterator, BreakIterator::createLineInstance("th",status)); 399 400 #if !UCONFIG_NO_SERVICE 401 TESTCLASSID_FACTORY_HIDDEN(ICULocaleService, BreakIterator::getAvailableLocales()); 402 #endif 403 #endif 404 405 //TESTCLASSID_DEFAULT(GregorianCalendar); 406 407 #if !UCONFIG_NO_TRANSLITERATION 408 TESTCLASSID_TRANSLIT(AnyTransliterator, "Any-Latin"); 409 TESTCLASSID_TRANSLIT(CompoundTransliterator, "Latin-Greek"); 410 TESTCLASSID_TRANSLIT(EscapeTransliterator, "Any-Hex"); 411 TESTCLASSID_TRANSLIT(LowercaseTransliterator, "Lower"); 412 TESTCLASSID_TRANSLIT(NameUnicodeTransliterator, "Name-Any"); 413 TESTCLASSID_TRANSLIT(NormalizationTransliterator, "NFD"); 414 TESTCLASSID_TRANSLIT(NullTransliterator, "Null"); 415 TESTCLASSID_TRANSLIT(RemoveTransliterator, "Remove"); 416 TESTCLASSID_FACTORY(RuleBasedTransliterator, Transliterator::createFromRules(UnicodeString("abcd"),UnicodeString("a>b;"),UTRANS_FORWARD,parseError,status)); 417 TESTCLASSID_TRANSLIT(TitlecaseTransliterator, "Title"); 418 TESTCLASSID_TRANSLIT(UnescapeTransliterator, "Hex-Any"); 419 TESTCLASSID_TRANSLIT(UnicodeNameTransliterator, "Any-Name"); 420 TESTCLASSID_TRANSLIT(UppercaseTransliterator, "Upper"); 421 TESTCLASSID_ABSTRACT(CaseMapTransliterator); 422 TESTCLASSID_ABSTRACT(Transliterator); 423 TESTCLASSID_FACTORY_HIDDEN(TransliteratorRegistry::Enumeration, Transliterator::getAvailableIDs(status)); 424 425 #if UOBJTEST_TEST_INTERNALS 426 TESTCLASSID_CTOR(Quantifier, (NULL, 0, 0)); 427 TESTCLASSID_CTOR(FunctionReplacer, (NULL,NULL)); 428 TESTCLASSID_CTOR(StringMatcher, (UnicodeString("x"), 0,0,0,TransliterationRuleData(status))); 429 TESTCLASSID_CTOR(StringReplacer,(UnicodeString(),new TransliterationRuleData(status))); 430 #endif 431 #endif 432 433 TESTCLASSID_FACTORY(Locale, new Locale("123")); 434 TESTCLASSID_FACTORY_HIDDEN(KeywordEnumeration, Locale("@a=b").createKeywords(status)); 435 436 //TESTCLASSID_DEFAULT(NumeratorSubstitution); 437 438 #if !UCONFIG_NO_TRANSLITERATION 439 TESTCLASSID_DEFAULT(ParsePosition); 440 #endif 441 442 443 // NO_REG_EX 444 //TESTCLASSID_DEFAULT(RegexCompile); 445 //TESTCLASSID_DEFAULT(RegexMatcher); 446 //TESTCLASSID_DEFAULT(RegexPattern); 447 448 //TESTCLASSID_DEFAULT(ReplaceableGlue); 449 TESTCLASSID_FACTORY(ResourceBundle, new ResourceBundle(UnicodeString(), status) ); 450 //TESTCLASSID_DEFAULT(RuleBasedTransliterator); 451 452 //TESTCLASSID_DEFAULT(SimpleFwdCharIterator); 453 //TESTCLASSID_DEFAULT(StringReplacer); 454 //TESTCLASSID_DEFAULT(StringSearch); 455 456 //TESTCLASSID_DEFAULT(TestMultipleKeyStringFactory); 457 //TESTCLASSID_DEFAULT(TestReplaceable); 458 459 #if !UCONFIG_NO_FORMATTING 460 TESTCLASSID_ABSTRACT(TimeZone); 461 TESTCLASSID_FACTORY(OlsonTimeZone, TimeZone::createTimeZone(UnicodeString("America/Los_Angeles"))); 462 TESTCLASSID_FACTORY_HIDDEN(KeywordEnumeration, TimeZone::createEnumeration()); 463 #endif 464 465 TESTCLASSID_NONE_DEFAULT(DummyAppendable); 466 TESTCLASSID_DEFAULT(UnicodeString); 467 TESTCLASSID_CTOR(UnicodeSet, (0, 1)); 468 TESTCLASSID_ABSTRACT(UnicodeFilter); 469 TESTCLASSID_ABSTRACT(UnicodeFunctor); 470 TESTCLASSID_CTOR(UnicodeSetIterator,(UnicodeSet(0,1))); 471 TESTCLASSID_CTOR(UStack, (status)); 472 TESTCLASSID_CTOR(UVector, (status)); 473 TESTCLASSID_CTOR(UVector32, (status)); 474 475 #if !UCONFIG_NO_SERVICE 476 TESTCLASSID_CTOR(SimpleFactory, (NULL, UnicodeString("foo"))); 477 TESTCLASSID_DEFAULT(EventListener); 478 TESTCLASSID_DEFAULT(ICUResourceBundleFactory); 479 //TESTCLASSID_DEFAULT(Key); // does not exist? 480 UnicodeString baz("baz"); 481 UnicodeString bat("bat"); 482 TESTCLASSID_FACTORY(LocaleKey, LocaleKey::createWithCanonicalFallback(&baz, &bat, LocaleKey::KIND_ANY, status)); 483 TESTCLASSID_CTOR(SimpleLocaleKeyFactory, (NULL, UnicodeString("bar"), 8, 12) ); 484 TESTCLASSID_CTOR(TestLocaleKeyFactory, (42)); // Test replacement for LocaleKeyFactory 485 //#if UOBJTEST_TEST_INTERNALS 486 // TESTCLASSID_CTOR(LocaleKeyFactory, (42)); 487 //#endif 488 #endif 489 490 #if !UCONFIG_NO_COLLATION && !UCONFIG_NO_NORMALIZATION 491 TESTCLASSID_NONE_CTOR(AlphabeticIndex, (Locale::getEnglish(), status)); 492 #endif 493 494 #if UOBJTEST_DUMP_IDS 495 int i; 496 for(i=0;i<ids_count;i++) { 497 char junk[800]; 498 sprintf(junk, " %4d:\t%p\t%s\t%s\n", 499 i, ids[i], ids_class[i], ids_factory[i]); 500 logln(UnicodeString(junk)); 501 } 502 #endif 503 } 504 505 void UObjectTest::testUMemory() { 506 // additional tests for code coverage 507 #if U_OVERRIDE_CXX_ALLOCATION && U_HAVE_PLACEMENT_NEW 508 union { 509 UAlignedMemory align_; 510 char bytes_[sizeof(UnicodeString)]; 511 } stackMemory; 512 char *bytes = stackMemory.bytes_; 513 UnicodeString *p; 514 enum { len=20 }; 515 516 p=new(bytes) UnicodeString(len, (UChar32)0x20ac, len); 517 if((void *)p!=(void *)bytes) { 518 errln("placement new did not place the object at the expected address"); 519 } 520 if(p->length()!=len || p->charAt(0)!=0x20ac || p->charAt(len-1)!=0x20ac) { 521 errln("constructor used with placement new did not work right"); 522 } 523 524 /* 525 * It is not possible to simply say 526 * delete(p, stackMemory); 527 * which results in a call to the normal, non-placement delete operator. 528 * 529 * Via a search on google.com for "c++ placement delete" I found 530 * http://cpptips.hyperformix.com/cpptips/placement_del3 531 * which says: 532 * 533 * TITLE: using placement delete 534 * 535 * (Newsgroups: comp.std.c++, 27 Aug 97) 536 * 537 * ISJ: isj (at) image.dk 538 * 539 * > I do not completely understand how placement works on operator delete. 540 * > ... 541 * There is no delete-expression which will invoke a placement 542 * form of operator delete. You can still call the function 543 * explicitly. Example: 544 * ... 545 * // destroy object and delete space manually 546 * p->~T(); 547 * operator delete(p, 12); 548 * 549 * ... so that's what I am doing here. 550 * markus 20031216 551 */ 552 // destroy object and delete space manually 553 p->~UnicodeString(); 554 555 // You normally wouldn't call an operator delete for object placed on the 556 // stack with a placement new(). 557 // This overload of delete is a nop, and is called here for code coverage purposes. 558 UnicodeString::operator delete(p, bytes); 559 560 // Jitterbug 4452, for coverage 561 UnicodeString *pa = new UnicodeString[2]; 562 if ( !pa[0].isEmpty() || !pa[1].isEmpty()){ 563 errln("constructor used with array new did not work right"); 564 } 565 delete [] pa; 566 #endif 567 568 // try to call the compiler-generated UMemory::operator=(class UMemory const &) 569 UMemory m, n; 570 m=n; 571 } 572 573 void UObjectTest::TestMFCCompatibility() { 574 #if U_HAVE_DEBUG_LOCATION_NEW 575 /* Make sure that it compiles with MFC's debuggable new usage. */ 576 UnicodeString *str = new(__FILE__, __LINE__) UnicodeString(); 577 str->append((UChar)0x0040); // Is it usable? 578 if(str->charAt(0) != 0x0040) { 579 errln("debug new doesn't work."); 580 } 581 UnicodeString::operator delete(str, __FILE__, __LINE__); 582 #endif 583 } 584 585 void UObjectTest::TestCompilerRTTI() { 586 #if !UCONFIG_NO_FORMATTING 587 UErrorCode errorCode = U_ZERO_ERROR; 588 NumberFormat *nf = NumberFormat::createInstance("de", errorCode); 589 if (U_FAILURE(errorCode)) { 590 dataerrln("NumberFormat::createInstance(de) failed - %s", u_errorName(errorCode)); 591 return; 592 } 593 if (dynamic_cast<DecimalFormat *>(nf) == NULL || dynamic_cast<ChoiceFormat *>(nf) != NULL) { 594 errln("dynamic_cast<>(NumberFormat) failed"); 595 } 596 UnicodeSet emptySet; 597 if (&typeid(*nf) == NULL || typeid(*nf) == typeid(UObject) || typeid(*nf) == typeid(Format) || 598 typeid(*nf) != typeid(DecimalFormat) || typeid(*nf) == typeid(ChoiceFormat) || 599 typeid(*nf) == typeid(emptySet) 600 ) { 601 errln("typeid(NumberFormat) failed"); 602 } 603 delete nf; 604 #endif 605 } 606 607 /* --------------- */ 608 609 void UObjectTest::runIndexedTest( int32_t index, UBool exec, const char* &name, char* /* par */ ) 610 { 611 switch (index) { 612 613 TESTCASE(0, testIDs); 614 TESTCASE(1, testUMemory); 615 TESTCASE(2, TestMFCCompatibility); 616 TESTCASE(3, TestCompilerRTTI); 617 618 default: name = ""; break; //needed to end loop 619 } 620 } 621