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