+/**
+ * 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<NumberFormat> fFormat; // formatter - en_US constructed currency
+};
+
+
+ThreadSafeFormat::ThreadSafeFormat(UErrorCode &status) {
+ fFormat.adoptInstead(NumberFormat::createCurrencyInstance(Locale::getUS(), status));
+}
+
+static const UChar kUSD[] = { 0x55, 0x53, 0x44, 0x00 };
+
+UBool ThreadSafeFormat::doStuff(int32_t offset, UnicodeString &appendErr, UErrorCode &status) const {
+ UBool okay = TRUE;
+
+ if(u_strcmp(fFormat->getCurrency(), kUSD)) {
+ appendErr.append("fFormat currency != ")
+ .append(kUSD)
+ .append(", =")
+ .append(fFormat->getCurrency())
+ .append("! ");
+ okay = FALSE;
+ }
+
+ if(u_strcmp(gSharedData->fFormat->getCurrency(), kUSD)) {
+ appendErr.append("gFormat currency != ")
+ .append(kUSD)
+ .append(", =")
+ .append(gSharedData->fFormat->getCurrency())
+ .append("! ");
+ 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;
+}
+
+//static UMTX debugMutex = NULL;
+//static UMTX gDebugMutex;
+
+
+class FormatThreadTest : public SimpleThread
+{
+public:
+ int fNum;
+ int fTraceInfo;
+
+ LocalPointer<ThreadSafeFormat> fTSF;
+
+ FormatThreadTest() // constructor is NOT multithread safe.
+ : 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;
+ }
+
+
+ virtual void run()
+ {
+ fTraceInfo = 1;
+ LocalPointer<NumberFormat> percentFormatter;
+ UErrorCode status = U_ZERO_ERROR;
+
+#if 0
+ // debugging code,
+ for (int i=0; i<4000; i++) {
+ status = U_ZERO_ERROR;
+ UDataMemory *data1 = udata_openChoice(0, "res", "en_US", isAcceptable, 0, &status);
+ UDataMemory *data2 = udata_openChoice(0, "res", "fr", isAcceptable, 0, &status);
+ udata_close(data1);
+ udata_close(data2);
+ if (U_FAILURE(status)) {
+ error("udata_openChoice failed.\n");
+ break;
+ }
+ }
+ return;
+#endif
+
+#if 0
+ // debugging code,
+ int m;
+ for (m=0; m<4000; m++) {
+ status = U_ZERO_ERROR;
+ UResourceBundle *res = NULL;
+ const char *localeName = NULL;
+
+ Locale loc = Locale::getEnglish();
+
+ localeName = loc.getName();
+ // localeName = "en";
+
+ // ResourceBundle bund = ResourceBundle(0, loc, status);
+ //umtx_lock(&gDebugMutex);
+ res = ures_open(NULL, localeName, &status);
+ //umtx_unlock(&gDebugMutex);
+
+ //umtx_lock(&gDebugMutex);
+ ures_close(res);
+ //umtx_unlock(&gDebugMutex);
+
+ if (U_FAILURE(status)) {
+ error("Resource bundle construction failed.\n");
+ break;
+ }
+ }
+ return;
+#endif
+
+ // Keep this data here to avoid static initialization.
+ FormatThreadTestData kNumberFormatTestData[] =
+ {