+// © 2016 and later: Unicode, Inc. and others.
+// License & terms of use: http://www.unicode.org/copyright.html
/********************************************************************
* COPYRIGHT:
- * Copyright (c) 1997-2005, International Business Machines Corporation and
+ * Copyright (c) 1997-2015, International Business Machines Corporation and
* others. All Rights Reserved.
********************************************************************/
/********************************************************************************
#include "unicode/putil.h"
#include "cstring.h"
#include "cintltst.h"
-#include "umutex.h"
#include "uassert.h"
#include "cmemory.h"
#include "unicode/uchar.h"
#include "unicode/uclean.h"
#include "unicode/ucal.h"
#include "uoptions.h"
-#include "putilimp.h" /* for uprv_getUTCtime() */
+#include "putilimp.h" /* for uprv_getRawUTCtime() */
+#ifdef URES_DEBUG
+#include "uresimp.h" /* for ures_dumpCacheContents() */
+#endif
#ifdef XP_MAC_CONSOLE
# include <console.h>
#endif
+#define CTST_MAX_ALLOC 8192
+/* Array used as a queue */
+static void * ctst_allocated_stuff[CTST_MAX_ALLOC] = {0};
+static int ctst_allocated = 0;
+static UBool ctst_free = FALSE;
+static int ctst_allocated_total = 0;
+
#define CTST_LEAK_CHECK 1
+
#ifdef CTST_LEAK_CHECK
-U_CFUNC void ctst_freeAll(void);
+static void ctst_freeAll(void);
#endif
static char* _testDataPath=NULL;
# define TRY_CNV_2 "sjis"
#endif
+static int gOrigArgc;
+static const char* const * gOrigArgv;
-/*
- * Tracing functions.
- */
-static int traceFnNestingDepth = 0;
-U_CDECL_BEGIN
-static void U_CALLCONV TraceEntry(const void *context, int32_t fnNumber) {
- char buf[500];
- utrace_format(buf, sizeof(buf), traceFnNestingDepth*3, "%s() enter.\n", utrace_functionName(fnNumber));
- buf[sizeof(buf)-1]=0;
- fputs(buf, stdout);
- traceFnNestingDepth++;
-}
-
-static void U_CALLCONV TraceExit(const void *context, int32_t fnNumber, const char *fmt, va_list args) {
- char buf[500];
-
- if (traceFnNestingDepth>0) {
- traceFnNestingDepth--;
- }
- utrace_format(buf, sizeof(buf), traceFnNestingDepth*3, "%s() ", utrace_functionName(fnNumber));
- buf[sizeof(buf)-1]=0;
- fputs(buf, stdout);
- utrace_vformat(buf, sizeof(buf), traceFnNestingDepth*3, fmt, args);
- buf[sizeof(buf)-1]=0;
- fputs(buf, stdout);
- putc('\n', stdout);
-}
-
-static void U_CALLCONV TraceData(const void *context, int32_t fnNumber,
- int32_t level, const char *fmt, va_list args) {
- char buf[500];
- utrace_vformat(buf, sizeof(buf), traceFnNestingDepth*3, fmt, args);
- buf[sizeof(buf)-1]=0;
- fputs(buf, stdout);
- putc('\n', stdout);
-}
-U_CDECL_END
-
-
+#ifdef UNISTR_COUNT_FINAL_STRING_LENGTHS
+U_CAPI void unistr_printLengths();
+#endif
int main(int argc, const char* const argv[])
{
int nerrors = 0;
- int warnOnMissingData = 0;
- int i, j;
UBool defaultDataFound;
TestNode *root;
const char *warnOrErr = "Failure";
- const char** argv2;
UDate startTime, endTime;
int32_t diffTime;
U_MAIN_INIT_ARGS(argc, argv);
- startTime = uprv_getUTCtime();
+ startTime = uprv_getRawUTCtime();
- argv2 = (const char**) malloc(sizeof(char*) * argc);
- if (argv2 == NULL) {
- printf("*** Error: Out of memory (too many cmd line args?)\n");
- return 1;
+ gOrigArgc = argc;
+ gOrigArgv = argv;
+ if (!initArgs(argc, argv, NULL, NULL)) {
+ /* Error already displayed. */
+ return -1;
}
- argv2[0] = argv[0];
-
-
- /* Checkargs */
- /* TODO: Test framework arg handling needs to be decoupled from test execution
- * so that the args being processed here don't need special handling,
- * separate from the other test args.
+
+ /* Check whether ICU will initialize without forcing the build data directory into
+ * the ICU_DATA path. Success here means either the data dll contains data, or that
+ * this test program was run with ICU_DATA set externally. Failure of this check
+ * is normal when ICU data is not packaged into a shared library.
+ *
+ * Whether or not this test succeeds, we want to cleanup and reinitialize
+ * with a data path so that data loading from individual files can be tested.
*/
- ICU_TRACE = UTRACE_OFF;
- for(i=1,j=1;i<argc;i++) {
- argv2[j++] = argv[i];
- if(!strcmp(argv[i],"-w")) {
- warnOnMissingData = 1;
- warnOrErr = "Warning";
- }
- else if (strcmp( argv[i], "-t_error") == 0) {
- ICU_TRACE = UTRACE_ERROR;
- }
- else if (strcmp( argv[i], "-t_warn") == 0) {
- ICU_TRACE = UTRACE_WARNING;
- }
- else if (strcmp( argv[i], "-t_oc") == 0) {
- ICU_TRACE = UTRACE_OPEN_CLOSE;
- }
- else if (strcmp( argv[i], "-t_info") == 0) {
- ICU_TRACE = UTRACE_INFO;
- }
- else if (strcmp( argv[i], "-t_verbose") == 0) {
- ICU_TRACE = UTRACE_VERBOSE;
- }
+ defaultDataFound = TRUE;
+ u_init(&errorCode);
+ if (U_FAILURE(errorCode)) {
+ fprintf(stderr,
+ "#### Note: ICU Init without build-specific setDataDirectory() failed. %s\n", u_errorName(errorCode));
+ defaultDataFound = FALSE;
}
- argc = j;
+ u_cleanup();
+#ifdef URES_DEBUG
+ fprintf(stderr, "After initial u_cleanup: RB cache %s empty.\n", ures_dumpCacheContents()?"WAS NOT":"was");
+#endif
-
- utrace_setFunctions(NULL, TraceEntry, TraceExit, TraceData);
- utrace_setLevel(ICU_TRACE);
-
-
- while (REPEAT_TESTS > 0) { /* Loop runs once per complete execution of the tests
+ while (getTestOption(REPEAT_TESTS_OPTION) > 0) { /* Loop runs once per complete execution of the tests
* used for -r (repeat) test option. */
-
- /* Check whether ICU will initialize without forcing the build data directory into
- * the ICU_DATA path. Success here means either the data dll contains data, or that
- * this test program was run with ICU_DATA set externally. Failure of this check
- * is normal when ICU data is not packaged into a shared library.
- *
- * Whether or not this test succeeds, we want to cleanup and reinitialize
- * with a data path so that data loading from individual files can be tested.
- */
- defaultDataFound = TRUE;
- u_init(&errorCode);
- if (U_FAILURE(errorCode)) {
- fprintf(stderr,
- "#### Note: ICU Init without build-specific setDataDirectory() failed.\n");
- defaultDataFound = FALSE;
+ if (!initArgs(argc, argv, NULL, NULL)) {
+ /* Error already displayed. */
+ return -1;
}
- u_cleanup();
errorCode = U_ZERO_ERROR;
- utrace_setFunctions(NULL, TraceEntry, TraceExit, TraceData);
- utrace_setLevel(ICU_TRACE);
/* Initialize ICU */
if (!defaultDataFound) {
"#### ERROR! %s: u_init() failed with status = \"%s\".\n"
"*** Check the ICU_DATA environment variable and \n"
"*** check that the data files are present.\n", argv[0], u_errorName(errorCode));
- if(warnOnMissingData == 0) {
+ if(!getTestOption(WARN_ON_MISSING_DATA_OPTION)) {
fprintf(stderr, "*** Exiting. Use the '-w' option if data files were\n*** purposely removed, to continue test anyway.\n");
u_cleanup();
return 1;
"*** %s! The converter for " TRY_CNV_2 " cannot be opened.\n"
"*** Check the ICU_DATA environment variable and \n"
"*** check that the data files are present.\n", warnOrErr);
- if(warnOnMissingData == 0) {
+ if(!getTestOption(WARN_ON_MISSING_DATA_OPTION)) {
fprintf(stderr, "*** Exitting. Use the '-w' option if data files were\n*** purposely removed, to continue test anyway.\n");
u_cleanup();
return 1;
"*** %s! The \"en\" locale resource bundle cannot be opened.\n"
"*** Check the ICU_DATA environment variable and \n"
"*** check that the data files are present.\n", warnOrErr);
- if(warnOnMissingData == 0) {
+ if(!getTestOption(WARN_ON_MISSING_DATA_OPTION)) {
fprintf(stderr, "*** Exitting. Use the '-w' option if data files were\n*** purposely removed, to continue test anyway.\n");
u_cleanup();
return 1;
}
}
+ errorCode = U_ZERO_ERROR;
+ rb = ures_open(NULL, NULL, &errorCode);
+ if(U_SUCCESS(errorCode)) {
+ /* ok */
+ if (errorCode == U_USING_DEFAULT_WARNING || errorCode == U_USING_FALLBACK_WARNING) {
+ fprintf(stderr,
+ "#### Note: The default locale %s is not available\n", uloc_getDefault());
+ }
+ ures_close(rb);
+ } else {
+ fprintf(stderr,
+ "*** %s! Can not open a resource bundle for the default locale %s\n", warnOrErr, uloc_getDefault());
+ if(!getTestOption(WARN_ON_MISSING_DATA_OPTION)) {
+ fprintf(stderr, "*** Exitting. Use the '-w' option if data files were\n"
+ "*** purposely removed, to continue test anyway.\n");
+ u_cleanup();
+ return 1;
+ }
+ }
fprintf(stdout, "Default locale for this run is %s\n", uloc_getDefault());
/* Build a tree of all tests.
addAllTests(&root);
/* Tests acutally run HERE. TODO: separate command line option parsing & setting from test execution!! */
- nerrors = processArgs(root, argc, argv2);
+ nerrors = runTestRequest(root, argc, argv);
- if (--REPEAT_TESTS > 0) {
- printf("Repeating tests %d more time(s)\n", REPEAT_TESTS);
+ setTestOption(REPEAT_TESTS_OPTION, DECREMENT_OPTION_VALUE);
+ if (getTestOption(REPEAT_TESTS_OPTION) > 0) {
+ printf("Repeating tests %d more time(s)\n", getTestOption(REPEAT_TESTS_OPTION));
}
cleanUpTestTree(root);
ctst_freeAll();
/* To check for leaks */
u_cleanup(); /* nuke the hashtable.. so that any still-open cnvs are leaked */
+
+ if(getTestOption(VERBOSITY_OPTION) && ctst_allocated_total>0) {
+ fprintf(stderr,"ctst_freeAll(): cleaned up after %d allocations (queue of %d)\n", ctst_allocated_total, CTST_MAX_ALLOC);
+ }
+#ifdef URES_DEBUG
+ if(ures_dumpCacheContents()) {
+ fprintf(stderr, "Error: After final u_cleanup, RB cache was not empty.\n");
+ nerrors++;
+ } else {
+ fprintf(stderr,"OK: After final u_cleanup, RB cache was empty.\n");
+ }
+#endif
#endif
} /* End of loop that repeats the entire test, if requested. (Normally doesn't loop) */
- free((void*)argv2);
+#ifdef UNISTR_COUNT_FINAL_STRING_LENGTHS
+ unistr_printLengths();
+#endif
- endTime = uprv_getUTCtime();
+ endTime = uprv_getRawUTCtime();
diffTime = (int32_t)(endTime - startTime);
printf("Elapsed Time: %02d:%02d:%02d.%03d\n",
(int)((diffTime%U_MILLIS_PER_DAY)/U_MILLIS_PER_HOUR),
dataSrcDir = ".."U_FILE_SEP_STRING".."U_FILE_SEP_STRING "data" U_FILE_SEP_STRING;
}
else {
- dataSrcDir = ".."U_FILE_SEP_STRING".."U_FILE_SEP_STRING".."U_FILE_SEP_STRING "data" U_FILE_SEP_STRING;
+ dataSrcDir = ".."U_FILE_SEP_STRING".."U_FILE_SEP_STRING".."U_FILE_SEP_STRING".."U_FILE_SEP_STRING "data" U_FILE_SEP_STRING;
}
}
}
dataOutDir = ".."U_FILE_SEP_STRING".."U_FILE_SEP_STRING "data" U_FILE_SEP_STRING "out" U_FILE_SEP_STRING;
}
else {
- dataOutDir = ".."U_FILE_SEP_STRING".."U_FILE_SEP_STRING".."U_FILE_SEP_STRING "data" U_FILE_SEP_STRING "out" U_FILE_SEP_STRING;
+ dataOutDir = ".."U_FILE_SEP_STRING".."U_FILE_SEP_STRING".."U_FILE_SEP_STRING".."U_FILE_SEP_STRING "data" U_FILE_SEP_STRING "out" U_FILE_SEP_STRING;
}
}
}
}
}
+/* These tests do cleanup and reinitialize ICU in the course of their operation.
+ * The ICU data directory must be preserved across these operations.
+ * Here is a helper function to assist with that.
+ */
+static char *safeGetICUDataDirectory() {
+ const char *dataDir = u_getDataDirectory(); /* Returned string vanashes with u_cleanup */
+ char *retStr = NULL;
+ if (dataDir != NULL) {
+ retStr = (char *)malloc(strlen(dataDir)+1);
+ strcpy(retStr, dataDir);
+ }
+ return retStr;
+}
+
+UBool ctest_resetICU() {
+ UErrorCode status = U_ZERO_ERROR;
+ char *dataDir = safeGetICUDataDirectory();
+
+ u_cleanup();
+ if (!initArgs(gOrigArgc, gOrigArgv, NULL, NULL)) {
+ /* Error already displayed. */
+ return FALSE;
+ }
+ u_setDataDirectory(dataDir);
+ free(dataDir);
+ u_init(&status);
+ if (U_FAILURE(status)) {
+ log_err_status(status, "u_init failed with %s\n", u_errorName(status));
+ return FALSE;
+ }
+ return TRUE;
+}
+
UChar* CharsToUChars(const char* str) {
/* Might be faster to just use uprv_strlen() as the preflight len - liu */
int32_t len = u_unescape(str, 0, 0); /* preflight */
const void *p;
UErrorCode errorCode = U_ZERO_ERROR;
#if U_CHARSET_FAMILY==U_EBCDIC_FAMILY
-# ifdef OS390
+# if U_PLATFORM == U_PF_OS390
static const char convName[] = "ibm-1047";
# else
static const char convName[] = "ibm-37";
newString = (char*)ctst_malloc ( sizeof(char) * 8 * (length +1));
target = newString;
targetLimit = newString+sizeof(char) * 8 * (length +1);
- ucnv_setFromUCallBack(conv, UCNV_FROM_U_CALLBACK_ESCAPE, UCNV_ESCAPE_JAVA, &cb, &p, &errorCode);
+ ucnv_setFromUCallBack(conv, UCNV_FROM_U_CALLBACK_ESCAPE, UCNV_ESCAPE_C, &cb, &p, &errorCode);
ucnv_fromUnicode(conv,&target,targetLimit, &unichars, (UChar*)(unichars+length),NULL,TRUE,&errorCode);
ucnv_close(conv);
*target = '\0';
/* Fall back did not succeed either so return */
if(U_FAILURE(*err)){
*err = U_FILE_ACCESS_ERROR;
- log_err("Could not load testtypes.res in testdata bundle with path %s - %s\n", tdpath, u_errorName(*err));
+ log_data_err("Could not load testtypes.res in testdata bundle with path %s - %s\n", tdpath, u_errorName(*err));
return "";
}
ures_close(test);
optionalTimeZone = "America/Los_Angeles";
}
if (gOriginalTimeZone[0]) {
- log_err("*** Error: time zone saved twice. New value will be %s\n",
+ log_data_err("*** Error: time zone saved twice. New value will be %s (Are you missing data?)\n",
optionalTimeZone);
}
ucal_getDefaultTimeZone(gOriginalTimeZone, CTEST_MAX_TIMEZONE_SIZE, status);
#endif
}
-#define CTST_MAX_ALLOC 10000
-/* Array used as a queue */
-static void * ctst_allocated_stuff[CTST_MAX_ALLOC] = {0};
-static int ctst_allocated = 0;
-static UBool ctst_free = FALSE;
void *ctst_malloc(size_t size) {
+ ctst_allocated_total++;
if(ctst_allocated >= CTST_MAX_ALLOC - 1) {
ctst_allocated = 0;
ctst_free = TRUE;
}
#ifdef CTST_LEAK_CHECK
-void ctst_freeAll() {
+static void ctst_freeAll() {
int i;
- if(ctst_free == 0) {
+ if(ctst_free == FALSE) { /* only free up to the allocated mark */
for(i=0; i<ctst_allocated; i++) {
free(ctst_allocated_stuff[i]);
ctst_allocated_stuff[i] = NULL;
}
- } else {
+ } else { /* free all */
for(i=0; i<CTST_MAX_ALLOC; i++) {
free(ctst_allocated_stuff[i]);
ctst_allocated_stuff[i] = NULL;
#define VERBOSE_ASSERTIONS
-U_CFUNC UBool assertSuccess(const char* msg, UErrorCode* ec) {
+U_CFUNC UBool assertSuccessCheck(const char* msg, UErrorCode* ec, UBool possibleDataError) {
U_ASSERT(ec!=NULL);
if (U_FAILURE(*ec)) {
- log_err("FAIL: %s (%s)\n", msg, u_errorName(*ec));
+ if (possibleDataError) {
+ log_data_err("FAIL: %s (%s)\n", msg, u_errorName(*ec));
+ } else {
+ log_err_status(*ec, "FAIL: %s (%s)\n", msg, u_errorName(*ec));
+ }
return FALSE;
}
return TRUE;
}
+U_CFUNC UBool assertSuccess(const char* msg, UErrorCode* ec) {
+ U_ASSERT(ec!=NULL);
+ return assertSuccessCheck(msg, ec, FALSE);
+}
+
/* if 'condition' is a UBool, the compiler complains bitterly about
expressions like 'a > 0' which it evaluates as int */
U_CFUNC UBool assertTrue(const char* msg, int /*not UBool*/ condition) {
#endif
return TRUE;
}
-/*--------------------------------------------------------------------
- * Time bomb - allows temporary behavior that expires at a given
- * release
- *--------------------------------------------------------------------
- */
-U_CFUNC UBool isICUVersionAtLeast(const UVersionInfo x) {
- UVersionInfo v;
- u_getVersion(v);
- return (uprv_memcmp(v, x, U_MAX_VERSION_LENGTH) >= 0);
+U_CFUNC UBool assertUEquals(const char* message, const UChar* expected,
+ const UChar* actual) {
+ for (int32_t i=0;; i++) {
+ if (expected[i] != actual[i]) {
+ log_err("FAIL: %s; got \"%s\"; expected \"%s\"\n",
+ message, austrdup(actual), austrdup(expected));
+ return FALSE;
+ }
+ UChar curr = expected[i];
+ U_ASSERT(curr == actual[i]);
+ if (curr == 0) {
+ break;
+ }
+ }
+#ifdef VERBOSE_ASSERTIONS
+ log_verbose("Ok: %s; got \"%s\"\n", message, austrdup(actual));
+#endif
+ return TRUE;
}
+
+U_CFUNC UBool assertIntEquals(const char* message, int64_t expected, int64_t actual) {
+ if (expected != actual) {
+ log_err("FAIL: %s; got \"%d\"; expected \"%d\"\n",
+ message, actual, expected);
+ return FALSE;
+ }
+#ifdef VERBOSE_ASSERTIONS
+ else {
+ log_verbose("Ok: %s; got \"%d\"\n", message, actual);
+ }
+#endif
+ return TRUE;
+}
+
#endif