/* ******************************************************************************** * * * COPYRIGHT: * * (C) Copyright Taligent, Inc., 1997 * * (C) Copyright International Business Machines Corporation, 1997-1998 * * Licensed Material - Program-Property of IBM - All Rights Reserved. * * US Government Users Restricted Rights - Use, duplication, or disclosure * * restricted by GSA ADP Schedule Contract with IBM Corp. * * * ******************************************************************************** * * File TMSGFMT.CPP * * Modification History: * * Date Name Description * 03/24/97 helena Converted from Java. * 07/11/97 helena Updated to work on AIX. * 08/04/97 jfitz Updated to intltest ******************************************************************************** */ #include "tmsgfmt.h" #include "format.h" #include "decimfmt.h" #include "locid.h" #include "msgfmt.h" #include "numfmt.h" #include "choicfmt.h" #include "gregocal.h" Formattable TestMessageFormat::testArgs[] = { Formattable(double(1)), Formattable(double(3456)), Formattable("Disk"), Formattable(UDate(T_INT32(1000000000)), Formattable::kIsDate) }; UnicodeString TestMessageFormat::testCases[] = { "Quotes '', '{', 'a' {0} '{0}'", "Quotes '', '{', 'a' {0,number} '{0}'", "'{'1,number,'#',##} {1,number,'#',##}", "There are {1} files on {2} at {3}.", "On {2}, there are {1} files, with {0,number,currency}.", "'{1,number,percent}', {1,number,percent},", "'{1,date,full}', {1,date,full},", "'{3,date,full}', {3,date,full},", "'{1,number,#,##}' {1,number,#,##}", }; UnicodeString TestMessageFormat::testResultPatterns[] = { "Quotes '', '{', a {0} '{'0}", "Quotes '', '{', a {0,number} '{'0}", "'{'1,number,#,##} {1,number,'#'#,##}", "There are {1} files on {2} at {3}.", "On {2}, there are {1} files, with {0,number,currency}.", "'{'1,number,percent}, {1,number,percent},", "'{'1,date,full}, {1,date,full},", "'{'3,date,full}, {3,date,full},", "'{'1,number,#,##} {1,number,#,##}" }; UnicodeString TestMessageFormat::testResultStrings[] = { "Quotes ', {, a 1 {0}", "Quotes ', {, a 1 {0}", "{1,number,#,##} #34,56", "There are 3,456 files on Disk at 1/12/70 5:46 AM.", "On Disk, there are 3,456 files, with $1.00.", "{1,number,percent}, 345,600%,", "{1,date,full}, Wednesday, December 31, 1969,", "{3,date,full}, Monday, January 12, 1970,", "{1,number,#,##} 34,56" }; void TestMessageFormat::testBug3() { double myNumber = -123456; DecimalFormat *form = 0; Locale locale[] = { Locale("ar", "", ""), Locale("be", "", ""), Locale("bg", "", ""), Locale("ca", "", ""), Locale("cs", "", ""), Locale("da", "", ""), Locale("de", "", ""), Locale("de", "AT", ""), Locale("de", "CH", ""), Locale("el", "", ""), // 10 Locale("en", "CA", ""), Locale("en", "GB", ""), Locale("en", "IE", ""), Locale("en", "US", ""), Locale("es", "", ""), Locale("et", "", ""), Locale("fi", "", ""), Locale("fr", "", ""), Locale("fr", "BE", ""), Locale("fr", "CA", ""), // 20 Locale("fr", "CH", ""), Locale("he", "", ""), Locale("hr", "", ""), Locale("hu", "", ""), Locale("is", "", ""), Locale("it", "", ""), Locale("it", "CH", ""), Locale("ja", "", ""), Locale("ko", "", ""), Locale("lt", "", ""), // 30 Locale("lv", "", ""), Locale("mk", "", ""), Locale("nl", "", ""), Locale("nl", "BE", ""), Locale("no", "", ""), Locale("pl", "", ""), Locale("pt", "", ""), Locale("ro", "", ""), Locale("ru", "", ""), Locale("sh", "", ""), // 40 Locale("sk", "", ""), Locale("sl", "", ""), Locale("sq", "", ""), Locale("sr", "", ""), Locale("sv", "", ""), Locale("tr", "", ""), Locale("uk", "", ""), Locale("zh", "", ""), Locale("zh", "TW", "") }; // 49 int32_t i; for (i= 0; i < 49; i++) { UnicodeString buffer; it_out << locale[i].getDisplayName(buffer) << endl; UErrorCode success = U_ZERO_ERROR; // form = (DecimalFormat*)NumberFormat::createCurrencyInstance(locale[i], success); form = (DecimalFormat*)NumberFormat::createInstance(locale[i], success); if (U_FAILURE(success)) { it_errln("Err: Number Format "); it_out << "Number format creation failed.\n"; continue; } Formattable result; FieldPosition pos(0); buffer.remove(); form->format(myNumber, buffer, pos); success = U_ZERO_ERROR; ParsePosition parsePos; form->parse(buffer, result, parsePos); #ifdef _DEBUG it_out << " -> " /*<< dec*/ /*<< result*/ << "[supposed output for result]" <format(T_INT32(1.0), toAppendTo, status); if (toAppendTo != "1.0<=Arg<2.0") { it_errln("ChoiceFormat cmp in testBug1"); } it_out << toAppendTo << endl; delete cf; } void TestMessageFormat::testBug2() { UErrorCode status = U_ZERO_ERROR; UnicodeString result; // {sfb} use double format in pattern, so result will match (not strictly necessary) const UnicodeString pattern = "There {0,choice,0.0#are no files|1.0#is one file|1.0toPattern(result) << endl; if (pattern != result) { it_errln("MessageFormat::toPattern() failed.\n"); it_out << "MessageFormat::toPattern() failed.\n"; } delete fmt; } #ifdef _DEBUG //---------------------------------------------------- // console I/O //---------------------------------------------------- ostream& operator<<(ostream& stream, const Formattable& obj); #include "datefmt.h" #include #include #include #include #include IntlTest& operator<<( IntlTest& stream, const Formattable& obj) { static DateFormat *defDateFormat = 0; UnicodeString buffer; switch(obj.getType()) { case Formattable::kDate : if (defDateFormat == 0) { defDateFormat = DateFormat::createInstance(); } defDateFormat->format(obj.getDate(), buffer); stream << buffer; break; case Formattable::kDouble : char convert[20]; sprintf( convert, "%lf", obj.getDouble() ); stream << convert << "D"; break; case Formattable::kLong : stream << obj.getLong() << "L"; break; case Formattable::kString: stream << "\"" << obj.getString(buffer) << "\""; break; case Formattable::kArray: int32_t i, count; const Formattable* array; array = obj.getArray(count); stream << "["; for (i=0; itoPattern(buffer) != testResultPatterns[i]) { errln(UnicodeString("TestMessageFormat::PatternTest failed test #2, i = ") + i); //form->toPattern(buffer); it_out << " Orig: " << testCases[i] << endl; it_out << " Exp: " << testResultPatterns[i] << endl; it_out << " Got: " << buffer << endl; } //it_out << "Pat out: " << form->toPattern(buffer) << endl; UnicodeString result; int32_t count = 4; FieldPosition fieldpos(0); form->format(testArgs, count, result, fieldpos, success); if (U_FAILURE(success)) { it_errln ("MessageFormat failed test #3"); it_out << "TestMessageFormat::PatternTest failed test #3" << endl; continue; } if (result != testResultStrings[i]) { errln("TestMessageFormat::PatternTest failed test #4"); logln("TestMessageFormat::PatternTest failed #4."); logln(UnicodeString(" Result: ") + result ); logln(UnicodeString(" Expected: ") + testResultStrings[i] ); } //it_out << "Result: " << result << endl; #if 0 it_out << "---------------- test parse ----------------" << endl; form->toPattern(buffer); it_out << "MSG pattern for parse: " << buffer << endl; Formattable* values = form->parse(result, count, success); if (U_FAILURE(success)) { errln("MessageFormat failed test #5"); logln(UnicodeString("MessageFormat failed test #5 with error code ")+(int32_t)success); }else if (count != 4) { errln("MSG count not 4 (as expected)"); } bool_t failed = FALSE; for (int32_t j = 0; j < count; ++j) { if (values == 0 || testArgs[j] != values[j]) { it_out << "MSG testargs[" << j << "]: " << testArgs[j] << endl; it_out << "MSG values[" << j << "] : " << values[j] << endl; failed = TRUE; } } if (failed) errln("MessageFormat failed test #6"); #endif delete form; } } void TestMessageFormat::sample() { MessageFormat *form = 0; UnicodeString buffer1, buffer2; UErrorCode success = U_ZERO_ERROR; form = new MessageFormat("There are {0} files on {1}", success); if (U_FAILURE(success)) { it_errln("Err: Message format creation failed"); it_out << "Sample message format creation failed.\n"; return; } UnicodeString abc("abc"); UnicodeString def("def"); Formattable testArgs[] = { abc, def }; FieldPosition fieldpos(0); it_out << form->toPattern(buffer1) << "; " << form->format(testArgs, 2, buffer2, fieldpos, success) << endl; delete form; } void TestMessageFormat::testStaticFormat(char* parm) { logln("running TestMessageFormat::testStaticFormat"); UErrorCode err = U_ZERO_ERROR; GregorianCalendar cal(err); Formattable arguments[] = { T_INT32(7), Formattable(UDate(8.71068e+011), Formattable::kIsDate), "a disturbance in the Force" }; UnicodeString result; result = MessageFormat::format( "At {1,time} on {1,date}, there was {2} on planet {0,number,integer}.", arguments, 3, result, err); if (U_FAILURE(err)) { errln("TestMessageFormat::testStaticFormat #1"); logln(UnicodeString("TestMessageFormat::testStaticFormat failed test #1 with error code ")+(int32_t)err); return; } static const UnicodeString expected = "At 12:20:00 PM on Aug 8, 1997, there was a disturbance in the Force on planet 7."; if (result != expected) { errln("TestMessageFormat::testStaticFormat failed on test"); logln( UnicodeString(" Result: ") + result ); logln( UnicodeString(" Expected: ") + expected ); } } void TestMessageFormat::testSimpleFormat(char* parm) { logln("running TestMessageFormat::testSimpleFormat"); UErrorCode err = U_ZERO_ERROR; Formattable testArgs1[] = {T_INT32(0), "MyDisk"}; Formattable testArgs2[] = {T_INT32(1), "MyDisk"}; Formattable testArgs3[] = {T_INT32(12), "MyDisk"}; MessageFormat* form = new MessageFormat( "The disk \"{1}\" contains {0} file(s).", err); UnicodeString string; FieldPosition ignore(FieldPosition::DONT_CARE); form->format(testArgs1, 2, string, ignore, err); if (U_FAILURE(err) || string != "The disk \"MyDisk\" contains 0 file(s).") { errln(UnicodeString("TestMessageFormat::testSimpleFormat failed on test #1")); } ignore.setField(FieldPosition::DONT_CARE); string.remove(); form->format(testArgs2, 2, string, ignore, err); if (U_FAILURE(err) || string != "The disk \"MyDisk\" contains 1 file(s).") { logln(string); errln(UnicodeString("TestMessageFormat::testSimpleFormat failed on test #2")+string); } ignore.setField(FieldPosition::DONT_CARE); string.remove(); form->format(testArgs3, 2, string, ignore, err); if (U_FAILURE(err) || string != "The disk \"MyDisk\" contains 12 file(s).") { errln(UnicodeString("TestMessageFormat::testSimpleFormat failed on test #3")+string); } delete form; } void TestMessageFormat::testMsgFormatChoice(char* parm) { logln("running TestMessageFormat::testMsgFormatChoice"); UErrorCode err = U_ZERO_ERROR; MessageFormat* form = new MessageFormat("The disk \"{1}\" contains {0}.", err); double filelimits[] = {0,1,2}; UnicodeString filepart[] = {"no files","one file","{0,number} files"}; ChoiceFormat* fileform = new ChoiceFormat(filelimits, filepart, 3); form->setFormat(1,*fileform); // NOT zero, see below //is the format adopted? FieldPosition ignore(FieldPosition::DONT_CARE); UnicodeString string; Formattable testArgs1[] = {T_INT32(0), "MyDisk"}; form->format(testArgs1, 2, string, ignore, err); if (string != "The disk \"MyDisk\" contains no files.") { errln("TestMessageFormat::testMsgFormatChoice failed on test #1"); } ignore.setField(FieldPosition::DONT_CARE); string.remove(); Formattable testArgs2[] = {T_INT32(1), "MyDisk"}; form->format(testArgs2, 2, string, ignore, err); if (string != "The disk \"MyDisk\" contains one file.") { errln("TestMessageFormat::testMsgFormatChoice failed on test #2"); } ignore.setField(FieldPosition::DONT_CARE); string.remove(); Formattable testArgs3[] = {T_INT32(1273), "MyDisk"}; form->format(testArgs3, 2, string, ignore, err); if (string != "The disk \"MyDisk\" contains 1,273 files.") { errln("TestMessageFormat::testMsgFormatChoice failed on test #3"); } delete form; delete fileform; } //--------------------------------- // API Tests //--------------------------------- void TestMessageFormat::testCopyConstructor() { logln("TestMessageFormat::testCopyConstructor"); UErrorCode success = U_ZERO_ERROR; MessageFormat *x = new MessageFormat("There are {0} files on {1}", success); MessageFormat *z = new MessageFormat("There are {0} files on {1} created", success); MessageFormat *y = 0; y = new MessageFormat(*x); if ( (*x == *y) && (*x != *z) && (*y != *z) ) it_out << "First test (operator ==): Passed!\n"; else { it_errln( "TestMessageFormat::testCopyConstructor failed #1"); it_out << "First test (operator ==): Failed!\n"; } if ( ((*x == *y) && (*y == *x)) && ((*x != *z) && (*z != *x)) && ((*y != *z) && (*z != *y)) ) it_out << "Second test (equals): Passed!\n"; else { it_errln( "TestMessageFormat::testCopyConstructor failed #2"); it_out << "Second test (equals): Failed!\n"; } delete x; delete y; delete z; } void TestMessageFormat::testAssignment() { logln("TestMessageFormat::testAssignment"); UErrorCode success = U_ZERO_ERROR; MessageFormat *x = new MessageFormat("There are {0} files on {1}", success); MessageFormat *z = new MessageFormat("There are {0} files on {1} created", success); MessageFormat *y = new MessageFormat("There are {0} files on {1} created", success); *y = *x; if ( (*x == *y) && (*x != *z) && (*y != *z) ) it_out << "First test (operator ==): Passed!\n"; else { it_errln( "TestMessageFormat::testAssignment failed #1"); it_out << "First test (operator ==): Failed!\n"; } if ( ((*x == *y) && (*y == *x)) && ((*x != *z) && (*z != *x)) && ((*y != *z) && (*z != *y)) ) it_out << "Second test (equals): Passed!\n"; else { it_errln( "TestMessageFormat::testAssignment failed #2"); it_out << "Second test (equals): Failed!\n"; } delete x; delete y; delete z; } void TestMessageFormat::testClone() { logln("TestMessageFormat::testClone"); UErrorCode success = U_ZERO_ERROR; MessageFormat *x = new MessageFormat("There are {0} files on {1}", success); MessageFormat *z = new MessageFormat("There are {0} files on {1} created", success); MessageFormat *y = 0; y = (MessageFormat*)x->clone(); if ( (*x == *y) && (*x != *z) && (*y != *z) ) it_out << "First test (operator ==): Passed!\n"; else { it_errln( "TestMessageFormat::testClone failed #1"); it_out << "First test (operator ==): Failed!\n"; } if ( ((*x == *y) && (*y == *x)) && ((*x != *z) && (*z != *x)) && ((*y != *z) && (*z != *y)) ) it_out << "Second test (equals): Passed!\n"; else { it_errln( "TestMessageFormat::testClone failed #2"); it_out << "Second test (equals): Failed!\n"; } delete x; delete y; delete z; } void TestMessageFormat::testEquals() { logln("TestMessageFormat::testClone"); UErrorCode success = U_ZERO_ERROR; MessageFormat x("There are {0} files on {1}", success); MessageFormat y("There are {0} files on {1}", success); if (!(x == y)) { it_errln( "TestMessageFormat::testEquals failed #1"); it_out << "First test (operator ==): Failed!\n"; } } void TestMessageFormat::testNotEquals() { UErrorCode success = U_ZERO_ERROR; MessageFormat x("There are {0} files on {1}", success); MessageFormat y(x); y.setLocale(Locale("fr")); if (!(x != y)) { it_errln( "TestMessageFormat::testEquals failed #1"); it_out << "First test (operator !=): Failed!\n"; } y = x; y.applyPattern("There are {0} files on {1} the disk", success); if (!(x != y)) { it_errln( "TestMessageFormat::testEquals failed #1"); it_out << "First test (operator !=): Failed!\n"; } } void TestMessageFormat::testSetLocale() { UErrorCode err = U_ZERO_ERROR; GregorianCalendar cal(err); Formattable arguments[] = { 456.83, Formattable(UDate(8.71068e+011), Formattable::kIsDate), "deposit" }; UnicodeString result; //UnicodeString formatStr = "At {1,time} on {1,date}, you made a {2} of {0,number,currency}."; UnicodeString formatStr = "At