]> git.saurik.com Git - apple/icu.git/blobdiff - icuSources/test/intltest/tchcfmt.cpp
ICU-551.51.tar.gz
[apple/icu.git] / icuSources / test / intltest / tchcfmt.cpp
index bd378659c41a413f4f094c67900a2c98b64b8ed5..bd50389d4d825fdb02ff2e94196b5ba8278fc482 100644 (file)
@@ -1,7 +1,7 @@
 
 /********************************************************************
  * COPYRIGHT: 
- * Copyright (c) 1997-2003, International Business Machines Corporation and
+ * Copyright (c) 1997-2011, International Business Machines Corporation and
  * others. All Rights Reserved.
  ********************************************************************/
 
@@ -26,6 +26,7 @@ void TestChoiceFormat::runIndexedTest(int32_t index, UBool exec,
         TESTCASE(1,TestComplexExample);
         TESTCASE(2,TestClosures);
         TESTCASE(3,TestPatterns);
+        TESTCASE(4,TestChoiceFormatToPatternOverflow);
         default: name = ""; break;
     }
 }
@@ -64,7 +65,7 @@ TestChoiceFormat::TestSimpleExample( void )
         }
         //form->parse(res1, f, parse_pos);
         res2 = " ??? ";
-        it_out << ix << " -> " << res1 << " -> " << res2 << endl;
+        it_logln(UnicodeString("") + ix + UnicodeString(" -> ") + res1 + UnicodeString(" -> ") + res2);
     }
     //Testing ==operator
     const double filelimits[] = {0,1,2};
@@ -78,27 +79,24 @@ TestChoiceFormat::TestSimpleExample( void )
         errln("ERROR: ==operator failed\n");
     }
     delete formequal; 
-    
-#ifdef U_USE_CHOICE_FORMAT_DEPRECATES
-    //Testing adoptChoices() 
-    double *limitsToAdopt = (double *)uprv_malloc(7 * sizeof(double));
-    UnicodeString *monthNamesToAdopt = new UnicodeString[7];
-
-    uprv_arrayCopy(monthNames, monthNamesToAdopt, 7);
-    uprv_memcpy(limitsToAdopt, limits, (size_t)(7 * sizeof(limits[0])));    
-
-    formnew->adoptChoices(limitsToAdopt, monthNamesToAdopt, 7);
-    if(!(*formnew == *form)){
-        errln("ERROR: ==Operator or adoptChoices failed\n");
-    }
-#endif
-
     delete formnew; 
-      
+
     //Testing getLimits()
-    double *gotLimits=0;
     int32_t count=0;
-    gotLimits=(double*)form->getLimits(count);
+    const double *gotLimits=form->getLimits(count);
+#if 1  // ICU 4.8 deprecates and disables the ChoiceFormat getters.
+    if(count != 0 || gotLimits != NULL) {
+        errln("getLimits() returns something, should be disabled");
+    }
+    const UnicodeString *gotFormats=form->getFormats(count);
+    if(count != 0 || gotFormats != NULL) {
+        errln("getFormats() returns something, should be disabled");
+    }
+    const UBool *gotClosures=form->getClosures(count);
+    if(count != 0 || gotClosures != NULL) {
+        errln("getClosures() returns something, should be disabled");
+    }
+#else
     if(count != 7){
         errln("getLimits didn't update the count correctly\n");
     }
@@ -107,10 +105,9 @@ TestChoiceFormat::TestSimpleExample( void )
             errln((UnicodeString)"getLimits didn't get the limits correctly.  Expected " + limits[ix] + " Got " + gotLimits[ix]);
         }
     }
-    //Testing getFormat()
+    //Testing getFormats()
     count=0;
-    UnicodeString *gotFormats=0;
-    gotFormats=(UnicodeString*)form->getFormats(count);
+    const UnicodeString *gotFormats=form->getFormats(count);
     if(count != 7){
         errln("getFormats didn't update the count correctly\n");
     }
@@ -119,10 +116,9 @@ TestChoiceFormat::TestSimpleExample( void )
             errln((UnicodeString)"getFormats didn't get the Formats correctly.  Expected " + monthNames[ix] + " Got " + gotFormats[ix]);
         }
     }
-    
-   
+#endif
+
     delete form;
-   
 }
 
 void
@@ -141,7 +137,7 @@ TestChoiceFormat::TestComplexExample( void )
 
     Format* filenumform = NumberFormat::createInstance( status );
     if (!filenumform) { 
-        it_errln("***  test_complex_example filenumform"); 
+        dataerrln((UnicodeString)"***  test_complex_example filenumform - " + u_errorName(status)); 
         delete fileform;
         return; 
     }
