test-framework-sh: Don't leave temporary directories on NetBSD.
[gnulib.git] / tests / test-c32swidth.c
blob33bdd655a3ef41668e7397f4451d9a40286da8d2
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. */
19 #include <config.h>
21 #include <uchar.h>
23 #include "signature.h"
24 SIGNATURE_CHECK (c32swidth, int, (const char32_t *, size_t));
26 #include <locale.h>
27 #include <string.h>
29 #include "localcharset.h"
30 #include "macros.h"
32 int
33 main ()
35 int w;
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");
48 return 77;
52 char32_t s[] = { 'f', 'p', 0, 'x' };
53 w = c32swidth (s, 0);
54 ASSERT (w == 0);
55 w = c32swidth (s, 1);
56 ASSERT (w == 1);
57 w = c32swidth (s, 2);
58 ASSERT (w == 2);
59 w = c32swidth (s, 3);
60 ASSERT (w == 2);
61 w = c32swidth (s, 4);
62 ASSERT (w == 2);
63 w = c32swidth (s, (size_t)(-1));
64 ASSERT (w == 2);
68 char32_t s[] = { 'f', 'p', '\n', 'x' };
69 w = c32swidth (s, 0);
70 ASSERT (w == 0);
71 w = c32swidth (s, 1);
72 ASSERT (w == 1);
73 w = c32swidth (s, 2);
74 ASSERT (w == 2);
75 w = c32swidth (s, 3);
76 ASSERT (w == -1);
77 w = c32swidth (s, 4);
78 ASSERT (w == -1);
81 /* Test width of some non-spacing characters. */
83 char32_t s[] = { 'a', 0x0301 };
84 w = c32swidth (s, 2);
85 ASSERT (w == 1);
88 /* Test width of some zero width characters. */
90 char32_t s[] = { 'i', 0x200B, 'j' };
91 w = c32swidth (s, 3);
92 ASSERT (w == 2);
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>
100 says
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);
112 ASSERT (w == 1);
115 /* Test width of some CJK characters. */
117 char32_t s[] = { 0x4E2D, 0x6587 };
118 w = c32swidth (s, 2);
119 ASSERT (w == 4);
122 char32_t s[] = { 0x20369, 0x2F876 };
123 w = c32swidth (s, 2);
124 ASSERT (w == 4);
127 return test_exit_status;