summaryrefslogtreecommitdiff
path: root/icu4c/source/test/cintltst/ulocaletst.c
blob: 6d3fae84c72dc73fe023568522253177b3f88e3f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
// © 2023 and later: Unicode, Inc. and others.
// License & terms of use: http://www.unicode.org/copyright.html

#include "unicode/ctest.h"
#include "unicode/uloc.h"
#include "unicode/ulocale.h"
#include "cintltst.h"
#include "cmemory.h"
#include "cstring.h"

#define WHERE __FILE__ ":" XLINE(__LINE__) " "
#define XLINE(s) LINE(s)
#define LINE(s) #s

#define TESTCASE(name) addTest(root, &name, "tsutil/ulocaletst/" #name)

enum {
    LANGUAGE = 0,
    SCRIPT,
    REGION,
    VAR,
    NAME,
    BASENAME,
};
static const char* const rawData[][6] = {
    { "en", "", "US", "", "en_US", "en_US"},
    { "fr", "", "FR", "", "fr_FR", "fr_FR"},
    { "ca", "", "ES", "", "ca_ES", "ca_ES"},
    { "el", "", "GR", "", "el_GR", "el_GR"},
    { "no", "", "NO", "NY", "no_NO_NY", "no_NO_NY"},
    { "it", "", "", "", "it", "it"},
    { "xx", "", "YY", "", "xx_YY", "xx_YY"},
    { "zh", "Hans", "CN", "", "zh_Hans_CN", "zh_Hans_CN"},
    { "zh", "Hans", "CN", "", "zh_Hans_CN", "zh_Hans_CN"},
    { "x-klingon", "Latn", "ZX", "", "x-klingon_Latn_ZX.utf32be@special", "x-klingon_Latn_ZX.utf32be@special"},
};

static void TestBasicGetters() {
    for (int32_t i = 0; i < UPRV_LENGTHOF(rawData); i++)  {
        UErrorCode status = U_ZERO_ERROR;
        ULocale* l = ulocale_openForLocaleID(rawData[i][NAME], -1, &status);
        if (assertSuccess(WHERE "ulocale_openForLocaleID()", &status)) {
            assertEquals(WHERE "ulocale_getLanguage()", rawData[i][LANGUAGE], ulocale_getLanguage(l));
            assertEquals(WHERE "ulocale_getScript()", rawData[i][SCRIPT], ulocale_getScript(l));
            assertEquals(WHERE "ulocale_getRegion()", rawData[i][REGION], ulocale_getRegion(l));
            assertEquals(WHERE "ulocale_getVariant()", rawData[i][VAR], ulocale_getVariant(l));
            assertEquals(WHERE "ulocale_getLocaleID()", rawData[i][NAME], ulocale_getLocaleID(l));
            assertEquals(WHERE "ulocale_getBaseName()", rawData[i][BASENAME], ulocale_getBaseName(l));
            ulocale_close(l);
        }
    }
}

static void VerifyMatch(const char* localeID, const char* tag) {
    UErrorCode status = U_ZERO_ERROR;
    ULocale* fromID = ulocale_openForLocaleID(localeID, -1, &status);
    if (assertSuccess(WHERE "ulocale_openForLocaleID()", &status)) {
        ULocale* fromTag = ulocale_openForLanguageTag(tag, -1, &status);
        if (assertSuccess(WHERE "ulocale_openForLanguageTag()", &status)) {
            assertEquals(tag, ulocale_getLocaleID(fromID), ulocale_getLocaleID(fromTag));
            ulocale_close(fromTag);
        }
        ulocale_close(fromID);
    }
}

static void TestForLanguageTag() {
    VerifyMatch("en_US", "en-US");
    VerifyMatch("en_GB_OXENDICT", "en-GB-oed");
    VerifyMatch("af@calendar=coptic;t=ar-i0-handwrit;x=foo", "af-t-ar-i0-handwrit-u-ca-coptic-x-foo");
    VerifyMatch("en_GB", "en-GB");
    VerifyMatch("en_GB@1=abc-efg;a=xyz", "en-GB-1-abc-efg-a-xyz"); // ext
    VerifyMatch("sl__1994_BISKE_ROZAJ", "sl-rozaj-biske-1994"); // var

    UErrorCode status = U_ZERO_ERROR;
    ULocale* result_ill = ulocale_openForLanguageTag("!", -1, &status);
    assertIntEquals("!", U_ILLEGAL_ARGUMENT_ERROR, status);
    assertPtrEquals("!", NULL, result_ill);
    ulocale_close(result_ill);

    VerifyMatch("", NULL);

    // ICU-21433
    VerifyMatch("__1994_BISKE_ROZAJ", "und-1994-biske-rozaj");
    VerifyMatch("de__1994_BISKE_ROZAJ", "de-1994-biske-rozaj");
    VerifyMatch("@x=private", "und-x-private");
    VerifyMatch("de__1994_BISKE_ROZAJ@x=private", "de-1994-biske-rozaj-x-private");
    VerifyMatch("__1994_BISKE_ROZAJ@x=private", "und-1994-biske-rozaj-x-private");
}

