/******************************************************************** * COPYRIGHT: * Copyright (c) 1997-2001, International Business Machines Corporation and * others. All Rights Reserved. ********************************************************************/ /******************************************************************************** * * File CNUMTST.C * * Madhu Katragadda Creation * * Modification History: * * Date Name Description * 06/24/99 helena Integrated Alan's NF enhancements and Java2 bug fixes * 07/15/99 helena Ported to HPUX 10/11 CC. ********************************************************************************* */ /* C API TEST FOR NUMBER FORMAT */ #include "unicode/uloc.h" #include "unicode/utypes.h" #include "unicode/unum.h" #include "unicode/ustring.h" #include "cintltst.h" #include "cnumtst.h" #include "cmemory.h" #define LENGTH(arr) (sizeof(arr)/sizeof(arr[0])) void addNumForTest(TestNode** root); void addNumForTest(TestNode** root) { addTest(root, &TestNumberFormat, "tsformat/cnumtst/TestNumberFormat"); 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 = (int32_t)(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 (int32_t)(p - dst); } /* test Number Format API */ static void TestNumberFormat() { UChar *result=NULL; UChar temp1[512]; UChar temp2[512]; UChar temp[5]; UChar prefix[5]; UChar suffix[5]; UChar symbol[20]; int32_t resultlength; int32_t resultlengthneeded; int32_t parsepos; double d1; int32_t l1; double d = -10456.37; int32_t l = 100000000; UFieldPosition pos1; UFieldPosition pos2; int32_t numlocales; int32_t i; UNumberFormatAttribute attr; UNumberFormatSymbol symType = UNUM_DECIMAL_SEPARATOR_SYMBOL; int32_t newvalue; UErrorCode status=U_ZERO_ERROR; UNumberFormatStyle style= UNUM_DEFAULT; UNumberFormat *pattern; UNumberFormat *def, *fr, *cur_def, *cur_fr, *per_def, *per_fr, *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"); def=unum_open(style, NULL,0,NULL, NULL,&status); if(U_FAILURE(status)) log_err("Error in creating NumberFormat default using unum_open(): %s\n", myErrorName(status)); log_verbose("\nTesting unum_open() with french locale and default style(decimal)\n"); fr=unum_open(style,NULL,0, "fr_FR",NULL, &status); if(U_FAILURE(status)) log_err("Error: could not create NumberFormat (french): %s\n", myErrorName(status)); log_verbose("\nTesting unum_open(currency,NULL,status)\n"); style=UNUM_CURRENCY; /* Can't hardcode the result to assume the default locale is "en_US". */ cur_def=unum_open(style, NULL,0,"en_US", NULL, &status); if(U_FAILURE(status)) log_err("Error: could not create NumberFormat using \n unum_open(currency, NULL, &status) %s\n", myErrorName(status) ); log_verbose("\nTesting unum_open(currency, frenchlocale, status)\n"); cur_fr=unum_open(style,NULL,0, "fr_FR", NULL, &status); if(U_FAILURE(status)) log_err("Error: could not create NumberFormat using unum_open(currency, french, &status): %s\n", myErrorName(status)); log_verbose("\nTesting unum_open(percent, NULL, status)\n"); style=UNUM_PERCENT; per_def=unum_open(style,NULL,0, NULL,NULL, &status); if(U_FAILURE(status)) log_err("Error: could not create NumberFormat using unum_open(percent, NULL, &status): %s\n", myErrorName(status)); log_verbose("\nTesting unum_open(percent,frenchlocale, status)\n"); per_fr=unum_open(style, NULL,0,"fr_FR", NULL,&status); 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, 0, "en_US", 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; myclone = unum_clone(def,&status); if(U_FAILURE(status)) log_err("Error: could not clone unum_clone(def, &status): %s\n", myErrorName(status)); else { log_verbose("unum_clone() successful\n"); } /*Testing unum_getAvailable() and unum_countAvailable()*/ log_verbose("\nTesting getAvailableLocales and countAvailable()\n"); numlocales=unum_countAvailable(); if(numlocales < 0) log_err("error in countAvailable"); else{ log_verbose("unum_countAvialable() successful\n"); log_verbose("The no: of locales where number formattting is applicable is %d\n", numlocales); } for(i=0;i