]> git.saurik.com Git - apple/icu.git/blobdiff - icuSources/test/intltest/tmsgfmt.cpp
ICU-551.30.tar.gz
[apple/icu.git] / icuSources / test / intltest / tmsgfmt.cpp
index d7c6d64034e9530d46e5a17812f52cb52a340f28..939de8c627d39919e706e64c8e6e321e8ab3c44c 100644 (file)
@@ -1,6 +1,6 @@
 /********************************************************************
  * 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
@@ -18,6 +18,7 @@
 #if !UCONFIG_NO_FORMATTING
 
 #include "tmsgfmt.h"
+#include "cmemory.h"
 
 #include "unicode/format.h"
 #include "unicode/decimfmt.h"
@@ -31,8 +32,6 @@
 #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*/) {
@@ -67,6 +66,7 @@ TestMessageFormat::runIndexedTest(int32_t index, UBool exec,
     TESTCASE_AUTO(testGetFormatNames);
     TESTCASE_AUTO(TestTrimArgumentName);
     TESTCASE_AUTO(TestSelectOrdinal);
+    TESTCASE_AUTO(TestDecimals);
     TESTCASE_AUTO_END;
 }
 
@@ -578,11 +578,11 @@ void TestMessageFormat::testMsgFormatPlural(/* char* par */)
     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);
 
@@ -654,7 +654,7 @@ void TestMessageFormat::internalFormat(MessageFormat* msgFmt ,
         //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 ) {
@@ -670,7 +670,7 @@ MessageFormat* TestMessageFormat::internalCreate(
     //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;
     }
@@ -752,6 +752,7 @@ void TestMessageFormat::testMsgFormatSelect(/* char* par */)
     //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)) {
@@ -771,7 +772,6 @@ void TestMessageFormat::testMsgFormatSelect(/* char* par */)
     }
     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;
@@ -1605,7 +1605,7 @@ void TestMessageFormat::TestApostropheMode() {
         "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];
@@ -1892,4 +1892,75 @@ void TestMessageFormat::TestSelectOrdinal() {
     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 */