2 *******************************************************************************
3 * Copyright (C) 2007-2008, International Business Machines Corporation and
4 * others. All Rights Reserved.
5 ********************************************************************************
9 ********************************************************************************
12 #include "unicode/utypes.h"
14 #if !UCONFIG_NO_FORMATTING
17 #include "unicode/plurrule.h"
21 void setupResult(const int32_t testSource
[], char result
[], int32_t* max
);
22 UBool
checkEqual(PluralRules
*test
, char *result
, int32_t max
);
23 UBool
testEquality(PluralRules
*test
);
25 // This is an API test, not a unit test. It doesn't test very many cases, and doesn't
26 // try to test the full functionality. It just calls each function in the class and
27 // verifies that it works on a basic level.
29 void PluralRulesTest::runIndexedTest( int32_t index
, UBool exec
, const char* &name
, char* /*par*/ )
31 if (exec
) logln("TestSuite PluralRulesAPI");
34 default: name
= ""; break;
38 #define PLURAL_TEST_NUM 18
40 * Test various generic API methods of PluralRules for API coverage.
42 void PluralRulesTest::testAPI(/*char *par*/)
44 UnicodeString pluralTestData
[PLURAL_TEST_NUM
] = {
45 UNICODE_STRING_SIMPLE("a: n is 1"),
46 UNICODE_STRING_SIMPLE("a: n mod 10 is 2"),
47 UNICODE_STRING_SIMPLE("a: n is not 1"),
48 UNICODE_STRING_SIMPLE("a: n mod 3 is not 1"),
49 UNICODE_STRING_SIMPLE("a: n in 2..5"),
50 UNICODE_STRING_SIMPLE("a: n within 2..5"),
51 UNICODE_STRING_SIMPLE("a: n not in 2..5"),
52 UNICODE_STRING_SIMPLE("a: n not within 2..5"),
53 UNICODE_STRING_SIMPLE("a: n mod 10 in 2..5"),
54 UNICODE_STRING_SIMPLE("a: n mod 10 within 2..5"),
55 UNICODE_STRING_SIMPLE("a: n mod 10 is 2 and n is not 12"),
56 UNICODE_STRING_SIMPLE("a: n mod 10 in 2..3 or n mod 10 is 5"),
57 UNICODE_STRING_SIMPLE("a: n mod 10 within 2..3 or n mod 10 is 5"),
58 UNICODE_STRING_SIMPLE("a: n is 1 or n is 4 or n is 23"),
59 UNICODE_STRING_SIMPLE("a: n mod 2 is 1 and n is not 3 and n in 1..11"),
60 UNICODE_STRING_SIMPLE("a: n mod 2 is 1 and n is not 3 and n within 1..11"),
61 UNICODE_STRING_SIMPLE("a: n mod 2 is 1 or n mod 5 is 1 and n is not 6"),
64 static const int32_t pluralTestResult
[PLURAL_TEST_NUM
][30] = {
73 {2,3,4,5,12,13,14,15,22,23,24,25,0},
74 {2,3,4,5,12,13,14,15,22,23,24,25,0},
76 {2,3,5,12,13,15,22,23,25,0},
77 {2,3,5,12,13,15,22,23,25,0},
81 {1,3,5,7,9,11,13,15,16,0},
83 UErrorCode status
= U_ZERO_ERROR
;
85 // ======= Test constructors
86 logln("Testing PluralRules constructors");
89 logln("\n start default locale test case ..\n");
91 PluralRules
defRule(status
);
92 PluralRules
* test
=new PluralRules(status
);
93 PluralRules
* newEnPlural
= test
->forLocale(Locale::getEnglish(), status
);
94 if(U_FAILURE(status
)) {
95 dataerrln("ERROR: Could not create PluralRules (default) - exitting");
100 // ======= Test clone, assignment operator && == operator.
101 PluralRules
*dupRule
= defRule
.clone();
103 if ( *dupRule
!= defRule
) {
104 errln("ERROR: clone plural rules test failed!");
107 *dupRule
= *newEnPlural
;
109 if ( *dupRule
!= *newEnPlural
) {
110 errln("ERROR: clone plural rules test failed!");
117 // ======= Test empty plural rules
118 logln("Testing Simple PluralRules");
120 PluralRules
* empRule
= test
->createRules(UNICODE_STRING_SIMPLE("a:n"), status
);
122 for (int32_t i
=0; i
<10; ++i
) {
123 key
= empRule
->select(i
);
124 if ( key
.charAt(0)!= 0x61 ) { // 'a'
125 errln("ERROR: empty plural rules test failed! - exitting");
132 // ======= Test simple plural rules
133 logln("Testing Simple PluralRules");
138 for (int32_t i
=0; i
<PLURAL_TEST_NUM
-1; ++i
) {
139 PluralRules
*newRules
= test
->createRules(pluralTestData
[i
], status
);
140 setupResult(pluralTestResult
[i
], result
, &max
);
141 if ( !checkEqual(newRules
, result
, max
) ) {
142 errln("ERROR: simple plural rules failed! - exitting");
146 if (newRules
!=NULL
) {
152 // ======= Test complex plural rules
153 logln("Testing Complex PluralRules");
154 // TODO: the complex test data is hard coded. It's better to implement
155 // a parser to parse the test data.
156 UnicodeString complexRule
= UNICODE_STRING_SIMPLE("a: n in 2..5; b: n in 5..8; c: n mod 2 is 1");
157 UnicodeString complexRule2
= UNICODE_STRING_SIMPLE("a: n within 2..5; b: n within 5..8; c: n mod 2 is 1");
173 PluralRules
*newRules
= test
->createRules(complexRule
, status
);
174 if ( !checkEqual(newRules
, cRuleResult
, 12) ) {
175 errln("ERROR: complex plural rules failed! - exitting");
179 if (newRules
!=NULL
) {
183 newRules
= test
->createRules(complexRule2
, status
);
184 if ( !checkEqual(newRules
, cRuleResult
, 12) ) {
185 errln("ERROR: complex plural rules failed! - exitting");
189 if (newRules
!=NULL
) {
194 // ======= Test decimal fractions plural rules
195 UnicodeString decimalRule
= UNICODE_STRING_SIMPLE("a: n not in 0..100;");
196 UnicodeString KEYWORD_A
= UNICODE_STRING_SIMPLE("a");
197 status
= U_ZERO_ERROR
;
198 newRules
= test
->createRules(decimalRule
, status
);
199 if (U_FAILURE(status
)) {
200 dataerrln("ERROR: Could not create PluralRules for testing fractions - exitting");
204 double fData
[10] = {-100, -1, -0.0, 0, 0.1, 1, 1.999, 2.0, 100, 100.001 };
205 UBool isKeywordA
[10] = {
206 TRUE
, TRUE
, FALSE
, FALSE
, TRUE
, FALSE
, TRUE
, FALSE
, FALSE
, TRUE
};
207 for (int32_t i
=0; i
<10; i
++) {
208 if ((newRules
->select(fData
[i
])== KEYWORD_A
) != isKeywordA
[i
]) {
209 errln("ERROR: plural rules for decimal fractions test failed!");
212 if (newRules
!=NULL
) {
219 // ======= Test Equality
220 logln("Testing Equality of PluralRules");
223 if ( !testEquality(test
) ) {
224 errln("ERROR: complex plural rules failed! - exitting");
230 // ======= Test getStaticClassID()
231 logln("Testing getStaticClassID()");
233 if(test
->getDynamicClassID() != PluralRules::getStaticClassID()) {
234 errln("ERROR: getDynamicClassID() didn't return the expected value");
236 // ====== Test fallback to parent locale
237 PluralRules
*en_UK
= test
->forLocale(Locale::getUK(), status
);
238 PluralRules
*en
= test
->forLocale(Locale::getEnglish(), status
);
239 if (en_UK
!= NULL
&& en
!= NULL
) {
240 if ( *en_UK
!= *en
) {
241 errln("ERROR: test locale fallback failed!");
247 PluralRules
*zh_Hant
= test
->forLocale(Locale::getTaiwan(), status
);
248 PluralRules
*zh
= test
->forLocale(Locale::getChinese(), status
);
249 if (zh_Hant
!= NULL
&& zh
!= NULL
) {
250 if ( *zh_Hant
!= *zh
) {
251 errln("ERROR: test locale fallback failed!");
259 void setupResult(const int32_t testSource
[], char result
[], int32_t* max
) {
264 while (curIndex
< testSource
[i
]) {
265 result
[curIndex
++]=0x6F; //'o' other
267 result
[curIndex
++]=0x61; // 'a'
269 } while(testSource
[++i
]>0);
274 UBool
checkEqual(PluralRules
*test
, char *result
, int32_t max
) {
276 UBool isEqual
= TRUE
;
277 for (int32_t i
=0; i
<max
; ++i
) {
278 key
= test
->select(i
);
279 if ( key
.charAt(0)!=result
[i
] ) {
288 UBool
testEquality(PluralRules
*test
) {
289 UnicodeString testEquRules
[MAX_EQ_ROW
][MAX_EQ_COL
] = {
290 { UNICODE_STRING_SIMPLE("a: n in 2..3"),
291 UNICODE_STRING_SIMPLE("a: n is 2 or n is 3"),
292 UNICODE_STRING_SIMPLE( "a:n is 3 and n in 2..5 or n is 2"),
295 { UNICODE_STRING_SIMPLE("a: n is 12; b:n mod 10 in 2..3"),
296 UNICODE_STRING_SIMPLE("b: n mod 10 in 2..3 and n is not 12; a: n in 12..12"),
297 UNICODE_STRING_SIMPLE("b: n is 13; a: n in 12..13; b: n mod 10 is 2 or n mod 10 is 3"),
301 UErrorCode status
= U_ZERO_ERROR
;
302 UnicodeString key
[MAX_EQ_COL
];
304 for (int32_t i
=0; i
<MAX_EQ_ROW
; ++i
) {
305 PluralRules
* rules
[MAX_EQ_COL
];
307 for (int32_t j
=0; j
<MAX_EQ_COL
; ++j
) {
310 int32_t totalRules
=0;
311 while((totalRules
<MAX_EQ_COL
) && (testEquRules
[i
][totalRules
].length()>0) ) {
312 rules
[totalRules
]=test
->createRules(testEquRules
[i
][totalRules
], status
);
315 for (int32_t n
=0; n
<300 && ret
; ++n
) {
316 for(int32_t j
=0; j
<totalRules
;++j
) {
317 key
[j
] = rules
[j
]->select(n
);
319 for(int32_t j
=0; j
<totalRules
-1;++j
) {
320 if (key
[j
]!=key
[j
+1]) {
327 for (int32_t j
=0; j
<MAX_EQ_COL
; ++j
) {
328 if (rules
[j
]!=NULL
) {
336 #endif /* #if !UCONFIG_NO_FORMATTING */