From b880d50f0bc14037a9a7e069d8710f0481e46849 Mon Sep 17 00:00:00 2001 From: Doug Felt Date: Wed, 28 Nov 2001 23:34:01 +0000 Subject: [PATCH] ICU-104 added test of fractionalRuleSet. Removed old ifdef'd out debugging code. X-SVN-Rev: 7150 --- icu4c/source/test/intltest/itrbnf.cpp | 154 ++++++++++++-------------- icu4c/source/test/intltest/itrbnf.h | 5 + 2 files changed, 75 insertions(+), 84 deletions(-) diff --git a/icu4c/source/test/intltest/itrbnf.cpp b/icu4c/source/test/intltest/itrbnf.cpp index 6cad22d743a..f10da64ee86 100644 --- a/icu4c/source/test/intltest/itrbnf.cpp +++ b/icu4c/source/test/intltest/itrbnf.cpp @@ -41,6 +41,7 @@ void IntlTestRBNF::runIndexedTest(int32_t index, UBool exec, const char* &name, TESTCASE(7, TestGermanSpellout); TESTCASE(8, TestThaiSpellout); TESTCASE(9, TestAPI); + TESTCASE(10, TestFractionalRuleSet); default: name = ""; break; @@ -116,7 +117,6 @@ IntlTestRBNF::TestAPI() { } - // test copy constructor { logln("Testing copy constructor"); RuleBasedNumberFormat copyCtorResult(*formatter); @@ -191,92 +191,78 @@ IntlTestRBNF::TestAPI() { delete formatter; } +void IntlTestRBNF::TestFractionalRuleSet() +{ + UnicodeString fracRules( + "%main:\n" + // this rule formats the number if it's 1 or more. It formats + // the integral part using a DecimalFormat ("#,##0" puts + // thousands separators in the right places) and the fractional + // part using %%frac. If there is no fractional part, it + // just shows the integral part. + " x.0: <#,##0<[ >%%frac>];\n" + // this rule formats the number if it's between 0 and 1. It + // shows only the fractional part (0.5 shows up as "1/2," not + // "0 1/2") + " 0.x: >%%frac>;\n" + // the fraction rule set. This works the same way as the one in the + // preceding example: We multiply the fractional part of the number + // being formatted by each rule's base value and use the rule that + // produces the result closest to 0 (or the first rule that produces 0). + // Since we only provide rules for the numbers from 2 to 10, we know + // we'll get a fraction with a denominator between 2 and 10. + // "<0<" causes the numerator of the fraction to be formatted + // using numerals + "%%frac:\n" + " 2: 1/2;\n" + " 3: <0getDynamicClassID() == RuleBasedCollator::getStaticClassID()) { - - RuleBasedCollator* collator = (RuleBasedCollator*)temp; - UnicodeString rules(collator->getRules()); - UnicodeString tailoring("&'\\u0000' << ' ' << '-'\n"); - tailoring = tailoring.unescape(); - rules.append(tailoring); - - collator = new RuleBasedCollator(rules, status); - if (U_SUCCESS(status)) { - collator->setDecomposition(Normalizer::DECOMP); - - UnicodeString prefix(" hundred"); - UnicodeString str("hundred-fifty"); - - CollationElementIterator* strIter = collator->createCollationElementIterator(str); - CollationElementIterator* prefixIter = collator->createCollationElementIterator(prefix); - - // match collation elements between the strings - int32_t oStr = strIter->next(status); - int32_t oPrefix = prefixIter->next(status); - - while (oPrefix != CollationElementIterator::NULLORDER) { - // skip over ignorable characters in the target string - while (CollationElementIterator::primaryOrder(oStr) == 0 - && oStr != CollationElementIterator::NULLORDER) { - oStr = strIter->next(status); - } - - // skip over ignorable characters in the prefix - while (CollationElementIterator::primaryOrder(oPrefix) == 0 - && oPrefix != CollationElementIterator::NULLORDER) { - oPrefix = prefixIter->next(status); - } - - // if skipping over ignorables brought us to the end - // of the target string, we didn't match and return 0 - if (oStr == CollationElementIterator::NULLORDER) { - result = -1; - break; - } - - // if skipping over ignorables brought to the end of - // the prefix, we DID match: drop out of the loop - else if (oPrefix == CollationElementIterator::NULLORDER) { - break; - } - - // match collation elements from the two strings - // (considering only primary differences). If we - // get a mismatch, dump out and return 0 - if (CollationElementIterator::primaryOrder(oStr) - != CollationElementIterator::primaryOrder(oPrefix)) { - result = -1; - break; - - // otherwise, advance to the next character in each string - // and loop (we drop out of the loop when we exhaust - // collation elements in the prefix) - } else { - oStr = strIter->next(status); - oPrefix = prefixIter->next(status); - } - } - if (result == 0) { - result = strIter->getOffset(); - } - delete prefixIter; - delete strIter; - } - delete collator; - } - delete temp; - - printf("result: %d\n", result); -#endif - UErrorCode status = U_ZERO_ERROR; RuleBasedNumberFormat* formatter = new RuleBasedNumberFormat(URBNF_SPELLOUT, Locale::US, status); diff --git a/icu4c/source/test/intltest/itrbnf.h b/icu4c/source/test/intltest/itrbnf.h index 9555268afd6..9d78f09a4c8 100644 --- a/icu4c/source/test/intltest/itrbnf.h +++ b/icu4c/source/test/intltest/itrbnf.h @@ -25,6 +25,11 @@ class IntlTestRBNF : public IntlTest { */ virtual void TestAPI(); + /** + * Perform a simple spot check on the FractionalRuleSet logic + */ + virtual void TestFractionalRuleSet(); + /** * Perform a simple spot check on the English spellout rules */