Home | History | Annotate | Download | only in cintltst
      1 /********************************************************************
      2  * Copyright (c) 2011, 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 
     18 void addPluralRulesTest(TestNode** root);
     19 
     20 #define TESTCASE(x) addTest(root, &x, "tsformat/cpluralrulestest/" #x)
     21 
     22 void addPluralRulesTest(TestNode** root)
     23 {
     24     TESTCASE(TestPluralRules);
     25 }
     26 
     27 typedef struct {
     28     const char * locale;
     29     double       number;
     30     const char * keywordExpected;
     31 } PluralRulesTestItem;
     32 
     33 /* Just a small set of tests for now, other functionality is tested in the C++ tests */
     34 static const PluralRulesTestItem testItems[] = {
     35     { "en",   0, "other" },
     36     { "en", 0.5, "other" },
     37     { "en",   1, "one" },
     38     { "en", 1.5, "other" },
     39     { "en",   2, "other" },
     40     { "fr",   0, "one" },
     41     { "fr", 0.5, "one" },
     42     { "fr",   1, "one" },
     43     { "fr", 1.5, "one" },
     44     { "fr",   2, "other" },
     45     { "ru",   0, "many" },
     46     { "ru", 0.5, "other" },
     47     { "ru",   1, "one" },
     48     { "ru", 1.5, "other" },
     49     { "ru",   2, "few" },
     50     { "ru",   5, "many" },
     51     { "ru",  10, "many" },
     52     { "ru",  11, "many" },
     53     { NULL,   0, NULL }
     54 };
     55 
     56 enum {
     57     kKeywordBufLen = 32
     58 };
     59 
     60 static void TestPluralRules()
     61 {
     62     const PluralRulesTestItem * testItemPtr;
     63     log_verbose("\nTesting uplrules_open() and uplrules_select() with various parameters\n");
     64     for ( testItemPtr = testItems; testItemPtr->locale != NULL; ++testItemPtr ) {
     65         UErrorCode status = U_ZERO_ERROR;
     66         UPluralRules* uplrules = uplrules_open(testItemPtr->locale, &status);
     67         if ( U_SUCCESS(status) ) {
     68             UChar keyword[kKeywordBufLen];
     69             UChar keywordExpected[kKeywordBufLen];
     70             int32_t keywdLen = uplrules_select(uplrules, testItemPtr->number, keyword, kKeywordBufLen, &status);
     71             if (keywdLen >= kKeywordBufLen) {
     72                 keyword[kKeywordBufLen-1] = 0;
     73             }
     74             if ( U_SUCCESS(status) ) {
     75                 u_unescape(testItemPtr->keywordExpected, keywordExpected, kKeywordBufLen);
     76                 if ( u_strcmp(keyword, keywordExpected) != 0 ) {
     77                     char bcharBuf[kKeywordBufLen];
     78                     log_data_err("ERROR: uplrules_select for locale %s, number %.1f: expect %s, get %s\n",
     79                              testItemPtr->locale, testItemPtr->number, testItemPtr->keywordExpected, u_austrcpy(bcharBuf,keyword) );
     80                 }
     81             } else {
     82                 log_err("FAIL: uplrules_select for locale %s, number %.1f: %s\n",
     83                         testItemPtr->locale, testItemPtr->number, myErrorName(status) );
     84             }
     85             uplrules_close(uplrules);
     86         } else {
     87             log_err("FAIL: uplrules_open for locale %s: %s\n", testItemPtr->locale, myErrorName(status) );
     88         }
     89     }
     90 }
     91 
     92 #endif /* #if !UCONFIG_NO_FORMATTING */
     93