uchar C++ tests: Fix build error on FreeBSD 12.
[gnulib.git] / tests / test-c-ctype.c
blob0cf1fdf29e4b410e3b91b16e9afe3ba27a52b4c6
1 /* Test of character handling in C locale.
2 Copyright (C) 2005, 2007-2020 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>, 2005. */
19 #include <config.h>
21 #include "c-ctype.h"
23 #include <ctype.h>
24 #include <limits.h>
25 #include <locale.h>
27 #include "macros.h"
29 static void
30 test_agree_with_C_locale (void)
32 int c;
34 for (c = 0; c <= UCHAR_MAX; c++)
36 ASSERT (c_isascii (c) == (isascii (c) != 0));
37 if (c_isascii (c))
39 ASSERT (c_isalnum (c) == (isalnum (c) != 0));
40 ASSERT (c_isalpha (c) == (isalpha (c) != 0));
41 ASSERT (c_isblank (c) == (isblank (c) != 0));
42 ASSERT (c_iscntrl (c) == (iscntrl (c) != 0));
43 ASSERT (c_isdigit (c) == (isdigit (c) != 0));
44 ASSERT (c_islower (c) == (islower (c) != 0));
45 ASSERT (c_isgraph (c) == (isgraph (c) != 0));
46 ASSERT (c_isprint (c) == (isprint (c) != 0));
47 ASSERT (c_ispunct (c) == (ispunct (c) != 0));
48 ASSERT (c_isspace (c) == (isspace (c) != 0));
49 ASSERT (c_isupper (c) == (isupper (c) != 0));
50 ASSERT (c_isxdigit (c) == (isxdigit (c) != 0));
51 ASSERT (c_tolower (c) == tolower (c));
52 ASSERT (c_toupper (c) == toupper (c));
57 static void
58 test_all (void)
60 int c;
61 int n_isascii = 0;
63 for (c = CHAR_MIN; c <= UCHAR_MAX; c++)
65 if (! (0 <= c && c <= CHAR_MAX))
67 ASSERT (! c_isascii (c));
68 ASSERT (! c_isalnum (c));
69 ASSERT (! c_isalpha (c));
70 ASSERT (! c_isblank (c));
71 ASSERT (! c_iscntrl (c));
72 ASSERT (! c_isdigit (c));
73 ASSERT (! c_islower (c));
74 ASSERT (! c_isgraph (c));
75 ASSERT (! c_isprint (c));
76 ASSERT (! c_ispunct (c));
77 ASSERT (! c_isspace (c));
78 ASSERT (! c_isupper (c));
79 ASSERT (! c_isxdigit (c));
80 ASSERT (c_tolower (c) == c);
81 ASSERT (c_toupper (c) == c);
84 n_isascii += c_isascii (c);
86 #ifdef C_CTYPE_ASCII
87 ASSERT (c_isascii (c) == (0 <= c && c <= 0x7f));
88 #endif
90 ASSERT (c_isascii (c) == (c_isprint (c) || c_iscntrl (c)));
92 ASSERT (c_isalnum (c) == (c_isalpha (c) || c_isdigit (c)));
94 ASSERT (c_isalpha (c) == (c_islower (c) || c_isupper (c)));
96 switch (c)
98 case '\t': case ' ':
99 ASSERT (c_isblank (c) == 1);
100 break;
101 default:
102 ASSERT (c_isblank (c) == 0);
103 break;
106 #ifdef C_CTYPE_ASCII
107 ASSERT (c_iscntrl (c) == ((c >= 0 && c < 0x20) || c == 0x7f));
108 #endif
110 switch (c)
112 case '\a': case '\b': case '\f': case '\n':
113 case '\r': case '\t': case '\v':
114 ASSERT (c_iscntrl (c));
115 break;
118 ASSERT (! (c_iscntrl (c) && c_isprint (c)));
120 switch (c)
122 case '0': case '1': case '2': case '3': case '4': case '5':
123 case '6': case '7': case '8': case '9':
124 ASSERT (c_isdigit (c) == 1);
125 break;
126 default:
127 ASSERT (c_isdigit (c) == 0);
128 break;
131 switch (c)
133 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
134 case 'g': case 'h': case 'i': case 'j': case 'k': case 'l':
135 case 'm': case 'n': case 'o': case 'p': case 'q': case 'r':
136 case 's': case 't': case 'u': case 'v': case 'w': case 'x':
137 case 'y': case 'z':
138 ASSERT (c_islower (c) == 1);
139 ASSERT (c_toupper (c) == c - 'a' + 'A');
140 break;
141 default:
142 ASSERT (c_islower (c) == 0);
143 ASSERT (c_toupper (c) == c);
144 break;
147 #ifdef C_CTYPE_ASCII
148 ASSERT (c_isgraph (c) == ((c >= 0x20 && c < 0x7f) && c != ' '));
150 ASSERT (c_isprint (c) == (c >= 0x20 && c < 0x7f));
151 #endif
153 ASSERT (c_isgraph (c) == (c_isalnum (c) || c_ispunct (c)));
155 ASSERT (c_isprint (c) == (c_isgraph (c) || c == ' '));
157 switch (c)
159 case '!': case '"': case '#': case '$': case '%': case '&': case '\'':
160 case '(': case ')': case '*': case '+': case ',': case '-': case '.':
161 case '/': case ':': case ';': case '<': case '=': case '>': case '?':
162 case '@': case '[': case'\\': case ']': case '^': case '_': case '`':
163 case '{': case '|': case '}': case '~':
164 ASSERT (c_ispunct (c) == 1);
165 break;
166 default:
167 ASSERT (c_ispunct (c) == 0);
168 break;
171 switch (c)
173 case ' ': case '\t': case '\n': case '\v': case '\f': case '\r':
174 ASSERT (c_isspace (c) == 1);
175 break;
176 default:
177 ASSERT (c_isspace (c) == 0);
178 break;
181 switch (c)
183 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
184 case 'G': case 'H': case 'I': case 'J': case 'K': case 'L':
185 case 'M': case 'N': case 'O': case 'P': case 'Q': case 'R':
186 case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
187 case 'Y': case 'Z':
188 ASSERT (c_isupper (c) == 1);
189 ASSERT (c_tolower (c) == c - 'A' + 'a');
190 break;
191 default:
192 ASSERT (c_isupper (c) == 0);
193 ASSERT (c_tolower (c) == c);
194 break;
197 switch (c)
199 case '0': case '1': case '2': case '3': case '4': case '5':
200 case '6': case '7': case '8': case '9':
201 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
202 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
203 ASSERT (c_isxdigit (c) == 1);
204 break;
205 default:
206 ASSERT (c_isxdigit (c) == 0);
207 break;
211 ASSERT (n_isascii == 128);
215 main ()
217 test_agree_with_C_locale ();
219 test_all ();
221 setlocale (LC_ALL, "de_DE");
222 test_all ();
224 setlocale (LC_ALL, "ja_JP.EUC-JP");
225 test_all ();
227 return 0;