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