1 /********************************************************************
2 * Copyright (c) 2011-2014, International Business Machines Corporation
3 * and others. All Rights Reserved.
4 ********************************************************************/
5 /* C API TEST FOR PLURAL RULES */
6
7 #include "unicode/utypes.h"
8
9 #if !UCONFIG_NO_FORMATTING
10
11 #include "unicode/upluralrules.h"
12 #include "unicode/ustring.h"
13 #include "cintltst.h"
14 #include "cmemory.h"
15
16 static void TestPluralRules(void);
17 static void TestOrdinalRules(void);
18
19 void addPluralRulesTest(TestNode** root);
20
21 #define TESTCASE(x) addTest(root, &x, "tsformat/cpluralrulestest/" #x)
22
addPluralRulesTest(TestNode ** root)23 void addPluralRulesTest(TestNode** root)
24 {
25 TESTCASE(TestPluralRules);
26 TESTCASE(TestOrdinalRules);
27 }
28
29 typedef struct {
30 const char * locale;
31 double number;
32 const char * keywordExpected;
33 } PluralRulesTestItem;
34
35 /* Just a small set of tests for now, other functionality is tested in the C++ tests */
36 static const PluralRulesTestItem testItems[] = {
37 { "en", 0, "other" },
38 { "en", 0.5, "other" },
39 { "en", 1, "one" },
40 { "en", 1.5, "other" },
41 { "en", 2, "other" },
42 { "fr", 0, "one" },
43 { "fr", 0.5, "one" },
44 { "fr", 1, "one" },
45 { "fr", 1.5, "one" },
46 { "fr", 2, "other" },
47 { "ru", 0, "many" },
48 { "ru", 0.5, "other" },
49 { "ru", 1, "one" },
50 { "ru", 1.5, "other" },
51 { "ru", 2, "few" },
52 { "ru", 5, "many" },
53 { "ru", 10, "many" },
54 { "ru", 11, "many" },
55 { NULL, 0, NULL }
56 };
57
58 enum {
59 kKeywordBufLen = 32
60 };
61
TestPluralRules()62 static void TestPluralRules()
63 {
64 const PluralRulesTestItem * testItemPtr;
65 log_verbose("\nTesting uplrules_open() and uplrules_select() with various parameters\n");
66 for ( testItemPtr = testItems; testItemPtr->locale != NULL; ++testItemPtr ) {
67 UErrorCode status = U_ZERO_ERROR;
68 UPluralRules* uplrules = uplrules_open(testItemPtr->locale, &status);
69 if ( U_SUCCESS(status) ) {
70 UChar keyword[kKeywordBufLen];
71 UChar keywordExpected[kKeywordBufLen];
72 int32_t keywdLen = uplrules_select(uplrules, testItemPtr->number, keyword, kKeywordBufLen, &status);
73 if (keywdLen >= kKeywordBufLen) {
74 keyword[kKeywordBufLen-1] = 0;
75 }
76 if ( U_SUCCESS(status) ) {
77 u_unescape(testItemPtr->keywordExpected, keywordExpected, kKeywordBufLen);
78 if ( u_strcmp(keyword, keywordExpected) != 0 ) {
79 char bcharBuf[kKeywordBufLen];
80 log_data_err("ERROR: uplrules_select for locale %s, number %.1f: expect %s, get %s\n",
81 testItemPtr->locale, testItemPtr->number, testItemPtr->keywordExpected, u_austrcpy(bcharBuf,keyword) );
82 }
83 } else {
84 log_err("FAIL: uplrules_select for locale %s, number %.1f: %s\n",
85 testItemPtr->locale, testItemPtr->number, myErrorName(status) );
86 }
87 uplrules_close(uplrules);
88 } else {
89 log_err("FAIL: uplrules_open for locale %s: %s\n", testItemPtr->locale, myErrorName(status) );
90 }
91 }
92 }
93
TestOrdinalRules()94 static void TestOrdinalRules() {
95 U_STRING_DECL(two, "two", 3);
96 UChar keyword[8];
97 int32_t length;
98 UErrorCode errorCode = U_ZERO_ERROR;
99 UPluralRules* upr = uplrules_openForType("en", UPLURAL_TYPE_ORDINAL, &errorCode);
100 if (U_FAILURE(errorCode)) {
101 log_err("uplrules_openForType(en, ordinal) failed - %s\n", u_errorName(errorCode));
102 return;
103 }
104 U_STRING_INIT(two, "two", 3);
105 length = uplrules_select(upr, 2., keyword, 8, &errorCode);
106 if (U_FAILURE(errorCode) || u_strCompare(keyword, length, two, 3, FALSE) != 0) {
107 log_data_err("uplrules_select(en-ordinal, 2) failed - %s\n", u_errorName(errorCode));
108 }
109 uplrules_close(upr);
110 }
111
112 #endif /* #if !UCONFIG_NO_FORMATTING */
113