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