Branch
Hash :
7b089321
Author :
Date :
2025-01-01T09:24:36
maint: run 'make update-copyright'
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
/* Test support for locale names in BCP 47 syntax.
Copyright (C) 2024-2025 Free Software Foundation, Inc.
This file is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This file is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>. */
/* Written by Bruno Haible <bruno@clisp.org>, 2024. */
#include <config.h>
#include "bcp47.h"
#include <string.h>
#include "macros.h"
static void
test_correspondence (const char *xpg, const char *bcp47)
{
/* Test xpg_to_bcp47. */
{
char buf[BCP47_MAX];
memset (buf, 0x77, BCP47_MAX);
xpg_to_bcp47 (buf, xpg);
ASSERT (strcmp (buf, bcp47) == 0);
}
/* Test bcp47_to_xpg. */
{
char buf[BCP47_MAX];
memset (buf, 0x77, BCP47_MAX);
bcp47_to_xpg (buf, bcp47, NULL);
ASSERT (strcmp (buf, xpg) == 0);
}
}
int
main ()
{
/* Languages with a single script. */
test_correspondence ("de", "de");
test_correspondence ("de_DE", "de-DE");
test_correspondence ("de_AT", "de-AT");
/* Languages with a script that depends on the territory. */
test_correspondence ("az_AZ", "az-Latn-AZ");
test_correspondence ("az_AZ@cyrillic", "az-Cyrl-AZ");
test_correspondence ("az_IR", "az-Arab-IR");
test_correspondence ("ku_IQ", "ku-Arab-IQ");
test_correspondence ("ku_IR", "ku-Arab-IR");
test_correspondence ("ku_SY", "ku-Latn-SY");
test_correspondence ("ku_TR", "ku-Latn-TR");
test_correspondence ("pa_PK", "pa-Arab-PK");
test_correspondence ("pa_IN", "pa-Guru-IN");
test_correspondence ("zh_CN", "zh-Hans-CN");
test_correspondence ("zh_HK", "zh-Hant-HK");
test_correspondence ("zh_MO", "zh-Hant-MO");
test_correspondence ("zh_SG", "zh-Hans-SG");
test_correspondence ("zh_TW", "zh-Hant-TW");
/* Languages with a main script and one or more alternate scripts. */
test_correspondence ("be_BY", "be-Cyrl-BY");
test_correspondence ("be_BY@latin", "be-Latn-BY");
test_correspondence ("ber@arabic", "ber-Arab");
test_correspondence ("ber", "ber-Latn");
test_correspondence ("ber_DZ", "ber-Latn-DZ");
test_correspondence ("ber_MA", "ber-Latn-MA");
test_correspondence ("bs_BA", "bs-Latn-BA");
test_correspondence ("bs_BA@cyrillic", "bs-Cyrl-BA");
test_correspondence ("ha_NG", "ha-Latn-NG");
test_correspondence ("ha_NG@arabic", "ha-Arab-NG");
test_correspondence ("iu_CA", "iu-Cans-CA");
test_correspondence ("iu_CA@latin", "iu-Latn-CA");
test_correspondence ("kk_KZ", "kk-Cyrl-KZ");
test_correspondence ("kk_KZ@latin", "kk-Latn-KZ");
test_correspondence ("ks_IN", "ks-Arab-IN");
test_correspondence ("ks_IN@devanagari", "ks-Deva-IN");
test_correspondence ("mn_MN", "mn-Cyrl-MN");
test_correspondence ("mn_MN@mongolian", "mn-Mong-MN");
test_correspondence ("nan_TW", "nan-Hant-TW");
test_correspondence ("nan_TW@latin", "nan-Latn-TW");
test_correspondence ("sd_PK", "sd-Arab-PK");
test_correspondence ("sd_IN", "sd-Arab-IN");
test_correspondence ("sd_IN@devanagari", "sd-Deva-IN");
test_correspondence ("sr_BA@latin", "sr-Latn-BA");
test_correspondence ("sr_BA", "sr-Cyrl-BA");
test_correspondence ("sr_RS", "sr-Cyrl-RS");
test_correspondence ("sr_RS@latin", "sr-Latn-RS");
test_correspondence ("uz_UZ", "uz-Latn-UZ");
test_correspondence ("uz_UZ@cyrillic", "uz-Cyrl-UZ");
test_correspondence ("yi_US", "yi-Hebr-US");
test_correspondence ("yi_US@latin", "yi-Latn-US");
/* For Quechua, Microsoft uses the ISO 639-3 code "quz" instead of the
ISO 639-1 code "qu". */
{
char buf[BCP47_MAX];
memset (buf, 0x77, BCP47_MAX);
bcp47_to_xpg (buf, "quz-PE", NULL);
ASSERT (strcmp (buf, "qu_PE") == 0);
}
/* For Tamazight, Microsoft uses the ISO 639-3 code "tzm" instead of the
ISO 639-2 code "ber". */
{
char buf[BCP47_MAX];
memset (buf, 0x77, BCP47_MAX);
bcp47_to_xpg (buf, "tzm-MA", NULL);
ASSERT (strcmp (buf, "ber_MA") == 0);
}
/* Languages with a regional variant. */
test_correspondence ("ca", "ca");
test_correspondence ("ca@valencia", "ca-valencia");
/* Test xpg_to_bcp47 with an encoding. */
{
char buf[BCP47_MAX];
memset (buf, 0x77, BCP47_MAX);
xpg_to_bcp47 (buf, "en_US.UTF-8");
ASSERT (strcmp (buf, "en-US") == 0);
}
{
char buf[BCP47_MAX];
memset (buf, 0x77, BCP47_MAX);
xpg_to_bcp47 (buf, "az_AZ.UTF-8@cyrillic");
ASSERT (strcmp (buf, "az-Cyrl-AZ") == 0);
}
/* Test bcp47_to_xpg with an encoding. */
{
char buf[BCP47_MAX];
memset (buf, 0x77, BCP47_MAX);
bcp47_to_xpg (buf, "en-US", "UTF-8");
ASSERT (strcmp (buf, "en_US.UTF-8") == 0);
}
{
char buf[BCP47_MAX];
memset (buf, 0x77, BCP47_MAX);
bcp47_to_xpg (buf, "az-Cyrl-AZ", "UTF-8");
ASSERT (strcmp (buf, "az_AZ.UTF-8@cyrillic") == 0);
}
/* Test case mapping done by bcp47_to_xpg. */
{
char buf[BCP47_MAX];
memset (buf, 0x77, BCP47_MAX);
bcp47_to_xpg (buf, "EN-US", "UTF-8");
ASSERT (strcmp (buf, "en_US.UTF-8") == 0);
}
{
char buf[BCP47_MAX];
memset (buf, 0x77, BCP47_MAX);
bcp47_to_xpg (buf, "en-us", "UTF-8");
ASSERT (strcmp (buf, "en_US.UTF-8") == 0);
}
{
char buf[BCP47_MAX];
memset (buf, 0x77, BCP47_MAX);
bcp47_to_xpg (buf, "Zh-hANs-cN", "UTF-8");
ASSERT (strcmp (buf, "zh_CN.UTF-8") == 0);
}
return test_exit_status;
}