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