home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 6 / AACD06.ISO / AACD / Programming / ICU / src / icu / source / test / intltest / tmsgfmt.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1999-10-19  |  34.5 KB  |  1,068 lines

  1. /*
  2. ********************************************************************************
  3. *                                                                              *
  4. * COPYRIGHT:                                                                   *
  5. *   (C) Copyright Taligent, Inc.,  1997                                        *
  6. *   (C) Copyright International Business Machines Corporation,  1997-1998           *
  7. *   Licensed Material - Program-Property of IBM - All Rights Reserved.         *
  8. *   US Government Users Restricted Rights - Use, duplication, or disclosure    *
  9. *   restricted by GSA ADP Schedule Contract with IBM Corp.                     *
  10. *                                                                              *
  11. ********************************************************************************
  12. *
  13. * File TMSGFMT.CPP
  14. *
  15. * Modification History:
  16. *
  17. *   Date        Name        Description
  18. *   03/24/97    helena      Converted from Java.
  19. *   07/11/97    helena      Updated to work on AIX.
  20. *   08/04/97    jfitz       Updated to intltest
  21. ********************************************************************************
  22. */
  23. #include "tmsgfmt.h"
  24.  
  25. #include "format.h"
  26. #include "decimfmt.h"
  27. #include "locid.h"
  28. #include "msgfmt.h"
  29. #include "numfmt.h"
  30. #include "choicfmt.h"
  31. #include "gregocal.h"
  32.  
  33. Formattable TestMessageFormat::testArgs[] = {
  34.     Formattable(double(1)), Formattable(double(3456)),
  35.         Formattable("Disk"), Formattable(UDate(T_INT32(1000000000)), Formattable::kIsDate)
  36. };
  37.  
  38. UnicodeString TestMessageFormat::testCases[] = {
  39.    "Quotes '', '{', 'a' {0} '{0}'",
  40.    "Quotes '', '{', 'a' {0,number} '{0}'",
  41.    "'{'1,number,'#',##} {1,number,'#',##}",
  42.    "There are {1} files on {2} at {3}.",
  43.    "On {2}, there are {1} files, with {0,number,currency}.",
  44.    "'{1,number,percent}', {1,number,percent},",
  45.    "'{1,date,full}', {1,date,full},",
  46.    "'{3,date,full}', {3,date,full},",
  47.    "'{1,number,#,##}' {1,number,#,##}",
  48. };
  49.  
  50. UnicodeString TestMessageFormat::testResultPatterns[] = {
  51.     "Quotes '', '{', a {0} '{'0}",
  52.     "Quotes '', '{', a {0,number} '{'0}",
  53.     "'{'1,number,#,##} {1,number,'#'#,##}",
  54.     "There are {1} files on {2} at {3}.",
  55.     "On {2}, there are {1} files, with {0,number,currency}.",
  56.     "'{'1,number,percent}, {1,number,percent},",
  57.     "'{'1,date,full}, {1,date,full},",
  58.     "'{'3,date,full}, {3,date,full},",
  59.     "'{'1,number,#,##} {1,number,#,##}"
  60. };
  61.  
  62. UnicodeString TestMessageFormat::testResultStrings[] = {
  63.     "Quotes ', {, a 1 {0}",
  64.     "Quotes ', {, a 1 {0}",
  65.     "{1,number,#,##} #34,56",
  66.     "There are 3,456 files on Disk at 1/12/70 5:46 AM.",
  67.     "On Disk, there are 3,456 files, with $1.00.",
  68.     "{1,number,percent}, 345,600%,",
  69.     "{1,date,full}, Wednesday, December 31, 1969,",
  70.     "{3,date,full}, Monday, January 12, 1970,",
  71.     "{1,number,#,##} 34,56"
  72. };
  73.  
  74.  
  75.  
  76. void TestMessageFormat::testBug3()
  77. {
  78.     double myNumber = -123456;
  79.     DecimalFormat *form = 0;
  80.         Locale locale[] = {
  81.     Locale("ar", "", ""),
  82.     Locale("be", "", ""),
  83.     Locale("bg", "", ""),
  84.     Locale("ca", "", ""),
  85.     Locale("cs", "", ""),
  86.     Locale("da", "", ""),
  87.     Locale("de", "", ""),
  88.     Locale("de", "AT", ""),
  89.     Locale("de", "CH", ""),
  90.     Locale("el", "", ""),       // 10
  91.     Locale("en", "CA", ""),
  92.     Locale("en", "GB", ""),
  93.     Locale("en", "IE", ""),
  94.     Locale("en", "US", ""),
  95.     Locale("es", "", ""),
  96.     Locale("et", "", ""),
  97.     Locale("fi", "", ""),
  98.     Locale("fr", "", ""),
  99.     Locale("fr", "BE", ""),
  100.     Locale("fr", "CA", ""),     // 20
  101.     Locale("fr", "CH", ""),
  102.     Locale("he", "", ""),
  103.     Locale("hr", "", ""),
  104.     Locale("hu", "", ""),
  105.     Locale("is", "", ""),
  106.     Locale("it", "", ""),
  107.     Locale("it", "CH", ""),
  108.     Locale("ja", "", ""),
  109.     Locale("ko", "", ""),
  110.     Locale("lt", "", ""),       // 30
  111.     Locale("lv", "", ""),
  112.     Locale("mk", "", ""),
  113.     Locale("nl", "", ""),
  114.     Locale("nl", "BE", ""),
  115.     Locale("no", "", ""),
  116.     Locale("pl", "", ""),
  117.     Locale("pt", "", ""),
  118.     Locale("ro", "", ""),
  119.     Locale("ru", "", ""),
  120.     Locale("sh", "", ""),       // 40
  121.     Locale("sk", "", ""),
  122.     Locale("sl", "", ""),
  123.     Locale("sq", "", ""),
  124.     Locale("sr", "", ""),
  125.     Locale("sv", "", ""),
  126.     Locale("tr", "", ""),
  127.     Locale("uk", "", ""),
  128.     Locale("zh", "", ""),
  129.     Locale("zh", "TW", "") };   // 49
  130.     int32_t i;
  131.     for (i= 0; i < 49; i++) {
  132.         UnicodeString buffer;
  133.         it_out << locale[i].getDisplayName(buffer) << endl;
  134.         UErrorCode success = U_ZERO_ERROR;
  135. //        form = (DecimalFormat*)NumberFormat::createCurrencyInstance(locale[i], success);
  136.         form = (DecimalFormat*)NumberFormat::createInstance(locale[i], success);
  137.         if (U_FAILURE(success)) {
  138.             it_errln("Err: Number Format ");
  139.             it_out << "Number format creation failed.\n";
  140.             continue;
  141.         }
  142.         Formattable result;
  143.         FieldPosition pos(0);
  144.         buffer.remove();
  145.         form->format(myNumber, buffer, pos);
  146.         success = U_ZERO_ERROR;
  147.         ParsePosition parsePos;
  148.         form->parse(buffer, result, parsePos);
  149. #ifdef _DEBUG
  150.         it_out << " -> " /*<< dec*/ /*<< result*/ << "[supposed output for result]" <<endl;
  151. #endif
  152.         if (U_FAILURE(success)) {
  153.             it_errln("Err: Number Format parse");
  154.             it_out << "Number format parse failed.\n";
  155.         }
  156.         delete form;
  157.     }
  158. }
  159.  
  160. void TestMessageFormat::testBug1()
  161. {
  162.     const double limit[] = {0.0, 1.0, 2.0};
  163.     const UnicodeString formats[] = {"0.0<=Arg<1.0",
  164.                                "1.0<=Arg<2.0",
  165.                                "2.0<-Arg"};
  166.     ChoiceFormat *cf = new ChoiceFormat(limit, formats, 3);
  167.     FieldPosition status(0);
  168.     UnicodeString toAppendTo;
  169.     cf->format(T_INT32(1.0), toAppendTo, status);
  170.     if (toAppendTo != "1.0<=Arg<2.0") {
  171.         it_errln("ChoiceFormat cmp in testBug1");
  172.     }
  173.     it_out <<  toAppendTo << endl;
  174.     delete cf;
  175. }
  176.  
  177. void TestMessageFormat::testBug2()
  178. {
  179.     UErrorCode status = U_ZERO_ERROR;
  180.     UnicodeString result;
  181.     // {sfb} use double format in pattern, so result will match (not strictly necessary)
  182.     const UnicodeString pattern = "There {0,choice,0.0#are no files|1.0#is one file|1.0<are {0, number} files} on disk {1}. ";
  183.     it_out << "The input pattern : " << pattern << endl;
  184.     MessageFormat *fmt = new MessageFormat(pattern, status);
  185.     if (U_FAILURE(status)) {
  186.         it_errln("MessageFormat pattern creation failed.\n");
  187.         it_out << "MessageFormat pattern creation failed.\n";
  188.         return;
  189.     }
  190.     it_out << "The output pattern is : " << fmt->toPattern(result) << endl;
  191.     if (pattern != result) {
  192.         it_errln("MessageFormat::toPattern() failed.\n");
  193.         it_out << "MessageFormat::toPattern() failed.\n";
  194.     }
  195.     delete fmt;
  196. }
  197.  
  198. #ifdef _DEBUG
  199. //----------------------------------------------------
  200. // console I/O
  201. //----------------------------------------------------
  202.  
  203. ostream& operator<<(ostream& stream,  const Formattable&   obj);
  204.  
  205. #include "datefmt.h"
  206. #include <limits.h>
  207. #include <stdlib.h>
  208. #include <stdio.h>
  209. #include <string.h>
  210. #include <iostream.h>
  211.  
  212. IntlTest&
  213. operator<<( IntlTest&           stream,
  214.             const Formattable&  obj)
  215. {
  216.     static DateFormat *defDateFormat = 0;
  217.  
  218.     UnicodeString buffer;
  219.     switch(obj.getType()) {
  220.         case Formattable::kDate : 
  221.             if (defDateFormat == 0) {
  222.                 defDateFormat = DateFormat::createInstance();
  223.             }
  224.             defDateFormat->format(obj.getDate(), buffer);
  225.             stream << buffer;
  226.             break;
  227.         case Formattable::kDouble :
  228.             char convert[20];
  229.             sprintf( convert, "%lf", obj.getDouble() );
  230.             stream << convert << "D";
  231.             break;
  232.         case Formattable::kLong :
  233.             stream << obj.getLong() << "L";
  234.             break;
  235.         case Formattable::kString:
  236.             stream << "\"" << obj.getString(buffer) << "\"";
  237.             break;
  238.         case Formattable::kArray:
  239.             int32_t i, count;
  240.             const Formattable* array;
  241.             array = obj.getArray(count);
  242.             stream << "[";
  243.             for (i=0; i<count; ++i) stream << array[i] << ( (i==(count-1)) ? "" : ", " );
  244.             stream << "]";
  245.             break;
  246.         default:
  247.             stream << "INVALID_Formattable";
  248.     }
  249.     return stream;
  250. }
  251. #endif
  252.  
  253. void TestMessageFormat::PatternTest() 
  254. {
  255.  
  256.     for (int32_t i = 0; i < 9; ++i) {
  257.         //it_out << "\nPat in:  " << testCases[i] << endl;
  258.  
  259.         MessageFormat *form = 0;
  260.         UErrorCode success = U_ZERO_ERROR;
  261.         UnicodeString buffer;
  262.         form = new MessageFormat(testCases[i], Locale::US, success);
  263.         if (U_FAILURE(success)) {
  264.             it_errln ("MessageFormat creation failed.#1");
  265.             it_out << "MessageFormat for "<< testCases[i] << " creation failed.\n";
  266.             continue;
  267.         }
  268.         if (form->toPattern(buffer) != testResultPatterns[i]) {
  269.             errln(UnicodeString("TestMessageFormat::PatternTest failed test #2, i = ") + i);
  270.             //form->toPattern(buffer);
  271.             it_out << " Orig: " << testCases[i] << endl;
  272.             it_out << " Exp:  " << testResultPatterns[i] << endl;
  273.             it_out << " Got:  " << buffer << endl;
  274.         }
  275.  
  276.         //it_out << "Pat out: " << form->toPattern(buffer) << endl;
  277.         UnicodeString result;
  278.         int32_t count = 4;
  279.         FieldPosition fieldpos(0);
  280.         form->format(testArgs, count, result, fieldpos, success);
  281.         if (U_FAILURE(success)) {
  282.             it_errln ("MessageFormat failed test #3");
  283.             it_out << "TestMessageFormat::PatternTest failed test #3" << endl;
  284.             continue;
  285.         }
  286.         if (result != testResultStrings[i]) {
  287.             errln("TestMessageFormat::PatternTest failed test #4");
  288.             logln("TestMessageFormat::PatternTest failed #4.");
  289.             logln(UnicodeString("    Result: ") + result );
  290.             logln(UnicodeString("  Expected: ") + testResultStrings[i] );
  291.         }
  292.         
  293.  
  294.         //it_out << "Result:  " << result << endl;
  295. #if 0
  296.         it_out << "---------------- test parse ----------------" << endl;
  297.  
  298.         form->toPattern(buffer);
  299.         it_out << "MSG pattern for parse: " << buffer << endl;
  300.  
  301.         Formattable* values = form->parse(result, count, success);
  302.         if (U_FAILURE(success)) {
  303.             errln("MessageFormat failed test #5");
  304.             logln(UnicodeString("MessageFormat failed test #5 with error code ")+(int32_t)success);
  305.         }else
  306.         if (count != 4) {
  307.             errln("MSG count not 4 (as expected)");
  308.         }
  309.         bool_t failed = FALSE;
  310.         for (int32_t j = 0; j < count; ++j) {
  311.              if (values == 0 || testArgs[j] != values[j]) {
  312.                  it_out << "MSG testargs[" << j << "]: " << testArgs[j] << endl;
  313.                  it_out << "MSG values[" << j << "]  : " << values[j] << endl;
  314.                 failed = TRUE;
  315.              }
  316.         }
  317.         if (failed) errln("MessageFormat failed test #6");
  318. #endif
  319.         delete form;
  320.     }
  321. }
  322.  
  323. void TestMessageFormat::sample() 
  324. {
  325.     MessageFormat *form = 0;
  326.     UnicodeString buffer1, buffer2;
  327.     UErrorCode success = U_ZERO_ERROR;
  328.     form = new MessageFormat("There are {0} files on {1}", success);
  329.     if (U_FAILURE(success)) {
  330.         it_errln("Err: Message format creation failed");
  331.         it_out << "Sample message format creation failed.\n";
  332.         return;
  333.     }
  334.     UnicodeString abc("abc");
  335.     UnicodeString def("def");
  336.     Formattable testArgs[] = { abc, def };
  337.     FieldPosition fieldpos(0);
  338.     it_out << form->toPattern(buffer1) << "; " << form->format(testArgs, 2, buffer2, fieldpos, success) << endl;
  339.     delete form;
  340. }
  341.  
  342.  
  343. void TestMessageFormat::testStaticFormat(char* parm)
  344. {
  345.     logln("running TestMessageFormat::testStaticFormat");
  346.  
  347.     UErrorCode err = U_ZERO_ERROR;
  348.     GregorianCalendar cal(err);   
  349.     Formattable arguments[] = {
  350.         T_INT32(7),
  351.         Formattable(UDate(8.71068e+011), Formattable::kIsDate),
  352.         "a disturbance in the Force"
  353.         };
  354.    
  355.         UnicodeString result;
  356.         result = MessageFormat::format(
  357.             "At {1,time} on {1,date}, there was {2} on planet {0,number,integer}.",
  358.             arguments,
  359.             3,
  360.             result,
  361.             err);
  362.  
  363.         if (U_FAILURE(err)) {
  364.             errln("TestMessageFormat::testStaticFormat #1");
  365.             logln(UnicodeString("TestMessageFormat::testStaticFormat failed test #1 with error code ")+(int32_t)err);
  366.             return;
  367.         }
  368.  
  369.         static const UnicodeString expected = 
  370.                 "At 12:20:00 PM on Aug 8, 1997, there was a disturbance in the Force on planet 7.";
  371.         if (result != expected) {
  372.             errln("TestMessageFormat::testStaticFormat failed on test");
  373.             logln( UnicodeString("     Result: ") + result );
  374.             logln( UnicodeString("   Expected: ") + expected );
  375.         }
  376. }
  377.  
  378.  
  379. void TestMessageFormat::testSimpleFormat(char* parm)
  380. {
  381.     logln("running TestMessageFormat::testSimpleFormat");
  382.  
  383.     UErrorCode err = U_ZERO_ERROR;
  384.  
  385.     Formattable testArgs1[] = {T_INT32(0), "MyDisk"};
  386.     Formattable testArgs2[] = {T_INT32(1), "MyDisk"};
  387.     Formattable testArgs3[] = {T_INT32(12), "MyDisk"};
  388.    
  389.     MessageFormat* form = new MessageFormat(
  390.         "The disk \"{1}\" contains {0} file(s).", err);
  391.     
  392.     UnicodeString string;
  393.     FieldPosition ignore(FieldPosition::DONT_CARE);
  394.     form->format(testArgs1, 2, string, ignore, err);
  395.     if (U_FAILURE(err) || string != "The disk \"MyDisk\" contains 0 file(s).") {
  396.         errln(UnicodeString("TestMessageFormat::testSimpleFormat failed on test #1"));
  397.     }
  398.  
  399.     ignore.setField(FieldPosition::DONT_CARE);
  400.     string.remove();
  401.     form->format(testArgs2, 2, string, ignore, err);
  402.     if (U_FAILURE(err) || string != "The disk \"MyDisk\" contains 1 file(s).") {
  403.         logln(string);
  404.         errln(UnicodeString("TestMessageFormat::testSimpleFormat failed on test #2")+string);
  405.     }
  406.  
  407.     ignore.setField(FieldPosition::DONT_CARE);
  408.     string.remove();
  409.     form->format(testArgs3, 2, string, ignore, err);
  410.     if (U_FAILURE(err) || string != "The disk \"MyDisk\" contains 12 file(s).") {
  411.         errln(UnicodeString("TestMessageFormat::testSimpleFormat failed on test #3")+string);
  412.     }
  413.  
  414.     delete form;
  415.  }
  416.  
  417. void TestMessageFormat::testMsgFormatChoice(char* parm)
  418. {
  419.     logln("running TestMessageFormat::testMsgFormatChoice");
  420.  
  421.     UErrorCode err = U_ZERO_ERROR;
  422.  
  423.     MessageFormat* form = new MessageFormat("The disk \"{1}\" contains {0}.", err);
  424.     double filelimits[] = {0,1,2};
  425.     UnicodeString filepart[] = {"no files","one file","{0,number} files"};
  426.     ChoiceFormat* fileform = new ChoiceFormat(filelimits, filepart, 3);
  427.     form->setFormat(1,*fileform); // NOT zero, see below
  428.         //is the format adopted?
  429.  
  430.     FieldPosition ignore(FieldPosition::DONT_CARE);
  431.     UnicodeString string;
  432.     Formattable testArgs1[] = {T_INT32(0), "MyDisk"};    
  433.     form->format(testArgs1, 2, string, ignore, err);
  434.     if (string != "The disk \"MyDisk\" contains no files.") {
  435.         errln("TestMessageFormat::testMsgFormatChoice failed on test #1");
  436.     }
  437.  
  438.     ignore.setField(FieldPosition::DONT_CARE);
  439.     string.remove();
  440.     Formattable testArgs2[] = {T_INT32(1), "MyDisk"};    
  441.     form->format(testArgs2, 2, string, ignore, err);
  442.     if (string != "The disk \"MyDisk\" contains one file.") {
  443.         errln("TestMessageFormat::testMsgFormatChoice failed on test #2");
  444.     }
  445.  
  446.     ignore.setField(FieldPosition::DONT_CARE);
  447.     string.remove();
  448.     Formattable testArgs3[] = {T_INT32(1273), "MyDisk"};    
  449.     form->format(testArgs3, 2, string, ignore, err);
  450.     if (string != "The disk \"MyDisk\" contains 1,273 files.") {
  451.         errln("TestMessageFormat::testMsgFormatChoice failed on test #3");
  452.     }
  453.  
  454.     delete form;
  455.     delete fileform;
  456. }
  457.  
  458.  
  459. //---------------------------------
  460. //  API Tests
  461. //---------------------------------
  462.  
  463. void TestMessageFormat::testCopyConstructor() 
  464. {
  465.     logln("TestMessageFormat::testCopyConstructor");
  466.     UErrorCode success = U_ZERO_ERROR;
  467.     MessageFormat *x = new MessageFormat("There are {0} files on {1}", success);
  468.     MessageFormat *z = new MessageFormat("There are {0} files on {1} created", success);
  469.     MessageFormat *y = 0;
  470.     y = new MessageFormat(*x);
  471.     if ( (*x == *y) && 
  472.          (*x != *z) && 
  473.          (*y != *z) )
  474.          it_out << "First test (operator ==): Passed!\n";
  475.     else {
  476.         it_errln( "TestMessageFormat::testCopyConstructor failed #1");
  477.         it_out << "First test (operator ==): Failed!\n";
  478.     }
  479.     if ( ((*x == *y) && (*y == *x)) &&
  480.          ((*x != *z) && (*z != *x)) &&
  481.          ((*y != *z) && (*z != *y)) )
  482.         it_out << "Second test (equals): Passed!\n";
  483.     else {
  484.         it_errln( "TestMessageFormat::testCopyConstructor failed #2");
  485.         it_out << "Second test (equals): Failed!\n";
  486.     }
  487.  
  488.     delete x;
  489.     delete y;
  490.     delete z;
  491. }
  492.  
  493.  
  494. void TestMessageFormat::testAssignment() 
  495. {
  496.     logln("TestMessageFormat::testAssignment");
  497.     UErrorCode success = U_ZERO_ERROR;
  498.     MessageFormat *x = new MessageFormat("There are {0} files on {1}", success);
  499.     MessageFormat *z = new MessageFormat("There are {0} files on {1} created", success);
  500.     MessageFormat *y = new MessageFormat("There are {0} files on {1} created", success);
  501.     *y = *x;
  502.     if ( (*x == *y) && 
  503.          (*x != *z) && 
  504.          (*y != *z) )
  505.          it_out << "First test (operator ==): Passed!\n";
  506.     else {
  507.         it_errln( "TestMessageFormat::testAssignment failed #1");
  508.         it_out << "First test (operator ==): Failed!\n";
  509.     }
  510.     if ( ((*x == *y) && (*y == *x)) &&
  511.          ((*x != *z) && (*z != *x)) &&
  512.          ((*y != *z) && (*z != *y)) )
  513.         it_out << "Second test (equals): Passed!\n";
  514.     else {
  515.         it_errln( "TestMessageFormat::testAssignment failed #2");
  516.         it_out << "Second test (equals): Failed!\n";
  517.     }
  518.  
  519.     delete x;
  520.     delete y;
  521.     delete z;
  522. }
  523.  
  524. void TestMessageFormat::testClone() 
  525. {
  526.     logln("TestMessageFormat::testClone");
  527.     UErrorCode success = U_ZERO_ERROR;
  528.     MessageFormat *x = new MessageFormat("There are {0} files on {1}", success);
  529.     MessageFormat *z = new MessageFormat("There are {0} files on {1} created", success);
  530.     MessageFormat *y = 0;
  531.     y = (MessageFormat*)x->clone();
  532.     if ( (*x == *y) && 
  533.          (*x != *z) && 
  534.          (*y != *z) )
  535.          it_out << "First test (operator ==): Passed!\n";
  536.     else {
  537.         it_errln( "TestMessageFormat::testClone failed #1");
  538.         it_out << "First test (operator ==): Failed!\n";
  539.     }
  540.     if ( ((*x == *y) && (*y == *x)) &&
  541.          ((*x != *z) && (*z != *x)) &&
  542.          ((*y != *z) && (*z != *y)) )
  543.         it_out << "Second test (equals): Passed!\n";
  544.     else {
  545.         it_errln( "TestMessageFormat::testClone failed #2");
  546.         it_out << "Second test (equals): Failed!\n";
  547.     }
  548.  
  549.     delete x;
  550.     delete y;
  551.     delete z;
  552. }
  553.  
  554. void TestMessageFormat::testEquals() 
  555. {
  556.     logln("TestMessageFormat::testClone");
  557.     UErrorCode success = U_ZERO_ERROR;
  558.     MessageFormat x("There are {0} files on {1}", success);
  559.     MessageFormat y("There are {0} files on {1}", success);
  560.     if (!(x == y)) {
  561.         it_errln( "TestMessageFormat::testEquals failed #1");
  562.         it_out << "First test (operator ==): Failed!\n";
  563.     }
  564.  
  565. }
  566.  
  567. void TestMessageFormat::testNotEquals() 
  568. {
  569.     UErrorCode success = U_ZERO_ERROR;
  570.     MessageFormat x("There are {0} files on {1}", success);
  571.     MessageFormat y(x);
  572.     y.setLocale(Locale("fr"));
  573.     if (!(x != y)) {
  574.         it_errln( "TestMessageFormat::testEquals failed #1");
  575.         it_out << "First test (operator !=): Failed!\n";
  576.     }
  577.     y = x;
  578.     y.applyPattern("There are {0} files on {1} the disk", success);
  579.     if (!(x != y)) {
  580.         it_errln( "TestMessageFormat::testEquals failed #1");
  581.         it_out << "First test (operator !=): Failed!\n";
  582.     }
  583. }
  584.  
  585.  
  586. void TestMessageFormat::testSetLocale()
  587. {
  588.     UErrorCode err = U_ZERO_ERROR;
  589.     GregorianCalendar cal(err);   
  590.     Formattable arguments[] = {
  591.         456.83,
  592.         Formattable(UDate(8.71068e+011), Formattable::kIsDate),
  593.         "deposit"
  594.         };
  595.    
  596.     UnicodeString result;
  597.  
  598.     //UnicodeString formatStr = "At {1,time} on {1,date}, you made a {2} of {0,number,currency}.";
  599.     UnicodeString formatStr = "At <time> on {1,date}, you made a {2} of {0,number,currency}.";
  600.     // {sfb} to get $, would need Locale::US, not Locale::ENGLISH
  601.     // Just use unlocalized currency symbol.
  602.     //UnicodeString compareStrEng = "At <time> on Aug 8, 1997, you made a deposit of $456.83.";
  603.     UnicodeString compareStrEng = "At <time> on Aug 8, 1997, you made a deposit of ";
  604.     compareStrEng += (UChar) 0x00a4;
  605.     compareStrEng += "456.83.";
  606.     // {sfb} to get DM, would need Locale::GERMANY, not Locale::GERMAN
  607.     // Just use unlocalized currency symbol.
  608.     //UnicodeString compareStrGer = "At <time> on 08.08.1997, you made a deposit of 456,83 DM.";
  609.     UnicodeString compareStrGer = "At <time> on 08.08.1997, you made a deposit of ";
  610.     compareStrGer += (UChar) 0x00a4;
  611.     compareStrGer += " 456,83.";
  612.  
  613.     MessageFormat msg( formatStr, err);
  614.     result = "";
  615.     FieldPosition pos(0);
  616.     result = msg.format(
  617.         arguments,
  618.         3,
  619.         result,
  620.         pos,
  621.         err);
  622.  
  623.     logln(result);
  624.     if (result != compareStrEng) {
  625.         it_errln("***  MSG format err.");
  626.     }
  627.  
  628.     msg.setLocale(Locale::ENGLISH);
  629.     bool_t getLocale_ok = TRUE;
  630.     if (msg.getLocale() != Locale::ENGLISH) {
  631.         it_errln("*** MSG getLocal err.");
  632.         getLocale_ok = FALSE;
  633.     }
  634.  
  635.     msg.setLocale(Locale::GERMAN);
  636.  
  637.     if (msg.getLocale() != Locale::GERMAN) {
  638.         it_errln("*** MSG getLocal err.");
  639.         getLocale_ok = FALSE;
  640.     }
  641.  
  642.     msg.applyPattern( formatStr, err);
  643.  
  644.     pos.setField(0);
  645.     result = "";
  646.     result = msg.format(
  647.         arguments,
  648.         3,
  649.         result,
  650.         pos,
  651.         err);
  652.  
  653.     logln(result);
  654.     if (result == compareStrGer) {
  655.         it_out << "MSG setLocale tested." << endl;
  656.     }else{
  657.         it_errln( "*** MSG setLocale err.");
  658.     }
  659.  
  660.     if (getLocale_ok) { 
  661.         it_out << "MSG getLocale tested." << endl;
  662.     }
  663. }
  664.  
  665. void TestMessageFormat::testFormat()
  666. {
  667.     UErrorCode err = U_ZERO_ERROR;
  668.     GregorianCalendar cal(err);   
  669.  
  670.     const Formattable ftarray[] = 
  671.     {
  672.         Formattable( UDate(8.71068e+011), Formattable::kIsDate )
  673.     };
  674.     const int32_t ft_cnt = sizeof(ftarray) / sizeof(Formattable);
  675.     Formattable ft_arr( ftarray, ft_cnt );
  676.  
  677.     Formattable* fmt = new Formattable(UDate(8.71068e+011), Formattable::kIsDate);
  678.    
  679.     UnicodeString result;
  680.  
  681.     //UnicodeString formatStr = "At {1,time} on {1,date}, you made a {2} of {0,number,currency}.";
  682.     UnicodeString formatStr = "On {0,date}, it began.";
  683.     UnicodeString compareStr = "On Aug 8, 1997, it began.";
  684.  
  685.     err = U_ZERO_ERROR;
  686.     MessageFormat msg( formatStr, err);
  687.     FieldPosition fp(0);
  688.  
  689.     result = "";
  690.     fp = 0;
  691.     result = msg.format(
  692.         *fmt,
  693.         result,
  694.         //FieldPosition(0),
  695.         fp,
  696.         err);
  697.  
  698.     if (err != U_ILLEGAL_ARGUMENT_ERROR) {
  699.         it_errln("*** MSG format without expected error code.");
  700.     }
  701.     err = U_ZERO_ERROR;
  702.  
  703.     result = "";
  704.     fp = 0;
  705.     result = msg.format(
  706.         ft_arr,
  707.         result,
  708.         //FieldPosition(0),
  709.         fp,
  710.         err);
  711.  
  712.     it_out << "MSG format( Formattable&, ... ) expected:" << compareStr << endl;
  713.     it_out << "MSG format( Formattable&, ... )   result:" << result << endl;
  714.     if (result != compareStr) {
  715.         it_errln("***  MSG format( Formattable&, .... ) err.");
  716.     }else{
  717.         it_out << "MSG format( Formattable&, ... ) tested." << endl;
  718.     }
  719.  
  720.     delete fmt;
  721.  
  722. }
  723.  
  724. void TestMessageFormat::testParse()
  725. {
  726.     UErrorCode err = U_ZERO_ERROR;
  727.     int32_t count;
  728.     UnicodeString msgFormatString = "{0} =sep= {1}";
  729.     MessageFormat msg( msgFormatString, err);
  730.     UnicodeString source = "abc =sep= def";
  731.     UnicodeString tmp1, tmp2;
  732.  
  733.     Formattable* fmt_arr = msg.parse( source, count, err );
  734.     if (U_FAILURE(err) || (!fmt_arr)) {
  735.         it_errln("*** MSG parse (ustring, count, err) error.");
  736.     }else{
  737.         it_out << "MSG parse -- count: " << count << endl;
  738.         if (count != 2) {
  739.             it_errln("*** MSG parse (ustring, count, err) count err.");
  740.         }else{
  741.             if ((fmt_arr[0].getType() == Formattable::kString)
  742.              && (fmt_arr[1].getType() == Formattable::kString)
  743.              && (fmt_arr[0].getString(tmp1) == "abc")
  744.              && (fmt_arr[1].getString(tmp2) == "def")) {
  745.                 it_out << "MSG parse (ustring, count, err) tested." << endl;
  746.             }else{
  747.                 it_errln("*** MSG parse (ustring, count, err) result err.");
  748.             }
  749.         }
  750.     }
  751.     delete[] fmt_arr;
  752.  
  753.     ParsePosition pp(0);
  754.  
  755.     fmt_arr = msg.parse( source, pp, count );
  756.     if ((pp == 0) || (!fmt_arr)) {
  757.         it_errln("*** MSG parse (ustring, parsepos., count) error.");
  758.     }else{
  759.         it_out << "MSG parse -- count: " << count << endl;
  760.         if (count != 2) {
  761.             it_errln("*** MSG parse (ustring, parsepos., count) count err.");
  762.         }else{
  763.             if ((fmt_arr[0].getType() == Formattable::kString)
  764.              && (fmt_arr[1].getType() == Formattable::kString)
  765.              && (fmt_arr[0].getString(tmp1) == "abc")
  766.              && (fmt_arr[1].getString(tmp2) == "def")) {
  767.                 it_out << "MSG parse (ustring, parsepos., count) tested." << endl;
  768.             }else{
  769.                 it_errln("*** MSG parse (ustring, parsepos., count) result err.");
  770.             }
  771.         }
  772.     }
  773.     delete[] fmt_arr;
  774.  
  775.     pp = 0;
  776.     Formattable fmta;
  777.  
  778.     msg.parseObject( source, fmta, pp );
  779.     if (pp == 0) {
  780.         it_errln("*** MSG parse (ustring, Formattable, parsepos ) error.");
  781.     }else{
  782.         it_out << "MSG parse -- count: " << count << endl;
  783.         const Formattable* tmpfmt = fmta.getArray( count );
  784.         if (count != 2) {
  785.             it_errln("*** MSG parse (ustring, Formattable, parsepos ) count err.");
  786.         }else{
  787.             if ((fmta[0].getType() == Formattable::kString)
  788.              && (fmta[1].getType() == Formattable::kString)
  789.              && (fmta[0].getString(tmp1) == "abc")
  790.              && (fmta[1].getString(tmp2) == "def")) {
  791.                 it_out << "MSG parse (ustring, Formattable, parsepos ) tested." << endl;
  792.             }else{
  793.                 it_errln("*** MSG parse (ustring, Formattable, parsepos ) result err.");
  794.             }
  795.         }
  796.     }
  797. }
  798.  
  799.  
  800. void TestMessageFormat::testAdopt()
  801. {
  802.     UErrorCode err = U_ZERO_ERROR;
  803.  
  804.     UnicodeString formatStr = "{0,date},{1},{2,number}";
  805.     UnicodeString formatStrChange = "{0,number},{1,number},{2,date}";
  806.     err = U_ZERO_ERROR;
  807.     MessageFormat msg( formatStr, err);
  808.     MessageFormat msgCmp( formatStr, err);
  809.     int32_t count, countCmp;
  810.     const Format** formats = msg.getFormats(count);
  811.     const Format** formatsCmp = msgCmp.getFormats(countCmp);
  812.     const Format** formatsChg = 0;
  813.     const Format** formatsAct = 0;
  814.     int32_t countAct;
  815.     const Format* a;
  816.     const Format* b;
  817.     UnicodeString patCmp;
  818.     UnicodeString patAct;
  819.     Format** formatsToAdopt;
  820.  
  821.     bool_t ok = FALSE;
  822.     if (!formats || !formatsCmp || (count <= 0) || (count != countCmp)) {
  823.         goto endtest;
  824.     }
  825.  
  826.     int32_t i;
  827.  
  828.     bool_t equal;
  829.     equal = TRUE;
  830.     for (i = 0; i < count; i++) {
  831.         a = formats[i];
  832.         b = formatsCmp[i];
  833.         if ((a != NULL) && (b != NULL)) {
  834.             if (*a != *b) {
  835.                 equal = FALSE;
  836.             }
  837.         }else if ((a != NULL) || (b != NULL)) {
  838.             equal = FALSE;
  839.         }
  840.     }
  841.     if (!equal) goto endtest;
  842.  
  843.     msg.applyPattern( formatStrChange, err ); //set msg formats to something different
  844.     int32_t countChg;
  845.     formatsChg = msg.getFormats(countChg); // tested function
  846.     if (!formatsChg || (countChg != count)) goto endtest;
  847.  
  848.     bool_t diff;
  849.     diff = TRUE;
  850.     for (i = 0; i < count; i++) {
  851.         a = formatsChg[i];
  852.         b = formatsCmp[i];
  853.         if ((a != NULL) && (b != NULL)) {
  854.             if (*a == *b) {
  855.                 it_out << "formatsChg != formatsCmp at index " << i << endl;
  856.                 diff = FALSE;
  857.             }
  858.         }
  859.     }
  860.     if (!diff) { it_errln("*** MSG getFormats err."); goto endtest; }
  861.  
  862.     it_out << "MSG getFormats tested." << endl;
  863.  
  864.     msg.setFormats( formatsCmp, countCmp ); //tested function
  865.  
  866.     formatsAct = msg.getFormats(countAct);
  867.     if (!formatsAct || (countAct <=0) || (countAct != countCmp)) goto endtest;
  868.  
  869.     
  870. #if 1
  871.     msgCmp.toPattern( patCmp );
  872.     it_out << "MSG patCmp: " << patCmp << endl;
  873.     msg.toPattern( patAct );
  874.     it_out << "MSG patAct: " << patAct << endl;
  875. #endif
  876.  
  877.     equal = TRUE;//=
  878.     for (i = 0; i < countAct; i++) {
  879.         a = formatsAct[i];
  880.         b = formatsCmp[i];
  881.         if ((a != NULL) && (b != NULL)) {
  882.             if (*a != *b) {
  883.                 it_out << "formatsAct != formatsCmp at index " << i << endl;
  884.                 equal = FALSE;
  885.             }
  886.         }else if ((a != NULL) || (b != NULL)) {
  887.             equal = FALSE;
  888.         }
  889.     }
  890.     if (equal) {
  891.         it_out << "MSG setFormats tested." << endl;
  892.     }else{
  893.         it_errln("*** MSG setFormats err.");
  894.     }
  895.  
  896.  
  897.     //----
  898.  
  899.     msg.applyPattern( formatStrChange, err ); //set msg formats to something different
  900.  
  901.     formatsToAdopt = new Format* [countCmp];
  902.     if (!formatsToAdopt) goto endtest;
  903.  
  904.     for (i = 0; i < countCmp; i++) {
  905.         if (formatsCmp[i] == NULL) {
  906.             formatsToAdopt[i] = NULL;
  907.         }else{
  908.             formatsToAdopt[i] = formatsCmp[i]->clone();
  909.             if (!formatsToAdopt[i]) goto endtest;
  910.         }
  911.     }
  912.     msg.adoptFormats( formatsToAdopt, countCmp ); // function to test
  913.     delete[] formatsToAdopt;
  914.  
  915. #if 1
  916.     msgCmp.toPattern( patCmp );
  917.     it_out << "MSG patCmp: " << patCmp << endl;
  918.     msg.toPattern( patAct );
  919.     it_out << "MSG patAct: " << patAct << endl;
  920. #endif
  921.  
  922.     formatsAct = msg.getFormats(countAct);
  923.     if (!formatsAct || (countAct <=0) || (countAct != countCmp)) goto endtest;
  924.  
  925.     equal = TRUE;
  926.     for (i = 0; i < countAct; i++) {
  927.         a = formatsAct[i];
  928.         b = formatsCmp[i];
  929.         if ((a != NULL) && (b != NULL)) {
  930.             if (*a != *b) {
  931.                 equal = FALSE;
  932.             }
  933.         }else if ((a != NULL) || (b != NULL)) {
  934.             equal = FALSE;
  935.         }
  936.     }
  937.     if (equal) {
  938.         it_out << "MSG adoptFormats tested." << endl;
  939.     }else{
  940.         it_errln("*** MSG adoptFormats err.");
  941.     }
  942.  
  943.     //---- adoptFormat
  944.  
  945.     msg.applyPattern( formatStrChange, err ); //set msg formats to something different
  946.  
  947.     formatsToAdopt = new Format* [countCmp];
  948.     if (!formatsToAdopt) goto endtest;
  949.  
  950.     for (i = 0; i < countCmp; i++) {
  951.         if (formatsCmp[i] == NULL) {
  952.             formatsToAdopt[i] = NULL;
  953.         }else{
  954.             formatsToAdopt[i] = formatsCmp[i]->clone();
  955.             if (!formatsToAdopt[i]) goto endtest;
  956.         }
  957.     }
  958.  
  959.     for ( i = 0; i < countCmp; i++ ) {
  960.         msg.adoptFormat( i, formatsToAdopt[i] ); // function to test
  961.     }
  962.     delete[] formatsToAdopt; // array itself not needed in this case;
  963.  
  964. #if 1
  965.     msgCmp.toPattern( patCmp );
  966.     it_out << "MSG patCmp: " << patCmp << endl;
  967.     msg.toPattern( patAct );
  968.     it_out << "MSG patAct: " << patAct << endl;
  969. #endif
  970.  
  971.     formatsAct = msg.getFormats(countAct);
  972.     if (!formatsAct || (countAct <=0) || (countAct != countCmp)) goto endtest;
  973.  
  974.     equal = TRUE;
  975.     for (i = 0; i < countAct; i++) {
  976.         a = formatsAct[i];
  977.         b = formatsCmp[i];
  978.         if ((a != NULL) && (b != NULL)) {
  979.             if (*a != *b) {
  980.                 equal = FALSE;
  981.             }
  982.         }else if ((a != NULL) || (b != NULL)) {
  983.             equal = FALSE;
  984.         }
  985.     }
  986.     if (equal) {
  987.         it_out << "MSG adoptFormat tested." << endl;
  988.     }else{
  989.         it_errln("*** MSG adoptFormat err.");
  990.     }
  991.  
  992.     // ===============
  993.  
  994.     ok = TRUE; // doesn't mean no error, only that no additional error msg is necessary.
  995.  
  996. endtest:
  997.     if (!ok) it_errln("*** MSG testAdopt err.");
  998. }
  999.  
  1000. // This test is a regression test for a fixed bug in the copy constructor.
  1001. // It is kept as a global function rather than as a method since the test depends on memory values.
  1002. // (At least before the bug was fixed, whether it showed up or not depended on memory contents,
  1003. // which is probably why it didn't show up in the regular test for the copy constructor.)
  1004. // For this reason, the test isn't changed even though it contains function calls whose results are
  1005. // not tested and had no problems. Actually, the test failed by *crashing*.
  1006. static void testCopyConstructor2()
  1007. {
  1008.     UErrorCode status = U_ZERO_ERROR;
  1009.     UnicodeString formatStr("Hello World on {0,date,full}");
  1010.     UnicodeString resultStr(" ");
  1011.     UnicodeString result;
  1012.     FieldPosition fp(0);
  1013.     UDate d = Calendar::getNow();
  1014.     const Formattable fargs( d, Formattable::kIsDate );
  1015.  
  1016.     MessageFormat* fmt1 = new MessageFormat( formatStr, status );
  1017.     MessageFormat* fmt2 = new MessageFormat( *fmt1 );
  1018.     MessageFormat* fmt3;
  1019.     MessageFormat* fmt4;
  1020.  
  1021.     if (fmt1 == NULL) it_err("testCopyConstructor2: (fmt1 != NULL)");
  1022.  
  1023.     result = fmt1->format( &fargs, 1, resultStr, fp, status );
  1024.  
  1025.     if (fmt2 == NULL) it_err("testCopyConstructor2: (fmt2 != NULL)");
  1026.  
  1027.     fmt3 = (MessageFormat*) fmt1->clone();
  1028.     fmt4 = (MessageFormat*) fmt2->clone();
  1029.  
  1030.     if (fmt3 == NULL) it_err("testCopyConstructor2: (fmt3 != NULL)");
  1031.     if (fmt4 == NULL) it_err("testCopyConstructor2: (fmt4 != NULL)");
  1032.  
  1033.     result = fmt1->format( &fargs, 1, resultStr, fp, status );
  1034.     result = fmt2->format( &fargs, 1, resultStr, fp, status );
  1035.     result = fmt3->format( &fargs, 1, resultStr, fp, status );
  1036.     result = fmt4->format( &fargs, 1, resultStr, fp, status );
  1037. }
  1038.  
  1039.  
  1040. void TestMessageFormat::runIndexedTest( int32_t index, bool_t exec, char* &name, char* parm )
  1041. {
  1042.     if (exec) logln("TestSuite MessageFormat");
  1043.  
  1044.     switch (index) {
  1045.         case 0:  name = "testBug1";             if (exec) testBug1(); break;
  1046.         case 1:  name = "testBug2";             if (exec) testBug2(); break;
  1047.         case 2:  name = "sample";               if (exec) sample(); break;
  1048.         case 3:  name = "PatternTest";          if (exec) PatternTest(); break;
  1049.         case 4:  name = "testStaticFormat";     if (exec) testStaticFormat(parm); break;
  1050.         case 5:  name = "testSimpleFormat";     if (exec) testSimpleFormat(parm); break;
  1051.         case 6:  name = "testMsgFormatChoice";  if (exec) testMsgFormatChoice(parm); break;
  1052.  
  1053.         case 7:  name = "testCopyConstructor";  if (exec) testCopyConstructor(); break;
  1054.         case 8:  name = "testAssignment";       if (exec) testAssignment(); break;
  1055.         case 9:  name = "testClone";            if (exec) testClone(); break;
  1056.         case 10: name = "testEquals";           if (exec) testEquals(); break;
  1057.         case 11: name = "testNotEquals";        if (exec) testNotEquals(); break;
  1058.         case 12: name = "testSetLocale";        if (exec) testSetLocale(); break;
  1059.         case 13: name = "testFormat";           if (exec) testFormat(); break;
  1060.         case 14: name = "testParse";            if (exec) testParse(); break;
  1061.         case 15: name = "testAdopt";            if (exec) testAdopt(); break;
  1062.         case 16: name = "testCopyConstructor2"; if (exec) testCopyConstructor2(); break;
  1063.  
  1064.  
  1065.         default: name = ""; break; //needed to end loop
  1066.     }
  1067. }
  1068.