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