Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(923)

Side by Side Diff: source/test/intltest/numrgts.cpp

Issue 1621843002: ICU 56 update step 1 (Closed) Base URL: https://chromium.googlesource.com/chromium/deps/icu.git@561
Patch Set: Created 4 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « source/test/intltest/numfmtst.cpp ('k') | source/test/intltest/pluralmaptest.cpp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /*********************************************************************** 1 /***********************************************************************
2 * Copyright (c) 1997-2014, International Business Machines Corporation 2 * Copyright (c) 1997-2015, International Business Machines Corporation
3 * and others. All Rights Reserved. 3 * and others. All Rights Reserved.
4 ***********************************************************************/ 4 ***********************************************************************/
5 5
6 #include "unicode/utypes.h" 6 #include "unicode/utypes.h"
7 7
8 #if !UCONFIG_NO_FORMATTING 8 #if !UCONFIG_NO_FORMATTING
9 9
10 #include "numrgts.h" 10 #include "numrgts.h"
11 11
12 #include <float.h> // DBL_MIN, DBL_MAX 12 #include <float.h> // DBL_MIN, DBL_MAX
13 #include <stdio.h> 13 #include <stdio.h>
14 14
15 #include "unicode/dcfmtsym.h" 15 #include "unicode/dcfmtsym.h"
16 #include "unicode/decimfmt.h" 16 #include "unicode/decimfmt.h"
17 #include "unicode/locid.h" 17 #include "unicode/locid.h"
18 #include "unicode/resbund.h" 18 #include "unicode/resbund.h"
19 #include "unicode/calendar.h" 19 #include "unicode/calendar.h"
20 #include "unicode/datefmt.h" 20 #include "unicode/datefmt.h"
21 #include "unicode/ucurr.h" 21 #include "unicode/ucurr.h"
22 #include "putilimp.h" 22 #include "putilimp.h"
23 #include "uassert.h"
23 24
24 class MyNumberFormatTest : public NumberFormat 25 class MyNumberFormatTest : public NumberFormat
25 { 26 {
26 public: 27 public:
27 28
28 virtual UClassID getDynamicClassID(void) const; 29 virtual UClassID getDynamicClassID(void) const;
29 30
30 virtual UnicodeString& format( double number, 31 virtual UnicodeString& format( double number,
31 UnicodeString& toAppendTo, 32 UnicodeString& toAppendTo,
32 FieldPositionIterator* posIter, 33 FieldPositionIterator* posIter,
(...skipping 305 matching lines...) Expand 10 before | Expand all | Expand 10 after
338 delete symbols; 339 delete symbols;
339 } 340 }
340 341
341 /* @bug 4087535 342 /* @bug 4087535
342 * DecimalFormat.format() incorrectly formats 0.0 343 * DecimalFormat.format() incorrectly formats 0.0
343 */ 344 */
344 void NumberFormatRegressionTest::Test4087535 (void) 345 void NumberFormatRegressionTest::Test4087535 (void)
345 { 346 {
346 UErrorCode status = U_ZERO_ERROR; 347 UErrorCode status = U_ZERO_ERROR;
347 DecimalFormat *df = new DecimalFormat(status); 348 DecimalFormat *df = new DecimalFormat(status);
348 failure(status, "new DecimalFormat", ""); 349 if (U_FAILURE(status)) {
350 dataerrln("Error creating DecimalFormat - %s", u_errorName(status));
351 return;
352 }
349 df->setMinimumIntegerDigits(0); 353 df->setMinimumIntegerDigits(0);
350 354
351 double n = 0; 355 double n = 0;
352 UnicodeString buffer; 356 UnicodeString buffer;
353 FieldPosition pos(FieldPosition::DONT_CARE); 357 FieldPosition pos(FieldPosition::DONT_CARE);
354 buffer = df->format(n, buffer, pos); 358 buffer = df->format(n, buffer, pos);
355 if (buffer.length() == 0) 359 if (buffer.length() == 0)
356 errln(/*n + */": '" + buffer + "'"); 360 errln(/*n + */": '" + buffer + "'");
357 n = 0.1; 361 n = 0.1;
358 buffer = df->format(n, buffer, pos); 362 buffer = df->format(n, buffer, pos);
359 if (buffer.length() == 0) 363 if (buffer.length() == 0)
360 errln(/*n + */": '" + buffer + "'"); 364 errln(/*n + */": '" + buffer + "'");
361 365
362 delete df; 366 delete df;
363 } 367 }
364 368
365 /* @bug 4088503 369 /* @bug 4088503
366 * DecimalFormat.format fails when groupingSize is set to 0. 370 * DecimalFormat.format fails when groupingSize is set to 0.
367 */ 371 */
368 // {sfb} how do I tell if this worked? --> FieldPosition doesn't change ?? 372 // {sfb} how do I tell if this worked? --> FieldPosition doesn't change ??
369 void NumberFormatRegressionTest::Test4088503 (void) 373 void NumberFormatRegressionTest::Test4088503 (void)
370 { 374 {
371 UErrorCode status = U_ZERO_ERROR; 375 UErrorCode status = U_ZERO_ERROR;
372 DecimalFormat *df = new DecimalFormat(status); 376 DecimalFormat *df = new DecimalFormat(status);
373 failure(status, "new DecimalFormat", ""); 377 if (U_FAILURE(status)) {
378 dataerrln("Error creating DecimalFormat - %s", u_errorName(status));
379 return;
380 }
374 df->setGroupingSize(0); 381 df->setGroupingSize(0);
375 UnicodeString sBuf; 382 UnicodeString sBuf;
376 FieldPosition fp(FieldPosition::DONT_CARE); 383 FieldPosition fp(FieldPosition::DONT_CARE);
377 //try { 384 //try {
378 logln(df->format((int32_t)123, sBuf, fp)); 385 logln(df->format((int32_t)123, sBuf, fp));
379 //if(fp == FieldPosition(0)) 386 //if(fp == FieldPosition(0))
380 // errln("Test for bug 4088503 failed."); 387 // errln("Test for bug 4088503 failed.");
381 /*} catch (Exception foo) { 388 /*} catch (Exception foo) {
382 errln("Test for bug 4088503 failed."); 389 errln("Test for bug 4088503 failed.");
383 }*/ 390 }*/
(...skipping 952 matching lines...) Expand 10 before | Expand all | Expand 10 after
1336 }*/ 1343 }*/
1337 delete fmt; 1344 delete fmt;
1338 } 1345 }
1339 /* @bug 4061302 1346 /* @bug 4061302
1340 * API tests for API addition request A9. 1347 * API tests for API addition request A9.
1341 */ 1348 */
1342 void NumberFormatRegressionTest::Test4061302(void) 1349 void NumberFormatRegressionTest::Test4061302(void)
1343 { 1350 {
1344 UErrorCode status = U_ZERO_ERROR; 1351 UErrorCode status = U_ZERO_ERROR;
1345 DecimalFormatSymbols *fmt = new DecimalFormatSymbols(status); 1352 DecimalFormatSymbols *fmt = new DecimalFormatSymbols(status);
1346 failure(status, "new DecimalFormatSymbols"); 1353 if (U_FAILURE(status)) {
1354 dataerrln("Error creating DecimalFormatSymbols - %s", u_errorName(status ));
1355 return;
1356 }
1347 UnicodeString currency(fmt->getSymbol(DecimalFormatSymbols::kCurrencySymbol) ); 1357 UnicodeString currency(fmt->getSymbol(DecimalFormatSymbols::kCurrencySymbol) );
1348 UnicodeString intlCurrency(fmt->getSymbol(DecimalFormatSymbols::kIntlCurrenc ySymbol)); 1358 UnicodeString intlCurrency(fmt->getSymbol(DecimalFormatSymbols::kIntlCurrenc ySymbol));
1349 UnicodeString monDecSeparator(fmt->getSymbol(DecimalFormatSymbols::kMonetary SeparatorSymbol)); 1359 UnicodeString monDecSeparator(fmt->getSymbol(DecimalFormatSymbols::kMonetary SeparatorSymbol));
1350 if (currency == UnicodeString("") || 1360 if (currency == UnicodeString("") ||
1351 intlCurrency == UnicodeString("") || 1361 intlCurrency == UnicodeString("") ||
1352 monDecSeparator == UnicodeString("")) 1362 monDecSeparator == UnicodeString(""))
1353 { 1363 {
1354 errln("getCurrencySymbols failed, got empty string."); 1364 errln("getCurrencySymbols failed, got empty string.");
1355 } 1365 }
1356 UnicodeString monDecSeparatorStr; 1366 UnicodeString monDecSeparatorStr;
(...skipping 17 matching lines...) Expand all
1374 delete fmt; 1384 delete fmt;
1375 } 1385 }
1376 /* @bug 4062486 1386 /* @bug 4062486
1377 * API tests for API addition request A23. FieldPosition.getBeginIndex and 1387 * API tests for API addition request A23. FieldPosition.getBeginIndex and
1378 * FieldPosition.getEndIndex. 1388 * FieldPosition.getEndIndex.
1379 */ 1389 */
1380 void NumberFormatRegressionTest::Test4062486(void) 1390 void NumberFormatRegressionTest::Test4062486(void)
1381 { 1391 {
1382 UErrorCode status = U_ZERO_ERROR; 1392 UErrorCode status = U_ZERO_ERROR;
1383 DecimalFormat *fmt = new DecimalFormat(UnicodeString("#,##0.00"), status); 1393 DecimalFormat *fmt = new DecimalFormat(UnicodeString("#,##0.00"), status);
1384 failure(status, "new DecimalFormat"); 1394 if (U_FAILURE(status)) {
1395 dataerrln("Error creating DecimalFormat - %s", u_errorName(status));
1396 return;
1397 }
1385 UnicodeString formatted; 1398 UnicodeString formatted;
1386 FieldPosition field(0); 1399 FieldPosition field(0);
1387 double num = 1234.5; 1400 double num = 1234.5;
1388 fmt->format(num, formatted, field); 1401 fmt->format(num, formatted, field);
1389 if (field.getBeginIndex() != 0 && field.getEndIndex() != 5) 1402 if (field.getBeginIndex() != 0 && field.getEndIndex() != 5)
1390 errln(UnicodeString("Format 1234.5 failed. Begin index: ") /*+ field.get BeginIndex() + " End index: " + field.getEndIndex()*/); 1403 errln(UnicodeString("Format 1234.5 failed. Begin index: ") /*+ field.get BeginIndex() + " End index: " + field.getEndIndex()*/);
1391 field.setBeginIndex(7); 1404 field.setBeginIndex(7);
1392 field.setEndIndex(4); 1405 field.setEndIndex(4);
1393 if (field.getBeginIndex() != 7 && field.getEndIndex() != 4) 1406 if (field.getBeginIndex() != 7 && field.getEndIndex() != 4)
1394 errln("Set begin/end field indexes failed. Begin index: " /*+ field.getB eginIndex() + " End index: " + field.getEndIndex()*/); 1407 errln("Set begin/end field indexes failed. Begin index: " /*+ field.getB eginIndex() + " End index: " + field.getEndIndex()*/);
(...skipping 1036 matching lines...) Expand 10 before | Expand all | Expand 10 after
2431 " -> \"" + pat + 2444 " -> \"" + pat +
2432 "\" -> \"" + f2.toPattern(p) + "\""); 2445 "\" -> \"" + f2.toPattern(p) + "\"");
2433 } else { 2446 } else {
2434 UnicodeString l, p; 2447 UnicodeString l, p;
2435 logln(UnicodeString("PASS: ") + type[j] + avail[i].getDisplayNam e(l) + 2448 logln(UnicodeString("PASS: ") + type[j] + avail[i].getDisplayNam e(l) +
2436 " -> \"" + pat + 2449 " -> \"" + pat +
2437 "\""); 2450 "\"");
2438 } 2451 }
2439 2452
2440 // Test toLocalizedPattern/applyLocalizedPattern round trip 2453 // Test toLocalizedPattern/applyLocalizedPattern round trip
2441 df->toLocalizedPattern(pat); 2454 // TODO(refactor): don't worry about localized patterns for now.
2442 f2.applyLocalizedPattern(pat, status); 2455 // df->toLocalizedPattern(pat);
2456 // f2.applyLocalizedPattern(pat, status);
2443 failure(status, 2457 failure(status,
2444 UnicodeString("applyLocalizedPattern(") + pat + ")", avail[i ]); 2458 UnicodeString("applyLocalizedPattern(") + pat + ")", avail[i ]);
2445 if (U_FAILURE(status)) { 2459 if (U_FAILURE(status)) {
2446 continue; 2460 continue;
2447 } 2461 }
2448 2462
2463 // TODO(refactor): don't worry about localized patterns for now.
2449 // Make sure we set the currency attributes appropriately 2464 // Make sure we set the currency attributes appropriately
2450 if (j == 1) { // Currency format 2465 // if (j == 1) { // Currency format
2451 f2.setCurrency(f2.getCurrency(), status); 2466 // f2.setCurrency(f2.getCurrency(), status);
2452 } 2467 // }
2453 failure(status, 2468 failure(status,
2454 UnicodeString("setCurrency() for (") + pat + ")", avail[i]); 2469 UnicodeString("setCurrency() for (") + pat + ")", avail[i]);
2455 if (U_FAILURE(status)) { 2470 if (U_FAILURE(status)) {
2456 continue; 2471 continue;
2457 } 2472 }
2458 2473
2459 if (*df != f2) { 2474 if (*df != f2) {
2460 UnicodeString l, p; 2475 UnicodeString l, p;
2461 errln(UnicodeString("FAIL: ") + type[j] + avail[i].getDisplayNam e(l) + 2476 errln(UnicodeString("FAIL: ") + type[j] + avail[i].getDisplayNam e(l) +
2462 " -> localized \"" + pat + 2477 " -> localized \"" + pat +
(...skipping 240 matching lines...) Expand 10 before | Expand all | Expand 10 after
2703 dataerrln("File %s, Line %d: status=%s", __FILE__, __LINE__, u_error Name(status)); \ 2718 dataerrln("File %s, Line %d: status=%s", __FILE__, __LINE__, u_error Name(status)); \
2704 } else { \ 2719 } else { \
2705 errln("File %s, Line %d: status=%s", __FILE__, __LINE__, u_errorName (status)); \ 2720 errln("File %s, Line %d: status=%s", __FILE__, __LINE__, u_errorName (status)); \
2706 } return; \ 2721 } return; \
2707 }} 2722 }}
2708 2723
2709 #define TEST_ASSERT(expr) \ 2724 #define TEST_ASSERT(expr) \
2710 if ((expr)==FALSE) {\ 2725 if ((expr)==FALSE) {\
2711 errln("File %s, line %d: Assertion Failed: " #expr "\n", __FILE__, __LIN E__);\ 2726 errln("File %s, line %d: Assertion Failed: " #expr "\n", __FILE__, __LIN E__);\
2712 } 2727 }
2728 #define TEST_ASSERT_EQUALS(x,y) \
2729 { \
2730 char _msg[1000]; \
2731 int32_t len = sprintf (_msg,"File %s, line %d: Assertion Failed: " #x "==" #y "\n", __FILE__, __LINE__); \
2732 U_ASSERT(len < (int32_t) sizeof(_msg)); \
2733 assertEquals((const char*) _msg, x,y); \
2734 }
2713 2735
2714 2736
2715 // Ticket 8199: Parse failure for numbers in the range of 1E10 - 1E18 2737 // Ticket 8199: Parse failure for numbers in the range of 1E10 - 1E18
2716 2738
2717 void NumberFormatRegressionTest::Test8199(void) { 2739 void NumberFormatRegressionTest::Test8199(void) {
2718 UErrorCode status = U_ZERO_ERROR; 2740 UErrorCode status = U_ZERO_ERROR;
2719 NumberFormat *nf = NumberFormat::createInstance(Locale::getEnglish(), status ); 2741 NumberFormat *nf = NumberFormat::createInstance(Locale::getEnglish(), status );
2720 if (nf == NULL) { 2742 if (nf == NULL) {
2721 dataerrln("Fail: NumberFormat::createInstance(Locale::getEnglish(), stat us)"); 2743 dataerrln("Fail: NumberFormat::createInstance(Locale::getEnglish(), stat us)");
2722 return; 2744 return;
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after
2820 TEST_ASSERT(Formattable::kDouble == val.getType()); 2842 TEST_ASSERT(Formattable::kDouble == val.getType());
2821 TEST_ASSERT(9007199254740992LL == val.getInt64(status)); 2843 TEST_ASSERT(9007199254740992LL == val.getInt64(status));
2822 TEST_ASSERT(9007199254740992.0 == val.getDouble(status)); 2844 TEST_ASSERT(9007199254740992.0 == val.getDouble(status));
2823 TEST_CHECK_STATUS(status); 2845 TEST_CHECK_STATUS(status);
2824 2846
2825 status = U_ZERO_ERROR; 2847 status = U_ZERO_ERROR;
2826 numStr = "9007199254740993.1"; // 54 bits for the int part. Double will ro und 2848 numStr = "9007199254740993.1"; // 54 bits for the int part. Double will ro und
2827 nf->parse(numStr, val, status); // the ones digit, putting it up to ...99 4 2849 nf->parse(numStr, val, status); // the ones digit, putting it up to ...99 4
2828 TEST_CHECK_STATUS(status); 2850 TEST_CHECK_STATUS(status);
2829 TEST_ASSERT(Formattable::kDouble == val.getType()); 2851 TEST_ASSERT(Formattable::kDouble == val.getType());
2830 TEST_ASSERT(9007199254740993LL == val.getInt64(status)); 2852 TEST_ASSERT_EQUALS((int64_t)9007199254740993LL,val.getInt64(status));
2831 TEST_ASSERT(9007199254740994.0 == val.getDouble(status)); 2853 TEST_ASSERT_EQUALS((double)9007199254740994.0,(double)val.getDouble(status)) ;
2832 TEST_CHECK_STATUS(status); 2854 TEST_CHECK_STATUS(status);
2833 2855
2834 delete nf; 2856 delete nf;
2835 } 2857 }
2836 2858
2837 void NumberFormatRegressionTest::Test9109(void) { 2859 void NumberFormatRegressionTest::Test9109(void) {
2838 UErrorCode status = U_ZERO_ERROR; 2860 UErrorCode status = U_ZERO_ERROR;
2839 Formattable val; 2861 Formattable val;
2840 ParsePosition pos; 2862 ParsePosition pos;
2841 DecimalFormat fmt("+##", status); 2863 DecimalFormat fmt("+##", status);
2842 fmt.setLenient(TRUE);
2843
2844 if (U_FAILURE(status)) { 2864 if (U_FAILURE(status)) {
2845 dataerrln("Failed to create DecimalFormat with pattern '+##' - %s", u_er rorName(status)); 2865 dataerrln("Failed to create DecimalFormat with pattern '+##' - %s", u_er rorName(status));
2866 return;
2846 } 2867 }
2847 2868
2869 fmt.setLenient(TRUE);
2848 UnicodeString text("123"); 2870 UnicodeString text("123");
2849 int32_t expected = 123; 2871 int32_t expected = 123;
2850 int32_t expos = 3; 2872 int32_t expos = 3;
2851 2873
2852 fmt.parse(text, val, pos); 2874 fmt.parse(text, val, pos);
2853 if (pos.getErrorIndex() >= 0) { 2875 if (pos.getErrorIndex() >= 0) {
2854 errln(UnicodeString("Parse failure at ") + pos.getErrorIndex()); 2876 errln(UnicodeString("Parse failure at ") + pos.getErrorIndex());
2855 } else if (val.getLong() != 123) { 2877 } else if (val.getLong() != 123) {
2856 errln(UnicodeString("Incorrect parse result: ") + val.getLong() + " expe cted: " + expected); 2878 errln(UnicodeString("Incorrect parse result: ") + val.getLong() + " expe cted: " + expected);
2857 } else if (pos.getIndex() != 3) { 2879 } else if (pos.getIndex() != 3) {
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after
2988 } 3010 }
2989 } 3011 }
2990 } 3012 }
2991 } 3013 }
2992 3014
2993 void NumberFormatRegressionTest::Test10361(void) { 3015 void NumberFormatRegressionTest::Test10361(void) {
2994 // DecimalFormat/NumberFormat were artificially limiting the number of digit s, 3016 // DecimalFormat/NumberFormat were artificially limiting the number of digit s,
2995 // preventing formatting of big decimals. 3017 // preventing formatting of big decimals.
2996 UErrorCode status = U_ZERO_ERROR; 3018 UErrorCode status = U_ZERO_ERROR;
2997 DecimalFormatSymbols symbols(Locale::getEnglish(), status); 3019 DecimalFormatSymbols symbols(Locale::getEnglish(), status);
2998 LocalPointer<DecimalFormat> df(new DecimalFormat("###.##", symbols, status)) ; 3020 LocalPointer<DecimalFormat> df(new DecimalFormat("###.##", symbols, status), status);
2999 TEST_CHECK_STATUS(status); 3021 TEST_CHECK_STATUS(status);
3000 3022
3001 // Create a decimal number with a million digits. 3023 // Create a decimal number with a million digits.
3002 const int32_t NUMSIZE=1000000; 3024 const int32_t NUMSIZE=1000000;
3003 char *num = new char[NUMSIZE]; 3025 char *num = new char[NUMSIZE];
3004 for (int32_t i=0; i<NUMSIZE; i++) { 3026 for (int32_t i=0; i<NUMSIZE; i++) {
3005 num[i] = '0' + (i+1) % 10; 3027 num[i] = '0' + (i+1) % 10;
3006 } 3028 }
3007 num[NUMSIZE-3] = '.'; 3029 num[NUMSIZE-3] = '.';
3008 num[NUMSIZE-1] = 0; 3030 num[NUMSIZE-1] = 0;
3009 3031
3010 UnicodeString s; 3032 UnicodeString s;
3011 Formattable fmtable; 3033 Formattable fmtable;
3012 fmtable.setDecimalNumber(num, status); 3034 fmtable.setDecimalNumber(num, status);
3013 TEST_CHECK_STATUS(status); 3035 TEST_CHECK_STATUS(status);
3014 3036
3015 FieldPosition pos(UNUM_DECIMAL_SEPARATOR_FIELD); 3037 FieldPosition pos(UNUM_DECIMAL_SEPARATOR_FIELD);
3016 df->format(fmtable, s, pos, status); 3038 df->format(fmtable, s, pos, status);
3017 TEST_CHECK_STATUS(status); 3039 TEST_CHECK_STATUS(status);
3018 TEST_ASSERT(999999 == s.length()); 3040 TEST_ASSERT(999999 == s.length());
3019 TEST_ASSERT(999997 == pos.getBeginIndex()); 3041 TEST_ASSERT(999997 == pos.getBeginIndex());
3020 TEST_ASSERT(999998 == pos.getEndIndex()); 3042 TEST_ASSERT(999998 == pos.getEndIndex());
3021 3043
3022 UnicodeString expected(num, -1, US_INV); 3044 UnicodeString expected(num, -1, US_INV);
3023 TEST_ASSERT(expected == s); 3045 TEST_ASSERT(expected == s);
3024 delete [] num; 3046 delete [] num;
3025 } 3047 }
3026 3048
3027 #endif /* #if !UCONFIG_NO_FORMATTING */ 3049 #endif /* #if !UCONFIG_NO_FORMATTING */
OLDNEW
« no previous file with comments | « source/test/intltest/numfmtst.cpp ('k') | source/test/intltest/pluralmaptest.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698