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