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

  1. /*
  2. ********************************************************************
  3. * COPYRIGHT: 
  4. * (C) Copyright International Business Machines Corporation, 1998
  5. * Licensed Material - Program-Property of IBM - All Rights Reserved. 
  6. * US Government Users Restricted Rights - Use, duplication, or disclosure 
  7. * restricted by GSA ADP Schedule Contract with IBM Corp. 
  8. *
  9. ********************************************************************
  10. */
  11.  
  12. #include "msfmrgts.h"
  13.  
  14. #include "format.h"
  15. #include "decimfmt.h"
  16. #include "locid.h"
  17. #include "msgfmt.h"
  18. #include "numfmt.h"
  19. #include "choicfmt.h"
  20. #include "gregocal.h"
  21.  
  22. // *****************************************************************************
  23. // class MessageFormatRegressionTest
  24. // *****************************************************************************
  25.  
  26. #define CASE(id,test) case id: name = #test; if (exec) { logln(#test "---"); logln((UnicodeString)""); test(); } break;
  27.  
  28. void 
  29. MessageFormatRegressionTest::runIndexedTest( int32_t index, bool_t exec, char* &name, char* par )
  30. {
  31.     // if (exec) logln((UnicodeString)"TestSuite MessageFormatRegressionTest");
  32.     switch (index) {
  33.         CASE(0,Test4074764)
  34.         CASE(1,Test4058973)
  35.         CASE(2,Test4031438)
  36.         CASE(3,Test4052223)
  37.         CASE(4,Test4104976)
  38.         CASE(5,Test4106659)
  39.         CASE(6,Test4106660)
  40.         CASE(7,Test4111739)
  41.         CASE(8,Test4114743)
  42.         CASE(9,Test4116444)
  43.         CASE(10,Test4114739)
  44.         CASE(11,Test4113018)
  45.         CASE(12,Test4106661)
  46.         CASE(13,Test4094906)
  47.         CASE(14,Test4118592)
  48.         CASE(15,Test4118594)
  49.         CASE(16,Test4105380)
  50.         CASE(17,Test4120552)
  51.         CASE(18,Test4142938)
  52.         CASE(19,TestChoicePatternQuote)
  53.         CASE(20,Test4112104);
  54.  
  55.         default: name = ""; break;
  56.     }
  57. }
  58.  
  59. bool_t 
  60. MessageFormatRegressionTest::failure(UErrorCode status, const char* msg)
  61. {
  62.     if(U_FAILURE(status)) {
  63.         errln(UnicodeString("FAIL: ") + msg + " failed, error " + errorName(status));
  64.         return TRUE;
  65.     }
  66.  
  67.     return FALSE;
  68. }
  69.  
  70. /* @bug 4074764
  71.  * Null exception when formatting pattern with MessageFormat
  72.  * with no parameters.
  73.  */
  74. void MessageFormatRegressionTest::Test4074764() {
  75.     UnicodeString pattern [] = {
  76.         "Message without param",
  77.         "Message with param:{0}",
  78.         "Longer Message with param {0}"
  79.     };
  80.     //difference between the two param strings are that
  81.     //in the first one, the param position is within the
  82.     //length of the string without param while it is not so
  83.     //in the other case.
  84.  
  85.     UErrorCode status = U_ZERO_ERROR;
  86.     MessageFormat *messageFormatter = new MessageFormat("", status);
  87.  
  88.     failure(status, "couldn't create MessageFormat");
  89.  
  90.     //try {
  91.         //Apply pattern with param and print the result
  92.         messageFormatter->applyPattern(pattern[1], status);
  93.         failure(status, "messageFormat->applyPattern");
  94.         //Object[] params = {new UnicodeString("BUG"), new Date()};
  95.         Formattable params [] = {
  96.             Formattable(UnicodeString("BUG")), 
  97.             Formattable(0, Formattable::kIsDate)
  98.         };
  99.         UnicodeString tempBuffer;
  100.         FieldPosition pos(FieldPosition::DONT_CARE);
  101.         tempBuffer = messageFormatter->format(params, 2, tempBuffer, pos, status);
  102.         if( tempBuffer != "Message with param:BUG" || failure(status, "messageFormat->format"))
  103.             errln("MessageFormat with one param test failed.");
  104.         logln("Formatted with one extra param : " + tempBuffer);
  105.  
  106.         //Apply pattern without param and print the result
  107.         messageFormatter->applyPattern(pattern[0], status);
  108.         failure(status, "messageFormatter->applyPattern");
  109.         
  110.         // {sfb} how much does this apply in C++?
  111.         // do we want to verify that the Formattable* array is not NULL,
  112.         // or is that the user's responsibility?
  113.         // additionally, what should be the item count?
  114.         // for bug testing purposes, assume that something was set to
  115.         // NULL by mistake, and that the length should be non-zero
  116.         
  117.         //tempBuffer = messageFormatter->format(NULL, 1, tempBuffer, FieldPosition(FieldPosition::DONT_CARE), status);
  118.         tempBuffer.remove();
  119.         tempBuffer = messageFormatter->format(NULL, 0, tempBuffer, pos, status);
  120.  
  121.         if( tempBuffer != "Message without param" || failure(status, "messageFormat->format"))
  122.             errln("MessageFormat with no param test failed.");
  123.         logln("Formatted with no params : " + tempBuffer);
  124.  
  125.         tempBuffer.remove();
  126.         tempBuffer = messageFormatter->format(params, 2, tempBuffer, pos, status);
  127.          if (tempBuffer != "Message without param" || failure(status, "messageFormat->format"))
  128.             errln("Formatted with arguments > subsitution failed. result = " + tempBuffer);
  129.          logln("Formatted with extra params : " + tempBuffer);
  130.         //This statement gives an exception while formatting...
  131.         //If we use pattern[1] for the message with param,
  132.         //we get an NullPointerException in MessageFormat.java(617)
  133.         //If we use pattern[2] for the message with param,
  134.         //we get an StringArrayIndexOutOfBoundsException in MessageFormat.java(614)
  135.         //Both are due to maxOffset not being reset to -1
  136.         //in applyPattern() when the pattern does not
  137.         //contain any param.
  138.     /*} catch (Exception foo) {
  139.         errln("Exception when formatting with no params.");
  140.     }*/
  141.  
  142.     delete messageFormatter;
  143. }
  144.  
  145. /* @bug 4058973
  146.  * MessageFormat.toPattern has weird rounding behavior.
  147.  */
  148. void MessageFormatRegressionTest::Test4058973() 
  149. {
  150.     UErrorCode status = U_ZERO_ERROR;
  151.     MessageFormat *fmt = new MessageFormat("{0,choice,0#no files|1#one file|1< {0,number,integer} files}", status);
  152.     failure(status, "new MessageFormat");
  153.  
  154.     UnicodeString pat;
  155.     pat = fmt->toPattern(pat);
  156.     UnicodeString exp("{0,choice,0.0#no files|1.0#one file|1.0< {0,number,integer} files}");
  157.     if (pat != exp) {
  158.         errln("MessageFormat.toPattern failed");
  159.         errln("Exp: " + exp);
  160.         errln("Got: " + pat);
  161.     }
  162.  
  163.     delete fmt;
  164. }
  165. /* @bug 4031438
  166.  * More robust message formats.
  167.  */
  168. void MessageFormatRegressionTest::Test4031438() 
  169. {
  170.     UErrorCode status = U_ZERO_ERROR;
  171.     
  172.     UnicodeString pattern1("Impossible {1} has occurred -- status code is {0} and message is {2}.");
  173.     UnicodeString pattern2("Double '' Quotes {0} test and quoted '{1}' test plus 'other {2} stuff'.");
  174.  
  175.     MessageFormat *messageFormatter = new MessageFormat("", status);
  176.     failure(status, "new MessageFormat");
  177.  
  178.     //try {
  179.         logln("Apply with pattern : " + pattern1);
  180.         messageFormatter->applyPattern(pattern1, status);
  181.         failure(status, "messageFormat->applyPattern");
  182.         //Object[] params = {new Integer(7)};
  183.         Formattable params []= {
  184.             Formattable((int32_t)7)
  185.         };
  186.         UnicodeString tempBuffer;
  187.         FieldPosition pos(FieldPosition::DONT_CARE);
  188.         tempBuffer = messageFormatter->format(params, 1, tempBuffer, pos, status);
  189.         if(tempBuffer != "Impossible {1} has occurred -- status code is 7 and message is {2}." || failure(status, "MessageFormat::format"))
  190.             errln("Tests arguments < substitution failed");
  191.         logln("Formatted with 7 : " + tempBuffer);
  192.         ParsePosition pp(0);
  193.         int32_t count = 0;
  194.         Formattable *objs = messageFormatter->parse(tempBuffer, pp, count);
  195.         //if(objs[7/*params.length*/] != NULL)
  196.         //    errln("Parse failed with more than expected arguments");
  197.  
  198.         NumberFormat *fmt = 0;
  199.         UnicodeString temp, temp1;
  200.         
  201.         for (int i = 0; i < count; i++) {
  202.             
  203.             // convert to string if not already
  204.             Formattable obj = objs[i];
  205.             temp.remove();
  206.             if(obj.getType() == Formattable::kString)
  207.                 temp = obj.getString(temp);
  208.             else {
  209.                 fmt = NumberFormat::createInstance(status);
  210.                 fmt->format(obj.getType() == Formattable::kLong ? obj.getLong() : obj.getDouble(), temp);
  211.             }
  212.  
  213.             // convert to string if not already
  214.             Formattable obj1 = params[i];
  215.             temp1.remove();
  216.             if(obj1.getType() == Formattable::kDouble || obj1.getType() == Formattable::kLong) {
  217.                 fmt = NumberFormat::createInstance(status);
  218.                 fmt->format(obj1.getType() == Formattable::kLong ? obj1.getLong() : obj1.getDouble(), temp1);
  219.             }
  220.             else
  221.                 temp1 = obj1.getString(temp1);
  222.  
  223.             //if (objs[i] != NULL && objs[i].getString(temp1) != params[i].getString(temp2)) {
  224.             if (temp != temp1) {
  225.                 errln("Parse failed on object " + objs[i].getString(temp1) + " at index : " + i);
  226.             }
  227.         
  228.         }
  229.  
  230.         delete fmt;
  231.         delete [] objs;
  232.  
  233.         // {sfb} does this apply?  no way to really pass a null Formattable, 
  234.         // only a null array
  235.  
  236.         /*tempBuffer = messageFormatter->format(null, tempBuffer, FieldPosition(FieldPosition::DONT_CARE), status);
  237.         if (tempBuffer != "Impossible {1} has occurred -- status code is {0} and message is {2}." || failure(status, "messageFormat->format"))
  238.             errln("Tests with no arguments failed");
  239.         logln("Formatted with null : " + tempBuffer);*/
  240.         logln("Apply with pattern : " + pattern2);
  241.         messageFormatter->applyPattern(pattern2, status);
  242.         failure(status, "messageFormatter->applyPattern");
  243.         tempBuffer.remove();
  244.         tempBuffer = messageFormatter->format(params, 1, tempBuffer, pos, status);
  245.         if (tempBuffer != "Double ' Quotes 7 test and quoted {1} test plus other {2} stuff.")
  246.             errln("quote format test (w/ params) failed.");
  247.         logln("Formatted with params : " + tempBuffer);
  248.         
  249.         /*tempBuffer = messageFormatter->format(null);
  250.         if (!tempBuffer.equals("Double ' Quotes {0} test and quoted {1} test plus other {2} stuff."))
  251.             errln("quote format test (w/ null) failed.");
  252.         logln("Formatted with null : " + tempBuffer);
  253.         logln("toPattern : " + messageFormatter.toPattern());*/
  254.     /*} catch (Exception foo) {
  255.         errln("Exception when formatting in bug 4031438. "+foo.getMessage());
  256.     }*/
  257. }
  258.  
  259. void MessageFormatRegressionTest::Test4052223()
  260. {
  261.  
  262.     ParsePosition pos(0);
  263.     if (pos.getErrorIndex() != -1) {
  264.         errln("ParsePosition.getErrorIndex initialization failed.");
  265.     }
  266.  
  267.     UErrorCode status = U_ZERO_ERROR;
  268.     MessageFormat *fmt = new MessageFormat("There are {0} apples growing on the {1} tree.", status);
  269.     failure(status, "new MessageFormat");
  270.     UnicodeString str("There is one apple growing on the peach tree.");
  271.     
  272.     int32_t count = 0;
  273.     Formattable *objs = fmt->parse(str, pos, count);
  274.  
  275.     logln(UnicodeString("unparsable string , should fail at ") + pos.getErrorIndex());
  276.     if (pos.getErrorIndex() == -1)
  277.         errln("Bug 4052223 failed : parsing string " + str);
  278.     pos.setErrorIndex(4);
  279.     if (pos.getErrorIndex() != 4)
  280.         errln(UnicodeString("setErrorIndex failed, got ") + pos.getErrorIndex() + " instead of 4");
  281.     
  282.     ChoiceFormat *f = new ChoiceFormat(
  283.         "-1#are negative|0#are no or fraction|1#is one|1.0<is 1+|2#are two|2<are more than 2.", status);
  284.     failure(status, "new ChoiceFormat");
  285.     pos.setIndex(0); 
  286.     pos.setErrorIndex(-1);
  287.     Formattable obj;
  288.     f->parse("are negative", obj, pos);
  289.     if (pos.getErrorIndex() != -1 && obj.getDouble() == -1.0)
  290.         errln(UnicodeString("Parse with \"are negative\" failed, at ") + pos.getErrorIndex());
  291.     pos.setIndex(0); 
  292.     pos.setErrorIndex(-1);
  293.     f->parse("are no or fraction ", obj, pos);
  294.     if (pos.getErrorIndex() != -1 && obj.getDouble() == 0.0)
  295.         errln(UnicodeString("Parse with \"are no or fraction\" failed, at ") + pos.getErrorIndex());
  296.     pos.setIndex(0); 
  297.     pos.setErrorIndex(-1);
  298.     f->parse("go postal", obj, pos);
  299.     if (pos.getErrorIndex() == -1 && ! icu_isNaN(obj.getDouble()))
  300.         errln(UnicodeString("Parse with \"go postal\" failed, at ") + pos.getErrorIndex());
  301.     
  302.     delete fmt;
  303.     delete f;
  304. }
  305. /* @bug 4104976
  306.  * ChoiceFormat.equals(null) throws NullPointerException
  307.  */
  308.  
  309. // {sfb} not really applicable in C++?? (kind of silly)
  310.  
  311. void MessageFormatRegressionTest::Test4104976()
  312. {
  313.     double limits [] = {1, 20};
  314.     UnicodeString formats [] = {
  315.         UnicodeString("xyz"), 
  316.         UnicodeString("abc")
  317.     };
  318.     UErrorCode status = U_ZERO_ERROR;
  319.     ChoiceFormat *cf = new ChoiceFormat(limits, formats, status);
  320.     failure(status, "new ChoiceFormat");
  321.     //try {
  322.         log("Compares to null is always false, returned : ");
  323.         logln(cf == NULL ? "TRUE" : "FALSE");
  324.     /*} catch (Exception foo) {
  325.         errln("ChoiceFormat.equals(null) throws exception.");
  326.     }*/
  327.  
  328.     delete cf;
  329. }
  330.  
  331. /* @bug 4106659
  332.  * ChoiceFormat.ctor(double[], String[]) doesn't check
  333.  * whether lengths of input arrays are equal.
  334.  */
  335.  
  336. // {sfb} again, not really applicable in C++
  337.  
  338. void MessageFormatRegressionTest::Test4106659()
  339. {
  340.     /*
  341.     double limits [] = {
  342.         1, 2, 3
  343.     };
  344.     UnicodeString formats [] = {
  345.         "one", "two"
  346.     };
  347.     ChoiceFormat *cf = NULL;
  348.     //try {
  349.     //    cf = new ChoiceFormat(limits, formats, 3);
  350.     //} catch (Exception foo) {
  351.     //    logln("ChoiceFormat constructor should check for the array lengths");
  352.     //    cf = null;
  353.     //}
  354.     //if (cf != null) 
  355.     //    errln(cf->format(5));
  356.     //
  357.     delete cf;
  358.     */
  359. }
  360.  
  361. /* @bug 4106660
  362.  * ChoiceFormat.ctor(double[], String[]) allows unordered double array.
  363.  * This is not a bug, added javadoc to emphasize the use of limit
  364.  * array must be in ascending order.
  365.  */
  366. void MessageFormatRegressionTest::Test4106660()
  367. {
  368.     double limits [] = {3, 1, 2};
  369.     UnicodeString formats [] = {
  370.         UnicodeString("Three"), 
  371.             UnicodeString("One"), 
  372.             UnicodeString("Two")
  373.     };
  374.     ChoiceFormat *cf = new ChoiceFormat(limits, formats, 3);
  375.     double d = 5.0;
  376.     UnicodeString str;
  377.     FieldPosition pos(FieldPosition::DONT_CARE);
  378.     str = cf->format(d, str, pos);
  379.     if (str != "Two")
  380.         errln( (UnicodeString) "format(" + d + ") = " + str);
  381.  
  382.     delete cf;
  383. }
  384.  
  385. /* @bug 4111739
  386.  * MessageFormat is incorrectly serialized/deserialized.
  387.  */
  388.  
  389. // {sfb} doesn't apply in C++
  390.  
  391. void MessageFormatRegressionTest::Test4111739()
  392. {
  393.     /*MessageFormat format1 = null;
  394.     MessageFormat format2 = null;
  395.     ObjectOutputStream ostream = null;
  396.     ByteArrayOutputStream baos = null;
  397.     ObjectInputStream istream = null;
  398.  
  399.     try {
  400.         baos = new ByteArrayOutputStream();
  401.         ostream = new ObjectOutputStream(baos);
  402.     } catch(IOException e) {
  403.         errln("Unexpected exception : " + e.getMessage());
  404.         return;
  405.     }
  406.  
  407.     try {
  408.         format1 = new MessageFormat("pattern{0}");
  409.         ostream.writeObject(format1);
  410.         ostream.flush();
  411.  
  412.         byte bytes[] = baos.toByteArray();
  413.  
  414.         istream = new ObjectInputStream(new ByteArrayInputStream(bytes));
  415.         format2 = (MessageFormat)istream.readObject();
  416.     } catch(Exception e) {
  417.         errln("Unexpected exception : " + e.getMessage());
  418.     }
  419.  
  420.     if (!format1.equals(format2)) {
  421.         errln("MessageFormats before and after serialization are not" +
  422.             " equal\nformat1 = " + format1 + "(" + format1.toPattern() + ")\nformat2 = " +
  423.             format2 + "(" + format2.toPattern() + ")");
  424.     } else {
  425.         logln("Serialization for MessageFormat is OK.");
  426.     }*/
  427. }
  428. /* @bug 4114743
  429.  * MessageFormat.applyPattern allows illegal patterns.
  430.  */
  431. void MessageFormatRegressionTest::Test4114743()
  432. {
  433.     UnicodeString originalPattern("initial pattern");
  434.     UErrorCode status = U_ZERO_ERROR;
  435.     MessageFormat *mf = new MessageFormat(originalPattern, status);
  436.     failure(status, "new MessageFormat");
  437.     //try {
  438.         UnicodeString illegalPattern("ab { '}' de");
  439.         mf->applyPattern(illegalPattern, status);
  440.         if( ! U_FAILURE(status))
  441.             errln("illegal pattern: \"" + illegalPattern + "\"");
  442.     /*} catch (IllegalArgumentException foo) {
  443.         if (!originalPattern.equals(mf.toPattern()))
  444.             errln("pattern after: \"" + mf.toPattern() + "\"");
  445.     }*/
  446. }
  447.  
  448. /* @bug 4116444
  449.  * MessageFormat.parse has different behavior in case of null.
  450.  */
  451. void MessageFormatRegressionTest::Test4116444()
  452. {
  453.     UnicodeString patterns [] = {
  454.         (UnicodeString)"", 
  455.         (UnicodeString)"one", 
  456.         (UnicodeString) "{0,date,short}"
  457.     };
  458.     
  459.     UErrorCode status = U_ZERO_ERROR;    
  460.     MessageFormat *mf = new MessageFormat("", status);
  461.     failure(status, "new MessageFormat");
  462.  
  463.     for (int i = 0; i < 3; i++) {
  464.         UnicodeString pattern = patterns[i];
  465.         mf->applyPattern(pattern, status);
  466.         failure(status, "mf->applyPattern");
  467.  
  468.         //try {
  469.         int32_t count = 0;    
  470.         ParsePosition pp(0);
  471.         Formattable *array = mf->parse(UnicodeString(""), pp, count);
  472.             logln("pattern: \"" + pattern + "\"");
  473.             log(" parsedObjects: ");
  474.             if (array != NULL) {
  475.                 log("{");
  476.                 for (int j = 0; j < count; j++) {
  477.                     //if (array[j] != null)
  478.                     UnicodeString dummy;
  479.                     err("\"" + array[j].getString(dummy) + "\"");
  480.                     //else
  481.                      //   log("null");
  482.                     if (j < count- 1) 
  483.                         log(",");
  484.                 }
  485.                 log("}") ;
  486.             } else {
  487.                 log("null");
  488.             }
  489.             logln("");
  490.         /*} catch (Exception e) {
  491.             errln("pattern: \"" + pattern + "\"");
  492.             errln("  Exception: " + e.getMessage());
  493.         }*/
  494.     }
  495.  
  496.     delete mf;
  497. }
  498. /* @bug 4114739 (FIX and add javadoc)
  499.  * MessageFormat.format has undocumented behavior about empty format objects.
  500.  */
  501.  
  502. // {sfb} doesn't apply in C++?
  503. void MessageFormatRegressionTest::Test4114739()
  504. {
  505.  
  506.     UErrorCode status = U_ZERO_ERROR;    
  507.     MessageFormat *mf = new MessageFormat("<{0}>", status);
  508.     failure(status, "new MessageFormat");
  509.  
  510.     Formattable *objs1 = NULL;
  511.     //Formattable objs2 [] = {};
  512.     //Formattable *objs3 [] = {NULL};
  513.     //try {
  514.     UnicodeString pat;
  515.     UnicodeString res;
  516.         logln("pattern: \"" + mf->toPattern(pat) + "\"");
  517.         log("format(null) : ");
  518.         FieldPosition pos(FieldPosition::DONT_CARE);
  519.         logln("\"" + mf->format(objs1, 0, res, pos, status) + "\"");
  520.         failure(status, "mf->format");
  521.         /*log("format({})   : ");
  522.         logln("\"" + mf->format(objs2, 0, res, FieldPosition(FieldPosition::DONT_CARE), status) + "\"");
  523.         failure(status, "mf->format");
  524.         log("format({null}) :");
  525.         logln("\"" + mf->format(objs3, 0, res, FieldPosition(FieldPosition::DONT_CARE), status) + "\"");
  526.         failure(status, "mf->format");*/
  527.     /*} catch (Exception e) {
  528.         errln("Exception thrown for null argument tests.");
  529.     }*/
  530.  
  531.     delete mf;
  532. }
  533.  
  534. /* @bug 4113018
  535.  * MessageFormat.applyPattern works wrong with illegal patterns.
  536.  */
  537. void MessageFormatRegressionTest::Test4113018()
  538. {
  539.     UnicodeString originalPattern("initial pattern");
  540.     UErrorCode status = U_ZERO_ERROR;
  541.     MessageFormat *mf = new MessageFormat(originalPattern, status);
  542.     failure(status, "new messageFormat");
  543.     UnicodeString illegalPattern("format: {0, xxxYYY}");
  544.     UnicodeString pat;
  545.     logln("pattern before: \"" + mf->toPattern(pat) + "\"");
  546.     logln("illegal pattern: \"" + illegalPattern + "\"");
  547.     //try {
  548.         mf->applyPattern(illegalPattern, status);
  549.         if( ! U_FAILURE(status))
  550.             errln("Should have thrown IllegalArgumentException for pattern : " + illegalPattern);
  551.     /*} catch (IllegalArgumentException e) {
  552.         if (!originalPattern.equals(mf.toPattern()))
  553.             errln("pattern after: \"" + mf.toPattern() + "\"");
  554.     }*/
  555.     delete mf;
  556. }
  557.  
  558. /* @bug 4106661
  559.  * ChoiceFormat is silent about the pattern usage in javadoc.
  560.  */
  561. void MessageFormatRegressionTest::Test4106661()
  562. {
  563.     UErrorCode status = U_ZERO_ERROR;
  564.     ChoiceFormat *fmt = new ChoiceFormat(
  565.       "-1#are negative| 0#are no or fraction | 1#is one |1.0<is 1+ |2#are two |2<are more than 2.", status);
  566.     failure(status, "new ChoiceFormat");
  567.     UnicodeString pat;
  568.     logln("Formatter Pattern : " + fmt->toPattern(pat));
  569.  
  570.     FieldPosition bogus(FieldPosition::DONT_CARE);
  571.     UnicodeString str;
  572.  
  573.     // Will this work for -inf?
  574.     logln("Format with -INF : " + fmt->format(Formattable(-icu_getInfinity()), str, bogus, status));
  575.     failure(status, "fmt->format");
  576.     str.remove();
  577.     logln("Format with -1.0 : " + fmt->format(Formattable(-1.0), str, bogus, status));
  578.     failure(status, "fmt->format");
  579.     str.remove();
  580.     logln("Format with -1.0 : " + fmt->format(Formattable(-1.0), str, bogus, status));
  581.     failure(status, "fmt->format");
  582.     str.remove();
  583.     logln("Format with 0 : " + fmt->format(Formattable((int32_t)0), str, bogus, status));
  584.     failure(status, "fmt->format");
  585.     str.remove();
  586.     logln("Format with 0.9 : " + fmt->format(Formattable(0.9), str, bogus, status));
  587.     failure(status, "fmt->format");
  588.     str.remove();
  589.     logln("Format with 1.0 : " + fmt->format(Formattable(1.0), str, bogus, status));
  590.     failure(status, "fmt->format");
  591.     str.remove();
  592.     logln("Format with 1.5 : " + fmt->format(Formattable(1.5), str, bogus, status));
  593.     failure(status, "fmt->format");
  594.     str.remove();
  595.     logln("Format with 2 : " + fmt->format(Formattable((int32_t)2), str, bogus, status));
  596.     failure(status, "fmt->format");
  597.     str.remove();
  598.     logln("Format with 2.1 : " + fmt->format(Formattable(2.1), str, bogus, status));
  599.     failure(status, "fmt->format");
  600.     str.remove();
  601.     logln("Format with NaN : " + fmt->format(Formattable(icu_getNaN()), str, bogus, status));
  602.     failure(status, "fmt->format");
  603.     str.remove();
  604.     logln("Format with +INF : " + fmt->format(Formattable(icu_getInfinity()), str, bogus, status));
  605.     failure(status, "fmt->format");
  606.  
  607.     delete fmt;
  608. }
  609.  
  610. /* @bug 4094906
  611.  * ChoiceFormat should accept \u221E as eq. to INF.
  612.  */
  613. void MessageFormatRegressionTest::Test4094906()
  614. {
  615.     UErrorCode status = U_ZERO_ERROR;
  616.     UnicodeString pattern("-");
  617.     pattern += (UChar) 0x221E;
  618.     pattern += "<are negative|0.0<are no or fraction|1.0#is one|1.0<is 1+|";
  619.     pattern += (UChar) 0x221E;
  620.     pattern += "<are many.";
  621.  
  622.     ChoiceFormat *fmt = new ChoiceFormat(pattern, status);
  623.     failure(status, "new ChoiceFormat");
  624.     UnicodeString pat;
  625.     if (fmt->toPattern(pat) != pattern) {
  626.         errln( (UnicodeString) "Formatter Pattern : " + pat);
  627.         errln( (UnicodeString) "Expected Pattern  : " + pattern);
  628.     }
  629.     FieldPosition bogus(FieldPosition::DONT_CARE);
  630.     UnicodeString str;
  631.  
  632.     // Will this work for -inf?
  633.     logln("Format with -INF : " + fmt->format(Formattable(-icu_getInfinity()), str, bogus, status));
  634.     failure(status, "fmt->format");
  635.     str.remove();
  636.     logln("Format with -1.0 : " + fmt->format(Formattable(-1.0), str, bogus, status));
  637.     failure(status, "fmt->format");
  638.     str.remove();
  639.     logln("Format with -1.0 : " + fmt->format(Formattable(-1.0), str, bogus, status));
  640.     failure(status, "fmt->format");
  641.     str.remove();
  642.     logln("Format with 0 : " + fmt->format(Formattable((int32_t)0), str, bogus, status));
  643.     failure(status, "fmt->format");
  644.     str.remove();
  645.     logln("Format with 0.9 : " + fmt->format(Formattable(0.9), str, bogus, status));
  646.     failure(status, "fmt->format");
  647.     str.remove();
  648.     logln("Format with 1.0 : " + fmt->format(Formattable(1.0), str, bogus, status));
  649.     failure(status, "fmt->format");
  650.     str.remove();
  651.     logln("Format with 1.5 : " + fmt->format(Formattable(1.5), str, bogus, status));
  652.     failure(status, "fmt->format");
  653.     str.remove();
  654.     logln("Format with 2 : " + fmt->format(Formattable((int32_t)2), str, bogus, status));
  655.     failure(status, "fmt->format");
  656.     str.remove();
  657.     logln("Format with 2.1 : " + fmt->format(Formattable(2.1), str, bogus, status));
  658.     failure(status, "fmt->format");
  659.     str.remove();
  660.     logln("Format with NaN : " + fmt->format(Formattable(icu_getNaN()), str, bogus, status));
  661.     failure(status, "fmt->format");
  662.     str.remove();
  663.     logln("Format with +INF : " + fmt->format(Formattable(icu_getInfinity()), str, bogus, status));
  664.     failure(status, "fmt->format");
  665.  
  666.     delete fmt;
  667. }
  668.  
  669. /* @bug 4118592
  670.  * MessageFormat.parse fails with ChoiceFormat.
  671.  */
  672. void MessageFormatRegressionTest::Test4118592()
  673. {
  674.     UErrorCode status = U_ZERO_ERROR;
  675.     MessageFormat *mf = new MessageFormat("", status);
  676.     failure(status, "new messageFormat");
  677.     UnicodeString pattern("{0,choice,1#YES|2#NO}");
  678.     UnicodeString prefix("");
  679.     Formattable *objs = 0;
  680.  
  681.     for (int i = 0; i < 5; i++) {
  682.         UnicodeString formatted;
  683.         formatted = prefix + "YES";
  684.         mf->applyPattern(prefix + pattern, status);
  685.         failure(status, "mf->applyPattern");
  686.         prefix += "x";
  687.         //Object[] objs = mf.parse(formatted, new ParsePosition(0));
  688.         int32_t count = 0;
  689.         ParsePosition pp(0);
  690.         objs = mf->parse(formatted, pp, count);
  691.         UnicodeString pat;
  692.         logln(UnicodeString("") + i + ". pattern :\"" + mf->toPattern(pat) + "\"");
  693.         log(" \"" + formatted + "\" parsed as ");
  694.         if (objs == NULL) 
  695.             logln("  null");
  696.         else {
  697.             UnicodeString temp;
  698.             if(objs[0].getType() == Formattable::kString)
  699.                 logln((UnicodeString)"  " + objs[0].getString(temp));
  700.             else
  701.                 logln((UnicodeString)"  " + (objs[0].getType() == Formattable::kLong ? objs[0].getLong() : objs[0].getDouble()));
  702.  
  703.         }
  704.     }
  705.  
  706.     delete [] objs;
  707.     delete mf;
  708. }
  709. /* @bug 4118594
  710.  * MessageFormat.parse fails for some patterns.
  711.  */
  712. void MessageFormatRegressionTest::Test4118594()
  713. {
  714.     UErrorCode status = U_ZERO_ERROR;
  715.     MessageFormat *mf = new MessageFormat("{0}, {0}, {0}", status);
  716.     failure(status, "new MessageFormat");
  717.     UnicodeString forParsing("x, y, z");
  718.     //Object[] objs = mf.parse(forParsing, new ParsePosition(0));
  719.     int32_t count = 0;
  720.     ParsePosition pp(0);
  721.     Formattable *objs = mf->parse(forParsing, pp, count);
  722.     UnicodeString pat;
  723.     logln("pattern: \"" + mf->toPattern(pat) + "\"");
  724.     logln("text for parsing: \"" + forParsing + "\"");
  725.     UnicodeString str;
  726.     if (objs[0].getString(str) != "z")
  727.         errln("argument0: \"" + objs[0].getString(str) + "\"");
  728.     mf->applyPattern("{0,number,#.##}, {0,number,#.#}", status);
  729.     failure(status, "mf->applyPattern");
  730.     //Object[] oldobjs = {new Double(3.1415)};
  731.     Formattable oldobjs [] = {Formattable(3.1415)};
  732.     UnicodeString result;
  733.     FieldPosition pos(FieldPosition::DONT_CARE);
  734.     result = mf->format( oldobjs, 1, result, pos, status );
  735.     failure(status, "mf->format");
  736.     pat.remove();
  737.     logln("pattern: \"" + mf->toPattern(pat) + "\"");
  738.     logln("text for parsing: \"" + result + "\"");
  739.     // result now equals "3.14, 3.1"
  740.     if (result != "3.14, 3.1")
  741.         errln("result = " + result);
  742.     //Object[] newobjs = mf.parse(result, new ParsePosition(0));
  743.     int32_t count1 = 0;
  744.     pp.setIndex(0);
  745.     Formattable *newobjs = mf->parse(result, pp, count1);
  746.     // newobjs now equals {new Double(3.1)}
  747.     if (newobjs[0].getDouble() != 3.1)
  748.         errln( UnicodeString("newobjs[0] = ") + newobjs[0].getDouble());
  749.  
  750.     delete [] objs;
  751.     delete [] newobjs;
  752.     delete mf;
  753. }
  754. /* @bug 4105380
  755.  * When using ChoiceFormat, MessageFormat is not good for I18n.
  756.  */
  757. void MessageFormatRegressionTest::Test4105380()
  758. {
  759.     UnicodeString patternText1("The disk \"{1}\" contains {0}.");
  760.     UnicodeString patternText2("There are {0} on the disk \"{1}\"");
  761.     UErrorCode status = U_ZERO_ERROR;
  762.     MessageFormat *form1 = new MessageFormat(patternText1, status);
  763.     failure(status, "new MessageFormat");
  764.     MessageFormat *form2 = new MessageFormat(patternText2, status);
  765.     failure(status, "new MessageFormat");
  766.     double filelimits [] = {0,1,2};
  767.     UnicodeString filepart [] = {
  768.         (UnicodeString)"no files",
  769.             (UnicodeString)"one file",
  770.             (UnicodeString)"{0,number} files"
  771.     };
  772.     ChoiceFormat *fileform = new ChoiceFormat(filelimits, filepart, 3);
  773.     form1->setFormat(1, *fileform);
  774.     form2->setFormat(0, *fileform);
  775.     //Object[] testArgs = {new Long(12373), "MyDisk"};
  776.     Formattable testArgs [] = {
  777.         Formattable((int32_t)12373), 
  778.             Formattable((UnicodeString)"MyDisk")
  779.     };
  780.     
  781.     FieldPosition bogus(FieldPosition::DONT_CARE);
  782.  
  783.     UnicodeString result;
  784.     logln(form1->format(testArgs, 2, result, bogus, status));
  785.     failure(status, "form1->format");
  786.     result.remove();
  787.     logln(form2->format(testArgs, 2, result, bogus, status));
  788.     failure(status, "form1->format");
  789.  
  790.     delete form1;
  791.     delete form2;
  792.     delete fileform;
  793. }
  794. /* @bug 4120552
  795.  * MessageFormat.parse incorrectly sets errorIndex.
  796.  */
  797. void MessageFormatRegressionTest::Test4120552()
  798. {
  799.     UErrorCode status = U_ZERO_ERROR;
  800.     MessageFormat *mf = new MessageFormat("pattern", status);
  801.     failure(status, "new MessageFormat");
  802.     UnicodeString texts[] = {
  803.         (UnicodeString)"pattern", 
  804.             (UnicodeString)"pat", 
  805.             (UnicodeString)"1234"
  806.     };
  807.     UnicodeString pat;
  808.     logln("pattern: \"" + mf->toPattern(pat) + "\"");
  809.     for (int i = 0; i < 3; i++) {
  810.         ParsePosition pp(0);
  811.         //Object[] objs = mf.parse(texts[i], pp);
  812.         int32_t count = 0;
  813.         Formattable *objs = mf->parse(texts[i], pp, count);
  814.         log("  text for parsing: \"" + texts[i] + "\"");
  815.         if (objs == NULL) {
  816.             logln("  (incorrectly formatted string)");
  817.             if (pp.getErrorIndex() == -1)
  818.                 errln(UnicodeString("Incorrect error index: ") + pp.getErrorIndex());
  819.         } else {
  820.             logln("  (correctly formatted string)");
  821.         }
  822.     }
  823. }
  824.  
  825. /**
  826.  * @bug 4142938
  827.  * MessageFormat handles single quotes in pattern wrong.
  828.  * This is actually a problem in ChoiceFormat; it doesn't
  829.  * understand single quotes.
  830.  */
  831. void MessageFormatRegressionTest::Test4142938() 
  832. {
  833.     UnicodeString pat( (UnicodeString)"''Vous'' {0,choice,0#n''|1#}avez sélectionné " + 
  834.         "{0,choice,0#aucun|1#{0}} client{0,choice,0#s|1#|2#s} " + 
  835.         "personnel{0,choice,0#s|1#|2#s}.");
  836.     UErrorCode status = U_ZERO_ERROR;
  837.     MessageFormat *mf = new MessageFormat(pat, status);
  838.     failure(status, "new MessageFormat");
  839.  
  840.     UnicodeString PREFIX [] = {
  841.         (UnicodeString) "'Vous' n'avez sélectionné aucun clients personnels.",
  842.         (UnicodeString) "'Vous' avez sélectionné ",
  843.         (UnicodeString) "'Vous' avez sélectionné "
  844.     };  
  845.     UnicodeString SUFFIX [] = {
  846.         (UnicodeString) "",
  847.         (UnicodeString) " client personnel.",
  848.         (UnicodeString) " clients personnels."
  849.     };
  850.  
  851.     for (int i=0; i<3; i++) {
  852.         UnicodeString out;
  853.         //out = mf->format(new Object[]{new Integer(i)});
  854.         Formattable objs [] = {
  855.             Formattable((int32_t)i)
  856.         };
  857.         FieldPosition pos(FieldPosition::DONT_CARE);
  858.         out = mf->format(objs, 1, out, pos, status);
  859.         failure(status, "mf->format");
  860.         if (SUFFIX[i] == "") {
  861.             if (out != PREFIX[i])
  862.                 errln((UnicodeString)"" + i + ": Got \"" + out + "\"; Want \"" + PREFIX[i] + "\"");
  863.         }
  864.         else {
  865.             if (!out.startsWith(PREFIX[i]) ||
  866.                 !out.endsWith(SUFFIX[i]))
  867.                 errln((UnicodeString)"" + i + ": Got \"" + out + "\"; Want \"" + PREFIX[i] + "\"...\"" +
  868.                       SUFFIX[i] + "\"");
  869.         }
  870.     }
  871.  
  872.     delete mf;
  873. }
  874.  
  875. /**
  876.  * @bug 4142938
  877.  * Test the applyPattern and toPattern handling of single quotes
  878.  * by ChoiceFormat.  (This is in here because this was a bug reported
  879.  * against MessageFormat.)  The single quote is used to quote the
  880.  * pattern characters '|', '#', '<', and '\u2264'.  Two quotes in a row
  881.  * is a quote literal.
  882.  */
  883. void MessageFormatRegressionTest::TestChoicePatternQuote() 
  884. {
  885.     UnicodeString DATA [] = {
  886.         // Pattern                  0 value           1 value
  887.         // {sfb} hacked - changed \u2264 to = (copied from Character Map)
  888.         (UnicodeString)"0#can''t|1#can",           (UnicodeString)"can't",          (UnicodeString)"can",
  889.         (UnicodeString)"0#'pound(#)=''#'''|1#xyz", (UnicodeString)"pound(#)='#'",   (UnicodeString)"xyz",
  890.         (UnicodeString)"0#'1<2 | 1=1'|1#''",  (UnicodeString)"1<2 | 1=1", (UnicodeString)"'",
  891.     };
  892.     for (int i=0; i<9; i+=3) {
  893.         //try {
  894.             UErrorCode status = U_ZERO_ERROR;
  895.             ChoiceFormat *cf = new ChoiceFormat(DATA[i], status);
  896.             failure(status, "new ChoiceFormat");
  897.             for (int j=0; j<=1; ++j) {
  898.                 UnicodeString out;
  899.                 FieldPosition pos(FieldPosition::DONT_CARE);
  900.                 out = cf->format((double)j, out, pos);
  901.                 if (out != DATA[i+1+j])
  902.                     errln("Fail: Pattern \"" + DATA[i] + "\" x "+j+" -> " +
  903.                           out + "; want \"" + DATA[i+1+j] + '"');
  904.             }
  905.             UnicodeString pat;
  906.             pat = cf->toPattern(pat);
  907.             UnicodeString pat2;
  908.             ChoiceFormat *cf2 = new ChoiceFormat(pat, status);
  909.             pat2 = cf2->toPattern(pat2);
  910.             if (pat != pat2)
  911.                 errln("Fail: Pattern \"" + DATA[i] + "\" x toPattern -> \"" + pat + '"');
  912.             else
  913.                 logln("Ok: Pattern \"" + DATA[i] + "\" x toPattern -> \"" + pat + '"');
  914.         /*}
  915.         catch (IllegalArgumentException e) {
  916.             errln("Fail: Pattern \"" + DATA[i] + "\" -> " + e);
  917.         }*/
  918.     
  919.         delete cf;
  920.         delete cf2;
  921.     }
  922. }
  923.  
  924. /**
  925.  * @bug 4112104
  926.  * MessageFormat.equals(null) throws a NullPointerException.  The JLS states
  927.  * that it should return false.
  928.  */
  929. void MessageFormatRegressionTest::Test4112104() 
  930. {
  931.     UErrorCode status = U_ZERO_ERROR;
  932.     MessageFormat *format = new MessageFormat("", status);
  933.     failure(status, "new MessageFormat");
  934.     //try {
  935.         // This should NOT throw an exception
  936.         if (format == NULL) {
  937.             // It also should return false
  938.             errln("MessageFormat.equals(null) returns false");
  939.         }
  940.     /*}
  941.     catch (NullPointerException e) {
  942.         errln("MessageFormat.equals(null) throws " + e);
  943.     }*/
  944. }
  945.  
  946.