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