2001-10-10 21:43:11 +00:00
|
|
|
/*
|
|
|
|
*******************************************************************************
|
2009-06-12 19:34:21 +00:00
|
|
|
* Copyright (C) 1996-2009, International Business Machines Corporation and *
|
2001-10-10 21:43:11 +00:00
|
|
|
* others. All Rights Reserved. *
|
|
|
|
*******************************************************************************
|
|
|
|
*/
|
|
|
|
|
2002-09-21 00:43:14 +00:00
|
|
|
#include "unicode/utypes.h"
|
|
|
|
|
|
|
|
#if !UCONFIG_NO_FORMATTING
|
|
|
|
|
2001-10-10 21:43:11 +00:00
|
|
|
#include "itrbnfrt.h"
|
|
|
|
|
|
|
|
#include "unicode/fmtable.h"
|
2003-10-15 08:03:56 +00:00
|
|
|
#include <math.h> // fabs
|
|
|
|
#include <stdio.h>
|
2001-10-10 21:43:11 +00:00
|
|
|
|
|
|
|
// current macro not in icu1.8.1
|
|
|
|
#define TESTCASE(id,test) \
|
|
|
|
case id: \
|
|
|
|
name = #test; \
|
|
|
|
if (exec) { \
|
|
|
|
logln(#test "---"); \
|
|
|
|
logln((UnicodeString)""); \
|
|
|
|
test(); \
|
|
|
|
} \
|
|
|
|
break
|
|
|
|
|
2001-10-12 17:26:10 +00:00
|
|
|
void RbnfRoundTripTest::runIndexedTest(int32_t index, UBool exec, const char* &name, char* /*par*/)
|
2001-10-10 21:43:11 +00:00
|
|
|
{
|
|
|
|
if (exec) logln("TestSuite RuleBasedNumberFormatRT");
|
|
|
|
switch (index) {
|
2002-03-14 00:28:22 +00:00
|
|
|
#if U_HAVE_RBNF
|
2001-10-10 21:43:11 +00:00
|
|
|
TESTCASE(0, TestEnglishSpelloutRT);
|
|
|
|
TESTCASE(1, TestDurationsRT);
|
|
|
|
TESTCASE(2, TestSpanishSpelloutRT);
|
|
|
|
TESTCASE(3, TestFrenchSpelloutRT);
|
|
|
|
TESTCASE(4, TestSwissFrenchSpelloutRT);
|
|
|
|
TESTCASE(5, TestItalianSpelloutRT);
|
|
|
|
TESTCASE(6, TestGermanSpelloutRT);
|
|
|
|
TESTCASE(7, TestSwedishSpelloutRT);
|
|
|
|
TESTCASE(8, TestDutchSpelloutRT);
|
|
|
|
TESTCASE(9, TestJapaneseSpelloutRT);
|
|
|
|
TESTCASE(10, TestRussianSpelloutRT);
|
2006-11-09 00:51:18 +00:00
|
|
|
TESTCASE(11, TestPortugueseSpelloutRT);
|
2002-03-14 00:28:22 +00:00
|
|
|
#else
|
2002-04-02 02:55:31 +00:00
|
|
|
TESTCASE(0, TestRBNFDisabled);
|
2002-03-14 00:28:22 +00:00
|
|
|
#endif
|
2001-10-10 21:43:11 +00:00
|
|
|
default:
|
|
|
|
name = "";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-03-14 00:28:22 +00:00
|
|
|
#if U_HAVE_RBNF
|
|
|
|
|
2001-10-10 21:43:11 +00:00
|
|
|
/**
|
|
|
|
* Perform an exhaustive round-trip test on the English spellout rules
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
RbnfRoundTripTest::TestEnglishSpelloutRT()
|
|
|
|
{
|
|
|
|
UErrorCode status = U_ZERO_ERROR;
|
|
|
|
RuleBasedNumberFormat* formatter
|
2002-08-21 00:16:30 +00:00
|
|
|
= new RuleBasedNumberFormat(URBNF_SPELLOUT, Locale::getUS(), status);
|
2001-10-10 21:43:11 +00:00
|
|
|
|
|
|
|
if (U_FAILURE(status)) {
|
2009-06-12 19:34:21 +00:00
|
|
|
errcheckln(status, "failed to construct formatter - %s", u_errorName(status));
|
2001-10-10 21:43:11 +00:00
|
|
|
} else {
|
|
|
|
doTest(formatter, -12345678, 12345678);
|
|
|
|
}
|
|
|
|
delete formatter;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Perform an exhaustive round-trip test on the duration-formatting rules
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
RbnfRoundTripTest::TestDurationsRT()
|
|
|
|
{
|
|
|
|
UErrorCode status = U_ZERO_ERROR;
|
|
|
|
RuleBasedNumberFormat* formatter
|
2002-08-21 00:16:30 +00:00
|
|
|
= new RuleBasedNumberFormat(URBNF_DURATION, Locale::getUS(), status);
|
2001-10-10 21:43:11 +00:00
|
|
|
|
|
|
|
if (U_FAILURE(status)) {
|
2009-06-12 19:34:21 +00:00
|
|
|
errcheckln(status, "failed to construct formatter - %s", u_errorName(status));
|
2001-10-10 21:43:11 +00:00
|
|
|
} else {
|
|
|
|
doTest(formatter, 0, 12345678);
|
|
|
|
}
|
|
|
|
delete formatter;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Perform an exhaustive round-trip test on the Spanish spellout rules
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
RbnfRoundTripTest::TestSpanishSpelloutRT()
|
|
|
|
{
|
|
|
|
UErrorCode status = U_ZERO_ERROR;
|
|
|
|
RuleBasedNumberFormat* formatter
|
|
|
|
= new RuleBasedNumberFormat(URBNF_SPELLOUT, Locale("es", "es"), status);
|
|
|
|
|
|
|
|
if (U_FAILURE(status)) {
|
2009-06-12 19:34:21 +00:00
|
|
|
errcheckln(status, "failed to construct formatter - %s", u_errorName(status));
|
2001-10-10 21:43:11 +00:00
|
|
|
} else {
|
|
|
|
doTest(formatter, -12345678, 12345678);
|
|
|
|
}
|
|
|
|
delete formatter;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Perform an exhaustive round-trip test on the French spellout rules
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
RbnfRoundTripTest::TestFrenchSpelloutRT()
|
|
|
|
{
|
|
|
|
UErrorCode status = U_ZERO_ERROR;
|
|
|
|
RuleBasedNumberFormat* formatter
|
2002-03-26 23:18:57 +00:00
|
|
|
= new RuleBasedNumberFormat(URBNF_SPELLOUT, Locale::getFrance(), status);
|
2001-10-10 21:43:11 +00:00
|
|
|
|
|
|
|
if (U_FAILURE(status)) {
|
2009-06-12 19:34:21 +00:00
|
|
|
errcheckln(status, "failed to construct formatter - %s", u_errorName(status));
|
2001-10-10 21:43:11 +00:00
|
|
|
} else {
|
|
|
|
doTest(formatter, -12345678, 12345678);
|
|
|
|
}
|
|
|
|
delete formatter;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Perform an exhaustive round-trip test on the Swiss French spellout rules
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
RbnfRoundTripTest::TestSwissFrenchSpelloutRT()
|
|
|
|
{
|
|
|
|
UErrorCode status = U_ZERO_ERROR;
|
|
|
|
RuleBasedNumberFormat* formatter
|
|
|
|
= new RuleBasedNumberFormat(URBNF_SPELLOUT, Locale("fr", "CH"), status);
|
|
|
|
|
|
|
|
if (U_FAILURE(status)) {
|
2009-06-12 19:34:21 +00:00
|
|
|
errcheckln(status, "failed to construct formatter - %s", u_errorName(status));
|
2001-10-10 21:43:11 +00:00
|
|
|
} else {
|
|
|
|
doTest(formatter, -12345678, 12345678);
|
|
|
|
}
|
|
|
|
delete formatter;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Perform an exhaustive round-trip test on the Italian spellout rules
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
RbnfRoundTripTest::TestItalianSpelloutRT()
|
|
|
|
{
|
|
|
|
UErrorCode status = U_ZERO_ERROR;
|
|
|
|
RuleBasedNumberFormat* formatter
|
2002-08-21 00:16:30 +00:00
|
|
|
= new RuleBasedNumberFormat(URBNF_SPELLOUT, Locale::getItalian(), status);
|
2001-10-10 21:43:11 +00:00
|
|
|
|
|
|
|
if (U_FAILURE(status)) {
|
2009-06-12 19:34:21 +00:00
|
|
|
errcheckln(status, "failed to construct formatter - %s", u_errorName(status));
|
2001-10-10 21:43:11 +00:00
|
|
|
} else {
|
|
|
|
doTest(formatter, -999999, 999999);
|
|
|
|
}
|
|
|
|
delete formatter;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Perform an exhaustive round-trip test on the German spellout rules
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
RbnfRoundTripTest::TestGermanSpelloutRT()
|
|
|
|
{
|
|
|
|
UErrorCode status = U_ZERO_ERROR;
|
|
|
|
RuleBasedNumberFormat* formatter
|
2002-03-26 23:18:57 +00:00
|
|
|
= new RuleBasedNumberFormat(URBNF_SPELLOUT, Locale::getGermany(), status);
|
2001-10-10 21:43:11 +00:00
|
|
|
|
|
|
|
if (U_FAILURE(status)) {
|
2009-06-12 19:34:21 +00:00
|
|
|
errcheckln(status, "failed to construct formatter - %s", u_errorName(status));
|
2001-10-10 21:43:11 +00:00
|
|
|
} else {
|
|
|
|
doTest(formatter, 0, 12345678);
|
|
|
|
}
|
|
|
|
delete formatter;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Perform an exhaustive round-trip test on the Swedish spellout rules
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
RbnfRoundTripTest::TestSwedishSpelloutRT()
|
|
|
|
{
|
|
|
|
UErrorCode status = U_ZERO_ERROR;
|
|
|
|
RuleBasedNumberFormat* formatter
|
|
|
|
= new RuleBasedNumberFormat(URBNF_SPELLOUT, Locale("sv", "SE"), status);
|
|
|
|
|
|
|
|
if (U_FAILURE(status)) {
|
2009-06-12 19:34:21 +00:00
|
|
|
errcheckln(status, "failed to construct formatter - %s", u_errorName(status));
|
2001-10-10 21:43:11 +00:00
|
|
|
} else {
|
|
|
|
doTest(formatter, 0, 12345678);
|
|
|
|
}
|
|
|
|
delete formatter;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Perform an exhaustive round-trip test on the Dutch spellout rules
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
RbnfRoundTripTest::TestDutchSpelloutRT()
|
|
|
|
{
|
|
|
|
UErrorCode status = U_ZERO_ERROR;
|
|
|
|
RuleBasedNumberFormat* formatter
|
|
|
|
= new RuleBasedNumberFormat(URBNF_SPELLOUT, Locale("nl", "NL"), status);
|
|
|
|
|
|
|
|
if (U_FAILURE(status)) {
|
2009-06-12 19:34:21 +00:00
|
|
|
errcheckln(status, "failed to construct formatter - %s", u_errorName(status));
|
2001-10-10 21:43:11 +00:00
|
|
|
} else {
|
|
|
|
doTest(formatter, -12345678, 12345678);
|
|
|
|
}
|
|
|
|
delete formatter;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Perform an exhaustive round-trip test on the Japanese spellout rules
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
RbnfRoundTripTest::TestJapaneseSpelloutRT()
|
|
|
|
{
|
|
|
|
UErrorCode status = U_ZERO_ERROR;
|
|
|
|
RuleBasedNumberFormat* formatter
|
2002-08-21 00:16:30 +00:00
|
|
|
= new RuleBasedNumberFormat(URBNF_SPELLOUT, Locale::getJapan(), status);
|
2001-10-10 21:43:11 +00:00
|
|
|
|
|
|
|
if (U_FAILURE(status)) {
|
2009-06-12 19:34:21 +00:00
|
|
|
errcheckln(status, "failed to construct formatter - %s", u_errorName(status));
|
2001-10-10 21:43:11 +00:00
|
|
|
} else {
|
|
|
|
doTest(formatter, 0, 12345678);
|
|
|
|
}
|
|
|
|
delete formatter;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Perform an exhaustive round-trip test on the Russian spellout rules
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
RbnfRoundTripTest::TestRussianSpelloutRT()
|
|
|
|
{
|
|
|
|
UErrorCode status = U_ZERO_ERROR;
|
|
|
|
RuleBasedNumberFormat* formatter
|
|
|
|
= new RuleBasedNumberFormat(URBNF_SPELLOUT, Locale("ru", "RU"), status);
|
|
|
|
|
|
|
|
if (U_FAILURE(status)) {
|
2009-06-12 19:34:21 +00:00
|
|
|
errcheckln(status, "failed to construct formatter - %s", u_errorName(status));
|
2001-10-10 21:43:11 +00:00
|
|
|
} else {
|
|
|
|
doTest(formatter, 0, 12345678);
|
|
|
|
}
|
|
|
|
delete formatter;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2006-11-09 00:51:18 +00:00
|
|
|
* Perform an exhaustive round-trip test on the Portuguese spellout rules
|
2001-10-10 21:43:11 +00:00
|
|
|
*/
|
|
|
|
void
|
2006-11-09 00:51:18 +00:00
|
|
|
RbnfRoundTripTest::TestPortugueseSpelloutRT()
|
2001-10-10 21:43:11 +00:00
|
|
|
{
|
|
|
|
UErrorCode status = U_ZERO_ERROR;
|
|
|
|
RuleBasedNumberFormat* formatter
|
2006-11-09 00:51:18 +00:00
|
|
|
= new RuleBasedNumberFormat(URBNF_SPELLOUT, Locale("pt", "BR"), status);
|
2001-10-10 21:43:11 +00:00
|
|
|
|
|
|
|
if (U_FAILURE(status)) {
|
2009-06-12 19:34:21 +00:00
|
|
|
errcheckln(status, "failed to construct formatter - %s", u_errorName(status));
|
2001-10-10 21:43:11 +00:00
|
|
|
} else {
|
2006-11-09 00:51:18 +00:00
|
|
|
doTest(formatter, -12345678, 12345678);
|
2001-10-10 21:43:11 +00:00
|
|
|
}
|
|
|
|
delete formatter;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
RbnfRoundTripTest::doTest(const RuleBasedNumberFormat* formatter,
|
|
|
|
double lowLimit,
|
|
|
|
double highLimit)
|
|
|
|
{
|
|
|
|
char buf[128];
|
|
|
|
|
|
|
|
uint32_t count = 0;
|
|
|
|
double increment = 1;
|
|
|
|
for (double i = lowLimit; i <= highLimit; i += increment) {
|
|
|
|
if (count % 1000 == 0) {
|
|
|
|
sprintf(buf, "%.12g", i);
|
|
|
|
logln(buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fabs(i) < 5000)
|
|
|
|
increment = 1;
|
|
|
|
else if (fabs(i) < 500000)
|
|
|
|
increment = 2737;
|
|
|
|
else
|
|
|
|
increment = 267437;
|
|
|
|
|
|
|
|
UnicodeString formatResult;
|
|
|
|
formatter->format(i, formatResult);
|
|
|
|
UErrorCode status = U_ZERO_ERROR;
|
|
|
|
Formattable parseResult;
|
|
|
|
formatter->parse(formatResult, parseResult, status);
|
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
sprintf(buf, "Round-trip status failure: %.12g, status: %d", i, status);
|
|
|
|
errln(buf);
|
2001-10-12 17:26:10 +00:00
|
|
|
return;
|
2001-10-10 21:43:11 +00:00
|
|
|
} else {
|
|
|
|
double rt = (parseResult.getType() == Formattable::kDouble) ?
|
|
|
|
parseResult.getDouble() :
|
|
|
|
(double)parseResult.getLong();
|
|
|
|
|
|
|
|
if (rt != i) {
|
|
|
|
sprintf(buf, "Round-trip failed: %.12g -> %.12g", i, rt);
|
|
|
|
errln(buf);
|
2001-10-12 17:26:10 +00:00
|
|
|
return;
|
2001-10-10 21:43:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
++count;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (lowLimit < 0) {
|
|
|
|
double d = 1.234;
|
|
|
|
while (d < 1000) {
|
|
|
|
UnicodeString formatResult;
|
|
|
|
formatter->format(d, formatResult);
|
|
|
|
UErrorCode status = U_ZERO_ERROR;
|
|
|
|
Formattable parseResult;
|
|
|
|
formatter->parse(formatResult, parseResult, status);
|
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
sprintf(buf, "Round-trip status failure: %.12g, status: %d", d, status);
|
|
|
|
errln(buf);
|
2001-10-12 17:26:10 +00:00
|
|
|
return;
|
2001-10-10 21:43:11 +00:00
|
|
|
} else {
|
|
|
|
double rt = (parseResult.getType() == Formattable::kDouble) ?
|
|
|
|
parseResult.getDouble() :
|
|
|
|
(double)parseResult.getLong();
|
|
|
|
|
|
|
|
if (rt != d) {
|
2001-10-12 17:26:10 +00:00
|
|
|
UnicodeString msg;
|
2001-10-10 21:43:11 +00:00
|
|
|
sprintf(buf, "Round-trip failed: %.12g -> ", d);
|
2001-10-12 17:26:10 +00:00
|
|
|
msg.append(buf);
|
|
|
|
msg.append(formatResult);
|
|
|
|
sprintf(buf, " -> %.12g", rt);
|
|
|
|
msg.append(buf);
|
2001-10-10 21:43:11 +00:00
|
|
|
errln(msg);
|
2001-10-12 17:26:10 +00:00
|
|
|
return;
|
2001-10-10 21:43:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
d *= 10;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-03-14 00:28:22 +00:00
|
|
|
/* U_HAVE_RBNF */
|
|
|
|
#else
|
|
|
|
|
|
|
|
void
|
|
|
|
RbnfRoundTripTest::TestRBNFDisabled() {
|
2002-04-02 02:55:31 +00:00
|
|
|
errln("*** RBNF currently disabled on this platform ***\n");
|
2002-03-14 00:28:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* U_HAVE_RBNF */
|
|
|
|
#endif
|
2002-09-21 00:43:14 +00:00
|
|
|
|
|
|
|
#endif /* #if !UCONFIG_NO_FORMATTING */
|