]> git.saurik.com Git - apple/icu.git/blame - icuSources/test/intltest/uobjtest.cpp
ICU-6.2.22.tar.gz
[apple/icu.git] / icuSources / test / intltest / uobjtest.cpp
CommitLineData
b75a7d8f
A
1/********************************************************************
2 * COPYRIGHT:
374ca955 3 * Copyright (c) 2002-2004, International Business Machines Corporation and
b75a7d8f
A
4 * others. All Rights Reserved.
5 ********************************************************************/
6
7#include "uobjtest.h"
374ca955 8#include "cmemory.h" // UAlignedMemory
b75a7d8f 9#include <string.h>
374ca955 10#include <stdio.h>
b75a7d8f
A
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,
374ca955
A
46 const char *className, const char *factory,
47 UClassID staticID)
b75a7d8f
A
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)) {
374ca955
A
97 logln("OK: ID found is the same as " + UnicodeString(ids_class[i]) + UnicodeString(" *y= ") + ids_factory[i] + what);
98 return obj;
b75a7d8f 99 } else {
374ca955
A
100 errln("FAIL: ID is the same as " + UnicodeString(ids_class[i]) + UnicodeString(" *y= ") + ids_factory[i] + what);
101 return obj;
b75a7d8f
A
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// Things we Patch
125#define protected public /* to access private factory function */
126#include "iculserv.h"
127#undef protected
128
129// Internal Things (woo)
130#include "cpdtrans.h"
131#include "rbt.h"
132#include "rbt_data.h"
b75a7d8f
A
133#include "nultrans.h"
134#include "anytrans.h"
135#include "digitlst.h"
136#include "esctrn.h"
137#include "funcrepl.h"
138#include "icunotif.h"
139#include "icuserv.h"
140#include "name2uni.h"
141#include "nfsubs.h"
142#include "nortrans.h"
143#include "quant.h"
144#include "remtrans.h"
145#include "strmatch.h"
146#include "strrepl.h"
147#include "titletrn.h"
148#include "tolowtrn.h"
149#include "toupptrn.h"
150#include "unesctrn.h"
151#include "uni2name.h"
152#include "uvector.h"
374ca955 153#include "islamcal.h"
b75a7d8f
A
154
155// External Things
156#include "unicode/brkiter.h"
157#include "unicode/calendar.h"
158#include "unicode/caniter.h"
159#include "unicode/chariter.h"
160#include "unicode/choicfmt.h"
161#include "unicode/coleitr.h"
162#include "unicode/coll.h"
163#include "unicode/datefmt.h"
164#include "unicode/dbbi.h"
165#include "unicode/dcfmtsym.h"
166#include "unicode/decimfmt.h"
167#include "unicode/dtfmtsym.h"
168#include "unicode/fieldpos.h"
169#include "unicode/fmtable.h"
170#include "unicode/format.h"
171#include "unicode/gregocal.h"
172#include "unicode/locid.h"
173#include "unicode/msgfmt.h"
174#include "unicode/normlzr.h"
175#include "unicode/numfmt.h"
176#include "unicode/parsepos.h"
177#include "unicode/rbbi.h"
178#include "unicode/rbnf.h"
179#include "unicode/regex.h"
180#include "unicode/resbund.h"
181#include "unicode/schriter.h"
182#include "unicode/simpletz.h"
183#include "unicode/smpdtfmt.h"
184#include "unicode/sortkey.h"
185#include "unicode/stsearch.h"
186#include "unicode/tblcoll.h"
187#include "unicode/timezone.h"
188#include "unicode/translit.h"
189#include "unicode/uchriter.h"
190#include "unicode/unifilt.h"
191#include "unicode/unifunct.h"
192#include "unicode/uniset.h"
193#include "unicode/unistr.h"
194#include "unicode/uobject.h"
195#include "unicode/usetiter.h"
196//#include "unicode/bidi.h"
197//#include "unicode/convert.h"
198
199// END includes =============================================================
200
201#define UOBJTEST_TEST_INTERNALS 0 /* do NOT test Internal things - their functions aren't exported on Win32 */
202
203void UObjectTest::testIDs()
204{
205 ids_count = 0;
206
207#if !UCONFIG_NO_TRANSLITERATION
208 UParseError parseError;
209#endif
210 UErrorCode status = U_ZERO_ERROR;
211
212
213
214 //TESTCLASSID_DEFAULT(AbbreviatedUnicodeSetIterator);
215 //TESTCLASSID_DEFAULT(AnonymousStringFactory);
216
217
218#if !UCONFIG_NO_NORMALIZATION
219 TESTCLASSID_FACTORY(CanonicalIterator, new CanonicalIterator(UnicodeString("abc"), status));
220#endif
221 //TESTCLASSID_DEFAULT(CollationElementIterator);
222#if !UCONFIG_NO_COLLATION
223 TESTCLASSID_DEFAULT(CollationKey);
224#endif
225 //TESTCLASSID_FACTORY(CompoundTransliterator, Transliterator::createInstance(UnicodeString("Any-Jex;Hangul-Jamo"), UTRANS_FORWARD, parseError, status));
226
227#if !UCONFIG_NO_FORMATTING
228 /* TESTCLASSID_FACTORY(NFSubstitution, NFSubstitution::makeSubstitution(8, */
229 /* TESTCLASSID_DEFAULT(DigitList); UMemory but not UObject*/
230 TESTCLASSID_ABSTRACT(NumberFormat);
231 TESTCLASSID_CTOR(DateFormatSymbols, (status));
232 TESTCLASSID_CTOR(DecimalFormatSymbols, (status));
233#if UOBJTEST_TEST_INTERNALS
234 TESTCLASSID_CTOR(FunctionReplacer, (NULL,NULL) ); /* don't care */
235#endif
236 TESTCLASSID_DEFAULT(FieldPosition);
237 TESTCLASSID_DEFAULT(Formattable);
238 TESTCLASSID_CTOR(GregorianCalendar, (status));
374ca955 239 TESTCLASSID_CTOR(IslamicCalendar, (Locale::getUS(), status));
b75a7d8f
A
240#endif
241
242#if !UCONFIG_NO_BREAK_ITERATION
243 /* TESTCLASSID_ABSTRACT(BreakIterator); No staticID! */
244 TESTCLASSID_FACTORY(RuleBasedBreakIterator, BreakIterator::createLineInstance("mt",status));
245 TESTCLASSID_FACTORY(DictionaryBasedBreakIterator, BreakIterator::createLineInstance("th",status));
246#endif
247
248 //TESTCLASSID_DEFAULT(EscapeTransliterator);
249
250 //TESTCLASSID_DEFAULT(GregorianCalendar);
251
252#if !UCONFIG_NO_TRANSLITERATION
253
254
b75a7d8f
A
255 TESTCLASSID_TRANSLIT(AnyTransliterator, "Any-Latin");
256 TESTCLASSID_TRANSLIT(CompoundTransliterator, "Latin-Greek");
257 TESTCLASSID_TRANSLIT(EscapeTransliterator, "Any-Hex");
258 TESTCLASSID_TRANSLIT(LowercaseTransliterator, "Lower");
259 TESTCLASSID_TRANSLIT(NameUnicodeTransliterator, "Name-Any");
260 TESTCLASSID_TRANSLIT(NormalizationTransliterator, "NFD");
261 TESTCLASSID_TRANSLIT(NullTransliterator, "Null");
262 TESTCLASSID_TRANSLIT(RemoveTransliterator, "Remove");
263 TESTCLASSID_CTOR(RuleBasedTransliterator, (UnicodeString("abcd"), UnicodeString("a>b;"), status));
264 TESTCLASSID_TRANSLIT(TitlecaseTransliterator, "Title");
265 TESTCLASSID_TRANSLIT(UnescapeTransliterator, "Hex-Any");
266 TESTCLASSID_TRANSLIT(UnicodeNameTransliterator, "Any-Name");
267 TESTCLASSID_TRANSLIT(UppercaseTransliterator, "Upper");
268#endif
269
270 TESTCLASSID_FACTORY(Locale, new Locale("123"));
271
272 //TESTCLASSID_DEFAULT(Normalizer);
273
274 //TESTCLASSID_DEFAULT(NumeratorSubstitution);
275
276#if !UCONFIG_NO_TRANSLITERATION
277 TESTCLASSID_DEFAULT(ParsePosition);
278 //TESTCLASSID_DEFAULT(Quantifier);
279#endif
280
281
282// NO_REG_EX
283 //TESTCLASSID_DEFAULT(RegexCompile);
284 //TESTCLASSID_DEFAULT(RegexMatcher);
285 //TESTCLASSID_DEFAULT(RegexPattern);
286
287 //TESTCLASSID_DEFAULT(ReplaceableGlue);
288 TESTCLASSID_FACTORY(ResourceBundle, new ResourceBundle(UnicodeString(), status) );
289 //TESTCLASSID_DEFAULT(RuleBasedTransliterator);
290
291 //TESTCLASSID_DEFAULT(SimpleFwdCharIterator);
292 //TESTCLASSID_DEFAULT(StringReplacer);
293 //TESTCLASSID_DEFAULT(StringSearch);
294
295 //TESTCLASSID_DEFAULT(TempSearch);
296 //TESTCLASSID_DEFAULT(TestMultipleKeyStringFactory);
297 //TESTCLASSID_DEFAULT(TestReplaceable);
374ca955 298
b75a7d8f
A
299#if !UCONFIG_NO_FORMATTING
300 TESTCLASSID_ABSTRACT(TimeZone);
301#endif
302
303#if !UCONFIG_NO_TRANSLITERATION
304 TESTCLASSID_FACTORY(TitlecaseTransliterator, Transliterator::createInstance(UnicodeString("Any-Title"), UTRANS_FORWARD, parseError, status));
305 TESTCLASSID_ABSTRACT(Transliterator);
306
307#if UOBJTEST_TEST_INTERNALS
308 TESTCLASSID_CTOR(StringMatcher, (UnicodeString("x"), 0,0,0,TransliterationRuleData(status)));
309 TESTCLASSID_CTOR(StringReplacer,(UnicodeString(),new TransliterationRuleData(status)));
310#endif
311#endif
312
313 TESTCLASSID_DEFAULT(UnicodeString);
314 TESTCLASSID_CTOR(UnicodeSet, (0, 1));
315 TESTCLASSID_ABSTRACT(UnicodeFilter);
316 TESTCLASSID_ABSTRACT(UnicodeFunctor);
317 TESTCLASSID_CTOR(UnicodeSetIterator,(UnicodeSet(0,1)));
318 TESTCLASSID_CTOR(UStack, (status));
319 TESTCLASSID_CTOR(UVector, (status));
320
321#if !UCONFIG_NO_SERVICE
322 TESTCLASSID_CTOR(SimpleFactory, (NULL, UnicodeString("foo")));
323 TESTCLASSID_DEFAULT(EventListener);
324#if UOBJTEST_TEST_INTERNALS
325 TESTCLASSID_DEFAULT(ICUResourceBundleFactory);
326 //TESTCLASSID_DEFAULT(Key); // does ont exist?
327 TESTCLASSID_CTOR(LocaleKey, (UnicodeString("baz"), UnicodeString("bat"), NULL, 92));
328 TESTCLASSID_CTOR(LocaleKeyFactory, (42));
329 TESTCLASSID_CTOR(SimpleLocaleKeyFactory, (NULL, UnicodeString("bar"), 8, 12) );
330#endif
331#endif
332
333#if UOBJTEST_DUMP_IDS
334 int i;
335 for(i=0;i<ids_count;i++) {
336 char junk[800];
337 sprintf(junk, " %4d:\t%p\t%s\t%s\n",
338 i, ids[i], ids_class[i], ids_factory[i]);
339 logln(UnicodeString(junk));
340 }
341#endif
342}
343
374ca955
A
344void UObjectTest::testUMemory() {
345 // additional tests for code coverage
346#if U_OVERRIDE_CXX_ALLOCATION && U_HAVE_PLACEMENT_NEW
347 UAlignedMemory stackMemory[sizeof(UnicodeString)/sizeof(UAlignedMemory)+1];
348 UnicodeString *p;
349 enum { len=20 };
350
351 p=new(stackMemory) UnicodeString(len, (UChar32)0x20ac, len);
352 if((void *)p!=(void *)stackMemory) {
353 errln("placement new did not place the object at the expected address");
354 }
355 if(p->length()!=len || p->charAt(0)!=0x20ac || p->charAt(len-1)!=0x20ac) {
356 errln("constructor used with placement new did not work right");
357 }
358
359 /*
360 * It is not possible to simply say
361 * delete(p, stackMemory);
362 * which results in a call to the normal, non-placement delete operator.
363 *
364 * Via a search on google.com for "c++ placement delete" I found
365 * http://cpptips.hyperformix.com/cpptips/placement_del3
366 * which says:
367 *
368 * TITLE: using placement delete
369 *
370 * (Newsgroups: comp.std.c++, 27 Aug 97)
371 *
372 * ISJ: isj@image.dk
373 *
374 * > I do not completely understand how placement works on operator delete.
375 * > ...
376 * There is no delete-expression which will invoke a placement
377 * form of operator delete. You can still call the function
378 * explicitly. Example:
379 * ...
380 * // destroy object and delete space manually
381 * p->~T();
382 * operator delete(p, 12);
383 *
384 * ... so that's what I am doing here.
385 * markus 20031216
386 */
387 // destroy object and delete space manually
388 p->~UnicodeString();
389 UnicodeString::operator delete(p, stackMemory);
390#endif
391
392 // try to call the compiler-generated UMemory::operator=(class UMemory const &)
393 UMemory m, n;
394 m=n;
395}
396
b75a7d8f
A
397/* --------------- */
398
399#define CASE(id,test) case id: name = #test; if (exec) { logln(#test "---"); logln((UnicodeString)""); test(); } break;
400
401
402void UObjectTest::runIndexedTest( int32_t index, UBool exec, const char* &name, char* /* par */ )
403{
404 switch (index) {
405
406 CASE(0, testIDs);
374ca955 407 CASE(1, testUMemory);
b75a7d8f
A
408
409 default: name = ""; break; //needed to end loop
410 }
411}