]> git.saurik.com Git - apple/icu.git/blame - icuSources/test/intltest/tsmthred.cpp
ICU-62141.0.1.tar.gz
[apple/icu.git] / icuSources / test / intltest / tsmthred.cpp
CommitLineData
f3c0d7a5
A
1// © 2016 and later: Unicode, Inc. and others.
2// License & terms of use: http://www.unicode.org/copyright.html
b75a7d8f 3/********************************************************************
b331163b 4 * COPYRIGHT:
2ca993e8 5 * Copyright (c) 1999-2015, International Business Machines Corporation and
b75a7d8f
A
6 * others. All Rights Reserved.
7 ********************************************************************/
8
729e4ab9 9#include "simplethread.h"
46f4442e 10
374ca955
A
11#include "unicode/utypes.h"
12#include "unicode/ustring.h"
13#include "umutex.h"
14#include "cmemory.h"
15#include "cstring.h"
16#include "uparse.h"
729e4ab9 17#include "unicode/localpointer.h"
374ca955
A
18#include "unicode/resbund.h"
19#include "unicode/udata.h"
20#include "unicode/uloc.h"
21#include "unicode/locid.h"
22#include "putilimp.h"
51004dcb
A
23#include "intltest.h"
24#include "tsmthred.h"
25#include "unicode/ushape.h"
57a6839d 26#include "unicode/translit.h"
b331163b
A
27#include "sharedobject.h"
28#include "unifiedcache.h"
29#include "uassert.h"
4388f060 30
b75a7d8f 31
73c04bcf
A
32#define TSMTHREAD_FAIL(msg) errln("%s at file %s, line %d", msg, __FILE__, __LINE__)
33#define TSMTHREAD_ASSERT(expr) {if (!(expr)) {TSMTHREAD_FAIL("Fail");}}
2ca993e8
A
34#define TSMTHREAD_ASSERT_SUCCESS(status) {if (U_FAILURE(status)) { \
35 errln("file: %s:%d status = %s\n", __FILE__, __LINE__, u_errorName(status));}}
b75a7d8f
A
36
37MultithreadTest::MultithreadTest()
38{
39}
40
41MultithreadTest::~MultithreadTest()
42{
43}
44
b75a7d8f
A
45#include <stdio.h>
46#include <string.h>
47#include <ctype.h> // tolower, toupper
0f5d89e8 48#include <memory>
b75a7d8f
A
49
50#include "unicode/putil.h"
51
729e4ab9 52// for mthreadtest
b75a7d8f
A
53#include "unicode/numfmt.h"
54#include "unicode/choicfmt.h"
55#include "unicode/msgfmt.h"
56#include "unicode/locid.h"
51004dcb 57#include "unicode/coll.h"
374ca955 58#include "unicode/calendar.h"
b75a7d8f
A
59#include "ucaconf.h"
60
374ca955 61
b331163b 62void MultithreadTest::runIndexedTest( int32_t index, UBool exec,
b75a7d8f
A
63 const char* &name, char* /*par*/ ) {
64 if (exec)
65 logln("TestSuite MultithreadTest: ");
0f5d89e8
A
66
67 TESTCASE_AUTO_BEGIN;
68 TESTCASE_AUTO(TestThreads);
69 TESTCASE_AUTO(TestMutex);
b75a7d8f 70#if !UCONFIG_NO_FORMATTING
0f5d89e8 71 TESTCASE_AUTO(TestThreadedIntl);
b75a7d8f 72#endif
b75a7d8f 73#if !UCONFIG_NO_COLLATION
0f5d89e8 74 TESTCASE_AUTO(TestCollators);
b75a7d8f 75#endif /* #if !UCONFIG_NO_COLLATION */
0f5d89e8
A
76 TESTCASE_AUTO(TestString);
77 TESTCASE_AUTO(TestArabicShapingThreads);
78 TESTCASE_AUTO(TestAnyTranslit);
79 TESTCASE_AUTO(TestConditionVariables);
80 TESTCASE_AUTO(TestUnifiedCache);
2ca993e8 81#if !UCONFIG_NO_TRANSLITERATION
0f5d89e8
A
82 TESTCASE_AUTO(TestBreakTranslit);
83 TESTCASE_AUTO(TestIncDec);
84#endif /* #if !UCONFIG_NO_TRANSLITERATION */
85 TESTCASE_AUTO_END
b75a7d8f
A
86}
87
88
374ca955
A
89//-----------------------------------------------------------------------------------
90//
91// TestThreads -- see if threads really work at all.
92//
93// Set up N threads pointing at N chars. When they are started, they will
2ca993e8 94// set their chars. At the end we make sure they are all set.
374ca955
A
95//
96//-----------------------------------------------------------------------------------
b75a7d8f
A
97
98class TestThreadsThread : public SimpleThread
99{
100public:
101 TestThreadsThread(char* whatToChange) { fWhatToChange = whatToChange; }
2ca993e8 102 virtual void run() { Mutex m;
b331163b 103 *fWhatToChange = '*';
b75a7d8f
A
104 }
105private:
106 char *fWhatToChange;
51004dcb 107};
b331163b 108
b75a7d8f
A
109
110void MultithreadTest::TestThreads()
111{
2ca993e8 112 static const int32_t THREADTEST_NRTHREADS = 8;
b75a7d8f
A
113 char threadTestChars[THREADTEST_NRTHREADS + 1];
114 SimpleThread *threads[THREADTEST_NRTHREADS];
73c04bcf 115 int32_t numThreadsStarted = 0;
b75a7d8f
A
116
117 int32_t i;
118 for(i=0;i<THREADTEST_NRTHREADS;i++)
119 {
120 threadTestChars[i] = ' ';
121 threads[i] = new TestThreadsThread(&threadTestChars[i]);
122 }
123 threadTestChars[THREADTEST_NRTHREADS] = '\0';
124
125 logln("->" + UnicodeString(threadTestChars) + "<- Firing off threads.. ");
126 for(i=0;i<THREADTEST_NRTHREADS;i++)
127 {
128 if (threads[i]->start() != 0) {
129 errln("Error starting thread %d", i);
130 }
73c04bcf
A
131 else {
132 numThreadsStarted++;
133 }
b75a7d8f
A
134 logln(" Subthread started.");
135 }
136
2ca993e8
A
137 if (numThreadsStarted != THREADTEST_NRTHREADS) {
138 errln("Not all threads could be started for testing!");
73c04bcf
A
139 return;
140 }
b75a7d8f 141
2ca993e8
A
142 logln("Waiting for threads to be set..");
143 for(i=0; i<THREADTEST_NRTHREADS; i++) {
144 threads[i]->join();
145 if (threadTestChars[i] != '*') {
146 errln("%s:%d Thread %d failed.", __FILE__, __LINE__, i);
b75a7d8f 147 }
b75a7d8f
A
148 delete threads[i];
149 }
150}
151
152
2ca993e8
A
153//-----------------------------------------------------------------------------------
154//
155// TestArabicShapeThreads -- see if calls to u_shapeArabic in many threads works successfully
156//
157// Set up N threads pointing at N chars. When they are started, they will make calls to doTailTest which tests
158// u_shapeArabic, if the calls are successful it will the set * chars.
159// At the end we make sure all threads managed to run u_shapeArabic successfully.
160// This is a unit test for ticket 9473
161//
162//-----------------------------------------------------------------------------------
163
164class TestArabicShapeThreads : public SimpleThread
51004dcb 165{
2ca993e8
A
166public:
167 TestArabicShapeThreads() {};
168 virtual void run() { doTailTest(); };
169private:
170 void doTailTest();
171};
51004dcb 172
b331163b 173
2ca993e8
A
174void TestArabicShapeThreads::doTailTest(void) {
175 static const UChar src[] = { 0x0020, 0x0633, 0 };
176 static const UChar dst_old[] = { 0xFEB1, 0x200B,0 };
177 static const UChar dst_new[] = { 0xFEB1, 0xFE73,0 };
178 UChar dst[3] = { 0x0000, 0x0000,0 };
179 int32_t length;
180 UErrorCode status;
51004dcb 181
2ca993e8
A
182 for (int32_t loopCount = 0; loopCount < 100; loopCount++) {
183 status = U_ZERO_ERROR;
184 length = u_shapeArabic(src, -1, dst, UPRV_LENGTHOF(dst),
185 U_SHAPE_LETTERS_SHAPE|U_SHAPE_SEEN_TWOCELL_NEAR, &status);
186 if(U_FAILURE(status)) {
187 IntlTest::gTest->errln("Fail: status %s\n", u_errorName(status));
188 return;
189 } else if(length!=2) {
190 IntlTest::gTest->errln("Fail: len %d expected 3\n", length);
191 return;
192 } else if(u_strncmp(dst,dst_old,UPRV_LENGTHOF(dst))) {
193 IntlTest::gTest->errln("Fail: got U+%04X U+%04X expected U+%04X U+%04X\n",
194 dst[0],dst[1],dst_old[0],dst_old[1]);
195 return;
51004dcb 196 }
51004dcb 197
2ca993e8
A
198
199 //"Trying new tail
200 status = U_ZERO_ERROR;
201 length = u_shapeArabic(src, -1, dst, UPRV_LENGTHOF(dst),
202 U_SHAPE_LETTERS_SHAPE|U_SHAPE_SEEN_TWOCELL_NEAR|U_SHAPE_TAIL_NEW_UNICODE, &status);
203 if(U_FAILURE(status)) {
204 IntlTest::gTest->errln("Fail: status %s\n", u_errorName(status));
205 return;
206 } else if(length!=2) {
207 IntlTest::gTest->errln("Fail: len %d expected 3\n", length);
208 return;
209 } else if(u_strncmp(dst,dst_new,UPRV_LENGTHOF(dst))) {
210 IntlTest::gTest->errln("Fail: got U+%04X U+%04X expected U+%04X U+%04X\n",
211 dst[0],dst[1],dst_new[0],dst_new[1]);
212 return;
213 }
51004dcb 214 }
2ca993e8
A
215 return;
216}
217
51004dcb 218
2ca993e8
A
219void MultithreadTest::TestArabicShapingThreads()
220{
221 TestArabicShapeThreads threads[30];
51004dcb 222
2ca993e8 223 int32_t i;
b331163b 224
2ca993e8
A
225 logln("-> do TestArabicShapingThreads <- Firing off threads.. ");
226 for(i=0; i < UPRV_LENGTHOF(threads); i++) {
227 if (threads[i].start() != 0) {
228 errln("Error starting thread %d", i);
51004dcb 229 }
51004dcb
A
230 }
231
2ca993e8
A
232 for(i=0; i < UPRV_LENGTHOF(threads); i++) {
233 threads[i].join();
51004dcb 234 }
2ca993e8 235 logln("->TestArabicShapingThreads <- Got all threads! cya");
51004dcb
A
236}
237
b331163b 238
73c04bcf
A
239//-----------------------------------------------------------------------
240//
241// TestMutex - a simple (non-stress) test to verify that ICU mutexes
2ca993e8 242// and condition variables are functioning. Does not test the use of
73c04bcf
A
243// mutexes within ICU services, but rather that the
244// platform's mutex support is at least superficially there.
245//
246//----------------------------------------------------------------------
2ca993e8
A
247static UMutex gTestMutexA = U_MUTEX_INITIALIZER;
248static UConditionVar gThreadsCountChanged = U_CONDITION_INITIALIZER;
73c04bcf 249
b331163b 250static int gThreadsStarted = 0;
73c04bcf
A
251static int gThreadsInMiddle = 0;
252static int gThreadsDone = 0;
253
2ca993e8 254static const int TESTMUTEX_THREAD_COUNT = 40;
b75a7d8f 255
73c04bcf 256class TestMutexThread : public SimpleThread
b75a7d8f
A
257{
258public:
2ca993e8 259 virtual void run() {
73c04bcf 260 // This is the code that each of the spawned threads runs.
2ca993e8
A
261 // All threads move together throught the started - middle - done sequence together,
262 // waiting for all other threads to reach each point before advancing.
73c04bcf 263 umtx_lock(&gTestMutexA);
2ca993e8
A
264 gThreadsStarted += 1;
265 umtx_condBroadcast(&gThreadsCountChanged);
266 while (gThreadsStarted < TESTMUTEX_THREAD_COUNT) {
267 if (gThreadsInMiddle != 0) {
268 IntlTest::gTest->errln(
269 "%s:%d gThreadsInMiddle = %d. Expected 0.", __FILE__, __LINE__, gThreadsInMiddle);
270 return;
271 }
272 umtx_condWait(&gThreadsCountChanged, &gTestMutexA);
273 }
274
275 gThreadsInMiddle += 1;
276 umtx_condBroadcast(&gThreadsCountChanged);
277 while (gThreadsInMiddle < TESTMUTEX_THREAD_COUNT) {
278 if (gThreadsDone != 0) {
279 IntlTest::gTest->errln(
280 "%s:%d gThreadsDone = %d. Expected 0.", __FILE__, __LINE__, gThreadsDone);
281 return;
282 }
283 umtx_condWait(&gThreadsCountChanged, &gTestMutexA);
284 }
285
286 gThreadsDone += 1;
287 umtx_condBroadcast(&gThreadsCountChanged);
288 while (gThreadsDone < TESTMUTEX_THREAD_COUNT) {
289 umtx_condWait(&gThreadsCountChanged, &gTestMutexA);
290 }
73c04bcf 291 umtx_unlock(&gTestMutexA);
b75a7d8f 292 }
b75a7d8f
A
293};
294
295void MultithreadTest::TestMutex()
296{
73c04bcf
A
297 gThreadsStarted = 0;
298 gThreadsInMiddle = 0;
299 gThreadsDone = 0;
2ca993e8
A
300 int32_t i = 0;
301 TestMutexThread threads[TESTMUTEX_THREAD_COUNT];
73c04bcf 302 umtx_lock(&gTestMutexA);
73c04bcf 303 for (i=0; i<TESTMUTEX_THREAD_COUNT; i++) {
2ca993e8
A
304 if (threads[i].start() != 0) {
305 errln("%s:%d Error starting thread %d", __FILE__, __LINE__, i);
b75a7d8f
A
306 return;
307 }
73c04bcf 308 }
73c04bcf 309
2ca993e8
A
310 // Because we are holding gTestMutexA, all of the threads should be blocked
311 // at the start of their run() function.
312 if (gThreadsStarted != 0) {
313 errln("%s:%d gThreadsStarted=%d. Expected 0.", __FILE__, __LINE__, gThreadsStarted);
314 return;
73c04bcf 315 }
73c04bcf 316
2ca993e8
A
317 while (gThreadsInMiddle < TESTMUTEX_THREAD_COUNT) {
318 if (gThreadsDone != 0) {
319 errln("%s:%d gThreadsDone=%d. Expected 0.", __FILE__, __LINE__, gThreadsStarted);
73c04bcf
A
320 return;
321 }
2ca993e8 322 umtx_condWait(&gThreadsCountChanged, &gTestMutexA);
b75a7d8f 323 }
b75a7d8f 324
2ca993e8
A
325 while (gThreadsDone < TESTMUTEX_THREAD_COUNT) {
326 umtx_condWait(&gThreadsCountChanged, &gTestMutexA);
327 }
328 umtx_unlock(&gTestMutexA);
73c04bcf
A
329
330 for (i=0; i<TESTMUTEX_THREAD_COUNT; i++) {
2ca993e8 331 threads[i].join();
73c04bcf 332 }
73c04bcf 333}
b75a7d8f
A
334
335
374ca955 336//-------------------------------------------------------------------------------------------
b75a7d8f 337//
2ca993e8 338// TestMultithreadedIntl. Test ICU Formatting in a multi-threaded environment
b75a7d8f 339//
374ca955 340//-------------------------------------------------------------------------------------------
b75a7d8f
A
341
342
b75a7d8f
A
343// * Show exactly where the string's differences lie.
344UnicodeString showDifference(const UnicodeString& expected, const UnicodeString& result)
345{
346 UnicodeString res;
347 res = expected + "<Expected\n";
348 if(expected.length() != result.length())
349 res += " [ Different lengths ] \n";
350 else
351 {
352 for(int32_t i=0;i<expected.length();i++)
353 {
354 if(expected[i] == result[i])
355 {
356 res += " ";
357 }
358 else
359 {
360 res += "|";
361 }
362 }
363 res += "<Differences";
364 res += "\n";
365 }
366 res += result + "<Result\n";
367
368 return res;
369}
370
371
374ca955
A
372//-------------------------------------------------------------------------------------------
373//
374// FormatThreadTest - a thread that tests performing a number of numberformats.
375//
376//-------------------------------------------------------------------------------------------
b75a7d8f 377
57a6839d 378const int kFormatThreadIterations = 100; // # of iterations per thread
b331163b 379const int kFormatThreadThreads = 10; // # of threads to spawn
b75a7d8f
A
380
381#if !UCONFIG_NO_FORMATTING
382
b75a7d8f
A
383
384
385struct FormatThreadTestData
386{
387 double number;
388 UnicodeString string;
389 FormatThreadTestData(double a, const UnicodeString& b) : number(a),string(b) {}
390} ;
391
392
b75a7d8f
A
393// "Someone from {2} is receiving a #{0} error - {1}. Their telephone call is costing {3 number,currency}."
394
57a6839d 395static void formatErrorMessage(UErrorCode &realStatus, const UnicodeString& pattern, const Locale& theLocale,
0f5d89e8
A
396 UErrorCode inStatus0, // statusString 1
397 const Locale &inCountry2, double currency3, // these numbers are the message arguments.
b75a7d8f
A
398 UnicodeString &result)
399{
400 if(U_FAILURE(realStatus))
401 return; // you messed up
402
73c04bcf 403 UnicodeString errString1(u_errorName(inStatus0));
b75a7d8f
A
404
405 UnicodeString countryName2;
406 inCountry2.getDisplayCountry(theLocale,countryName2);
407
408 Formattable myArgs[] = {
409 Formattable((int32_t)inStatus0), // inStatus0 {0}
410 Formattable(errString1), // statusString1 {1}
411 Formattable(countryName2), // inCountry2 {2}
412 Formattable(currency3)// currency3 {3,number,currency}
413 };
414
415 MessageFormat *fmt = new MessageFormat("MessageFormat's API is broken!!!!!!!!!!!",realStatus);
416 fmt->setLocale(theLocale);
417 fmt->applyPattern(pattern, realStatus);
b331163b 418
b75a7d8f
A
419 if (U_FAILURE(realStatus)) {
420 delete fmt;
421 return;
422 }
423
b331163b 424 FieldPosition ignore = 0;
b75a7d8f
A
425 fmt->format(myArgs,4,result,ignore,realStatus);
426
427 delete fmt;
73c04bcf 428}
b75a7d8f 429
57a6839d 430/**
b331163b
A
431 * Shared formatters & data used by instances of ThreadSafeFormat.
432 * Exactly one instance of this class is created, and it is then shared concurrently
433 * by the multiple instances of ThreadSafeFormat.
434 */
435class ThreadSafeFormatSharedData {
436 public:
437 ThreadSafeFormatSharedData(UErrorCode &status);
438 ~ThreadSafeFormatSharedData();
439 LocalPointer<NumberFormat> fFormat;
440 Formattable fYDDThing;
441 Formattable fBBDThing;
442 UnicodeString fYDDStr;
443 UnicodeString fBBDStr;
444};
445
446const ThreadSafeFormatSharedData *gSharedData = NULL;
447
448ThreadSafeFormatSharedData::ThreadSafeFormatSharedData(UErrorCode &status) {
449 fFormat.adoptInstead(NumberFormat::createCurrencyInstance(Locale::getUS(), status));
450 static const UChar kYDD[] = { 0x59, 0x44, 0x44, 0x00 };
451 static const UChar kBBD[] = { 0x42, 0x42, 0x44, 0x00 };
452 fYDDThing.adoptObject(new CurrencyAmount(123.456, kYDD, status));
453 fBBDThing.adoptObject(new CurrencyAmount(987.654, kBBD, status));
454 if (U_FAILURE(status)) {
455 return;
456 }
457 fFormat->format(fYDDThing, fYDDStr, NULL, status);
458 fFormat->format(fBBDThing, fBBDStr, NULL, status);
459 gSharedData = this;
460}
461
462ThreadSafeFormatSharedData::~ThreadSafeFormatSharedData() {
463 gSharedData = NULL;
464}
465
466/**
467 * Class for thread-safe testing of format.
468 * Instances of this class appear as members of class FormatThreadTest.
469 * Multiple instances of FormatThreadTest coexist.
470 * ThreadSafeFormat::doStuff() is called concurrently to test the thread safety of
471 * various shared format operations.
57a6839d
A
472 */
473class ThreadSafeFormat {
474public:
475 /* give a unique offset to each thread */
b331163b
A
476 ThreadSafeFormat(UErrorCode &status);
477 UBool doStuff(int32_t offset, UnicodeString &appendErr, UErrorCode &status) const;
57a6839d 478private:
b331163b 479 LocalPointer<NumberFormat> fFormat; // formatter - en_US constructed currency
57a6839d
A
480};
481
57a6839d 482
b331163b
A
483ThreadSafeFormat::ThreadSafeFormat(UErrorCode &status) {
484 fFormat.adoptInstead(NumberFormat::createCurrencyInstance(Locale::getUS(), status));
57a6839d
A
485}
486
b331163b 487static const UChar kUSD[] = { 0x55, 0x53, 0x44, 0x00 };
57a6839d 488
b331163b 489UBool ThreadSafeFormat::doStuff(int32_t offset, UnicodeString &appendErr, UErrorCode &status) const {
57a6839d 490 UBool okay = TRUE;
57a6839d
A
491
492 if(u_strcmp(fFormat->getCurrency(), kUSD)) {
493 appendErr.append("fFormat currency != ")
494 .append(kUSD)
495 .append(", =")
496 .append(fFormat->getCurrency())
497 .append("! ");
498 okay = FALSE;
499 }
500
b331163b 501 if(u_strcmp(gSharedData->fFormat->getCurrency(), kUSD)) {
57a6839d
A
502 appendErr.append("gFormat currency != ")
503 .append(kUSD)
504 .append(", =")
b331163b 505 .append(gSharedData->fFormat->getCurrency())
57a6839d
A
506 .append("! ");
507 okay = FALSE;
508 }
509 UnicodeString str;
510 const UnicodeString *o=NULL;
511 Formattable f;
512 const NumberFormat *nf = NULL; // only operate on it as const.
513 switch(offset%4) {
b331163b
A
514 case 0: f = gSharedData->fYDDThing; o = &gSharedData->fYDDStr; nf = gSharedData->fFormat.getAlias(); break;
515 case 1: f = gSharedData->fBBDThing; o = &gSharedData->fBBDStr; nf = gSharedData->fFormat.getAlias(); break;
516 case 2: f = gSharedData->fYDDThing; o = &gSharedData->fYDDStr; nf = fFormat.getAlias(); break;
517 case 3: f = gSharedData->fBBDThing; o = &gSharedData->fBBDStr; nf = fFormat.getAlias(); break;
57a6839d
A
518 }
519 nf->format(f, str, NULL, status);
520
521 if(*o != str) {
522 appendErr.append(showDifference(*o, str));
523 okay = FALSE;
524 }
525 return okay;
526}
374ca955
A
527
528UBool U_CALLCONV isAcceptable(void *, const char *, const char *, const UDataInfo *) {
529 return TRUE;
530}
531
532//static UMTX debugMutex = NULL;
533//static UMTX gDebugMutex;
534
b75a7d8f 535
2ca993e8 536class FormatThreadTest : public SimpleThread
b75a7d8f
A
537{
538public:
374ca955
A
539 int fNum;
540 int fTraceInfo;
541
b331163b 542 LocalPointer<ThreadSafeFormat> fTSF;
57a6839d 543
b75a7d8f 544 FormatThreadTest() // constructor is NOT multithread safe.
2ca993e8 545 : SimpleThread(),
374ca955
A
546 fNum(0),
547 fTraceInfo(0),
b331163b 548 fTSF(NULL),
b75a7d8f
A
549 fOffset(0)
550 // the locale to use
551 {
b331163b
A
552 UErrorCode status = U_ZERO_ERROR; // TODO: rearrange code to allow checking of status.
553 fTSF.adoptInstead(new ThreadSafeFormat(status));
b75a7d8f
A
554 static int32_t fgOffset = 0;
555 fgOffset += 3;
556 fOffset = fgOffset;
557 }
558
559
560 virtual void run()
561 {
374ca955 562 fTraceInfo = 1;
729e4ab9 563 LocalPointer<NumberFormat> percentFormatter;
374ca955
A
564 UErrorCode status = U_ZERO_ERROR;
565
566#if 0
b331163b 567 // debugging code,
374ca955
A
568 for (int i=0; i<4000; i++) {
569 status = U_ZERO_ERROR;
570 UDataMemory *data1 = udata_openChoice(0, "res", "en_US", isAcceptable, 0, &status);
571 UDataMemory *data2 = udata_openChoice(0, "res", "fr", isAcceptable, 0, &status);
572 udata_close(data1);
573 udata_close(data2);
574 if (U_FAILURE(status)) {
575 error("udata_openChoice failed.\n");
576 break;
577 }
578 }
579 return;
580#endif
581
46f4442e 582#if 0
b331163b 583 // debugging code,
374ca955
A
584 int m;
585 for (m=0; m<4000; m++) {
586 status = U_ZERO_ERROR;
587 UResourceBundle *res = NULL;
588 const char *localeName = NULL;
589
590 Locale loc = Locale::getEnglish();
591
592 localeName = loc.getName();
593 // localeName = "en";
594
595 // ResourceBundle bund = ResourceBundle(0, loc, status);
596 //umtx_lock(&gDebugMutex);
597 res = ures_open(NULL, localeName, &status);
598 //umtx_unlock(&gDebugMutex);
599
600 //umtx_lock(&gDebugMutex);
601 ures_close(res);
602 //umtx_unlock(&gDebugMutex);
603
604 if (U_FAILURE(status)) {
605 error("Resource bundle construction failed.\n");
606 break;
607 }
608 }
609 return;
610#endif
611
b75a7d8f 612 // Keep this data here to avoid static initialization.
b331163b 613 FormatThreadTestData kNumberFormatTestData[] =
b75a7d8f 614 {
0f5d89e8
A
615 FormatThreadTestData((double)5.0, UnicodeString(u"5")),
616 FormatThreadTestData( 6.0, UnicodeString(u"6")),
617 FormatThreadTestData( 20.0, UnicodeString(u"20")),
618 FormatThreadTestData( 8.0, UnicodeString(u"8")),
619 FormatThreadTestData( 8.3, UnicodeString(u"8.3")),
620 FormatThreadTestData( 12345, UnicodeString(u"12,345")),
621 FormatThreadTestData( 81890.23, UnicodeString(u"81,890.23")),
b75a7d8f 622 };
b331163b
A
623 int32_t kNumberFormatTestDataLength = UPRV_LENGTHOF(kNumberFormatTestData);
624
b75a7d8f 625 // Keep this data here to avoid static initialization.
b331163b 626 FormatThreadTestData kPercentFormatTestData[] =
b75a7d8f 627 {
46f4442e
A
628 FormatThreadTestData((double)5.0, CharsToUnicodeString("500\\u00a0%")),
629 FormatThreadTestData( 1.0, CharsToUnicodeString("100\\u00a0%")),
630 FormatThreadTestData( 0.26, CharsToUnicodeString("26\\u00a0%")),
b331163b 631 FormatThreadTestData(
46f4442e 632 16384.99, CharsToUnicodeString("1\\u00a0638\\u00a0499\\u00a0%")), // U+00a0 = NBSP
b331163b 633 FormatThreadTestData(
46f4442e 634 81890.23, CharsToUnicodeString("8\\u00a0189\\u00a0023\\u00a0%")),
b75a7d8f 635 };
b331163b 636 int32_t kPercentFormatTestDataLength = UPRV_LENGTHOF(kPercentFormatTestData);
b75a7d8f 637 int32_t iteration;
b331163b 638
374ca955 639 status = U_ZERO_ERROR;
729e4ab9 640 LocalPointer<NumberFormat> formatter(NumberFormat::createInstance(Locale::getEnglish(),status));
374ca955 641 if(U_FAILURE(status)) {
2ca993e8
A
642 IntlTest::gTest->dataerrln("%s:%d Error %s on NumberFormat::createInstance().",
643 __FILE__, __LINE__, u_errorName(status));
374ca955 644 goto cleanupAndReturn;
b75a7d8f 645 }
b331163b 646
729e4ab9 647 percentFormatter.adoptInstead(NumberFormat::createPercentInstance(Locale::getFrench(),status));
374ca955 648 if(U_FAILURE(status)) {
2ca993e8
A
649 IntlTest::gTest->errln("%s:%d Error %s on NumberFormat::createPercentInstance().",
650 __FILE__, __LINE__, u_errorName(status));
374ca955 651 goto cleanupAndReturn;
b75a7d8f 652 }
b331163b 653
2ca993e8 654 for(iteration = 0;!IntlTest::gTest->getErrors() && iteration<kFormatThreadIterations;iteration++)
b75a7d8f 655 {
b331163b 656
b75a7d8f 657 int32_t whichLine = (iteration + fOffset)%kNumberFormatTestDataLength;
b331163b 658
b75a7d8f 659 UnicodeString output;
b331163b 660
b75a7d8f 661 formatter->format(kNumberFormatTestData[whichLine].number, output);
b331163b 662
374ca955 663 if(0 != output.compare(kNumberFormatTestData[whichLine].string)) {
2ca993e8 664 IntlTest::gTest->errln("format().. expected " + kNumberFormatTestData[whichLine].string
374ca955
A
665 + " got " + output);
666 goto cleanupAndReturn;
b75a7d8f 667 }
b331163b 668
b75a7d8f
A
669 // Now check percent.
670 output.remove();
671 whichLine = (iteration + fOffset)%kPercentFormatTestDataLength;
b331163b 672
b75a7d8f 673 percentFormatter->format(kPercentFormatTestData[whichLine].number, output);
b75a7d8f
A
674 if(0 != output.compare(kPercentFormatTestData[whichLine].string))
675 {
2ca993e8 676 IntlTest::gTest->errln("percent format().. \n" +
374ca955
A
677 showDifference(kPercentFormatTestData[whichLine].string,output));
678 goto cleanupAndReturn;
b75a7d8f 679 }
b331163b
A
680
681 // Test message error
374ca955 682 const int kNumberOfMessageTests = 3;
b75a7d8f
A
683 UErrorCode statusToCheck;
684 UnicodeString patternToCheck;
685 Locale messageLocale;
686 Locale countryToCheck;
687 double currencyToCheck;
b331163b 688
b75a7d8f 689 UnicodeString expected;
b331163b 690
b75a7d8f
A
691 // load the cases.
692 switch((iteration+fOffset) % kNumberOfMessageTests)
693 {
694 default:
695 case 0:
696 statusToCheck= U_FILE_ACCESS_ERROR;
374ca955
A
697 patternToCheck= "0:Someone from {2} is receiving a #{0}"
698 " error - {1}. Their telephone call is costing "
699 "{3,number,currency}."; // number,currency
b75a7d8f
A
700 messageLocale= Locale("en","US");
701 countryToCheck= Locale("","HR");
702 currencyToCheck= 8192.77;
374ca955
A
703 expected= "0:Someone from Croatia is receiving a #4 error - "
704 "U_FILE_ACCESS_ERROR. Their telephone call is costing $8,192.77.";
b75a7d8f
A
705 break;
706 case 1:
707 statusToCheck= U_INDEX_OUTOFBOUNDS_ERROR;
b331163b
A
708 patternToCheck= "1:A customer in {2} is receiving a #{0} error - {1}. "
709 "Their telephone call is costing {3,number,currency}."; // number,currency
374ca955 710 messageLocale= Locale("de","DE@currency=DEM");
b75a7d8f
A
711 countryToCheck= Locale("","BF");
712 currencyToCheck= 2.32;
46f4442e 713 expected= CharsToUnicodeString(
b331163b
A
714 "1:A customer in Burkina Faso is receiving a #8 error - U_INDEX_OUTOFBOUNDS_ERROR. "
715 "Their telephone call is costing 2,32\\u00A0DM.");
374ca955 716 break;
b75a7d8f
A
717 case 2:
718 statusToCheck= U_MEMORY_ALLOCATION_ERROR;
374ca955
A
719 patternToCheck= "2:user in {2} is receiving a #{0} error - {1}. "
720 "They insist they just spent {3,number,currency} "
721 "on memory."; // number,currency
722 messageLocale= Locale("de","AT@currency=ATS"); // Austrian German
b75a7d8f
A
723 countryToCheck= Locale("","US"); // hmm
724 currencyToCheck= 40193.12;
374ca955
A
725 expected= CharsToUnicodeString(
726 "2:user in Vereinigte Staaten is receiving a #7 error"
727 " - U_MEMORY_ALLOCATION_ERROR. They insist they just spent"
46f4442e 728 " \\u00f6S\\u00A040.193,12 on memory.");
b75a7d8f
A
729 break;
730 }
b331163b 731
b75a7d8f
A
732 UnicodeString result;
733 UErrorCode status = U_ZERO_ERROR;
374ca955
A
734 formatErrorMessage(status,patternToCheck,messageLocale,statusToCheck,
735 countryToCheck,currencyToCheck,result);
b75a7d8f
A
736 if(U_FAILURE(status))
737 {
73c04bcf 738 UnicodeString tmp(u_errorName(status));
2ca993e8 739 IntlTest::gTest->errln("Failure on message format, pattern=" + patternToCheck +
374ca955
A
740 ", error = " + tmp);
741 goto cleanupAndReturn;
b75a7d8f 742 }
b331163b 743
b75a7d8f
A
744 if(result != expected)
745 {
2ca993e8 746 IntlTest::gTest->errln("PatternFormat: \n" + showDifference(expected,result));
374ca955 747 goto cleanupAndReturn;
b75a7d8f 748 }
57a6839d
A
749 // test the Thread Safe Format
750 UnicodeString appendErr;
b331163b 751 if(!fTSF->doStuff(fNum, appendErr, status)) {
2ca993e8 752 IntlTest::gTest->errln(appendErr);
57a6839d
A
753 goto cleanupAndReturn;
754 }
374ca955 755 } /* end of for loop */
57a6839d
A
756
757
758
374ca955 759cleanupAndReturn:
374ca955 760 fTraceInfo = 2;
b75a7d8f 761 }
b331163b 762
b75a7d8f
A
763private:
764 int32_t fOffset; // where we are testing from.
765};
766
767// ** The actual test function.
768
769void MultithreadTest::TestThreadedIntl()
770{
374ca955 771 UnicodeString theErr;
374ca955 772
57a6839d
A
773 UErrorCode threadSafeErr = U_ZERO_ERROR;
774
b331163b 775 ThreadSafeFormatSharedData sharedData(threadSafeErr);
57a6839d
A
776 assertSuccess("initializing ThreadSafeFormat", threadSafeErr, TRUE);
777
374ca955
A
778 //
779 // Create and start the test threads
780 //
781 logln("Spawning: %d threads * %d iterations each.",
782 kFormatThreadThreads, kFormatThreadIterations);
2ca993e8
A
783 FormatThreadTest tests[kFormatThreadThreads];
784 int32_t j;
785 for(j = 0; j < UPRV_LENGTHOF(tests); j++) {
374ca955 786 tests[j].fNum = j;
b75a7d8f
A
787 int32_t threadStatus = tests[j].start();
788 if (threadStatus != 0) {
2ca993e8
A
789 errln("%s:%d System Error %d starting thread number %d.",
790 __FILE__, __LINE__, threadStatus, j);
729e4ab9 791 return;
b75a7d8f 792 }
2ca993e8 793 }
b75a7d8f 794
2ca993e8
A
795
796 for (j=0; j<UPRV_LENGTHOF(tests); j++) {
797 tests[j].join();
798 logln("Thread # %d is complete..", j);
799 }
374ca955
A
800}
801#endif /* #if !UCONFIG_NO_FORMATTING */
b75a7d8f 802
b75a7d8f 803
b75a7d8f 804
b75a7d8f 805
b75a7d8f 806
374ca955
A
807//-------------------------------------------------------------------------------------------
808//
809// Collation threading test
810//
811//-------------------------------------------------------------------------------------------
b75a7d8f
A
812#if !UCONFIG_NO_COLLATION
813
814#define kCollatorThreadThreads 10 // # of threads to spawn
73c04bcf 815#define kCollatorThreadPatience kCollatorThreadThreads*30
b75a7d8f
A
816
817struct Line {
374ca955
A
818 UChar buff[25];
819 int32_t buflen;
b75a7d8f
A
820} ;
821
51004dcb
A
822static UBool
823skipLineBecauseOfBug(const UChar *s, int32_t length) {
824 // TODO: Fix ICU ticket #8052
825 if(length >= 3 &&
826 (s[0] == 0xfb2 || s[0] == 0xfb3) &&
827 s[1] == 0x334 &&
828 (s[2] == 0xf73 || s[2] == 0xf75 || s[2] == 0xf81)) {
829 return TRUE;
830 }
831 return FALSE;
832}
833
834static UCollationResult
835normalizeResult(int32_t result) {
836 return result<0 ? UCOL_LESS : result==0 ? UCOL_EQUAL : UCOL_GREATER;
837}
838
2ca993e8 839class CollatorThreadTest : public SimpleThread
b75a7d8f 840{
b331163b 841private:
51004dcb 842 const Collator *coll;
374ca955
A
843 const Line *lines;
844 int32_t noLines;
51004dcb 845 UBool isAtLeastUCA62;
b75a7d8f 846public:
2ca993e8 847 CollatorThreadTest() : SimpleThread(),
374ca955
A
848 coll(NULL),
849 lines(NULL),
51004dcb
A
850 noLines(0),
851 isAtLeastUCA62(TRUE)
374ca955
A
852 {
853 };
51004dcb 854 void setCollator(Collator *c, Line *l, int32_t nl, UBool atLeastUCA62)
374ca955
A
855 {
856 coll = c;
857 lines = l;
858 noLines = nl;
51004dcb 859 isAtLeastUCA62 = atLeastUCA62;
374ca955
A
860 }
861 virtual void run() {
374ca955
A
862 uint8_t sk1[1024], sk2[1024];
863 uint8_t *oldSk = NULL, *newSk = sk1;
51004dcb
A
864 int32_t oldLen = 0;
865 int32_t prev = 0;
374ca955 866 int32_t i = 0;
729e4ab9 867
374ca955 868 for(i = 0; i < noLines; i++) {
51004dcb
A
869 if(lines[i].buflen == 0) { continue; }
870
871 if(skipLineBecauseOfBug(lines[i].buff, lines[i].buflen)) { continue; }
729e4ab9 872
51004dcb 873 int32_t resLen = coll->getSortKey(lines[i].buff, lines[i].buflen, newSk, 1024);
729e4ab9 874
374ca955 875 if(oldSk != NULL) {
51004dcb
A
876 int32_t skres = strcmp((char *)oldSk, (char *)newSk);
877 int32_t cmpres = coll->compare(lines[prev].buff, lines[prev].buflen, lines[i].buff, lines[i].buflen);
878 int32_t cmpres2 = coll->compare(lines[i].buff, lines[i].buflen, lines[prev].buff, lines[prev].buflen);
729e4ab9 879
374ca955 880 if(cmpres != -cmpres2) {
2ca993e8 881 IntlTest::gTest->errln(UnicodeString("Compare result not symmetrical on line ") + (i + 1));
374ca955
A
882 break;
883 }
729e4ab9 884
51004dcb 885 if(cmpres != normalizeResult(skres)) {
2ca993e8 886 IntlTest::gTest->errln(UnicodeString("Difference between coll->compare and sortkey compare on line ") + (i + 1));
374ca955
A
887 break;
888 }
729e4ab9 889
51004dcb
A
890 int32_t res = cmpres;
891 if(res == 0 && !isAtLeastUCA62) {
892 // Up to UCA 6.1, the collation test files use a custom tie-breaker,
893 // comparing the raw input strings.
894 res = u_strcmpCodePointOrder(lines[prev].buff, lines[i].buff);
895 // Starting with UCA 6.2, the collation test files use the standard UCA tie-breaker,
896 // comparing the NFD versions of the input strings,
897 // which we do via setting strength=identical.
898 }
374ca955 899 if(res > 0) {
2ca993e8 900 IntlTest::gTest->errln(UnicodeString("Line is not greater or equal than previous line, for line ") + (i + 1));
374ca955 901 break;
374ca955
A
902 }
903 }
729e4ab9 904
374ca955
A
905 oldSk = newSk;
906 oldLen = resLen;
57a6839d 907 (void)oldLen; // Suppress set but not used warning.
51004dcb 908 prev = i;
729e4ab9 909
374ca955 910 newSk = (newSk == sk1)?sk2:sk1;
b75a7d8f 911 }
b75a7d8f 912 }
b75a7d8f
A
913};
914
915void MultithreadTest::TestCollators()
916{
917
374ca955
A
918 UErrorCode status = U_ZERO_ERROR;
919 FILE *testFile = NULL;
920 char testDataPath[1024];
921 strcpy(testDataPath, IntlTest::getSourceTestData(status));
922 if (U_FAILURE(status)) {
923 errln("ERROR: could not open test data %s", u_errorName(status));
924 return;
925 }
926 strcat(testDataPath, "CollationTest_");
b75a7d8f 927
374ca955 928 const char* type = "NON_IGNORABLE";
b75a7d8f 929
374ca955
A
930 const char *ext = ".txt";
931 if(testFile) {
932 fclose(testFile);
933 }
934 char buffer[1024];
935 strcpy(buffer, testDataPath);
936 strcat(buffer, type);
937 size_t bufLen = strlen(buffer);
b75a7d8f 938
374ca955
A
939 // we try to open 3 files:
940 // path/CollationTest_type.txt
941 // path/CollationTest_type_SHORT.txt
942 // path/CollationTest_type_STUB.txt
943 // we are going to test with the first one that we manage to open.
b75a7d8f 944
374ca955 945 strcpy(buffer+bufLen, ext);
b75a7d8f 946
b75a7d8f
A
947 testFile = fopen(buffer, "rb");
948
949 if(testFile == 0) {
374ca955
A
950 strcpy(buffer+bufLen, "_SHORT");
951 strcat(buffer, ext);
952 testFile = fopen(buffer, "rb");
953
954 if(testFile == 0) {
955 strcpy(buffer+bufLen, "_STUB");
956 strcat(buffer, ext);
957 testFile = fopen(buffer, "rb");
958
959 if (testFile == 0) {
960 *(buffer+bufLen) = 0;
729e4ab9 961 dataerrln("could not open any of the conformance test files, tried opening base %s", buffer);
b331163b 962 return;
374ca955
A
963 } else {
964 infoln(
965 "INFO: Working with the stub file.\n"
966 "If you need the full conformance test, please\n"
967 "download the appropriate data files from:\n"
46f4442e 968 "http://source.icu-project.org/repos/icu/tools/trunk/unicodetools/com/ibm/text/data/");
374ca955
A
969 }
970 }
b75a7d8f 971 }
b75a7d8f 972
51004dcb
A
973 LocalArray<Line> lines(new Line[200000]);
974 memset(lines.getAlias(), 0, sizeof(Line)*200000);
374ca955 975 int32_t lineNum = 0;
b75a7d8f 976
374ca955 977 UChar bufferU[1024];
374ca955 978 uint32_t first = 0;
b75a7d8f 979
374ca955 980 while (fgets(buffer, 1024, testFile) != NULL) {
51004dcb
A
981 if(*buffer == 0 || buffer[0] == '#') {
982 // Store empty and comment lines so that errors are reported
983 // for the real test file lines.
984 lines[lineNum].buflen = 0;
985 lines[lineNum].buff[0] = 0;
986 } else {
987 int32_t buflen = u_parseString(buffer, bufferU, 1024, &first, &status);
988 lines[lineNum].buflen = buflen;
989 u_memcpy(lines[lineNum].buff, bufferU, buflen);
990 lines[lineNum].buff[buflen] = 0;
374ca955 991 }
374ca955 992 lineNum++;
b75a7d8f 993 }
374ca955 994 fclose(testFile);
73c04bcf 995 if(U_FAILURE(status)) {
729e4ab9 996 dataerrln("Couldn't read the test file!");
73c04bcf
A
997 return;
998 }
374ca955 999
51004dcb
A
1000 UVersionInfo uniVersion;
1001 static const UVersionInfo v62 = { 6, 2, 0, 0 };
1002 u_getUnicodeVersion(uniVersion);
1003 UBool isAtLeastUCA62 = uprv_memcmp(uniVersion, v62, 4) >= 0;
1004
1005 LocalPointer<Collator> coll(Collator::createInstance(Locale::getRoot(), status));
374ca955 1006 if(U_FAILURE(status)) {
729e4ab9 1007 errcheckln(status, "Couldn't open UCA collator");
374ca955
A
1008 return;
1009 }
51004dcb
A
1010 coll->setAttribute(UCOL_NORMALIZATION_MODE, UCOL_ON, status);
1011 coll->setAttribute(UCOL_CASE_FIRST, UCOL_OFF, status);
1012 coll->setAttribute(UCOL_CASE_LEVEL, UCOL_OFF, status);
1013 coll->setAttribute(UCOL_STRENGTH, isAtLeastUCA62 ? UCOL_IDENTICAL : UCOL_TERTIARY, status);
1014 coll->setAttribute(UCOL_ALTERNATE_HANDLING, UCOL_NON_IGNORABLE, status);
374ca955 1015
374ca955 1016 int32_t spawnResult = 0;
729e4ab9 1017 LocalArray<CollatorThreadTest> tests(new CollatorThreadTest[kCollatorThreadThreads]);
374ca955 1018
b75a7d8f
A
1019 logln(UnicodeString("Spawning: ") + kCollatorThreadThreads + " threads * " + kFormatThreadIterations + " iterations each.");
1020 int32_t j = 0;
1021 for(j = 0; j < kCollatorThreadThreads; j++) {
374ca955 1022 //logln("Setting collator %i", j);
51004dcb 1023 tests[j].setCollator(coll.getAlias(), lines.getAlias(), lineNum, isAtLeastUCA62);
b75a7d8f
A
1024 }
1025 for(j = 0; j < kCollatorThreadThreads; j++) {
374ca955
A
1026 log("%i ", j);
1027 spawnResult = tests[j].start();
1028 if(spawnResult != 0) {
2ca993e8
A
1029 errln("%s:%d THREAD INFO: thread %d failed to start with status %d", __FILE__, __LINE__, j, spawnResult);
1030 return;
374ca955 1031 }
b75a7d8f
A
1032 }
1033 logln("Spawned all");
b75a7d8f 1034
2ca993e8
A
1035 for(int32_t i=0;i<kCollatorThreadThreads;i++) {
1036 tests[i].join();
1037 //logln(UnicodeString("Test #") + i + " is complete.. ");
b75a7d8f 1038 }
b75a7d8f
A
1039}
1040
1041#endif /* #if !UCONFIG_NO_COLLATION */
1042
374ca955
A
1043
1044
1045
1046//-------------------------------------------------------------------------------------------
1047//
b331163b 1048// StringThreadTest2
374ca955
A
1049//
1050//-------------------------------------------------------------------------------------------
1051
1052const int kStringThreadIterations = 2500;// # of iterations per thread
1053const int kStringThreadThreads = 10; // # of threads to spawn
374ca955
A
1054
1055
2ca993e8 1056class StringThreadTest2 : public SimpleThread
374ca955
A
1057{
1058public:
1059 int fNum;
1060 int fTraceInfo;
2ca993e8 1061 static const UnicodeString *gSharedString;
374ca955 1062
2ca993e8
A
1063 StringThreadTest2() // constructor is NOT multithread safe.
1064 : SimpleThread(),
1065 fTraceInfo(0)
374ca955
A
1066 {
1067 };
1068
1069
1070 virtual void run()
1071 {
1072 fTraceInfo = 1;
1073 int loopCount = 0;
1074
1075 for (loopCount = 0; loopCount < kStringThreadIterations; loopCount++) {
2ca993e8
A
1076 if (*gSharedString != "This is the original test string.") {
1077 IntlTest::gTest->errln("%s:%d Original string is corrupt.", __FILE__, __LINE__);
374ca955
A
1078 break;
1079 }
2ca993e8 1080 UnicodeString s1 = *gSharedString;
374ca955
A
1081 s1 += "cat this";
1082 UnicodeString s2(s1);
2ca993e8 1083 UnicodeString s3 = *gSharedString;
374ca955
A
1084 s2 = s3;
1085 s3.truncate(12);
1086 s2.truncate(0);
1087 }
1088
374ca955
A
1089 fTraceInfo = 2;
1090 }
b331163b 1091
374ca955
A
1092};
1093
2ca993e8
A
1094const UnicodeString *StringThreadTest2::gSharedString = NULL;
1095
374ca955
A
1096// ** The actual test function.
1097
2ca993e8 1098
374ca955
A
1099void MultithreadTest::TestString()
1100{
374ca955 1101 int j;
2ca993e8
A
1102 StringThreadTest2::gSharedString = new UnicodeString("This is the original test string.");
1103 StringThreadTest2 tests[kStringThreadThreads];
b331163b 1104
374ca955
A
1105 logln(UnicodeString("Spawning: ") + kStringThreadThreads + " threads * " + kStringThreadIterations + " iterations each.");
1106 for(j = 0; j < kStringThreadThreads; j++) {
2ca993e8 1107 int32_t threadStatus = tests[j].start();
374ca955 1108 if (threadStatus != 0) {
2ca993e8 1109 errln("%s:%d System Error %d starting thread number %d.", __FILE__, __LINE__, threadStatus, j);
374ca955
A
1110 }
1111 }
1112
2ca993e8
A
1113 // Force a failure, to verify test is functioning and can report errors.
1114 // const_cast<UnicodeString *>(StringThreadTest2::gSharedString)->setCharAt(5, 'x');
b331163b 1115
2ca993e8
A
1116 for(j=0; j<kStringThreadThreads; j++) {
1117 tests[j].join();
1118 logln(UnicodeString("Test #") + j + " is complete.. ");
374ca955
A
1119 }
1120
2ca993e8
A
1121 delete StringThreadTest2::gSharedString;
1122 StringThreadTest2::gSharedString = NULL;
374ca955
A
1123}
1124
57a6839d 1125
2ca993e8 1126//
57a6839d
A
1127// Test for ticket #10673, race in cache code in AnyTransliterator.
1128// It's difficult to make the original unsafe code actually fail, but
b331163b 1129// this test will fairly reliably take the code path for races in
57a6839d 1130// populating the cache.
2ca993e8 1131//
57a6839d
A
1132
1133#if !UCONFIG_NO_TRANSLITERATION
2ca993e8 1134Transliterator *gSharedTranslit = NULL;
57a6839d 1135class TxThread: public SimpleThread {
57a6839d 1136 public:
2ca993e8 1137 TxThread() {};
57a6839d
A
1138 ~TxThread();
1139 void run();
1140};
1141
1142TxThread::~TxThread() {}
1143void TxThread::run() {
1144 UnicodeString greekString("\\u03B4\\u03B9\\u03B1\\u03C6\\u03BF\\u03C1\\u03B5\\u03C4\\u03B9\\u03BA\\u03BF\\u03CD\\u03C2");
1145 greekString = greekString.unescape();
2ca993e8
A
1146 gSharedTranslit->transliterate(greekString);
1147 if (greekString[0] != 0x64) // 'd'. The whole transliterated string is "diaphoretikous" (accented u).
1148 {
1149 IntlTest::gTest->errln("%s:%d Transliteration failed.", __FILE__, __LINE__);
1150 }
57a6839d
A
1151}
1152#endif
b331163b 1153
57a6839d
A
1154
1155void MultithreadTest::TestAnyTranslit() {
1156#if !UCONFIG_NO_TRANSLITERATION
1157 UErrorCode status = U_ZERO_ERROR;
1158 LocalPointer<Transliterator> tx(Transliterator::createInstance("Any-Latin", UTRANS_FORWARD, status));
1159 if (U_FAILURE(status)) {
1160 dataerrln("File %s, Line %d: Error, status = %s", __FILE__, __LINE__, u_errorName(status));
1161 return;
1162 }
2ca993e8
A
1163 gSharedTranslit = tx.getAlias();
1164 TxThread threads[4];
57a6839d 1165 int32_t i;
2ca993e8
A
1166 for (i=0; i<UPRV_LENGTHOF(threads); i++) {
1167 threads[i].start();
57a6839d 1168 }
57a6839d 1169
2ca993e8
A
1170 for (i=0; i<UPRV_LENGTHOF(threads); i++) {
1171 threads[i].join();
57a6839d 1172 }
2ca993e8 1173 gSharedTranslit = NULL;
57a6839d
A
1174#endif // !UCONFIG_NO_TRANSLITERATION
1175}
1176
b331163b 1177
2ca993e8 1178//
b331163b
A
1179// Condition Variables Test
1180// Create a swarm of threads.
1181// Using a mutex and a condition variables each thread
1182// Increments a global count of started threads.
1183// Broadcasts that it has started.
1184// Waits on the condition that all threads have started.
1185// Increments a global count of finished threads.
1186// Waits on the condition that all threads have finished.
1187// Exits.
2ca993e8 1188//
b331163b
A
1189
1190class CondThread: public SimpleThread {
1191 public:
1192 CondThread() :fFinished(false) {};
1193 ~CondThread() {};
1194 void run();
1195 bool fFinished;
1196};
1197
1198static UMutex gCTMutex = U_MUTEX_INITIALIZER;
1199static UConditionVar gCTConditionVar = U_CONDITION_INITIALIZER;
1200int gConditionTestOne = 1; // Value one. Non-const, extern linkage to inhibit
1201 // compiler assuming a known value.
1202int gStartedThreads;
1203int gFinishedThreads;
1204static const int NUMTHREADS = 10;
1205
b331163b
A
1206
1207// Worker thread function.
1208void CondThread::run() {
1209 umtx_lock(&gCTMutex);
1210 gStartedThreads += gConditionTestOne;
1211 umtx_condBroadcast(&gCTConditionVar);
1212
1213 while (gStartedThreads < NUMTHREADS) {
1214 if (gFinishedThreads != 0) {
2ca993e8 1215 IntlTest::gTest->errln("File %s, Line %d: Error, gStartedThreads = %d, gFinishedThreads = %d",
b331163b
A
1216 __FILE__, __LINE__, gStartedThreads, gFinishedThreads);
1217 }
1218 umtx_condWait(&gCTConditionVar, &gCTMutex);
1219 }
1220
1221 gFinishedThreads += gConditionTestOne;
1222 fFinished = true;
1223 umtx_condBroadcast(&gCTConditionVar);
1224
1225 while (gFinishedThreads < NUMTHREADS) {
1226 umtx_condWait(&gCTConditionVar, &gCTMutex);
1227 }
1228 umtx_unlock(&gCTMutex);
1229}
1230
1231void MultithreadTest::TestConditionVariables() {
b331163b
A
1232 gStartedThreads = 0;
1233 gFinishedThreads = 0;
1234 int i;
1235
1236 umtx_lock(&gCTMutex);
1237 CondThread *threads[NUMTHREADS];
1238 for (i=0; i<NUMTHREADS; ++i) {
1239 threads[i] = new CondThread;
1240 threads[i]->start();
1241 }
1242
1243 while (gStartedThreads < NUMTHREADS) {
1244 umtx_condWait(&gCTConditionVar, &gCTMutex);
1245 }
1246
1247 while (gFinishedThreads < NUMTHREADS) {
1248 umtx_condWait(&gCTConditionVar, &gCTMutex);
1249 }
1250
1251 umtx_unlock(&gCTMutex);
1252
1253 for (i=0; i<NUMTHREADS; ++i) {
1254 if (!threads[i]->fFinished) {
1255 errln("File %s, Line %d: Error, threads[%d]->fFinished == false", __FILE__, __LINE__, i);
1256 }
2ca993e8
A
1257 }
1258
1259 for (i=0; i<NUMTHREADS; ++i) {
1260 threads[i]->join();
b331163b
A
1261 delete threads[i];
1262 }
1263}
1264
2ca993e8
A
1265
1266//
1267// Unified Cache Test
1268//
1269
1270// Each thread fetches a pair of objects. There are 8 distinct pairs:
1271// ("en_US", "bs"), ("en_GB", "ca"), ("fr_FR", "ca_AD") etc.
1272// These pairs represent 8 distinct languages
1273
1274// Note that only one value per language gets created in the cache.
1275// In particular each cached value can have multiple keys.
1276static const char *gCacheLocales[] = {
1277 "en_US", "en_GB", "fr_FR", "fr",
1278 "de", "sr_ME", "sr_BA", "sr_CS"};
1279static const char *gCacheLocales2[] = {
1280 "bs", "ca", "ca_AD", "ca_ES",
1281 "en_US", "fi", "ff_CM", "ff_GN"};
1282
1283static int32_t gObjectsCreated = 0; // protected by gCTMutex
b331163b
A
1284static const int32_t CACHE_LOAD = 3;
1285
1286class UCTMultiThreadItem : public SharedObject {
1287 public:
1288 char *value;
1289 UCTMultiThreadItem(const char *x) : value(NULL) {
1290 value = uprv_strdup(x);
1291 }
1292 virtual ~UCTMultiThreadItem() {
1293 uprv_free(value);
1294 }
1295};
1296
1297U_NAMESPACE_BEGIN
1298
1299template<> U_EXPORT
1300const UCTMultiThreadItem *LocaleCacheKey<UCTMultiThreadItem>::createObject(
2ca993e8
A
1301 const void *context, UErrorCode &status) const {
1302 const UnifiedCache *cacheContext = (const UnifiedCache *) context;
1303
1304 if (uprv_strcmp(fLoc.getLanguage(), fLoc.getName()) != 0) {
1305 const UCTMultiThreadItem *result = NULL;
1306 if (cacheContext == NULL) {
1307 UnifiedCache::getByLocale(fLoc.getLanguage(), result, status);
1308 return result;
1309 }
1310 cacheContext->get(LocaleCacheKey<UCTMultiThreadItem>(fLoc.getLanguage()), result, status);
1311 return result;
1312 }
1313
b331163b 1314 umtx_lock(&gCTMutex);
2ca993e8
A
1315 bool firstObject = (gObjectsCreated == 0);
1316 if (firstObject) {
1317 // Force the first object creation that comes through to wait
1318 // until other have completed. Verifies that cache doesn't
1319 // deadlock when a creation is slow.
1320
1321 // Note that gObjectsCreated needs to be incremeneted from 0 to 1
1322 // early, to keep subsequent threads from entering this path.
1323 gObjectsCreated = 1;
1324 while (gObjectsCreated < 3) {
1325 umtx_condWait(&gCTConditionVar, &gCTMutex);
1326 }
b331163b
A
1327 }
1328 umtx_unlock(&gCTMutex);
1329
2ca993e8
A
1330 const UCTMultiThreadItem *result =
1331 new UCTMultiThreadItem(fLoc.getLanguage());
1332 if (result == NULL) {
1333 status = U_MEMORY_ALLOCATION_ERROR;
1334 } else {
1335 result->addRef();
1336 }
0f5d89e8 1337
2ca993e8
A
1338 // Log that we created an object. The first object was already counted,
1339 // don't do it again.
b331163b 1340 umtx_lock(&gCTMutex);
2ca993e8
A
1341 if (!firstObject) {
1342 gObjectsCreated += 1;
1343 }
1344 umtx_condBroadcast(&gCTConditionVar);
b331163b 1345 umtx_unlock(&gCTMutex);
2ca993e8 1346
b331163b
A
1347 return result;
1348}
1349
1350U_NAMESPACE_END
1351
1352class UnifiedCacheThread: public SimpleThread {
1353 public:
2ca993e8
A
1354 UnifiedCacheThread(
1355 const UnifiedCache *cache,
1356 const char *loc,
1357 const char *loc2) : fCache(cache), fLoc(loc), fLoc2(loc2) {};
b331163b
A
1358 ~UnifiedCacheThread() {};
1359 void run();
2ca993e8
A
1360 void exerciseByLocale(const Locale &);
1361 const UnifiedCache *fCache;
1362 Locale fLoc;
1363 Locale fLoc2;
b331163b
A
1364};
1365
2ca993e8 1366void UnifiedCacheThread::exerciseByLocale(const Locale &locale) {
b331163b 1367 UErrorCode status = U_ZERO_ERROR;
2ca993e8
A
1368 const UCTMultiThreadItem *origItem = NULL;
1369 fCache->get(
1370 LocaleCacheKey<UCTMultiThreadItem>(locale), fCache, origItem, status);
1371 U_ASSERT(U_SUCCESS(status));
1372 if (uprv_strcmp(locale.getLanguage(), origItem->value)) {
1373 IntlTest::gTest->errln(
1374 "%s:%d Expected %s, got %s", __FILE__, __LINE__,
1375 locale.getLanguage(),
1376 origItem->value);
1377 }
1378
1379 // Fetch the same item again many times. We should always get the same
1380 // pointer since this client is already holding onto it
1381 for (int32_t i = 0; i < 1000; ++i) {
1382 const UCTMultiThreadItem *item = NULL;
1383 fCache->get(
1384 LocaleCacheKey<UCTMultiThreadItem>(locale), fCache, item, status);
1385 if (item != origItem) {
1386 IntlTest::gTest->errln(
1387 "%s:%d Expected to get the same pointer",
1388 __FILE__,
1389 __LINE__);
1390 }
1391 if (item != NULL) {
1392 item->removeRef();
1393 }
b331163b 1394 }
2ca993e8
A
1395 origItem->removeRef();
1396}
b331163b 1397
2ca993e8
A
1398void UnifiedCacheThread::run() {
1399 // Run the exercise with 2 different locales so that we can exercise
0f5d89e8 1400 // eviction more. If each thread exercises just one locale, then
2ca993e8
A
1401 // eviction can't start until the threads end.
1402 exerciseByLocale(fLoc);
1403 exerciseByLocale(fLoc2);
b331163b
A
1404}
1405
1406void MultithreadTest::TestUnifiedCache() {
2ca993e8
A
1407
1408 // Start with our own local cache so that we have complete control
1409 // and set the eviction policy to evict starting with 2 unused
1410 // values
b331163b 1411 UErrorCode status = U_ZERO_ERROR;
2ca993e8
A
1412 UnifiedCache::getInstance(status);
1413 UnifiedCache cache(status);
1414 cache.setEvictionPolicy(2, 0, status);
1415 U_ASSERT(U_SUCCESS(status));
1416
b331163b
A
1417 gFinishedThreads = 0;
1418 gObjectsCreated = 0;
1419
1420 UnifiedCacheThread *threads[CACHE_LOAD][UPRV_LENGTHOF(gCacheLocales)];
1421 for (int32_t i=0; i<CACHE_LOAD; ++i) {
1422 for (int32_t j=0; j<UPRV_LENGTHOF(gCacheLocales); ++j) {
2ca993e8
A
1423 // Each thread works with a pair of locales.
1424 threads[i][j] = new UnifiedCacheThread(
1425 &cache, gCacheLocales[j], gCacheLocales2[j]);
b331163b
A
1426 threads[i][j]->start();
1427 }
1428 }
2ca993e8
A
1429
1430 for (int32_t i=0; i<CACHE_LOAD; ++i) {
1431 for (int32_t j=0; j<UPRV_LENGTHOF(gCacheLocales); ++j) {
1432 threads[i][j]->join();
1433 }
b331163b 1434 }
2ca993e8
A
1435 // Because of cache eviction, we can't assert exactly how many
1436 // distinct objects get created over the course of this run.
1437 // However we know that at least 8 objects get created because that
1438 // is how many distinct languages we have in our test.
1439 if (gObjectsCreated < 8) {
1440 errln("%s:%d Too few objects created.", __FILE__, __LINE__);
1441 }
1442 // We know that each thread cannot create more than 2 objects in
1443 // the cache, and there are UPRV_LENGTHOF(gCacheLocales) pairs of
1444 // objects fetched from the cache. If the threads run in series because
1445 // of eviction, at worst case each thread creates two objects.
1446 if (gObjectsCreated > 2 * CACHE_LOAD * UPRV_LENGTHOF(gCacheLocales)) {
1447 errln("%s:%d Too many objects created, got %d, expected %d", __FILE__, __LINE__, gObjectsCreated, 2 * CACHE_LOAD * UPRV_LENGTHOF(gCacheLocales));
1448
1449 }
1450
1451 assertEquals("unused values", 2, cache.unusedCount());
b331163b
A
1452
1453 // clean up threads
1454 for (int32_t i=0; i<CACHE_LOAD; ++i) {
1455 for (int32_t j=0; j<UPRV_LENGTHOF(gCacheLocales); ++j) {
1456 delete threads[i][j];
1457 }
1458 }
1459}
1460
2ca993e8
A
1461#if !UCONFIG_NO_TRANSLITERATION
1462//
1463// BreakTransliterator Threading Test
1464// This is a test for bug #11603. Test verified to fail prior to fix.
1465//
1466
1467static const Transliterator *gSharedTransliterator;
1468static const UnicodeString *gTranslitInput;
1469static const UnicodeString *gTranslitExpected;
1470
1471class BreakTranslitThread: public SimpleThread {
1472 public:
1473 BreakTranslitThread() {};
1474 ~BreakTranslitThread() {};
1475 void run();
1476};
1477
1478void BreakTranslitThread::run() {
1479 for (int i=0; i<10; i++) {
1480 icu::UnicodeString s(*gTranslitInput);
1481 gSharedTransliterator->transliterate(s);
1482 if (*gTranslitExpected != s) {
1483 IntlTest::gTest->errln("%s:%d Transliteration threading failure.", __FILE__, __LINE__);
1484 break;
1485 }
1486 }
1487}
1488
1489void MultithreadTest::TestBreakTranslit() {
1490 UErrorCode status = U_ZERO_ERROR;
1491 UnicodeString input(
1492 "\\u0E42\\u0E14\\u0E22\\u0E1E\\u0E37\\u0E49\\u0E19\\u0E10\\u0E32\\u0E19\\u0E41\\u0E25\\u0E49\\u0E27,");
1493 input = input.unescape();
1494 gTranslitInput = &input;
1495
1496 gSharedTransliterator = Transliterator::createInstance(
1497 UNICODE_STRING_SIMPLE("Any-Latin; Lower; NFD; [:Diacritic:]Remove; NFC; Latin-ASCII;"), UTRANS_FORWARD, status);
1498 if (!gSharedTransliterator) {
1499 return;
1500 }
1501 TSMTHREAD_ASSERT_SUCCESS(status);
1502
1503 UnicodeString expected(*gTranslitInput);
1504 gSharedTransliterator->transliterate(expected);
1505 gTranslitExpected = &expected;
1506
1507 BreakTranslitThread threads[4];
1508 for (int i=0; i<UPRV_LENGTHOF(threads); ++i) {
1509 threads[i].start();
1510 }
1511 for (int i=0; i<UPRV_LENGTHOF(threads); ++i) {
1512 threads[i].join();
1513 }
1514
1515 delete gSharedTransliterator;
1516 gTranslitInput = NULL;
1517 gTranslitExpected = NULL;
1518}
1519
0f5d89e8
A
1520
1521class TestIncDecThread : public SimpleThread {
1522public:
1523 TestIncDecThread() { };
1524 virtual void run();
1525};
1526
1527static u_atomic_int32_t gIncDecCounter;
1528
1529void TestIncDecThread::run() {
1530 umtx_atomic_inc(&gIncDecCounter);
1531 for (int32_t i=0; i<5000000; ++i) {
1532 umtx_atomic_inc(&gIncDecCounter);
1533 umtx_atomic_dec(&gIncDecCounter);
1534 }
1535}
1536
1537void MultithreadTest::TestIncDec()
1538{
1539 static constexpr int NUM_THREADS = 4;
1540 gIncDecCounter = 0;
1541 TestIncDecThread threads[NUM_THREADS];
1542 for (auto &thread:threads) {
1543 thread.start();
1544 }
1545 for (auto &thread:threads) {
1546 thread.join();
1547 }
1548 assertEquals("TestIncDec", NUM_THREADS, gIncDecCounter);
1549}
1550
1551
2ca993e8 1552#endif /* !UCONFIG_NO_TRANSLITERATION */