2000-01-15 02:00:06 +00:00
/********************************************************************
* COPYRIGHT :
2001-03-21 20:09:56 +00:00
* Copyright ( c ) 2001 , International Business Machines Corporation and
2000-01-15 02:00:06 +00:00
* others . All Rights Reserved .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2000-06-01 18:27:44 +00:00
# if defined(hpux)
# ifndef _INCLUDE_POSIX_SOURCE
# define _INCLUDE_POSIX_SOURCE
# endif
# endif
2000-05-12 01:15:50 +00:00
# include <unicode/umachine.h>
2002-01-24 07:51:02 +00:00
# if !defined(WIN32) && !defined(XP_MAC) && !defined(U_RHAPSODY)
2000-01-27 00:00:04 +00:00
# define POSIX 1
# endif
2000-08-11 17:40:12 +00:00
# if defined(POSIX) || defined(U_SOLARIS) || defined(AIX) || defined(HPUX)
2000-01-27 00:00:04 +00:00
# define HAVE_IMP
2000-10-04 18:30:18 +00:00
# if (ICU_USE_THREADS == 1)
2000-01-27 00:00:04 +00:00
# include <pthread.h>
2000-10-04 18:30:18 +00:00
# endif
2000-05-31 22:18:22 +00:00
# if defined(__hpux) && defined(HPUX_CMA)
# if defined(read) // read being defined as cma_read causes trouble with iostream::read
# undef read
# endif
# endif
2001-11-27 19:44:51 +00:00
/* Define __EXTENSIONS__ for Solaris and old friends in strict mode. */
# ifndef __EXTENSIONS__
# define __EXTENSIONS__
# endif
# include <signal.h>
2000-05-12 01:15:50 +00:00
/* 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
2000-05-31 22:18:22 +00:00
# define __USE_XOPEN_EXTENDED
2000-05-12 01:15:50 +00:00
# endif
2000-11-22 01:54:24 +00:00
/* Define _INCLUDE_XOPEN_SOURCE_EXTENDED for HP/UX (11?). */
# ifndef _INCLUDE_XOPEN_SOURCE_EXTENDED
# define _INCLUDE_XOPEN_SOURCE_EXTENDED
# endif
2000-05-12 01:15:50 +00:00
# include <unistd.h>
2000-01-27 00:00:04 +00:00
# endif
/* HPUX */
# ifdef sleep
# undef sleep
# endif
1999-12-28 23:57:50 +00:00
# include "unicode/utypes.h"
1999-12-09 23:11:48 +00:00
/* APP_NO_THREADS is an old symbol. We'll honour it if present. */
# ifdef APP_NO_THREADS
# define ICU_USE_THREADS 0
# endif
/* Default: use threads. */
# ifndef ICU_USE_THREADS
# define ICU_USE_THREADS 1
# endif
# include "tsmthred.h"
MultithreadTest : : MultithreadTest ( )
{
}
MultithreadTest : : ~ MultithreadTest ( )
{
}
# if (ICU_USE_THREADS==0)
2000-05-18 22:08:39 +00:00
void MultithreadTest : : runIndexedTest ( int32_t index , UBool exec ,
2000-08-14 21:42:36 +00:00
const char * & name , char * par ) {
1999-12-09 23:11:48 +00:00
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
1999-08-16 21:50:52 +00:00
// 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 <stdio.h>
# include <string.h>
# include <ctype.h> // tolower, toupper
1999-12-28 23:57:50 +00:00
# include "unicode/putil.h"
1999-08-16 21:50:52 +00:00
/* for mthreadtest*/
1999-12-28 23:57:50 +00:00
# include "unicode/numfmt.h"
# include "unicode/choicfmt.h"
# include "unicode/msgfmt.h"
# include "unicode/locid.h"
1999-08-16 21:50:52 +00:00
# ifdef WIN32
# define HAVE_IMP
# include <windows.h>
struct Win32ThreadImplementation
{
HANDLE fHandle ;
DWORD fThreadID ;
} ;
extern " C " unsigned long _stdcall SimpleThreadProc ( void * arg )
{
( ( SimpleThread * ) arg ) - > run ( ) ;
return 0 ;
}
SimpleThread : : SimpleThread ( )
: fImplementation ( 0 )
{
Win32ThreadImplementation * imp = new Win32ThreadImplementation ;
imp - > fHandle = 0 ;
imp - > fThreadID = 0 ;
fImplementation = imp ;
}
SimpleThread : : ~ SimpleThread ( )
{
delete ( Win32ThreadImplementation * ) fImplementation ;
}
void SimpleThread : : start ( )
{
Win32ThreadImplementation * imp = ( Win32ThreadImplementation * ) fImplementation ;
if ( imp - > fHandle ! = NULL )
return ;
imp - > fHandle = CreateThread ( NULL , 0 , SimpleThreadProc , ( void * ) this , 0 , & imp - > fThreadID ) ;
}
void SimpleThread : : sleep ( int32_t millis )
{
: : Sleep ( millis ) ;
}
# 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 ( )
{ }
void
SimpleThread : : start ( )
{ }
void
SimpleThread : : run ( )
{ }
void
SimpleThread : : sleep ( int32_t millis )
{ }
# endif
1999-12-09 23:11:48 +00:00
2000-06-30 21:35:03 +00:00
# if defined(POSIX)||defined(U_SOLARIS)||defined(AIX)||defined(HPUX)
1999-08-16 21:50:52 +00:00
# define HAVE_IMP
struct PosixThreadImplementation
{
pthread_t fThread ;
} ;
extern " C " void * SimpleThreadProc ( void * arg )
{
( ( SimpleThread * ) arg ) - > run ( ) ;
return 0 ;
}
SimpleThread : : SimpleThread ( ) : fImplementation ( 0 )
{
PosixThreadImplementation * imp = new PosixThreadImplementation ;
fImplementation = imp ;
}
SimpleThread : : ~ SimpleThread ( )
{
delete ( PosixThreadImplementation * ) fImplementation ;
}
void SimpleThread : : start ( )
{
PosixThreadImplementation * imp = ( PosixThreadImplementation * ) fImplementation ;
int32_t rc ;
pthread_attr_t attr ;
2000-01-22 02:54:48 +00:00
# ifdef HPUX_CMA
2000-08-14 21:42:36 +00:00
rc = pthread_attr_create ( & attr ) ;
1999-08-16 21:50:52 +00:00
rc = pthread_create ( & ( imp - > fThread ) , attr , & SimpleThreadProc , ( void * ) this ) ;
pthread_attr_delete ( & attr ) ;
# else
2000-08-14 21:42:36 +00:00
rc = pthread_attr_init ( & attr ) ;
1999-08-16 21:50:52 +00:00
rc = pthread_create ( & ( imp - > fThread ) , & attr , & SimpleThreadProc , ( void * ) this ) ;
pthread_attr_destroy ( & attr ) ;
# endif
}
void SimpleThread : : sleep ( int32_t millis )
{
2000-06-30 21:35:03 +00:00
# ifdef U_SOLARIS
2000-11-22 01:54:24 +00:00
sigignore ( SIGALRM ) ;
1999-08-16 21:50:52 +00:00
# endif
2000-01-22 02:54:48 +00:00
# ifdef HPUX_CMA
2000-11-22 01:54:24 +00:00
cma_sleep ( millis / 100 ) ;
# elif defined(HPUX) || defined(OS390)
millis * = 1000 ;
while ( millis > = 1000000 ) {
usleep ( 999999 ) ;
millis - = 1000000 ;
}
if ( millis > 0 ) {
usleep ( millis ) ;
}
2000-01-27 21:01:18 +00:00
# else
2000-11-22 01:54:24 +00:00
usleep ( millis * 1000 ) ;
1999-08-16 21:50:52 +00:00
# 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. */
2000-05-18 22:08:39 +00:00
void MultithreadTest : : runIndexedTest ( int32_t index , UBool exec ,
2000-08-23 19:11:16 +00:00
const char * & name , char * /*par*/ ) {
2000-11-22 01:54:24 +00:00
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 ( exec )
TestThreadedIntl ( ) ;
break ;
default :
name = " " ;
break ; //needed to end loop
}
1999-08-16 21:50:52 +00:00
}
/*
TestThreads - - see if threads really work at all .
Set up N threads pointing at N chars . When they are started , they will
2000-01-27 17:57:14 +00:00
each sleep 1 second and then set their chars . At the end we make sure they
1999-08-16 21:50:52 +00:00
are all set .
*/
# define THREADTEST_NRTHREADS 8
class TestThreadsThread : public SimpleThread
{
public :
TestThreadsThread ( char * whatToChange ) { fWhatToChange = whatToChange ; }
2000-01-27 17:57:14 +00:00
virtual void run ( ) { SimpleThread : : sleep ( 1000 ) ; * fWhatToChange = ' * ' ; }
1999-08-16 21:50:52 +00:00
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 + + )
{
threads [ i ] - > start ( ) ;
SimpleThread : : sleep ( 200 ) ;
logln ( " Subthread started. " ) ;
}
logln ( " Waiting for threads to be set.. " ) ;
int32_t patience = 40 ; // seconds to wait
while ( patience - - )
{
int32_t count = 0 ;
for ( i = 0 ; i < THREADTEST_NRTHREADS ; i + + )
{
if ( threadTestChars [ i ] = = ' * ' )
{
count + + ;
}
}
if ( count = = THREADTEST_NRTHREADS )
{
logln ( " -> " + UnicodeString ( threadTestChars ) + " <- Got all threads! cya " ) ;
2000-06-29 19:42:17 +00:00
for ( i = 0 ; i < THREADTEST_NRTHREADS ; i + + )
{
delete threads [ i ] ;
}
1999-08-16 21:50:52 +00:00
return ;
}
logln ( " -> " + UnicodeString ( threadTestChars ) + " <- Waiting.. " ) ;
SimpleThread : : sleep ( 500 ) ;
}
errln ( " -> " + UnicodeString ( threadTestChars ) + " <- PATIENCE EXCEEDED!! Still missing some. " ) ;
2000-06-29 19:42:17 +00:00
for ( i = 0 ; i < THREADTEST_NRTHREADS ; i + + )
{
delete threads [ i ] ;
}
1999-08-16 21:50:52 +00:00
}
class TestMutexThread1 : public SimpleThread
{
public :
TestMutexThread1 ( ) : fDone ( FALSE ) { }
virtual void run ( )
{
Mutex m ; // grab the lock first thing
2000-01-27 17:57:14 +00:00
SimpleThread : : sleep ( 900 ) ; // then wait
1999-08-16 21:50:52 +00:00
fDone = TRUE ; // finally, set our flag
}
public :
2000-05-18 22:08:39 +00:00
UBool fDone ;
1999-08-16 21:50:52 +00:00
} ;
class TestMutexThread2 : public SimpleThread
{
public :
TestMutexThread2 ( TestMutexThread1 & r ) : fOtherThread ( r ) , fDone ( FALSE ) , fErr ( FALSE ) { }
virtual void run ( )
{
2000-01-27 17:57:14 +00:00
SimpleThread : : sleep ( 500 ) ; // wait, make sure they aquire the lock
1999-12-28 23:57:50 +00:00
fElapsed = uprv_getUTCtime ( ) ;
1999-08-16 21:50:52 +00:00
{
Mutex m ; // wait here
1999-12-28 23:57:50 +00:00
fElapsed = uprv_getUTCtime ( ) - fElapsed ;
1999-08-16 21:50:52 +00:00
if ( fOtherThread . fDone = = FALSE )
fErr = TRUE ; // they didnt get to it yet
fDone = TRUE ; // we're done.
}
}
public :
TestMutexThread1 & fOtherThread ;
2000-05-18 22:08:39 +00:00
UBool fDone , fErr ;
1999-08-16 21:50:52 +00:00
int32_t fElapsed ;
2000-08-23 19:11:16 +00:00
private :
/**
* The assignment operator has no real implementation .
* It is provided to make the compiler happy . Do not call .
*/
TestMutexThread2 & operator = ( const TestMutexThread2 & ) { return * this ; }
1999-08-16 21:50:52 +00:00
} ;
void MultithreadTest : : TestMutex ( )
{
2000-11-22 01:54:24 +00:00
/* this test uses printf so that we don't hang by calling UnicodeString inside of a mutex. */
//logln("Bye.");
// printf("Warning: MultiThreadTest::Testmutex() disabled.\n");
// return;
1999-08-16 21:50:52 +00:00
if ( verbose )
2001-03-26 23:46:57 +00:00
printf ( " Before mutex. \n " ) ;
1999-08-16 21:50:52 +00:00
{
2000-11-22 01:54:24 +00:00
Mutex m ;
if ( verbose )
2001-03-26 23:46:57 +00:00
printf ( " Exited 2nd mutex \n " ) ;
2000-11-22 01:54:24 +00:00
}
if ( verbose )
2001-03-26 23:46:57 +00:00
printf ( " exited 1st mutex. Now testing with threads: " ) ;
2000-11-22 01:54:24 +00:00
TestMutexThread1 thread1 ;
TestMutexThread2 thread2 ( thread1 ) ;
thread2 . start ( ) ;
thread1 . start ( ) ;
for ( int32_t patience = 12 ; patience > 0 ; patience - - )
{
if ( thread1 . fDone & & verbose )
2001-03-26 23:46:57 +00:00
printf ( " Thread1 done \n " ) ;
2000-11-22 01:54:24 +00:00
if ( thread1 . fDone & & thread2 . fDone )
1999-08-16 21:50:52 +00:00
{
2000-11-22 01:54:24 +00:00
if ( thread2 . fErr )
errln ( " Thread 2 says: thread1 didn't run before I aquired the mutex. " ) ;
2001-03-26 23:46:57 +00:00
logln ( " took %lu seconds for thread2 to aquire the mutex. " , thread2 . fElapsed ) ;
2000-11-22 01:54:24 +00:00
return ;
1999-08-16 21:50:52 +00:00
}
2000-11-22 01:54:24 +00:00
SimpleThread : : sleep ( 1000 ) ;
1999-08-16 21:50:52 +00:00
}
2000-11-22 01:54:24 +00:00
if ( verbose )
printf ( " patience exceeded. [WARNING mutex may still be acquired.] " ) ;
1999-08-16 21:50:52 +00:00
}
// ***********
// *********** TestMultithreadedIntl. Test the ICU in a multithreaded way.
// ** First, some utility classes.
//
///* Here is an idea which needs more work
// TestATest simply runs another Intltest subset against itself.
// The correct subset of intltest that should be run in this way should be identified.
// */
//
//class TestATest : public SimpleThread
//{
//public:
// TestATest(IntlTest &t) : fTest(t), fDone(FALSE) {}
// virtual void run()
// {
// fTest.runTest(NULL,"TestNumberSpelloutFormat");
// fErrs = fTest.getErrors();
// fDone = TRUE;
// }
//public:
// IntlTest &fTest;
2000-05-18 22:08:39 +00:00
// UBool fDone;
1999-08-16 21:50:52 +00:00
// int32_t fErrs;
//};
//
//
//#include "itutil.h"
////#include "tscoll.h"
////#include "ittxtbd.h"
//#include "itformat.h"
////#include "itcwrap.h"
//
///* main code was:
// IntlTestFormat formatTest;
//// IntlTestCollator collatorTest;
//
// #define NUMTESTS 2
// TestATest tests[NUMTESTS] = { TestATest(formatTest), TestATest(formatTest) };
// char testName[NUMTESTS][20] = { "formatTest", "formatTest2" };
//*/
# include <string.h>
// * Show exactly where the string's differences lie.
UnicodeString showDifference ( const UnicodeString & expected , const UnicodeString & result )
{
UnicodeString res ;
res = expected + " <Expected \n " ;
1999-12-08 02:11:04 +00:00
if ( expected . length ( ) ! = result . length ( ) )
1999-08-16 21:50:52 +00:00
res + = " [ Different lengths ] \n " ;
else
{
1999-12-08 02:11:04 +00:00
for ( int32_t i = 0 ; i < expected . length ( ) ; i + + )
1999-08-16 21:50:52 +00:00
{
if ( expected [ i ] = = result [ i ] )
{
res + = " " ;
}
else
{
res + = " | " ;
}
}
res + = " <Differences " ;
res + = " \n " ;
}
res + = result + " <Result \n " ;
return res ;
}
// ** ThreadWithStatus - a thread that we can check the status and error condition of
class ThreadWithStatus : public SimpleThread
{
public :
2000-05-18 22:08:39 +00:00
UBool getDone ( ) { return fDone ; }
UBool getError ( ) { return ( fErrors > 0 ) ; }
UBool getError ( UnicodeString & fillinError ) { fillinError = fErrorString ; return ( fErrors > 0 ) ; }
1999-08-16 21:50:52 +00:00
virtual ~ ThreadWithStatus ( ) { }
protected :
ThreadWithStatus ( ) : fDone ( FALSE ) , fErrors ( 0 ) { }
void done ( ) { fDone = TRUE ; }
void error ( const UnicodeString & error ) { fErrors + + ; fErrorString = error ; done ( ) ; }
void error ( ) { error ( " An error occured. " ) ; }
private :
2000-05-18 22:08:39 +00:00
UBool fDone ;
1999-08-16 21:50:52 +00:00
int32_t fErrors ;
UnicodeString fErrorString ;
} ;
// ** FormatThreadTest - a thread that tests performing a number of numberformats.
# define kFormatThreadIterations 20 // # of iterations per thread
# define kFormatThreadThreads 10 // # of threads to spawn
# define kFormatThreadPatience 60 // time in seconds to wait for all threads
struct FormatThreadTestData
{
double number ;
UnicodeString string ;
FormatThreadTestData ( double a , const UnicodeString & b ) : number ( a ) , string ( b ) { }
} ;
void errorToString ( UErrorCode theStatus , UnicodeString & string )
{
1999-12-08 23:31:17 +00:00
string = u_errorName ( theStatus ) ;
1999-08-16 21:50:52 +00:00
}
// "Someone from {2} is receiving a #{0} error - {1}. Their telephone call is costing {3 number,currency}."
void formatErrorMessage ( UErrorCode & realStatus , const UnicodeString & pattern , const Locale & theLocale ,
UErrorCode inStatus0 , /* statusString 1 */ const Locale & inCountry2 , double currency3 , // these numbers are the message arguments.
UnicodeString & result )
{
1999-10-18 22:48:32 +00:00
if ( U_FAILURE ( realStatus ) )
1999-08-16 21:50:52 +00:00
return ; // you messed up
UnicodeString errString1 ;
errorToString ( inStatus0 , errString1 ) ;
UnicodeString countryName2 ;
inCountry2 . getDisplayCountry ( theLocale , countryName2 ) ;
Formattable myArgs [ ] = {
Formattable ( ( int32_t ) inStatus0 ) , // inStatus0 {0}
Formattable ( errString1 ) , // statusString1 {1}
Formattable ( countryName2 ) , // inCountry2 {2}
Formattable ( currency3 ) // currency3 {3,number,currency}
} ;
MessageFormat * fmt = new MessageFormat ( " MessageFormat's API is broken!!!!!!!!!!! " , realStatus ) ;
fmt - > setLocale ( theLocale ) ;
fmt - > applyPattern ( pattern , realStatus ) ;
1999-10-18 22:48:32 +00:00
if ( U_FAILURE ( realStatus ) ) {
1999-08-16 21:50:52 +00:00
delete fmt ;
return ;
}
FieldPosition ignore = 0 ;
fmt - > format ( myArgs , 4 , result , ignore , realStatus ) ;
delete fmt ;
} ;
class FormatThreadTest : public ThreadWithStatus
{
public :
FormatThreadTest ( ) // constructor is NOT multithread safe.
2001-07-03 19:47:22 +00:00
: ThreadWithStatus ( ) ,
fOffset ( 0 )
1999-08-16 21:50:52 +00:00
// the locale to use
{
static int32_t fgOffset = 0 ;
2001-07-03 19:47:22 +00:00
fgOffset + = 3 ;
fOffset = fgOffset ;
1999-08-16 21:50:52 +00:00
}
virtual void run ( )
{
2001-07-03 19:47:22 +00:00
// Keep this data here to avoid static initialization.
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 ] ) ) ;
// Keep this data here to avoid static initialization.
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% " ) ) ,
} ;
int32_t kPercentFormatTestDataLength = ( int32_t ) ( sizeof ( kPercentFormatTestData ) / sizeof ( kPercentFormatTestData [ 0 ] ) ) ;
1999-08-16 21:50:52 +00:00
int32_t iteration ;
1999-10-07 00:07:53 +00:00
UErrorCode status = U_ZERO_ERROR ;
1999-08-16 21:50:52 +00:00
NumberFormat * formatter = NumberFormat : : createInstance ( Locale : : ENGLISH , status ) ;
1999-10-18 22:48:32 +00:00
if ( U_FAILURE ( status ) )
1999-08-16 21:50:52 +00:00
{
error ( " Error on NumberFormat::createInstance() " ) ;
return ;
}
NumberFormat * percentFormatter = NumberFormat : : createPercentInstance ( Locale : : FRENCH , status ) ;
1999-10-18 22:48:32 +00:00
if ( U_FAILURE ( status ) )
1999-08-16 21:50:52 +00:00
{
error ( " Error on NumberFormat::createPercentInstance() " ) ;
delete formatter ;
return ;
}
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 ) ;
continue ; // will break
}
// 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 " + showDifference ( kPercentFormatTestData [ whichLine ] . string , output ) ) ;
continue ;
}
// Test message error
# define kNumberOfMessageTests 3
UErrorCode statusToCheck ;
UnicodeString patternToCheck ;
Locale messageLocale ;
Locale countryToCheck ;
double currencyToCheck ;
UnicodeString expected ;
// load the cases.
switch ( ( iteration + fOffset ) % kNumberOfMessageTests )
{
default :
case 0 :
1999-10-07 00:07:53 +00:00
statusToCheck = U_FILE_ACCESS_ERROR ;
1999-08-16 21:50:52 +00:00
patternToCheck = " 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 ;
1999-10-07 00:07:53 +00:00
expected = " 0:Someone from Croatia is receiving a #4 error - U_FILE_ACCESS_ERROR. Their telephone call is costing $8,192.77. " ;
1999-08-16 21:50:52 +00:00
break ;
case 1 :
1999-10-07 00:07:53 +00:00
statusToCheck = U_INDEX_OUTOFBOUNDS_ERROR ;
1999-08-16 21:50:52 +00:00
patternToCheck = " 1:A customer in {2} is receiving a #{0} error - {1}. Their telephone call is costing {3,number,currency}. " ; // number,currency
2001-10-12 19:30:13 +00:00
messageLocale = Locale ( " de " , " DE_PREEURO " ) ;
1999-08-16 21:50:52 +00:00
countryToCheck = Locale ( " " , " BF " ) ;
currencyToCheck = 2.32 ;
1999-10-07 00:07:53 +00:00
expected = " 1:A customer in Burkina Faso is receiving a #8 error - U_INDEX_OUTOFBOUNDS_ERROR. Their telephone call is costing $2.32. " ;
1999-08-16 21:50:52 +00:00
case 2 :
1999-10-07 00:07:53 +00:00
statusToCheck = U_MEMORY_ALLOCATION_ERROR ;
1999-08-16 21:50:52 +00:00
patternToCheck = " 2:user in {2} is receiving a #{0} error - {1}. They insist they just spent {3,number,currency} on memory. " ; // number,currency
2001-10-12 19:30:13 +00:00
messageLocale = Locale ( " de " , " AT_PREEURO " ) ; // Austrian German
1999-08-16 21:50:52 +00:00
countryToCheck = Locale ( " " , " US " ) ; // hmm
currencyToCheck = 40193.12 ;
2000-03-22 23:17:42 +00:00
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. " ) ;
1999-08-16 21:50:52 +00:00
break ;
}
UnicodeString result ;
1999-10-07 00:07:53 +00:00
UErrorCode status = U_ZERO_ERROR ;
1999-08-16 21:50:52 +00:00
formatErrorMessage ( status , patternToCheck , messageLocale , statusToCheck , countryToCheck , currencyToCheck , result ) ;
1999-10-18 22:48:32 +00:00
if ( U_FAILURE ( status ) )
1999-08-16 21:50:52 +00:00
{
UnicodeString tmp ;
errorToString ( status , tmp ) ;
error ( " Failure on message format, pattern= " + patternToCheck + " , error = " + tmp ) ;
continue ;
}
if ( result ! = expected )
{
error ( " PatternFormat: \n " + showDifference ( expected , result ) ) ;
continue ;
}
}
delete formatter ;
delete percentFormatter ;
done ( ) ;
}
private :
int32_t fOffset ; // where we are testing from.
} ;
// ** The actual test function.
void MultithreadTest : : TestThreadedIntl ( )
{
FormatThreadTest tests [ kFormatThreadThreads ] ;
logln ( UnicodeString ( " Spawning: " ) + kFormatThreadThreads + " threads * " + kFormatThreadIterations + " iterations each. " ) ;
for ( int32_t j = 0 ; j < kFormatThreadThreads ; j + + )
tests [ j ] . start ( ) ;
for ( int32_t patience = kFormatThreadPatience ; patience > 0 ; patience - - )
{
logln ( " Waiting... " ) ;
int32_t i ;
int32_t terrs = 0 ;
int32_t completed = 0 ;
for ( i = 0 ; i < kFormatThreadThreads ; i + + )
{
if ( tests [ i ] . getDone ( ) )
{
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.
}
}
if ( completed = = kFormatThreadThreads )
{
logln ( " Done! " ) ;
if ( terrs )
{
errln ( " There were errors. " ) ;
}
return ;
}
2000-01-27 17:57:14 +00:00
SimpleThread : : sleep ( 900 ) ;
1999-08-16 21:50:52 +00:00
}
errln ( " patience exceeded. " ) ;
}
1999-12-09 23:11:48 +00:00
# endif // ICU_USE_THREADS