summaryrefslogtreecommitdiffstats
path: root/intl/icu_capi/c/examples/locale/test.c
blob: 65f1fbad36bbb8bd2d68014e0ae56031c606be05 (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
// This file is part of ICU4X. For terms of use, please see the file
// called LICENSE at the top level of the ICU4X source tree
// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ).

#include "../../include/ICU4XLocaleCanonicalizer.h"
#include "../../include/ICU4XLocaleExpander.h"
#include "../../include/ICU4XLogger.h"
#include <string.h>
#include <stdio.h>

/**
 * A helper for testing the locale with nice error messages.
 */
bool test_locale(ICU4XLocale* locale, const char* message, const char* expected) {
    char output[40];

    // Test setters
    DiplomatWriteable write = diplomat_simple_writeable(output, 40);
    diplomat_result_void_ICU4XError result = ICU4XLocale_to_string(locale, &write);
    if (!result.is_ok) {
        return 1;
    }
    printf("Output for %s is \"%s\"\n", message, output);
    if (strcmp(output, expected) != 0) {
        printf("Output does not match expected output!\n");
        return false;
    }
    return true;
}

ICU4XLocale* get_locale(const char* localeText) {
    diplomat_result_box_ICU4XLocale_ICU4XError locale_result = ICU4XLocale_create_from_string(localeText, strlen(localeText));
    if (!locale_result.is_ok) {
        printf("Could not create locale from: %s", localeText);
    }
    return locale_result.ok;
}


int main() {
    ICU4XLogger_init_simple_logger();
    char output[40];

    // Test creating a locale.
    DiplomatWriteable write = diplomat_simple_writeable(output, 40);
    diplomat_result_box_ICU4XLocale_ICU4XError locale_result = ICU4XLocale_create_from_string("ar", 2);
    if (!locale_result.is_ok) {
        return 1;
    }
    ICU4XLocale* locale = locale_result.ok;
    diplomat_result_void_ICU4XError result = ICU4XLocale_to_string(locale, &write);
    if (!result.is_ok) {
        return 1;
    }
    printf("Output for a basic locale is %s\n", output);
    const char* expected = u8"ar";
    if (strcmp(output, expected) != 0) {
        printf("Output does not match expected output!\n");
        return 1;
    }
    ICU4XLocale_destroy(locale);

    // Test some accessors.
    write = diplomat_simple_writeable(output, 40);
    locale_result = ICU4XLocale_create_from_string("fr-FR-u-hc-h23", 14);
    if (!locale_result.is_ok) {
        return 1;
    }
    locale = locale_result.ok;
    result = ICU4XLocale_language(locale, &write);
    if (!result.is_ok) {
        return 1;
    }
    printf("Output for the language is %s\n", output);
    expected = u8"fr";
    if (strcmp(output, expected) != 0) {
        printf("Output does not match expected output!\n");
        return 1;
    }

    write = diplomat_simple_writeable(output, 40);
    result = ICU4XLocale_region(locale, &write);
    if (!result.is_ok) {
        return 1;
    }
    printf("Output for the region is %s\n", output);
    expected = u8"FR";
    if (strcmp(output, expected) != 0) {
        printf("Output does not match expected output!\n");
        return 1;
    }

    write = diplomat_simple_writeable(output, 40);
    result = ICU4XLocale_get_unicode_extension(locale, "hc", 2, &write);
    if (!result.is_ok) {
        return 1;
    }
    printf("Output for the extension is %s\n", output);
    expected = u8"h23";
    if (strcmp(output, expected) != 0) {
        printf("Output does not match expected output!\n");
        return 1;
    }

    result = ICU4XLocale_get_unicode_extension(locale, "ca", 2, &write);
    if (!(!result.is_ok && result.err == ICU4XError_LocaleUndefinedSubtagError)) {
        return 1;
    }

    // Test setting the language
    write = diplomat_simple_writeable(output, 40);
    const char* str = "fr-FR-u-hc-h23";
    locale_result = ICU4XLocale_create_from_string(str, strlen(str));
    if (!locale_result.is_ok) {
        printf("Could not create the locale.");
        return 1;
    }
    locale = locale_result.ok;
    str = "zh";
    result = ICU4XLocale_set_language(locale, str, strlen(str));
    if (!result.is_ok) {
        printf("Could not set the language tag.");
        return 1;
    }
    if (!test_locale(locale, "setting the language tag", "zh-FR-u-hc-h23")) {
        return 1;
    }
    ICU4XLocale_destroy(locale);

    // Test setting the region
    write = diplomat_simple_writeable(output, 40);
    str = "es-ES-u-hc-h23";
    locale_result = ICU4XLocale_create_from_string(str, strlen(str));
    if (!locale_result.is_ok) {
        printf("Could not create the locale.");
        return 1;
    }
    locale = locale_result.ok;
    if (!test_locale(locale, "The region starts as es-ES", "es-ES-u-hc-h23")) {
        return 1;
    }
    str = "MX";
    result = ICU4XLocale_set_region(locale, str, strlen(str));
    if (!result.is_ok) {
        printf("Could not set the region.");
        return 1;
    }
    if (!test_locale(locale, "setting the region", "es-MX-u-hc-h23")) {
        return 1;
    }
    ICU4XLocale_destroy(locale);

     // Test setting the script
    write = diplomat_simple_writeable(output, 40);
    str = "en-US";
    locale_result = ICU4XLocale_create_from_string(str, strlen(str));
    if (!locale_result.is_ok) {
        printf("Could not create the locale.");
        return 1;
    }
    locale = locale_result.ok;
    if (!test_locale(locale, "initial script-less locale", "en-US")) {
        return 1;
    }
    str = "Latn";
    result = ICU4XLocale_set_script(locale, str, strlen(str));
    if (!result.is_ok) {
        printf("Could not set the script.");
        return 1;
    }
    if (!test_locale(locale, "setting the script", "en-Latn-US")) {
        return 1;
    }
    result = ICU4XLocale_set_script(locale, "", 0);
    if (!result.is_ok) {
        printf("Could not set the script.");
        return 1;
    }
    if (!test_locale(locale, "removing the script", "en-US")) {
        return 1;
    }

    ICU4XLocale_destroy(locale);

    // Create a LocaleCanonicalizer and LocaleExpander.
    ICU4XDataProvider* provider = ICU4XDataProvider_create_compiled();
    diplomat_result_box_ICU4XLocaleCanonicalizer_ICU4XError result2 = ICU4XLocaleCanonicalizer_create(provider);
    if (!result2.is_ok) {
        printf("Could not construct Locale Canonicalizer");
        return 1;
    }
    ICU4XLocaleCanonicalizer* lc = result2.ok;
    diplomat_result_box_ICU4XLocaleExpander_ICU4XError result3 = ICU4XLocaleExpander_create(provider);
    if (!result3.is_ok) {
        printf("Could not construct Locale Canonicalizer");
        return 1;
    }
    ICU4XLocaleExpander* le = result3.ok;

    // Test maximize.
    write = diplomat_simple_writeable(output, 40);
    locale_result = ICU4XLocale_create_from_string("und", 3);
    if (!locale_result.is_ok) {
        printf("Could not create the locale.");
        return 1;
    }
    locale = locale_result.ok;
    ICU4XLocaleExpander_maximize(le, locale);
    result = ICU4XLocale_to_string(locale, &write);
    if (!result.is_ok) {
        return 1;
    }
    printf("Output for maximizing is %s\n", output);
    expected = u8"en-Latn-US";
    if (strcmp(output, expected) != 0) {
        printf("Output does not match expected output!\n");
        return 1;
    }
    ICU4XLocale_destroy(locale);

    // Test minimize.
    write = diplomat_simple_writeable(output, 40);
    locale_result = ICU4XLocale_create_from_string("zh-Hant", 7);
    if (!locale_result.is_ok) {
        printf("Could not create the locale.");
        return 1;
    }
    locale = locale_result.ok;
    ICU4XLocaleExpander_minimize(le, locale);
    result = ICU4XLocale_to_string(locale, &write);
    if (!result.is_ok) {
        return 1;
    }
    printf("Output for minimizing is %s\n", output);
    expected = u8"zh-TW";
    if (strcmp(output, expected) != 0) {
        printf("Output does not match expected output!\n");
        return 1;
    }
    ICU4XLocale_destroy(locale);

    // Test canonicalize.
    write = diplomat_simple_writeable(output, 40);
    locale_result = ICU4XLocale_create_from_string("no-nynorsk", 10);
    if (!locale_result.is_ok) {
        printf("Could not create the locale.");
        return 1;
    }
    locale = locale_result.ok;
    ICU4XLocaleCanonicalizer_canonicalize(lc, locale);
    result = ICU4XLocale_to_string(locale, &write);
    if (!result.is_ok) {
        return 1;
    }
    printf("Output for canonicalizing is %s\n", output);
    expected = u8"nn";
    if (strcmp(output, expected) != 0) {
        printf("Output does not match expected output!\n");
        return 1;
    }
    ICU4XLocale_destroy(locale);

    ICU4XLocaleCanonicalizer_destroy(lc);
    ICU4XLocaleExpander_destroy(le);

    return 0;
}