1 /********************************************************************
3 * Copyright (c) 1997-2004, International Business Machines Corporation and
4 * others. All Rights Reserved.
5 ********************************************************************/
6 /********************************************************************************
10 * Modification History:
12 * Madhu Katragadda Creation
13 *********************************************************************************
16 /*The main root for C API tests*/
21 #include "unicode/utypes.h"
22 #include "unicode/putil.h"
27 #include "unicode/uchar.h"
28 #include "unicode/ustring.h"
29 #include "unicode/ucnv.h"
30 #include "unicode/ures.h"
31 #include "unicode/uclean.h"
32 #include "unicode/ucal.h"
39 #define CTST_LEAK_CHECK 1
40 #ifdef CTST_LEAK_CHECK
41 U_CFUNC
void ctst_freeAll(void);
44 static char* _testDataPath
=NULL
;
47 * Forward Declarations
49 void ctest_setICU_DATA(void);
53 #if UCONFIG_NO_LEGACY_CONVERSION
54 # define TRY_CNV_1 "iso-8859-1"
55 # define TRY_CNV_2 "ibm-1208"
57 # define TRY_CNV_1 "iso-8859-7"
58 # define TRY_CNV_2 "sjis"
65 static int traceFnNestingDepth
= 0;
67 static void U_CALLCONV
TraceEntry(const void *context
, int32_t fnNumber
) {
69 utrace_format(buf
, sizeof(buf
), traceFnNestingDepth
*3, "%s() enter.\n", utrace_functionName(fnNumber
));
72 traceFnNestingDepth
++;
75 static void U_CALLCONV
TraceExit(const void *context
, int32_t fnNumber
, const char *fmt
, va_list args
) {
78 if (traceFnNestingDepth
>0) {
79 traceFnNestingDepth
--;
81 utrace_format(buf
, sizeof(buf
), traceFnNestingDepth
*3, "%s() ", utrace_functionName(fnNumber
));
84 utrace_vformat(buf
, sizeof(buf
), traceFnNestingDepth
*3, fmt
, args
);
90 static void U_CALLCONV
TraceData(const void *context
, int32_t fnNumber
,
91 int32_t level
, const char *fmt
, va_list args
) {
93 utrace_vformat(buf
, sizeof(buf
), traceFnNestingDepth
*3, fmt
, args
);
102 int main(int argc
, const char* const argv
[])
105 int warnOnMissingData
= 0;
107 UBool defaultDataFound
;
109 const char *warnOrErr
= "Failure";
112 /* initial check for the default converter */
113 UErrorCode errorCode
= U_ZERO_ERROR
;
117 U_MAIN_INIT_ARGS(argc
, argv
);
119 argv2
= (const char**) malloc(sizeof(char*) * argc
);
121 printf("*** Error: Out of memory (too many cmd line args?)\n");
128 /* TODO: Test framework arg handling needs to be decoupled from test execution
129 * so that the args being processed here don't need special handling,
130 * separate from the other test args.
132 ICU_TRACE
= UTRACE_OFF
;
133 for(i
=1,j
=1;i
<argc
;i
++) {
134 argv2
[j
++] = argv
[i
];
135 if(!strcmp(argv
[i
],"-w")) {
136 warnOnMissingData
= 1;
137 warnOrErr
= "Warning";
139 else if (strcmp( argv
[i
], "-t_error") == 0) {
140 ICU_TRACE
= UTRACE_ERROR
;
142 else if (strcmp( argv
[i
], "-t_warn") == 0) {
143 ICU_TRACE
= UTRACE_WARNING
;
145 else if (strcmp( argv
[i
], "-t_oc") == 0) {
146 ICU_TRACE
= UTRACE_OPEN_CLOSE
;
148 else if (strcmp( argv
[i
], "-t_info") == 0) {
149 ICU_TRACE
= UTRACE_INFO
;
151 else if (strcmp( argv
[i
], "-t_verbose") == 0) {
152 ICU_TRACE
= UTRACE_VERBOSE
;
158 utrace_setFunctions(NULL
, TraceEntry
, TraceExit
, TraceData
);
159 utrace_setLevel(ICU_TRACE
);
162 while (REPEAT_TESTS
> 0) { /* Loop runs once per complete execution of the tests
163 * used for -r (repeat) test option. */
165 /* Check whether ICU will initialize without forcing the build data directory into
166 * the ICU_DATA path. Success here means either the data dll contains data, or that
167 * this test program was run with ICU_DATA set externally. Failure of this check
168 * is normal when ICU data is not packaged into a shared library.
170 * Whether or not this test succeeds, we want to cleanup and reinitialize
171 * with a data path so that data loading from individual files can be tested.
173 defaultDataFound
= TRUE
;
175 if (U_FAILURE(errorCode
)) {
177 "#### Note: ICU Init without build-specific setDataDirectory() failed.\n");
178 defaultDataFound
= FALSE
;
181 errorCode
= U_ZERO_ERROR
;
182 utrace_setFunctions(NULL
, TraceEntry
, TraceExit
, TraceData
);
183 utrace_setLevel(ICU_TRACE
);
186 if (!defaultDataFound
) {
187 ctest_setICU_DATA(); /* u_setDataDirectory() must happen Before u_init() */
190 if (U_FAILURE(errorCode
)) {
192 "#### ERROR! %s: u_init() failed with status = \"%s\".\n"
193 "*** Check the ICU_DATA environment variable and \n"
194 "*** check that the data files are present.\n", argv
[0], u_errorName(errorCode
));
195 if(warnOnMissingData
== 0) {
196 fprintf(stderr
, "*** Exiting. Use the '-w' option if data files were\n*** purposely removed, to continue test anyway.\n");
205 cnv
= ucnv_open(TRY_CNV_2
, &errorCode
);
211 "*** %s! The converter for " TRY_CNV_2
" cannot be opened.\n"
212 "*** Check the ICU_DATA environment variable and \n"
213 "*** check that the data files are present.\n", warnOrErr
);
214 if(warnOnMissingData
== 0) {
215 fprintf(stderr
, "*** Exitting. Use the '-w' option if data files were\n*** purposely removed, to continue test anyway.\n");
221 rb
= ures_open(NULL
, "en", &errorCode
);
222 if(U_SUCCESS(errorCode
)) {
227 "*** %s! The \"en\" locale resource bundle cannot be opened.\n"
228 "*** Check the ICU_DATA environment variable and \n"
229 "*** check that the data files are present.\n", warnOrErr
);
230 if(warnOnMissingData
== 0) {
231 fprintf(stderr
, "*** Exitting. Use the '-w' option if data files were\n*** purposely removed, to continue test anyway.\n");
237 fprintf(stdout
, "Default locale for this run is %s\n", uloc_getDefault());
239 /* Build a tree of all tests.
240 * Subsequently will be used to find / iterate the tests to run */
244 /* Tests acutally run HERE. TODO: separate command line option parsing & setting from test execution!! */
245 nerrors
= processArgs(root
, argc
, argv2
);
247 if (--REPEAT_TESTS
> 0) {
248 printf("Repeating tests %d more time(s)\n", REPEAT_TESTS
);
250 cleanUpTestTree(root
);
252 #ifdef CTST_LEAK_CHECK
254 /* To check for leaks */
255 u_cleanup(); /* nuke the hashtable.. so that any still-open cnvs are leaked */
258 } /* End of loop that repeats the entire test, if requested. (Normally doesn't loop) */
261 return nerrors
? 1 : 0;
265 static void ctest_appendToDataDirectory(const char *toAppend)
267 const char *oldPath ="";
269 char *newPath = newBuf;
273 if((toAppend == NULL) || (*toAppend == 0)) {
277 oldPath = u_getDataDirectory();
278 if( (oldPath==NULL) || (*oldPath == 0)) {
279 u_setDataDirectory(toAppend);
281 oldLen = strlen(oldPath);
282 newLen = strlen(toAppend)+1+oldLen;
286 newPath = (char *)ctst_malloc(newLen);
289 strcpy(newPath, oldPath);
290 strcpy(newPath+oldLen, U_PATH_SEP_STRING);
291 strcpy(newPath+oldLen+1, toAppend);
293 u_setDataDirectory(newPath);
295 if(newPath != newBuf)
304 ctest_pathnameInContext( char* fullname
, int32_t maxsize
, const char* relPath
)
306 char mainDirBuffer
[1024];
307 char* mainDir
= NULL
;
308 const char *dataDirectory
= ctest_dataOutDir();
309 const char inpSepChar
= '|';
317 OSErr err
= GetVol( volName
, &volNum
);
320 mainDir
= (char*) &(volName
[1]);
321 mainDir
[volName
[0]] = 0;
323 if (dataDirectory
!= NULL
) {
324 strcpy(mainDirBuffer
, dataDirectory
);
325 strcat(mainDirBuffer
, ".." U_FILE_SEP_STRING
);
327 mainDirBuffer
[0]='\0';
329 mainDir
= mainDirBuffer
;
332 lenMainDir
= (int32_t)strlen(mainDir
);
333 if(lenMainDir
> 0 && mainDir
[lenMainDir
- 1] != U_FILE_SEP_CHAR
) {
334 mainDir
[lenMainDir
++] = U_FILE_SEP_CHAR
;
335 mainDir
[lenMainDir
] = 0;
338 if (relPath
[0] == '|')
340 lenRelPath
= (int32_t)strlen(relPath
);
341 if (maxsize
< lenMainDir
+ lenRelPath
+ 2) {
345 strcpy(fullname
, mainDir
);
346 /*strcat(fullname, U_FILE_SEP_STRING);*/
347 strcat(fullname
, relPath
);
348 strchr(fullname
, inpSepChar
);
349 tmp
= strchr(fullname
, inpSepChar
);
351 *tmp
= U_FILE_SEP_CHAR
;
352 tmp
= strchr(tmp
+1, inpSepChar
);
357 /* returns the path to icu/source/data */
358 const char * ctest_dataSrcDir()
360 static const char *dataSrcDir
= NULL
;
366 /* U_TOPSRCDIR is set by the makefiles on UNIXes when building cintltst and intltst
367 // to point to the top of the build hierarchy, which may or
368 // may not be the same as the source directory, depending on
369 // the configure options used. At any rate,
370 // set the data path to the built data from this directory.
371 // The value is complete with quotes, so it can be used
372 // as-is as a string constant.
374 #if defined (U_TOPSRCDIR)
376 dataSrcDir
= U_TOPSRCDIR U_FILE_SEP_STRING
"data" U_FILE_SEP_STRING
;
380 /* On Windows, the file name obtained from __FILE__ includes a full path.
381 * This file is "wherever\icu\source\test\cintltst\cintltst.c"
382 * Change to "wherever\icu\source\data"
385 static char p
[sizeof(__FILE__
) + 20];
390 /* We want to back over three '\' chars. */
391 /* Only Windows should end up here, so looking for '\' is safe. */
392 for (i
=1; i
<=3; i
++) {
393 pBackSlash
= strrchr(p
, U_FILE_SEP_CHAR
);
394 if (pBackSlash
!= NULL
) {
395 *pBackSlash
= 0; /* Truncate the string at the '\' */
399 if (pBackSlash
!= NULL
) {
400 /* We found and truncated three names from the path.
401 * Now append "source\data" and set the environment
403 strcpy(pBackSlash
, U_FILE_SEP_STRING
"data" U_FILE_SEP_STRING
);
407 /* __FILE__ on MSVC7 does not contain the directory */
408 FILE *file
= fopen(".."U_FILE_SEP_STRING
".."U_FILE_SEP_STRING
"data" U_FILE_SEP_STRING
"Makefile.in", "r");
411 dataSrcDir
= ".."U_FILE_SEP_STRING
".."U_FILE_SEP_STRING
"data" U_FILE_SEP_STRING
;
414 dataSrcDir
= ".."U_FILE_SEP_STRING
".."U_FILE_SEP_STRING
".."U_FILE_SEP_STRING
"data" U_FILE_SEP_STRING
;
424 /* returns the path to icu/source/data/out */
425 const char *ctest_dataOutDir()
427 static const char *dataOutDir
= NULL
;
433 /* U_TOPBUILDDIR is set by the makefiles on UNIXes when building cintltst and intltst
434 // to point to the top of the build hierarchy, which may or
435 // may not be the same as the source directory, depending on
436 // the configure options used. At any rate,
437 // set the data path to the built data from this directory.
438 // The value is complete with quotes, so it can be used
439 // as-is as a string constant.
441 #if defined (U_TOPBUILDDIR)
443 dataOutDir
= U_TOPBUILDDIR
"data"U_FILE_SEP_STRING
"out"U_FILE_SEP_STRING
;
447 /* On Windows, the file name obtained from __FILE__ includes a full path.
448 * This file is "wherever\icu\source\test\cintltst\cintltst.c"
449 * Change to "wherever\icu\source\data"
452 static char p
[sizeof(__FILE__
) + 20];
457 /* We want to back over three '\' chars. */
458 /* Only Windows should end up here, so looking for '\' is safe. */
459 for (i
=1; i
<=3; i
++) {
460 pBackSlash
= strrchr(p
, U_FILE_SEP_CHAR
);
461 if (pBackSlash
!= NULL
) {
462 *pBackSlash
= 0; /* Truncate the string at the '\' */
466 if (pBackSlash
!= NULL
) {
467 /* We found and truncated three names from the path.
468 * Now append "source\data" and set the environment
470 strcpy(pBackSlash
, U_FILE_SEP_STRING
"data" U_FILE_SEP_STRING
"out" U_FILE_SEP_STRING
);
474 /* __FILE__ on MSVC7 does not contain the directory */
475 FILE *file
= fopen(".."U_FILE_SEP_STRING
".."U_FILE_SEP_STRING
"data" U_FILE_SEP_STRING
"Makefile.in", "r");
478 dataOutDir
= ".."U_FILE_SEP_STRING
".."U_FILE_SEP_STRING
"data" U_FILE_SEP_STRING
"out" U_FILE_SEP_STRING
;
481 dataOutDir
= ".."U_FILE_SEP_STRING
".."U_FILE_SEP_STRING
".."U_FILE_SEP_STRING
"data" U_FILE_SEP_STRING
"out" U_FILE_SEP_STRING
;
490 /* ctest_setICU_DATA - if the ICU_DATA environment variable is not already
491 * set, try to deduce the directory in which ICU was built,
492 * and set ICU_DATA to "icu/source/data" in that location.
493 * The intent is to allow the tests to have a good chance
494 * of running without requiring that the user manually set
495 * ICU_DATA. Common data isn't a problem, since it is
496 * picked up via a static (build time) reference, but the
497 * tests dynamically load some data.
499 void ctest_setICU_DATA() {
501 /* No location for the data dir was identifiable.
502 * Add other fallbacks for the test data location here if the need arises
504 if (getenv("ICU_DATA") == NULL
) {
505 /* If ICU_DATA isn't set, set it to the usual location */
506 u_setDataDirectory(ctest_dataOutDir());
510 UChar
* CharsToUChars(const char* str
) {
511 /* Might be faster to just use uprv_strlen() as the preflight len - liu */
512 int32_t len
= u_unescape(str
, 0, 0); /* preflight */
513 /* Do NOT use malloc() - we are supposed to be acting like user code! */
514 UChar
*buf
= (UChar
*) malloc(sizeof(UChar
) * (len
+ 1));
515 u_unescape(str
, buf
, len
+ 1);
519 char *austrdup(const UChar
* unichars
)
524 length
= u_strlen ( unichars
);
525 /*newString = (char*)malloc ( sizeof( char ) * 4 * ( length + 1 ) );*/ /* this leaks for now */
526 newString
= (char*)ctst_malloc ( sizeof( char ) * 4 * ( length
+ 1 ) ); /* this shouldn't */
528 if ( newString
== NULL
)
531 u_austrcpy ( newString
, unichars
);
536 char *aescstrdup(const UChar
* unichars
,int32_t length
){
537 char *newString
,*targetLimit
,*target
;
538 UConverterFromUCallback cb
;
540 UErrorCode errorCode
= U_ZERO_ERROR
;
541 #if U_CHARSET_FAMILY==U_EBCDIC_FAMILY
543 static const char convName
[] = "ibm-1047";
545 static const char convName
[] = "ibm-37";
548 static const char convName
[] = "US-ASCII";
550 UConverter
* conv
= ucnv_open(convName
, &errorCode
);
552 length
= u_strlen( unichars
);
554 newString
= (char*)ctst_malloc ( sizeof(char) * 8 * (length
+1));
556 targetLimit
= newString
+sizeof(char) * 8 * (length
+1);
557 ucnv_setFromUCallBack(conv
, UCNV_FROM_U_CALLBACK_ESCAPE
, UCNV_ESCAPE_JAVA
, &cb
, &p
, &errorCode
);
558 ucnv_fromUnicode(conv
,&target
,targetLimit
, &unichars
, (UChar
*)(unichars
+length
),NULL
,TRUE
,&errorCode
);
564 const char* loadTestData(UErrorCode
* err
){
565 const char* directory
=NULL
;
566 UResourceBundle
* test
=NULL
;
568 const char* tdrelativepath
= ".."U_FILE_SEP_STRING
".."U_FILE_SEP_STRING
"test"U_FILE_SEP_STRING
"testdata"U_FILE_SEP_STRING
"out"U_FILE_SEP_STRING
;
569 if( _testDataPath
== NULL
){
570 directory
= ctest_dataOutDir();
572 tdpath
= (char*) ctst_malloc(sizeof(char) *(( strlen(directory
) * strlen(tdrelativepath
)) + 10));
575 /* u_getDataDirectory shoul return \source\data ... set the
576 * directory to ..\source\data\..\test\testdata\out\testdata
578 * Fallback: When Memory mapped file is built
579 * ..\source\data\out\..\..\test\testdata\out\testdata
581 strcpy(tdpath
, directory
);
582 strcat(tdpath
, tdrelativepath
);
583 strcat(tdpath
,"testdata");
586 test
=ures_open(tdpath
, "testtypes", err
);
588 /* Fall back did not succeed either so return */
590 *err
= U_FILE_ACCESS_ERROR
;
591 log_err("Could not load testtypes.res in testdata bundle with path %s - %s\n", tdpath
, u_errorName(*err
));
595 _testDataPath
= tdpath
;
596 return _testDataPath
;
598 return _testDataPath
;
601 #define CTEST_MAX_TIMEZONE_SIZE 256
602 static UChar gOriginalTimeZone
[CTEST_MAX_TIMEZONE_SIZE
] = {0};
605 * Call this once to get a consistent timezone. Use ctest_resetTimeZone to set it back to the original value.
606 * @param optionalTimeZone Set this to a requested timezone.
607 * Set to NULL to use the standard test timezone (Pacific Time)
609 U_CFUNC
void ctest_setTimeZone(const char *optionalTimeZone
, UErrorCode
*status
) {
610 #if !UCONFIG_NO_FORMATTING
611 UChar zoneID
[CTEST_MAX_TIMEZONE_SIZE
];
613 if (optionalTimeZone
== NULL
) {
614 optionalTimeZone
= "America/Los_Angeles";
616 if (gOriginalTimeZone
[0]) {
617 log_err("*** Error: time zone saved twice. New value will be %s\n",
620 ucal_getDefaultTimeZone(gOriginalTimeZone
, CTEST_MAX_TIMEZONE_SIZE
, status
);
621 if (U_FAILURE(*status
)) {
622 log_err("*** Error: Failed to save default time zone: %s\n",
623 u_errorName(*status
));
624 *status
= U_ZERO_ERROR
;
627 u_uastrncpy(zoneID
, optionalTimeZone
, CTEST_MAX_TIMEZONE_SIZE
-1);
628 zoneID
[CTEST_MAX_TIMEZONE_SIZE
-1] = 0;
629 ucal_setDefaultTimeZone(zoneID
, status
);
630 if (U_FAILURE(*status
)) {
631 log_err("*** Error: Failed to set default time zone to \"%s\": %s\n",
632 optionalTimeZone
, u_errorName(*status
));
638 * Call this once get back the original timezone
640 U_CFUNC
void ctest_resetTimeZone(void) {
641 #if !UCONFIG_NO_FORMATTING
642 UErrorCode status
= U_ZERO_ERROR
;
644 ucal_setDefaultTimeZone(gOriginalTimeZone
, &status
);
645 if (U_FAILURE(status
)) {
646 log_err("*** Error: Failed to reset default time zone: %s\n",
647 u_errorName(status
));
649 /* Set to an empty state */
650 gOriginalTimeZone
[0] = 0;
654 #define CTST_MAX_ALLOC 10000
655 /* Array used as a queue */
656 static void * ctst_allocated_stuff
[CTST_MAX_ALLOC
] = {0};
657 static int ctst_allocated
= 0;
658 static UBool ctst_free
= FALSE
;
660 void *ctst_malloc(size_t size
) {
661 if(ctst_allocated
>= CTST_MAX_ALLOC
- 1) {
665 if(ctst_allocated_stuff
[ctst_allocated
]) {
666 free(ctst_allocated_stuff
[ctst_allocated
]);
668 return ctst_allocated_stuff
[ctst_allocated
++] = malloc(size
);
671 #ifdef CTST_LEAK_CHECK
672 void ctst_freeAll() {
675 for(i
=0; i
<ctst_allocated
; i
++) {
676 free(ctst_allocated_stuff
[i
]);
677 ctst_allocated_stuff
[i
] = NULL
;
680 for(i
=0; i
<CTST_MAX_ALLOC
; i
++) {
681 free(ctst_allocated_stuff
[i
]);
682 ctst_allocated_stuff
[i
] = NULL
;
689 #define VERBOSE_ASSERTIONS
691 U_CFUNC UBool
assertSuccess(const char* msg
, UErrorCode
* ec
) {
693 if (U_FAILURE(*ec
)) {
694 log_err("FAIL: %s (%s)\n", msg
, u_errorName(*ec
));
700 /* if 'condition' is a UBool, the compiler complains bitterly about
701 expressions like 'a > 0' which it evaluates as int */
702 U_CFUNC UBool
assertTrue(const char* msg
, int /*not UBool*/ condition
) {
704 log_err("FAIL: assertTrue() failed: %s\n", msg
);
706 #ifdef VERBOSE_ASSERTIONS
708 log_verbose("Ok: %s\n", msg
);
711 return (UBool
)condition
;
714 U_CFUNC UBool
assertEquals(const char* message
, const char* expected
,
715 const char* actual
) {
716 if (uprv_strcmp(expected
, actual
) != 0) {
717 log_err("FAIL: %s; got \"%s\"; expected \"%s\"\n",
718 message
, actual
, expected
);
721 #ifdef VERBOSE_ASSERTIONS
723 log_verbose("Ok: %s; got \"%s\"\n", message
, actual
);