1 /********************************************************************
3 * Copyright (c) 1997-2011, 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();
36 #define CASE(id, test) case id: \
39 logln(#test "---"); logln(); \
45 void IntlTestUtilities::runIndexedTest( int32_t index
, UBool exec
, const char* &name
, char* par
)
47 if (exec
) logln("TestSuite Utilities: ");
49 CASE(0, MultithreadTest
);
51 CASE(2, UnicodeStringTest
);
53 CASE(4, CharIterTest
);
56 CASE(7, ResourceBundleTest
);
57 CASE(8, NewResourceBundleTest
);
59 CASE(10, UVector32Test
);
60 CASE(11, UVectorTest
);
62 CASE(13, LocaleAliasTest
);
63 CASE(14, UnicodeSetTest
);
64 CASE(15, ErrorCodeTest
);
66 name
= "LocalPointerTest";
68 logln("TestSuite LocalPointerTest---"); logln();
69 LocalPointer
<IntlTest
> test(createLocalPointerTest());
74 name
= "BytesTrieTest";
76 logln("TestSuite BytesTrieTest---"); logln();
77 LocalPointer
<IntlTest
> test(createBytesTrieTest());
82 name
= "UCharsTrieTest";
84 logln("TestSuite UCharsTrieTest---"); logln();
85 LocalPointer
<IntlTest
> test(createUCharsTrieTest());
89 default: name
= ""; break; //needed to end loop
93 void ErrorCodeTest::runIndexedTest(int32_t index
, UBool exec
, const char* &name
, char* /*par*/) {
94 if (exec
) logln("TestSuite Utilities: ");
96 case 0: name
= "TestErrorCode"; if (exec
) TestErrorCode(); break;
97 case 1: name
= "TestSubclass"; if (exec
) TestSubclass(); break;
98 default: name
= ""; break; //needed to end loop
102 static void RefPlusOne(UErrorCode
&code
) { code
=(UErrorCode
)(code
+1); }
103 static void PtrPlusTwo(UErrorCode
*code
) { *code
=(UErrorCode
)(*code
+2); }
105 void ErrorCodeTest::TestErrorCode() {
107 if(errorCode
.get()!=U_ZERO_ERROR
|| !errorCode
.isSuccess() || errorCode
.isFailure()) {
108 errln("ErrorCode did not initialize properly");
111 errorCode
.assertSuccess();
112 if(errorCode
.errorName()!=u_errorName(U_ZERO_ERROR
)) {
113 errln("ErrorCode did not format error message string properly");
115 RefPlusOne(errorCode
);
116 if(errorCode
.get()!=U_ILLEGAL_ARGUMENT_ERROR
|| errorCode
.isSuccess() || !errorCode
.isFailure()) {
117 errln("ErrorCode did not yield a writable reference");
119 PtrPlusTwo(errorCode
);
120 if(errorCode
.get()!=U_INVALID_FORMAT_ERROR
|| errorCode
.isSuccess() || !errorCode
.isFailure()) {
121 errln("ErrorCode did not yield a writable pointer");
123 errorCode
.set(U_PARSE_ERROR
);
124 if(errorCode
.get()!=U_PARSE_ERROR
|| errorCode
.isSuccess() || !errorCode
.isFailure()) {
125 errln("ErrorCode.set() failed");
127 if( errorCode
.reset()!=U_PARSE_ERROR
|| errorCode
.get()!=U_ZERO_ERROR
||
128 !errorCode
.isSuccess() || errorCode
.isFailure()
130 errln("ErrorCode did not reset properly");
134 class MyErrorCode
: public ErrorCode
{
136 MyErrorCode(int32_t &countChecks
, int32_t &countDests
)
137 : checks(countChecks
), dests(countDests
) {}
144 virtual void handleFailure() const {
151 void ErrorCodeTest::TestSubclass() {
152 int32_t countChecks
=0;
153 int32_t countDests
=0;
155 MyErrorCode
errorCode(countChecks
, countDests
);
156 if( errorCode
.get()!=U_ZERO_ERROR
|| !errorCode
.isSuccess() || errorCode
.isFailure() ||
157 countChecks
!=0 || countDests
!=0
159 errln("ErrorCode did not initialize properly");
162 errorCode
.assertSuccess();
164 errln("ErrorCode.assertSuccess() called handleFailure() despite success");
166 RefPlusOne(errorCode
);
167 if(errorCode
.get()!=U_ILLEGAL_ARGUMENT_ERROR
|| errorCode
.isSuccess() || !errorCode
.isFailure()) {
168 errln("ErrorCode did not yield a writable reference");
170 errorCode
.assertSuccess();
172 errln("ErrorCode.assertSuccess() did not handleFailure()");
174 PtrPlusTwo(errorCode
);
175 if(errorCode
.get()!=U_INVALID_FORMAT_ERROR
|| errorCode
.isSuccess() || !errorCode
.isFailure()) {
176 errln("ErrorCode did not yield a writable pointer");
178 errorCode
.assertSuccess();
180 errln("ErrorCode.assertSuccess() did not handleFailure()");
182 errorCode
.set(U_PARSE_ERROR
);
183 if(errorCode
.get()!=U_PARSE_ERROR
|| errorCode
.isSuccess() || !errorCode
.isFailure()) {
184 errln("ErrorCode.set() failed");
186 if( errorCode
.reset()!=U_PARSE_ERROR
|| errorCode
.get()!=U_ZERO_ERROR
||
187 !errorCode
.isSuccess() || errorCode
.isFailure()
189 errln("ErrorCode did not reset properly");
191 errorCode
.assertSuccess();
193 errln("ErrorCode.assertSuccess() called handleFailure() despite success");
197 errln("MyErrorCode destructor detected failure despite success");
199 countChecks
=countDests
=0;
201 MyErrorCode
errorCode(countChecks
, countDests
);
202 errorCode
.set(U_PARSE_ERROR
);
205 errln("MyErrorCode destructor failed to detect failure");
209 class LocalPointerTest
: public IntlTest
{
211 LocalPointerTest() {}
213 void runIndexedTest(int32_t index
, UBool exec
, const char *&name
, char *par
=NULL
);
215 void TestLocalPointer();
216 void TestLocalArray();
217 void TestLocalXyzPointer();
218 void TestLocalXyzPointerNull();
221 static IntlTest
*createLocalPointerTest() {
222 return new LocalPointerTest();
225 void LocalPointerTest::runIndexedTest(int32_t index
, UBool exec
, const char *&name
, char * /*par*/) {
227 logln("TestSuite LocalPointerTest: ");
230 TESTCASE(0, TestLocalPointer
);
231 TESTCASE(1, TestLocalArray
);
232 TESTCASE(2, TestLocalXyzPointer
);
233 TESTCASE(3, TestLocalXyzPointerNull
);
236 break; // needed to end the loop
240 // Exercise every LocalPointer and LocalPointerBase method.
241 void LocalPointerTest::TestLocalPointer() {
243 LocalPointer
<UnicodeString
> s(new UnicodeString((UChar32
)0x50005));
244 // isNULL(), isValid(), operator==(), operator!=()
245 if(s
.isNull() || !s
.isValid() || s
==NULL
|| !(s
!=NULL
)) {
246 errln("LocalPointer constructor or NULL test failure");
249 // getAlias(), operator->, operator*
250 if(s
.getAlias()->length()!=2 || s
->length()!=2 || (*s
).length()!=2) {
251 errln("LocalPointer access failure");
253 // adoptInstead(), orphan()
254 s
.adoptInstead(new UnicodeString((UChar
)0xfffc));
256 errln("LocalPointer adoptInstead(U+FFFC) failure");
258 UnicodeString
*orphan
=s
.orphan();
259 if(orphan
==NULL
|| orphan
->length()!=1 || s
.isValid() || s
!=NULL
) {
260 errln("LocalPointer orphan() failure");
264 s
.adoptInstead(new UnicodeString());
266 errln("LocalPointer adoptInstead(empty) failure");
270 // Exercise every LocalArray method (but not LocalPointerBase).
271 void LocalPointerTest::TestLocalArray() {
273 LocalArray
<UnicodeString
> a(new UnicodeString
[2]);
275 a
[0].append((UChar
)0x61);
276 a
[1].append((UChar32
)0x60006);
277 if(a
[0].length()!=1 || a
[1].length()!=2) {
278 errln("LocalArray access failure");
281 a
.adoptInstead(new UnicodeString
[4]);
282 a
[3].append((UChar
)0x62).append((UChar
)0x63).reverse();
283 if(a
[3].length()!=2 || a
[3][1]!=0x62) {
284 errln("LocalArray adoptInstead() failure");
289 #include "unicode/ucnvsel.h"
290 #include "unicode/ucal.h"
291 #include "unicode/udatpg.h"
292 #include "unicode/uidna.h"
293 #include "unicode/uldnames.h"
294 #include "unicode/umsg.h"
295 #include "unicode/unorm2.h"
296 #include "unicode/uregex.h"
297 #include "unicode/utrans.h"
299 // Use LocalXyzPointer types that are not covered elsewhere in the intltest suite.
300 void LocalPointerTest::TestLocalXyzPointer() {
301 IcuTestErrorCode
errorCode(*this, "TestLocalXyzPointer");
303 static const char *const encoding
="ISO-8859-1";
304 LocalUConverterSelectorPointer
sel(
305 ucnvsel_open(&encoding
, 1, NULL
, UCNV_ROUNDTRIP_SET
, errorCode
));
306 if(errorCode
.logIfFailureAndReset("ucnvsel_open()")) {
310 errln("LocalUConverterSelectorPointer failure");
314 #if !UCONFIG_NO_FORMATTING
315 LocalUCalendarPointer
cal(ucal_open(NULL
, 0, "root", UCAL_GREGORIAN
, errorCode
));
316 if(errorCode
.logDataIfFailureAndReset("ucal_open()")) {
320 errln("LocalUCalendarPointer failure");
324 LocalUDateTimePatternGeneratorPointer
patgen(udatpg_open("root", errorCode
));
325 if(errorCode
.logDataIfFailureAndReset("udatpg_open()")) {
328 if(patgen
.isNull()) {
329 errln("LocalUDateTimePatternGeneratorPointer failure");
333 LocalULocaleDisplayNamesPointer
ldn(uldn_open("de-CH", ULDN_STANDARD_NAMES
, errorCode
));
334 if(errorCode
.logIfFailureAndReset("uldn_open()")) {
338 errln("LocalULocaleDisplayNamesPointer failure");
342 UnicodeString hello
=UNICODE_STRING_SIMPLE("Hello {0}!");
343 LocalUMessageFormatPointer
msg(
344 umsg_open(hello
.getBuffer(), hello
.length(), "root", NULL
, errorCode
));
345 if(errorCode
.logIfFailureAndReset("umsg_open()")) {
349 errln("LocalUMessageFormatPointer failure");
352 #endif /* UCONFIG_NO_FORMATTING */
354 #if !UCONFIG_NO_NORMALIZATION
355 const UNormalizer2
*nfc
=unorm2_getNFCInstance(errorCode
);
357 LocalUNormalizer2Pointer
fn2(unorm2_openFiltered(nfc
, emptySet
.toUSet(), errorCode
));
358 if(errorCode
.logIfFailureAndReset("unorm2_openFiltered()")) {
362 errln("LocalUNormalizer2Pointer failure");
365 #endif /* !UCONFIG_NO_NORMALIZATION */
368 LocalUIDNAPointer
idna(uidna_openUTS46(0, errorCode
));
369 if(errorCode
.logIfFailureAndReset("uidna_openUTS46()")) {
373 errln("LocalUIDNAPointer failure");
376 #endif /* !UCONFIG_NO_IDNA */
378 #if !UCONFIG_NO_REGULAR_EXPRESSIONS
379 UnicodeString pattern
=UNICODE_STRING_SIMPLE("abc|xy+z");
380 LocalURegularExpressionPointer
regex(
381 uregex_open(pattern
.getBuffer(), pattern
.length(), 0, NULL
, errorCode
));
382 if(errorCode
.logIfFailureAndReset("uregex_open()")) {
386 errln("LocalURegularExpressionPointer failure");
389 #endif /* UCONFIG_NO_REGULAR_EXPRESSIONS */
391 #if !UCONFIG_NO_TRANSLITERATION
392 UnicodeString id
=UNICODE_STRING_SIMPLE("Grek-Latn");
393 LocalUTransliteratorPointer
trans(
394 utrans_openU(id
.getBuffer(), id
.length(), UTRANS_FORWARD
, NULL
, 0, NULL
, errorCode
));
395 if(errorCode
.logIfFailureAndReset("utrans_open()")) {
399 errln("LocalUTransliteratorPointer failure");
402 #endif /* !UCONFIG_NO_TRANSLITERATION */
407 // Try LocalXyzPointer types with NULL pointers.
408 void LocalPointerTest::TestLocalXyzPointerNull() {
410 IcuTestErrorCode
errorCode(*this, "TestLocalXyzPointerNull/LocalUConverterSelectorPointer");
411 static const char *const encoding
="ISO-8859-1";
412 LocalUConverterSelectorPointer null
;
413 LocalUConverterSelectorPointer
sel(
414 ucnvsel_open(&encoding
, 1, NULL
, UCNV_ROUNDTRIP_SET
, errorCode
));
415 sel
.adoptInstead(NULL
);
417 #if !UCONFIG_NO_FORMATTING
419 IcuTestErrorCode
errorCode(*this, "TestLocalXyzPointerNull/LocalUCalendarPointer");
420 LocalUCalendarPointer null
;
421 LocalUCalendarPointer
cal(ucal_open(NULL
, 0, "root", UCAL_GREGORIAN
, errorCode
));
422 if(!errorCode
.logDataIfFailureAndReset("ucal_open()")) {
423 cal
.adoptInstead(NULL
);
427 IcuTestErrorCode
errorCode(*this, "TestLocalXyzPointerNull/LocalUDateTimePatternGeneratorPointer");
428 LocalUDateTimePatternGeneratorPointer null
;
429 LocalUDateTimePatternGeneratorPointer
patgen(udatpg_open("root", errorCode
));
430 patgen
.adoptInstead(NULL
);
433 IcuTestErrorCode
errorCode(*this, "TestLocalXyzPointerNull/LocalUMessageFormatPointer");
434 UnicodeString hello
=UNICODE_STRING_SIMPLE("Hello {0}!");
435 LocalUMessageFormatPointer null
;
436 LocalUMessageFormatPointer
msg(
437 umsg_open(hello
.getBuffer(), hello
.length(), "root", NULL
, errorCode
));
438 msg
.adoptInstead(NULL
);
440 #endif /* !UCONFIG_NO_FORMATTING */
442 #if !UCONFIG_NO_REGULAR_EXPRESSIONS
444 IcuTestErrorCode
errorCode(*this, "TestLocalXyzPointerNull/LocalURegularExpressionPointer");
445 UnicodeString pattern
=UNICODE_STRING_SIMPLE("abc|xy+z");
446 LocalURegularExpressionPointer null
;
447 LocalURegularExpressionPointer
regex(
448 uregex_open(pattern
.getBuffer(), pattern
.length(), 0, NULL
, errorCode
));
449 if(!errorCode
.logDataIfFailureAndReset("urege_open()")) {
450 regex
.adoptInstead(NULL
);
453 #endif /* !UCONFIG_NO_REGULAR_EXPRESSIONS */
455 #if !UCONFIG_NO_TRANSLITERATION
457 IcuTestErrorCode
errorCode(*this, "TestLocalXyzPointerNull/LocalUTransliteratorPointer");
458 UnicodeString id
=UNICODE_STRING_SIMPLE("Grek-Latn");
459 LocalUTransliteratorPointer null
;
460 LocalUTransliteratorPointer
trans(
461 utrans_openU(id
.getBuffer(), id
.length(), UTRANS_FORWARD
, NULL
, 0, NULL
, errorCode
));
462 if(!errorCode
.logDataIfFailureAndReset("utrans_openU()")) {
463 trans
.adoptInstead(NULL
);
466 #endif /* !UCONFIG_NO_TRANSLITERATION */