Debian Codegraph @

gnulib-tests/test-c32isblank.c

raw
1 /* Test of c32isblank() function.
2 Copyright (C) 2020-2025 Free Software Foundation, Inc.
3
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.
8
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.
13
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/>. */
16
17 #include <config.h>
18
19 #include <uchar.h>
20
21 #include "signature.h"
22 SIGNATURE_CHECK (c32isblank, int, (wint_t));
23
24 #include <locale.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <wchar.h>
28
29 #include "macros.h"
30
31 /* Returns the value of c32isblank for the multibyte character s[0..n-1]. */
32 static int
33 for_character (const char *s, size_t n)
34 {
35 mbstate_t state;
36 char32_t wc;
37 size_t ret;
38
39 memset (&state, '\0', sizeof (mbstate_t));
40 wc = (char32_t) 0xBADFACE;
41 ret = mbrtoc32 (&wc, s, n, &state);
42 ASSERT (ret == n);
43
44 return c32isblank (wc);
45 }
46
47 int
48 main (int argc, char *argv[])
49 {
50 int is;
51 char buf[4];
52
53 /* configure should already have checked that the locale is supported. */
54 if (setlocale (LC_ALL, "") == NULL)
55 return 1;
56
57 /* Test WEOF. */
58 is = c32isblank (WEOF);
59 ASSERT (is == 0);
60
61 /* Test single-byte characters.
62 POSIX specifies in
63 <https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap07.html>
64 that
65 - in all locales, the blank characters include the <space> and <tab>
66 characters,
67 - in the "POSIX" locale (which is usually the same as the "C" locale),
68 the blank characters include only the ASCII <space> and <tab>
69 characters. */
70 {
71 int c;
72
73 for (c = 0; c < 0x100; c++)
74 switch (c)
75 {
76 case '\t':
77 #if !(defined __FreeBSD__ || defined __NetBSD__)
78 case '\v':
79 #endif
80 #if !defined __NetBSD__
81 case '\f':
82 #endif
83 case ' ': case '!': case '"': case '#': case '%':
84 case '&': case '\'': case '(': case ')': case '*':
85 case '+': case ',': case '-': case '.': case '/':
86 case '0': case '1': case '2': case '3': case '4':
87 case '5': case '6': case '7': case '8': case '9':
88 case ':': case ';': case '<': case '=': case '>':
89 case '?':
90 case 'A': case 'B': case 'C': case 'D': case 'E':
91 case 'F': case 'G': case 'H': case 'I': case 'J':
92 case 'K': case 'L': case 'M': case 'N': case 'O':
93 case 'P': case 'Q': case 'R': case 'S': case 'T':
94 case 'U': case 'V': case 'W': case 'X': case 'Y':
95 case 'Z':
96 case '[': case '\\': case ']': case '^': case '_':
97 case 'a': case 'b': case 'c': case 'd': case 'e':
98 case 'f': case 'g': case 'h': case 'i': case 'j':
99 case 'k': case 'l': case 'm': case 'n': case 'o':
100 case 'p': case 'q': case 'r': case 's': case 't':
101 case 'u': case 'v': case 'w': case 'x': case 'y':
102 case 'z': case '{': case '|': case '}': case '~':
103 /* c is in the ISO C "basic character set". */
104 buf[0] = (unsigned char) c;
105 is = for_character (buf, 1);
106 if (c == '\t' || c == ' ')
107 ASSERT (is != 0);
108 else
109 ASSERT (is == 0);
110 break;
111 }
112 }
113
114 if (argc > 1)
115 switch (argv[1][0])
116 {
117 case '0':
118 /* C locale; tested above. */
119 return test_exit_status;
120
121 case '1':
122 /* Locale encoding is ISO-8859-1 or ISO-8859-15. */
123 {
124 #if defined __GLIBC__
125 /* U+00A0 NO-BREAK SPACE */
126 is = for_character ("\240", 1);
127 ASSERT (is == 0);
128 #endif
129 /* U+00B7 MIDDLE DOT */
130 is = for_character ("\267", 1);
131 ASSERT (is == 0);
132 }
133 return test_exit_status;
134
135 case '2':
136 /* Locale encoding is EUC-JP. */
137 {
138 /* U+3002 IDEOGRAPHIC FULL STOP */
139 is = for_character ("\241\243", 2);
140 ASSERT (is == 0);
141 }
142 return test_exit_status;
143
144 case '3':
145 /* Locale encoding is UTF-8. */
146 {
147 #if defined __GLIBC__
148 /* U+00A0 NO-BREAK SPACE */
149 is = for_character ("\302\240", 2);
150 ASSERT (is == 0);
151 #endif
152 /* U+00B7 MIDDLE DOT */
153 is = for_character ("\302\267", 2);
154 ASSERT (is == 0);
155 #if defined __GLIBC__
156 /* U+202F NARROW NO-BREAK SPACE */
157 is = for_character ("\342\200\257", 3);
158 ASSERT (is == 0);
159 #endif
160 /* U+3002 IDEOGRAPHIC FULL STOP */
161 is = for_character ("\343\200\202", 3);
162 ASSERT (is == 0);
163 /* U+1D13D MUSICAL SYMBOL QUARTER REST */
164 is = for_character ("\360\235\204\275", 4);
165 ASSERT (is == 0);
166 /* U+E0020 TAG SPACE */
167 is = for_character ("\363\240\200\240", 4);
168 ASSERT (is == 0);
169 }
170 return test_exit_status;
171
172 case '4':
173 /* Locale encoding is GB18030. */
174 #if (defined __GLIBC__ && __GLIBC__ == 2 && __GLIBC_MINOR__ >= 13 && __GLIBC_MINOR__ <= 15) || (GL_CHAR32_T_IS_UNICODE && (defined __FreeBSD__ || defined __NetBSD__ || defined __sun))
175 if (test_exit_status != EXIT_SUCCESS)
176 return test_exit_status;
177 fputs ("Skipping test: The GB18030 converter in this system's iconv is broken.\n", stderr);
178 return 77;
179 #endif
180 {
181 #if defined __GLIBC__
182 /* U+00A0 NO-BREAK SPACE */
183 is = for_character ("\201\060\204\062", 4);
184 ASSERT (is == 0);
185 #endif
186 /* U+00B7 MIDDLE DOT */
187 is = for_character ("\241\244", 2);
188 ASSERT (is == 0);
189 #if defined __GLIBC__
190 /* U+202F NARROW NO-BREAK SPACE */
191 is = for_character ("\201\066\247\062", 4);
192 ASSERT (is == 0);
193 #endif
194 /* U+3002 IDEOGRAPHIC FULL STOP */
195 is = for_character ("\241\243", 2);
196 ASSERT (is == 0);
197 /* U+1D13D MUSICAL SYMBOL QUARTER REST */
198 is = for_character ("\224\062\301\065", 4);
199 ASSERT (is == 0);
200 /* U+E0020 TAG SPACE */
201 is = for_character ("\323\066\231\060", 4);
202 ASSERT (is == 0);
203 }
204 return test_exit_status;
205
206 }
207
208 return 1;
209 }