static void TestGetKeywords() {
    UErrorCode status = U_ZERO_ERROR;
    ULocale* l = ulocale_openForLocaleID("de@calendar=buddhist;collation=phonebook", -1, &status);
    if (assertSuccess(WHERE "ulocale_openForLocaleID()", &status)) {
        UEnumeration* en = ulocale_getKeywords(l, &status);
        if (assertSuccess(WHERE "ulocale_getKeywords()", &status)) {
            assertIntEquals("uenum_count()", 2, uenum_count(en, &status));
            bool hasCalendar = false;
            bool hasCollation = false;
            const char* key = NULL;
            while ((key = uenum_next(en, NULL, &status)) != NULL) {
                if (uprv_strcmp(key, "calendar") == 0) {
                    hasCalendar = true;
                } else if (uprv_strcmp(key, "collation") == 0) {
                    hasCollation = true;
                }
            }
            assertTrue(
                WHERE "ulocale_getKeywords() should return UEnumeration that has \"calendar\"",
                hasCalendar);
            assertTrue(
                WHERE "ulocale_getKeywords() should return UEnumeration that has \"collation\"",
                hasCollation);
            uenum_close(en);
        }
        ulocale_close(l);
    }
}

static void TestGetKeywordsEmpty() {
    UErrorCode status = U_ZERO_ERROR;
    ULocale* l = ulocale_openForLocaleID("de", -1, &status);
    if (assertSuccess(WHERE "ulocale_openForLocaleID()", &status)) {
        UEnumeration* en = ulocale_getKeywords(l, &status);
        if (assertSuccess(WHERE "ulocale_getKeywords()", &status)) {
            assertPtrEquals(WHERE "ulocale_getKeyword()", NULL, en);
            uenum_close(en);
        }
        ulocale_close(l);
    }
}

static void TestGetKeywordsWithPrivateUse() {
    UErrorCode status = U_ZERO_ERROR;
    ULocale* l = ulocale_openForLanguageTag("en-US-u-ca-gregory-x-foo", -1, &status);
    if (assertSuccess(WHERE "ulocale_openForLanguageTag()", &status)) {
        UEnumeration* en = ulocale_getKeywords(l, &status);
        if (assertSuccess(WHERE "ulocale_getKeywords()", &status)) {
            assertIntEquals("uenum_count()", 2, uenum_count(en, &status));
            bool hasCalendar = false;
            bool hasX = false;
            const char* key = NULL;
            while ((key = uenum_next(en, NULL, &status)) != NULL) {
                if (uprv_strcmp(key, "calendar") == 0) {
                    hasCalendar = true;
                } else if (uprv_strcmp(key, "x") == 0) {
                    hasX = true;
                }
            }
            assertTrue(WHERE "ulocale_getKeywords() should return UEnumeration that has \"calendar\"",
                       hasCalendar);
            assertTrue(WHERE "ulocale_getKeywords() should return UEnumeration that has \"x\"",
                       hasX);
            uenum_close(en);
        }
        ulocale_close(l);
    }
}

static void TestGetUnicodeKeywords() {
    UErrorCode status = U_ZERO_ERROR;
    ULocale* l = ulocale_openForLocaleID("de@calendar=buddhist;collation=phonebook", -1, &status);
    if (assertSuccess(WHERE "ulocale_openForLocaleID()", &status)) {
        UEnumeration* en = ulocale_getUnicodeKeywords(l, &status);
        if (assertSuccess(WHERE "ulocale_getUnicodeKeywords()", &status)) {
            assertIntEquals("uenum_count()", 2, uenum_count(en, &status));
            bool hasCa = false;
            bool hasCo = false;
            const char* key = NULL;
            while ((key = uenum_next(en, NULL, &status)) != NULL) {
                if (uprv_strcmp(key, "ca") == 0) {
                    hasCa = true;
                } else if (uprv_strcmp(key, "co") == 0) {
                    hasCo = true;
                }
            }
            assertTrue(
                WHERE "ulocale_getUnicodeKeywords() should return UEnumeration that has \"ca\"",
                hasCa);
            assertTrue(
                WHERE "ulocale_getUnicodeKeywords() should return UEnumeration that has \"co\"",
                hasCo);
            uenum_close(en);
        }
        ulocale_close(l);
    }
}

static void TestGetUnicodeKeywordsEmpty() {
    UErrorCode status = U_ZERO_ERROR;
    ULocale* l = ulocale_openForLocaleID("de", -1, &status);
    if (assertSuccess(WHERE "ulocale_openForLocaleID()", &status)) {
        UEnumeration* en = ulocale_getUnicodeKeywords(l, &status);
        if (assertSuccess(WHERE "ulocale_getUnicodeKeywords()", &status)) {
            assertPtrEquals("ulocale_getUnicodeKeyword()", NULL, en);
            uenum_close(en);
        }
        ulocale_close(l);
    }
}

