OLD | NEW |
1 /* | 1 /* |
2 ******************************************************************************* | 2 ******************************************************************************* |
3 * Copyright (C) 2007-2013, International Business Machines Corporation and | 3 * Copyright (C) 2007-2014, International Business Machines Corporation and |
4 * others. All Rights Reserved. | 4 * others. All Rights Reserved. |
5 ******************************************************************************** | 5 ******************************************************************************** |
6 | 6 |
7 * File PLURRULTS.cpp | 7 * File PLURRULTS.cpp |
8 * | 8 * |
9 ******************************************************************************** | 9 ******************************************************************************** |
10 */ | 10 */ |
11 | 11 |
12 #include "unicode/utypes.h" | 12 #include "unicode/utypes.h" |
13 | 13 |
14 #if !UCONFIG_NO_FORMATTING | 14 #if !UCONFIG_NO_FORMATTING |
15 | 15 |
16 #include <stdlib.h> | 16 #include <stdlib.h> |
17 #include <stdarg.h> | 17 #include <stdarg.h> |
18 #include <string.h> | 18 #include <string.h> |
19 | 19 |
20 #include "unicode/localpointer.h" | 20 #include "unicode/localpointer.h" |
21 #include "unicode/plurrule.h" | 21 #include "unicode/plurrule.h" |
22 #include "unicode/stringpiece.h" | 22 #include "unicode/stringpiece.h" |
23 | 23 |
24 #include "cmemory.h" | 24 #include "cmemory.h" |
25 #include "digitlst.h" | 25 #include "digitlst.h" |
26 #include "plurrule_impl.h" | 26 #include "plurrule_impl.h" |
27 #include "plurults.h" | 27 #include "plurults.h" |
28 #include "uhash.h" | 28 #include "uhash.h" |
29 | 29 |
30 #define LENGTHOF(array) (int32_t)(sizeof(array)/sizeof(array[0])) | |
31 | |
32 void setupResult(const int32_t testSource[], char result[], int32_t* max); | 30 void setupResult(const int32_t testSource[], char result[], int32_t* max); |
33 UBool checkEqual(const PluralRules &test, char *result, int32_t max); | 31 UBool checkEqual(const PluralRules &test, char *result, int32_t max); |
34 UBool testEquality(const PluralRules &test); | 32 UBool testEquality(const PluralRules &test); |
35 | 33 |
36 // This is an API test, not a unit test. It doesn't test very many cases, and d
oesn't | 34 // This is an API test, not a unit test. It doesn't test very many cases, and d
oesn't |
37 // try to test the full functionality. It just calls each function in the class
and | 35 // try to test the full functionality. It just calls each function in the class
and |
38 // verifies that it works on a basic level. | 36 // verifies that it works on a basic level. |
39 | 37 |
40 void PluralRulesTest::runIndexedTest( int32_t index, UBool exec, const char* &na
me, char* /*par*/ ) | 38 void PluralRulesTest::runIndexedTest( int32_t index, UBool exec, const char* &na
me, char* /*par*/ ) |
41 { | 39 { |
(...skipping 172 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
214 UnicodeString decimalRule= UNICODE_STRING_SIMPLE("a: n not in 0..100;"); | 212 UnicodeString decimalRule= UNICODE_STRING_SIMPLE("a: n not in 0..100;"); |
215 UnicodeString KEYWORD_A = UNICODE_STRING_SIMPLE("a"); | 213 UnicodeString KEYWORD_A = UNICODE_STRING_SIMPLE("a"); |
216 status = U_ZERO_ERROR; | 214 status = U_ZERO_ERROR; |
217 newRules.adoptInstead(test->createRules(decimalRule, status)); | 215 newRules.adoptInstead(test->createRules(decimalRule, status)); |
218 if (U_FAILURE(status)) { | 216 if (U_FAILURE(status)) { |
219 dataerrln("ERROR: Could not create PluralRules for testing fractions - e
xitting"); | 217 dataerrln("ERROR: Could not create PluralRules for testing fractions - e
xitting"); |
220 return; | 218 return; |
221 } | 219 } |
222 double fData[] = {-101, -100, -1, -0.0, 0, 0.1, 1, 1.999,
2.0, 100, 100.001 }; | 220 double fData[] = {-101, -100, -1, -0.0, 0, 0.1, 1, 1.999,
2.0, 100, 100.001 }; |
223 UBool isKeywordA[] = {TRUE, FALSE, FALSE, FALSE, FALSE, TRUE, FALSE, TRUE,
FALSE, FALSE, TRUE }; | 221 UBool isKeywordA[] = {TRUE, FALSE, FALSE, FALSE, FALSE, TRUE, FALSE, TRUE,
FALSE, FALSE, TRUE }; |
224 for (int32_t i=0; i<LENGTHOF(fData); i++) { | 222 for (int32_t i=0; i<UPRV_LENGTHOF(fData); i++) { |
225 if ((newRules->select(fData[i])== KEYWORD_A) != isKeywordA[i]) { | 223 if ((newRules->select(fData[i])== KEYWORD_A) != isKeywordA[i]) { |
226 errln("File %s, Line %d, ERROR: plural rules for decimal fractions
test failed!\n" | 224 errln("File %s, Line %d, ERROR: plural rules for decimal fractions
test failed!\n" |
227 " number = %g, expected %s", __FILE__, __LINE__, fData[i], i
sKeywordA?"TRUE":"FALSE"); | 225 " number = %g, expected %s", __FILE__, __LINE__, fData[i], i
sKeywordA[i]?"TRUE":"FALSE"); |
228 } | 226 } |
229 } | 227 } |
230 | 228 |
231 // ======= Test Equality | 229 // ======= Test Equality |
232 logln("Testing Equality of PluralRules"); | 230 logln("Testing Equality of PluralRules"); |
233 | 231 |
234 if ( !testEquality(*test) ) { | 232 if ( !testEquality(*test) ) { |
235 errln("ERROR: complex plural rules failed! - exitting"); | 233 errln("ERROR: complex plural rules failed! - exitting"); |
236 return; | 234 return; |
237 } | 235 } |
(...skipping 153 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
391 if (U_FAILURE(status)) { | 389 if (U_FAILURE(status)) { |
392 break; | 390 break; |
393 } | 391 } |
394 StringEnumeration *keywords = rules->getKeywords(status); | 392 StringEnumeration *keywords = rules->getKeywords(status); |
395 if (U_FAILURE(status)) { | 393 if (U_FAILURE(status)) { |
396 delete rules; | 394 delete rules; |
397 break; | 395 break; |
398 } | 396 } |
399 const UnicodeString* keyword; | 397 const UnicodeString* keyword; |
400 while (NULL != (keyword = keywords->snext(status))) { | 398 while (NULL != (keyword = keywords->snext(status))) { |
401 int32_t count = rules->getSamples(*keyword, values, LENGTHOF(values), stat
us); | 399 int32_t count = rules->getSamples(*keyword, values, UPRV_LENGTHOF(values),
status); |
402 if (U_FAILURE(status)) { | 400 if (U_FAILURE(status)) { |
403 errln(UNICODE_STRING_SIMPLE("getSamples() failed for locale ") + | 401 errln(UNICODE_STRING_SIMPLE("getSamples() failed for locale ") + |
404 locales[i].getName() + | 402 locales[i].getName() + |
405 UNICODE_STRING_SIMPLE(", keyword ") + *keyword); | 403 UNICODE_STRING_SIMPLE(", keyword ") + *keyword); |
406 continue; | 404 continue; |
407 } | 405 } |
408 if (count == 0) { | 406 if (count == 0) { |
409 // TODO: Lots of these. | 407 // TODO: Lots of these. |
410 // errln(UNICODE_STRING_SIMPLE("no samples for keyword ") + *keyword +
UNICODE_STRING_SIMPLE(" in locale ") + locales[i].getName() ); | 408 // errln(UNICODE_STRING_SIMPLE("no samples for keyword ") + *keyword +
UNICODE_STRING_SIMPLE(" in locale ") + locales[i].getName() ); |
411 } | 409 } |
412 if (count > LENGTHOF(values)) { | 410 if (count > UPRV_LENGTHOF(values)) { |
413 errln(UNICODE_STRING_SIMPLE("getSamples()=") + count + | 411 errln(UNICODE_STRING_SIMPLE("getSamples()=") + count + |
414 UNICODE_STRING_SIMPLE(", too many values, for locale ") + | 412 UNICODE_STRING_SIMPLE(", too many values, for locale ") + |
415 locales[i].getName() + | 413 locales[i].getName() + |
416 UNICODE_STRING_SIMPLE(", keyword ") + *keyword); | 414 UNICODE_STRING_SIMPLE(", keyword ") + *keyword); |
417 count = LENGTHOF(values); | 415 count = UPRV_LENGTHOF(values); |
418 } | 416 } |
419 for (int32_t j = 0; j < count; ++j) { | 417 for (int32_t j = 0; j < count; ++j) { |
420 if (values[j] == UPLRULES_NO_UNIQUE_VALUE) { | 418 if (values[j] == UPLRULES_NO_UNIQUE_VALUE) { |
421 errln("got 'no unique value' among values"); | 419 errln("got 'no unique value' among values"); |
422 } else { | 420 } else { |
423 UnicodeString resultKeyword = rules->select(values[j]); | 421 UnicodeString resultKeyword = rules->select(values[j]); |
424 // if (strcmp(locales[i].getName(), "uk") == 0) { // Debug only. | 422 // if (strcmp(locales[i].getName(), "uk") == 0) { // Debug only. |
425 // std::cout << " uk " << US(resultKeyword).cstr() << " " << valu
es[j] << std::endl; | 423 // std::cout << " uk " << US(resultKeyword).cstr() << " " << valu
es[j] << std::endl; |
426 // } | 424 // } |
427 if (*keyword != resultKeyword) { | 425 if (*keyword != resultKeyword) { |
(...skipping 504 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
932 "a: n is 13, 15, b: n is 4", | 930 "a: n is 13, 15, b: n is 4", |
933 "a: n is 1, 3, 4.. ", | 931 "a: n is 1, 3, 4.. ", |
934 "a: n within 5..4", | 932 "a: n within 5..4", |
935 "A: n is 13", // Uppercase keywords not allowed. | 933 "A: n is 13", // Uppercase keywords not allowed. |
936 "a: n ! = 3", // spaces in != operator | 934 "a: n ! = 3", // spaces in != operator |
937 "a: n = not 3", // '=' not exact equivalent of 'is' | 935 "a: n = not 3", // '=' not exact equivalent of 'is' |
938 "a: n ! in 3..4" // '!' not exact equivalent of 'not' | 936 "a: n ! in 3..4" // '!' not exact equivalent of 'not' |
939 "a: n % 37 ! in 3..4" | 937 "a: n % 37 ! in 3..4" |
940 | 938 |
941 }; | 939 }; |
942 for (int i=0; i<LENGTHOF(testCases); i++) { | 940 for (int i=0; i<UPRV_LENGTHOF(testCases); i++) { |
943 const char *rules = testCases[i]; | 941 const char *rules = testCases[i]; |
944 UErrorCode status = U_ZERO_ERROR; | 942 UErrorCode status = U_ZERO_ERROR; |
945 PluralRules *pr = PluralRules::createRules(UnicodeString(rules), status)
; | 943 PluralRules *pr = PluralRules::createRules(UnicodeString(rules), status)
; |
946 if (U_SUCCESS(status)) { | 944 if (U_SUCCESS(status)) { |
947 errln("file %s, line %d, expected failure with \"%s\".", __FILE__, _
_LINE__, rules); | 945 errln("file %s, line %d, expected failure with \"%s\".", __FILE__, _
_LINE__, rules); |
948 } | 946 } |
949 if (pr != NULL) { | 947 if (pr != NULL) { |
950 errln("file %s, line %d, expected NULL. Rules: \"%s\"", __FILE__, __
LINE__, rules); | 948 errln("file %s, line %d, expected NULL. Rules: \"%s\"", __FILE__, __
LINE__, rules); |
951 } | 949 } |
952 } | 950 } |
(...skipping 21 matching lines...) Expand all Loading... |
974 {.1234, 4, 1234}, | 972 {.1234, 4, 1234}, |
975 {.01234, 5, 1234}, | 973 {.01234, 5, 1234}, |
976 {.001234, 6, 1234}, | 974 {.001234, 6, 1234}, |
977 {.0001234, 7, 1234}, | 975 {.0001234, 7, 1234}, |
978 {100.1234, 4, 1234}, | 976 {100.1234, 4, 1234}, |
979 {100.01234, 5, 1234}, | 977 {100.01234, 5, 1234}, |
980 {100.001234, 6, 1234}, | 978 {100.001234, 6, 1234}, |
981 {100.0001234, 7, 1234} | 979 {100.0001234, 7, 1234} |
982 }; | 980 }; |
983 | 981 |
984 for (int i=0; i<LENGTHOF(testCases); ++i) { | 982 for (int i=0; i<UPRV_LENGTHOF(testCases); ++i) { |
985 DoubleTestCase &tc = testCases[i]; | 983 DoubleTestCase &tc = testCases[i]; |
986 int32_t numFractionDigits = FixedDecimal::decimals(tc.n); | 984 int32_t numFractionDigits = FixedDecimal::decimals(tc.n); |
987 if (numFractionDigits != tc.fractionDigitCount) { | 985 if (numFractionDigits != tc.fractionDigitCount) { |
988 errln("file %s, line %d: decimals(%g) expected %d, actual %d", | 986 errln("file %s, line %d: decimals(%g) expected %d, actual %d", |
989 __FILE__, __LINE__, tc.n, tc.fractionDigitCount, numFractionD
igits); | 987 __FILE__, __LINE__, tc.n, tc.fractionDigitCount, numFractionD
igits); |
990 continue; | 988 continue; |
991 } | 989 } |
992 int64_t actualFractionDigits = FixedDecimal::getFractionalDigits(tc.n, n
umFractionDigits); | 990 int64_t actualFractionDigits = FixedDecimal::getFractionalDigits(tc.n, n
umFractionDigits); |
993 if (actualFractionDigits != tc.fractionDigits) { | 991 if (actualFractionDigits != tc.fractionDigits) { |
994 errln("file %s, line %d: getFractionDigits(%g, %d): expected %ld, go
t %ld", | 992 errln("file %s, line %d: getFractionDigits(%g, %d): expected %ld, go
t %ld", |
995 __FILE__, __LINE__, tc.n, numFractionDigits, tc.fractionDigits
, actualFractionDigits); | 993 __FILE__, __LINE__, tc.n, numFractionDigits, tc.fractionDigits
, actualFractionDigits); |
996 } | 994 } |
997 } | 995 } |
998 } | 996 } |
999 | 997 |
1000 | 998 |
1001 | 999 |
1002 #endif /* #if !UCONFIG_NO_FORMATTING */ | 1000 #endif /* #if !UCONFIG_NO_FORMATTING */ |
OLD | NEW |