X-Git-Url: https://git.saurik.com/apple/icu.git/blobdiff_plain/73c04bcfe1096173b00431f0cdc742894b15eef0..340931cb2e044a2141d11567dd0f782524e32994:/icuSources/test/intltest/tsmthred.cpp?ds=sidebyside diff --git a/icuSources/test/intltest/tsmthred.cpp b/icuSources/test/intltest/tsmthred.cpp index 05b29597..98e04869 100644 --- a/icuSources/test/intltest/tsmthred.cpp +++ b/icuSources/test/intltest/tsmthred.cpp @@ -1,80 +1,34 @@ +// © 2016 and later: Unicode, Inc. and others. +// License & terms of use: http://www.unicode.org/copyright.html /******************************************************************** - * COPYRIGHT: - * Copyright (c) 1999-2006, International Business Machines Corporation and + * COPYRIGHT: + * Copyright (c) 1999-2015, International Business Machines Corporation and * others. All Rights Reserved. ********************************************************************/ -#if defined(hpux) -# ifndef _INCLUDE_POSIX_SOURCE -# define _INCLUDE_POSIX_SOURCE -# endif -#endif +#include "simplethread.h" #include "unicode/utypes.h" #include "unicode/ustring.h" #include "umutex.h" #include "cmemory.h" #include "cstring.h" +#include "indiancal.h" #include "uparse.h" +#include "unicode/localpointer.h" #include "unicode/resbund.h" #include "unicode/udata.h" #include "unicode/uloc.h" #include "unicode/locid.h" #include "putilimp.h" -#if !defined(U_WINDOWS) && !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 -#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 - -/* Define __EXTENSIONS__ for Solaris and old friends in strict mode. */ -#ifndef __EXTENSIONS__ -#define __EXTENSIONS__ -#endif - -#include - -/* Define _XPG4_2 for Solaris and friends. */ -#ifndef _XPG4_2 -#define _XPG4_2 -#endif - -/* Define __USE_XOPEN_EXTENDED for Linux and glibc. */ -#ifndef __USE_XOPEN_EXTENDED -#define __USE_XOPEN_EXTENDED -#endif - -/* Define _INCLUDE_XOPEN_SOURCE_EXTENDED for HP/UX (11?). */ -#ifndef _INCLUDE_XOPEN_SOURCE_EXTENDED -#define _INCLUDE_XOPEN_SOURCE_EXTENDED -#endif - -#include - -#endif -/* HPUX */ -#ifdef sleep -#undef sleep -#endif - - - +#include "intltest.h" #include "tsmthred.h" +#include "unicode/ushape.h" +#include "unicode/translit.h" +#include "sharedobject.h" +#include "unifiedcache.h" +#include "uassert.h" -#define TSMTHREAD_FAIL(msg) errln("%s at file %s, line %d", msg, __FILE__, __LINE__) -#define TSMTHREAD_ASSERT(expr) {if (!(expr)) {TSMTHREAD_FAIL("Fail");}} MultithreadTest::MultithreadTest() { @@ -84,443 +38,48 @@ 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 - - - -// Note: A LOT OF THE FUNCTIONS IN THIS FILE SHOULD LIVE ELSEWHERE!!!!! -// Note: A LOT OF THE FUNCTIONS IN THIS FILE SHOULD LIVE ELSEWHERE!!!!! -// -srl - #include #include #include // tolower, toupper +#include #include "unicode/putil.h" -/* for mthreadtest*/ +// for mthreadtest #include "unicode/numfmt.h" #include "unicode/choicfmt.h" #include "unicode/msgfmt.h" #include "unicode/locid.h" -#include "unicode/ucol.h" +#include "unicode/coll.h" #include "unicode/calendar.h" #include "ucaconf.h" -//----------------------------------------------------------------------------------- -// -// class SimpleThread Of course we need a thread class first.. -// This wrapper has a ported implementation. -// -//----------------------------------------------------------------------------------- -class SimpleThread -{ -public: - SimpleThread(); - virtual ~SimpleThread(); - int32_t start(void); // start the thread - UBool isRunning(); // return true if a started thread has exited. - - virtual void run(void) = 0; // Override this to provide the code to run - // in the thread. - void *fImplementation; - -public: - static void sleep(int32_t millis); // probably shouldn't go here but oh well. - static void errorFunc(); // Empty function, provides a single convenient place - // to break on errors. -}; -void SimpleThread::errorFunc() { - // *(char *)0 = 3; // Force entry into a debugger via a crash; -} - - - - -#ifdef U_WINDOWS -#define HAVE_IMP - -# define VC_EXTRALEAN -# define WIN32_LEAN_AND_MEAN -# define NOUSER -# define NOSERVICE -# define NOIME -# define NOMCX -#include -#include - - - -//----------------------------------------------------------------------------------- -// -// class SimpleThread Windows Implementation -// -//----------------------------------------------------------------------------------- -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() -{ - // Destructor. Because we start the thread running with _beginthreadex(), - // we own the Windows HANDLE for the thread and must - // close it here. - 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) { - // The thread appears to have already been started. - // This is probably an error on the part of our caller. - return -1; - } - - imp->fHandle = (HANDLE) _beginthreadex( - NULL, // Security - 0x20000, // Stack Size - SimpleThreadProc, // Function to Run - (void *)this, // Arg List - 0, // initflag. Start running, not suspended - &imp->fThreadID // thraddr - ); - - if (imp->fHandle == 0) { - // An error occured - int err = errno; - if (err == 0) { - err = -1; - } - return err; - } - return 0; -} - - -UBool SimpleThread::isRunning() { - // - // Test whether the thread associated with the SimpleThread object is - // still actually running. - // - // NOTE: on Win64 on Itanium processors, a crashes - // occur if the main thread of a process exits concurrently with some - // other thread(s) exiting. To avoid the possibility, we wait until the - // OS indicates that all threads have terminated, rather than waiting - // only until the end of the user's Run function has been reached. - // - // I don't know whether the crashes represent a Windows bug, or whether - // main() programs are supposed to have to wait for their threads. - // - Win32ThreadImplementation *imp = (Win32ThreadImplementation*)fImplementation; - - bool success; - DWORD threadExitCode; - - if (imp->fHandle == 0) { - // No handle, thread must not be running. - return FALSE; - } - success = GetExitCodeThread(imp->fHandle, &threadExitCode) != 0; - if (! success) { - // Can't get status, thread must not be running. - return FALSE; - } - return (threadExitCode == STILL_ACTIVE); -} - - -void SimpleThread::sleep(int32_t millis) -{ - ::Sleep(millis); -} - -//----------------------------------------------------------------------------------- -// -// class SimpleThread NULL Implementation -// -//----------------------------------------------------------------------------------- -#elif defined XP_MAC - -// since the Mac has no preemptive threading (at least on MacOS 8), only -// cooperative threading, threads are a no-op. We have no yield() calls -// anywhere in the ICU, so we are guaranteed to be thread-safe. - -#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 - - -//----------------------------------------------------------------------------------- -// -// class SimpleThread POSIX implementation -// -// A note on the POSIX vs the Windows implementations of this class.. -// On Windows, the main thread must verify that other threads have finished -// before exiting, or crashes occasionally occur. (Seen on Itanium Win64 only) -// The function SimpleThread::isRunning() is used for this purpose. -// -// On POSIX, there is NO reliable non-blocking mechanism to determine -// whether a thread has exited. pthread_kill(thread, 0) almost works, -// but the system can recycle thread ids immediately, so seeing that a -// thread exists with this call could mean that the original thread has -// finished and a new one started with the same ID. Useless. -// -// So we need to do the check with user code, by setting a flag just before -// the thread function returns. A technique that is guaranteed to fail -// on Windows, because it indicates that the thread is done before all -// system level cleanup has happened. -// -//----------------------------------------------------------------------------------- -#if defined(POSIX)||defined(U_SOLARIS)||defined(U_AIX)||defined(U_HPUX) -#define HAVE_IMP - -struct PosixThreadImplementation -{ - pthread_t fThread; - UBool fRunning; - UBool fRan; /* True if the thread was successfully started */ -}; - -extern "C" void* SimpleThreadProc(void *arg) -{ - // This is the code that is run in the new separate thread. - SimpleThread *This = (SimpleThread *)arg; - This->run(); // Run the user code. - - // The user function has returned. Set the flag indicating that this thread - // is done. Need a mutex for memory barrier purposes only, so that other thread - // will reliably see that the flag has changed. - 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; /* jdc30: detach state of zero causes - threads created with this attr to be in - an undetached state. An undetached - thread will keep its resources after - termination. */ - pthread_attr_setdetachstate(&attr, &detachstate); - } -#else - // pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); - pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE); -#endif - attrIsInitialized = TRUE; - } - rc = pthread_create(&(imp->fThread),&attr,&SimpleThreadProc,(void*)this); -#endif - - if (rc != 0) { - // some kind of error occured, the thread did not start. - imp->fRan = FALSE; - imp->fRunning = FALSE; - } - - return rc; -} - - -UBool -SimpleThread::isRunning() { - // Note: Mutex functions are used here not for synchronization, - // but to force memory barriors to exist, to ensure that one thread - // can see changes made by another when running on processors - // with memory models having weak coherency. - 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 -// end POSIX - - -#ifndef HAVE_IMP -#error No implementation for threads! Cannot test. -0 = 216; //die -#endif - - -// *************** end fluff ****************** - -/* now begins the real test. */ -void MultithreadTest::runIndexedTest( int32_t index, UBool exec, +void MultithreadTest::runIndexedTest( int32_t index, UBool exec, const char* &name, char* /*par*/ ) { 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"; + + TESTCASE_AUTO_BEGIN; + TESTCASE_AUTO(TestThreads); #if !UCONFIG_NO_FORMATTING - if (exec) { - TestThreadedIntl(); - } + TESTCASE_AUTO(TestThreadedIntl); #endif - break; - - case 3: - name = "TestCollators"; #if !UCONFIG_NO_COLLATION - if (exec) { - TestCollators(); - } + TESTCASE_AUTO(TestCollators); #endif /* #if !UCONFIG_NO_COLLATION */ - break; - - case 4: - name = "TestString"; - if (exec) { - TestString(); - } - break; - - default: - name = ""; - break; //needed to end loop - } + TESTCASE_AUTO(TestString); + TESTCASE_AUTO(TestArabicShapingThreads); + TESTCASE_AUTO(TestAnyTranslit); + TESTCASE_AUTO(TestUnifiedCache); +#if !UCONFIG_NO_TRANSLITERATION + TESTCASE_AUTO(TestBreakTranslit); + TESTCASE_AUTO(TestIncDec); +#if !UCONFIG_NO_FORMATTING + TESTCASE_AUTO(Test20104); +#endif /* #if !UCONFIG_NO_FORMATTING */ +#endif /* #if !UCONFIG_NO_TRANSLITERATION */ + TESTCASE_AUTO_END; } @@ -529,26 +88,25 @@ void MultithreadTest::runIndexedTest( int32_t index, UBool exec, // TestThreads -- see if threads really work at all. // // Set up N threads pointing at N chars. When they are started, they will -// each sleep 1 second and then set their chars. At the end we make sure they -// are all set. +// set their chars. At the end we make sure they are all set. // //----------------------------------------------------------------------------------- -#define THREADTEST_NRTHREADS 8 class TestThreadsThread : public SimpleThread { public: TestThreadsThread(char* whatToChange) { fWhatToChange = whatToChange; } - virtual void run() { SimpleThread::sleep(1000); - Mutex m; - *fWhatToChange = '*'; + virtual void run() { Mutex m; + *fWhatToChange = '*'; } private: char *fWhatToChange; }; + void MultithreadTest::TestThreads() { + static const int32_t THREADTEST_NRTHREADS = 8; char threadTestChars[THREADTEST_NRTHREADS + 1]; SimpleThread *threads[THREADTEST_NRTHREADS]; int32_t numThreadsStarted = 0; @@ -570,206 +128,111 @@ void MultithreadTest::TestThreads() else { numThreadsStarted++; } - SimpleThread::sleep(100); logln(" Subthread started."); } - logln("Waiting for threads to be set.."); - if (numThreadsStarted == 0) { - errln("No threads could be started for testing!"); - return; - } - - int32_t patience = 40; // seconds to wait + assertTrue(WHERE, THREADTEST_NRTHREADS == numThreadsStarted); - while(patience--) - { - int32_t count = 0; - umtx_lock(NULL); - for(i=0;i" + UnicodeString(threadTestChars) + "<- Got all threads! cya"); - for(i=0;ijoin(); + if (threadTestChars[i] != '*') { + errln("%s:%d Thread %d failed.", __FILE__, __LINE__, i); } - - logln("->" + UnicodeString(threadTestChars) + "<- Waiting.."); - SimpleThread::sleep(500); - } - - errln("->" + UnicodeString(threadTestChars) + "<- PATIENCE EXCEEDED!! Still missing some."); - for(i=0;istart() != 0) { - errln("Error starting thread %d", i); - } - else { - numThreadsStarted++; - } - } - if (numThreadsStarted == 0) { - errln("No threads could be started for testing!"); - return; - } - int patience = 0; - while (safeIncr(gThreadsStarted, 0) != TESTMUTEX_THREAD_COUNT) { - if (patience++ > 24) { - TSMTHREAD_FAIL("Patience Exceeded"); +void TestArabicShapeThreads::doTailTest(void) { + static const UChar src[] = { 0x0020, 0x0633, 0 }; + static const UChar dst_old[] = { 0xFEB1, 0x200B,0 }; + static const UChar dst_new[] = { 0xFEB1, 0xFE73,0 }; + UChar dst[3] = { 0x0000, 0x0000,0 }; + int32_t length; + UErrorCode status; + + for (int32_t loopCount = 0; loopCount < 100; loopCount++) { + status = U_ZERO_ERROR; + length = u_shapeArabic(src, -1, dst, UPRV_LENGTHOF(dst), + U_SHAPE_LETTERS_SHAPE|U_SHAPE_SEEN_TWOCELL_NEAR, &status); + if(U_FAILURE(status)) { + IntlTest::gTest->errln("Fail: status %s\n", u_errorName(status)); return; - } - SimpleThread::sleep(500); - } - // None of the test threads should have advanced past the first mutex. - TSMTHREAD_ASSERT(gThreadsInMiddle==0); - TSMTHREAD_ASSERT(gThreadsDone==0); - - // All of the test threads have made it to the first mutex. - // We (the main thread) now let them advance to the second mutex, - // where they should all pile up again. - umtx_lock(&gTestMutexB); - umtx_unlock(&gTestMutexA); - - patience = 0; - while (safeIncr(gThreadsInMiddle, 0) != TESTMUTEX_THREAD_COUNT) { - if (patience++ > 24) { - TSMTHREAD_FAIL("Patience Exceeded"); + } else if(length!=2) { + IntlTest::gTest->errln("Fail: len %d expected 3\n", length); return; - } - SimpleThread::sleep(500); - } - TSMTHREAD_ASSERT(gThreadsDone==0); - - // All test threads made it to the second mutex. - // Now let them proceed from there. They will all terminate. - umtx_unlock(&gTestMutexB); - patience = 0; - while (safeIncr(gThreadsDone, 0) != TESTMUTEX_THREAD_COUNT) { - if (patience++ > 24) { - TSMTHREAD_FAIL("Patience Exceeded"); + } else if(u_strncmp(dst,dst_old,UPRV_LENGTHOF(dst))) { + IntlTest::gTest->errln("Fail: got U+%04X U+%04X expected U+%04X U+%04X\n", + dst[0],dst[1],dst_old[0],dst_old[1]); return; } - SimpleThread::sleep(500); - } - // All threads made it by both mutexes. - // Destroy the test mutexes. - umtx_destroy(&gTestMutexA); - umtx_destroy(&gTestMutexB); - gTestMutexA=NULL; - gTestMutexB=NULL; - for (i=0; ierrln("Fail: status %s\n", u_errorName(status)); + return; + } else if(length!=2) { + IntlTest::gTest->errln("Fail: len %d expected 3\n", length); + return; + } else if(u_strncmp(dst,dst_new,UPRV_LENGTHOF(dst))) { + IntlTest::gTest->errln("Fail: got U+%04X U+%04X expected U+%04X U+%04X\n", + dst[0],dst[1],dst_new[0],dst_new[1]); + return; + } } - + return; } + - -//------------------------------------------------------------------------------------------- -// -// class ThreadWithStatus - a thread that we can check the status and error condition of -// -//------------------------------------------------------------------------------------------- -class ThreadWithStatus : public SimpleThread +void MultithreadTest::TestArabicShapingThreads() { -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(); + TestArabicShapeThreads threads[30]; + + int32_t i; + + logln("-> do TestArabicShapingThreads <- Firing off threads.. "); + for(i=0; i < UPRV_LENGTHOF(threads); i++) { + if (threads[i].start() != 0) { + errln("Error starting thread %d", i); + } } - void error() { error("An error occured."); } -private: - int32_t fErrors; - UnicodeString fErrorString; -}; + for(i=0; i < UPRV_LENGTHOF(threads); i++) { + threads[i].join(); + } + logln("->TestArabicShapingThreads <- Got all threads! cya"); +} //------------------------------------------------------------------------------------------- // -// TestMultithreadedIntl. Test ICU Formatting n a multi-threaded environment +// TestMultithreadedIntl. Test ICU Formatting in a multi-threaded environment // //------------------------------------------------------------------------------------------- @@ -778,42 +241,39 @@ private: UnicodeString showDifference(const UnicodeString& expected, const UnicodeString& result) { UnicodeString res; - res = expected + "setLocale(theLocale); - fmt->applyPattern(pattern, realStatus); - + LocalPointer fmt(new MessageFormat(u"MessageFormat's API is broken!!!!!!!!!!!",realStatus), realStatus); if (U_FAILURE(realStatus)) { - delete fmt; return; } + fmt->setLocale(theLocale); + fmt->applyPattern(pattern, realStatus); - FieldPosition ignore = 0; + FieldPosition ignore = 0; fmt->format(myArgs,4,result,ignore,realStatus); +} + +/** + * Shared formatters & data used by instances of ThreadSafeFormat. + * Exactly one instance of this class is created, and it is then shared concurrently + * by the multiple instances of ThreadSafeFormat. + */ +class ThreadSafeFormatSharedData { + public: + ThreadSafeFormatSharedData(UErrorCode &status); + ~ThreadSafeFormatSharedData(); + LocalPointer fFormat; + Formattable fYDDThing; + Formattable fBBDThing; + UnicodeString fYDDStr; + UnicodeString fBBDStr; +}; + +const ThreadSafeFormatSharedData *gSharedData = NULL; + +ThreadSafeFormatSharedData::ThreadSafeFormatSharedData(UErrorCode &status) { + fFormat.adoptInstead(NumberFormat::createCurrencyInstance(Locale::getUS(), status)); + static const UChar *kYDD = u"YDD"; + static const UChar *kBBD = u"BBD"; + fYDDThing.adoptObject(new CurrencyAmount(123.456, kYDD, status)); + fBBDThing.adoptObject(new CurrencyAmount(987.654, kBBD, status)); + if (U_FAILURE(status)) { + return; + } + fFormat->format(fYDDThing, fYDDStr, NULL, status); + fFormat->format(fBBDThing, fBBDStr, NULL, status); + gSharedData = this; +} - delete fmt; +ThreadSafeFormatSharedData::~ThreadSafeFormatSharedData() { + gSharedData = NULL; } +/** + * Class for thread-safe testing of format. + * Instances of this class appear as members of class FormatThreadTest. + * Multiple instances of FormatThreadTest coexist. + * ThreadSafeFormat::doStuff() is called concurrently to test the thread safety of + * various shared format operations. + */ +class ThreadSafeFormat { +public: + /* give a unique offset to each thread */ + ThreadSafeFormat(UErrorCode &status); + UBool doStuff(int32_t offset, UnicodeString &appendErr, UErrorCode &status) const; +private: + LocalPointer fFormat; // formatter - en_US constructed currency +}; + + +ThreadSafeFormat::ThreadSafeFormat(UErrorCode &status) { + fFormat.adoptInstead(NumberFormat::createCurrencyInstance(Locale::getUS(), status)); +} + +static const UChar *kUSD = u"USD"; + +UBool ThreadSafeFormat::doStuff(int32_t offset, UnicodeString &appendErr, UErrorCode &status) const { + UBool okay = TRUE; + + if(u_strcmp(fFormat->getCurrency(), kUSD)) { + appendErr.append(u"fFormat currency != ") + .append(kUSD) + .append(u", =") + .append(fFormat->getCurrency()) + .append(u"! "); + okay = FALSE; + } + + if(u_strcmp(gSharedData->fFormat->getCurrency(), kUSD)) { + appendErr.append(u"gFormat currency != ") + .append(kUSD) + .append(u", =") + .append(gSharedData->fFormat->getCurrency()) + .append(u"! "); + okay = FALSE; + } + UnicodeString str; + const UnicodeString *o=NULL; + Formattable f; + const NumberFormat *nf = NULL; // only operate on it as const. + switch(offset%4) { + case 0: f = gSharedData->fYDDThing; o = &gSharedData->fYDDStr; nf = gSharedData->fFormat.getAlias(); break; + case 1: f = gSharedData->fBBDThing; o = &gSharedData->fBBDStr; nf = gSharedData->fFormat.getAlias(); break; + case 2: f = gSharedData->fYDDThing; o = &gSharedData->fYDDStr; nf = fFormat.getAlias(); break; + case 3: f = gSharedData->fBBDThing; o = &gSharedData->fBBDStr; nf = fFormat.getAlias(); break; + } + nf->format(f, str, NULL, status); + + if(*o != str) { + appendErr.append(showDifference(*o, str)); + okay = FALSE; + } + return okay; +} UBool U_CALLCONV isAcceptable(void *, const char *, const char *, const UDataInfo *) { return TRUE; @@ -872,19 +426,24 @@ UBool U_CALLCONV isAcceptable(void *, const char *, const char *, const UDataInf //static UMTX gDebugMutex; -class FormatThreadTest : public ThreadWithStatus +class FormatThreadTest : public SimpleThread { public: int fNum; int fTraceInfo; + LocalPointer fTSF; + FormatThreadTest() // constructor is NOT multithread safe. - : ThreadWithStatus(), + : SimpleThread(), fNum(0), fTraceInfo(0), + fTSF(NULL), fOffset(0) // the locale to use { + UErrorCode status = U_ZERO_ERROR; // TODO: rearrange code to allow checking of status. + fTSF.adoptInstead(new ThreadSafeFormat(status)); static int32_t fgOffset = 0; fgOffset += 3; fOffset = fgOffset; @@ -894,12 +453,11 @@ public: virtual void run() { fTraceInfo = 1; - NumberFormat *formatter = NULL; - NumberFormat *percentFormatter = NULL; + LocalPointer percentFormatter; UErrorCode status = U_ZERO_ERROR; #if 0 - // debugging code, + // debugging code, for (int i=0; i<4000; i++) { status = U_ZERO_ERROR; UDataMemory *data1 = udata_openChoice(0, "res", "en_US", isAcceptable, 0, &status); @@ -914,8 +472,8 @@ public: return; #endif -#if 1 - // debugging code, +#if 0 + // debugging code, int m; for (m=0; m<4000; m++) { status = U_ZERO_ERROR; @@ -945,122 +503,123 @@ public: #endif // Keep this data here to avoid static initialization. - FormatThreadTestData kNumberFormatTestData[] = + 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", "")), + FormatThreadTestData((double)5.0, UnicodeString(u"5")), + FormatThreadTestData( 6.0, UnicodeString(u"6")), + FormatThreadTestData( 20.0, UnicodeString(u"20")), + FormatThreadTestData( 8.0, UnicodeString(u"8")), + FormatThreadTestData( 8.3, UnicodeString(u"8.3")), + FormatThreadTestData( 12345, UnicodeString(u"12,345")), + FormatThreadTestData( 81890.23, UnicodeString(u"81,890.23")), }; - int32_t kNumberFormatTestDataLength = (int32_t)(sizeof(kNumberFormatTestData) / - sizeof(kNumberFormatTestData[0])); - + int32_t kNumberFormatTestDataLength = UPRV_LENGTHOF(kNumberFormatTestData); + // Keep this data here to avoid static initialization. - FormatThreadTestData kPercentFormatTestData[] = + 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%") ), // U+00a0 = NBSP - FormatThreadTestData( - 81890.23, CharsToUnicodeString("8\\u00a0189\\u00a0023%" )), + FormatThreadTestData((double)5.0, CharsToUnicodeString("500\\u00a0%")), + FormatThreadTestData( 1.0, CharsToUnicodeString("100\\u00a0%")), + FormatThreadTestData( 0.26, CharsToUnicodeString("26\\u00a0%")), + FormatThreadTestData( + 16384.99, CharsToUnicodeString("1\\u202F638\\u202F499\\u00a0%")), // U+202F = NNBSP + FormatThreadTestData( + 81890.23, CharsToUnicodeString("8\\u202F189\\u202F023\\u00a0%")), }; - int32_t kPercentFormatTestDataLength = - (int32_t)(sizeof(kPercentFormatTestData) / sizeof(kPercentFormatTestData[0])); + int32_t kPercentFormatTestDataLength = UPRV_LENGTHOF(kPercentFormatTestData); int32_t iteration; - + status = U_ZERO_ERROR; - formatter = NumberFormat::createInstance(Locale::getEnglish(),status); + LocalPointer formatter(NumberFormat::createInstance(Locale::getEnglish(),status)); if(U_FAILURE(status)) { - error("Error on NumberFormat::createInstance()"); + IntlTest::gTest->dataerrln("%s:%d Error %s on NumberFormat::createInstance().", + __FILE__, __LINE__, u_errorName(status)); goto cleanupAndReturn; } - - percentFormatter = NumberFormat::createPercentInstance(Locale::getFrench(),status); + + percentFormatter.adoptInstead(NumberFormat::createPercentInstance(Locale::getFrench(),status)); if(U_FAILURE(status)) { - error("Error on NumberFormat::createPercentInstance()"); + IntlTest::gTest->errln("%s:%d Error %s on NumberFormat::createPercentInstance().", + __FILE__, __LINE__, u_errorName(status)); goto cleanupAndReturn; } - - for(iteration = 0;!getError() && iterationgetErrors() && iterationformat(kNumberFormatTestData[whichLine].number, output); - + if(0 != output.compare(kNumberFormatTestData[whichLine].string)) { - error("format().. expected " + kNumberFormatTestData[whichLine].string + IntlTest::gTest->errln("format().. expected " + kNumberFormatTestData[whichLine].string + " got " + output); goto cleanupAndReturn; } - + // Now check percent. output.remove(); whichLine = (iteration + fOffset)%kPercentFormatTestDataLength; - + percentFormatter->format(kPercentFormatTestData[whichLine].number, output); if(0 != output.compare(kPercentFormatTestData[whichLine].string)) { - error("percent format().. \n" + + IntlTest::gTest->errln("percent format().. \n" + showDifference(kPercentFormatTestData[whichLine].string,output)); goto cleanupAndReturn; } - - // Test message error + + // Test message error const int kNumberOfMessageTests = 3; UErrorCode statusToCheck; UnicodeString patternToCheck; Locale messageLocale; Locale countryToCheck; double currencyToCheck; - + UnicodeString expected; - + // load the cases. 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}."; // number,currency + patternToCheck= u"0:Someone from {2} is receiving a #{0}" + " error - {1}. Their telephone call is costing " + "{3,number,currency}."; // number,currency messageLocale= Locale("en","US"); countryToCheck= Locale("","HR"); currencyToCheck= 8192.77; - expected= "0:Someone from Croatia is receiving a #4 error - " + expected= u"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}."; // number,currency + patternToCheck= u"1:A customer in {2} is receiving a #{0} error - {1}. " + "Their telephone call is costing {3,number,currency}."; // 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."; + expected= u"1:A customer in Burkina Faso is receiving a #8 error - U_INDEX_OUTOFBOUNDS_ERROR. " + u"Their telephone call is costing 2,32\u00A0DM."; break; case 2: statusToCheck= U_MEMORY_ALLOCATION_ERROR; - patternToCheck= "2:user in {2} is receiving a #{0} error - {1}. " + patternToCheck= u"2:user in {2} is receiving a #{0} error - {1}. " "They insist they just spent {3,number,currency} " "on memory."; // number,currency messageLocale= Locale("de","AT@currency=ATS"); // Austrian German countryToCheck= Locale("","US"); // hmm 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."); + expected= u"2:user in Vereinigte Staaten is receiving a #7 error" + u" - U_MEMORY_ALLOCATION_ERROR. They insist they just spent" + u" \u00f6S\u00A040.193,12 on memory."; break; } - + UnicodeString result; UErrorCode status = U_ZERO_ERROR; formatErrorMessage(status,patternToCheck,messageLocale,statusToCheck, @@ -1068,26 +627,30 @@ public: if(U_FAILURE(status)) { UnicodeString tmp(u_errorName(status)); - error("Failure on message format, pattern=" + patternToCheck + + IntlTest::gTest->errln(u"Failure on message format, pattern=" + patternToCheck + ", error = " + tmp); goto cleanupAndReturn; } - + if(result != expected) { - error("PatternFormat: \n" + showDifference(expected,result)); + IntlTest::gTest->errln(u"PatternFormat: \n" + showDifference(expected,result)); goto cleanupAndReturn; } + // test the Thread Safe Format + UnicodeString appendErr; + if(!fTSF->doStuff(fNum, appendErr, status)) { + IntlTest::gTest->errln(appendErr); + goto cleanupAndReturn; + } } /* end of for loop */ - + + + cleanupAndReturn: - delete formatter; - delete percentFormatter; - - // while (fNum == 4) {SimpleThread::sleep(10000);} // Force a failure by preventing thread from finishing fTraceInfo = 2; } - + private: int32_t fOffset; // where we are testing from. }; @@ -1096,66 +659,35 @@ private: void MultithreadTest::TestThreadedIntl() { - int i; UnicodeString theErr; - UBool haveDisplayedInfo[kFormatThreadThreads]; - static const int32_t PATIENCE_SECONDS = 45; + + UErrorCode threadSafeErr = U_ZERO_ERROR; + + ThreadSafeFormatSharedData sharedData(threadSafeErr); + assertSuccess(WHERE, threadSafeErr, TRUE); // // Create and start the test threads // logln("Spawning: %d threads * %d iterations each.", kFormatThreadThreads, kFormatThreadIterations); - FormatThreadTest *tests = new FormatThreadTest[kFormatThreadThreads]; - for(int32_t j = 0; j < kFormatThreadThreads; j++) { + FormatThreadTest tests[kFormatThreadThreads]; + int32_t j; + for(j = 0; j < UPRV_LENGTHOF(tests); 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; + errln("%s:%d System Error %d starting thread number %d.", + __FILE__, __LINE__, threadStatus, j); + return; } - haveDisplayedInfo[j] = FALSE; } - // Spin, waiting for the test threads to finish. - UBool stillRunning; - UDate startTime, endTime; - startTime = Calendar::getNow(); - do { - /* Spin until the test threads complete. */ - stillRunning = FALSE; - endTime = Calendar::getNow(); - if (((int32_t)(endTime - startTime)/U_MILLIS_PER_SECOND) > PATIENCE_SECONDS) { - errln("Patience exceeded. Test is taking too long."); - return; - } - /* - The following sleep must be here because the *BSD operating systems - have a brain dead thread scheduler. They starve the child threads from - CPU time. - */ - SimpleThread::sleep(1); // yield - for(i=0;igetSortKey(lines[i].buff, lines[i].buflen, newSk, 1024); + 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); - //cmpres = res; - //cmpres2 = -cmpres; - + int32_t skres = strcmp((char *)oldSk, (char *)newSk); + int32_t cmpres = coll->compare(lines[prev].buff, lines[prev].buflen, lines[i].buff, lines[i].buflen); + int32_t cmpres2 = coll->compare(lines[i].buff, lines[i].buflen, lines[prev].buff, lines[prev].buflen); + if(cmpres != -cmpres2) { - error("Compare result not symmetrical on line "+ line); + IntlTest::gTest->errln(UnicodeString(u"Compare result not symmetrical on line ") + (i + 1)); 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)); + + if(cmpres != normalizeResult(skres)) { + IntlTest::gTest->errln(UnicodeString(u"Difference between coll->compare and sortkey compare on line ") + (i + 1)); break; } - + + int32_t res = cmpres; + if(res == 0 && !isAtLeastUCA62) { + // Up to UCA 6.1, the collation test files use a custom tie-breaker, + // comparing the raw input strings. + res = u_strcmpCodePointOrder(lines[prev].buff, lines[i].buff); + // Starting with UCA 6.2, the collation test files use the standard UCA tie-breaker, + // comparing the NFD versions of the input strings, + // which we do via setting strength=identical. + } if(res > 0) { - error(UnicodeString("Line %i is not greater or equal than previous line ")+ UnicodeString(i)); + IntlTest::gTest->errln(UnicodeString(u"Line is not greater or equal than previous line, for line ") + (i + 1)); break; - } else if(res == 0) { /* equal */ - 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; - + (void)oldLen; // Suppress set but not used warning. + prev = i; + newSk = (newSk == sk1)?sk2:sk1; } } - }; void MultithreadTest::TestCollators() @@ -1298,134 +836,84 @@ void MultithreadTest::TestCollators() if (testFile == 0) { *(buffer+bufLen) = 0; - errln("ERROR: could not open any of the conformance test files, tried opening base %s", buffer); - return; + dataerrln("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://dev.icu-project.org/cgi-bin/viewcvs.cgi/unicodetools/com/ibm/text/data/"); + "http://source.icu-project.org/repos/icu/tools/trunk/unicodetools/com/ibm/text/data/"); } } } - Line *lines = new Line[200000]; - memset(lines, 0, sizeof(Line)*200000); + LocalArray lines(new Line[200000]); + memset(lines.getAlias(), 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 || strlen(buffer) < 3 || buffer[0] == '#') { - continue; + if(*buffer == 0 || buffer[0] == '#') { + // Store empty and comment lines so that errors are reported + // for the real test file lines. + lines[lineNum].buflen = 0; + lines[lineNum].buff[0] = 0; + } else { + int32_t buflen = u_parseString(buffer, bufferU, 1024, &first, &status); + lines[lineNum].buflen = buflen; + u_memcpy(lines[lineNum].buff, bufferU, buflen); + lines[lineNum].buff[buflen] = 0; } - offset = u_parseString(buffer, bufferU, 1024, &first, &status); - buflen = offset; - bufferU[offset++] = 0; - lines[lineNum].buflen = buflen; - //lines[lineNum].buff = new UChar[buflen+1]; - u_memcpy(lines[lineNum].buff, bufferU, buflen); lineNum++; } fclose(testFile); if(U_FAILURE(status)) { - errln("Couldn't read the test file!"); + dataerrln("Couldn't read the test file!"); return; } - UCollator *coll = ucol_open("root", &status); + UVersionInfo uniVersion; + static const UVersionInfo v62 = { 6, 2, 0, 0 }; + u_getUnicodeVersion(uniVersion); + UBool isAtLeastUCA62 = uprv_memcmp(uniVersion, v62, 4) >= 0; + + LocalPointer coll(Collator::createInstance(Locale::getRoot(), status)); if(U_FAILURE(status)) { - errln("Couldn't open UCA collator"); + errcheckln(status, "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); + coll->setAttribute(UCOL_NORMALIZATION_MODE, UCOL_ON, status); + coll->setAttribute(UCOL_CASE_FIRST, UCOL_OFF, status); + coll->setAttribute(UCOL_CASE_LEVEL, UCOL_OFF, status); + coll->setAttribute(UCOL_STRENGTH, isAtLeastUCA62 ? UCOL_IDENTICAL : UCOL_TERTIARY, status); + coll->setAttribute(UCOL_ALTERNATE_HANDLING, UCOL_NON_IGNORABLE, status); - int32_t noSpawned = 0; int32_t spawnResult = 0; - CollatorThreadTest *tests; - tests = new CollatorThreadTest[kCollatorThreadThreads]; + LocalArray tests(new CollatorThreadTest[kCollatorThreadThreads]); - logln(UnicodeString("Spawning: ") + kCollatorThreadThreads + " threads * " + kFormatThreadIterations + " iterations each."); + logln(UnicodeString(u"Spawning: ") + kCollatorThreadThreads + u" threads * " + kFormatThreadIterations + u" iterations each."); int32_t j = 0; for(j = 0; j < kCollatorThreadThreads; j++) { //logln("Setting collator %i", j); - tests[j].setCollator(coll, lines, lineNum); + tests[j].setCollator(coll.getAlias(), lines.getAlias(), lineNum, isAtLeastUCA62); } 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; + errln("%s:%d THREAD INFO: thread %d failed to start with status %d", __FILE__, __LINE__, j, spawnResult); + return; } - noSpawned++; } logln("Spawned all"); - if (noSpawned == 0) { - errln("No threads could be spawned."); - return; - } - - for(int32_t patience = kCollatorThreadPatience;patience > 0; patience --) - { - logln("Waiting..."); - int32_t i; - int32_t terrs = 0; - int32_t completed =0; - - for(i=0;ierrln("%s:%d Original string is corrupt.", __FILE__, __LINE__); break; } - UnicodeString s1 = *fSharedString; - s1 += "cat this"; + UnicodeString s1 = *gSharedString; + s1 += u"cat this"; UnicodeString s2(s1); - UnicodeString s3 = *fSharedString; + UnicodeString s3 = *gSharedString; s2 = s3; s3.truncate(12); s2.truncate(0); } - // while (fNum == 4) {SimpleThread::sleep(10000);} // Force a failure by preventing thread from finishing fTraceInfo = 2; } - + }; +const UnicodeString *StringThreadTest2::gSharedString = NULL; + // ** The actual test function. + void MultithreadTest::TestString() { - int patience; - int terrs = 0; int j; + StringThreadTest2::gSharedString = new UnicodeString(u"This is the original test string."); + StringThreadTest2 tests[kStringThreadThreads]; - 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."); + logln(UnicodeString(u"Spawning: ") + kStringThreadThreads + u" threads * " + kStringThreadIterations + u" iterations each."); for(j = 0; j < kStringThreadThreads; j++) { - int32_t threadStatus = tests[j]->start(); + int32_t threadStatus = tests[j].start(); if (threadStatus != 0) { - errln("System Error %d starting thread number %d.", threadStatus, j); - SimpleThread::errorFunc(); - goto cleanupAndReturn; + errln("%s:%d System Error %d starting thread number %d.", __FILE__, __LINE__, threadStatus, j); } } - for(patience = kStringThreadPatience;patience > 0; patience --) - { - logln("Waiting..."); + // Force a failure, to verify test is functioning and can report errors. + // const_cast(StringThreadTest2::gSharedString)->setCharAt(5, 'x'); - int32_t i; - terrs = 0; - int32_t completed =0; + for(j=0; jisRunning() == FALSE) - { - completed++; - - logln(UnicodeString("Test #") + i + " is complete.. "); - - UnicodeString theErr; - if(tests[i]->getError(theErr)) - { - terrs++; - errln(UnicodeString("#") + i + ": " + theErr); - } - // print out the error, too, if any. - } + delete StringThreadTest2::gSharedString; + StringThreadTest2::gSharedString = NULL; +} + + +// +// Test for ticket #10673, race in cache code in AnyTransliterator. +// It's difficult to make the original unsafe code actually fail, but +// this test will fairly reliably take the code path for races in +// populating the cache. +// + +#if !UCONFIG_NO_TRANSLITERATION +Transliterator *gSharedTranslit = NULL; +class TxThread: public SimpleThread { + public: + TxThread() {} + ~TxThread(); + void run(); +}; + +TxThread::~TxThread() {} +void TxThread::run() { + UnicodeString greekString(u"διαφορετικούς"); + gSharedTranslit->transliterate(greekString); + IntlTest::gTest->assertEquals(WHERE, UnicodeString(u"diaphoretikoús"), greekString); +} +#endif + + +void MultithreadTest::TestAnyTranslit() { +#if !UCONFIG_NO_TRANSLITERATION + UErrorCode status = U_ZERO_ERROR; + LocalPointer tx(Transliterator::createInstance("Any-Latin", UTRANS_FORWARD, status)); + if (!assertSuccess(WHERE, status, true)) { return; } + + gSharedTranslit = tx.getAlias(); + TxThread threads[4]; + int32_t i; + for (i=0; i U_EXPORT +const UCTMultiThreadItem *LocaleCacheKey::createObject( + const void *context, UErrorCode &status) const { + const UnifiedCache *cacheContext = (const UnifiedCache *) context; + + if (uprv_strcmp(fLoc.getLanguage(), fLoc.getName()) != 0) { + const UCTMultiThreadItem *result = NULL; + if (cacheContext == NULL) { + UnifiedCache::getByLocale(fLoc.getLanguage(), result, status); + return result; } - - if(completed == kStringThreadThreads) - { - logln("Done!"); - if(terrs) { - errln("There were errors."); + cacheContext->get(LocaleCacheKey(fLoc.getLanguage()), result, status); + return result; + } + + bool firstObject = false; + { + std::unique_lock lock(*gCTMutex); + firstObject = (gObjectsCreated == 0); + if (firstObject) { + // Force the first object creation that comes through to wait + // until other have completed. Verifies that cache doesn't + // deadlock when a creation is slow. + + // Note that gObjectsCreated needs to be incremeneted from 0 to 1 + // early, to keep subsequent threads from entering this path. + gObjectsCreated = 1; + while (gObjectsCreated < 3) { + gCTConditionVar->wait(lock); } - break; } + } + + const UCTMultiThreadItem *result = + new UCTMultiThreadItem(fLoc.getLanguage()); + if (result == NULL) { + status = U_MEMORY_ALLOCATION_ERROR; + } else { + result->addRef(); + } + + // Log that we created an object. The first object was already counted, + // don't do it again. + { + std::unique_lock lock(*gCTMutex); + if (!firstObject) { + gObjectsCreated += 1; + } + gCTConditionVar->notify_all(); + } + + return result; +} + +U_NAMESPACE_END + +class UnifiedCacheThread: public SimpleThread { + public: + UnifiedCacheThread( + const UnifiedCache *cache, + const char *loc, + const char *loc2) : fCache(cache), fLoc(loc), fLoc2(loc2) {} + ~UnifiedCacheThread() {} + void run(); + void exerciseByLocale(const Locale &); + const UnifiedCache *fCache; + Locale fLoc; + Locale fLoc2; +}; + +void UnifiedCacheThread::exerciseByLocale(const Locale &locale) { + UErrorCode status = U_ZERO_ERROR; + const UCTMultiThreadItem *origItem = NULL; + fCache->get( + LocaleCacheKey(locale), fCache, origItem, status); + U_ASSERT(U_SUCCESS(status)); + IntlTest::gTest->assertEquals(WHERE, locale.getLanguage(), origItem->value); + + // Fetch the same item again many times. We should always get the same + // pointer since this client is already holding onto it + for (int32_t i = 0; i < 1000; ++i) { + const UCTMultiThreadItem *item = NULL; + fCache->get( + LocaleCacheKey(locale), fCache, item, status); + IntlTest::gTest->assertTrue(WHERE, item == origItem); + if (item != NULL) { + item->removeRef(); + } + } + origItem->removeRef(); +} - SimpleThread::sleep(900); +void UnifiedCacheThread::run() { + // Run the exercise with 2 different locales so that we can exercise + // eviction more. If each thread exercises just one locale, then + // eviction can't start until the threads end. + exerciseByLocale(fLoc); + exerciseByLocale(fLoc2); +} + +void MultithreadTest::TestUnifiedCache() { + + // Start with our own local cache so that we have complete control + // and set the eviction policy to evict starting with 2 unused + // values + UErrorCode status = U_ZERO_ERROR; + UnifiedCache::getInstance(status); + UnifiedCache cache(status); + cache.setEvictionPolicy(2, 0, status); + U_ASSERT(U_SUCCESS(status)); + + gCTMutex = new std::mutex(); + gCTConditionVar = new std::condition_variable(); + + gObjectsCreated = 0; + + UnifiedCacheThread *threads[CACHE_LOAD][UPRV_LENGTHOF(gCacheLocales)]; + for (int32_t i=0; istart(); + } } - if (patience <= 0) { - errln("patience exceeded. "); - // while (TRUE) {SimpleThread::sleep(10000);} // TODO: for debugging. Sleep forever on failure. - terrs++; + for (int32_t i=0; ijoin(); + } + } + // Because of cache eviction, we can't assert exactly how many + // distinct objects get created over the course of this run. + // However we know that at least 8 objects get created because that + // is how many distinct languages we have in our test. + if (gObjectsCreated < 8) { + errln("%s:%d Too few objects created.", __FILE__, __LINE__); } + // We know that each thread cannot create more than 2 objects in + // the cache, and there are UPRV_LENGTHOF(gCacheLocales) pairs of + // objects fetched from the cache. If the threads run in series because + // of eviction, at worst case each thread creates two objects. + if (gObjectsCreated > 2 * CACHE_LOAD * UPRV_LENGTHOF(gCacheLocales)) { + errln("%s:%d Too many objects created, got %d, expected %d", __FILE__, __LINE__, gObjectsCreated, 2 * CACHE_LOAD * UPRV_LENGTHOF(gCacheLocales)); - if (terrs > 0) { - SimpleThread::errorFunc(); } -cleanupAndReturn: - if (terrs == 0) { - /* - Don't clean up if there are errors. This prevents crashes if the - threads are still running and using this data. This will only happen - if there is an error with the test, ICU, or the machine is too slow. - It's better to leak than crash. - */ - for(j = 0; j < kStringThreadThreads; j++) { - delete tests[j]; + assertEquals(WHERE, 2, cache.unusedCount()); + + // clean up threads + for (int32_t i=0; itransliterate(s); + if (*gTranslitExpected != s) { + IntlTest::gTest->errln("%s:%d Transliteration threading failure.", __FILE__, __LINE__); + break; } - delete testString; } } +void MultithreadTest::TestBreakTranslit() { + UErrorCode status = U_ZERO_ERROR; + UnicodeString input( + u"\u0E42\u0E14\u0E22\u0E1E\u0E37\u0E49\u0E19\u0E10\u0E32\u0E19\u0E41\u0E25\u0E49\u0E27,"); + // Thai script, โดยพื้นฐานแล้ว + gTranslitInput = &input; + + gSharedTransliterator = Transliterator::createInstance( + UnicodeString(u"Any-Latin; Lower; NFD; [:Diacritic:]Remove; NFC; Latin-ASCII;"), UTRANS_FORWARD, status); + assertSuccess(WHERE, status); + if (!assertTrue(WHERE, gSharedTransliterator != nullptr)) { + return; + } + + UnicodeString expected(*gTranslitInput); + gSharedTransliterator->transliterate(expected); + gTranslitExpected = &expected; + + BreakTranslitThread threads[4]; + for (int i=0; idefaultCenturyStartYear(); +} + +void MultithreadTest::Test20104() { + UErrorCode status = U_ZERO_ERROR; + Locale loc("hi_IN"); + gSharedCalendar = new IndianCalendar(loc, status); + assertSuccess(WHERE, status); + + static constexpr int NUM_THREADS = 4; + Test20104Thread threads[NUM_THREADS]; + for (auto &thread:threads) { + thread.start(); + } + for (auto &thread:threads) { + thread.join(); + } + delete gSharedCalendar; + // Note: failure is reported by Thread Sanitizer. Test itself succeeds. +} +#endif /* !UCONFIG_NO_FORMATTING */ +#endif /* !UCONFIG_NO_TRANSLITERATION */