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