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