• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /********************************************************************
2  * COPYRIGHT:
3  * Copyright (c) 1997-2011, International Business Machines Corporation and
4  * others. All Rights Reserved.
5  ********************************************************************/
6 
7 
8 /**
9  * IntlTestUtilities is the medium level test class for everything in the directory "utility".
10  */
11 
12 #include "unicode/utypes.h"
13 #include "unicode/errorcode.h"
14 #include "unicode/localpointer.h"
15 #include "itutil.h"
16 #include "strtest.h"
17 #include "loctest.h"
18 #include "citrtest.h"
19 #include "ustrtest.h"
20 #include "ucdtest.h"
21 #include "restest.h"
22 #include "restsnew.h"
23 #include "tsmthred.h"
24 #include "tsputil.h"
25 #include "uobjtest.h"
26 #include "utxttest.h"
27 #include "v32test.h"
28 #include "uvectest.h"
29 #include "aliastst.h"
30 #include "usettest.h"
31 
32 extern IntlTest *createBytesTrieTest();
33 static IntlTest *createLocalPointerTest();
34 extern IntlTest *createUCharsTrieTest();
35 
36 #define CASE(id, test) case id:                               \
37                           name = #test;                       \
38                           if (exec) {                         \
39                               logln(#test "---"); logln();    \
40                               test t;                         \
41                               callTest(t, par);               \
42                           }                                   \
43                           break
44 
runIndexedTest(int32_t index,UBool exec,const char * & name,char * par)45 void IntlTestUtilities::runIndexedTest( int32_t index, UBool exec, const char* &name, char* par )
46 {
47     if (exec) logln("TestSuite Utilities: ");
48     switch (index) {
49         CASE(0, MultithreadTest);
50         CASE(1, StringTest);
51         CASE(2, UnicodeStringTest);
52         CASE(3, LocaleTest);
53         CASE(4, CharIterTest);
54         CASE(5, UObjectTest);
55         CASE(6, UnicodeTest);
56         CASE(7, ResourceBundleTest);
57         CASE(8, NewResourceBundleTest);
58         CASE(9, PUtilTest);
59         CASE(10, UVector32Test);
60         CASE(11, UVectorTest);
61         CASE(12, UTextTest);
62         CASE(13, LocaleAliasTest);
63         CASE(14, UnicodeSetTest);
64         CASE(15, ErrorCodeTest);
65         case 16:
66             name = "LocalPointerTest";
67             if (exec) {
68                 logln("TestSuite LocalPointerTest---"); logln();
69                 LocalPointer<IntlTest> test(createLocalPointerTest());
70                 callTest(*test, par);
71             }
72             break;
73         case 17:
74             name = "BytesTrieTest";
75             if (exec) {
76                 logln("TestSuite BytesTrieTest---"); logln();
77                 LocalPointer<IntlTest> test(createBytesTrieTest());
78                 callTest(*test, par);
79             }
80             break;
81         case 18:
82             name = "UCharsTrieTest";
83             if (exec) {
84                 logln("TestSuite UCharsTrieTest---"); logln();
85                 LocalPointer<IntlTest> test(createUCharsTrieTest());
86                 callTest(*test, par);
87             }
88             break;
89         default: name = ""; break; //needed to end loop
90     }
91 }
92 
runIndexedTest(int32_t index,UBool exec,const char * & name,char *)93 void ErrorCodeTest::runIndexedTest(int32_t index, UBool exec, const char* &name, char* /*par*/) {
94     if (exec) logln("TestSuite Utilities: ");
95     switch (index) {
96         case 0: name = "TestErrorCode"; if (exec) TestErrorCode(); break;
97         case 1: name = "TestSubclass"; if (exec) TestSubclass(); break;
98         default: name = ""; break; //needed to end loop
99     }
100 }
101 
RefPlusOne(UErrorCode & code)102 static void RefPlusOne(UErrorCode &code) { code=(UErrorCode)(code+1); }
PtrPlusTwo(UErrorCode * code)103 static void PtrPlusTwo(UErrorCode *code) { *code=(UErrorCode)(*code+2); }
104 
TestErrorCode()105 void ErrorCodeTest::TestErrorCode() {
106     ErrorCode errorCode;
107     if(errorCode.get()!=U_ZERO_ERROR || !errorCode.isSuccess() || errorCode.isFailure()) {
108         errln("ErrorCode did not initialize properly");
109         return;
110     }
111     errorCode.assertSuccess();
112     if(errorCode.errorName()!=u_errorName(U_ZERO_ERROR)) {
113         errln("ErrorCode did not format error message string properly");
114     }
115     RefPlusOne(errorCode);
116     if(errorCode.get()!=U_ILLEGAL_ARGUMENT_ERROR || errorCode.isSuccess() || !errorCode.isFailure()) {
117         errln("ErrorCode did not yield a writable reference");
118     }
119     PtrPlusTwo(errorCode);
120     if(errorCode.get()!=U_INVALID_FORMAT_ERROR || errorCode.isSuccess() || !errorCode.isFailure()) {
121         errln("ErrorCode did not yield a writable pointer");
122     }
123     errorCode.set(U_PARSE_ERROR);
124     if(errorCode.get()!=U_PARSE_ERROR || errorCode.isSuccess() || !errorCode.isFailure()) {
125         errln("ErrorCode.set() failed");
126     }
127     if( errorCode.reset()!=U_PARSE_ERROR || errorCode.get()!=U_ZERO_ERROR ||
128         !errorCode.isSuccess() || errorCode.isFailure()
129     ) {
130         errln("ErrorCode did not reset properly");
131     }
132 }
133 
134 class MyErrorCode: public ErrorCode {
135 public:
MyErrorCode(int32_t & countChecks,int32_t & countDests)136     MyErrorCode(int32_t &countChecks, int32_t &countDests)
137         : checks(countChecks), dests(countDests) {}
~MyErrorCode()138     ~MyErrorCode() {
139         if(isFailure()) {
140             ++dests;
141         }
142     }
143 private:
handleFailure() const144     virtual void handleFailure() const {
145         ++checks;
146     }
147     int32_t &checks;
148     int32_t &dests;
149 };
150 
TestSubclass()151 void ErrorCodeTest::TestSubclass() {
152     int32_t countChecks=0;
153     int32_t countDests=0;
154     {
155         MyErrorCode errorCode(countChecks, countDests);
156         if( errorCode.get()!=U_ZERO_ERROR || !errorCode.isSuccess() || errorCode.isFailure() ||
157             countChecks!=0 || countDests!=0
158         ) {
159             errln("ErrorCode did not initialize properly");
160             return;
161         }
162         errorCode.assertSuccess();
163         if(countChecks!=0) {
164             errln("ErrorCode.assertSuccess() called handleFailure() despite success");
165         }
166         RefPlusOne(errorCode);
167         if(errorCode.get()!=U_ILLEGAL_ARGUMENT_ERROR || errorCode.isSuccess() || !errorCode.isFailure()) {
168             errln("ErrorCode did not yield a writable reference");
169         }
170         errorCode.assertSuccess();
171         if(countChecks!=1) {
172             errln("ErrorCode.assertSuccess() did not handleFailure()");
173         }
174         PtrPlusTwo(errorCode);
175         if(errorCode.get()!=U_INVALID_FORMAT_ERROR || errorCode.isSuccess() || !errorCode.isFailure()) {
176             errln("ErrorCode did not yield a writable pointer");
177         }
178         errorCode.assertSuccess();
179         if(countChecks!=2) {
180             errln("ErrorCode.assertSuccess() did not handleFailure()");
181         }
182         errorCode.set(U_PARSE_ERROR);
183         if(errorCode.get()!=U_PARSE_ERROR || errorCode.isSuccess() || !errorCode.isFailure()) {
184             errln("ErrorCode.set() failed");
185         }
186         if( errorCode.reset()!=U_PARSE_ERROR || errorCode.get()!=U_ZERO_ERROR ||
187             !errorCode.isSuccess() || errorCode.isFailure()
188         ) {
189             errln("ErrorCode did not reset properly");
190         }
191         errorCode.assertSuccess();
192         if(countChecks!=2) {
193             errln("ErrorCode.assertSuccess() called handleFailure() despite success");
194         }
195     }
196     if(countDests!=0) {
197         errln("MyErrorCode destructor detected failure despite success");
198     }
199     countChecks=countDests=0;
200     {
201         MyErrorCode errorCode(countChecks, countDests);
202         errorCode.set(U_PARSE_ERROR);
203     }
204     if(countDests!=1) {
205         errln("MyErrorCode destructor failed to detect failure");
206     }
207 }
208 
209 class LocalPointerTest : public IntlTest {
210 public:
LocalPointerTest()211     LocalPointerTest() {}
212 
213     void runIndexedTest(int32_t index, UBool exec, const char *&name, char *par=NULL);
214 
215     void TestLocalPointer();
216     void TestLocalArray();
217     void TestLocalXyzPointer();
218     void TestLocalXyzPointerNull();
219 };
220 
createLocalPointerTest()221 static IntlTest *createLocalPointerTest() {
222     return new LocalPointerTest();
223 }
224 
runIndexedTest(int32_t index,UBool exec,const char * & name,char *)225 void LocalPointerTest::runIndexedTest(int32_t index, UBool exec, const char *&name, char * /*par*/) {
226     if(exec) {
227         logln("TestSuite LocalPointerTest: ");
228     }
229     switch (index) {
230         TESTCASE(0, TestLocalPointer);
231         TESTCASE(1, TestLocalArray);
232         TESTCASE(2, TestLocalXyzPointer);
233         TESTCASE(3, TestLocalXyzPointerNull);
234         default:
235             name="";
236             break; // needed to end the loop
237     }
238 }
239 
240 // Exercise every LocalPointer and LocalPointerBase method.
TestLocalPointer()241 void LocalPointerTest::TestLocalPointer() {
242     // constructor
243     LocalPointer<UnicodeString> s(new UnicodeString((UChar32)0x50005));
244     // isNULL(), isValid(), operator==(), operator!=()
245     if(s.isNull() || !s.isValid() || s==NULL || !(s!=NULL)) {
246         errln("LocalPointer constructor or NULL test failure");
247         return;
248     }
249     // getAlias(), operator->, operator*
250     if(s.getAlias()->length()!=2 || s->length()!=2 || (*s).length()!=2) {
251         errln("LocalPointer access failure");
252     }
253     // adoptInstead(), orphan()
254     s.adoptInstead(new UnicodeString((UChar)0xfffc));
255     if(s->length()!=1) {
256         errln("LocalPointer adoptInstead(U+FFFC) failure");
257     }
258     UnicodeString *orphan=s.orphan();
259     if(orphan==NULL || orphan->length()!=1 || s.isValid() || s!=NULL) {
260         errln("LocalPointer orphan() failure");
261     }
262     delete orphan;
263     // destructor
264     s.adoptInstead(new UnicodeString());
265     if(s->length()!=0) {
266         errln("LocalPointer adoptInstead(empty) failure");
267     }
268 }
269 
270 // Exercise every LocalArray method (but not LocalPointerBase).
TestLocalArray()271 void LocalPointerTest::TestLocalArray() {
272     // constructor
273     LocalArray<UnicodeString> a(new UnicodeString[2]);
274     // operator[]()
275     a[0].append((UChar)0x61);
276     a[1].append((UChar32)0x60006);
277     if(a[0].length()!=1 || a[1].length()!=2) {
278         errln("LocalArray access failure");
279     }
280     // adoptInstead()
281     a.adoptInstead(new UnicodeString[4]);
282     a[3].append((UChar)0x62).append((UChar)0x63).reverse();
283     if(a[3].length()!=2 || a[3][1]!=0x62) {
284         errln("LocalArray adoptInstead() failure");
285     }
286     // destructor
287 }
288 
289 #include "unicode/ucnvsel.h"
290 #include "unicode/ucal.h"
291 #include "unicode/udatpg.h"
292 #include "unicode/uidna.h"
293 #include "unicode/uldnames.h"
294 #include "unicode/umsg.h"
295 #include "unicode/unorm2.h"
296 #include "unicode/uregex.h"
297 #include "unicode/utrans.h"
298 
299 // Use LocalXyzPointer types that are not covered elsewhere in the intltest suite.
TestLocalXyzPointer()300 void LocalPointerTest::TestLocalXyzPointer() {
301     IcuTestErrorCode errorCode(*this, "TestLocalXyzPointer");
302 
303     static const char *const encoding="ISO-8859-1";
304     LocalUConverterSelectorPointer sel(
305         ucnvsel_open(&encoding, 1, NULL, UCNV_ROUNDTRIP_SET, errorCode));
306     if(errorCode.logIfFailureAndReset("ucnvsel_open()")) {
307         return;
308     }
309     if(sel.isNull()) {
310         errln("LocalUConverterSelectorPointer failure");
311         return;
312     }
313 
314 #if !UCONFIG_NO_FORMATTING
315     LocalUCalendarPointer cal(ucal_open(NULL, 0, "root", UCAL_GREGORIAN, errorCode));
316     if(errorCode.logDataIfFailureAndReset("ucal_open()")) {
317         return;
318     }
319     if(cal.isNull()) {
320         errln("LocalUCalendarPointer failure");
321         return;
322     }
323 
324     LocalUDateTimePatternGeneratorPointer patgen(udatpg_open("root", errorCode));
325     if(errorCode.logDataIfFailureAndReset("udatpg_open()")) {
326         return;
327     }
328     if(patgen.isNull()) {
329         errln("LocalUDateTimePatternGeneratorPointer failure");
330         return;
331     }
332 
333     LocalULocaleDisplayNamesPointer ldn(uldn_open("de-CH", ULDN_STANDARD_NAMES, errorCode));
334     if(errorCode.logIfFailureAndReset("uldn_open()")) {
335         return;
336     }
337     if(ldn.isNull()) {
338         errln("LocalULocaleDisplayNamesPointer failure");
339         return;
340     }
341 
342     UnicodeString hello=UNICODE_STRING_SIMPLE("Hello {0}!");
343     LocalUMessageFormatPointer msg(
344         umsg_open(hello.getBuffer(), hello.length(), "root", NULL, errorCode));
345     if(errorCode.logIfFailureAndReset("umsg_open()")) {
346         return;
347     }
348     if(msg.isNull()) {
349         errln("LocalUMessageFormatPointer failure");
350         return;
351     }
352 #endif  /* UCONFIG_NO_FORMATTING  */
353 
354 #if !UCONFIG_NO_NORMALIZATION
355     const UNormalizer2 *nfc=unorm2_getNFCInstance(errorCode);
356     UnicodeSet emptySet;
357     LocalUNormalizer2Pointer fn2(unorm2_openFiltered(nfc, emptySet.toUSet(), errorCode));
358     if(errorCode.logIfFailureAndReset("unorm2_openFiltered()")) {
359         return;
360     }
361     if(fn2.isNull()) {
362         errln("LocalUNormalizer2Pointer failure");
363         return;
364     }
365 #endif /* !UCONFIG_NO_NORMALIZATION */
366 
367 #if !UCONFIG_NO_IDNA
368     LocalUIDNAPointer idna(uidna_openUTS46(0, errorCode));
369     if(errorCode.logIfFailureAndReset("uidna_openUTS46()")) {
370         return;
371     }
372     if(idna.isNull()) {
373         errln("LocalUIDNAPointer failure");
374         return;
375     }
376 #endif  /* !UCONFIG_NO_IDNA */
377 
378 #if !UCONFIG_NO_REGULAR_EXPRESSIONS
379     UnicodeString pattern=UNICODE_STRING_SIMPLE("abc|xy+z");
380     LocalURegularExpressionPointer regex(
381         uregex_open(pattern.getBuffer(), pattern.length(), 0, NULL, errorCode));
382     if(errorCode.logIfFailureAndReset("uregex_open()")) {
383         return;
384     }
385     if(regex.isNull()) {
386         errln("LocalURegularExpressionPointer failure");
387         return;
388     }
389 #endif /* UCONFIG_NO_REGULAR_EXPRESSIONS */
390 
391 #if !UCONFIG_NO_TRANSLITERATION
392     UnicodeString id=UNICODE_STRING_SIMPLE("Grek-Latn");
393     LocalUTransliteratorPointer trans(
394         utrans_openU(id.getBuffer(), id.length(), UTRANS_FORWARD, NULL, 0, NULL, errorCode));
395     if(errorCode.logIfFailureAndReset("utrans_open()")) {
396         return;
397     }
398     if(trans.isNull()) {
399         errln("LocalUTransliteratorPointer failure");
400         return;
401     }
402 #endif /* !UCONFIG_NO_TRANSLITERATION */
403 
404     // destructors
405 }
406 
407 // Try LocalXyzPointer types with NULL pointers.
TestLocalXyzPointerNull()408 void LocalPointerTest::TestLocalXyzPointerNull() {
409     {
410         IcuTestErrorCode errorCode(*this, "TestLocalXyzPointerNull/LocalUConverterSelectorPointer");
411         static const char *const encoding="ISO-8859-1";
412         LocalUConverterSelectorPointer null;
413         LocalUConverterSelectorPointer sel(
414             ucnvsel_open(&encoding, 1, NULL, UCNV_ROUNDTRIP_SET, errorCode));
415         sel.adoptInstead(NULL);
416     }
417 #if !UCONFIG_NO_FORMATTING
418     {
419         IcuTestErrorCode errorCode(*this, "TestLocalXyzPointerNull/LocalUCalendarPointer");
420         LocalUCalendarPointer null;
421         LocalUCalendarPointer cal(ucal_open(NULL, 0, "root", UCAL_GREGORIAN, errorCode));
422         if(!errorCode.logDataIfFailureAndReset("ucal_open()")) {
423             cal.adoptInstead(NULL);
424         }
425     }
426     {
427         IcuTestErrorCode errorCode(*this, "TestLocalXyzPointerNull/LocalUDateTimePatternGeneratorPointer");
428         LocalUDateTimePatternGeneratorPointer null;
429         LocalUDateTimePatternGeneratorPointer patgen(udatpg_open("root", errorCode));
430         patgen.adoptInstead(NULL);
431     }
432     {
433         IcuTestErrorCode errorCode(*this, "TestLocalXyzPointerNull/LocalUMessageFormatPointer");
434         UnicodeString hello=UNICODE_STRING_SIMPLE("Hello {0}!");
435         LocalUMessageFormatPointer null;
436         LocalUMessageFormatPointer msg(
437             umsg_open(hello.getBuffer(), hello.length(), "root", NULL, errorCode));
438         msg.adoptInstead(NULL);
439     }
440 #endif /* !UCONFIG_NO_FORMATTING */
441 
442 #if !UCONFIG_NO_REGULAR_EXPRESSIONS
443     {
444         IcuTestErrorCode errorCode(*this, "TestLocalXyzPointerNull/LocalURegularExpressionPointer");
445         UnicodeString pattern=UNICODE_STRING_SIMPLE("abc|xy+z");
446         LocalURegularExpressionPointer null;
447         LocalURegularExpressionPointer regex(
448             uregex_open(pattern.getBuffer(), pattern.length(), 0, NULL, errorCode));
449         if(!errorCode.logDataIfFailureAndReset("urege_open()")) {
450             regex.adoptInstead(NULL);
451         }
452     }
453 #endif /* !UCONFIG_NO_REGULAR_EXPRESSIONS */
454 
455 #if !UCONFIG_NO_TRANSLITERATION
456     {
457         IcuTestErrorCode errorCode(*this, "TestLocalXyzPointerNull/LocalUTransliteratorPointer");
458         UnicodeString id=UNICODE_STRING_SIMPLE("Grek-Latn");
459         LocalUTransliteratorPointer null;
460         LocalUTransliteratorPointer trans(
461             utrans_openU(id.getBuffer(), id.length(), UTRANS_FORWARD, NULL, 0, NULL, errorCode));
462         if(!errorCode.logDataIfFailureAndReset("utrans_openU()")) {
463             trans.adoptInstead(NULL);
464         }
465     }
466 #endif /* !UCONFIG_NO_TRANSLITERATION */
467 
468 }
469