]> git.saurik.com Git - apple/icu.git/blob - icuSources/test/intltest/itutil.cpp
ICU-511.34.tar.gz
[apple/icu.git] / icuSources / test / intltest / itutil.cpp
1 /********************************************************************
2 * COPYRIGHT:
3 * Copyright (c) 1997-2012, International Business Machines Corporation and
4 * others. All Rights Reserved.
5 ********************************************************************/
6
7
8 /**
9 * IntlTestUtilities is the medium level test class for everything in the directory "utility".
10 */
11
12 #include "unicode/utypes.h"
13 #include "unicode/errorcode.h"
14 #include "unicode/localpointer.h"
15 #include "itutil.h"
16 #include "strtest.h"
17 #include "loctest.h"
18 #include "citrtest.h"
19 #include "ustrtest.h"
20 #include "ucdtest.h"
21 #include "restest.h"
22 #include "restsnew.h"
23 #include "tsmthred.h"
24 #include "tsputil.h"
25 #include "uobjtest.h"
26 #include "utxttest.h"
27 #include "v32test.h"
28 #include "uvectest.h"
29 #include "aliastst.h"
30 #include "usettest.h"
31
32 extern IntlTest *createBytesTrieTest();
33 static IntlTest *createLocalPointerTest();
34 extern IntlTest *createUCharsTrieTest();
35 static IntlTest *createEnumSetTest();
36
37 #define CASE(id, test) case id: \
38 name = #test; \
39 if (exec) { \
40 logln(#test "---"); logln(); \
41 test t; \
42 callTest(t, par); \
43 } \
44 break
45
46 void IntlTestUtilities::runIndexedTest( int32_t index, UBool exec, const char* &name, char* par )
47 {
48 if (exec) logln("TestSuite Utilities: ");
49 switch (index) {
50 CASE(0, MultithreadTest);
51 CASE(1, StringTest);
52 CASE(2, UnicodeStringTest);
53 CASE(3, LocaleTest);
54 CASE(4, CharIterTest);
55 CASE(5, UObjectTest);
56 CASE(6, UnicodeTest);
57 CASE(7, ResourceBundleTest);
58 CASE(8, NewResourceBundleTest);
59 CASE(9, PUtilTest);
60 CASE(10, UVector32Test);
61 CASE(11, UVectorTest);
62 CASE(12, UTextTest);
63 CASE(13, LocaleAliasTest);
64 CASE(14, UnicodeSetTest);
65 CASE(15, ErrorCodeTest);
66 case 16:
67 name = "LocalPointerTest";
68 if (exec) {
69 logln("TestSuite LocalPointerTest---"); logln();
70 LocalPointer<IntlTest> test(createLocalPointerTest());
71 callTest(*test, par);
72 }
73 break;
74 case 17:
75 name = "BytesTrieTest";
76 if (exec) {
77 logln("TestSuite BytesTrieTest---"); logln();
78 LocalPointer<IntlTest> test(createBytesTrieTest());
79 callTest(*test, par);
80 }
81 break;
82 case 18:
83 name = "UCharsTrieTest";
84 if (exec) {
85 logln("TestSuite UCharsTrieTest---"); logln();
86 LocalPointer<IntlTest> test(createUCharsTrieTest());
87 callTest(*test, par);
88 }
89 break;
90 case 19:
91 name = "EnumSetTest";
92 if (exec) {
93 logln("TestSuite EnumSetTest---"); logln();
94 LocalPointer<IntlTest> test(createEnumSetTest());
95 callTest(*test, par);
96 }
97 break;
98 default: name = ""; break; //needed to end loop
99 }
100 }
101
102 void ErrorCodeTest::runIndexedTest(int32_t index, UBool exec, const char* &name, char* /*par*/) {
103 if (exec) logln("TestSuite Utilities: ");
104 switch (index) {
105 case 0: name = "TestErrorCode"; if (exec) TestErrorCode(); break;
106 case 1: name = "TestSubclass"; if (exec) TestSubclass(); break;
107 default: name = ""; break; //needed to end loop
108 }
109 }
110
111 static void RefPlusOne(UErrorCode &code) { code=(UErrorCode)(code+1); }
112 static void PtrPlusTwo(UErrorCode *code) { *code=(UErrorCode)(*code+2); }
113
114 void ErrorCodeTest::TestErrorCode() {
115 ErrorCode errorCode;
116 if(errorCode.get()!=U_ZERO_ERROR || !errorCode.isSuccess() || errorCode.isFailure()) {
117 errln("ErrorCode did not initialize properly");
118 return;
119 }
120 errorCode.assertSuccess();
121 if(errorCode.errorName()!=u_errorName(U_ZERO_ERROR)) {
122 errln("ErrorCode did not format error message string properly");
123 }
124 RefPlusOne(errorCode);
125 if(errorCode.get()!=U_ILLEGAL_ARGUMENT_ERROR || errorCode.isSuccess() || !errorCode.isFailure()) {
126 errln("ErrorCode did not yield a writable reference");
127 }
128 PtrPlusTwo(errorCode);
129 if(errorCode.get()!=U_INVALID_FORMAT_ERROR || errorCode.isSuccess() || !errorCode.isFailure()) {
130 errln("ErrorCode did not yield a writable pointer");
131 }
132 errorCode.set(U_PARSE_ERROR);
133 if(errorCode.get()!=U_PARSE_ERROR || errorCode.isSuccess() || !errorCode.isFailure()) {
134 errln("ErrorCode.set() failed");
135 }
136 if( errorCode.reset()!=U_PARSE_ERROR || errorCode.get()!=U_ZERO_ERROR ||
137 !errorCode.isSuccess() || errorCode.isFailure()
138 ) {
139 errln("ErrorCode did not reset properly");
140 }
141 }
142
143 class MyErrorCode: public ErrorCode {
144 public:
145 MyErrorCode(int32_t &countChecks, int32_t &countDests)
146 : checks(countChecks), dests(countDests) {}
147 ~MyErrorCode() {
148 if(isFailure()) {
149 ++dests;
150 }
151 }
152 private:
153 virtual void handleFailure() const {
154 ++checks;
155 }
156 int32_t &checks;
157 int32_t &dests;
158 };
159
160 void ErrorCodeTest::TestSubclass() {
161 int32_t countChecks=0;
162 int32_t countDests=0;
163 {
164 MyErrorCode errorCode(countChecks, countDests);
165 if( errorCode.get()!=U_ZERO_ERROR || !errorCode.isSuccess() || errorCode.isFailure() ||
166 countChecks!=0 || countDests!=0
167 ) {
168 errln("ErrorCode did not initialize properly");
169 return;
170 }
171 errorCode.assertSuccess();
172 if(countChecks!=0) {
173 errln("ErrorCode.assertSuccess() called handleFailure() despite success");
174 }
175 RefPlusOne(errorCode);
176 if(errorCode.get()!=U_ILLEGAL_ARGUMENT_ERROR || errorCode.isSuccess() || !errorCode.isFailure()) {
177 errln("ErrorCode did not yield a writable reference");
178 }
179 errorCode.assertSuccess();
180 if(countChecks!=1) {
181 errln("ErrorCode.assertSuccess() did not handleFailure()");
182 }
183 PtrPlusTwo(errorCode);
184 if(errorCode.get()!=U_INVALID_FORMAT_ERROR || errorCode.isSuccess() || !errorCode.isFailure()) {
185 errln("ErrorCode did not yield a writable pointer");
186 }
187 errorCode.assertSuccess();
188 if(countChecks!=2) {
189 errln("ErrorCode.assertSuccess() did not handleFailure()");
190 }
191 errorCode.set(U_PARSE_ERROR);
192 if(errorCode.get()!=U_PARSE_ERROR || errorCode.isSuccess() || !errorCode.isFailure()) {
193 errln("ErrorCode.set() failed");
194 }
195 if( errorCode.reset()!=U_PARSE_ERROR || errorCode.get()!=U_ZERO_ERROR ||
196 !errorCode.isSuccess() || errorCode.isFailure()
197 ) {
198 errln("ErrorCode did not reset properly");
199 }
200 errorCode.assertSuccess();
201 if(countChecks!=2) {
202 errln("ErrorCode.assertSuccess() called handleFailure() despite success");
203 }
204 }
205 if(countDests!=0) {
206 errln("MyErrorCode destructor detected failure despite success");
207 }
208 countChecks=countDests=0;
209 {
210 MyErrorCode errorCode(countChecks, countDests);
211 errorCode.set(U_PARSE_ERROR);
212 }
213 if(countDests!=1) {
214 errln("MyErrorCode destructor failed to detect failure");
215 }
216 }
217
218 class LocalPointerTest : public IntlTest {
219 public:
220 LocalPointerTest() {}
221
222 void runIndexedTest(int32_t index, UBool exec, const char *&name, char *par=NULL);
223
224 void TestLocalPointer();
225 void TestLocalArray();
226 void TestLocalXyzPointer();
227 void TestLocalXyzPointerNull();
228 };
229
230 static IntlTest *createLocalPointerTest() {
231 return new LocalPointerTest();
232 }
233
234 void LocalPointerTest::runIndexedTest(int32_t index, UBool exec, const char *&name, char * /*par*/) {
235 if(exec) {
236 logln("TestSuite LocalPointerTest: ");
237 }
238 switch (index) {
239 TESTCASE(0, TestLocalPointer);
240 TESTCASE(1, TestLocalArray);
241 TESTCASE(2, TestLocalXyzPointer);
242 TESTCASE(3, TestLocalXyzPointerNull);
243 default:
244 name="";
245 break; // needed to end the loop
246 }
247 }
248
249 // Exercise every LocalPointer and LocalPointerBase method.
250 void LocalPointerTest::TestLocalPointer() {
251 // constructor
252 LocalPointer<UnicodeString> s(new UnicodeString((UChar32)0x50005));
253 // isNULL(), isValid(), operator==(), operator!=()
254 if(s.isNull() || !s.isValid() || s==NULL || !(s!=NULL)) {
255 errln("LocalPointer constructor or NULL test failure");
256 return;
257 }
258 // getAlias(), operator->, operator*
259 if(s.getAlias()->length()!=2 || s->length()!=2 || (*s).length()!=2) {
260 errln("LocalPointer access failure");
261 }
262 // adoptInstead(), orphan()
263 s.adoptInstead(new UnicodeString((UChar)0xfffc));
264 if(s->length()!=1) {
265 errln("LocalPointer adoptInstead(U+FFFC) failure");
266 }
267 UnicodeString *orphan=s.orphan();
268 if(orphan==NULL || orphan->length()!=1 || s.isValid() || s!=NULL) {
269 errln("LocalPointer orphan() failure");
270 }
271 delete orphan;
272 // destructor
273 s.adoptInstead(new UnicodeString());
274 if(s->length()!=0) {
275 errln("LocalPointer adoptInstead(empty) failure");
276 }
277 }
278
279 // Exercise every LocalArray method (but not LocalPointerBase).
280 void LocalPointerTest::TestLocalArray() {
281 // constructor
282 LocalArray<UnicodeString> a(new UnicodeString[2]);
283 // operator[]()
284 a[0].append((UChar)0x61);
285 a[1].append((UChar32)0x60006);
286 if(a[0].length()!=1 || a[1].length()!=2) {
287 errln("LocalArray access failure");
288 }
289 // adoptInstead()
290 a.adoptInstead(new UnicodeString[4]);
291 a[3].append((UChar)0x62).append((UChar)0x63).reverse();
292 if(a[3].length()!=2 || a[3][1]!=0x62) {
293 errln("LocalArray adoptInstead() failure");
294 }
295 // destructor
296 }
297
298 #include "unicode/ucnvsel.h"
299 #include "unicode/ucal.h"
300 #include "unicode/udatpg.h"
301 #include "unicode/uidna.h"
302 #include "unicode/uldnames.h"
303 #include "unicode/umsg.h"
304 #include "unicode/unorm2.h"
305 #include "unicode/uregex.h"
306 #include "unicode/utrans.h"
307
308 // Use LocalXyzPointer types that are not covered elsewhere in the intltest suite.
309 void LocalPointerTest::TestLocalXyzPointer() {
310 IcuTestErrorCode errorCode(*this, "TestLocalXyzPointer");
311
312 static const char *const encoding="ISO-8859-1";
313 LocalUConverterSelectorPointer sel(
314 ucnvsel_open(&encoding, 1, NULL, UCNV_ROUNDTRIP_SET, errorCode));
315 if(errorCode.logIfFailureAndReset("ucnvsel_open()")) {
316 return;
317 }
318 if(sel.isNull()) {
319 errln("LocalUConverterSelectorPointer failure");
320 return;
321 }
322
323 #if !UCONFIG_NO_FORMATTING
324 LocalUCalendarPointer cal(ucal_open(NULL, 0, "root", UCAL_GREGORIAN, errorCode));
325 if(errorCode.logDataIfFailureAndReset("ucal_open()")) {
326 return;
327 }
328 if(cal.isNull()) {
329 errln("LocalUCalendarPointer failure");
330 return;
331 }
332
333 LocalUDateTimePatternGeneratorPointer patgen(udatpg_open("root", errorCode));
334 if(errorCode.logDataIfFailureAndReset("udatpg_open()")) {
335 return;
336 }
337 if(patgen.isNull()) {
338 errln("LocalUDateTimePatternGeneratorPointer failure");
339 return;
340 }
341
342 LocalULocaleDisplayNamesPointer ldn(uldn_open("de-CH", ULDN_STANDARD_NAMES, errorCode));
343 if(errorCode.logIfFailureAndReset("uldn_open()")) {
344 return;
345 }
346 if(ldn.isNull()) {
347 errln("LocalULocaleDisplayNamesPointer failure");
348 return;
349 }
350
351 UnicodeString hello=UNICODE_STRING_SIMPLE("Hello {0}!");
352 LocalUMessageFormatPointer msg(
353 umsg_open(hello.getBuffer(), hello.length(), "root", NULL, errorCode));
354 if(errorCode.logIfFailureAndReset("umsg_open()")) {
355 return;
356 }
357 if(msg.isNull()) {
358 errln("LocalUMessageFormatPointer failure");
359 return;
360 }
361 #endif /* UCONFIG_NO_FORMATTING */
362
363 #if !UCONFIG_NO_NORMALIZATION
364 const UNormalizer2 *nfc=unorm2_getNFCInstance(errorCode);
365 UnicodeSet emptySet;
366 LocalUNormalizer2Pointer fn2(unorm2_openFiltered(nfc, emptySet.toUSet(), errorCode));
367 if(errorCode.logIfFailureAndReset("unorm2_openFiltered()")) {
368 return;
369 }
370 if(fn2.isNull()) {
371 errln("LocalUNormalizer2Pointer failure");
372 return;
373 }
374 #endif /* !UCONFIG_NO_NORMALIZATION */
375
376 #if !UCONFIG_NO_IDNA
377 LocalUIDNAPointer idna(uidna_openUTS46(0, errorCode));
378 if(errorCode.logIfFailureAndReset("uidna_openUTS46()")) {
379 return;
380 }
381 if(idna.isNull()) {
382 errln("LocalUIDNAPointer failure");
383 return;
384 }
385 #endif /* !UCONFIG_NO_IDNA */
386
387 #if !UCONFIG_NO_REGULAR_EXPRESSIONS
388 UnicodeString pattern=UNICODE_STRING_SIMPLE("abc|xy+z");
389 LocalURegularExpressionPointer regex(
390 uregex_open(pattern.getBuffer(), pattern.length(), 0, NULL, errorCode));
391 if(errorCode.logIfFailureAndReset("uregex_open()")) {
392 return;
393 }
394 if(regex.isNull()) {
395 errln("LocalURegularExpressionPointer failure");
396 return;
397 }
398 #endif /* UCONFIG_NO_REGULAR_EXPRESSIONS */
399
400 #if !UCONFIG_NO_TRANSLITERATION
401 UnicodeString id=UNICODE_STRING_SIMPLE("Grek-Latn");
402 LocalUTransliteratorPointer trans(
403 utrans_openU(id.getBuffer(), id.length(), UTRANS_FORWARD, NULL, 0, NULL, errorCode));
404 if(errorCode.logIfFailureAndReset("utrans_open()")) {
405 return;
406 }
407 if(trans.isNull()) {
408 errln("LocalUTransliteratorPointer failure");
409 return;
410 }
411 #endif /* !UCONFIG_NO_TRANSLITERATION */
412
413 // destructors
414 }
415
416 // Try LocalXyzPointer types with NULL pointers.
417 void LocalPointerTest::TestLocalXyzPointerNull() {
418 {
419 IcuTestErrorCode errorCode(*this, "TestLocalXyzPointerNull/LocalUConverterSelectorPointer");
420 static const char *const encoding="ISO-8859-1";
421 LocalUConverterSelectorPointer null;
422 LocalUConverterSelectorPointer sel(
423 ucnvsel_open(&encoding, 1, NULL, UCNV_ROUNDTRIP_SET, errorCode));
424 sel.adoptInstead(NULL);
425 }
426 #if !UCONFIG_NO_FORMATTING
427 {
428 IcuTestErrorCode errorCode(*this, "TestLocalXyzPointerNull/LocalUCalendarPointer");
429 LocalUCalendarPointer null;
430 LocalUCalendarPointer cal(ucal_open(NULL, 0, "root", UCAL_GREGORIAN, errorCode));
431 if(!errorCode.logDataIfFailureAndReset("ucal_open()")) {
432 cal.adoptInstead(NULL);
433 }
434 }
435 {
436 IcuTestErrorCode errorCode(*this, "TestLocalXyzPointerNull/LocalUDateTimePatternGeneratorPointer");
437 LocalUDateTimePatternGeneratorPointer null;
438 LocalUDateTimePatternGeneratorPointer patgen(udatpg_open("root", errorCode));
439 patgen.adoptInstead(NULL);
440 }
441 {
442 IcuTestErrorCode errorCode(*this, "TestLocalXyzPointerNull/LocalUMessageFormatPointer");
443 UnicodeString hello=UNICODE_STRING_SIMPLE("Hello {0}!");
444 LocalUMessageFormatPointer null;
445 LocalUMessageFormatPointer msg(
446 umsg_open(hello.getBuffer(), hello.length(), "root", NULL, errorCode));
447 msg.adoptInstead(NULL);
448 }
449 #endif /* !UCONFIG_NO_FORMATTING */
450
451 #if !UCONFIG_NO_REGULAR_EXPRESSIONS
452 {
453 IcuTestErrorCode errorCode(*this, "TestLocalXyzPointerNull/LocalURegularExpressionPointer");
454 UnicodeString pattern=UNICODE_STRING_SIMPLE("abc|xy+z");
455 LocalURegularExpressionPointer null;
456 LocalURegularExpressionPointer regex(
457 uregex_open(pattern.getBuffer(), pattern.length(), 0, NULL, errorCode));
458 if(!errorCode.logDataIfFailureAndReset("urege_open()")) {
459 regex.adoptInstead(NULL);
460 }
461 }
462 #endif /* !UCONFIG_NO_REGULAR_EXPRESSIONS */
463
464 #if !UCONFIG_NO_TRANSLITERATION
465 {
466 IcuTestErrorCode errorCode(*this, "TestLocalXyzPointerNull/LocalUTransliteratorPointer");
467 UnicodeString id=UNICODE_STRING_SIMPLE("Grek-Latn");
468 LocalUTransliteratorPointer null;
469 LocalUTransliteratorPointer trans(
470 utrans_openU(id.getBuffer(), id.length(), UTRANS_FORWARD, NULL, 0, NULL, errorCode));
471 if(!errorCode.logDataIfFailureAndReset("utrans_openU()")) {
472 trans.adoptInstead(NULL);
473 }
474 }
475 #endif /* !UCONFIG_NO_TRANSLITERATION */
476
477 }
478
479 /** EnumSet test **/
480 #include "unicode/enumset.h"
481
482 class EnumSetTest : public IntlTest {
483 public:
484 EnumSetTest() {}
485 virtual void runIndexedTest(int32_t index, UBool exec, const char *&name, char *par=NULL);
486 void TestEnumSet();
487 };
488
489 static IntlTest *createEnumSetTest() {
490 return new EnumSetTest();
491 }
492
493 void EnumSetTest::runIndexedTest(int32_t index, UBool exec, const char *&name, char * /*par*/) {
494 TESTCASE_AUTO_BEGIN;
495 TESTCASE_AUTO(TestEnumSet);
496 TESTCASE_AUTO_END;
497 }
498 enum myEnum {
499 MAX_NONBOOLEAN=-1,
500 THING1,
501 THING2,
502 THING3,
503 LIMIT_BOOLEAN
504 };
505
506 void EnumSetTest::TestEnumSet() {
507 EnumSet<myEnum,
508 MAX_NONBOOLEAN+1,
509 LIMIT_BOOLEAN>
510 flags;
511
512 logln("Enum is from [%d..%d]\n", MAX_NONBOOLEAN+1,
513 LIMIT_BOOLEAN);
514
515 TEST_ASSERT_TRUE(flags.get(THING1) == FALSE);
516 TEST_ASSERT_TRUE(flags.get(THING2) == FALSE);
517 TEST_ASSERT_TRUE(flags.get(THING3) == FALSE);
518
519 logln("get(thing1)=%d, get(thing2)=%d, get(thing3)=%d\n", flags.get(THING1), flags.get(THING2), flags.get(THING3));
520 logln("Value now: %d\n", flags.getAll());
521 flags.clear();
522 logln("clear -Value now: %d\n", flags.getAll());
523 logln("get(thing1)=%d, get(thing2)=%d, get(thing3)=%d\n", flags.get(THING1), flags.get(THING2), flags.get(THING3));
524 TEST_ASSERT_TRUE(flags.get(THING1) == FALSE);
525 TEST_ASSERT_TRUE(flags.get(THING2) == FALSE);
526 TEST_ASSERT_TRUE(flags.get(THING3) == FALSE);
527 flags.add(THING1);
528 logln("set THING1 -Value now: %d\n", flags.getAll());
529 TEST_ASSERT_TRUE(flags.get(THING1) == TRUE);
530 TEST_ASSERT_TRUE(flags.get(THING2) == FALSE);
531 TEST_ASSERT_TRUE(flags.get(THING3) == FALSE);
532 logln("get(thing1)=%d, get(thing2)=%d, get(thing3)=%d\n", flags.get(THING1), flags.get(THING2), flags.get(THING3));
533 flags.add(THING3);
534 logln("set THING3 -Value now: %d\n", flags.getAll());
535 TEST_ASSERT_TRUE(flags.get(THING1) == TRUE);
536 TEST_ASSERT_TRUE(flags.get(THING2) == FALSE);
537 TEST_ASSERT_TRUE(flags.get(THING3) == TRUE);
538 logln("get(thing1)=%d, get(thing2)=%d, get(thing3)=%d\n", flags.get(THING1), flags.get(THING2), flags.get(THING3));
539 flags.remove(THING2);
540 TEST_ASSERT_TRUE(flags.get(THING1) == TRUE);
541 TEST_ASSERT_TRUE(flags.get(THING2) == FALSE);
542 TEST_ASSERT_TRUE(flags.get(THING3) == TRUE);
543 logln("remove THING2 -Value now: %d\n", flags.getAll());
544 logln("get(thing1)=%d, get(thing2)=%d, get(thing3)=%d\n", flags.get(THING1), flags.get(THING2), flags.get(THING3));
545 flags.remove(THING1);
546 TEST_ASSERT_TRUE(flags.get(THING1) == FALSE);
547 TEST_ASSERT_TRUE(flags.get(THING2) == FALSE);
548 TEST_ASSERT_TRUE(flags.get(THING3) == TRUE);
549 logln("remove THING1 -Value now: %d\n", flags.getAll());
550 logln("get(thing1)=%d, get(thing2)=%d, get(thing3)=%d\n", flags.get(THING1), flags.get(THING2), flags.get(THING3));
551
552 flags.clear();
553 logln("clear -Value now: %d\n", flags.getAll());
554 logln("get(thing1)=%d, get(thing2)=%d, get(thing3)=%d\n", flags.get(THING1), flags.get(THING2), flags.get(THING3));
555 TEST_ASSERT_TRUE(flags.get(THING1) == FALSE);
556 TEST_ASSERT_TRUE(flags.get(THING2) == FALSE);
557 TEST_ASSERT_TRUE(flags.get(THING3) == FALSE);
558 }