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