static void TestGetUnicodeKeywordsWithPrivateUse() {
    UErrorCode status = U_ZERO_ERROR;
    ULocale* l = ulocale_openForLanguageTag("en-US-u-ca-gregory-x-foo", -1, &status);
    if (assertSuccess(WHERE "ulocale_openForLanguageTag()", &status)) {
        UEnumeration* en = ulocale_getUnicodeKeywords(l, &status);
        if (assertSuccess(WHERE "ulocale_getUnicodeKeywords()", &status)) {
            int32_t count = uenum_count(en, &status);
            if (count != 1) {
                log_knownIssue("ICU-22457", "uenum_count() should be 1 but get %d\n", count);
            }
            const char* key = uenum_next(en, NULL, &status);
            if (assertSuccess(WHERE "uenum_next()", &status)) {
                assertEquals(WHERE "ulocale_getUnicodeKeywords() should return UEnumeration that has \"ca\"",
                             "ca", key);
            }
            uenum_close(en);
        }
        ulocale_close(l);
    }
}

static void TestGetKeywordValue() {
    UErrorCode status = U_ZERO_ERROR;
    ULocale* l = ulocale_openForLanguageTag("fa-u-nu-thai", -1, &status);
    if (assertSuccess(WHERE "ulocale_openForLanguageTag()", &status)) {
        char buffer[ULOC_FULLNAME_CAPACITY];
        ulocale_getKeywordValue(l, "numbers", -1, buffer, ULOC_FULLNAME_CAPACITY, &status);
        if (assertSuccess(WHERE "ulocale_getKeywordValue()", &status)) {
            assertEquals(WHERE "ulocale_getKeywordValue(\"numbers\")", "thai", buffer);
        }

        ulocale_getKeywordValue(l, "calendar", -1, buffer, ULOC_FULLNAME_CAPACITY, &status);
        if (assertSuccess(WHERE "ulocale_getKeywordValue()", &status)) {
            assertEquals(WHERE "ulocale_getKeywordValue(\"calendar\")", "", buffer);
        }
        ulocale_getKeywordValue(l, "collation", -1, buffer, ULOC_FULLNAME_CAPACITY, &status);
        if (assertSuccess(WHERE "ulocale_getKeywordValue()", &status)) {
            assertEquals(WHERE "ulocale_getKeywordValue(\"collation\")", "", buffer);
        }
        ulocale_close(l);
    }
}

static void TestGetUnicodeKeywordValue() {
    UErrorCode status = U_ZERO_ERROR;
    ULocale* l = ulocale_openForLanguageTag("fa-u-nu-thai", -1, &status);
    if (assertSuccess(WHERE "ulocale_openForLanguageTag()", &status)) {
        char buffer[ULOC_FULLNAME_CAPACITY];
        ulocale_getUnicodeKeywordValue(l, "nu", -1, buffer, ULOC_FULLNAME_CAPACITY, &status);
        if (assertSuccess(WHERE "ulocale_getUnicodeKeywordValue()", &status)) {
            assertEquals(WHERE "ulocale_getUnicodeKeywordValue(\"nu\")", "thai", buffer);
        }

        ulocale_getUnicodeKeywordValue(l, "ca", -1, buffer, ULOC_FULLNAME_CAPACITY, &status);
        if (U_FAILURE(status) || buffer[0] != '\0') {
            log_knownIssue(
                "ICU-22459",
                WHERE "ulocale_getUnicodeKeywordValue(\"fa-u-nu-thai\", \"ca\") should not return error and should return empty string.");
        }
        ulocale_getUnicodeKeywordValue(l, "co", -1, buffer, ULOC_FULLNAME_CAPACITY, &status);
        if (U_FAILURE(status) || buffer[0] != '\0') {
            log_knownIssue(
                "ICU-22459",
                WHERE "ulocale_getUnicodeKeywordValue(\"fa-u-nu-thai\", \"co\") should not return error and should return empty string.");
        }
        ulocale_close(l);
    }
}

void addULocaleTest(TestNode** root);
void addULocaleTest(TestNode** root)
{
    TESTCASE(TestBasicGetters);
    TESTCASE(TestForLanguageTag);
    TESTCASE(TestGetKeywords);
    TESTCASE(TestGetKeywordsEmpty);
    TESTCASE(TestGetKeywordsWithPrivateUse);
    TESTCASE(TestGetUnicodeKeywords);
    TESTCASE(TestGetUnicodeKeywordsEmpty);
    TESTCASE(TestGetUnicodeKeywordsWithPrivateUse);
    TESTCASE(TestGetKeywordValue);
    TESTCASE(TestGetUnicodeKeywordValue);
}