]> git.saurik.com Git - apple/icu.git/blame - icuSources/test/intltest/genderinfotest.cpp
ICU-62107.0.1.tar.gz
[apple/icu.git] / icuSources / test / intltest / genderinfotest.cpp
CommitLineData
f3c0d7a5
A
1// © 2016 and later: Unicode, Inc. and others.
2// License & terms of use: http://www.unicode.org/copyright.html
51004dcb
A
3/********************************************************************
4 * COPYRIGHT:
b331163b 5 * Copyright (c) 1997-2014, International Business Machines Corporation and
51004dcb
A
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"
b331163b 19#include "cmemory.h"
51004dcb
A
20
21static const UGender kSingleFemale[] = {UGENDER_FEMALE};
22static const UGender kSingleMale[] = {UGENDER_MALE};
23static const UGender kSingleOther[] = {UGENDER_OTHER};
24
25static const UGender kAllFemale[] = {UGENDER_FEMALE, UGENDER_FEMALE};
26static const UGender kAllMale[] = {UGENDER_MALE, UGENDER_MALE};
27static const UGender kAllOther[] = {UGENDER_OTHER, UGENDER_OTHER};
28
29static const UGender kFemaleMale[] = {UGENDER_FEMALE, UGENDER_MALE};
30static const UGender kFemaleOther[] = {UGENDER_FEMALE, UGENDER_OTHER};
31static const UGender kMaleOther[] = {UGENDER_MALE, UGENDER_OTHER};
32
33
34class GenderInfoTest : public IntlTest {
35public:
36 GenderInfoTest() {
37 }
38
39 void runIndexedTest(int32_t index, UBool exec, const char *&name, char *par=0);
40private:
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
47void 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
57void GenderInfoTest::TestGetListGender() {
58 check(UGENDER_OTHER, UGENDER_OTHER, UGENDER_OTHER, NULL, 0);
b331163b
A
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));
51004dcb 62
b331163b
A
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));
51004dcb 66
b331163b
A
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));
51004dcb
A
70}
71
72void 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
94void 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
101void 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
119extern IntlTest *createGenderInfoTest() {
120 return new GenderInfoTest();
121}
122
123#endif /* #if !UCONFIG_NO_FORMATTING */