1 /* Test of c32swidth() function.
2 Copyright (C) 2023-2024 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <https://www.gnu.org/licenses/>. */
17 /* Written by Bruno Haible <bruno@clisp.org>, 2023. */
23 #include "signature.h"
24 SIGNATURE_CHECK (c32swidth
, int, (const char32_t
*, size_t));
29 #include "localcharset.h"
37 /* Switch to an UTF-8 locale. */
38 if (!((setlocale (LC_ALL
, "fr_FR.UTF-8") != NULL
39 || setlocale (LC_ALL
, "de_DE.UTF-8") != NULL
40 || setlocale (LC_ALL
, "es_ES.UTF-8") != NULL
41 || setlocale (LC_ALL
, "en_US.UTF-8") != NULL
)
42 /* Check whether it's really an UTF-8 locale.
43 On native Windows, these setlocale calls succeed but the encoding
44 of this locale is CP1252, not UTF-8. */
45 && strcmp (locale_charset (), "UTF-8") == 0))
47 fprintf (stderr
, "Skipping test: No common Unicode locale is installed\n");
52 char32_t s
[] = { 'f', 'p', 0, 'x' };
63 w
= c32swidth (s
, (size_t)(-1));
68 char32_t s
[] = { 'f', 'p', '\n', 'x' };
81 /* Test width of some non-spacing characters. */
83 char32_t s
[] = { 'a', 0x0301 };
88 /* Test width of some zero width characters. */
90 char32_t s
[] = { 'i', 0x200B, 'j' };
95 /* Test width of some math symbols.
96 U+2202 is marked as having ambiguous width (A) in EastAsianWidth.txt
97 (see <https://www.unicode.org/Public/12.0.0/ucd/EastAsianWidth.txt>).
98 The Unicode Standard Annex 11
99 <https://www.unicode.org/reports/tr11/tr11-36.html>
101 "Ambiguous characters behave like wide or narrow characters
102 depending on the context (language tag, script identification,
103 associated font, source of data, or explicit markup; all can
104 provide the context). If the context cannot be established
105 reliably, they should be treated as narrow characters by default."
106 For c32width(), the only available context information is the locale.
107 The chosen locale above is a Western locale, not an East Asian locale,
108 therefore U+2202 should be treated like a narrow character. */
110 char32_t s
[] = { 0x2202 };
111 w
= c32swidth (s
, 1);
115 /* Test width of some CJK characters. */
117 char32_t s
[] = { 0x4E2D, 0x6587 };
118 w
= c32swidth (s
, 2);
122 char32_t s
[] = { 0x20369, 0x2F876 };
123 w
= c32swidth (s
, 2);
127 return test_exit_status
;