/********************************************************************
* COPYRIGHT:
- * Copyright (c) 1997-2012, International Business Machines Corporation and
+ * Copyright (c) 1997-2014, International Business Machines Corporation and
* others. All Rights Reserved.
********************************************************************
* File TMSGFMT.CPP
#if !UCONFIG_NO_FORMATTING
#include "tmsgfmt.h"
+#include "cmemory.h"
#include "unicode/format.h"
#include "unicode/decimfmt.h"
#include "unicode/gregocal.h"
#include <stdio.h>
-#define LENGTHOF(array) (int32_t)(sizeof(array)/sizeof((array)[0]))
-
void
TestMessageFormat::runIndexedTest(int32_t index, UBool exec,
const char* &name, char* /*par*/) {
TESTCASE_AUTO(testGetFormatNames);
TESTCASE_AUTO(TestTrimArgumentName);
TESTCASE_AUTO(TestSelectOrdinal);
+ TESTCASE_AUTO(TestDecimals);
TESTCASE_AUTO_END;
}
delete mfNum;
delete mfAlpha;
- MessageFormat* mfNum2 = new MessageFormat(t3, Locale("ru"), err);
+ MessageFormat* mfNum2 = new MessageFormat(t3, Locale("uk"), err);
numResult1.remove();
Formattable testArgs2((int32_t)4);
mfNum2->format(&testArgs2, 1, numResult1, ignore, err);
- MessageFormat* mfAlpha2 = new MessageFormat(t4, Locale("ru"), err);
+ MessageFormat* mfAlpha2 = new MessageFormat(t4, Locale("uk"), err);
argNameResult.remove();
mfAlpha2->format(argName, &testArgs2, 1, argNameResult, err);
//Format with passed arguments
msgFmt->format( args , numOfArgs , result, ignore, status);
if (U_FAILURE(status)) {
- dataerrln( "%serror while formatting with ErrorCode as %s" ,errMsg, u_errorName(status) );
+ dataerrln( "%s error while formatting with ErrorCode as %s" ,errMsg, u_errorName(status) );
}
//Compare expected with obtained result
if ( result!= expected ) {
//Create the MessageFormat with simple SelectFormat
MessageFormat* msgFmt = new MessageFormat(pattern, locale, status);
if (U_FAILURE(status)) {
- dataerrln( "%serror while constructing with ErrorCode as %s" ,errMsg, u_errorName(status) );
+ dataerrln( "%s error while constructing with ErrorCode as %s" ,errMsg, u_errorName(status) );
logln(UnicodeString("TestMessageFormat::testMsgFormatSelect #1 with error code ")+(int32_t)status);
return NULL;
}
//Nested patterns with plural, number ,choice ,select format etc.
//Select Format with embedded number format
UnicodeString t4("{0} est {1, select, female {{2,number,integer} all\\u00E9e} other {all\\u00E9}} \\u00E0 Paris.");
+ err = U_ZERO_ERROR;
//Create the MessageFormat with Select Format with embedded number format (nested pattern)
MessageFormat* msgFmt4 = internalCreate(t4.unescape(), Locale("fr"),err,(char*)"From TestMessageFormat::TestSelectFormat create t4");
if (!U_FAILURE(err)) {
}
delete msgFmt4;
- err = U_ZERO_ERROR;
//Plural format with embedded select format
UnicodeString t5("{0} {1, plural, one {est {2, select, female {all\\u00E9e} other {all\\u00E9}}} other {sont {2, select, female {all\\u00E9es} other {all\\u00E9s}}}} \\u00E0 Paris.");
err = U_ZERO_ERROR;
"I don't know", "I don't know", "I don''t know",
"I don't know", "I don''t know", "I don''t know"
};
- int32_t tuples_count = LENGTHOF(tuples);
+ int32_t tuples_count = UPRV_LENGTHOF(tuples);
for (int i = 0; i < tuples_count; i += 3) {
UnicodeString& desired = tuples[i];
errorCode.logDataIfFailureAndReset("");
}
+void TestMessageFormat::TestDecimals() {
+ IcuTestErrorCode errorCode(*this, "TestDecimals");
+ // Simple number replacement.
+ MessageFormat m(
+ "{0,plural,one{one meter}other{# meters}}",
+ Locale::getEnglish(), errorCode);
+ Formattable args[1] = { (int32_t)1 };
+ FieldPosition ignore;
+ UnicodeString result;
+ assertEquals("simple format(1)", "one meter",
+ m.format(args, 1, result, ignore, errorCode), TRUE);
+
+ args[0] = (double)1.5;
+ result.remove();
+ assertEquals("simple format(1.5)", "1.5 meters",
+ m.format(args, 1, result, ignore, errorCode), TRUE);
+
+ // Simple but explicit.
+ MessageFormat m0(
+ "{0,plural,one{one meter}other{{0} meters}}",
+ Locale::getEnglish(), errorCode);
+ args[0] = (int32_t)1;
+ result.remove();
+ assertEquals("explicit format(1)", "one meter",
+ m0.format(args, 1, result, ignore, errorCode), TRUE);
+
+ args[0] = (double)1.5;
+ result.remove();
+ assertEquals("explicit format(1.5)", "1.5 meters",
+ m0.format(args, 1, result, ignore, errorCode), TRUE);
+
+ // With offset and specific simple format with optional decimals.
+ MessageFormat m1(
+ "{0,plural,offset:1 one{another meter}other{{0,number,00.#} meters}}",
+ Locale::getEnglish(), errorCode);
+ args[0] = (int32_t)1;
+ result.remove();
+ assertEquals("offset format(1)", "01 meters",
+ m1.format(args, 1, result, ignore, errorCode), TRUE);
+
+ args[0] = (int32_t)2;
+ result.remove();
+ assertEquals("offset format(1)", "another meter",
+ m1.format(args, 1, result, ignore, errorCode), TRUE);
+
+ args[0] = (double)2.5;
+ result.remove();
+ assertEquals("offset format(1)", "02.5 meters",
+ m1.format(args, 1, result, ignore, errorCode), TRUE);
+
+ // With offset and specific simple format with forced decimals.
+ MessageFormat m2(
+ "{0,plural,offset:1 one{another meter}other{{0,number,0.0} meters}}",
+ Locale::getEnglish(), errorCode);
+ args[0] = (int32_t)1;
+ result.remove();
+ assertEquals("offset-decimals format(1)", "1.0 meters",
+ m2.format(args, 1, result, ignore, errorCode), TRUE);
+
+ args[0] = (int32_t)2;
+ result.remove();
+ assertEquals("offset-decimals format(1)", "2.0 meters",
+ m2.format(args, 1, result, ignore, errorCode), TRUE);
+
+ args[0] = (double)2.5;
+ result.remove();
+ assertEquals("offset-decimals format(1)", "2.5 meters",
+ m2.format(args, 1, result, ignore, errorCode), TRUE);
+ errorCode.reset();
+}
+
#endif /* #if !UCONFIG_NO_FORMATTING */