1 /********************************************************************
3 * Copyright (c) 1997-2010, 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 static IntlTest
*createLocalPointerTest();
34 #define CASE(id, test) case id: \
37 logln(#test "---"); logln(); \
43 void IntlTestUtilities::runIndexedTest( int32_t index
, UBool exec
, const char* &name
, char* par
)
45 if (exec
) logln("TestSuite Utilities: ");
47 CASE(0, MultithreadTest
);
49 CASE(2, UnicodeStringTest
);
51 CASE(4, CharIterTest
);
54 CASE(7, ResourceBundleTest
);
55 CASE(8, NewResourceBundleTest
);
57 CASE(10, UVector32Test
);
58 CASE(11, UVectorTest
);
60 CASE(13, LocaleAliasTest
);
61 CASE(14, UnicodeSetTest
);
62 CASE(15, ErrorCodeTest
);
64 name
= "LocalPointerTest";
66 logln("TestSuite LocalPointerTest---"); logln();
67 LocalPointer
<IntlTest
> test(createLocalPointerTest());
71 default: name
= ""; break; //needed to end loop
75 void ErrorCodeTest::runIndexedTest(int32_t index
, UBool exec
, const char* &name
, char* /*par*/) {
76 if (exec
) logln("TestSuite Utilities: ");
78 case 0: name
= "TestErrorCode"; if (exec
) TestErrorCode(); break;
79 case 1: name
= "TestSubclass"; if (exec
) TestSubclass(); break;
80 default: name
= ""; break; //needed to end loop
84 static void RefPlusOne(UErrorCode
&code
) { code
=(UErrorCode
)(code
+1); }
85 static void PtrPlusTwo(UErrorCode
*code
) { *code
=(UErrorCode
)(*code
+2); }
87 void ErrorCodeTest::TestErrorCode() {
89 if(errorCode
.get()!=U_ZERO_ERROR
|| !errorCode
.isSuccess() || errorCode
.isFailure()) {
90 errln("ErrorCode did not initialize properly");
93 errorCode
.assertSuccess();
94 if(errorCode
.errorName()!=u_errorName(U_ZERO_ERROR
)) {
95 errln("ErrorCode did not format error message string properly");
97 RefPlusOne(errorCode
);
98 if(errorCode
.get()!=U_ILLEGAL_ARGUMENT_ERROR
|| errorCode
.isSuccess() || !errorCode
.isFailure()) {
99 errln("ErrorCode did not yield a writable reference");
101 PtrPlusTwo(errorCode
);
102 if(errorCode
.get()!=U_INVALID_FORMAT_ERROR
|| errorCode
.isSuccess() || !errorCode
.isFailure()) {
103 errln("ErrorCode did not yield a writable pointer");
105 errorCode
.set(U_PARSE_ERROR
);
106 if(errorCode
.get()!=U_PARSE_ERROR
|| errorCode
.isSuccess() || !errorCode
.isFailure()) {
107 errln("ErrorCode.set() failed");
109 if( errorCode
.reset()!=U_PARSE_ERROR
|| errorCode
.get()!=U_ZERO_ERROR
||
110 !errorCode
.isSuccess() || errorCode
.isFailure()
112 errln("ErrorCode did not reset properly");
116 class MyErrorCode
: public ErrorCode
{
118 MyErrorCode(int32_t &countChecks
, int32_t &countDests
)
119 : checks(countChecks
), dests(countDests
) {}
126 virtual void handleFailure() const {
133 void ErrorCodeTest::TestSubclass() {
134 int32_t countChecks
=0;
135 int32_t countDests
=0;
137 MyErrorCode
errorCode(countChecks
, countDests
);
138 if( errorCode
.get()!=U_ZERO_ERROR
|| !errorCode
.isSuccess() || errorCode
.isFailure() ||
139 countChecks
!=0 || countDests
!=0
141 errln("ErrorCode did not initialize properly");
144 errorCode
.assertSuccess();
146 errln("ErrorCode.assertSuccess() called handleFailure() despite success");
148 RefPlusOne(errorCode
);
149 if(errorCode
.get()!=U_ILLEGAL_ARGUMENT_ERROR
|| errorCode
.isSuccess() || !errorCode
.isFailure()) {
150 errln("ErrorCode did not yield a writable reference");
152 errorCode
.assertSuccess();
154 errln("ErrorCode.assertSuccess() did not handleFailure()");
156 PtrPlusTwo(errorCode
);
157 if(errorCode
.get()!=U_INVALID_FORMAT_ERROR
|| errorCode
.isSuccess() || !errorCode
.isFailure()) {
158 errln("ErrorCode did not yield a writable pointer");
160 errorCode
.assertSuccess();
162 errln("ErrorCode.assertSuccess() did not handleFailure()");
164 errorCode
.set(U_PARSE_ERROR
);
165 if(errorCode
.get()!=U_PARSE_ERROR
|| errorCode
.isSuccess() || !errorCode
.isFailure()) {
166 errln("ErrorCode.set() failed");
168 if( errorCode
.reset()!=U_PARSE_ERROR
|| errorCode
.get()!=U_ZERO_ERROR
||
169 !errorCode
.isSuccess() || errorCode
.isFailure()
171 errln("ErrorCode did not reset properly");
173 errorCode
.assertSuccess();
175 errln("ErrorCode.assertSuccess() called handleFailure() despite success");
179 errln("MyErrorCode destructor detected failure despite success");
181 countChecks
=countDests
=0;
183 MyErrorCode
errorCode(countChecks
, countDests
);
184 errorCode
.set(U_PARSE_ERROR
);
187 errln("MyErrorCode destructor failed to detect failure");
191 class LocalPointerTest
: public IntlTest
{
193 LocalPointerTest() {}
195 void runIndexedTest(int32_t index
, UBool exec
, const char *&name
, char *par
=NULL
);
197 void TestLocalPointer();
198 void TestLocalArray();
199 void TestLocalXyzPointer();
200 void TestLocalXyzPointerNull();
203 static IntlTest
*createLocalPointerTest() {
204 return new LocalPointerTest();
207 void LocalPointerTest::runIndexedTest(int32_t index
, UBool exec
, const char *&name
, char * /*par*/) {
209 logln("TestSuite LocalPointerTest: ");
212 TESTCASE(0, TestLocalPointer
);
213 TESTCASE(1, TestLocalArray
);
214 TESTCASE(2, TestLocalXyzPointer
);
215 TESTCASE(3, TestLocalXyzPointerNull
);
218 break; // needed to end the loop
222 // Exercise every LocalPointer and LocalPointerBase method.
223 void LocalPointerTest::TestLocalPointer() {
225 LocalPointer
<UnicodeString
> s(new UnicodeString((UChar32
)0x50005));
226 // isNULL(), isValid(), operator==(), operator!=()
227 if(s
.isNull() || !s
.isValid() || s
==NULL
|| !(s
!=NULL
)) {
228 errln("LocalPointer constructor or NULL test failure");
231 // getAlias(), operator->, operator*
232 if(s
.getAlias()->length()!=2 || s
->length()!=2 || (*s
).length()!=2) {
233 errln("LocalPointer access failure");
235 // adoptInstead(), orphan()
236 s
.adoptInstead(new UnicodeString((UChar
)0xfffc));
238 errln("LocalPointer adoptInstead(U+FFFC) failure");
240 UnicodeString
*orphan
=s
.orphan();
241 if(orphan
==NULL
|| orphan
->length()!=1 || s
.isValid() || s
!=NULL
) {
242 errln("LocalPointer orphan() failure");
246 s
.adoptInstead(new UnicodeString());
248 errln("LocalPointer adoptInstead(empty) failure");
252 // Exercise every LocalArray method (but not LocalPointerBase).
253 void LocalPointerTest::TestLocalArray() {
255 LocalArray
<UnicodeString
> a(new UnicodeString
[2]);
257 a
[0].append((UChar
)0x61);
258 a
[1].append((UChar32
)0x60006);
259 if(a
[0].length()!=1 || a
[1].length()!=2) {
260 errln("LocalArray access failure");
263 a
.adoptInstead(new UnicodeString
[4]);
264 a
[3].append((UChar
)0x62).append((UChar
)0x63).reverse();
265 if(a
[3].length()!=2 || a
[3][1]!=0x62) {
266 errln("LocalArray adoptInstead() failure");
271 #include "unicode/ucnvsel.h"
272 #include "unicode/ucal.h"
273 #include "unicode/udatpg.h"
274 #include "unicode/uidna.h"
275 #include "unicode/uldnames.h"
276 #include "unicode/umsg.h"
277 #include "unicode/unorm2.h"
278 #include "unicode/uregex.h"
279 #include "unicode/utrans.h"
281 // Use LocalXyzPointer types that are not covered elsewhere in the intltest suite.
282 void LocalPointerTest::TestLocalXyzPointer() {
283 IcuTestErrorCode
errorCode(*this, "TestLocalXyzPointer");
285 static const char *const encoding
="ISO-8859-1";
286 LocalUConverterSelectorPointer
sel(
287 ucnvsel_open(&encoding
, 1, NULL
, UCNV_ROUNDTRIP_SET
, errorCode
));
288 if(errorCode
.logIfFailureAndReset("ucnvsel_open()")) {
292 errln("LocalUConverterSelectorPointer failure");
296 #if !UCONFIG_NO_FORMATTING
297 LocalUCalendarPointer
cal(ucal_open(NULL
, 0, "root", UCAL_GREGORIAN
, errorCode
));
298 if(errorCode
.logDataIfFailureAndReset("ucal_open()")) {
302 errln("LocalUCalendarPointer failure");
306 LocalUDateTimePatternGeneratorPointer
patgen(udatpg_open("root", errorCode
));
307 if(errorCode
.logIfFailureAndReset("udatpg_open()")) {
310 if(patgen
.isNull()) {
311 errln("LocalUDateTimePatternGeneratorPointer failure");
315 LocalULocaleDisplayNamesPointer
ldn(uldn_open("de-CH", ULDN_STANDARD_NAMES
, errorCode
));
316 if(errorCode
.logIfFailureAndReset("uldn_open()")) {
320 errln("LocalULocaleDisplayNamesPointer failure");
324 UnicodeString hello
=UNICODE_STRING_SIMPLE("Hello {0}!");
325 LocalUMessageFormatPointer
msg(
326 umsg_open(hello
.getBuffer(), hello
.length(), "root", NULL
, errorCode
));
327 if(errorCode
.logIfFailureAndReset("umsg_open()")) {
331 errln("LocalUMessageFormatPointer failure");
334 #endif /* UCONFIG_NO_FORMATTING */
336 #if !UCONFIG_NO_NORMALIZATION
337 const UNormalizer2
*nfc
=unorm2_getInstance(NULL
, "nfc", UNORM2_COMPOSE
, errorCode
);
339 LocalUNormalizer2Pointer
fn2(unorm2_openFiltered(nfc
, emptySet
.toUSet(), errorCode
));
340 if(errorCode
.logIfFailureAndReset("unorm2_openFiltered()")) {
344 errln("LocalUNormalizer2Pointer failure");
347 #endif /* !UCONFIG_NO_NORMALIZATION */
350 LocalUIDNAPointer
idna(uidna_openUTS46(0, errorCode
));
351 if(errorCode
.logIfFailureAndReset("uidna_openUTS46()")) {
355 errln("LocalUIDNAPointer failure");
358 #endif /* !UCONFIG_NO_IDNA */
360 #if !UCONFIG_NO_REGULAR_EXPRESSIONS
361 UnicodeString pattern
=UNICODE_STRING_SIMPLE("abc|xy+z");
362 LocalURegularExpressionPointer
regex(
363 uregex_open(pattern
.getBuffer(), pattern
.length(), 0, NULL
, errorCode
));
364 if(errorCode
.logIfFailureAndReset("uregex_open()")) {
368 errln("LocalURegularExpressionPointer failure");
371 #endif /* UCONFIG_NO_REGULAR_EXPRESSIONS */
373 #if !UCONFIG_NO_TRANSLITERATION
374 UnicodeString id
=UNICODE_STRING_SIMPLE("Grek-Latn");
375 LocalUTransliteratorPointer
trans(
376 utrans_openU(id
.getBuffer(), id
.length(), UTRANS_FORWARD
, NULL
, 0, NULL
, errorCode
));
377 if(errorCode
.logIfFailureAndReset("utrans_open()")) {
381 errln("LocalUTransliteratorPointer failure");
384 #endif /* !UCONFIG_NO_TRANSLITERATION */
389 // Try LocalXyzPointer types with NULL pointers.
390 void LocalPointerTest::TestLocalXyzPointerNull() {
392 IcuTestErrorCode
errorCode(*this, "TestLocalXyzPointerNull/LocalUConverterSelectorPointer");
393 static const char *const encoding
="ISO-8859-1";
394 LocalUConverterSelectorPointer null
;
395 LocalUConverterSelectorPointer
sel(
396 ucnvsel_open(&encoding
, 1, NULL
, UCNV_ROUNDTRIP_SET
, errorCode
));
397 sel
.adoptInstead(NULL
);
399 #if !UCONFIG_NO_FORMATTING
401 IcuTestErrorCode
errorCode(*this, "TestLocalXyzPointerNull/LocalUCalendarPointer");
402 LocalUCalendarPointer null
;
403 LocalUCalendarPointer
cal(ucal_open(NULL
, 0, "root", UCAL_GREGORIAN
, errorCode
));
404 if(!errorCode
.logDataIfFailureAndReset("ucal_open()")) {
405 cal
.adoptInstead(NULL
);
409 IcuTestErrorCode
errorCode(*this, "TestLocalXyzPointerNull/LocalUDateTimePatternGeneratorPointer");
410 LocalUDateTimePatternGeneratorPointer null
;
411 LocalUDateTimePatternGeneratorPointer
patgen(udatpg_open("root", errorCode
));
412 patgen
.adoptInstead(NULL
);
415 IcuTestErrorCode
errorCode(*this, "TestLocalXyzPointerNull/LocalUMessageFormatPointer");
416 UnicodeString hello
=UNICODE_STRING_SIMPLE("Hello {0}!");
417 LocalUMessageFormatPointer null
;
418 LocalUMessageFormatPointer
msg(
419 umsg_open(hello
.getBuffer(), hello
.length(), "root", NULL
, errorCode
));
420 msg
.adoptInstead(NULL
);
422 #endif /* !UCONFIG_NO_FORMATTING */
424 #if !UCONFIG_NO_REGULAR_EXPRESSIONS
426 IcuTestErrorCode
errorCode(*this, "TestLocalXyzPointerNull/LocalURegularExpressionPointer");
427 UnicodeString pattern
=UNICODE_STRING_SIMPLE("abc|xy+z");
428 LocalURegularExpressionPointer null
;
429 LocalURegularExpressionPointer
regex(
430 uregex_open(pattern
.getBuffer(), pattern
.length(), 0, NULL
, errorCode
));
431 if(!errorCode
.logDataIfFailureAndReset("urege_open()")) {
432 regex
.adoptInstead(NULL
);
435 #endif /* !UCONFIG_NO_REGULAR_EXPRESSIONS */
437 #if !UCONFIG_NO_TRANSLITERATION
439 IcuTestErrorCode
errorCode(*this, "TestLocalXyzPointerNull/LocalUTransliteratorPointer");
440 UnicodeString id
=UNICODE_STRING_SIMPLE("Grek-Latn");
441 LocalUTransliteratorPointer null
;
442 LocalUTransliteratorPointer
trans(
443 utrans_openU(id
.getBuffer(), id
.length(), UTRANS_FORWARD
, NULL
, 0, NULL
, errorCode
));
444 if(!errorCode
.logDataIfFailureAndReset("utrans_openU()")) {
445 trans
.adoptInstead(NULL
);
448 #endif /* !UCONFIG_NO_TRANSLITERATION */