]> git.saurik.com Git - apple/icu.git/blame - icuSources/test/intltest/itutil.cpp
ICU-59180.0.1.tar.gz
[apple/icu.git] / icuSources / test / intltest / itutil.cpp
CommitLineData
f3c0d7a5
A
1// © 2016 and later: Unicode, Inc. and others.
2// License & terms of use: http://www.unicode.org/copyright.html
b75a7d8f
A
3/********************************************************************
4 * COPYRIGHT:
2ca993e8 5 * Copyright (c) 1997-2016, International Business Machines Corporation and
b75a7d8f
A
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"
729e4ab9
A
15#include "unicode/errorcode.h"
16#include "unicode/localpointer.h"
b331163b 17#include "charstr.h"
b75a7d8f
A
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"
b75a7d8f
A
26#include "tsmthred.h"
27#include "tsputil.h"
28#include "uobjtest.h"
73c04bcf 29#include "utxttest.h"
374ca955 30#include "v32test.h"
73c04bcf
A
31#include "uvectest.h"
32#include "aliastst.h"
46f4442e
A
33#include "usettest.h"
34
4388f060 35extern IntlTest *createBytesTrieTest();
729e4ab9 36static IntlTest *createLocalPointerTest();
4388f060 37extern IntlTest *createUCharsTrieTest();
51004dcb 38static IntlTest *createEnumSetTest();
2ca993e8 39extern IntlTest *createSimpleFormatterTest();
b331163b
A
40extern IntlTest *createUnifiedCacheTest();
41extern IntlTest *createQuantityFormatterTest();
2ca993e8
A
42extern IntlTest *createPluralMapTest();
43
46f4442e
A
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
b75a7d8f
A
53
54void IntlTestUtilities::runIndexedTest( int32_t index, UBool exec, const char* &name, char* par )
55{
56 if (exec) logln("TestSuite Utilities: ");
57 switch (index) {
729e4ab9
A
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;
4388f060
A
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;
51004dcb
A
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;
57a6839d 106 case 20:
2ca993e8 107 name = "SimpleFormatterTest";
57a6839d 108 if (exec) {
2ca993e8
A
109 logln("TestSuite SimpleFormatterTest---"); logln();
110 LocalPointer<IntlTest> test(createSimpleFormatterTest());
57a6839d
A
111 callTest(*test, par);
112 }
113 break;
114 case 21:
b331163b 115 name = "UnifiedCacheTest";
57a6839d 116 if (exec) {
b331163b
A
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());
57a6839d
A
127 callTest(*test, par);
128 }
129 break;
2ca993e8
A
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;
b75a7d8f
A
138 default: name = ""; break; //needed to end loop
139 }
140}
141
729e4ab9
A
142void 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
151static void RefPlusOne(UErrorCode &code) { code=(UErrorCode)(code+1); }
152static void PtrPlusTwo(UErrorCode *code) { *code=(UErrorCode)(*code+2); }
153
154void 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
183class MyErrorCode: public ErrorCode {
184public:
185 MyErrorCode(int32_t &countChecks, int32_t &countDests)
186 : checks(countChecks), dests(countDests) {}
187 ~MyErrorCode() {
188 if(isFailure()) {
189 ++dests;
190 }
191 }
192private:
193 virtual void handleFailure() const {
194 ++checks;
195 }
196 int32_t &checks;
197 int32_t &dests;
198};
199
200void 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
258class LocalPointerTest : public IntlTest {
259public:
260 LocalPointerTest() {}
261
262 void runIndexedTest(int32_t index, UBool exec, const char *&name, char *par=NULL);
263
264 void TestLocalPointer();
2ca993e8 265 void TestLocalPointerMoveSwap();
729e4ab9 266 void TestLocalArray();
2ca993e8 267 void TestLocalArrayMoveSwap();
729e4ab9 268 void TestLocalXyzPointer();
2ca993e8 269 void TestLocalXyzPointerMoveSwap();
729e4ab9
A
270 void TestLocalXyzPointerNull();
271};
272
273static IntlTest *createLocalPointerTest() {
274 return new LocalPointerTest();
275}
276
277void LocalPointerTest::runIndexedTest(int32_t index, UBool exec, const char *&name, char * /*par*/) {
278 if(exec) {
279 logln("TestSuite LocalPointerTest: ");
280 }
b331163b
A
281 TESTCASE_AUTO_BEGIN;
282 TESTCASE_AUTO(TestLocalPointer);
2ca993e8 283 TESTCASE_AUTO(TestLocalPointerMoveSwap);
b331163b 284 TESTCASE_AUTO(TestLocalArray);
2ca993e8 285 TESTCASE_AUTO(TestLocalArrayMoveSwap);
b331163b 286 TESTCASE_AUTO(TestLocalXyzPointer);
2ca993e8 287 TESTCASE_AUTO(TestLocalXyzPointerMoveSwap);
b331163b
A
288 TESTCASE_AUTO(TestLocalXyzPointerNull);
289 TESTCASE_AUTO_END;
729e4ab9
A
290}
291
2ca993e8 292// Exercise almost every LocalPointer and LocalPointerBase method.
729e4ab9
A
293void 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;
729e4ab9
A
315 s.adoptInstead(new UnicodeString());
316 if(s->length()!=0) {
317 errln("LocalPointer adoptInstead(empty) failure");
318 }
b331163b
A
319
320 // LocalPointer(p, errorCode) sets U_MEMORY_ALLOCATION_ERROR if p==NULL.
321 UErrorCode errorCode = U_ZERO_ERROR;
2ca993e8
A
322 LocalPointer<CharString> csx(new CharString("some chars", errorCode), errorCode);
323 if(csx.isNull() && U_SUCCESS(errorCode)) {
b331163b
A
324 errln("LocalPointer(p, errorCode) failure");
325 return;
326 }
327 errorCode = U_ZERO_ERROR;
2ca993e8
A
328 csx.adoptInsteadAndCheckErrorCode(new CharString("different chars", errorCode), errorCode);
329 if(csx.isNull() && U_SUCCESS(errorCode)) {
b331163b
A
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;
2ca993e8
A
335 csx.adoptInsteadAndCheckErrorCode(new CharString("unused", errorCode), errorCode);
336 if(csx.isValid() && strcmp(csx->data(), "different chars") != 0) {
b331163b
A
337 errln("adoptInsteadAndCheckErrorCode(p, U_FAILURE) did not retain the old object");
338 return;
339 }
340 errorCode = U_ZERO_ERROR;
2ca993e8 341 csx.adoptInsteadAndCheckErrorCode(NULL, errorCode);
b331163b
A
342 if(errorCode != U_MEMORY_ALLOCATION_ERROR) {
343 errln("adoptInsteadAndCheckErrorCode(NULL, errorCode) did not set U_MEMORY_ALLOCATION_ERROR");
344 return;
345 }
2ca993e8 346 if(csx.isValid()) {
b331163b
A
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 }
2ca993e8
A
356
357 // destructor
358}
359
360void 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#if U_HAVE_RVALUE_REFERENCES
379 infoln("TestLocalPointerMoveSwap() with rvalue references");
380 s1 = static_cast<LocalPointer<UnicodeString> &&>(s3);
381 if(s1.getAlias() != p1 || s3.isValid()) {
382 errln("LocalPointer move assignment operator did not move");
383 }
384 LocalPointer<UnicodeString> s4(static_cast<LocalPointer<UnicodeString> &&>(s2));
385 if(s4.getAlias() != p2 || s2.isValid()) {
386 errln("LocalPointer move constructor did not move");
387 }
388#else
389 infoln("TestLocalPointerMoveSwap() without rvalue references");
390#endif
391
392 // Move self assignment leaves the object valid but in an undefined state.
393 // Do it to make sure there is no crash,
394 // but do not check for any particular resulting value.
395 s1.moveFrom(s1);
396 s3.moveFrom(s3);
729e4ab9
A
397}
398
2ca993e8 399// Exercise almost every LocalArray method (but not LocalPointerBase).
729e4ab9
A
400void LocalPointerTest::TestLocalArray() {
401 // constructor
402 LocalArray<UnicodeString> a(new UnicodeString[2]);
403 // operator[]()
404 a[0].append((UChar)0x61);
405 a[1].append((UChar32)0x60006);
406 if(a[0].length()!=1 || a[1].length()!=2) {
407 errln("LocalArray access failure");
408 }
409 // adoptInstead()
410 a.adoptInstead(new UnicodeString[4]);
411 a[3].append((UChar)0x62).append((UChar)0x63).reverse();
412 if(a[3].length()!=2 || a[3][1]!=0x62) {
413 errln("LocalArray adoptInstead() failure");
414 }
2ca993e8
A
415
416 // LocalArray(p, errorCode) sets U_MEMORY_ALLOCATION_ERROR if p==NULL.
417 UErrorCode errorCode = U_ZERO_ERROR;
418 LocalArray<UnicodeString> ua(new UnicodeString[3], errorCode);
419 if(ua.isNull() && U_SUCCESS(errorCode)) {
420 errln("LocalArray(p, errorCode) failure");
421 return;
422 }
423 errorCode = U_ZERO_ERROR;
424 UnicodeString *u4 = new UnicodeString[4];
425 ua.adoptInsteadAndCheckErrorCode(u4, errorCode);
426 if(ua.isNull() && U_SUCCESS(errorCode)) {
427 errln("adoptInsteadAndCheckErrorCode(p, errorCode) failure");
428 return;
429 }
430 // Incoming failure: Keep the current object and delete the input object.
431 errorCode = U_ILLEGAL_ARGUMENT_ERROR;
432 ua.adoptInsteadAndCheckErrorCode(new UnicodeString[5], errorCode);
433 if(ua.isValid() && ua.getAlias() != u4) {
434 errln("adoptInsteadAndCheckErrorCode(p, U_FAILURE) did not retain the old array");
435 return;
436 }
437 errorCode = U_ZERO_ERROR;
438 ua.adoptInsteadAndCheckErrorCode(NULL, errorCode);
439 if(errorCode != U_MEMORY_ALLOCATION_ERROR) {
440 errln("adoptInsteadAndCheckErrorCode(NULL, errorCode) did not set U_MEMORY_ALLOCATION_ERROR");
441 return;
442 }
443 if(ua.isValid()) {
444 errln("adoptInsteadAndCheckErrorCode(NULL, errorCode) kept the array");
445 return;
446 }
447 errorCode = U_ZERO_ERROR;
448 LocalArray<UnicodeString> null(NULL, errorCode);
449 if(errorCode != U_MEMORY_ALLOCATION_ERROR) {
450 errln("LocalArray(NULL, errorCode) did not set U_MEMORY_ALLOCATION_ERROR");
451 return;
452 }
453
729e4ab9
A
454 // destructor
455}
456
2ca993e8
A
457void LocalPointerTest::TestLocalArrayMoveSwap() {
458 UnicodeString *p1 = new UnicodeString[2];
459 UnicodeString *p2 = new UnicodeString[3];
460 LocalArray<UnicodeString> a1(p1);
461 LocalArray<UnicodeString> a2(p2);
462 a1.swap(a2);
463 if(a1.getAlias() != p2 || a2.getAlias() != p1) {
464 errln("LocalArray.swap() did not swap");
465 }
466 swap(a1, a2);
467 if(a1.getAlias() != p1 || a2.getAlias() != p2) {
468 errln("swap(LocalArray) did not swap back");
469 }
470 LocalArray<UnicodeString> a3;
471 a3.moveFrom(a1);
472 if(a3.getAlias() != p1 || a1.isValid()) {
473 errln("LocalArray.moveFrom() did not move");
474 }
475#if U_HAVE_RVALUE_REFERENCES
476 infoln("TestLocalArrayMoveSwap() with rvalue references");
477 a1 = static_cast<LocalArray<UnicodeString> &&>(a3);
478 if(a1.getAlias() != p1 || a3.isValid()) {
479 errln("LocalArray move assignment operator did not move");
480 }
481 LocalArray<UnicodeString> a4(static_cast<LocalArray<UnicodeString> &&>(a2));
482 if(a4.getAlias() != p2 || a2.isValid()) {
483 errln("LocalArray move constructor did not move");
484 }
485#else
486 infoln("TestLocalArrayMoveSwap() without rvalue references");
487#endif
488
489 // Move self assignment leaves the object valid but in an undefined state.
490 // Do it to make sure there is no crash,
491 // but do not check for any particular resulting value.
492 a1.moveFrom(a1);
493 a3.moveFrom(a3);
494}
495
729e4ab9
A
496#include "unicode/ucnvsel.h"
497#include "unicode/ucal.h"
498#include "unicode/udatpg.h"
499#include "unicode/uidna.h"
500#include "unicode/uldnames.h"
501#include "unicode/umsg.h"
502#include "unicode/unorm2.h"
503#include "unicode/uregex.h"
504#include "unicode/utrans.h"
505
506// Use LocalXyzPointer types that are not covered elsewhere in the intltest suite.
507void LocalPointerTest::TestLocalXyzPointer() {
508 IcuTestErrorCode errorCode(*this, "TestLocalXyzPointer");
509
510 static const char *const encoding="ISO-8859-1";
511 LocalUConverterSelectorPointer sel(
512 ucnvsel_open(&encoding, 1, NULL, UCNV_ROUNDTRIP_SET, errorCode));
513 if(errorCode.logIfFailureAndReset("ucnvsel_open()")) {
514 return;
515 }
516 if(sel.isNull()) {
517 errln("LocalUConverterSelectorPointer failure");
518 return;
519 }
520
521#if !UCONFIG_NO_FORMATTING
522 LocalUCalendarPointer cal(ucal_open(NULL, 0, "root", UCAL_GREGORIAN, errorCode));
523 if(errorCode.logDataIfFailureAndReset("ucal_open()")) {
524 return;
525 }
526 if(cal.isNull()) {
527 errln("LocalUCalendarPointer failure");
528 return;
529 }
530
531 LocalUDateTimePatternGeneratorPointer patgen(udatpg_open("root", errorCode));
4388f060 532 if(errorCode.logDataIfFailureAndReset("udatpg_open()")) {
729e4ab9
A
533 return;
534 }
535 if(patgen.isNull()) {
536 errln("LocalUDateTimePatternGeneratorPointer failure");
537 return;
538 }
539
540 LocalULocaleDisplayNamesPointer ldn(uldn_open("de-CH", ULDN_STANDARD_NAMES, errorCode));
541 if(errorCode.logIfFailureAndReset("uldn_open()")) {
542 return;
543 }
544 if(ldn.isNull()) {
545 errln("LocalULocaleDisplayNamesPointer failure");
546 return;
547 }
548
549 UnicodeString hello=UNICODE_STRING_SIMPLE("Hello {0}!");
550 LocalUMessageFormatPointer msg(
551 umsg_open(hello.getBuffer(), hello.length(), "root", NULL, errorCode));
552 if(errorCode.logIfFailureAndReset("umsg_open()")) {
553 return;
554 }
555 if(msg.isNull()) {
556 errln("LocalUMessageFormatPointer failure");
557 return;
558 }
559#endif /* UCONFIG_NO_FORMATTING */
560
561#if !UCONFIG_NO_NORMALIZATION
4388f060 562 const UNormalizer2 *nfc=unorm2_getNFCInstance(errorCode);
729e4ab9
A
563 UnicodeSet emptySet;
564 LocalUNormalizer2Pointer fn2(unorm2_openFiltered(nfc, emptySet.toUSet(), errorCode));
565 if(errorCode.logIfFailureAndReset("unorm2_openFiltered()")) {
566 return;
567 }
568 if(fn2.isNull()) {
569 errln("LocalUNormalizer2Pointer failure");
570 return;
571 }
572#endif /* !UCONFIG_NO_NORMALIZATION */
573
574#if !UCONFIG_NO_IDNA
575 LocalUIDNAPointer idna(uidna_openUTS46(0, errorCode));
576 if(errorCode.logIfFailureAndReset("uidna_openUTS46()")) {
577 return;
578 }
579 if(idna.isNull()) {
580 errln("LocalUIDNAPointer failure");
581 return;
582 }
583#endif /* !UCONFIG_NO_IDNA */
584
585#if !UCONFIG_NO_REGULAR_EXPRESSIONS
586 UnicodeString pattern=UNICODE_STRING_SIMPLE("abc|xy+z");
587 LocalURegularExpressionPointer regex(
588 uregex_open(pattern.getBuffer(), pattern.length(), 0, NULL, errorCode));
589 if(errorCode.logIfFailureAndReset("uregex_open()")) {
590 return;
591 }
592 if(regex.isNull()) {
593 errln("LocalURegularExpressionPointer failure");
594 return;
595 }
596#endif /* UCONFIG_NO_REGULAR_EXPRESSIONS */
597
598#if !UCONFIG_NO_TRANSLITERATION
599 UnicodeString id=UNICODE_STRING_SIMPLE("Grek-Latn");
600 LocalUTransliteratorPointer trans(
601 utrans_openU(id.getBuffer(), id.length(), UTRANS_FORWARD, NULL, 0, NULL, errorCode));
602 if(errorCode.logIfFailureAndReset("utrans_open()")) {
603 return;
604 }
605 if(trans.isNull()) {
606 errln("LocalUTransliteratorPointer failure");
607 return;
608 }
609#endif /* !UCONFIG_NO_TRANSLITERATION */
610
611 // destructors
612}
613
2ca993e8
A
614void LocalPointerTest::TestLocalXyzPointerMoveSwap() {
615#if !UCONFIG_NO_NORMALIZATION
616 IcuTestErrorCode errorCode(*this, "TestLocalXyzPointerMoveSwap");
617 const UNormalizer2 *nfc=unorm2_getNFCInstance(errorCode);
618 const UNormalizer2 *nfd=unorm2_getNFDInstance(errorCode);
619 if(errorCode.logIfFailureAndReset("unorm2_getNF[CD]Instance()")) {
620 return;
621 }
622 UnicodeSet emptySet;
623 UNormalizer2 *p1 = unorm2_openFiltered(nfc, emptySet.toUSet(), errorCode);
624 UNormalizer2 *p2 = unorm2_openFiltered(nfd, emptySet.toUSet(), errorCode);
625 LocalUNormalizer2Pointer f1(p1);
626 LocalUNormalizer2Pointer f2(p2);
627 if(errorCode.logIfFailureAndReset("unorm2_openFiltered()")) {
628 return;
629 }
630 if(f1.isNull() || f2.isNull()) {
631 errln("LocalUNormalizer2Pointer failure");
632 return;
633 }
634 f1.swap(f2);
635 if(f1.getAlias() != p2 || f2.getAlias() != p1) {
636 errln("LocalUNormalizer2Pointer.swap() did not swap");
637 }
638 swap(f1, f2);
639 if(f1.getAlias() != p1 || f2.getAlias() != p2) {
640 errln("swap(LocalUNormalizer2Pointer) did not swap back");
641 }
642 LocalUNormalizer2Pointer f3;
643 f3.moveFrom(f1);
644 if(f3.getAlias() != p1 || f1.isValid()) {
645 errln("LocalUNormalizer2Pointer.moveFrom() did not move");
646 }
647#if U_HAVE_RVALUE_REFERENCES
648 infoln("TestLocalXyzPointerMoveSwap() with rvalue references");
649 f1 = static_cast<LocalUNormalizer2Pointer &&>(f3);
650 if(f1.getAlias() != p1 || f3.isValid()) {
651 errln("LocalUNormalizer2Pointer move assignment operator did not move");
652 }
653 LocalUNormalizer2Pointer f4(static_cast<LocalUNormalizer2Pointer &&>(f2));
654 if(f4.getAlias() != p2 || f2.isValid()) {
655 errln("LocalUNormalizer2Pointer move constructor did not move");
656 }
657#else
658 infoln("TestLocalXyzPointerMoveSwap() without rvalue references");
659#endif
660 // Move self assignment leaves the object valid but in an undefined state.
661 // Do it to make sure there is no crash,
662 // but do not check for any particular resulting value.
663 f1.moveFrom(f1);
664 f3.moveFrom(f3);
665#endif /* !UCONFIG_NO_NORMALIZATION */
666}
667
729e4ab9
A
668// Try LocalXyzPointer types with NULL pointers.
669void LocalPointerTest::TestLocalXyzPointerNull() {
670 {
671 IcuTestErrorCode errorCode(*this, "TestLocalXyzPointerNull/LocalUConverterSelectorPointer");
672 static const char *const encoding="ISO-8859-1";
673 LocalUConverterSelectorPointer null;
674 LocalUConverterSelectorPointer sel(
675 ucnvsel_open(&encoding, 1, NULL, UCNV_ROUNDTRIP_SET, errorCode));
676 sel.adoptInstead(NULL);
677 }
678#if !UCONFIG_NO_FORMATTING
679 {
680 IcuTestErrorCode errorCode(*this, "TestLocalXyzPointerNull/LocalUCalendarPointer");
681 LocalUCalendarPointer null;
682 LocalUCalendarPointer cal(ucal_open(NULL, 0, "root", UCAL_GREGORIAN, errorCode));
683 if(!errorCode.logDataIfFailureAndReset("ucal_open()")) {
684 cal.adoptInstead(NULL);
685 }
686 }
687 {
688 IcuTestErrorCode errorCode(*this, "TestLocalXyzPointerNull/LocalUDateTimePatternGeneratorPointer");
689 LocalUDateTimePatternGeneratorPointer null;
690 LocalUDateTimePatternGeneratorPointer patgen(udatpg_open("root", errorCode));
691 patgen.adoptInstead(NULL);
692 }
693 {
694 IcuTestErrorCode errorCode(*this, "TestLocalXyzPointerNull/LocalUMessageFormatPointer");
695 UnicodeString hello=UNICODE_STRING_SIMPLE("Hello {0}!");
696 LocalUMessageFormatPointer null;
697 LocalUMessageFormatPointer msg(
698 umsg_open(hello.getBuffer(), hello.length(), "root", NULL, errorCode));
699 msg.adoptInstead(NULL);
700 }
701#endif /* !UCONFIG_NO_FORMATTING */
702
703#if !UCONFIG_NO_REGULAR_EXPRESSIONS
704 {
705 IcuTestErrorCode errorCode(*this, "TestLocalXyzPointerNull/LocalURegularExpressionPointer");
706 UnicodeString pattern=UNICODE_STRING_SIMPLE("abc|xy+z");
707 LocalURegularExpressionPointer null;
708 LocalURegularExpressionPointer regex(
709 uregex_open(pattern.getBuffer(), pattern.length(), 0, NULL, errorCode));
710 if(!errorCode.logDataIfFailureAndReset("urege_open()")) {
711 regex.adoptInstead(NULL);
712 }
713 }
714#endif /* !UCONFIG_NO_REGULAR_EXPRESSIONS */
715
716#if !UCONFIG_NO_TRANSLITERATION
717 {
718 IcuTestErrorCode errorCode(*this, "TestLocalXyzPointerNull/LocalUTransliteratorPointer");
719 UnicodeString id=UNICODE_STRING_SIMPLE("Grek-Latn");
720 LocalUTransliteratorPointer null;
721 LocalUTransliteratorPointer trans(
722 utrans_openU(id.getBuffer(), id.length(), UTRANS_FORWARD, NULL, 0, NULL, errorCode));
723 if(!errorCode.logDataIfFailureAndReset("utrans_openU()")) {
724 trans.adoptInstead(NULL);
725 }
726 }
727#endif /* !UCONFIG_NO_TRANSLITERATION */
728
729}
51004dcb
A
730
731/** EnumSet test **/
732#include "unicode/enumset.h"
733
734class EnumSetTest : public IntlTest {
735public:
736 EnumSetTest() {}
737 virtual void runIndexedTest(int32_t index, UBool exec, const char *&name, char *par=NULL);
738 void TestEnumSet();
739};
740
741static IntlTest *createEnumSetTest() {
742 return new EnumSetTest();
743}
744
745void EnumSetTest::runIndexedTest(int32_t index, UBool exec, const char *&name, char * /*par*/) {
746 TESTCASE_AUTO_BEGIN;
747 TESTCASE_AUTO(TestEnumSet);
748 TESTCASE_AUTO_END;
749}
750enum myEnum {
751 MAX_NONBOOLEAN=-1,
752 THING1,
753 THING2,
754 THING3,
755 LIMIT_BOOLEAN
756};
757
758void EnumSetTest::TestEnumSet() {
759 EnumSet<myEnum,
760 MAX_NONBOOLEAN+1,
761 LIMIT_BOOLEAN>
762 flags;
763
764 logln("Enum is from [%d..%d]\n", MAX_NONBOOLEAN+1,
765 LIMIT_BOOLEAN);
766
767 TEST_ASSERT_TRUE(flags.get(THING1) == FALSE);
768 TEST_ASSERT_TRUE(flags.get(THING2) == FALSE);
769 TEST_ASSERT_TRUE(flags.get(THING3) == FALSE);
770
771 logln("get(thing1)=%d, get(thing2)=%d, get(thing3)=%d\n", flags.get(THING1), flags.get(THING2), flags.get(THING3));
772 logln("Value now: %d\n", flags.getAll());
773 flags.clear();
774 logln("clear -Value now: %d\n", flags.getAll());
775 logln("get(thing1)=%d, get(thing2)=%d, get(thing3)=%d\n", flags.get(THING1), flags.get(THING2), flags.get(THING3));
776 TEST_ASSERT_TRUE(flags.get(THING1) == FALSE);
777 TEST_ASSERT_TRUE(flags.get(THING2) == FALSE);
778 TEST_ASSERT_TRUE(flags.get(THING3) == FALSE);
779 flags.add(THING1);
780 logln("set THING1 -Value now: %d\n", flags.getAll());
781 TEST_ASSERT_TRUE(flags.get(THING1) == TRUE);
782 TEST_ASSERT_TRUE(flags.get(THING2) == FALSE);
783 TEST_ASSERT_TRUE(flags.get(THING3) == FALSE);
784 logln("get(thing1)=%d, get(thing2)=%d, get(thing3)=%d\n", flags.get(THING1), flags.get(THING2), flags.get(THING3));
785 flags.add(THING3);
786 logln("set THING3 -Value now: %d\n", flags.getAll());
787 TEST_ASSERT_TRUE(flags.get(THING1) == TRUE);
788 TEST_ASSERT_TRUE(flags.get(THING2) == FALSE);
789 TEST_ASSERT_TRUE(flags.get(THING3) == TRUE);
790 logln("get(thing1)=%d, get(thing2)=%d, get(thing3)=%d\n", flags.get(THING1), flags.get(THING2), flags.get(THING3));
791 flags.remove(THING2);
792 TEST_ASSERT_TRUE(flags.get(THING1) == TRUE);
793 TEST_ASSERT_TRUE(flags.get(THING2) == FALSE);
794 TEST_ASSERT_TRUE(flags.get(THING3) == TRUE);
795 logln("remove THING2 -Value now: %d\n", flags.getAll());
796 logln("get(thing1)=%d, get(thing2)=%d, get(thing3)=%d\n", flags.get(THING1), flags.get(THING2), flags.get(THING3));
797 flags.remove(THING1);
798 TEST_ASSERT_TRUE(flags.get(THING1) == FALSE);
799 TEST_ASSERT_TRUE(flags.get(THING2) == FALSE);
800 TEST_ASSERT_TRUE(flags.get(THING3) == TRUE);
801 logln("remove THING1 -Value now: %d\n", flags.getAll());
802 logln("get(thing1)=%d, get(thing2)=%d, get(thing3)=%d\n", flags.get(THING1), flags.get(THING2), flags.get(THING3));
803
804 flags.clear();
805 logln("clear -Value now: %d\n", flags.getAll());
806 logln("get(thing1)=%d, get(thing2)=%d, get(thing3)=%d\n", flags.get(THING1), flags.get(THING2), flags.get(THING3));
807 TEST_ASSERT_TRUE(flags.get(THING1) == FALSE);
808 TEST_ASSERT_TRUE(flags.get(THING2) == FALSE);
809 TEST_ASSERT_TRUE(flags.get(THING3) == FALSE);
810}