1 /********************************************************************
2  * COPYRIGHT:
3  * Copyright (c) 1997-2015, 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 "charstr.h"
16 #include "itutil.h"
17 #include "strtest.h"
18 #include "loctest.h"
19 #include "citrtest.h"
20 #include "ustrtest.h"
21 #include "ucdtest.h"
22 #include "restest.h"
23 #include "restsnew.h"
24 #include "tsmthred.h"
25 #include "tsputil.h"
26 #include "uobjtest.h"
27 #include "utxttest.h"
28 #include "v32test.h"
29 #include "uvectest.h"
30 #include "aliastst.h"
31 #include "usettest.h"
32 
33 extern IntlTest *createBytesTrieTest();
34 static IntlTest *createLocalPointerTest();
35 extern IntlTest *createUCharsTrieTest();
36 static IntlTest *createEnumSetTest();
37 extern IntlTest *createSimplePatternFormatterTest();
38 extern IntlTest *createUnifiedCacheTest();
39 extern IntlTest *createQuantityFormatterTest();
40 extern IntlTest *createPluralMapTest();
41 
42 
43 #define CASE(id, test) case id:                               \
44                           name = #test;                       \
45                           if (exec) {                         \
46                               logln(#test "---"); logln();    \
47                               test t;                         \
48                               callTest(t, par);               \
49                           }                                   \
50                           break
51 
runIndexedTest(int32_t index,UBool exec,const char * & name,char * par)52 void IntlTestUtilities::runIndexedTest( int32_t index, UBool exec, const char* &name, char* par )
53 {
54     if (exec) logln("TestSuite Utilities: ");
55     switch (index) {
56         CASE(0, MultithreadTest);
57         CASE(1, StringTest);
58         CASE(2, UnicodeStringTest);
59         CASE(3, LocaleTest);
60         CASE(4, CharIterTest);
61         CASE(5, UObjectTest);
62         CASE(6, UnicodeTest);
63         CASE(7, ResourceBundleTest);
64         CASE(8, NewResourceBundleTest);
65         CASE(9, PUtilTest);
66         CASE(10, UVector32Test);
67         CASE(11, UVectorTest);
68         CASE(12, UTextTest);
69         CASE(13, LocaleAliasTest);
70         CASE(14, UnicodeSetTest);
71         CASE(15, ErrorCodeTest);
72         case 16:
73             name = "LocalPointerTest";
74             if (exec) {
75                 logln("TestSuite LocalPointerTest---"); logln();
76                 LocalPointer<IntlTest> test(createLocalPointerTest());
77                 callTest(*test, par);
78             }
79             break;
80         case 17:
81             name = "BytesTrieTest";
82             if (exec) {
83                 logln("TestSuite BytesTrieTest---"); logln();
84                 LocalPointer<IntlTest> test(createBytesTrieTest());
85                 callTest(*test, par);
86             }
87             break;
88         case 18:
89             name = "UCharsTrieTest";
90             if (exec) {
91                 logln("TestSuite UCharsTrieTest---"); logln();
92                 LocalPointer<IntlTest> test(createUCharsTrieTest());
93                 callTest(*test, par);
94             }
95             break;
96         case 19:
97             name = "EnumSetTest";
98             if (exec) {
99                 logln("TestSuite EnumSetTest---"); logln();
100                 LocalPointer<IntlTest> test(createEnumSetTest());
101                 callTest(*test, par);
102             }
103             break;
104         case 20:
105             name = "SimplePatternFormatterTest";
106             if (exec) {
107                 logln("TestSuite SimplePatternFormatterTest---"); logln();
108                 LocalPointer<IntlTest> test(createSimplePatternFormatterTest());
109                 callTest(*test, par);
110             }
111             break;
112         case 21:
113             name = "UnifiedCacheTest";
114             if (exec) {
115                 logln("TestSuite UnifiedCacheTest---"); logln();
116                 LocalPointer<IntlTest> test(createUnifiedCacheTest());
117                 callTest(*test, par);
118             }
119             break;
120         case 22:
121             name = "QuantityFormatterTest";
122             if (exec) {
123                 logln("TestSuite QuantityFormatterTest---"); logln();
124                 LocalPointer<IntlTest> test(createQuantityFormatterTest());
125                 callTest(*test, par);
126             }
127             break;
128         case 23:
129             name = "PluralMapTest";
130             if (exec) {
131                 logln("TestSuite PluralMapTest---"); logln();
132                 LocalPointer<IntlTest> test(createPluralMapTest());
133                 callTest(*test, par);
134             }
135             break;
136         default: name = ""; break; //needed to end loop
137     }
138 }
139 
runIndexedTest(int32_t index,UBool exec,const char * & name,char *)140 void ErrorCodeTest::runIndexedTest(int32_t index, UBool exec, const char* &name, char* /*par*/) {
141     if (exec) logln("TestSuite Utilities: ");
142     switch (index) {
143         case 0: name = "TestErrorCode"; if (exec) TestErrorCode(); break;
144         case 1: name = "TestSubclass"; if (exec) TestSubclass(); break;
145         default: name = ""; break; //needed to end loop
146     }
147 }
148 
RefPlusOne(UErrorCode & code)149 static void RefPlusOne(UErrorCode &code) { code=(UErrorCode)(code+1); }
PtrPlusTwo(UErrorCode * code)150 static void PtrPlusTwo(UErrorCode *code) { *code=(UErrorCode)(*code+2); }
151 
TestErrorCode()152 void ErrorCodeTest::TestErrorCode() {
153     ErrorCode errorCode;
154     if(errorCode.get()!=U_ZERO_ERROR || !errorCode.isSuccess() || errorCode.isFailure()) {
155         errln("ErrorCode did not initialize properly");
156         return;
157     }
158     errorCode.assertSuccess();
159     if(errorCode.errorName()!=u_errorName(U_ZERO_ERROR)) {
160         errln("ErrorCode did not format error message string properly");
161     }
162     RefPlusOne(errorCode);
163     if(errorCode.get()!=U_ILLEGAL_ARGUMENT_ERROR || errorCode.isSuccess() || !errorCode.isFailure()) {
164         errln("ErrorCode did not yield a writable reference");
165     }
166     PtrPlusTwo(errorCode);
167     if(errorCode.get()!=U_INVALID_FORMAT_ERROR || errorCode.isSuccess() || !errorCode.isFailure()) {
168         errln("ErrorCode did not yield a writable pointer");
169     }
170     errorCode.set(U_PARSE_ERROR);
171     if(errorCode.get()!=U_PARSE_ERROR || errorCode.isSuccess() || !errorCode.isFailure()) {
172         errln("ErrorCode.set() failed");
173     }
174     if( errorCode.reset()!=U_PARSE_ERROR || errorCode.get()!=U_ZERO_ERROR ||
175         !errorCode.isSuccess() || errorCode.isFailure()
176     ) {
177         errln("ErrorCode did not reset properly");
178     }
179 }
180 
181 class MyErrorCode: public ErrorCode {
182 public:
MyErrorCode(int32_t & countChecks,int32_t & countDests)183     MyErrorCode(int32_t &countChecks, int32_t &countDests)
184         : checks(countChecks), dests(countDests) {}
~MyErrorCode()185     ~MyErrorCode() {
186         if(isFailure()) {
187             ++dests;
188         }
189     }
190 private:
handleFailure() const191     virtual void handleFailure() const {
192         ++checks;
193     }
194     int32_t &checks;
195     int32_t &dests;
196 };
197 
TestSubclass()198 void ErrorCodeTest::TestSubclass() {
199     int32_t countChecks=0;
200     int32_t countDests=0;
201     {
202         MyErrorCode errorCode(countChecks, countDests);
203         if( errorCode.get()!=U_ZERO_ERROR || !errorCode.isSuccess() || errorCode.isFailure() ||
204             countChecks!=0 || countDests!=0
205         ) {
206             errln("ErrorCode did not initialize properly");
207             return;
208         }
209         errorCode.assertSuccess();
210         if(countChecks!=0) {
211             errln("ErrorCode.assertSuccess() called handleFailure() despite success");
212         }
213         RefPlusOne(errorCode);
214         if(errorCode.get()!=U_ILLEGAL_ARGUMENT_ERROR || errorCode.isSuccess() || !errorCode.isFailure()) {
215             errln("ErrorCode did not yield a writable reference");
216         }
217         errorCode.assertSuccess();
218         if(countChecks!=1) {
219             errln("ErrorCode.assertSuccess() did not handleFailure()");
220         }
221         PtrPlusTwo(errorCode);
222         if(errorCode.get()!=U_INVALID_FORMAT_ERROR || errorCode.isSuccess() || !errorCode.isFailure()) {
223             errln("ErrorCode did not yield a writable pointer");
224         }
225         errorCode.assertSuccess();
226         if(countChecks!=2) {
227             errln("ErrorCode.assertSuccess() did not handleFailure()");
228         }
229         errorCode.set(U_PARSE_ERROR);
230         if(errorCode.get()!=U_PARSE_ERROR || errorCode.isSuccess() || !errorCode.isFailure()) {
231             errln("ErrorCode.set() failed");
232         }
233         if( errorCode.reset()!=U_PARSE_ERROR || errorCode.get()!=U_ZERO_ERROR ||
234             !errorCode.isSuccess() || errorCode.isFailure()
235         ) {
236             errln("ErrorCode did not reset properly");
237         }
238         errorCode.assertSuccess();
239         if(countChecks!=2) {
240             errln("ErrorCode.assertSuccess() called handleFailure() despite success");
241         }
242     }
243     if(countDests!=0) {
244         errln("MyErrorCode destructor detected failure despite success");
245     }
246     countChecks=countDests=0;
247     {
248         MyErrorCode errorCode(countChecks, countDests);
249         errorCode.set(U_PARSE_ERROR);
250     }
251     if(countDests!=1) {
252         errln("MyErrorCode destructor failed to detect failure");
253     }
254 }
255 
256 class LocalPointerTest : public IntlTest {
257 public:
LocalPointerTest()258     LocalPointerTest() {}
259 
260     void runIndexedTest(int32_t index, UBool exec, const char *&name, char *par=NULL);
261 
262     void TestLocalPointer();
263     void TestLocalPointerMoveSwap();
264     void TestLocalArray();
265     void TestLocalArrayMoveSwap();
266     void TestLocalXyzPointer();
267     void TestLocalXyzPointerMoveSwap();
268     void TestLocalXyzPointerNull();
269 };
270 
createLocalPointerTest()271 static IntlTest *createLocalPointerTest() {
272     return new LocalPointerTest();
273 }
274 
runIndexedTest(int32_t index,UBool exec,const char * & name,char *)275 void LocalPointerTest::runIndexedTest(int32_t index, UBool exec, const char *&name, char * /*par*/) {
276     if(exec) {
277         logln("TestSuite LocalPointerTest: ");
278     }
279     TESTCASE_AUTO_BEGIN;
280     TESTCASE_AUTO(TestLocalPointer);
281     TESTCASE_AUTO(TestLocalPointerMoveSwap);
282     TESTCASE_AUTO(TestLocalArray);
283     TESTCASE_AUTO(TestLocalArrayMoveSwap);
284     TESTCASE_AUTO(TestLocalXyzPointer);
285     TESTCASE_AUTO(TestLocalXyzPointerMoveSwap);
286     TESTCASE_AUTO(TestLocalXyzPointerNull);
287     TESTCASE_AUTO_END;
288 }
289 
290 // Exercise almost every LocalPointer and LocalPointerBase method.
TestLocalPointer()291 void LocalPointerTest::TestLocalPointer() {
292     // constructor
293     LocalPointer<UnicodeString> s(new UnicodeString((UChar32)0x50005));
294     // isNULL(), isValid(), operator==(), operator!=()
295     if(s.isNull() || !s.isValid() || s==NULL || !(s!=NULL)) {
296         errln("LocalPointer constructor or NULL test failure");
297         return;
298     }
299     // getAlias(), operator->, operator*
300     if(s.getAlias()->length()!=2 || s->length()!=2 || (*s).length()!=2) {
301         errln("LocalPointer access failure");
302     }
303     // adoptInstead(), orphan()
304     s.adoptInstead(new UnicodeString((UChar)0xfffc));
305     if(s->length()!=1) {
306         errln("LocalPointer adoptInstead(U+FFFC) failure");
307     }
308     UnicodeString *orphan=s.orphan();
309     if(orphan==NULL || orphan->length()!=1 || s.isValid() || s!=NULL) {
310         errln("LocalPointer orphan() failure");
311     }
312     delete orphan;
313     s.adoptInstead(new UnicodeString());
314     if(s->length()!=0) {
315         errln("LocalPointer adoptInstead(empty) failure");
316     }
317 
318     // LocalPointer(p, errorCode) sets U_MEMORY_ALLOCATION_ERROR if p==NULL.
319     UErrorCode errorCode = U_ZERO_ERROR;
320     LocalPointer<CharString> cs(new CharString("some chars", errorCode), errorCode);
321     if(cs.isNull() && U_SUCCESS(errorCode)) {
322         errln("LocalPointer(p, errorCode) failure");
323         return;
324     }
325     errorCode = U_ZERO_ERROR;
326     cs.adoptInsteadAndCheckErrorCode(new CharString("different chars", errorCode), errorCode);
327     if(cs.isNull() && U_SUCCESS(errorCode)) {
328         errln("adoptInsteadAndCheckErrorCode(p, errorCode) failure");
329         return;
330     }
331     // Incoming failure: Keep the current object and delete the input object.
332     errorCode = U_ILLEGAL_ARGUMENT_ERROR;
333     cs.adoptInsteadAndCheckErrorCode(new CharString("unused", errorCode), errorCode);
334     if(cs.isValid() && strcmp(cs->data(), "different chars") != 0) {
335         errln("adoptInsteadAndCheckErrorCode(p, U_FAILURE) did not retain the old object");
336         return;
337     }
338     errorCode = U_ZERO_ERROR;
339     cs.adoptInsteadAndCheckErrorCode(NULL, errorCode);
340     if(errorCode != U_MEMORY_ALLOCATION_ERROR) {
341         errln("adoptInsteadAndCheckErrorCode(NULL, errorCode) did not set U_MEMORY_ALLOCATION_ERROR");
342         return;
343     }
344     if(cs.isValid()) {
345         errln("adoptInsteadAndCheckErrorCode(NULL, errorCode) kept the object");
346         return;
347     }
348     errorCode = U_ZERO_ERROR;
349     LocalPointer<CharString> null(NULL, errorCode);
350     if(errorCode != U_MEMORY_ALLOCATION_ERROR) {
351         errln("LocalPointer(NULL, errorCode) did not set U_MEMORY_ALLOCATION_ERROR");
352         return;
353     }
354 
355     // destructor
356 }
357 
TestLocalPointerMoveSwap()358 void LocalPointerTest::TestLocalPointerMoveSwap() {
359     UnicodeString *p1 = new UnicodeString((UChar)0x61);
360     UnicodeString *p2 = new UnicodeString((UChar)0x62);
361     LocalPointer<UnicodeString> s1(p1);
362     LocalPointer<UnicodeString> s2(p2);
363     s1.swap(s2);
364     if(s1.getAlias() != p2 || s2.getAlias() != p1) {
365         errln("LocalPointer.swap() did not swap");
366     }
367     swap(s1, s2);
368     if(s1.getAlias() != p1 || s2.getAlias() != p2) {
369         errln("swap(LocalPointer) did not swap back");
370     }
371     LocalPointer<UnicodeString> s3;
372     s3.moveFrom(s1);
373     if(s3.getAlias() != p1 || s1.isValid()) {
374         errln("LocalPointer.moveFrom() did not move");
375     }
376 #if U_HAVE_RVALUE_REFERENCES
377     infoln("TestLocalPointerMoveSwap() with rvalue references");
378     s1 = static_cast<LocalPointer<UnicodeString> &&>(s3);
379     if(s1.getAlias() != p1 || s3.isValid()) {
380         errln("LocalPointer move assignment operator did not move");
381     }
382     LocalPointer<UnicodeString> s4(static_cast<LocalPointer<UnicodeString> &&>(s2));
383     if(s4.getAlias() != p2 || s2.isValid()) {
384         errln("LocalPointer move constructor did not move");
385     }
386 #else
387     infoln("TestLocalPointerMoveSwap() without rvalue references");
388 #endif
389 
390     // Move self assignment leaves the object valid but in an undefined state.
391     // Do it to make sure there is no crash,
392     // but do not check for any particular resulting value.
393     s1.moveFrom(s1);
394     s3.moveFrom(s3);
395 }
396 
397 // Exercise almost every LocalArray method (but not LocalPointerBase).
TestLocalArray()398 void LocalPointerTest::TestLocalArray() {
399     // constructor
400     LocalArray<UnicodeString> a(new UnicodeString[2]);
401     // operator[]()
402     a[0].append((UChar)0x61);
403     a[1].append((UChar32)0x60006);
404     if(a[0].length()!=1 || a[1].length()!=2) {
405         errln("LocalArray access failure");
406     }
407     // adoptInstead()
408     a.adoptInstead(new UnicodeString[4]);
409     a[3].append((UChar)0x62).append((UChar)0x63).reverse();
410     if(a[3].length()!=2 || a[3][1]!=0x62) {
411         errln("LocalArray adoptInstead() failure");
412     }
413 
414     // LocalArray(p, errorCode) sets U_MEMORY_ALLOCATION_ERROR if p==NULL.
415     UErrorCode errorCode = U_ZERO_ERROR;
416     LocalArray<UnicodeString> ua(new UnicodeString[3], errorCode);
417     if(ua.isNull() && U_SUCCESS(errorCode)) {
418         errln("LocalArray(p, errorCode) failure");
419         return;
420     }
421     errorCode = U_ZERO_ERROR;
422     UnicodeString *u4 = new UnicodeString[4];
423     ua.adoptInsteadAndCheckErrorCode(u4, errorCode);
424     if(ua.isNull() && U_SUCCESS(errorCode)) {
425         errln("adoptInsteadAndCheckErrorCode(p, errorCode) failure");
426         return;
427     }
428     // Incoming failure: Keep the current object and delete the input object.
429     errorCode = U_ILLEGAL_ARGUMENT_ERROR;
430     ua.adoptInsteadAndCheckErrorCode(new UnicodeString[5], errorCode);
431     if(ua.isValid() && ua.getAlias() != u4) {
432         errln("adoptInsteadAndCheckErrorCode(p, U_FAILURE) did not retain the old array");
433         return;
434     }
435     errorCode = U_ZERO_ERROR;
436     ua.adoptInsteadAndCheckErrorCode(NULL, errorCode);
437     if(errorCode != U_MEMORY_ALLOCATION_ERROR) {
438         errln("adoptInsteadAndCheckErrorCode(NULL, errorCode) did not set U_MEMORY_ALLOCATION_ERROR");
439         return;
440     }
441     if(ua.isValid()) {
442         errln("adoptInsteadAndCheckErrorCode(NULL, errorCode) kept the array");
443         return;
444     }
445     errorCode = U_ZERO_ERROR;
446     LocalArray<UnicodeString> null(NULL, errorCode);
447     if(errorCode != U_MEMORY_ALLOCATION_ERROR) {
448         errln("LocalArray(NULL, errorCode) did not set U_MEMORY_ALLOCATION_ERROR");
449         return;
450     }
451 
452     // destructor
453 }
454 
TestLocalArrayMoveSwap()455 void LocalPointerTest::TestLocalArrayMoveSwap() {
456     UnicodeString *p1 = new UnicodeString[2];
457     UnicodeString *p2 = new UnicodeString[3];
458     LocalArray<UnicodeString> a1(p1);
459     LocalArray<UnicodeString> a2(p2);
460     a1.swap(a2);
461     if(a1.getAlias() != p2 || a2.getAlias() != p1) {
462         errln("LocalArray.swap() did not swap");
463     }
464     swap(a1, a2);
465     if(a1.getAlias() != p1 || a2.getAlias() != p2) {
466         errln("swap(LocalArray) did not swap back");
467     }
468     LocalArray<UnicodeString> a3;
469     a3.moveFrom(a1);
470     if(a3.getAlias() != p1 || a1.isValid()) {
471         errln("LocalArray.moveFrom() did not move");
472     }
473 #if U_HAVE_RVALUE_REFERENCES
474     infoln("TestLocalArrayMoveSwap() with rvalue references");
475     a1 = static_cast<LocalArray<UnicodeString> &&>(a3);
476     if(a1.getAlias() != p1 || a3.isValid()) {
477         errln("LocalArray move assignment operator did not move");
478     }
479     LocalArray<UnicodeString> a4(static_cast<LocalArray<UnicodeString> &&>(a2));
480     if(a4.getAlias() != p2 || a2.isValid()) {
481         errln("LocalArray move constructor did not move");
482     }
483 #else
484     infoln("TestLocalArrayMoveSwap() without rvalue references");
485 #endif
486 
487     // Move self assignment leaves the object valid but in an undefined state.
488     // Do it to make sure there is no crash,
489     // but do not check for any particular resulting value.
490     a1.moveFrom(a1);
491     a3.moveFrom(a3);
492 }
493 
494 #include "unicode/ucnvsel.h"
495 #include "unicode/ucal.h"
496 #include "unicode/udatpg.h"
497 #include "unicode/uidna.h"
498 #include "unicode/uldnames.h"
499 #include "unicode/umsg.h"
500 #include "unicode/unorm2.h"
501 #include "unicode/uregex.h"
502 #include "unicode/utrans.h"
503 
504 // Use LocalXyzPointer types that are not covered elsewhere in the intltest suite.
TestLocalXyzPointer()505 void LocalPointerTest::TestLocalXyzPointer() {
506     IcuTestErrorCode errorCode(*this, "TestLocalXyzPointer");
507 
508     static const char *const encoding="ISO-8859-1";
509     LocalUConverterSelectorPointer sel(
510         ucnvsel_open(&encoding, 1, NULL, UCNV_ROUNDTRIP_SET, errorCode));
511     if(errorCode.logIfFailureAndReset("ucnvsel_open()")) {
512         return;
513     }
514     if(sel.isNull()) {
515         errln("LocalUConverterSelectorPointer failure");
516         return;
517     }
518 
519 #if !UCONFIG_NO_FORMATTING
520     LocalUCalendarPointer cal(ucal_open(NULL, 0, "root", UCAL_GREGORIAN, errorCode));
521     if(errorCode.logDataIfFailureAndReset("ucal_open()")) {
522         return;
523     }
524     if(cal.isNull()) {
525         errln("LocalUCalendarPointer failure");
526         return;
527     }
528 
529     LocalUDateTimePatternGeneratorPointer patgen(udatpg_open("root", errorCode));
530     if(errorCode.logDataIfFailureAndReset("udatpg_open()")) {
531         return;
532     }
533     if(patgen.isNull()) {
534         errln("LocalUDateTimePatternGeneratorPointer failure");
535         return;
536     }
537 
538     LocalULocaleDisplayNamesPointer ldn(uldn_open("de-CH", ULDN_STANDARD_NAMES, errorCode));
539     if(errorCode.logIfFailureAndReset("uldn_open()")) {
540         return;
541     }
542     if(ldn.isNull()) {
543         errln("LocalULocaleDisplayNamesPointer failure");
544         return;
545     }
546 
547     UnicodeString hello=UNICODE_STRING_SIMPLE("Hello {0}!");
548     LocalUMessageFormatPointer msg(
549         umsg_open(hello.getBuffer(), hello.length(), "root", NULL, errorCode));
550     if(errorCode.logIfFailureAndReset("umsg_open()")) {
551         return;
552     }
553     if(msg.isNull()) {
554         errln("LocalUMessageFormatPointer failure");
555         return;
556     }
557 #endif  /* UCONFIG_NO_FORMATTING  */
558 
559 #if !UCONFIG_NO_NORMALIZATION
560     const UNormalizer2 *nfc=unorm2_getNFCInstance(errorCode);
561     UnicodeSet emptySet;
562     LocalUNormalizer2Pointer fn2(unorm2_openFiltered(nfc, emptySet.toUSet(), errorCode));
563     if(errorCode.logIfFailureAndReset("unorm2_openFiltered()")) {
564         return;
565     }
566     if(fn2.isNull()) {
567         errln("LocalUNormalizer2Pointer failure");
568         return;
569     }
570 #endif /* !UCONFIG_NO_NORMALIZATION */
571 
572 #if !UCONFIG_NO_IDNA
573     LocalUIDNAPointer idna(uidna_openUTS46(0, errorCode));
574     if(errorCode.logIfFailureAndReset("uidna_openUTS46()")) {
575         return;
576     }
577     if(idna.isNull()) {
578         errln("LocalUIDNAPointer failure");
579         return;
580     }
581 #endif  /* !UCONFIG_NO_IDNA */
582 
583 #if !UCONFIG_NO_REGULAR_EXPRESSIONS
584     UnicodeString pattern=UNICODE_STRING_SIMPLE("abc|xy+z");
585     LocalURegularExpressionPointer regex(
586         uregex_open(pattern.getBuffer(), pattern.length(), 0, NULL, errorCode));
587     if(errorCode.logIfFailureAndReset("uregex_open()")) {
588         return;
589     }
590     if(regex.isNull()) {
591         errln("LocalURegularExpressionPointer failure");
592         return;
593     }
594 #endif /* UCONFIG_NO_REGULAR_EXPRESSIONS */
595 
596 #if !UCONFIG_NO_TRANSLITERATION
597     UnicodeString id=UNICODE_STRING_SIMPLE("Grek-Latn");
598     LocalUTransliteratorPointer trans(
599         utrans_openU(id.getBuffer(), id.length(), UTRANS_FORWARD, NULL, 0, NULL, errorCode));
600     if(errorCode.logIfFailureAndReset("utrans_open()")) {
601         return;
602     }
603     if(trans.isNull()) {
604         errln("LocalUTransliteratorPointer failure");
605         return;
606     }
607 #endif /* !UCONFIG_NO_TRANSLITERATION */
608 
609     // destructors
610 }
611 
TestLocalXyzPointerMoveSwap()612 void LocalPointerTest::TestLocalXyzPointerMoveSwap() {
613 #if !UCONFIG_NO_NORMALIZATION
614     IcuTestErrorCode errorCode(*this, "TestLocalXyzPointerMoveSwap");
615     const UNormalizer2 *nfc=unorm2_getNFCInstance(errorCode);
616     const UNormalizer2 *nfd=unorm2_getNFDInstance(errorCode);
617     if(errorCode.logIfFailureAndReset("unorm2_getNF[CD]Instance()")) {
618         return;
619     }
620     UnicodeSet emptySet;
621     UNormalizer2 *p1 = unorm2_openFiltered(nfc, emptySet.toUSet(), errorCode);
622     UNormalizer2 *p2 = unorm2_openFiltered(nfd, emptySet.toUSet(), errorCode);
623     LocalUNormalizer2Pointer f1(p1);
624     LocalUNormalizer2Pointer f2(p2);
625     if(errorCode.logIfFailureAndReset("unorm2_openFiltered()")) {
626         return;
627     }
628     if(f1.isNull() || f2.isNull()) {
629         errln("LocalUNormalizer2Pointer failure");
630         return;
631     }
632     f1.swap(f2);
633     if(f1.getAlias() != p2 || f2.getAlias() != p1) {
634         errln("LocalUNormalizer2Pointer.swap() did not swap");
635     }
636     swap(f1, f2);
637     if(f1.getAlias() != p1 || f2.getAlias() != p2) {
638         errln("swap(LocalUNormalizer2Pointer) did not swap back");
639     }
640     LocalUNormalizer2Pointer f3;
641     f3.moveFrom(f1);
642     if(f3.getAlias() != p1 || f1.isValid()) {
643         errln("LocalUNormalizer2Pointer.moveFrom() did not move");
644     }
645 #if U_HAVE_RVALUE_REFERENCES
646     infoln("TestLocalXyzPointerMoveSwap() with rvalue references");
647     f1 = static_cast<LocalUNormalizer2Pointer &&>(f3);
648     if(f1.getAlias() != p1 || f3.isValid()) {
649         errln("LocalUNormalizer2Pointer move assignment operator did not move");
650     }
651     LocalUNormalizer2Pointer f4(static_cast<LocalUNormalizer2Pointer &&>(f2));
652     if(f4.getAlias() != p2 || f2.isValid()) {
653         errln("LocalUNormalizer2Pointer move constructor did not move");
654     }
655 #else
656     infoln("TestLocalXyzPointerMoveSwap() without rvalue references");
657 #endif
658     // Move self assignment leaves the object valid but in an undefined state.
659     // Do it to make sure there is no crash,
660     // but do not check for any particular resulting value.
661     f1.moveFrom(f1);
662     f3.moveFrom(f3);
663 #endif /* !UCONFIG_NO_NORMALIZATION */
664 }
665 
666 // Try LocalXyzPointer types with NULL pointers.
TestLocalXyzPointerNull()667 void LocalPointerTest::TestLocalXyzPointerNull() {
668     {
669         IcuTestErrorCode errorCode(*this, "TestLocalXyzPointerNull/LocalUConverterSelectorPointer");
670         static const char *const encoding="ISO-8859-1";
671         LocalUConverterSelectorPointer null;
672         LocalUConverterSelectorPointer sel(
673             ucnvsel_open(&encoding, 1, NULL, UCNV_ROUNDTRIP_SET, errorCode));
674         sel.adoptInstead(NULL);
675     }
676 #if !UCONFIG_NO_FORMATTING
677     {
678         IcuTestErrorCode errorCode(*this, "TestLocalXyzPointerNull/LocalUCalendarPointer");
679         LocalUCalendarPointer null;
680         LocalUCalendarPointer cal(ucal_open(NULL, 0, "root", UCAL_GREGORIAN, errorCode));
681         if(!errorCode.logDataIfFailureAndReset("ucal_open()")) {
682             cal.adoptInstead(NULL);
683         }
684     }
685     {
686         IcuTestErrorCode errorCode(*this, "TestLocalXyzPointerNull/LocalUDateTimePatternGeneratorPointer");
687         LocalUDateTimePatternGeneratorPointer null;
688         LocalUDateTimePatternGeneratorPointer patgen(udatpg_open("root", errorCode));
689         patgen.adoptInstead(NULL);
690     }
691     {
692         IcuTestErrorCode errorCode(*this, "TestLocalXyzPointerNull/LocalUMessageFormatPointer");
693         UnicodeString hello=UNICODE_STRING_SIMPLE("Hello {0}!");
694         LocalUMessageFormatPointer null;
695         LocalUMessageFormatPointer msg(
696             umsg_open(hello.getBuffer(), hello.length(), "root", NULL, errorCode));
697         msg.adoptInstead(NULL);
698     }
699 #endif /* !UCONFIG_NO_FORMATTING */
700 
701 #if !UCONFIG_NO_REGULAR_EXPRESSIONS
702     {
703         IcuTestErrorCode errorCode(*this, "TestLocalXyzPointerNull/LocalURegularExpressionPointer");
704         UnicodeString pattern=UNICODE_STRING_SIMPLE("abc|xy+z");
705         LocalURegularExpressionPointer null;
706         LocalURegularExpressionPointer regex(
707             uregex_open(pattern.getBuffer(), pattern.length(), 0, NULL, errorCode));
708         if(!errorCode.logDataIfFailureAndReset("urege_open()")) {
709             regex.adoptInstead(NULL);
710         }
711     }
712 #endif /* !UCONFIG_NO_REGULAR_EXPRESSIONS */
713 
714 #if !UCONFIG_NO_TRANSLITERATION
715     {
716         IcuTestErrorCode errorCode(*this, "TestLocalXyzPointerNull/LocalUTransliteratorPointer");
717         UnicodeString id=UNICODE_STRING_SIMPLE("Grek-Latn");
718         LocalUTransliteratorPointer null;
719         LocalUTransliteratorPointer trans(
720             utrans_openU(id.getBuffer(), id.length(), UTRANS_FORWARD, NULL, 0, NULL, errorCode));
721         if(!errorCode.logDataIfFailureAndReset("utrans_openU()")) {
722             trans.adoptInstead(NULL);
723         }
724     }
725 #endif /* !UCONFIG_NO_TRANSLITERATION */
726 
727 }
728 
729 /** EnumSet test **/
730 #include "unicode/enumset.h"
731 
732 class EnumSetTest : public IntlTest {
733 public:
EnumSetTest()734   EnumSetTest() {}
735   virtual void runIndexedTest(int32_t index, UBool exec, const char *&name, char *par=NULL);
736   void TestEnumSet();
737 };
738 
createEnumSetTest()739 static IntlTest *createEnumSetTest() {
740     return new EnumSetTest();
741 }
742 
runIndexedTest(int32_t index,UBool exec,const char * & name,char *)743 void EnumSetTest::runIndexedTest(int32_t index, UBool exec, const char *&name, char * /*par*/) {
744   TESTCASE_AUTO_BEGIN;
745   TESTCASE_AUTO(TestEnumSet);
746   TESTCASE_AUTO_END;
747 }
748 enum myEnum {
749     MAX_NONBOOLEAN=-1,
750     THING1,
751     THING2,
752     THING3,
753     LIMIT_BOOLEAN
754 };
755 
TestEnumSet()756 void EnumSetTest::TestEnumSet() {
757     EnumSet<myEnum,
758             MAX_NONBOOLEAN+1,
759             LIMIT_BOOLEAN>
760                             flags;
761 
762     logln("Enum is from [%d..%d]\n", MAX_NONBOOLEAN+1,
763           LIMIT_BOOLEAN);
764 
765     TEST_ASSERT_TRUE(flags.get(THING1) == FALSE);
766     TEST_ASSERT_TRUE(flags.get(THING2) == FALSE);
767     TEST_ASSERT_TRUE(flags.get(THING3) == FALSE);
768 
769     logln("get(thing1)=%d, get(thing2)=%d, get(thing3)=%d\n",          flags.get(THING1),          flags.get(THING2),          flags.get(THING3));
770     logln("Value now: %d\n", flags.getAll());
771     flags.clear();
772     logln("clear -Value now: %d\n", flags.getAll());
773     logln("get(thing1)=%d, get(thing2)=%d, get(thing3)=%d\n",          flags.get(THING1),          flags.get(THING2),          flags.get(THING3));
774     TEST_ASSERT_TRUE(flags.get(THING1) == FALSE);
775     TEST_ASSERT_TRUE(flags.get(THING2) == FALSE);
776     TEST_ASSERT_TRUE(flags.get(THING3) == FALSE);
777     flags.add(THING1);
778     logln("set THING1 -Value now: %d\n", flags.getAll());
779     TEST_ASSERT_TRUE(flags.get(THING1) == TRUE);
780     TEST_ASSERT_TRUE(flags.get(THING2) == FALSE);
781     TEST_ASSERT_TRUE(flags.get(THING3) == FALSE);
782     logln("get(thing1)=%d, get(thing2)=%d, get(thing3)=%d\n",          flags.get(THING1),          flags.get(THING2),          flags.get(THING3));
783     flags.add(THING3);
784     logln("set THING3 -Value now: %d\n", flags.getAll());
785     TEST_ASSERT_TRUE(flags.get(THING1) == TRUE);
786     TEST_ASSERT_TRUE(flags.get(THING2) == FALSE);
787     TEST_ASSERT_TRUE(flags.get(THING3) == TRUE);
788     logln("get(thing1)=%d, get(thing2)=%d, get(thing3)=%d\n",          flags.get(THING1),          flags.get(THING2),          flags.get(THING3));
789     flags.remove(THING2);
790     TEST_ASSERT_TRUE(flags.get(THING1) == TRUE);
791     TEST_ASSERT_TRUE(flags.get(THING2) == FALSE);
792     TEST_ASSERT_TRUE(flags.get(THING3) == TRUE);
793     logln("remove THING2 -Value now: %d\n", flags.getAll());
794     logln("get(thing1)=%d, get(thing2)=%d, get(thing3)=%d\n",          flags.get(THING1),          flags.get(THING2),          flags.get(THING3));
795     flags.remove(THING1);
796     TEST_ASSERT_TRUE(flags.get(THING1) == FALSE);
797     TEST_ASSERT_TRUE(flags.get(THING2) == FALSE);
798     TEST_ASSERT_TRUE(flags.get(THING3) == TRUE);
799     logln("remove THING1 -Value now: %d\n", flags.getAll());
800     logln("get(thing1)=%d, get(thing2)=%d, get(thing3)=%d\n",          flags.get(THING1),          flags.get(THING2),          flags.get(THING3));
801 
802     flags.clear();
803     logln("clear -Value now: %d\n", flags.getAll());
804     logln("get(thing1)=%d, get(thing2)=%d, get(thing3)=%d\n",          flags.get(THING1),          flags.get(THING2),          flags.get(THING3));
805     TEST_ASSERT_TRUE(flags.get(THING1) == FALSE);
806     TEST_ASSERT_TRUE(flags.get(THING2) == FALSE);
807     TEST_ASSERT_TRUE(flags.get(THING3) == FALSE);
808 }
809