1ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru/******************************************************************** 2ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * COPYRIGHT: 3f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius * Copyright (c) 2005-2014, International Business Machines Corporation and 4ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * others. All Rights Reserved. 5ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru ********************************************************************/ 6ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#include "unicode/utypes.h" 7ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 8ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#if !UCONFIG_NO_FORMATTING 9ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#include "unicode/unum.h" 10ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#include "unicode/ucurr.h" 11ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#include "unicode/ustring.h" 12ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#include "cintltst.h" 13ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#include "cstring.h" 14ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 15ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Querustatic void expectInList(const char *isoCurrency, uint32_t currencyType, UBool isExpected) { 16ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru UErrorCode status = U_ZERO_ERROR; 17ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru const char *foundCurrency = NULL; 18ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru const char *currentCurrency; 19ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru UEnumeration *en = ucurr_openISOCurrencies(currencyType, &status); 20ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if (U_FAILURE(status)) { 21ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru log_err("Error: ucurr_openISOCurrencies returned %s\n", myErrorName(status)); 22ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru return; 23ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 24ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 25ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru while ((currentCurrency = uenum_next(en, NULL, &status)) != NULL) { 26ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if (strcmp(isoCurrency, currentCurrency) == 0) { 27ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru foundCurrency = currentCurrency; 28ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru break; 29ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 30ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 31ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 32ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if ((foundCurrency != NULL) != isExpected) { 33ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru log_err("Error: could not find %s as expected. isExpected = %s type=0x%X\n", 34ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru isoCurrency, isExpected ? "TRUE" : "FALSE", currencyType); 35ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 36ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru uenum_close(en); 37ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru} 38ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 39ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Querustatic void TestEnumList(void) { 40ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru expectInList("ADP", UCURR_ALL, TRUE); /* First in list */ 41ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru expectInList("ZWD", UCURR_ALL, TRUE); /* Last in list */ 42ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 43ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru expectInList("USD", UCURR_ALL, TRUE); 44ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru expectInList("USD", UCURR_COMMON, TRUE); 45ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru expectInList("USD", UCURR_UNCOMMON, FALSE); 46ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru expectInList("USD", UCURR_DEPRECATED, FALSE); 47ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru expectInList("USD", UCURR_NON_DEPRECATED, TRUE); 48ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru expectInList("USD", UCURR_COMMON|UCURR_DEPRECATED, FALSE); 49ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru expectInList("USD", UCURR_COMMON|UCURR_NON_DEPRECATED, TRUE); 50ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru expectInList("USD", UCURR_UNCOMMON|UCURR_DEPRECATED, FALSE); 51ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru expectInList("USD", UCURR_UNCOMMON|UCURR_NON_DEPRECATED, FALSE); 52ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 53ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru expectInList("USN", UCURR_ALL, TRUE); 54ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru expectInList("USN", UCURR_COMMON, FALSE); 55ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru expectInList("USN", UCURR_UNCOMMON, TRUE); 56ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru expectInList("USN", UCURR_DEPRECATED, FALSE); 57ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru expectInList("USN", UCURR_NON_DEPRECATED, TRUE); 58ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru expectInList("USN", UCURR_COMMON|UCURR_DEPRECATED, FALSE); 59ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru expectInList("USN", UCURR_COMMON|UCURR_NON_DEPRECATED, FALSE); 60ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru expectInList("USN", UCURR_UNCOMMON|UCURR_DEPRECATED, FALSE); 61ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru expectInList("USN", UCURR_UNCOMMON|UCURR_NON_DEPRECATED, TRUE); 62ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 63ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru expectInList("DEM", UCURR_ALL, TRUE); 64ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru expectInList("DEM", UCURR_COMMON, TRUE); 65ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru expectInList("DEM", UCURR_UNCOMMON, FALSE); 66ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru expectInList("DEM", UCURR_DEPRECATED, TRUE); 67ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru expectInList("DEM", UCURR_NON_DEPRECATED, FALSE); 68ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru expectInList("DEM", UCURR_COMMON|UCURR_DEPRECATED, TRUE); 69ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru expectInList("DEM", UCURR_COMMON|UCURR_NON_DEPRECATED, FALSE); 70ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru expectInList("DEM", UCURR_UNCOMMON|UCURR_DEPRECATED, FALSE); 71ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru expectInList("DEM", UCURR_UNCOMMON|UCURR_NON_DEPRECATED, FALSE); 72ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 73ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru expectInList("XEU", UCURR_ALL, TRUE); 74ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru expectInList("XEU", UCURR_COMMON, FALSE); 75ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru expectInList("XEU", UCURR_UNCOMMON, TRUE); 76ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru expectInList("XEU", UCURR_DEPRECATED, TRUE); 77ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru expectInList("XEU", UCURR_NON_DEPRECATED, FALSE); 78ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru expectInList("XEU", UCURR_COMMON|UCURR_DEPRECATED, FALSE); 79ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru expectInList("XEU", UCURR_COMMON|UCURR_NON_DEPRECATED, FALSE); 80ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru expectInList("XEU", UCURR_UNCOMMON|UCURR_DEPRECATED, TRUE); 81ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru expectInList("XEU", UCURR_UNCOMMON|UCURR_NON_DEPRECATED, FALSE); 82ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 83ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru} 84ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 85ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Querustatic void TestEnumListReset(void) { 86ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru UErrorCode status = U_ZERO_ERROR; 87ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru const char *currency1; 88ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru const char *currency2; 89ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru UEnumeration *en = ucurr_openISOCurrencies(UCURR_ALL, &status); 90ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if (U_FAILURE(status)) { 91ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru log_err("Error: ucurr_openISOCurrencies returned %s\n", myErrorName(status)); 92ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru return; 93ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 94ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 95ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru currency1 = uenum_next(en, NULL, &status); 96ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru uenum_reset(en, &status); 97ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru currency2 = uenum_next(en, NULL, &status); 98ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if (U_FAILURE(status)) { 99ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru log_err("Error: uenum_next or uenum_reset returned %s\n", myErrorName(status)); 100ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru return; 101ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 102ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru /* The first item's pointer in the list should be the same between resets. */ 103ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if (currency1 != currency2) { 104ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru log_err("Error: reset doesn't work %s != %s\n", currency1, currency2); 105ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 106ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru uenum_close(en); 107ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru} 108ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 109ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Querustatic int32_t checkItemCount(uint32_t currencyType) { 110ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru UErrorCode status = U_ZERO_ERROR; 111ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru int32_t originalCount, count; 112ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru UEnumeration *en = ucurr_openISOCurrencies(currencyType, &status); 113ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru int32_t expectedLen = 3, len; 114ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if (U_FAILURE(status)) { 115ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru log_err("Error: ucurr_openISOCurrencies returned %s\n", myErrorName(status)); 116ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru return -1; 117ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 118ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 119ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru originalCount = uenum_count(en, &status); 120ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru for (count=0;;count++) { 121ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru const char *str = uenum_next(en, &len, &status); 122ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if (str == NULL || len != expectedLen || strlen(str) != expectedLen) { 123ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru break; 124ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 125ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 126ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 127ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if (originalCount != count) { 128ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru log_err("Error: uenum_count returned the wrong value (type = 0x%X). Got: %d Expected %d\n", 129ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru currencyType, count, originalCount); 130ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 131ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if (U_FAILURE(status)) { 132ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru log_err("Error: uenum_next got an error: %s\n", u_errorName(status)); 133ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 134ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru uenum_close(en); 135ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru return count; 136ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru} 137ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 138ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Querustatic void TestEnumListCount(void) { 139ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru checkItemCount(UCURR_ALL); 140ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru checkItemCount(UCURR_COMMON); 141ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru checkItemCount(UCURR_UNCOMMON); 142ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru checkItemCount(UCURR_DEPRECATED); 143ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru checkItemCount(UCURR_NON_DEPRECATED); 144ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru checkItemCount(UCURR_COMMON|UCURR_DEPRECATED); 145ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru checkItemCount(UCURR_COMMON|UCURR_NON_DEPRECATED); 146ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru checkItemCount(UCURR_UNCOMMON|UCURR_DEPRECATED); 147ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru checkItemCount(UCURR_UNCOMMON|UCURR_NON_DEPRECATED); 148ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 149ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if (checkItemCount(UCURR_DEPRECATED|UCURR_NON_DEPRECATED) != 0) { 150ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru log_err("Error: UCURR_DEPRECATED|UCURR_NON_DEPRECATED should return 0 items\n"); 151ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 152ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if (checkItemCount(UCURR_COMMON|UCURR_UNCOMMON) != 0) { 153ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru log_err("Error: UCURR_DEPRECATED|UCURR_NON_DEPRECATED should return 0 items\n"); 154ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 155ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru} 156ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 157ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Querustatic void TestFractionDigitOverride(void) { 158ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru UErrorCode status = U_ZERO_ERROR; 159ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru UNumberFormat *fmt = unum_open(UNUM_CURRENCY, NULL, 0, "hu_HU", NULL, &status); 160ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru UChar buffer[256]; 161ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru UChar expectedBuf[256]; 162f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius const char expectedFirst[] = "123,46\\u00A0HUF"; /* changed to use 2 fraction digits */ 163f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius const char expectedSecond[] = "123,46\\u00A0HUF"; 164f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius const char expectedThird[] = "123,456\\u00A0HUF"; 165ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if (U_FAILURE(status)) { 16685bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho log_data_err("Error: unum_open returned %s (Are you missing data?)\n", myErrorName(status)); 167ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru return; 168ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 169ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru /* Make sure that you can format normal fraction digits. */ 170ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru unum_formatDouble(fmt, 123.456, buffer, sizeof(buffer)/sizeof(buffer[0]), NULL, &status); 17185bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho u_unescape(expectedFirst, expectedBuf, strlen(expectedFirst)+1); 172ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if (u_strcmp(buffer, expectedBuf) != 0) { 173ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru log_err("Error: unum_formatDouble didn't return %s\n", expectedFirst); 174ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 17585bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho /* Make sure that you can format 2 fraction digits. */ 17685bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho unum_setAttribute(fmt, UNUM_FRACTION_DIGITS, 2); 177ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru unum_formatDouble(fmt, 123.456, buffer, sizeof(buffer)/sizeof(buffer[0]), NULL, &status); 17885bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho u_unescape(expectedSecond, expectedBuf, strlen(expectedSecond)+1); 179ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if (u_strcmp(buffer, expectedBuf) != 0) { 180ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru log_err("Error: unum_formatDouble didn't return %s\n", expectedSecond); 181ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 182ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru /* Make sure that you can format more fraction digits. */ 183ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru unum_setAttribute(fmt, UNUM_FRACTION_DIGITS, 3); 184ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru unum_formatDouble(fmt, 123.456, buffer, sizeof(buffer)/sizeof(buffer[0]), NULL, &status); 18585bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho u_unescape(expectedThird, expectedBuf, strlen(expectedThird)+1); 186ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if (u_strcmp(buffer, expectedBuf) != 0) { 187ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru log_err("Error: unum_formatDouble didn't return %s\n", expectedThird); 188ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 189ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru unum_close(fmt); 190ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru} 191ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 192ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Querustatic void TestPrefixSuffix(void) { 193ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru int32_t pos; 194ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru UErrorCode status; 195ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru double result1 = 0.0, result2 = 0.0; 196ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru UNumberFormat* parser; 197ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru UChar buffer[4]; 198ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru static const UChar TEST_NUMBER[] = {0x0024,0x0031,0x0032,0x002E,0x0030,0x0030,0}; /* $12.00 */ 199ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru static const UChar NEG_PREFIX[] = {0x005B,0}; /* "[" */ 200ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru static const UChar NEG_SUFFIX[] = {0x005D,0}; /* "]" */ 201ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 202ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 203ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru status = U_ZERO_ERROR; 204ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru parser = unum_open(UNUM_CURRENCY, NULL, -1, "en_US", NULL, &status); 205ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if (U_FAILURE(status)) { 20685bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho log_data_err("Error: unum_open returned %s (Are you missing data?)\n", u_errorName(status)); 207ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru return; 208ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 209ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 210ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru pos = 0; 211ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru status = U_ZERO_ERROR; 212ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru result1 = unum_parseDoubleCurrency(parser, TEST_NUMBER, -1, &pos, buffer, &status); 213ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 214ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru unum_setTextAttribute(parser, UNUM_NEGATIVE_SUFFIX, NEG_SUFFIX, -1, &status); 215ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru unum_setTextAttribute(parser, UNUM_NEGATIVE_PREFIX, NEG_PREFIX, -1, &status); 216ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if (U_FAILURE(status)) { 217ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru log_err("Error: unum_setTextAttribute returned %s\n", u_errorName(status)); 218ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru return; 219ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 220ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 221ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru pos = 0; 222ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru result2 = unum_parseDoubleCurrency(parser, TEST_NUMBER, -1, &pos, buffer, &status); 223ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if (result1 != result2 || U_FAILURE(status)) { 224ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru log_err("Error: unum_parseDoubleCurrency didn't return the same value for same string %f %f %s\n", 225ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru result1, result2, u_errorName(status)); 226ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 227ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru unum_close(parser); 228ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru} 229ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 23083a171d1a62abf406f7f44ae671823d5ec20db7dCraig Corneliustypedef struct { 23183a171d1a62abf406f7f44ae671823d5ec20db7dCraig Cornelius const char* alphaCode; 23283a171d1a62abf406f7f44ae671823d5ec20db7dCraig Cornelius int32_t numericCode; 23383a171d1a62abf406f7f44ae671823d5ec20db7dCraig Cornelius} NumCodeTestEntry; 23483a171d1a62abf406f7f44ae671823d5ec20db7dCraig Cornelius 23583a171d1a62abf406f7f44ae671823d5ec20db7dCraig Corneliusstatic const NumCodeTestEntry NUMCODE_TESTDATA[] = { 23683a171d1a62abf406f7f44ae671823d5ec20db7dCraig Cornelius {"USD", 840}, 23783a171d1a62abf406f7f44ae671823d5ec20db7dCraig Cornelius {"Usd", 840}, /* mixed casing */ 23883a171d1a62abf406f7f44ae671823d5ec20db7dCraig Cornelius {"EUR", 978}, 23983a171d1a62abf406f7f44ae671823d5ec20db7dCraig Cornelius {"JPY", 392}, 24083a171d1a62abf406f7f44ae671823d5ec20db7dCraig Cornelius {"XFU", 0}, /* XFU: no numeric code */ 24183a171d1a62abf406f7f44ae671823d5ec20db7dCraig Cornelius {"ZZZ", 0}, /* ZZZ: undefined ISO currency code */ 24283a171d1a62abf406f7f44ae671823d5ec20db7dCraig Cornelius {"bogus", 0}, /* bogus code */ 24383a171d1a62abf406f7f44ae671823d5ec20db7dCraig Cornelius {0, 0}, 24483a171d1a62abf406f7f44ae671823d5ec20db7dCraig Cornelius}; 24583a171d1a62abf406f7f44ae671823d5ec20db7dCraig Cornelius 24683a171d1a62abf406f7f44ae671823d5ec20db7dCraig Corneliusstatic void TestNumericCode(void) { 24783a171d1a62abf406f7f44ae671823d5ec20db7dCraig Cornelius UChar code[4]; 24883a171d1a62abf406f7f44ae671823d5ec20db7dCraig Cornelius int32_t i; 24983a171d1a62abf406f7f44ae671823d5ec20db7dCraig Cornelius int32_t numCode; 25083a171d1a62abf406f7f44ae671823d5ec20db7dCraig Cornelius 25183a171d1a62abf406f7f44ae671823d5ec20db7dCraig Cornelius for (i = 0; NUMCODE_TESTDATA[i].alphaCode; i++) { 25283a171d1a62abf406f7f44ae671823d5ec20db7dCraig Cornelius u_charsToUChars(NUMCODE_TESTDATA[i].alphaCode, code, sizeof(code)/sizeof(code[0])); 25383a171d1a62abf406f7f44ae671823d5ec20db7dCraig Cornelius numCode = ucurr_getNumericCode(code); 25483a171d1a62abf406f7f44ae671823d5ec20db7dCraig Cornelius if (numCode != NUMCODE_TESTDATA[i].numericCode) { 25583a171d1a62abf406f7f44ae671823d5ec20db7dCraig Cornelius log_data_err("Error: ucurr_getNumericCode returned %d for currency %s, expected - %d\n", 25683a171d1a62abf406f7f44ae671823d5ec20db7dCraig Cornelius numCode, NUMCODE_TESTDATA[i].alphaCode, NUMCODE_TESTDATA[i].numericCode); 25783a171d1a62abf406f7f44ae671823d5ec20db7dCraig Cornelius } 25883a171d1a62abf406f7f44ae671823d5ec20db7dCraig Cornelius } 25983a171d1a62abf406f7f44ae671823d5ec20db7dCraig Cornelius} 26083a171d1a62abf406f7f44ae671823d5ec20db7dCraig Cornelius 261ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruvoid addCurrencyTest(TestNode** root); 262ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 263ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#define TESTCASE(x) addTest(root, &x, "tsformat/currtest/" #x) 264ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 265ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruvoid addCurrencyTest(TestNode** root) 266ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru{ 267ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru TESTCASE(TestEnumList); 268ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru TESTCASE(TestEnumListReset); 269ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru TESTCASE(TestEnumListCount); 270ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru TESTCASE(TestFractionDigitOverride); 271ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru TESTCASE(TestPrefixSuffix); 27283a171d1a62abf406f7f44ae671823d5ec20db7dCraig Cornelius TESTCASE(TestNumericCode); 273ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru} 274ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 275ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#endif /* #if !UCONFIG_NO_FORMATTING */ 276