@@ -176,11 +172,11 @@ TestChoiceFormat::TestComplexExample( void )
     UnicodeString str;
     UnicodeString res1, res2;
     pattform->toPattern( res1 );
-    it_out << "MessageFormat toPattern: " << res1 << endl;
+    it_logln("MessageFormat toPattern: " + res1);
     fileform->toPattern( res1 );
-    it_out << "ChoiceFormat toPattern: " << res1 << endl;
-    if (res1 == "-1.0#are corrupted files|0.0#are no files|1.0#is one file|2.0#are {2} files") {
-        it_out << "toPattern tested!" << endl;
+    it_logln("ChoiceFormat toPattern: " + res1);
+    if (res1 == "-1#are corrupted files|0#are no files|1#is one file|2#are {2} files") {
+        it_logln("toPattern tested!");
     }else{
         it_errln("***  ChoiceFormat to Pattern result!");
     }
@@ -219,16 +215,18 @@ TestChoiceFormat::TestComplexExample( void )
             delete pattform;
             return;
         }
-        it_out << i << " -> " << res2 << endl;
+        it_logln(i + UnicodeString(" -> ") + res2);
         if (res2 != checkstr[i - start]) {
             it_errln("***  test_complex_example res string");
-            it_out << "*** " << i << " -> '" << res2 << "' unlike '" << checkstr[i] << "' ! " << endl;
+            it_errln(UnicodeString("*** ") + i + UnicodeString(" -> '") + res2 + UnicodeString("' unlike '") + checkstr[i] + UnicodeString("' ! "));
         }
     }
-    it_out << endl;
+    it_logln();
 
-    it_out << "------ additional testing in complex test ------" << endl << endl;
+    it_logln("------ additional testing in complex test ------");
+    it_logln();
     //
+#if 0  // ICU 4.8 deprecates and disables the ChoiceFormat getters.
     int32_t retCount;
     const double* retLimits = fileform->getLimits( retCount );
     if ((retCount == 4) && (retLimits)
@@ -236,7 +234,7 @@ TestChoiceFormat::TestComplexExample( void )
     && (retLimits[1] == 0.0)
     && (retLimits[2] == 1.0)
     && (retLimits[3] == 2.0)) {
-        it_out << "getLimits tested!" << endl;
+        it_logln("getLimits tested!");
     }else{
         it_errln("***  getLimits unexpected result!");
     }
@@ -247,10 +245,11 @@ TestChoiceFormat::TestComplexExample( void )
     && (retFormats[1] == "are no files") 
     && (retFormats[2] == "is one file")
     && (retFormats[3] == "are {2} files")) {
-        it_out << "getFormats tested!" << endl;
+        it_logln("getFormats tested!");
     }else{
         it_errln("***  getFormats unexpected result!");
     }
+#endif
 
     UnicodeString checkstr2[] = { 
         "There is no folder on Disk_A",
@@ -260,7 +259,8 @@ TestChoiceFormat::TestComplexExample( void )
     };
 
     fileform->applyPattern("0#is no folder|1#is one folder|2#are many folders", status );
-    if (status == U_ZERO_ERROR) it_out << "status applyPattern OK!" << endl;
+    if (status == U_ZERO_ERROR)
+        it_logln("status applyPattern OK!");
     if (!chkstatus( status, "***  test_complex_example pattform" )) {
         delete fileform;
         delete filenumform;
@@ -281,10 +281,10 @@ TestChoiceFormat::TestComplexExample( void )
             delete pattform;
             return;
         }
-        it_out << i << " -> " << res2 << endl;
+        it_logln(UnicodeString() + i + UnicodeString(" -> ") + res2);
         if (res2 != checkstr2[i]) {
             it_errln("***  test_complex_example res string");
-            it_out << "*** " << i << " -> '" << res2 << "' unlike '" << checkstr2[i] << "' ! " << endl;
+            it_errln(UnicodeString("*** ") + i + UnicodeString(" -> '") + res2 + UnicodeString("' unlike '") + checkstr2[i] + UnicodeString("' ! "));
         }
     }
 
