]> git.saurik.com Git - apple/icu.git/blame_incremental - icuSources/test/intltest/uobjtest.cpp
ICU-8.11.4.tar.gz
[apple/icu.git] / icuSources / test / intltest / uobjtest.cpp
... / ...
CommitLineData
1/********************************************************************
2 * COPYRIGHT:
3 * Copyright (c) 2002-2006, International Business Machines Corporation and
4 * others. All Rights Reserved.
5 ********************************************************************/
6
7#include "uobjtest.h"
8#include "cmemory.h" // UAlignedMemory
9#include <string.h>
10#include <stdio.h>
11
12/**
13 *
14 * Test for UObject, currently only the classID.
15 *
16 * Usage
17 * TESTCLASSID_ABSTRACT(Bar)
18 * -- Bar is expected to be abstract. Only the static ID will be tested.
19 *
20 * TESTCLASSID_DEFAULT(Foo)
21 * -- Foo will be default-constructed.
22 *
23 * TESTCLASSID_CTOR(Foo, (1, 2, 3, status))
24 * -- Second argument is (parenthesized) constructor argument.
25 * Will be called as: new Foo ( 1, 2, 3, status) [status is tested]
26 *
27 * TESTCLASSID_FACTORY(Foo, fooCreateFunction(status) )
28 * -- call fooCreateFunction. 'status' will be tested & reset
29 */
30
31
32#define TESTCLASSID_FACTORY(c, f) { delete testClass(f, #c, #f, c ::getStaticClassID()); if(U_FAILURE(status)) { errln(UnicodeString(#c " - " #f " - got err status ") + UnicodeString(u_errorName(status))); status = U_ZERO_ERROR; } }
33#define TESTCLASSID_TRANSLIT(c, t) { delete testClass(Transliterator::createInstance(UnicodeString(t), UTRANS_FORWARD,parseError,status), #c, "Transliterator: " #t, c ::getStaticClassID()); if(U_FAILURE(status)) { errln(UnicodeString(#c " - Transliterator: " #t " - got err status ") + UnicodeString(u_errorName(status))); status = U_ZERO_ERROR; } }
34#define TESTCLASSID_CTOR(c, x) { delete testClass(new c x, #c, "new " #c #x, c ::getStaticClassID()); if(U_FAILURE(status)) { errln(UnicodeString(#c " - new " #x " - got err status ") + UnicodeString(u_errorName(status))); status = U_ZERO_ERROR; } }
35#define TESTCLASSID_DEFAULT(c) delete testClass(new c, #c, "new " #c , c::getStaticClassID())
36#define TESTCLASSID_ABSTRACT(c) testClass(NULL, #c, NULL, c::getStaticClassID())
37
38#define MAX_CLASS_ID 200
39
40UClassID ids[MAX_CLASS_ID];
41const char *ids_factory[MAX_CLASS_ID];
42const char *ids_class[MAX_CLASS_ID];
43uint32_t ids_count = 0;
44
45UObject *UObjectTest::testClass(UObject *obj,
46 const char *className, const char *factory,
47 UClassID staticID)
48{
49 uint32_t i;
50 UnicodeString what = UnicodeString(className) + " * x= " + UnicodeString(factory?factory:" ABSTRACT ") + "; ";
51 UClassID dynamicID = NULL;
52
53 if(ids_count >= MAX_CLASS_ID) {
54 char count[100];
55 sprintf(count, " (currently %d) ", MAX_CLASS_ID);
56 errln("FAIL: Fatal: Ran out of IDs! Increase MAX_CLASS_ID." + UnicodeString(count) + what);
57 return obj;
58 }
59
60 if(obj) {
61 dynamicID = obj->getDynamicClassID();
62 }
63
64 {
65 char tmp[500];
66 sprintf(tmp, " [static=%p, dynamic=%p] ", staticID, dynamicID);
67 logln(what + tmp);
68 }
69
70 if(staticID == NULL) {
71 errln( "FAIL: staticID == NULL!" + what);
72 }
73
74 if(factory != NULL) { /* NULL factory means: abstract */
75 if(!obj) {
76 errln( "FAIL: ==NULL!" + what);
77 return obj;
78 }
79
80 if(dynamicID == NULL) {
81 errln("FAIL: dynamicID == NULL!" + what);
82 }
83
84 if(dynamicID != staticID) {
85 errln("FAIL: dynamicID != staticID!" + what );
86 }
87 }
88
89 // Bail out if static ID is null
90 if(staticID == NULL) {
91 return obj;
92 }
93
94 for(i=0;i<ids_count;i++) {
95 if(staticID == ids[i]) {
96 if(!strcmp(ids_class[i], className)) {
97 logln("OK: ID found is the same as " + UnicodeString(ids_class[i]) + UnicodeString(" *y= ") + ids_factory[i] + what);
98 return obj;
99 } else {
100 errln("FAIL: ID is the same as " + UnicodeString(ids_class[i]) + UnicodeString(" *y= ") + ids_factory[i] + what);
101 return obj;
102 }
103 }
104 }
105
106 ids[ids_count] = staticID;
107 ids_factory[ids_count] = factory;
108 ids_class[ids_count] = className;
109 ids_count++;
110
111 return obj;
112}
113
114
115// begin actual #includes for things to be tested
116//
117// The following script will generate the #includes needed here:
118//
119// find common i18n -name '*.h' -print | xargs fgrep ClassID | cut -d: -f1 | cut -d\/ -f2- | sort | uniq | sed -e 's%.*%#include "&"%'
120
121
122#include "unicode/utypes.h"
123
124// Internal Things (woo)
125#include "cpdtrans.h"
126#include "rbt.h"
127#include "rbt_data.h"
128#include "nultrans.h"
129#include "anytrans.h"
130#include "digitlst.h"
131#include "esctrn.h"
132#include "funcrepl.h"
133#include "servnotf.h"
134#include "serv.h"
135#include "servloc.h"
136#include "name2uni.h"
137#include "nfsubs.h"
138#include "nortrans.h"
139#include "quant.h"
140#include "remtrans.h"
141#include "strmatch.h"
142#include "strrepl.h"
143#include "titletrn.h"
144#include "tolowtrn.h"
145#include "toupptrn.h"
146#include "unesctrn.h"
147#include "uni2name.h"
148#include "uvector.h"
149#include "uvectr32.h"
150#include "currfmt.h"
151#include "buddhcal.h"
152#include "islamcal.h"
153#include "japancal.h"
154#include "hebrwcal.h"
155#include "ustrenum.h"
156
157// External Things
158#include "unicode/brkiter.h"
159#include "unicode/calendar.h"
160#include "unicode/caniter.h"
161#include "unicode/chariter.h"
162#include "unicode/choicfmt.h"
163#include "unicode/coleitr.h"
164#include "unicode/coll.h"
165#include "unicode/curramt.h"
166#include "unicode/datefmt.h"
167#include "unicode/dbbi.h"
168#include "unicode/dcfmtsym.h"
169#include "unicode/decimfmt.h"
170#include "unicode/dtfmtsym.h"
171#include "unicode/fieldpos.h"
172#include "unicode/fmtable.h"
173#include "unicode/format.h"
174#include "unicode/gregocal.h"
175#include "unicode/locid.h"
176#include "unicode/msgfmt.h"
177#include "unicode/normlzr.h"
178#include "unicode/numfmt.h"
179#include "unicode/parsepos.h"
180#include "unicode/rbbi.h"
181#include "unicode/rbnf.h"
182#include "unicode/regex.h"
183#include "unicode/resbund.h"
184#include "unicode/schriter.h"
185#include "unicode/simpletz.h"
186#include "unicode/smpdtfmt.h"
187#include "unicode/sortkey.h"
188#include "unicode/stsearch.h"
189#include "unicode/tblcoll.h"
190#include "unicode/timezone.h"
191#include "unicode/translit.h"
192#include "unicode/uchriter.h"
193#include "unicode/unifilt.h"
194#include "unicode/unifunct.h"
195#include "unicode/uniset.h"
196#include "unicode/unistr.h"
197#include "unicode/uobject.h"
198#include "unicode/usetiter.h"
199//#include "unicode/bidi.h"
200//#include "unicode/convert.h"
201
202// END includes =============================================================
203
204#define UOBJTEST_TEST_INTERNALS 0 /* do NOT test Internal things - their functions aren't exported on Win32 */
205
206#if !UCONFIG_NO_SERVICE
207/* The whole purpose of this class is to expose the constructor, and gain access to the superclasses RTTI. */
208class TestLocaleKeyFactory : public LocaleKeyFactory {
209public:
210 TestLocaleKeyFactory(int32_t coverage) : LocaleKeyFactory(coverage) {}
211};
212#endif
213
214void UObjectTest::testIDs()
215{
216 ids_count = 0;
217 UErrorCode status = U_ZERO_ERROR;
218 static const UChar SMALL_STR[] = {0x51, 0x51, 0x51, 0}; // "QQQ"
219
220#if !UCONFIG_NO_TRANSLITERATION || !UCONFIG_NO_FORMATTING
221 UParseError parseError;
222#endif
223
224
225
226 //TESTCLASSID_DEFAULT(AbbreviatedUnicodeSetIterator);
227 //TESTCLASSID_DEFAULT(AnonymousStringFactory);
228
229
230#if !UCONFIG_NO_NORMALIZATION
231 TESTCLASSID_FACTORY(CanonicalIterator, new CanonicalIterator(UnicodeString("abc"), status));
232#endif
233 //TESTCLASSID_DEFAULT(CollationElementIterator);
234#if !UCONFIG_NO_COLLATION
235 TESTCLASSID_DEFAULT(CollationKey);
236 TESTCLASSID_FACTORY(UStringEnumeration, Collator::getKeywords(status));
237#endif
238 //TESTCLASSID_FACTORY(CompoundTransliterator, Transliterator::createInstance(UnicodeString("Any-Jex;Hangul-Jamo"), UTRANS_FORWARD, parseError, status));
239
240#if !UCONFIG_NO_FORMATTING
241 /* TESTCLASSID_FACTORY(NFSubstitution, NFSubstitution::makeSubstitution(8, */
242 /* TESTCLASSID_DEFAULT(DigitList); UMemory but not UObject*/
243 TESTCLASSID_ABSTRACT(NumberFormat);
244 TESTCLASSID_CTOR(RuleBasedNumberFormat, (UnicodeString("%default: -x: minus >>;"), parseError, status));
245 TESTCLASSID_CTOR(ChoiceFormat, (UNICODE_STRING_SIMPLE("0#are no files|1#is one file|1<are many files"), status));
246 TESTCLASSID_CTOR(MessageFormat, (UnicodeString(), status));
247 TESTCLASSID_CTOR(DateFormatSymbols, (status));
248 TESTCLASSID_CTOR(DecimalFormatSymbols, (status));
249 TESTCLASSID_DEFAULT(FieldPosition);
250 TESTCLASSID_DEFAULT(Formattable);
251 TESTCLASSID_CTOR(CurrencyAmount, (1.0, SMALL_STR, status));
252 TESTCLASSID_CTOR(CurrencyUnit, (SMALL_STR, status));
253 TESTCLASSID_CTOR(CurrencyFormat, (Locale::getUS(), status));
254 TESTCLASSID_CTOR(GregorianCalendar, (status));
255 TESTCLASSID_CTOR(BuddhistCalendar, (Locale::getUS(), status));
256 TESTCLASSID_CTOR(IslamicCalendar, (Locale::getUS(), status));
257 TESTCLASSID_CTOR(JapaneseCalendar, (Locale::getUS(), status));
258 TESTCLASSID_CTOR(HebrewCalendar, (Locale::getUS(), status));
259#endif
260
261#if !UCONFIG_NO_BREAK_ITERATION
262 /* TESTCLASSID_ABSTRACT(BreakIterator); No staticID! */
263 TESTCLASSID_FACTORY(RuleBasedBreakIterator, BreakIterator::createLineInstance("mt",status));
264 //TESTCLASSID_FACTORY(DictionaryBasedBreakIterator, BreakIterator::createLineInstance("th",status));
265#endif
266
267 //TESTCLASSID_DEFAULT(EscapeTransliterator);
268
269 //TESTCLASSID_DEFAULT(GregorianCalendar);
270
271#if !UCONFIG_NO_TRANSLITERATION
272
273 TESTCLASSID_TRANSLIT(AnyTransliterator, "Any-Latin");
274 TESTCLASSID_TRANSLIT(CompoundTransliterator, "Latin-Greek");
275 TESTCLASSID_TRANSLIT(EscapeTransliterator, "Any-Hex");
276 TESTCLASSID_TRANSLIT(LowercaseTransliterator, "Lower");
277 TESTCLASSID_TRANSLIT(NameUnicodeTransliterator, "Name-Any");
278 TESTCLASSID_TRANSLIT(NormalizationTransliterator, "NFD");
279 TESTCLASSID_TRANSLIT(NullTransliterator, "Null");
280 TESTCLASSID_TRANSLIT(RemoveTransliterator, "Remove");
281 TESTCLASSID_CTOR(RuleBasedTransliterator, (UnicodeString("abcd"), UnicodeString("a>b;"), status));
282 TESTCLASSID_TRANSLIT(TitlecaseTransliterator, "Title");
283 TESTCLASSID_TRANSLIT(UnescapeTransliterator, "Hex-Any");
284 TESTCLASSID_TRANSLIT(UnicodeNameTransliterator, "Any-Name");
285 TESTCLASSID_TRANSLIT(UppercaseTransliterator, "Upper");
286 TESTCLASSID_CTOR(CaseMapTransliterator, (UnicodeString(), NULL));
287 TESTCLASSID_CTOR(Quantifier, (NULL, 0, 0));
288#if UOBJTEST_TEST_INTERNALS
289 TESTCLASSID_CTOR(FunctionReplacer, (NULL,NULL) ); /* don't care */
290#endif
291#endif
292
293 TESTCLASSID_FACTORY(Locale, new Locale("123"));
294
295 //TESTCLASSID_DEFAULT(Normalizer);
296
297 //TESTCLASSID_DEFAULT(NumeratorSubstitution);
298
299#if !UCONFIG_NO_TRANSLITERATION
300 TESTCLASSID_DEFAULT(ParsePosition);
301 //TESTCLASSID_DEFAULT(Quantifier);
302#endif
303
304
305// NO_REG_EX
306 //TESTCLASSID_DEFAULT(RegexCompile);
307 //TESTCLASSID_DEFAULT(RegexMatcher);
308 //TESTCLASSID_DEFAULT(RegexPattern);
309
310 //TESTCLASSID_DEFAULT(ReplaceableGlue);
311 TESTCLASSID_FACTORY(ResourceBundle, new ResourceBundle(UnicodeString(), status) );
312 //TESTCLASSID_DEFAULT(RuleBasedTransliterator);
313
314 //TESTCLASSID_DEFAULT(SimpleFwdCharIterator);
315 //TESTCLASSID_DEFAULT(StringReplacer);
316 //TESTCLASSID_DEFAULT(StringSearch);
317
318 //TESTCLASSID_DEFAULT(TempSearch);
319 //TESTCLASSID_DEFAULT(TestMultipleKeyStringFactory);
320 //TESTCLASSID_DEFAULT(TestReplaceable);
321
322#if !UCONFIG_NO_FORMATTING
323 TESTCLASSID_ABSTRACT(TimeZone);
324#endif
325
326#if !UCONFIG_NO_TRANSLITERATION
327 TESTCLASSID_FACTORY(TitlecaseTransliterator, Transliterator::createInstance(UnicodeString("Any-Title"), UTRANS_FORWARD, parseError, status));
328 TESTCLASSID_ABSTRACT(Transliterator);
329
330#if UOBJTEST_TEST_INTERNALS
331 TESTCLASSID_CTOR(StringMatcher, (UnicodeString("x"), 0,0,0,TransliterationRuleData(status)));
332 TESTCLASSID_CTOR(StringReplacer,(UnicodeString(),new TransliterationRuleData(status)));
333#endif
334#endif
335
336 TESTCLASSID_DEFAULT(UnicodeString);
337 TESTCLASSID_CTOR(UnicodeSet, (0, 1));
338 TESTCLASSID_ABSTRACT(UnicodeFilter);
339 TESTCLASSID_ABSTRACT(UnicodeFunctor);
340 TESTCLASSID_CTOR(UnicodeSetIterator,(UnicodeSet(0,1)));
341 TESTCLASSID_CTOR(UStack, (status));
342 TESTCLASSID_CTOR(UVector, (status));
343 TESTCLASSID_CTOR(UVector32, (status));
344
345#if !UCONFIG_NO_SERVICE
346 TESTCLASSID_CTOR(SimpleFactory, (NULL, UnicodeString("foo")));
347 TESTCLASSID_DEFAULT(EventListener);
348 TESTCLASSID_DEFAULT(ICUResourceBundleFactory);
349 //TESTCLASSID_DEFAULT(Key); // does not exist?
350 UnicodeString baz("baz");
351 UnicodeString bat("bat");
352 TESTCLASSID_FACTORY(LocaleKey, LocaleKey::createWithCanonicalFallback(&baz, &bat, LocaleKey::KIND_ANY, status));
353 TESTCLASSID_CTOR(SimpleLocaleKeyFactory, (NULL, UnicodeString("bar"), 8, 12) );
354 TESTCLASSID_CTOR(TestLocaleKeyFactory, (42)); // Test replacement for LocaleKeyFactory
355//#if UOBJTEST_TEST_INTERNALS
356// TESTCLASSID_CTOR(LocaleKeyFactory, (42));
357//#endif
358#endif
359
360#if UOBJTEST_DUMP_IDS
361 int i;
362 for(i=0;i<ids_count;i++) {
363 char junk[800];
364 sprintf(junk, " %4d:\t%p\t%s\t%s\n",
365 i, ids[i], ids_class[i], ids_factory[i]);
366 logln(UnicodeString(junk));
367 }
368#endif
369}
370
371void UObjectTest::testUMemory() {
372 // additional tests for code coverage
373#if U_OVERRIDE_CXX_ALLOCATION && U_HAVE_PLACEMENT_NEW
374 UAlignedMemory stackMemory[sizeof(UnicodeString)/sizeof(UAlignedMemory)+1];
375 UnicodeString *p;
376 enum { len=20 };
377
378 p=new(stackMemory) UnicodeString(len, (UChar32)0x20ac, len);
379 if((void *)p!=(void *)stackMemory) {
380 errln("placement new did not place the object at the expected address");
381 }
382 if(p->length()!=len || p->charAt(0)!=0x20ac || p->charAt(len-1)!=0x20ac) {
383 errln("constructor used with placement new did not work right");
384 }
385
386 /*
387 * It is not possible to simply say
388 * delete(p, stackMemory);
389 * which results in a call to the normal, non-placement delete operator.
390 *
391 * Via a search on google.com for "c++ placement delete" I found
392 * http://cpptips.hyperformix.com/cpptips/placement_del3
393 * which says:
394 *
395 * TITLE: using placement delete
396 *
397 * (Newsgroups: comp.std.c++, 27 Aug 97)
398 *
399 * ISJ: isj@image.dk
400 *
401 * > I do not completely understand how placement works on operator delete.
402 * > ...
403 * There is no delete-expression which will invoke a placement
404 * form of operator delete. You can still call the function
405 * explicitly. Example:
406 * ...
407 * // destroy object and delete space manually
408 * p->~T();
409 * operator delete(p, 12);
410 *
411 * ... so that's what I am doing here.
412 * markus 20031216
413 */
414 // destroy object and delete space manually
415 p->~UnicodeString();
416 UnicodeString::operator delete(p, stackMemory);
417
418 // Jitterbug 4452, for coverage
419 UnicodeString *pa = new UnicodeString[2];
420 if ( !pa[0].isEmpty() || !pa[1].isEmpty()){
421 errln("constructor used with array new did not work right");
422 }
423 delete [] pa;
424#endif
425
426 // try to call the compiler-generated UMemory::operator=(class UMemory const &)
427 UMemory m, n;
428 m=n;
429}
430
431void UObjectTest::TestMFCCompatibility() {
432#if U_HAVE_DEBUG_LOCATION_NEW
433 /* Make sure that it compiles with MFC's debuggable new usage. */
434 UnicodeString *str = new(__FILE__, __LINE__) UnicodeString();
435 str->append((UChar)0x0040); // Is it usable?
436 if(str->charAt(0) != 0x0040) {
437 errln("debug new doesn't work.");
438 }
439 UnicodeString::operator delete(str, __FILE__, __LINE__);
440#endif
441}
442
443/* --------------- */
444
445#define CASE(id,test) case id: name = #test; if (exec) { logln(#test "---"); logln((UnicodeString)""); test(); } break;
446
447
448void UObjectTest::runIndexedTest( int32_t index, UBool exec, const char* &name, char* /* par */ )
449{
450 switch (index) {
451
452 CASE(0, testIDs);
453 CASE(1, testUMemory);
454 CASE(2, TestMFCCompatibility);
455
456 default: name = ""; break; //needed to end loop
457 }
458}