#if defined(hpux)
# ifndef _INCLUDE_POSIX_SOURCE
# define _INCLUDE_POSIX_SOURCE
# endif
#endif
#include "unicode/utypes.h"
#include "unicode/ustring.h"
#include "umutex.h"
#include "cmemory.h"
#include "cstring.h"
#include "uparse.h"
#include "unicode/resbund.h"
#include "unicode/udata.h"
#include "unicode/uloc.h"
#include "unicode/locid.h"
#include "putilimp.h"
#if !defined(WIN32) && !defined(XP_MAC) && !defined(U_RHAPSODY)
#define POSIX 1
#endif
#if defined(POSIX) || defined(U_SOLARIS) || defined(U_AIX) || defined(U_HPUX)
#define HAVE_IMP
#if (ICU_USE_THREADS == 1)
#include <pthread.h>
#endif
#if defined(__hpux) && defined(HPUX_CMA)
# if defined(read) // read being defined as cma_read causes trouble with iostream::read
# undef read
# endif
#endif
#ifndef __EXTENSIONS__
#define __EXTENSIONS__
#endif
#include <signal.h>
#ifndef _XPG4_2
#define _XPG4_2
#endif
#ifndef __USE_XOPEN_EXTENDED
#define __USE_XOPEN_EXTENDED
#endif
#ifndef _INCLUDE_XOPEN_SOURCE_EXTENDED
#define _INCLUDE_XOPEN_SOURCE_EXTENDED
#endif
#include <unistd.h>
#endif
#ifdef sleep
#undef sleep
#endif
#include "tsmthred.h"
MultithreadTest::MultithreadTest()
{
}
MultithreadTest::~MultithreadTest()
{
}
#if (ICU_USE_THREADS==0)
void MultithreadTest::runIndexedTest( int32_t index, UBool exec,
const char* &name, char* par ) {
if (exec) logln("TestSuite MultithreadTest: ");
if(index == 0)
name = "NO_THREADED_TESTS";
else
name = "";
if(exec) { logln("MultithreadTest - test DISABLED. ICU_USE_THREADS set to 0, check your configuration if this is a problem..");
}
}
#else
#include <stdio.h>
#include <string.h>
#include <ctype.h> // tolower, toupper
#include "unicode/putil.h"
#include "unicode/numfmt.h"
#include "unicode/choicfmt.h"
#include "unicode/msgfmt.h"
#include "unicode/locid.h"
#include "unicode/ucol.h"
#include "unicode/calendar.h"
#include "ucaconf.h"
class SimpleThread
{
public:
SimpleThread();
virtual ~SimpleThread();
int32_t start(void); UBool isRunning();
virtual void run(void) = 0; void *fImplementation;
public:
static void sleep(int32_t millis); static void errorFunc(); };
void SimpleThread::errorFunc() {
}
#ifdef WIN32
#define HAVE_IMP
# define VC_EXTRALEAN
# define WIN32_LEAN_AND_MEAN
# define NOUSER
# define NOSERVICE
# define NOIME
# define NOMCX
#include <windows.h>
#include <process.h>
struct Win32ThreadImplementation
{
HANDLE fHandle;
unsigned int fThreadID;
};
extern "C" unsigned int __stdcall SimpleThreadProc(void *arg)
{
((SimpleThread*)arg)->run();
return 0;
}
SimpleThread::SimpleThread()
:fImplementation(0)
{
Win32ThreadImplementation *imp = new Win32ThreadImplementation;
imp->fHandle = 0;
fImplementation = imp;
}
SimpleThread::~SimpleThread()
{
Win32ThreadImplementation *imp = (Win32ThreadImplementation*)fImplementation;
if (imp != 0) {
if (imp->fHandle != 0) {
CloseHandle(imp->fHandle);
imp->fHandle = 0;
}
}
delete (Win32ThreadImplementation*)fImplementation;
}
int32_t SimpleThread::start()
{
Win32ThreadImplementation *imp = (Win32ThreadImplementation*)fImplementation;
if(imp->fHandle != NULL) {
return -1;
}
imp->fHandle = (HANDLE) _beginthreadex(
NULL, 0x20000, SimpleThreadProc, (void *)this, 0, &imp->fThreadID );
if (imp->fHandle == 0) {
int err = errno;
if (err == 0) {
err = -1;
}
return err;
}
return 0;
}
UBool SimpleThread::isRunning() {
Win32ThreadImplementation *imp = (Win32ThreadImplementation*)fImplementation;
bool success;
DWORD threadExitCode;
if (imp->fHandle == 0) {
return FALSE;
}
success = GetExitCodeThread(imp->fHandle, &threadExitCode) != 0;
if (! success) {
return FALSE;
}
return (threadExitCode == STILL_ACTIVE);
}
void SimpleThread::sleep(int32_t millis)
{
::Sleep(millis);
}
#elif defined XP_MAC
#define HAVE_IMP
SimpleThread::SimpleThread()
{}
SimpleThread::~SimpleThread()
{}
int32_t
SimpleThread::start()
{ return 0; }
void
SimpleThread::run()
{}
void
SimpleThread::sleep(int32_t millis)
{}
UBool
SimpleThread::isRunning() {
return FALSE;
}
#endif
#if defined(POSIX)||defined(U_SOLARIS)||defined(U_AIX)||defined(U_HPUX)
#define HAVE_IMP
struct PosixThreadImplementation
{
pthread_t fThread;
UBool fRunning;
UBool fRan;
};
extern "C" void* SimpleThreadProc(void *arg)
{
SimpleThread *This = (SimpleThread *)arg;
This->run();
PosixThreadImplementation *imp = (PosixThreadImplementation*)This->fImplementation;
umtx_lock(NULL);
imp->fRunning = FALSE;
umtx_unlock(NULL);
return 0;
}
SimpleThread::SimpleThread()
{
PosixThreadImplementation *imp = new PosixThreadImplementation;
imp->fRunning = FALSE;
imp->fRan = FALSE;
fImplementation = imp;
}
SimpleThread::~SimpleThread()
{
PosixThreadImplementation *imp = (PosixThreadImplementation*)fImplementation;
if (imp->fRan) {
pthread_join(imp->fThread, NULL);
}
delete imp;
fImplementation = (void *)0xdeadbeef;
}
int32_t SimpleThread::start()
{
int32_t rc;
static pthread_attr_t attr;
static UBool attrIsInitialized = FALSE;
PosixThreadImplementation *imp = (PosixThreadImplementation*)fImplementation;
imp->fRunning = TRUE;
imp->fRan = TRUE;
#ifdef HPUX_CMA
if (attrIsInitialized == FALSE) {
rc = pthread_attr_create(&attr);
attrIsInitialized = TRUE;
}
rc = pthread_create(&(imp->fThread),attr,&SimpleThreadProc,(void*)this);
#else
if (attrIsInitialized == FALSE) {
rc = pthread_attr_init(&attr);
#if defined(OS390)
{
int detachstate = 0;
pthread_attr_setdetachstate(&attr, &detachstate);
}
#else
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
#endif
attrIsInitialized = TRUE;
}
rc = pthread_create(&(imp->fThread),&attr,&SimpleThreadProc,(void*)this);
#endif
if (rc != 0) {
imp->fRan = FALSE;
imp->fRunning = FALSE;
}
return rc;
}
UBool
SimpleThread::isRunning() {
PosixThreadImplementation *imp = (PosixThreadImplementation*)fImplementation;
umtx_lock(NULL);
UBool retVal = imp->fRunning;
umtx_unlock(NULL);
return retVal;
}
void SimpleThread::sleep(int32_t millis)
{
#ifdef U_SOLARIS
sigignore(SIGALRM);
#endif
#ifdef HPUX_CMA
cma_sleep(millis/100);
#elif defined(U_HPUX) || defined(OS390)
millis *= 1000;
while(millis >= 1000000) {
usleep(999999);
millis -= 1000000;
}
if(millis > 0) {
usleep(millis);
}
#else
usleep(millis * 1000);
#endif
}
#endif
#ifndef HAVE_IMP
#error No implementation for threads! Cannot test.
0 = 216; #endif
void MultithreadTest::runIndexedTest( int32_t index, UBool exec,
const char* &name, char* ) {
if (exec)
logln("TestSuite MultithreadTest: ");
switch (index) {
case 0:
name = "TestThreads";
if (exec)
TestThreads();
break;
case 1:
name = "TestMutex";
if (exec)
TestMutex();
break;
case 2:
name = "TestThreadedIntl";
#if !UCONFIG_NO_FORMATTING
if (exec) {
TestThreadedIntl();
}
#endif
break;
case 3:
name = "TestCollators";
#if !UCONFIG_NO_COLLATION
if (exec) {
TestCollators();
}
#endif
break;
case 4:
name = "TestString";
if (exec) {
TestString();
}
break;
default:
name = "";
break; }
}
#define THREADTEST_NRTHREADS 8
class TestThreadsThread : public SimpleThread
{
public:
TestThreadsThread(char* whatToChange) { fWhatToChange = whatToChange; }
virtual void run() { SimpleThread::sleep(1000);
Mutex m;
*fWhatToChange = '*';
}
private:
char *fWhatToChange;
};
void MultithreadTest::TestThreads()
{
char threadTestChars[THREADTEST_NRTHREADS + 1];
SimpleThread *threads[THREADTEST_NRTHREADS];
int32_t i;
for(i=0;i<THREADTEST_NRTHREADS;i++)
{
threadTestChars[i] = ' ';
threads[i] = new TestThreadsThread(&threadTestChars[i]);
}
threadTestChars[THREADTEST_NRTHREADS] = '\0';
logln("->" + UnicodeString(threadTestChars) + "<- Firing off threads.. ");
for(i=0;i<THREADTEST_NRTHREADS;i++)
{
if (threads[i]->start() != 0) {
errln("Error starting thread %d", i);
}
SimpleThread::sleep(200);
logln(" Subthread started.");
}
logln("Waiting for threads to be set..");
int32_t patience = 40;
while(patience--)
{
int32_t count = 0;
umtx_lock(NULL);
for(i=0;i<THREADTEST_NRTHREADS;i++)
{
if(threadTestChars[i] == '*')
{
count++;
}
}
umtx_unlock(NULL);
if(count == THREADTEST_NRTHREADS)
{
logln("->" + UnicodeString(threadTestChars) + "<- Got all threads! cya");
for(i=0;i<THREADTEST_NRTHREADS;i++)
{
delete threads[i];
}
return;
}
logln("->" + UnicodeString(threadTestChars) + "<- Waiting..");
SimpleThread::sleep(500);
}
errln("->" + UnicodeString(threadTestChars) + "<- PATIENCE EXCEEDED!! Still missing some.");
for(i=0;i<THREADTEST_NRTHREADS;i++)
{
delete threads[i];
}
}
class TestMutexThread1 : public SimpleThread
{
public:
TestMutexThread1() : fDone(FALSE) {}
virtual void run()
{
Mutex m; SimpleThread::sleep(900); fDone = TRUE; }
public:
UBool fDone;
};
class TestMutexThread2 : public SimpleThread
{
public:
TestMutexThread2(TestMutexThread1& r) : fOtherThread(r), fDone(FALSE), fErr(FALSE) {}
virtual void run()
{
SimpleThread::sleep(500); fElapsed = uprv_getUTCtime();
{
Mutex m;
fElapsed = uprv_getUTCtime() - fElapsed;
if(fOtherThread.fDone == FALSE)
fErr = TRUE;
fDone = TRUE; }
}
public:
TestMutexThread1 & fOtherThread;
UBool fDone, fErr;
UDate fElapsed;
private:
TestMutexThread2& operator=(const TestMutexThread2&) { return *this; }
};
void MultithreadTest::TestMutex()
{
if(verbose)
printf("Before mutex.\n");
{
Mutex m;
if(verbose)
printf(" Exited 2nd mutex\n");
}
if(verbose)
printf("exited 1st mutex. Now testing with threads:");
TestMutexThread1 thread1;
TestMutexThread2 thread2(thread1);
if (thread2.start() != 0 ||
thread1.start() != 0 ) {
errln("Error starting threads.");
}
for(int32_t patience = 12; patience > 0;patience--)
{
if(thread1.fDone && verbose)
printf("Thread1 done\n");
if(thread1.fDone && thread2.fDone)
{
if(thread2.fErr)
errln("Thread 2 says: thread1 didn't run before I aquired the mutex.");
logln("took %lu seconds for thread2 to aquire the mutex.", (int)(thread2.fElapsed/U_MILLIS_PER_DAY));
return;
}
SimpleThread::sleep(1000);
}
if(verbose)
printf("patience exceeded. [WARNING mutex may still be acquired.] ");
}
class ThreadWithStatus : public SimpleThread
{
public:
UBool getError() { return (fErrors > 0); }
UBool getError(UnicodeString& fillinError) { fillinError = fErrorString; return (fErrors > 0); }
virtual ~ThreadWithStatus(){}
protected:
ThreadWithStatus() : fErrors(0) {}
void error(const UnicodeString &error) {
fErrors++; fErrorString = error;
SimpleThread::errorFunc();
}
void error() { error("An error occured."); }
private:
int32_t fErrors;
UnicodeString fErrorString;
};
UnicodeString showDifference(const UnicodeString& expected, const UnicodeString& result)
{
UnicodeString res;
res = expected + "<Expected\n";
if(expected.length() != result.length())
res += " [ Different lengths ] \n";
else
{
for(int32_t i=0;i<expected.length();i++)
{
if(expected[i] == result[i])
{
res += " ";
}
else
{
res += "|";
}
}
res += "<Differences";
res += "\n";
}
res += result + "<Result\n";
return res;
}
const int kFormatThreadIterations = 20; const int kFormatThreadThreads = 10; const int kFormatThreadPatience = 60;
#if !UCONFIG_NO_FORMATTING
struct FormatThreadTestData
{
double number;
UnicodeString string;
FormatThreadTestData(double a, const UnicodeString& b) : number(a),string(b) {}
} ;
void errorToString(UErrorCode theStatus, UnicodeString &string)
{
string=u_errorName(theStatus);
}
void formatErrorMessage(UErrorCode &realStatus, const UnicodeString& pattern, const Locale& theLocale,
UErrorCode inStatus0, const Locale &inCountry2, double currency3, UnicodeString &result)
{
if(U_FAILURE(realStatus))
return;
UnicodeString errString1;
errorToString(inStatus0, errString1);
UnicodeString countryName2;
inCountry2.getDisplayCountry(theLocale,countryName2);
Formattable myArgs[] = {
Formattable((int32_t)inStatus0), Formattable(errString1), Formattable(countryName2), Formattable(currency3) };
MessageFormat *fmt = new MessageFormat("MessageFormat's API is broken!!!!!!!!!!!",realStatus);
fmt->setLocale(theLocale);
fmt->applyPattern(pattern, realStatus);
if (U_FAILURE(realStatus)) {
delete fmt;
return;
}
FieldPosition ignore = 0;
fmt->format(myArgs,4,result,ignore,realStatus);
delete fmt;
};
UBool U_CALLCONV isAcceptable(void *, const char *, const char *, const UDataInfo *) {
return TRUE;
}
class FormatThreadTest : public ThreadWithStatus
{
public:
int fNum;
int fTraceInfo;
FormatThreadTest() : ThreadWithStatus(),
fNum(0),
fTraceInfo(0),
fOffset(0)
{
static int32_t fgOffset = 0;
fgOffset += 3;
fOffset = fgOffset;
}
virtual void run()
{
fTraceInfo = 1;
NumberFormat *formatter = NULL;
NumberFormat *percentFormatter = NULL;
UErrorCode status = U_ZERO_ERROR;
#if 0
for (int i=0; i<4000; i++) {
status = U_ZERO_ERROR;
UDataMemory *data1 = udata_openChoice(0, "res", "en_US", isAcceptable, 0, &status);
UDataMemory *data2 = udata_openChoice(0, "res", "fr", isAcceptable, 0, &status);
udata_close(data1);
udata_close(data2);
if (U_FAILURE(status)) {
error("udata_openChoice failed.\n");
break;
}
}
return;
#endif
#if 1
int m;
for (m=0; m<4000; m++) {
status = U_ZERO_ERROR;
UResourceBundle *res = NULL;
const char *localeName = NULL;
Locale loc = Locale::getEnglish();
localeName = loc.getName();
res = ures_open(NULL, localeName, &status);
ures_close(res);
if (U_FAILURE(status)) {
error("Resource bundle construction failed.\n");
break;
}
}
return;
#endif
FormatThreadTestData kNumberFormatTestData[] =
{
FormatThreadTestData((double)5.0, UnicodeString("5", "")),
FormatThreadTestData( 6.0, UnicodeString("6", "")),
FormatThreadTestData( 20.0, UnicodeString("20", "")),
FormatThreadTestData( 8.0, UnicodeString("8", "")),
FormatThreadTestData( 8.3, UnicodeString("8.3", "")),
FormatThreadTestData( 12345, UnicodeString("12,345", "")),
FormatThreadTestData( 81890.23, UnicodeString("81,890.23", "")),
};
int32_t kNumberFormatTestDataLength = (int32_t)(sizeof(kNumberFormatTestData) /
sizeof(kNumberFormatTestData[0]));
FormatThreadTestData kPercentFormatTestData[] =
{
FormatThreadTestData((double)5.0, UnicodeString("500%", "")),
FormatThreadTestData( 1.0, UnicodeString("100%", "")),
FormatThreadTestData( 0.26, UnicodeString("26%", "")),
FormatThreadTestData(
16384.99, CharsToUnicodeString("1\\u00a0638\\u00a0499%") ), FormatThreadTestData(
81890.23, CharsToUnicodeString("8\\u00a0189\\u00a0023%" )),
};
int32_t kPercentFormatTestDataLength =
(int32_t)(sizeof(kPercentFormatTestData) / sizeof(kPercentFormatTestData[0]));
int32_t iteration;
status = U_ZERO_ERROR;
formatter = NumberFormat::createInstance(Locale::getEnglish(),status);
if(U_FAILURE(status)) {
error("Error on NumberFormat::createInstance()");
goto cleanupAndReturn;
}
percentFormatter = NumberFormat::createPercentInstance(Locale::getFrench(),status);
if(U_FAILURE(status)) {
error("Error on NumberFormat::createPercentInstance()");
goto cleanupAndReturn;
}
for(iteration = 0;!getError() && iteration<kFormatThreadIterations;iteration++)
{
int32_t whichLine = (iteration + fOffset)%kNumberFormatTestDataLength;
UnicodeString output;
formatter->format(kNumberFormatTestData[whichLine].number, output);
if(0 != output.compare(kNumberFormatTestData[whichLine].string)) {
error("format().. expected " + kNumberFormatTestData[whichLine].string
+ " got " + output);
goto cleanupAndReturn;
}
output.remove();
whichLine = (iteration + fOffset)%kPercentFormatTestDataLength;
percentFormatter->format(kPercentFormatTestData[whichLine].number, output);
if(0 != output.compare(kPercentFormatTestData[whichLine].string))
{
error("percent format().. \n" +
showDifference(kPercentFormatTestData[whichLine].string,output));
goto cleanupAndReturn;
}
const int kNumberOfMessageTests = 3;
UErrorCode statusToCheck;
UnicodeString patternToCheck;
Locale messageLocale;
Locale countryToCheck;
double currencyToCheck;
UnicodeString expected;
switch((iteration+fOffset) % kNumberOfMessageTests)
{
default:
case 0:
statusToCheck= U_FILE_ACCESS_ERROR;
patternToCheck= "0:Someone from {2} is receiving a #{0}"
" error - {1}. Their telephone call is costing "
"{3,number,currency}."; messageLocale= Locale("en","US");
countryToCheck= Locale("","HR");
currencyToCheck= 8192.77;
expected= "0:Someone from Croatia is receiving a #4 error - "
"U_FILE_ACCESS_ERROR. Their telephone call is costing $8,192.77.";
break;
case 1:
statusToCheck= U_INDEX_OUTOFBOUNDS_ERROR;
patternToCheck= "1:A customer in {2} is receiving a #{0} error - {1}. Their telephone call is costing {3,number,currency}."; messageLocale= Locale("de","DE@currency=DEM");
countryToCheck= Locale("","BF");
currencyToCheck= 2.32;
expected= "1:A customer in Burkina Faso is receiving a #8 error - U_INDEX_OUTOFBOUNDS_ERROR. Their telephone call is costing 2,32 DM.";
break;
case 2:
statusToCheck= U_MEMORY_ALLOCATION_ERROR;
patternToCheck= "2:user in {2} is receiving a #{0} error - {1}. "
"They insist they just spent {3,number,currency} "
"on memory."; messageLocale= Locale("de","AT@currency=ATS"); countryToCheck= Locale("","US"); currencyToCheck= 40193.12;
expected= CharsToUnicodeString(
"2:user in Vereinigte Staaten is receiving a #7 error"
" - U_MEMORY_ALLOCATION_ERROR. They insist they just spent"
" \\u00f6S 40.193,12 on memory.");
break;
}
UnicodeString result;
UErrorCode status = U_ZERO_ERROR;
formatErrorMessage(status,patternToCheck,messageLocale,statusToCheck,
countryToCheck,currencyToCheck,result);
if(U_FAILURE(status))
{
UnicodeString tmp;
errorToString(status,tmp);
error("Failure on message format, pattern=" + patternToCheck +
", error = " + tmp);
goto cleanupAndReturn;
}
if(result != expected)
{
error("PatternFormat: \n" + showDifference(expected,result));
goto cleanupAndReturn;
}
}
cleanupAndReturn:
delete formatter;
delete percentFormatter;
fTraceInfo = 2;
}
private:
int32_t fOffset; };
void MultithreadTest::TestThreadedIntl()
{
int i;
UnicodeString theErr;
UBool haveDisplayedInfo[kFormatThreadThreads];
logln("Spawning: %d threads * %d iterations each.",
kFormatThreadThreads, kFormatThreadIterations);
FormatThreadTest *tests = new FormatThreadTest[kFormatThreadThreads];
for(int32_t j = 0; j < kFormatThreadThreads; j++) {
tests[j].fNum = j;
int32_t threadStatus = tests[j].start();
if (threadStatus != 0) {
errln("System Error %d starting thread number %d.", threadStatus, j);
SimpleThread::errorFunc();
goto cleanupAndReturn;
}
haveDisplayedInfo[j] = FALSE;
}
UBool stillRunning;
do {
stillRunning = FALSE;
for(i=0;i<kFormatThreadThreads;i++) {
if (tests[i].isRunning()) {
stillRunning = TRUE;
} else if (haveDisplayedInfo[i] == FALSE) {
logln("Thread # %d is complete..", i);
if(tests[i].getError(theErr)) {
errln(UnicodeString("#") + i + ": " + theErr);
SimpleThread::errorFunc();
}
haveDisplayedInfo[i] = TRUE;
}
}
} while (stillRunning);
cleanupAndReturn:
delete [] tests;
return;
}
#endif
#if !UCONFIG_NO_COLLATION
#define kCollatorThreadThreads 10 // # of threads to spawn
#define kCollatorThreadPatience kCollatorThreadThreads*100
struct Line {
UChar buff[25];
int32_t buflen;
} ;
class CollatorThreadTest : public ThreadWithStatus
{
private:
const UCollator *coll;
const Line *lines;
int32_t noLines;
public:
CollatorThreadTest() : ThreadWithStatus(),
coll(NULL),
lines(NULL),
noLines(0)
{
};
void setCollator(UCollator *c, Line *l, int32_t nl)
{
coll = c;
lines = l;
noLines = nl;
}
virtual void run() {
int32_t line = 0;
uint8_t sk1[1024], sk2[1024];
uint8_t *oldSk = NULL, *newSk = sk1;
int32_t resLen = 0, oldLen = 0;
int32_t i = 0;
for(i = 0; i < noLines; i++) {
resLen = ucol_getSortKey(coll, lines[i].buff, lines[i].buflen, newSk, 1024);
int32_t res = 0, cmpres = 0, cmpres2 = 0;
if(oldSk != NULL) {
res = strcmp((char *)oldSk, (char *)newSk);
cmpres = ucol_strcoll(coll, lines[i-1].buff, lines[i-1].buflen, lines[i].buff, lines[i].buflen);
cmpres2 = ucol_strcoll(coll, lines[i].buff, lines[i].buflen, lines[i-1].buff, lines[i-1].buflen);
if(cmpres != -cmpres2) {
error("Compare result not symmetrical on line "+ line);
break;
}
if(((res&0x80000000) != (cmpres&0x80000000)) || (res == 0 && cmpres != 0) || (res != 0 && cmpres == 0)) {
error(UnicodeString("Difference between ucol_strcoll and sortkey compare on line ")+ UnicodeString(line));
break;
}
if(res > 0) {
error(UnicodeString("Line %i is not greater or equal than previous line ")+ UnicodeString(i));
break;
} else if(res == 0) {
res = u_strcmpCodePointOrder(lines[i-1].buff, lines[i].buff);
if (res == 0) {
error(UnicodeString("Probable error in test file on line %i (comparing identical strings)")+ UnicodeString(i));
break;
} else if (res > 0) {
error(UnicodeString("Sortkeys are identical, but code point comapare gives >0 on line ")+ UnicodeString(i));
break;
}
}
}
oldSk = newSk;
oldLen = resLen;
newSk = (newSk == sk1)?sk2:sk1;
}
}
};
void MultithreadTest::TestCollators()
{
UErrorCode status = U_ZERO_ERROR;
FILE *testFile = NULL;
char testDataPath[1024];
strcpy(testDataPath, IntlTest::getSourceTestData(status));
if (U_FAILURE(status)) {
errln("ERROR: could not open test data %s", u_errorName(status));
return;
}
strcat(testDataPath, "CollationTest_");
const char* type = "NON_IGNORABLE";
const char *ext = ".txt";
if(testFile) {
fclose(testFile);
}
char buffer[1024];
strcpy(buffer, testDataPath);
strcat(buffer, type);
size_t bufLen = strlen(buffer);
strcpy(buffer+bufLen, ext);
testFile = fopen(buffer, "rb");
if(testFile == 0) {
strcpy(buffer+bufLen, "_SHORT");
strcat(buffer, ext);
testFile = fopen(buffer, "rb");
if(testFile == 0) {
strcpy(buffer+bufLen, "_STUB");
strcat(buffer, ext);
testFile = fopen(buffer, "rb");
if (testFile == 0) {
*(buffer+bufLen) = 0;
errln("ERROR: could not open any of the conformance test files, tried opening base %s", buffer);
return;
} else {
infoln(
"INFO: Working with the stub file.\n"
"If you need the full conformance test, please\n"
"download the appropriate data files from:\n"
"http://oss.software.ibm.com/cvs/icu4j/unicodetools/com/ibm/text/data/");
}
}
}
Line *lines = new Line[200000];
memset(lines, 0, sizeof(Line)*200000);
int32_t lineNum = 0;
UChar bufferU[1024];
int32_t buflen = 0;
uint32_t first = 0;
uint32_t offset = 0;
while (fgets(buffer, 1024, testFile) != NULL) {
offset = 0;
if(*buffer == 0 || buffer[0] == '#') {
continue;
}
offset = u_parseString(buffer, bufferU, 1024, &first, &status);
buflen = offset;
bufferU[offset++] = 0;
lines[lineNum].buflen = buflen;
u_memcpy(lines[lineNum].buff, bufferU, buflen);
lineNum++;
}
fclose(testFile);
UCollator *coll = ucol_open("root", &status);
if(U_FAILURE(status)) {
errln("Couldn't open UCA collator");
return;
}
ucol_setAttribute(coll, UCOL_NORMALIZATION_MODE, UCOL_ON, &status);
ucol_setAttribute(coll, UCOL_CASE_FIRST, UCOL_OFF, &status);
ucol_setAttribute(coll, UCOL_CASE_LEVEL, UCOL_OFF, &status);
ucol_setAttribute(coll, UCOL_STRENGTH, UCOL_TERTIARY, &status);
ucol_setAttribute(coll, UCOL_ALTERNATE_HANDLING, UCOL_NON_IGNORABLE, &status);
int32_t noSpawned = 0;
int32_t spawnResult = 0;
CollatorThreadTest *tests;
tests = new CollatorThreadTest[kCollatorThreadThreads];
logln(UnicodeString("Spawning: ") + kCollatorThreadThreads + " threads * " + kFormatThreadIterations + " iterations each.");
int32_t j = 0;
for(j = 0; j < kCollatorThreadThreads; j++) {
tests[j].setCollator(coll, lines, lineNum);
}
for(j = 0; j < kCollatorThreadThreads; j++) {
log("%i ", j);
spawnResult = tests[j].start();
if(spawnResult != 0) {
infoln("THREAD INFO: Couldn't spawn more than %i threads", noSpawned);
break;
}
noSpawned++;
}
logln("Spawned all");
for(;;)
{
logln("Waiting...");
int32_t i;
int32_t terrs = 0;
int32_t completed =0;
for(i=0;i<kCollatorThreadThreads;i++)
{
if (tests[i].isRunning() == FALSE)
{
completed++;
UnicodeString theErr;
if(tests[i].getError(theErr))
{
terrs++;
errln(UnicodeString("#") + i + ": " + theErr);
}
}
}
logln("Completed %i tests", completed);
if(completed == noSpawned)
{
logln("Done! All %i tests are finished", noSpawned);
if(terrs)
{
errln("There were errors.");
SimpleThread::errorFunc();
}
ucol_close(coll);
delete[] tests;
delete[] lines;
return;
}
SimpleThread::sleep(900);
}
errln("patience exceeded. ");
SimpleThread::errorFunc();
ucol_close(coll);
}
#endif
const int kStringThreadIterations = 2500;const int kStringThreadThreads = 10; const int kStringThreadPatience = 120;
class StringThreadTest2 : public ThreadWithStatus
{
public:
int fNum;
int fTraceInfo;
const UnicodeString *fSharedString;
StringThreadTest2(const UnicodeString *sharedString, int num) : ThreadWithStatus(),
fNum(num),
fTraceInfo(0),
fSharedString(sharedString)
{
};
virtual void run()
{
fTraceInfo = 1;
int loopCount = 0;
for (loopCount = 0; loopCount < kStringThreadIterations; loopCount++) {
if (*fSharedString != "This is the original test string.") {
error("Original string is corrupt.");
break;
}
UnicodeString s1 = *fSharedString;
s1 += "cat this";
UnicodeString s2(s1);
UnicodeString s3 = *fSharedString;
s2 = s3;
s3.truncate(12);
s2.truncate(0);
}
fTraceInfo = 2;
}
};
void MultithreadTest::TestString()
{
int patience;
int terrs = 0;
int j;
UnicodeString *testString = new UnicodeString("This is the original test string.");
StringThreadTest2 *tests[kStringThreadThreads];
for(j = 0; j < kStringThreadThreads; j++) {
tests[j] = new StringThreadTest2(testString, j);
}
logln(UnicodeString("Spawning: ") + kStringThreadThreads + " threads * " + kStringThreadIterations + " iterations each.");
for(j = 0; j < kStringThreadThreads; j++) {
int32_t threadStatus = tests[j]->start();
if (threadStatus != 0) {
errln("System Error %d starting thread number %d.", threadStatus, j);
SimpleThread::errorFunc();
goto cleanupAndReturn;
}
}
for(patience = kStringThreadPatience;patience > 0; patience --)
{
logln("Waiting...");
int32_t i;
terrs = 0;
int32_t completed =0;
for(i=0;i<kStringThreadThreads;i++) {
if (tests[i]->isRunning() == FALSE)
{
completed++;
logln(UnicodeString("Test #") + i + " is complete.. ");
UnicodeString theErr;
if(tests[i]->getError(theErr))
{
terrs++;
errln(UnicodeString("#") + i + ": " + theErr);
}
}
}
if(completed == kStringThreadThreads)
{
logln("Done!");
if(terrs) {
errln("There were errors.");
}
break;
}
SimpleThread::sleep(900);
}
if (patience <= 0) {
errln("patience exceeded. ");
terrs++;
}
if (terrs > 0) {
SimpleThread::errorFunc();
}
cleanupAndReturn:
if (terrs == 0) {
for(j = 0; j < kStringThreadThreads; j++) {
delete tests[j];
}
delete testString;
}
}
#endif // ICU_USE_THREADS