1 /********************************************************************
2  * COPYRIGHT:
3  * Copyright (c) 1997-2014, International Business Machines Corporation and
4  * others. All Rights Reserved.
5  ********************************************************************/
6 /* Modification History:
7 */
8 
9 #include "unicode/utypes.h"
10 
11 #if !UCONFIG_NO_FORMATTING
12 
13 #include <stdlib.h>
14 #include "unicode/gender.h"
15 #include "unicode/unum.h"
16 #include "intltest.h"
17 #include "cmemory.h"
18 
19 static const UGender kSingleFemale[] = {UGENDER_FEMALE};
20 static const UGender kSingleMale[] = {UGENDER_MALE};
21 static const UGender kSingleOther[] = {UGENDER_OTHER};
22 
23 static const UGender kAllFemale[] = {UGENDER_FEMALE, UGENDER_FEMALE};
24 static const UGender kAllMale[] = {UGENDER_MALE, UGENDER_MALE};
25 static const UGender kAllOther[] = {UGENDER_OTHER, UGENDER_OTHER};
26 
27 static const UGender kFemaleMale[] = {UGENDER_FEMALE, UGENDER_MALE};
28 static const UGender kFemaleOther[] = {UGENDER_FEMALE, UGENDER_OTHER};
29 static const UGender kMaleOther[] = {UGENDER_MALE, UGENDER_OTHER};
30 
31 
32 class GenderInfoTest : public IntlTest {
33 public:
GenderInfoTest()34     GenderInfoTest() {
35     }
36 
37     void runIndexedTest(int32_t index, UBool exec, const char *&name, char *par=0);
38 private:
39     void TestGetListGender();
40     void TestFallback();
41     void check(UGender expected_neutral, UGender expected_mixed, UGender expected_taints, const UGender* genderList, int32_t listLength);
42     void checkLocale(const Locale& locale, UGender expected, const UGender* genderList, int32_t listLength);
43 };
44 
runIndexedTest(int32_t index,UBool exec,const char * & name,char *)45 void GenderInfoTest::runIndexedTest(int32_t index, UBool exec, const char *&name, char * /* par */) {
46   if (exec) {
47     logln("TestSuite GenderInfoTest: ");
48   }
49   TESTCASE_AUTO_BEGIN;
50   TESTCASE_AUTO(TestGetListGender);
51   TESTCASE_AUTO(TestFallback);
52   TESTCASE_AUTO_END;
53 }
54 
TestGetListGender()55 void GenderInfoTest::TestGetListGender() {
56     check(UGENDER_OTHER, UGENDER_OTHER, UGENDER_OTHER, NULL, 0);
57     check(UGENDER_FEMALE, UGENDER_FEMALE, UGENDER_FEMALE, kSingleFemale, UPRV_LENGTHOF(kSingleFemale));
58     check(UGENDER_MALE, UGENDER_MALE, UGENDER_MALE, kSingleMale, UPRV_LENGTHOF(kSingleMale));
59     check(UGENDER_OTHER, UGENDER_OTHER, UGENDER_OTHER, kSingleOther, UPRV_LENGTHOF(kSingleOther));
60 
61     check(UGENDER_OTHER, UGENDER_FEMALE, UGENDER_FEMALE, kAllFemale, UPRV_LENGTHOF(kAllFemale));
62     check(UGENDER_OTHER, UGENDER_MALE, UGENDER_MALE, kAllMale, UPRV_LENGTHOF(kAllMale));
63     check(UGENDER_OTHER, UGENDER_OTHER, UGENDER_MALE, kAllOther, UPRV_LENGTHOF(kAllOther));
64 
65     check(UGENDER_OTHER, UGENDER_OTHER, UGENDER_MALE, kFemaleMale, UPRV_LENGTHOF(kFemaleMale));
66     check(UGENDER_OTHER, UGENDER_OTHER, UGENDER_MALE, kFemaleOther, UPRV_LENGTHOF(kFemaleOther));
67     check(UGENDER_OTHER, UGENDER_OTHER, UGENDER_MALE, kMaleOther, UPRV_LENGTHOF(kMaleOther));
68 }
69 
TestFallback()70 void GenderInfoTest::TestFallback() {
71   UErrorCode status = U_ZERO_ERROR;
72   const GenderInfo* actual = GenderInfo::getInstance("xx", status);
73   if (U_FAILURE(status)) {
74     errcheckln(status, "Fail to create GenderInfo - %s", u_errorName(status));
75     return;
76   }
77   const GenderInfo* expected = GenderInfo::getNeutralInstance();
78   if (expected != actual) {
79     errln("For Neutral, expected %d got %d", expected, actual);
80   }
81   actual = GenderInfo::getInstance("fr_CA", status);
82   if (U_FAILURE(status)) {
83     errcheckln(status, "Fail to create GenderInfo - %s", u_errorName(status));
84     return;
85   }
86   expected = GenderInfo::getMaleTaintsInstance();
87   if (expected != actual) {
88     errln("For Male Taints, Expected %d got %d", expected, actual);
89   }
90 }
91 
check(UGender expected_neutral,UGender expected_mixed,UGender expected_taints,const UGender * genderList,int32_t listLength)92 void GenderInfoTest::check(
93     UGender expected_neutral, UGender expected_mixed, UGender expected_taints, const UGender* genderList, int32_t listLength) {
94   checkLocale(Locale::getUS(), expected_neutral, genderList, listLength);
95   checkLocale("is", expected_mixed, genderList, listLength);
96   checkLocale(Locale::getFrench(), expected_taints, genderList, listLength);
97 }
98 
checkLocale(const Locale & locale,UGender expected,const UGender * genderList,int32_t listLength)99 void GenderInfoTest::checkLocale(
100     const Locale& locale, UGender expected, const UGender* genderList, int32_t listLength) {
101   UErrorCode status = U_ZERO_ERROR;
102   const GenderInfo* gi = GenderInfo::getInstance(locale, status);
103   if (U_FAILURE(status)) {
104     errcheckln(status, "Fail to create GenderInfo - %s", u_errorName(status));
105     return;
106   }
107   UGender actual = gi->getListGender(genderList, listLength, status);
108   if (U_FAILURE(status)) {
109     errcheckln(status, "Fail to get gender of list - %s", u_errorName(status));
110     return;
111   }
112   if (actual != expected) {
113     errln("For locale: %s expected: %d got %d", locale.getName(), expected, actual);
114   }
115 }
116 
createGenderInfoTest()117 extern IntlTest *createGenderInfoTest() {
118   return new GenderInfoTest();
119 }
120 
121 #endif /* #if !UCONFIG_NO_FORMATTING */
122