1 /********************************************************************
2 * Copyright (c) 2011-2012, International Business Machines Corporation
3 * and others. All Rights Reserved.
4 ********************************************************************/
5 /* C API TEST FOR PLURAL RULES */
7 #include "unicode/utypes.h"
9 #if !UCONFIG_NO_FORMATTING
11 #include "unicode/upluralrules.h"
12 #include "unicode/ustring.h"
16 static void TestPluralRules(void);
17 static void TestOrdinalRules(void);
19 void addPluralRulesTest(TestNode
** root
);
21 #define TESTCASE(x) addTest(root, &x, "tsformat/cpluralrulestest/" #x)
23 void addPluralRulesTest(TestNode
** root
)
25 TESTCASE(TestPluralRules
);
26 TESTCASE(TestOrdinalRules
);
32 const char * keywordExpected
;
33 } PluralRulesTestItem
;
35 /* Just a small set of tests for now, other functionality is tested in the C++ tests */
36 static const PluralRulesTestItem testItems
[] = {
38 { "en", 0.5, "other" },
40 { "en", 1.5, "other" },
48 { "ru", 0.5, "other" },
50 { "ru", 1.5, "other" },
62 static void TestPluralRules()
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;
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
) );
84 log_err("FAIL: uplrules_select for locale %s, number %.1f: %s\n",
85 testItemPtr
->locale
, testItemPtr
->number
, myErrorName(status
) );
87 uplrules_close(uplrules
);
89 log_err("FAIL: uplrules_open for locale %s: %s\n", testItemPtr
->locale
, myErrorName(status
) );
94 static void TestOrdinalRules() {
95 U_STRING_DECL(two
, "two", 3);
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
));
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
));
112 #endif /* #if !UCONFIG_NO_FORMATTING */