1 /********************************************************************
3 * Copyright (c) 2002-2004, International Business Machines Corporation and
4 * others. All Rights Reserved.
5 ********************************************************************/
8 #include "cmemory.h" // UAlignedMemory
14 * Test for UObject, currently only the classID.
17 * TESTCLASSID_ABSTRACT(Bar)
18 * -- Bar is expected to be abstract. Only the static ID will be tested.
20 * TESTCLASSID_DEFAULT(Foo)
21 * -- Foo will be default-constructed.
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]
27 * TESTCLASSID_FACTORY(Foo, fooCreateFunction(status) )
28 * -- call fooCreateFunction. 'status' will be tested & reset
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())
38 #define MAX_CLASS_ID 200
40 UClassID ids
[MAX_CLASS_ID
];
41 const char *ids_factory
[MAX_CLASS_ID
];
42 const char *ids_class
[MAX_CLASS_ID
];
43 uint32_t ids_count
= 0;
45 UObject
*UObjectTest::testClass(UObject
*obj
,
46 const char *className
, const char *factory
,
50 UnicodeString what
= UnicodeString(className
) + " * x= " + UnicodeString(factory
?factory
:" ABSTRACT ") + "; ";
51 UClassID dynamicID
= NULL
;
53 if(ids_count
>= MAX_CLASS_ID
) {
55 sprintf(count
, " (currently %d) ", MAX_CLASS_ID
);
56 errln("FAIL: Fatal: Ran out of IDs! Increase MAX_CLASS_ID." + UnicodeString(count
) + what
);
61 dynamicID
= obj
->getDynamicClassID();
66 sprintf(tmp
, " [static=%p, dynamic=%p] ", staticID
, dynamicID
);
70 if(staticID
== NULL
) {
71 errln( "FAIL: staticID == NULL!" + what
);
74 if(factory
!= NULL
) { /* NULL factory means: abstract */
76 errln( "FAIL: ==NULL!" + what
);
80 if(dynamicID
== NULL
) {
81 errln("FAIL: dynamicID == NULL!" + what
);
84 if(dynamicID
!= staticID
) {
85 errln("FAIL: dynamicID != staticID!" + what
);
89 // Bail out if static ID is null
90 if(staticID
== NULL
) {
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
);
100 errln("FAIL: ID is the same as " + UnicodeString(ids_class
[i
]) + UnicodeString(" *y= ") + ids_factory
[i
] + what
);
106 ids
[ids_count
] = staticID
;
107 ids_factory
[ids_count
] = factory
;
108 ids_class
[ids_count
] = className
;
115 // begin actual #includes for things to be tested
117 // The following script will generate the #includes needed here:
119 // find common i18n -name '*.h' -print | xargs fgrep ClassID | cut -d: -f1 | cut -d\/ -f2- | sort | uniq | sed -e 's%.*%#include "&"%'
122 #include "unicode/utypes.h"
125 #define protected public /* to access private factory function */
126 #include "iculserv.h"
129 // Internal Things (woo)
130 #include "cpdtrans.h"
132 #include "rbt_data.h"
133 #include "nultrans.h"
134 #include "anytrans.h"
135 #include "digitlst.h"
137 #include "funcrepl.h"
138 #include "icunotif.h"
140 #include "name2uni.h"
142 #include "nortrans.h"
144 #include "remtrans.h"
145 #include "strmatch.h"
147 #include "titletrn.h"
148 #include "tolowtrn.h"
149 #include "toupptrn.h"
150 #include "unesctrn.h"
151 #include "uni2name.h"
153 #include "islamcal.h"
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"
199 // END includes =============================================================
201 #define UOBJTEST_TEST_INTERNALS 0 /* do NOT test Internal things - their functions aren't exported on Win32 */
203 void UObjectTest::testIDs()
207 #if !UCONFIG_NO_TRANSLITERATION
208 UParseError parseError
;
210 UErrorCode status
= U_ZERO_ERROR
;
214 //TESTCLASSID_DEFAULT(AbbreviatedUnicodeSetIterator);
215 //TESTCLASSID_DEFAULT(AnonymousStringFactory);
218 #if !UCONFIG_NO_NORMALIZATION
219 TESTCLASSID_FACTORY(CanonicalIterator
, new CanonicalIterator(UnicodeString("abc"), status
));
221 //TESTCLASSID_DEFAULT(CollationElementIterator);
222 #if !UCONFIG_NO_COLLATION
223 TESTCLASSID_DEFAULT(CollationKey
);
225 //TESTCLASSID_FACTORY(CompoundTransliterator, Transliterator::createInstance(UnicodeString("Any-Jex;Hangul-Jamo"), UTRANS_FORWARD, parseError, status));
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 */
236 TESTCLASSID_DEFAULT(FieldPosition
);
237 TESTCLASSID_DEFAULT(Formattable
);
238 TESTCLASSID_CTOR(GregorianCalendar
, (status
));
239 TESTCLASSID_CTOR(IslamicCalendar
, (Locale::getUS(), status
));
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
));
248 //TESTCLASSID_DEFAULT(EscapeTransliterator);
250 //TESTCLASSID_DEFAULT(GregorianCalendar);
252 #if !UCONFIG_NO_TRANSLITERATION
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");
270 TESTCLASSID_FACTORY(Locale
, new Locale("123"));
272 //TESTCLASSID_DEFAULT(Normalizer);
274 //TESTCLASSID_DEFAULT(NumeratorSubstitution);
276 #if !UCONFIG_NO_TRANSLITERATION
277 TESTCLASSID_DEFAULT(ParsePosition
);
278 //TESTCLASSID_DEFAULT(Quantifier);
283 //TESTCLASSID_DEFAULT(RegexCompile);
284 //TESTCLASSID_DEFAULT(RegexMatcher);
285 //TESTCLASSID_DEFAULT(RegexPattern);
287 //TESTCLASSID_DEFAULT(ReplaceableGlue);
288 TESTCLASSID_FACTORY(ResourceBundle
, new ResourceBundle(UnicodeString(), status
) );
289 //TESTCLASSID_DEFAULT(RuleBasedTransliterator);
291 //TESTCLASSID_DEFAULT(SimpleFwdCharIterator);
292 //TESTCLASSID_DEFAULT(StringReplacer);
293 //TESTCLASSID_DEFAULT(StringSearch);
295 //TESTCLASSID_DEFAULT(TempSearch);
296 //TESTCLASSID_DEFAULT(TestMultipleKeyStringFactory);
297 //TESTCLASSID_DEFAULT(TestReplaceable);
299 #if !UCONFIG_NO_FORMATTING
300 TESTCLASSID_ABSTRACT(TimeZone
);
303 #if !UCONFIG_NO_TRANSLITERATION
304 TESTCLASSID_FACTORY(TitlecaseTransliterator
, Transliterator::createInstance(UnicodeString("Any-Title"), UTRANS_FORWARD
, parseError
, status
));
305 TESTCLASSID_ABSTRACT(Transliterator
);
307 #if UOBJTEST_TEST_INTERNALS
308 TESTCLASSID_CTOR(StringMatcher
, (UnicodeString("x"), 0,0,0,TransliterationRuleData(status
)));
309 TESTCLASSID_CTOR(StringReplacer
,(UnicodeString(),new TransliterationRuleData(status
)));
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
));
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) );
333 #if UOBJTEST_DUMP_IDS
335 for(i
=0;i
<ids_count
;i
++) {
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
));
344 void 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];
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");
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");
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.
364 * Via a search on google.com for "c++ placement delete" I found
365 * http://cpptips.hyperformix.com/cpptips/placement_del3
368 * TITLE: using placement delete
370 * (Newsgroups: comp.std.c++, 27 Aug 97)
374 * > I do not completely understand how placement works on operator delete.
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:
380 * // destroy object and delete space manually
382 * operator delete(p, 12);
384 * ... so that's what I am doing here.
387 // destroy object and delete space manually
389 UnicodeString::operator delete(p
, stackMemory
);
392 // try to call the compiler-generated UMemory::operator=(class UMemory const &)
397 /* --------------- */
399 #define CASE(id,test) case id: name = #test; if (exec) { logln(#test "---"); logln((UnicodeString)""); test(); } break;
402 void UObjectTest::runIndexedTest( int32_t index
, UBool exec
, const char* &name
, char* /* par */ )
407 CASE(1, testUMemory
);
409 default: name
= ""; break; //needed to end loop