@@ -299,13 +299,13 @@ TestChoiceFormat::TestComplexExample( void )
         it_errln("***  test-choiceFormat not allocatable!");
     }else{
         if (*form_A == *form_A2) {
-            it_out << "operator== tested." << endl;
+            it_logln("operator== tested.");
         }else{
             it_errln("***  operator==");
         }
 
         if (*form_A != *form_B) {
-            it_out << "operator!= tested." << endl;
+            it_logln("operator!= tested.");
         }else{
             it_errln("***  operator!=");
         }
@@ -315,7 +315,7 @@ TestChoiceFormat::TestComplexExample( void )
             it_errln("***  ChoiceFormat->clone is nil.");
         }else{
             if ((*form_A3 == *form_A) && (*form_A3 != *form_B)) {
-                it_out << "method clone tested." << endl;
+                it_logln("method clone tested.");
             }else{
                 it_errln("***  ChoiceFormat clone or operator==, or operator!= .");
             }
@@ -326,7 +326,7 @@ TestChoiceFormat::TestComplexExample( void )
         form_Assigned = *form_B;
         ok = ok && (form_Assigned != *form_A) && (form_Assigned == *form_B);
         if (ok) {
-            it_out << "copy constructor and operator= tested." << endl;
+            it_logln("copy constructor and operator= tested.");
         }else{
             it_errln("***  copy constructor or operator= or operator == or operator != .");
         }
@@ -346,39 +346,20 @@ TestChoiceFormat::TestComplexExample( void )
     }
 
     form_pat.toPattern( res1 );
-    if (res1 == "0.0#none|1.0#one|2.0#many") {
-        it_out << "ChoiceFormat contructor( newPattern, status) tested" << endl;
+    if (res1 == "0#none|1#one|2#many") {
+        it_logln("ChoiceFormat contructor( newPattern, status) tested");
     }else{
         it_errln("***  ChoiceFormat contructor( newPattern, status) or toPattern result!");
     }
 
-#ifdef U_USE_CHOICE_FORMAT_DEPRECATES
-    double* d_a = (double *)uprv_malloc(2 * sizeof(double));
-    if (!d_a) { it_errln("*** allocation error."); return; }
-    d_a[0] = 1.0; d_a[1] = 2.0;
-
-    UnicodeString* s_a = new UnicodeString[2];
-    if (!s_a) { it_errln("*** allocation error."); return; }
-    s_a[0] = "first"; s_a[1] = "second";
-
-    form_pat.adoptChoices( d_a, s_a, 2 );
-    form_pat.toPattern( res1 );
-    it_out << "ChoiceFormat adoptChoices toPattern: " << res1 << endl;
-    if (res1 == "1.0#first|2.0#second") {
-        it_out << "ChoiceFormat adoptChoices tested" << endl;
-    }else{
-        it_errln("***  ChoiceFormat adoptChoices result!");
-    }
-#endif
-
     double d_a2[] = { 3.0, 4.0 };
     UnicodeString s_a2[] = { "third", "forth" };
 
     form_pat.setChoices( d_a2, s_a2, 2 );
     form_pat.toPattern( res1 );
-    it_out << "ChoiceFormat adoptChoices toPattern: " << res1 << endl;
-    if (res1 == "3.0#third|4.0#forth") {
-        it_out << "ChoiceFormat adoptChoices tested" << endl;
+    it_logln(UnicodeString("ChoiceFormat adoptChoices toPattern: ") + res1);
+    if (res1 == "3#third|4#forth") {
+        it_logln("ChoiceFormat adoptChoices tested");
     }else{
         it_errln("***  ChoiceFormat adoptChoices result!");
     }
@@ -388,15 +369,23 @@ TestChoiceFormat::TestComplexExample( void )
     status = U_ZERO_ERROR;
     double arg_double = 3.0;
     res1 = form_pat.format( arg_double, str, fpos );
-    it_out << "ChoiceFormat format:" << res1 << endl;
+    it_logln(UnicodeString("ChoiceFormat format:") + res1);
     if (res1 != "third") it_errln("***  ChoiceFormat format (double, ...) result!");
 
+    str = "";
+    fpos = 0;
+    status = U_ZERO_ERROR;
+    int64_t arg_64 = 3;
+    res1 = form_pat.format( arg_64, str, fpos );
+    it_logln(UnicodeString("ChoiceFormat format:") + res1);
+    if (res1 != "third") it_errln("***  ChoiceFormat format (int64_t, ...) result!");
+
     str = "";
     fpos = 0;
     status = U_ZERO_ERROR;
     int32_t arg_long = 3;
     res1 = form_pat.format( arg_long, str, fpos );
-    it_out << "ChoiceFormat format:" << res1 << endl;
+    it_logln(UnicodeString("ChoiceFormat format:") + res1);
     if (res1 != "third") it_errln("***  ChoiceFormat format (int32_t, ...) result!");
 
     Formattable ft( (int32_t)3 );
@@ -410,7 +399,7 @@ TestChoiceFormat::TestComplexExample( void )
         delete pattform;
         return;
     }
-    it_out << "ChoiceFormat format:" << res1 << endl;
+    it_logln(UnicodeString("ChoiceFormat format:") + res1);
     if (res1 != "third") it_errln("***  ChoiceFormat format (Formattable, ...) result!");
 
     Formattable fta[] = { (int32_t)3 };
@@ -424,7 +413,7 @@ TestChoiceFormat::TestComplexExample( void )
         delete pattform;
         return;
     }
