From a05d92bdc7b93f3f70abd64049a52b12e2c3188f Mon Sep 17 00:00:00 2001 From: Doug Felt Date: Wed, 7 Nov 2001 00:02:41 +0000 Subject: [PATCH] ICU-1413 return RuleBasedNumberFormat for unum_open when handed the style UNUM_SPELLOUT. Test open, format, parse, close of RBNF using unum C API X-SVN-Rev: 6652 --- icu4c/source/test/cintltst/cnumtst.c | 73 ++++++++++++++++++++++++++-- 1 file changed, 68 insertions(+), 5 deletions(-) diff --git a/icu4c/source/test/cintltst/cnumtst.c b/icu4c/source/test/cintltst/cnumtst.c index d702130e32..c4ddec1cf5 100644 --- a/icu4c/source/test/cintltst/cnumtst.c +++ b/icu4c/source/test/cintltst/cnumtst.c @@ -27,6 +27,8 @@ #include "cnumtst.h" #include "cmemory.h" +#define LENGTH(arr) (sizeof(arr)/sizeof(arr[0])) + void addNumForTest(TestNode** root); void addNumForTest(TestNode** root) @@ -35,6 +37,38 @@ void addNumForTest(TestNode** root) addTest(root, &TestNumberFormatPadding, "tsformat/cnumtst/TestNumberFormatPadding"); } +/** copy src to dst with unicode-escapes for values < 0x20 and > 0x7e, null terminate if possible */ +static int32_t ustrToAstr(const UChar* src, int32_t srcLength, char* dst, int32_t dstLength) { + static const char hex[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' }; + + char *p = dst; + const char *e = p + dstLength; + if (srcLength < 0) { + const UChar* s = src; + while (*s) ++s; + srcLength = s - src; + } + while (p < e && --srcLength >= 0) { + UChar c = *src++; + if (c == 0xd || c == 0xa || c == 0x9 || (c>= 0x20 && c <= 0x7e)) { + *p++ = (char) c & 0x7f; + } else if (e - p >= 6) { + *p++ = '\\'; + *p++ = 'u'; + *p++ = hex[(c >> 12) & 0xf]; + *p++ = hex[(c >> 8) & 0xf]; + *p++ = hex[(c >> 4) & 0xf]; + *p++ = hex[c & 0xf]; + } else { + break; + } + } + if (p < e) { + *p = 0; + } + return p - dst; +} + /* test Number Format API */ static void TestNumberFormat() { @@ -66,7 +100,8 @@ static void TestNumberFormat() UNumberFormatStyle style= UNUM_DEFAULT; UNumberFormat *pattern; UNumberFormat *def, *fr, *cur_def, *cur_fr, *per_def, *per_fr, - *cur_frpattern, *myclone; + *cur_frpattern, *myclone, *spellout_def; + /* Testing unum_open() with various Numberformat styles and locales*/ status = U_ZERO_ERROR; log_verbose("Testing unum_open() with default style and locale\n"); @@ -104,13 +139,12 @@ static void TestNumberFormat() if(U_FAILURE(status)) log_err("Error: could not create NumberFormat using unum_open(percent, french, &status): %s\n", myErrorName(status)); - /* log_verbose("\nTesting unum_open(spellout, NULL, status)"); style=UNUM_SPELLOUT; - spellout_def=unum_open(style, NULL, &status); + spellout_def=unum_open(style, NULL, 0, NULL, NULL, &status); if(U_FAILURE(status)) log_err("Error: could not create NumberFormat using unum_open(spellout, NULL, &status): %s\n", myErrorName(status)); - */ + /* Testing unum_clone(..) */ log_verbose("\nTesting unum_clone(fmt, status)"); status = U_ZERO_ERROR; @@ -589,6 +623,35 @@ uprv_free(result); log_verbose("Pass: attributes set and retrieved successfully\n"); } + /*testing spellout format to make sure we can use it successfully.*/ + log_verbose("\nTesting spellout format\n"); + { + int i; + int32_t values[] = { 0, -5, 105, 1005, 105050 }; + for (i = 0; i < LENGTH(values); ++i) { + UChar buffer[128]; + int32_t len; + int32_t value = values[i]; + status = U_ZERO_ERROR; + len = unum_format(spellout_def, value, buffer, LENGTH(buffer), NULL, &status); + if(U_FAILURE(status)) { + log_err("Error in formatting using unum_format(spellout_fmt, ...): %s\n", myErrorName(status)); + } else { + int32_t pp = 0; + int32_t result; + char logbuf[256]; + ustrToAstr(buffer, len, logbuf, LENGTH(logbuf)); + log_verbose("formatted %d as '%s', length: %d\n", value, logbuf, len); + + result = unum_parse(spellout_def, buffer, len, &pp, &status); + if (U_FAILURE(status)) { + log_err("Error in parsing using unum_format(spellout_fmt, ...): %s\n", myErrorName(status)); + } else if (result != value) { + log_err("unum_format result %d != value %d\n", result, value); + } + } + } + } /*closing the NumberFormat() using unum_close(UNumberFormat*)")*/ unum_close(def); @@ -597,7 +660,7 @@ uprv_free(result); unum_close(cur_fr); unum_close(per_def); unum_close(per_fr); - /*unum_close(spellout_def);*/ + unum_close(spellout_def); unum_close(pattern); unum_close(cur_frpattern); unum_close(myclone);