#include "unicode/utypes.h"
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <stdarg.h>
#include <stdlib.h>
#include "unicode/unistr.h"
#include "unicode/ures.h"
#include "unicode/smpdtfmt.h"
#include "unicode/ucnv.h"
#include "unicode/uclean.h"
#include "unicode/timezone.h"
#include "unicode/curramt.h"
#include "unicode/putil.h"
#include "intltest.h"
#include "caltztst.h"
#include "itmajor.h"
#include "cstring.h"
#include "umutex.h"
#include "uassert.h"
#include "cmemory.h"
#include "uoptions.h"
#include "putilimp.h" // for uprv_getUTCtime()
#include "unicode/locid.h"
#ifdef XP_MAC_CONSOLE
#include <console.h>
#include "Files.h"
#endif
static char* _testDataPath=NULL;
static UnicodeString errorList;
UnicodeString
UCharToUnicodeString(UChar c)
{ return UnicodeString(c); }
UnicodeString
operator+(const UnicodeString& left,
long num)
{
char buffer[64]; char danger = 'p';
sprintf(buffer, "%ld", num);
assert(danger == 'p');
return left + buffer;
}
UnicodeString
operator+(const UnicodeString& left,
unsigned long num)
{
char buffer[64]; char danger = 'p';
sprintf(buffer, "%lu", num);
assert(danger == 'p');
return left + buffer;
}
UnicodeString
Int64ToUnicodeString(int64_t num)
{
char buffer[64]; char danger = 'p';
#ifdef U_WINDOWS
sprintf(buffer, "%I64d", num);
#else
sprintf(buffer, "%lld", (long long)num);
#endif
assert(danger == 'p');
return buffer;
}
UnicodeString
operator+(const UnicodeString& left,
double num)
{
char buffer[64]; char danger = 'p';
sprintf(buffer, "%.16g", num);
assert(danger == 'p');
return left + buffer;
}
#if !UCONFIG_NO_FORMATTING
UnicodeString _toString(const Formattable& f) {
UnicodeString s;
switch (f.getType()) {
case Formattable::kDate:
{
UErrorCode status = U_ZERO_ERROR;
SimpleDateFormat fmt(status);
if (U_SUCCESS(status)) {
FieldPosition pos;
fmt.format(f.getDate(), s, pos);
s.insert(0, "Date:");
} else {
s = UnicodeString("Error creating date format]");
}
}
break;
case Formattable::kDouble:
s = UnicodeString("double:") + f.getDouble();
break;
case Formattable::kLong:
s = UnicodeString("long:") + f.getLong();
break;
case Formattable::kInt64:
s = UnicodeString("int64:") + Int64ToUnicodeString(f.getInt64());
break;
case Formattable::kString:
f.getString(s);
s.insert(0, "String:");
break;
case Formattable::kArray:
{
int32_t i, n;
const Formattable* array = f.getArray(n);
s.insert(0, UnicodeString("Array:"));
UnicodeString delim(", ");
for (i=0; i<n; ++i) {
if (i > 0) {
s.append(delim);
}
s = s + _toString(array[i]);
}
}
break;
case Formattable::kObject:
if (f.getObject()->getDynamicClassID() ==
CurrencyAmount::getStaticClassID()) {
const CurrencyAmount& c = (const CurrencyAmount&) *f.getObject();
s = _toString(c.getNumber()) + " " + UnicodeString(c.getISOCurrency());
} else {
s = UnicodeString("Unknown UObject");
}
break;
default:
s = UnicodeString("Unknown Formattable type=") + (int32_t)f.getType();
break;
}
return s;
}
UnicodeString toString(const Formattable& f) {
UnicodeString s((UChar)91);
s.append(_toString(f));
s.append((UChar)0x5d);
return s;
}
#endif
UnicodeString toString(int32_t n) {
return UnicodeString() + (long)n;
}
UnicodeString operator+(const UnicodeString& left, char num)
{ return left + (long)num; }
UnicodeString operator+(const UnicodeString& left, short num)
{ return left + (long)num; }
UnicodeString operator+(const UnicodeString& left, int num)
{ return left + (long)num; }
UnicodeString operator+(const UnicodeString& left, unsigned char num)
{ return left + (unsigned long)num; }
UnicodeString operator+(const UnicodeString& left, unsigned short num)
{ return left + (unsigned long)num; }
UnicodeString operator+(const UnicodeString& left, unsigned int num)
{ return left + (unsigned long)num; }
UnicodeString operator+(const UnicodeString& left, float num)
{ return left + (double)num; }
UnicodeString&
IntlTest::appendHex(uint32_t number,
int32_t digits,
UnicodeString& target)
{
static const UChar digitString[] = {
0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39,
0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0
};
switch (digits)
{
case 8:
target += digitString[(number >> 28) & 0xF];
case 7:
target += digitString[(number >> 24) & 0xF];
case 6:
target += digitString[(number >> 20) & 0xF];
case 5:
target += digitString[(number >> 16) & 0xF];
case 4:
target += digitString[(number >> 12) & 0xF];
case 3:
target += digitString[(number >> 8) & 0xF];
case 2:
target += digitString[(number >> 4) & 0xF];
case 1:
target += digitString[(number >> 0) & 0xF];
break;
default:
target += "**";
}
return target;
}
UnicodeString&
IntlTest::prettify(const UnicodeString &source,
UnicodeString &target)
{
int32_t i;
target.remove();
target += "\"";
for (i = 0; i < source.length(); )
{
UChar32 ch = source.char32At(i);
i += UTF_CHAR_LENGTH(ch);
if (ch < 0x09 || (ch > 0x0A && ch < 0x20)|| ch > 0x7E)
{
if (ch <= 0xFFFF) {
target += "\\u";
appendHex(ch, 4, target);
} else {
target += "\\U";
appendHex(ch, 8, target);
}
}
else
{
target += ch;
}
}
target += "\"";
return target;
}
UnicodeString
IntlTest::prettify(const UnicodeString &source, UBool parseBackslash)
{
int32_t i;
UnicodeString target;
target.remove();
target += "\"";
for (i = 0; i < source.length();)
{
UChar32 ch = source.char32At(i);
i += UTF_CHAR_LENGTH(ch);
if (ch < 0x09 || (ch > 0x0A && ch < 0x20)|| ch > 0x7E)
{
if (parseBackslash) {
int32_t backslashCount = 0;
for (int32_t j=target.length()-1; j>=0; --j) {
if (target.charAt(j) == (UChar)92) {
++backslashCount;
} else {
break;
}
}
if ((backslashCount % 2) == 1) {
target.truncate(target.length() - 1);
}
}
if (ch <= 0xFFFF) {
target += "\\u";
appendHex(ch, 4, target);
} else {
target += "\\U";
appendHex(ch, 8, target);
}
}
else
{
target += ch;
}
}
target += "\"";
return target;
}
void IntlTest::setICU_DATA() {
const char *original_ICU_DATA = getenv("ICU_DATA");
if (original_ICU_DATA != NULL && *original_ICU_DATA != 0) {
return;
}
#if defined (U_TOPBUILDDIR)
{
static char env_string[] = U_TOPBUILDDIR "data" U_FILE_SEP_STRING "out" U_FILE_SEP_STRING;
u_setDataDirectory(env_string);
return;
}
#else
{
char p[sizeof(__FILE__) + 10];
char *pBackSlash;
int i;
strcpy(p, __FILE__);
for (i=1; i<=3; i++) {
pBackSlash = strrchr(p, U_FILE_SEP_CHAR);
if (pBackSlash != NULL) {
*pBackSlash = 0;
}
}
if (pBackSlash != NULL) {
strcpy(pBackSlash, U_FILE_SEP_STRING "data" U_FILE_SEP_STRING "out" U_FILE_SEP_STRING);
u_setDataDirectory(p);
return;
}
else {
u_setDataDirectory(".."U_FILE_SEP_STRING".."U_FILE_SEP_STRING "data" U_FILE_SEP_STRING "out" U_FILE_SEP_STRING);
return;
}
}
#endif
}
static const int32_t indentLevel_offset = 3;
static const char delim = '/';
IntlTest* IntlTest::gTest = NULL;
static int32_t execCount = 0;
void it_log( UnicodeString message )
{
if (IntlTest::gTest)
IntlTest::gTest->log( message );
}
void it_logln( UnicodeString message )
{
if (IntlTest::gTest)
IntlTest::gTest->logln( message );
}
void it_logln( void )
{
if (IntlTest::gTest)
IntlTest::gTest->logln();
}
void it_info( UnicodeString message )
{
if (IntlTest::gTest)
IntlTest::gTest->info( message );
}
void it_infoln( UnicodeString message )
{
if (IntlTest::gTest)
IntlTest::gTest->infoln( message );
}
void it_infoln( void )
{
if (IntlTest::gTest)
IntlTest::gTest->infoln();
}
void it_err()
{
if (IntlTest::gTest)
IntlTest::gTest->err();
}
void it_err( UnicodeString message )
{
if (IntlTest::gTest)
IntlTest::gTest->err( message );
}
void it_errln( UnicodeString message )
{
if (IntlTest::gTest)
IntlTest::gTest->errln( message );
}
void it_dataerr( UnicodeString message )
{
if (IntlTest::gTest)
IntlTest::gTest->dataerr( message );
}
void it_dataerrln( UnicodeString message )
{
if (IntlTest::gTest)
IntlTest::gTest->dataerrln( message );
}
IntlTest::IntlTest()
{
caller = NULL;
testPath = NULL;
LL_linestart = TRUE;
errorCount = 0;
dataErrorCount = 0;
verbose = FALSE;
no_err_msg = FALSE;
warn_on_missing_data = FALSE;
quick = FALSE;
leaks = FALSE;
testoutfp = stdout;
LL_indentlevel = indentLevel_offset;
}
void IntlTest::setCaller( IntlTest* callingTest )
{
caller = callingTest;
if (caller) {
verbose = caller->verbose;
no_err_msg = caller->no_err_msg;
quick = caller->quick;
testoutfp = caller->testoutfp;
LL_indentlevel = caller->LL_indentlevel + indentLevel_offset;
}
}
UBool IntlTest::callTest( IntlTest& testToBeCalled, char* par )
{
execCount--; testToBeCalled.setCaller( this );
return testToBeCalled.runTest( testPath, par );
}
void IntlTest::setPath( char* pathVal )
{
this->testPath = pathVal;
}
UBool IntlTest::setVerbose( UBool verboseVal )
{
UBool rval = this->verbose;
this->verbose = verboseVal;
return rval;
}
UBool IntlTest::setWarnOnMissingData( UBool warn_on_missing_dataVal )
{
UBool rval = this->warn_on_missing_data;
this->warn_on_missing_data = warn_on_missing_dataVal;
return rval;
}
UBool IntlTest::setNoErrMsg( UBool no_err_msgVal )
{
UBool rval = this->no_err_msg;
this->no_err_msg = no_err_msgVal;
return rval;
}
UBool IntlTest::setQuick( UBool quickVal )
{
UBool rval = this->quick;
this->quick = quickVal;
return rval;
}
UBool IntlTest::setLeaks( UBool leaksVal )
{
UBool rval = this->leaks;
this->leaks = leaksVal;
return rval;
}
int32_t IntlTest::getErrors( void )
{
return errorCount;
}
int32_t IntlTest::getDataErrors( void )
{
return dataErrorCount;
}
UBool IntlTest::runTest( char* name, char* par )
{
UBool rval;
char* pos = NULL;
if (name)
pos = strchr( name, delim ); if (pos) {
testPath = pos+1; *pos = 0; }else{
testPath = NULL;
}
if (!name || (name[0] == 0) || (strcmp(name, "*") == 0)) {
rval = runTestLoop( NULL, par );
}else if (strcmp( name, "LIST" ) == 0) {
this->usage();
rval = TRUE;
}else{
rval = runTestLoop( name, par );
}
if (pos)
*pos = delim; return rval;
}
void IntlTest::runIndexedTest( int32_t index, UBool exec, const char* &name, char* par )
{
this->errln("*** runIndexedTest needs to be overriden! ***");
name = ""; exec = exec; index = index; par = par;
}
UBool IntlTest::runTestLoop( char* testname, char* par )
{
int32_t index = 0;
const char* name;
UBool run_this_test;
int32_t lastErrorCount;
UBool rval = FALSE;
UBool lastTestFailed;
IntlTest* saveTest = gTest;
gTest = this;
do {
this->runIndexedTest( index, FALSE, name, par );
if (!name || (name[0] == 0))
break;
if (!testname) {
run_this_test = TRUE;
}else{
run_this_test = (UBool) (strcmp( name, testname ) == 0);
}
if (run_this_test) {
lastErrorCount = errorCount;
execCount++;
this->runIndexedTest( index, TRUE, name, par );
rval = TRUE; char msg[256];
if (lastErrorCount == errorCount) {
sprintf( msg, "---OK: %s", name );
lastTestFailed = FALSE;
}else{
sprintf(msg, "---ERRORS (%li) in %s", (long)(errorCount-lastErrorCount), name);
for(int i=0;i<LL_indentlevel;i++) {
errorList += " ";
}
errorList += name;
errorList += "\n";
lastTestFailed = TRUE;
}
LL_indentlevel -= 3;
if (lastTestFailed) {
LL_message( "", TRUE);
}
LL_message( msg, TRUE);
if (lastTestFailed) {
LL_message( "", TRUE);
}
LL_indentlevel += 3;
}
index++;
}while(name);
gTest = saveTest;
return rval;
}
void IntlTest::log( const UnicodeString &message )
{
if( verbose ) {
LL_message( message, FALSE );
}
}
void IntlTest::logln( const UnicodeString &message )
{
if( verbose ) {
LL_message( message, TRUE );
}
}
void IntlTest::logln( void )
{
if( verbose ) {
LL_message( "", TRUE );
}
}
void IntlTest::info( const UnicodeString &message )
{
LL_message( message, FALSE );
}
void IntlTest::infoln( const UnicodeString &message )
{
LL_message( message, TRUE );
}
void IntlTest::infoln( void )
{
LL_message( "", TRUE );
}
int32_t IntlTest::IncErrorCount( void )
{
errorCount++;
if (caller) caller->IncErrorCount();
return errorCount;
}
int32_t IntlTest::IncDataErrorCount( void )
{
dataErrorCount++;
if (caller) caller->IncDataErrorCount();
return dataErrorCount;
}
void IntlTest::err()
{
IncErrorCount();
}
void IntlTest::err( const UnicodeString &message )
{
IncErrorCount();
if (!no_err_msg) LL_message( message, FALSE );
}
void IntlTest::errln( const UnicodeString &message )
{
IncErrorCount();
if (!no_err_msg) LL_message( message, TRUE );
}
void IntlTest::dataerr( const UnicodeString &message )
{
IncDataErrorCount();
if (!warn_on_missing_data) {
IncErrorCount();
}
if (!no_err_msg) LL_message( message, FALSE );
}
void IntlTest::dataerrln( const UnicodeString &message )
{
IncDataErrorCount();
if (!warn_on_missing_data) {
IncErrorCount();
}
if (!no_err_msg) LL_message( message, TRUE );
}
void IntlTest::log(const char *fmt, ...)
{
char buffer[4000];
va_list ap;
va_start(ap, fmt);
vsprintf(buffer, fmt, ap);
va_end(ap);
if( verbose ) {
log(UnicodeString(buffer, ""));
}
}
void IntlTest::logln(const char *fmt, ...)
{
char buffer[4000];
va_list ap;
va_start(ap, fmt);
vsprintf(buffer, fmt, ap);
va_end(ap);
if( verbose ) {
logln(UnicodeString(buffer, ""));
}
}
void IntlTest::info(const char *fmt, ...)
{
char buffer[4000];
va_list ap;
va_start(ap, fmt);
vsprintf(buffer, fmt, ap);
va_end(ap);
info(UnicodeString(buffer, ""));
}
void IntlTest::infoln(const char *fmt, ...)
{
char buffer[4000];
va_list ap;
va_start(ap, fmt);
vsprintf(buffer, fmt, ap);
va_end(ap);
infoln(UnicodeString(buffer, ""));
}
void IntlTest::err(const char *fmt, ...)
{
char buffer[4000];
va_list ap;
va_start(ap, fmt);
vsprintf(buffer, fmt, ap);
va_end(ap);
err(UnicodeString(buffer, ""));
}
void IntlTest::errln(const char *fmt, ...)
{
char buffer[4000];
va_list ap;
va_start(ap, fmt);
vsprintf(buffer, fmt, ap);
va_end(ap);
errln(UnicodeString(buffer, ""));
}
void IntlTest::dataerrln(const char *fmt, ...)
{
char buffer[4000];
va_list ap;
va_start(ap, fmt);
vsprintf(buffer, fmt, ap);
va_end(ap);
dataerrln(UnicodeString(buffer, ""));
}
void IntlTest::printErrors()
{
IntlTest::LL_message(errorList, TRUE);
}
void IntlTest::LL_message( UnicodeString message, UBool newline )
{
static const UChar indentUChars[] = {
'\n',
32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32,
32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32,
32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32,
32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32,
32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32,
32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32,
32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32,
32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32,
32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32,
32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32
};
UnicodeString indent(FALSE, indentUChars, 1 + LL_indentlevel);
char buffer[10000];
int32_t length;
length = indent.extract(1, indent.length(), buffer, sizeof(buffer));
if (length > 0) {
fwrite(buffer, sizeof(*buffer), length, (FILE *)testoutfp);
}
message.findAndReplace(UnicodeString((UChar)'\n'), indent);
length = message.extract(0, message.length(), buffer, sizeof(buffer));
if (length > 0) {
length = length > 10000 ? 10000 : length;
fwrite(buffer, sizeof(*buffer), length, (FILE *)testoutfp);
}
if (newline) {
char newLine = '\n';
fwrite(&newLine, sizeof(newLine), 1, (FILE *)testoutfp);
}
fflush((FILE *)testoutfp);
}
void IntlTest::usage( void )
{
UBool save_verbose = setVerbose( TRUE );
logln("Test names:");
logln("-----------");
int32_t index = 0;
const char* name = NULL;
do{
this->runIndexedTest( index, FALSE, name );
if (!name) break;
logln(name);
index++;
}while (name && (name[0] != 0));
setVerbose( save_verbose );
}
UBool
IntlTest::run_phase2( char* name, char* par ) {
UnicodeString* strLeak = new UnicodeString("forced leak"); strLeak->append(" for verifying purify filter");
return this->runTest( name, par );
}
#if UCONFIG_NO_LEGACY_CONVERSION
# define TRY_CNV_1 "iso-8859-1"
# define TRY_CNV_2 "ibm-1208"
#else
# define TRY_CNV_1 "iso-8859-7"
# define TRY_CNV_2 "sjis"
#endif
int
main(int argc, char* argv[])
{
UBool syntax = FALSE;
UBool all = FALSE;
UBool verbose = FALSE;
UBool no_err_msg = FALSE;
UBool quick = TRUE;
UBool name = FALSE;
UBool leaks = FALSE;
UBool warnOnMissingData = FALSE;
UBool defaultDataFound = FALSE;
UErrorCode errorCode = U_ZERO_ERROR;
UConverter *cnv = NULL;
const char *warnOrErr = "Failure";
UDate startTime, endTime;
int32_t diffTime;
U_MAIN_INIT_ARGS(argc, argv);
startTime = uprv_getUTCtime();
for (int i = 1; i < argc; ++i) {
if (argv[i][0] == '-') {
const char* str = argv[i] + 1;
if (strcmp("verbose", str) == 0 ||
strcmp("v", str) == 0)
verbose = TRUE;
else if (strcmp("noerrormsg", str) == 0 ||
strcmp("n", str) == 0)
no_err_msg = TRUE;
else if (strcmp("exhaustive", str) == 0 ||
strcmp("e", str) == 0)
quick = FALSE;
else if (strcmp("all", str) == 0 ||
strcmp("a", str) == 0)
all = TRUE;
else if (strcmp("leaks", str) == 0 ||
strcmp("l", str) == 0)
leaks = TRUE;
else if (strcmp("w", str) == 0) {
warnOnMissingData = TRUE;
warnOrErr = "WARNING";
}
else {
syntax = TRUE;
}
}else{
name = TRUE;
}
}
if (!all && !name) {
all = TRUE;
} else if (all && name) {
syntax = TRUE;
}
if (syntax) {
fprintf(stdout,
"### Syntax:\n"
"### IntlTest [-option1 -option2 ...] [testname1 testname2 ...] \n"
"### where options are: verbose (v), all (a), noerrormsg (n), \n"
"### exhaustive (e), leaks (l)"
"### (Specify either -all (shortcut -a) or a test name). \n"
"### -all will run all of the tests.\n"
"### \n"
"### To get a list of the test names type: intltest LIST \n"
"### To run just the utility tests type: intltest utility \n"
"### \n"
"### Test names can be nested using slashes (\"testA/subtest1\") \n"
"### For example to list the utility tests type: intltest utility/LIST \n"
"### To run just the Locale test type: intltest utility/LocaleTest \n"
"### \n"
"### A parameter can be specified for a test by appending '@' and the value \n"
"### to the testname. \n\n");
return 1;
}
UBool all_tests_exist = TRUE;
MajorTestLevel major;
major.setVerbose( verbose );
major.setNoErrMsg( no_err_msg );
major.setQuick( quick );
major.setLeaks( leaks );
major.setWarnOnMissingData( warnOnMissingData );
fprintf(stdout, "-----------------------------------------------\n");
fprintf(stdout, " IntlTest (C++) Test Suite for \n");
fprintf(stdout, " International Components for Unicode %s\n", U_ICU_VERSION);
fprintf(stdout, "-----------------------------------------------\n");
fprintf(stdout, " Options: \n");
fprintf(stdout, " all (a) : %s\n", (all? "On" : "Off"));
fprintf(stdout, " Verbose (v) : %s\n", (verbose? "On" : "Off"));
fprintf(stdout, " No error messages (n) : %s\n", (no_err_msg? "On" : "Off"));
fprintf(stdout, " Exhaustive (e) : %s\n", (!quick? "On" : "Off"));
fprintf(stdout, " Leaks (l) : %s\n", (leaks? "On" : "Off"));
fprintf(stdout, " Warn on missing data (w) : %s\n", (warnOnMissingData? "On" : "Off"));
fprintf(stdout, "-----------------------------------------------\n");
u_init(&errorCode);
if (U_FAILURE(errorCode)) {
fprintf(stderr,
"#### Note: ICU Init without build-specific setDataDirectory() failed.\n");
defaultDataFound = FALSE;
}
else {
defaultDataFound = TRUE;
}
u_cleanup();
errorCode = U_ZERO_ERROR;
if (!defaultDataFound) {
IntlTest::setICU_DATA(); }
u_init(&errorCode);
if (U_FAILURE(errorCode)) {
fprintf(stderr,
"#### 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) {
fprintf(stderr, "*** Exiting. Use the '-w' option if data files were\n*** purposely removed, to continue test anyway.\n");
u_cleanup();
return 1;
}
}
errorCode = U_ZERO_ERROR;
cnv = ucnv_open(0, &errorCode);
if(cnv != 0) {
ucnv_close(cnv);
} else {
fprintf(stdout,
"*** %s! The default converter [%s] cannot be opened.\n"
"*** Check the ICU_DATA environment variable and\n"
"*** check that the data files are present.\n",
warnOrErr, ucnv_getDefaultName());
if(!warnOnMissingData) {
fprintf(stdout, "*** Exiting. Use the '-w' option if data files were\n*** purposely removed, to continue test anyway.\n");
return 1;
}
}
cnv = ucnv_open(TRY_CNV_2, &errorCode);
if(cnv != 0) {
ucnv_close(cnv);
} else {
fprintf(stdout,
"*** %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) {
fprintf(stdout, "*** Exiting. Use the '-w' option if data files were\n*** purposely removed, to continue test anyway.\n");
return 1;
}
}
UResourceBundle *rb = ures_open(0, "en", &errorCode);
ures_close(rb);
if(U_FAILURE(errorCode)) {
fprintf(stdout,
"*** %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) {
fprintf(stdout, "*** Exiting. Use the '-w' option if data files were\n*** purposely removed, to continue test anyway.\n");
return 1;
}
}
Locale originalLocale;
if (all) {
major.runTest();
if (leaks) {
major.run_phase2( NULL, NULL );
}
}else{
for (int i = 1; i < argc; ++i) {
if (argv[i][0] != '-') {
char* name = argv[i];
fprintf(stdout, "\n=== Handling test: %s: ===\n", name);
char* parameter = strchr( name, '@' );
if (parameter) {
*parameter = 0;
parameter += 1;
}
execCount = 0;
UBool res = major.runTest( name, parameter );
if (leaks && res) {
major.run_phase2( name, parameter );
}
if (!res || (execCount <= 0)) {
fprintf(stdout, "\n---ERROR: Test doesn't exist: %s!\n", name);
all_tests_exist = FALSE;
}
}
}
}
#if !UCONFIG_NO_FORMATTING
CalendarTimeZoneTest::cleanup();
#endif
free(_testDataPath);
_testDataPath = 0;
Locale lastDefaultLocale;
if (originalLocale != lastDefaultLocale) {
major.errln("FAILURE: A test changed the default locale without resetting it.");
}
fprintf(stdout, "\n--------------------------------------\n");
if (major.getErrors() == 0) {
u_cleanup();
u_cleanup();
fprintf(stdout, "OK: All tests passed without error.\n");
if (major.getDataErrors() != 0) {
fprintf(stdout, "\t*WARNING* some data-loading errors were ignored by the -w option.\n");
}
}else{
fprintf(stdout, "Errors in total: %ld.\n", (long)major.getErrors());
major.printErrors();
if (major.getDataErrors() != 0) {
fprintf(stdout, "\t*Note* some errors are data-loading related. If the data used is not the \n"
"\tstock ICU data (i.e some have been added or removed), consider using\n"
"\tthe '-w' option to turn these errors into warnings.\n");
}
u_cleanup();
}
fprintf(stdout, "--------------------------------------\n");
if (execCount <= 0) {
fprintf(stdout, "***** Not all called tests actually exist! *****\n");
}
endTime = uprv_getUTCtime();
diffTime = (int32_t)(endTime - startTime);
printf("Elapsed Time: %02d:%02d:%02d.%03d\n",
(int)((diffTime%U_MILLIS_PER_DAY)/U_MILLIS_PER_HOUR),
(int)((diffTime%U_MILLIS_PER_HOUR)/U_MILLIS_PER_MINUTE),
(int)((diffTime%U_MILLIS_PER_MINUTE)/U_MILLIS_PER_SECOND),
(int)(diffTime%U_MILLIS_PER_SECOND));
return major.getErrors();
}
const char* IntlTest::loadTestData(UErrorCode& err){
if( _testDataPath == NULL){
const char* directory=NULL;
UResourceBundle* test =NULL;
char* tdpath=NULL;
const char* tdrelativepath;
#if defined (U_TOPBUILDDIR)
tdrelativepath = "test"U_FILE_SEP_STRING"testdata"U_FILE_SEP_STRING"out"U_FILE_SEP_STRING;
directory = U_TOPBUILDDIR;
#else
tdrelativepath = ".."U_FILE_SEP_STRING"test"U_FILE_SEP_STRING"testdata"U_FILE_SEP_STRING"out"U_FILE_SEP_STRING;
directory = pathToDataDirectory();
#endif
tdpath = (char*) malloc(sizeof(char) *(( strlen(directory) * strlen(tdrelativepath)) + 100));
strcpy(tdpath, directory);
strcat(tdpath, tdrelativepath);
strcat(tdpath,"testdata");
test=ures_open(tdpath, "testtypes", &err);
if(U_FAILURE(err)){
err = U_FILE_ACCESS_ERROR;
it_errln((UnicodeString)"Could not load testtypes.res in testdata bundle with path " + tdpath + (UnicodeString)" - " + u_errorName(err));
return "";
}
ures_close(test);
_testDataPath = tdpath;
return _testDataPath;
}
return _testDataPath;
}
const char* IntlTest::getTestDataPath(UErrorCode& err) {
return loadTestData(err);
}
const char *IntlTest::getSourceTestData(UErrorCode& ) {
const char *srcDataDir = NULL;
#ifdef U_TOPSRCDIR
srcDataDir = U_TOPSRCDIR U_FILE_SEP_STRING"test"U_FILE_SEP_STRING"testdata"U_FILE_SEP_STRING;
#else
srcDataDir = ".."U_FILE_SEP_STRING".."U_FILE_SEP_STRING"test"U_FILE_SEP_STRING"testdata"U_FILE_SEP_STRING;
FILE *f = fopen(".."U_FILE_SEP_STRING".."U_FILE_SEP_STRING"test"U_FILE_SEP_STRING"testdata"U_FILE_SEP_STRING"rbbitst.txt", "r");
if (f) {
fclose(f);
}
else {
srcDataDir = ".."U_FILE_SEP_STRING".."U_FILE_SEP_STRING".."U_FILE_SEP_STRING"test"U_FILE_SEP_STRING"testdata"U_FILE_SEP_STRING;
}
#endif
return srcDataDir;
}
const char* IntlTest::fgDataDir = NULL;
const char * IntlTest::pathToDataDirectory()
{
if(fgDataDir != NULL) {
return fgDataDir;
}
#if defined (U_TOPSRCDIR)
{
fgDataDir = U_TOPSRCDIR U_FILE_SEP_STRING "data" U_FILE_SEP_STRING;
}
#else
{
static char p[sizeof(__FILE__) + 10];
char *pBackSlash;
int i;
strcpy(p, __FILE__);
for (i=1; i<=3; i++) {
pBackSlash = strrchr(p, U_FILE_SEP_CHAR);
if (pBackSlash != NULL) {
*pBackSlash = 0;
}
}
if (pBackSlash != NULL) {
strcpy(pBackSlash, U_FILE_SEP_STRING "data" U_FILE_SEP_STRING );
fgDataDir = p;
}
else {
FILE *file = fopen(".."U_FILE_SEP_STRING".."U_FILE_SEP_STRING "data" U_FILE_SEP_STRING "Makefile.in", "r");
if (file) {
fclose(file);
fgDataDir = ".."U_FILE_SEP_STRING".."U_FILE_SEP_STRING "data" U_FILE_SEP_STRING;
}
else {
fgDataDir = ".."U_FILE_SEP_STRING".."U_FILE_SEP_STRING".."U_FILE_SEP_STRING "data" U_FILE_SEP_STRING;
}
}
}
#endif
return fgDataDir;
}
UnicodeString CharsToUnicodeString(const char* chars)
{
UnicodeString str(chars, ""); return str.unescape();
}
UnicodeString ctou(const char* chars) {
return CharsToUnicodeString(chars);
}
#define RAND_M (714025)
#define RAND_IA (1366)
#define RAND_IC (150889)
static int32_t RAND_SEED;
float IntlTest::random(int32_t* seedp) {
static int32_t iy, ir[98];
static UBool first=TRUE;
int32_t j;
if (*seedp < 0 || first) {
first = FALSE;
if ((*seedp=(RAND_IC-(*seedp)) % RAND_M) < 0) *seedp = -(*seedp);
for (j=1;j<=97;++j) {
*seedp=(RAND_IA*(*seedp)+RAND_IC) % RAND_M;
ir[j]=(*seedp);
}
*seedp=(RAND_IA*(*seedp)+RAND_IC) % RAND_M;
iy=(*seedp);
}
j=(int32_t)(1 + 97.0*iy/RAND_M);
U_ASSERT(j>=1 && j<=97);
iy=ir[j];
*seedp=(RAND_IA*(*seedp)+RAND_IC) % RAND_M;
ir[j]=(*seedp);
return (float) iy/RAND_M;
}
float IntlTest::random() {
return random(&RAND_SEED);
}
static inline UChar toHex(int32_t i) {
return (UChar)(i + (i < 10 ? 0x30 : (0x41 - 10)));
}
static UnicodeString& escape(const UnicodeString& s, UnicodeString& result) {
for (int32_t i=0; i<s.length(); ++i) {
UChar c = s[i];
if (c <= (UChar)0x7F) {
result += c;
} else {
result += (UChar)0x5c;
result += (UChar)0x75;
result += toHex((c >> 12) & 0xF);
result += toHex((c >> 8) & 0xF);
result += toHex((c >> 4) & 0xF);
result += toHex( c & 0xF);
}
}
return result;
}
#define VERBOSE_ASSERTIONS
UBool IntlTest::assertTrue(const char* message, UBool condition, UBool quiet) {
if (!condition) {
errln("FAIL: assertTrue() failed: %s", message);
} else if (!quiet) {
logln("Ok: %s", message);
}
return condition;
}
UBool IntlTest::assertFalse(const char* message, UBool condition, UBool quiet) {
if (condition) {
errln("FAIL: assertFalse() failed: %s", message);
} else if (!quiet) {
logln("Ok: %s", message);
}
return !condition;
}
UBool IntlTest::assertSuccess(const char* message, UErrorCode ec) {
if (U_FAILURE(ec)) {
errln("FAIL: %s (%s)", message, u_errorName(ec));
return FALSE;
}
return TRUE;
}
UBool IntlTest::assertEquals(const char* message,
const UnicodeString& expected,
const UnicodeString& actual) {
if (expected != actual) {
errln((UnicodeString)"FAIL: " + message + "; got " +
prettify(actual) +
"; expected " + prettify(expected));
return FALSE;
}
#ifdef VERBOSE_ASSERTIONS
else {
logln((UnicodeString)"Ok: " + message + "; got " + prettify(actual));
}
#endif
return TRUE;
}
UBool IntlTest::assertEquals(const char* message,
const char* expected,
const char* actual) {
if (uprv_strcmp(expected, actual) != 0) {
errln((UnicodeString)"FAIL: " + message + "; got \"" +
actual +
"\"; expected \"" + expected + "\"");
return FALSE;
}
#ifdef VERBOSE_ASSERTIONS
else {
logln((UnicodeString)"Ok: " + message + "; got \"" + actual + "\"");
}
#endif
return TRUE;
}
#if !UCONFIG_NO_FORMATTING
UBool IntlTest::assertEquals(const char* message,
const Formattable& expected,
const Formattable& actual) {
if (expected != actual) {
errln((UnicodeString)"FAIL: " + message + "; got " +
toString(actual) +
"; expected " + toString(expected));
return FALSE;
}
#ifdef VERBOSE_ASSERTIONS
else {
logln((UnicodeString)"Ok: " + message + "; got " + toString(actual));
}
#endif
return TRUE;
}
#endif
static char ASSERT_BUF[256];
static const char* extractToAssertBuf(const UnicodeString& message) {
UnicodeString buf;
escape(message, buf);
buf.extract(0, 0x7FFFFFFF, ASSERT_BUF, sizeof(ASSERT_BUF)-1, 0);
ASSERT_BUF[sizeof(ASSERT_BUF)-1] = 0;
return ASSERT_BUF;
}
UBool IntlTest::assertTrue(const UnicodeString& message, UBool condition, UBool quiet) {
return assertTrue(extractToAssertBuf(message), condition, quiet);
}
UBool IntlTest::assertFalse(const UnicodeString& message, UBool condition, UBool quiet) {
return assertFalse(extractToAssertBuf(message), condition, quiet);
}
UBool IntlTest::assertSuccess(const UnicodeString& message, UErrorCode ec) {
return assertSuccess(extractToAssertBuf(message), ec);
}
UBool IntlTest::assertEquals(const UnicodeString& message,
const UnicodeString& expected,
const UnicodeString& actual) {
return assertEquals(extractToAssertBuf(message), expected, actual);
}
UBool IntlTest::assertEquals(const UnicodeString& message,
const char* expected,
const char* actual) {
return assertEquals(extractToAssertBuf(message), expected, actual);
}
UBool IntlTest::isICUVersionAtLeast(const UVersionInfo x) {
UVersionInfo v;
u_getVersion(v);
return (uprv_memcmp(v, x, U_MAX_VERSION_LENGTH) >= 0);
}
#if !UCONFIG_NO_FORMATTING
UBool IntlTest::assertEquals(const UnicodeString& message,
const Formattable& expected,
const Formattable& actual) {
return assertEquals(extractToAssertBuf(message), expected, actual);
}
#endif