-    it_out << "ChoiceFormat format:" << res1 << endl;
+    it_logln(UnicodeString("ChoiceFormat format:") + res1);
     if (res1 != "third") it_errln("***  ChoiceFormat format (Formattable[], cnt, ...) result!");
 
     ParsePosition parse_pos = 0;
@@ -433,7 +422,7 @@ TestChoiceFormat::TestComplexExample( void )
     form_pat.parse( parsetext, result, parse_pos );
     double rd = (result.getType() == Formattable::kLong) ? result.getLong() : result.getDouble();
     if (rd == 3.0) {
-        it_out << "parse( ..., ParsePos ) tested." << endl;
+        it_logln("parse( ..., ParsePos ) tested.");
     }else{
         it_errln("*** ChoiceFormat parse( ..., ParsePos )!");
     }
@@ -441,7 +430,7 @@ TestChoiceFormat::TestComplexExample( void )
     form_pat.parse( parsetext, result, status );
     rd = (result.getType() == Formattable::kLong) ? result.getLong() : result.getDouble();
     if (rd == 3.0) {
-        it_out << "parse( ..., UErrorCode ) tested." << endl;
+        it_logln("parse( ..., UErrorCode ) tested.");
     }else{
         it_errln("*** ChoiceFormat parse( ..., UErrorCode )!");
     }
@@ -455,7 +444,7 @@ TestChoiceFormat::TestComplexExample( void )
     }
     */
 
-    it_out << endl;
+    it_logln();
 
     delete fileform; 
     delete filenumform;
@@ -487,7 +476,7 @@ void TestChoiceFormat::TestClosures(void) {
 
     // 'fmt2' is created using a pattern; it should be equivalent
     UErrorCode status = U_ZERO_ERROR;
-    const char* PAT = "0.0#,1)|1.0#[1,2]|2.0<(2,3]|3.0<(3,4)|4.0#[4,5)|5.0#[5,";
+    const char* PAT = "0#,1)|1#[1,2]|2<(2,3]|3<(3,4)|4#[4,5)|5#[5,";
     ChoiceFormat fmt2(PAT, status);
     if (U_FAILURE(status)) {
         errln("FAIL: ChoiceFormat constructor failed");
@@ -509,6 +498,7 @@ void TestChoiceFormat::TestClosures(void) {
         errln("FAIL: fmt1 != fmt2");
     }
 
+#if 0  // ICU 4.8 deprecates and disables the ChoiceFormat getters.
     int32_t i;
     int32_t count2 = 0;
     const double *limits2 = fmt2.getLimits(count2);
@@ -530,6 +520,7 @@ void TestChoiceFormat::TestClosures(void) {
           }
         }
     }
+#endif
 
     // Now test both format objects
     UnicodeString exp[] = {
@@ -619,6 +610,7 @@ void TestChoiceFormat::TestPatterns(void) {
                  1.0, "b",
                  1.0 + 1e-9, "c");
 
+#if 0  // ICU 4.8 only checks the pattern syntax, not whether the ranges make sense.
     // Try an invalid pattern that isolates a single value.
     // [-Inf,1.0) [1.0,1.0) [1.0,+Inf]
     _testPattern("0.0#a|1.0#b|1.0#c", FALSE,
@@ -637,6 +629,20 @@ void TestChoiceFormat::TestPatterns(void) {
     // [-Inf,2.0) [2.0,1.0) [1.0,+Inf]
     _testPattern("0.0#a|2.0#b|1.0#c", FALSE,
                  0, 0, 0, 0, 0, 0);
+#endif
+}
+
+void TestChoiceFormat::TestChoiceFormatToPatternOverflow() 
+{
+    static const double limits[] = {0.1e-78, 1e13, 0.1e78};
+    UnicodeString monthNames[] = { "one", "two", "three" };
+    ChoiceFormat fmt(limits, monthNames, sizeof(limits)/sizeof(limits[0]));
+    UnicodeString patStr, expectedPattern1("1e-79#one|10000000000000#two|1e+77#three"), 
+        expectedPattern2("1e-079#one|10000000000000#two|1e+077#three");
+    fmt.toPattern(patStr);
+    if (patStr != expectedPattern1 && patStr != expectedPattern2) {
+        errln("ChoiceFormat returned \"" + patStr + "\" instead of \"" + expectedPattern1 + " or " + expectedPattern2 + "\"");
+    }
 }
 
 #endif /* #if !UCONFIG_NO_FORMATTING */