1 /********************************************************************
3 * Copyright (c) 1997-2014, International Business Machines Corporation and
4 * others. All Rights Reserved.
5 ********************************************************************/
9 * IntlTestUtilities is the medium level test class for everything in the directory "utility".
12 #include "unicode/utypes.h"
13 #include "unicode/errorcode.h"
14 #include "unicode/localpointer.h"
32 extern IntlTest
*createBytesTrieTest();
33 static IntlTest
*createLocalPointerTest();
34 extern IntlTest
*createUCharsTrieTest();
35 static IntlTest
*createEnumSetTest();
36 extern IntlTest
*createLRUCacheTest();
37 extern IntlTest
*createSimplePatternFormatterTest();
39 #define CASE(id, test) case id: \
42 logln(#test "---"); logln(); \
48 void IntlTestUtilities::runIndexedTest( int32_t index
, UBool exec
, const char* &name
, char* par
)
50 if (exec
) logln("TestSuite Utilities: ");
52 CASE(0, MultithreadTest
);
54 CASE(2, UnicodeStringTest
);
56 CASE(4, CharIterTest
);
59 CASE(7, ResourceBundleTest
);
60 CASE(8, NewResourceBundleTest
);
62 CASE(10, UVector32Test
);
63 CASE(11, UVectorTest
);
65 CASE(13, LocaleAliasTest
);
66 CASE(14, UnicodeSetTest
);
67 CASE(15, ErrorCodeTest
);
69 name
= "LocalPointerTest";
71 logln("TestSuite LocalPointerTest---"); logln();
72 LocalPointer
<IntlTest
> test(createLocalPointerTest());
77 name
= "BytesTrieTest";
79 logln("TestSuite BytesTrieTest---"); logln();
80 LocalPointer
<IntlTest
> test(createBytesTrieTest());
85 name
= "UCharsTrieTest";
87 logln("TestSuite UCharsTrieTest---"); logln();
88 LocalPointer
<IntlTest
> test(createUCharsTrieTest());
95 logln("TestSuite EnumSetTest---"); logln();
96 LocalPointer
<IntlTest
> test(createEnumSetTest());
101 name
= "LRUCacheTest";
103 logln("TestSuite LRUCacheTest---"); logln();
104 LocalPointer
<IntlTest
> test(createLRUCacheTest());
105 callTest(*test
, par
);
109 name
= "SimplePatternFormatterTest";
111 logln("TestSuite SimplePatternFormatterTest---"); logln();
112 LocalPointer
<IntlTest
> test(createSimplePatternFormatterTest());
113 callTest(*test
, par
);
116 default: name
= ""; break; //needed to end loop
120 void ErrorCodeTest::runIndexedTest(int32_t index
, UBool exec
, const char* &name
, char* /*par*/) {
121 if (exec
) logln("TestSuite Utilities: ");
123 case 0: name
= "TestErrorCode"; if (exec
) TestErrorCode(); break;
124 case 1: name
= "TestSubclass"; if (exec
) TestSubclass(); break;
125 default: name
= ""; break; //needed to end loop
129 static void RefPlusOne(UErrorCode
&code
) { code
=(UErrorCode
)(code
+1); }
130 static void PtrPlusTwo(UErrorCode
*code
) { *code
=(UErrorCode
)(*code
+2); }
132 void ErrorCodeTest::TestErrorCode() {
134 if(errorCode
.get()!=U_ZERO_ERROR
|| !errorCode
.isSuccess() || errorCode
.isFailure()) {
135 errln("ErrorCode did not initialize properly");
138 errorCode
.assertSuccess();
139 if(errorCode
.errorName()!=u_errorName(U_ZERO_ERROR
)) {
140 errln("ErrorCode did not format error message string properly");
142 RefPlusOne(errorCode
);
143 if(errorCode
.get()!=U_ILLEGAL_ARGUMENT_ERROR
|| errorCode
.isSuccess() || !errorCode
.isFailure()) {
144 errln("ErrorCode did not yield a writable reference");
146 PtrPlusTwo(errorCode
);
147 if(errorCode
.get()!=U_INVALID_FORMAT_ERROR
|| errorCode
.isSuccess() || !errorCode
.isFailure()) {
148 errln("ErrorCode did not yield a writable pointer");
150 errorCode
.set(U_PARSE_ERROR
);
151 if(errorCode
.get()!=U_PARSE_ERROR
|| errorCode
.isSuccess() || !errorCode
.isFailure()) {
152 errln("ErrorCode.set() failed");
154 if( errorCode
.reset()!=U_PARSE_ERROR
|| errorCode
.get()!=U_ZERO_ERROR
||
155 !errorCode
.isSuccess() || errorCode
.isFailure()
157 errln("ErrorCode did not reset properly");
161 class MyErrorCode
: public ErrorCode
{
163 MyErrorCode(int32_t &countChecks
, int32_t &countDests
)
164 : checks(countChecks
), dests(countDests
) {}
171 virtual void handleFailure() const {
178 void ErrorCodeTest::TestSubclass() {
179 int32_t countChecks
=0;
180 int32_t countDests
=0;
182 MyErrorCode
errorCode(countChecks
, countDests
);
183 if( errorCode
.get()!=U_ZERO_ERROR
|| !errorCode
.isSuccess() || errorCode
.isFailure() ||
184 countChecks
!=0 || countDests
!=0
186 errln("ErrorCode did not initialize properly");
189 errorCode
.assertSuccess();
191 errln("ErrorCode.assertSuccess() called handleFailure() despite success");
193 RefPlusOne(errorCode
);
194 if(errorCode
.get()!=U_ILLEGAL_ARGUMENT_ERROR
|| errorCode
.isSuccess() || !errorCode
.isFailure()) {
195 errln("ErrorCode did not yield a writable reference");
197 errorCode
.assertSuccess();
199 errln("ErrorCode.assertSuccess() did not handleFailure()");
201 PtrPlusTwo(errorCode
);
202 if(errorCode
.get()!=U_INVALID_FORMAT_ERROR
|| errorCode
.isSuccess() || !errorCode
.isFailure()) {
203 errln("ErrorCode did not yield a writable pointer");
205 errorCode
.assertSuccess();
207 errln("ErrorCode.assertSuccess() did not handleFailure()");
209 errorCode
.set(U_PARSE_ERROR
);
210 if(errorCode
.get()!=U_PARSE_ERROR
|| errorCode
.isSuccess() || !errorCode
.isFailure()) {
211 errln("ErrorCode.set() failed");
213 if( errorCode
.reset()!=U_PARSE_ERROR
|| errorCode
.get()!=U_ZERO_ERROR
||
214 !errorCode
.isSuccess() || errorCode
.isFailure()
216 errln("ErrorCode did not reset properly");
218 errorCode
.assertSuccess();
220 errln("ErrorCode.assertSuccess() called handleFailure() despite success");
224 errln("MyErrorCode destructor detected failure despite success");
226 countChecks
=countDests
=0;
228 MyErrorCode
errorCode(countChecks
, countDests
);
229 errorCode
.set(U_PARSE_ERROR
);
232 errln("MyErrorCode destructor failed to detect failure");
236 class LocalPointerTest
: public IntlTest
{
238 LocalPointerTest() {}
240 void runIndexedTest(int32_t index
, UBool exec
, const char *&name
, char *par
=NULL
);
242 void TestLocalPointer();
243 void TestLocalArray();
244 void TestLocalXyzPointer();
245 void TestLocalXyzPointerNull();
248 static IntlTest
*createLocalPointerTest() {
249 return new LocalPointerTest();
252 void LocalPointerTest::runIndexedTest(int32_t index
, UBool exec
, const char *&name
, char * /*par*/) {
254 logln("TestSuite LocalPointerTest: ");
257 TESTCASE(0, TestLocalPointer
);
258 TESTCASE(1, TestLocalArray
);
259 TESTCASE(2, TestLocalXyzPointer
);
260 TESTCASE(3, TestLocalXyzPointerNull
);
263 break; // needed to end the loop
267 // Exercise every LocalPointer and LocalPointerBase method.
268 void LocalPointerTest::TestLocalPointer() {
270 LocalPointer
<UnicodeString
> s(new UnicodeString((UChar32
)0x50005));
271 // isNULL(), isValid(), operator==(), operator!=()
272 if(s
.isNull() || !s
.isValid() || s
==NULL
|| !(s
!=NULL
)) {
273 errln("LocalPointer constructor or NULL test failure");
276 // getAlias(), operator->, operator*
277 if(s
.getAlias()->length()!=2 || s
->length()!=2 || (*s
).length()!=2) {
278 errln("LocalPointer access failure");
280 // adoptInstead(), orphan()
281 s
.adoptInstead(new UnicodeString((UChar
)0xfffc));
283 errln("LocalPointer adoptInstead(U+FFFC) failure");
285 UnicodeString
*orphan
=s
.orphan();
286 if(orphan
==NULL
|| orphan
->length()!=1 || s
.isValid() || s
!=NULL
) {
287 errln("LocalPointer orphan() failure");
291 s
.adoptInstead(new UnicodeString());
293 errln("LocalPointer adoptInstead(empty) failure");
297 // Exercise every LocalArray method (but not LocalPointerBase).
298 void LocalPointerTest::TestLocalArray() {
300 LocalArray
<UnicodeString
> a(new UnicodeString
[2]);
302 a
[0].append((UChar
)0x61);
303 a
[1].append((UChar32
)0x60006);
304 if(a
[0].length()!=1 || a
[1].length()!=2) {
305 errln("LocalArray access failure");
308 a
.adoptInstead(new UnicodeString
[4]);
309 a
[3].append((UChar
)0x62).append((UChar
)0x63).reverse();
310 if(a
[3].length()!=2 || a
[3][1]!=0x62) {
311 errln("LocalArray adoptInstead() failure");
316 #include "unicode/ucnvsel.h"
317 #include "unicode/ucal.h"
318 #include "unicode/udatpg.h"
319 #include "unicode/uidna.h"
320 #include "unicode/uldnames.h"
321 #include "unicode/umsg.h"
322 #include "unicode/unorm2.h"
323 #include "unicode/uregex.h"
324 #include "unicode/utrans.h"
326 // Use LocalXyzPointer types that are not covered elsewhere in the intltest suite.
327 void LocalPointerTest::TestLocalXyzPointer() {
328 IcuTestErrorCode
errorCode(*this, "TestLocalXyzPointer");
330 static const char *const encoding
="ISO-8859-1";
331 LocalUConverterSelectorPointer
sel(
332 ucnvsel_open(&encoding
, 1, NULL
, UCNV_ROUNDTRIP_SET
, errorCode
));
333 if(errorCode
.logIfFailureAndReset("ucnvsel_open()")) {
337 errln("LocalUConverterSelectorPointer failure");
341 #if !UCONFIG_NO_FORMATTING
342 LocalUCalendarPointer
cal(ucal_open(NULL
, 0, "root", UCAL_GREGORIAN
, errorCode
));
343 if(errorCode
.logDataIfFailureAndReset("ucal_open()")) {
347 errln("LocalUCalendarPointer failure");
351 LocalUDateTimePatternGeneratorPointer
patgen(udatpg_open("root", errorCode
));
352 if(errorCode
.logDataIfFailureAndReset("udatpg_open()")) {
355 if(patgen
.isNull()) {
356 errln("LocalUDateTimePatternGeneratorPointer failure");
360 LocalULocaleDisplayNamesPointer
ldn(uldn_open("de-CH", ULDN_STANDARD_NAMES
, errorCode
));
361 if(errorCode
.logIfFailureAndReset("uldn_open()")) {
365 errln("LocalULocaleDisplayNamesPointer failure");
369 UnicodeString hello
=UNICODE_STRING_SIMPLE("Hello {0}!");
370 LocalUMessageFormatPointer
msg(
371 umsg_open(hello
.getBuffer(), hello
.length(), "root", NULL
, errorCode
));
372 if(errorCode
.logIfFailureAndReset("umsg_open()")) {
376 errln("LocalUMessageFormatPointer failure");
379 #endif /* UCONFIG_NO_FORMATTING */
381 #if !UCONFIG_NO_NORMALIZATION
382 const UNormalizer2
*nfc
=unorm2_getNFCInstance(errorCode
);
384 LocalUNormalizer2Pointer
fn2(unorm2_openFiltered(nfc
, emptySet
.toUSet(), errorCode
));
385 if(errorCode
.logIfFailureAndReset("unorm2_openFiltered()")) {
389 errln("LocalUNormalizer2Pointer failure");
392 #endif /* !UCONFIG_NO_NORMALIZATION */
395 LocalUIDNAPointer
idna(uidna_openUTS46(0, errorCode
));
396 if(errorCode
.logIfFailureAndReset("uidna_openUTS46()")) {
400 errln("LocalUIDNAPointer failure");
403 #endif /* !UCONFIG_NO_IDNA */
405 #if !UCONFIG_NO_REGULAR_EXPRESSIONS
406 UnicodeString pattern
=UNICODE_STRING_SIMPLE("abc|xy+z");
407 LocalURegularExpressionPointer
regex(
408 uregex_open(pattern
.getBuffer(), pattern
.length(), 0, NULL
, errorCode
));
409 if(errorCode
.logIfFailureAndReset("uregex_open()")) {
413 errln("LocalURegularExpressionPointer failure");
416 #endif /* UCONFIG_NO_REGULAR_EXPRESSIONS */
418 #if !UCONFIG_NO_TRANSLITERATION
419 UnicodeString id
=UNICODE_STRING_SIMPLE("Grek-Latn");
420 LocalUTransliteratorPointer
trans(
421 utrans_openU(id
.getBuffer(), id
.length(), UTRANS_FORWARD
, NULL
, 0, NULL
, errorCode
));
422 if(errorCode
.logIfFailureAndReset("utrans_open()")) {
426 errln("LocalUTransliteratorPointer failure");
429 #endif /* !UCONFIG_NO_TRANSLITERATION */
434 // Try LocalXyzPointer types with NULL pointers.
435 void LocalPointerTest::TestLocalXyzPointerNull() {
437 IcuTestErrorCode
errorCode(*this, "TestLocalXyzPointerNull/LocalUConverterSelectorPointer");
438 static const char *const encoding
="ISO-8859-1";
439 LocalUConverterSelectorPointer null
;
440 LocalUConverterSelectorPointer
sel(
441 ucnvsel_open(&encoding
, 1, NULL
, UCNV_ROUNDTRIP_SET
, errorCode
));
442 sel
.adoptInstead(NULL
);
444 #if !UCONFIG_NO_FORMATTING
446 IcuTestErrorCode
errorCode(*this, "TestLocalXyzPointerNull/LocalUCalendarPointer");
447 LocalUCalendarPointer null
;
448 LocalUCalendarPointer
cal(ucal_open(NULL
, 0, "root", UCAL_GREGORIAN
, errorCode
));
449 if(!errorCode
.logDataIfFailureAndReset("ucal_open()")) {
450 cal
.adoptInstead(NULL
);
454 IcuTestErrorCode
errorCode(*this, "TestLocalXyzPointerNull/LocalUDateTimePatternGeneratorPointer");
455 LocalUDateTimePatternGeneratorPointer null
;
456 LocalUDateTimePatternGeneratorPointer
patgen(udatpg_open("root", errorCode
));
457 patgen
.adoptInstead(NULL
);
460 IcuTestErrorCode
errorCode(*this, "TestLocalXyzPointerNull/LocalUMessageFormatPointer");
461 UnicodeString hello
=UNICODE_STRING_SIMPLE("Hello {0}!");
462 LocalUMessageFormatPointer null
;
463 LocalUMessageFormatPointer
msg(
464 umsg_open(hello
.getBuffer(), hello
.length(), "root", NULL
, errorCode
));
465 msg
.adoptInstead(NULL
);
467 #endif /* !UCONFIG_NO_FORMATTING */
469 #if !UCONFIG_NO_REGULAR_EXPRESSIONS
471 IcuTestErrorCode
errorCode(*this, "TestLocalXyzPointerNull/LocalURegularExpressionPointer");
472 UnicodeString pattern
=UNICODE_STRING_SIMPLE("abc|xy+z");
473 LocalURegularExpressionPointer null
;
474 LocalURegularExpressionPointer
regex(
475 uregex_open(pattern
.getBuffer(), pattern
.length(), 0, NULL
, errorCode
));
476 if(!errorCode
.logDataIfFailureAndReset("urege_open()")) {
477 regex
.adoptInstead(NULL
);
480 #endif /* !UCONFIG_NO_REGULAR_EXPRESSIONS */
482 #if !UCONFIG_NO_TRANSLITERATION
484 IcuTestErrorCode
errorCode(*this, "TestLocalXyzPointerNull/LocalUTransliteratorPointer");
485 UnicodeString id
=UNICODE_STRING_SIMPLE("Grek-Latn");
486 LocalUTransliteratorPointer null
;
487 LocalUTransliteratorPointer
trans(
488 utrans_openU(id
.getBuffer(), id
.length(), UTRANS_FORWARD
, NULL
, 0, NULL
, errorCode
));
489 if(!errorCode
.logDataIfFailureAndReset("utrans_openU()")) {
490 trans
.adoptInstead(NULL
);
493 #endif /* !UCONFIG_NO_TRANSLITERATION */
498 #include "unicode/enumset.h"
500 class EnumSetTest
: public IntlTest
{
503 virtual void runIndexedTest(int32_t index
, UBool exec
, const char *&name
, char *par
=NULL
);
507 static IntlTest
*createEnumSetTest() {
508 return new EnumSetTest();
511 void EnumSetTest::runIndexedTest(int32_t index
, UBool exec
, const char *&name
, char * /*par*/) {
513 TESTCASE_AUTO(TestEnumSet
);
524 void EnumSetTest::TestEnumSet() {
530 logln("Enum is from [%d..%d]\n", MAX_NONBOOLEAN
+1,
533 TEST_ASSERT_TRUE(flags
.get(THING1
) == FALSE
);
534 TEST_ASSERT_TRUE(flags
.get(THING2
) == FALSE
);
535 TEST_ASSERT_TRUE(flags
.get(THING3
) == FALSE
);
537 logln("get(thing1)=%d, get(thing2)=%d, get(thing3)=%d\n", flags
.get(THING1
), flags
.get(THING2
), flags
.get(THING3
));
538 logln("Value now: %d\n", flags
.getAll());
540 logln("clear -Value now: %d\n", flags
.getAll());
541 logln("get(thing1)=%d, get(thing2)=%d, get(thing3)=%d\n", flags
.get(THING1
), flags
.get(THING2
), flags
.get(THING3
));
542 TEST_ASSERT_TRUE(flags
.get(THING1
) == FALSE
);
543 TEST_ASSERT_TRUE(flags
.get(THING2
) == FALSE
);
544 TEST_ASSERT_TRUE(flags
.get(THING3
) == FALSE
);
546 logln("set THING1 -Value now: %d\n", flags
.getAll());
547 TEST_ASSERT_TRUE(flags
.get(THING1
) == TRUE
);
548 TEST_ASSERT_TRUE(flags
.get(THING2
) == FALSE
);
549 TEST_ASSERT_TRUE(flags
.get(THING3
) == FALSE
);
550 logln("get(thing1)=%d, get(thing2)=%d, get(thing3)=%d\n", flags
.get(THING1
), flags
.get(THING2
), flags
.get(THING3
));
552 logln("set THING3 -Value now: %d\n", flags
.getAll());
553 TEST_ASSERT_TRUE(flags
.get(THING1
) == TRUE
);
554 TEST_ASSERT_TRUE(flags
.get(THING2
) == FALSE
);
555 TEST_ASSERT_TRUE(flags
.get(THING3
) == TRUE
);
556 logln("get(thing1)=%d, get(thing2)=%d, get(thing3)=%d\n", flags
.get(THING1
), flags
.get(THING2
), flags
.get(THING3
));
557 flags
.remove(THING2
);
558 TEST_ASSERT_TRUE(flags
.get(THING1
) == TRUE
);
559 TEST_ASSERT_TRUE(flags
.get(THING2
) == FALSE
);
560 TEST_ASSERT_TRUE(flags
.get(THING3
) == TRUE
);
561 logln("remove THING2 -Value now: %d\n", flags
.getAll());
562 logln("get(thing1)=%d, get(thing2)=%d, get(thing3)=%d\n", flags
.get(THING1
), flags
.get(THING2
), flags
.get(THING3
));
563 flags
.remove(THING1
);
564 TEST_ASSERT_TRUE(flags
.get(THING1
) == FALSE
);
565 TEST_ASSERT_TRUE(flags
.get(THING2
) == FALSE
);
566 TEST_ASSERT_TRUE(flags
.get(THING3
) == TRUE
);
567 logln("remove THING1 -Value now: %d\n", flags
.getAll());
568 logln("get(thing1)=%d, get(thing2)=%d, get(thing3)=%d\n", flags
.get(THING1
), flags
.get(THING2
), flags
.get(THING3
));
571 logln("clear -Value now: %d\n", flags
.getAll());
572 logln("get(thing1)=%d, get(thing2)=%d, get(thing3)=%d\n", flags
.get(THING1
), flags
.get(THING2
), flags
.get(THING3
));
573 TEST_ASSERT_TRUE(flags
.get(THING1
) == FALSE
);
574 TEST_ASSERT_TRUE(flags
.get(THING2
) == FALSE
);
575 TEST_ASSERT_TRUE(flags
.get(THING3
) == FALSE
);