• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2014 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include <gtest/gtest.h>
18 
19 #include <ctype.h>
20 
21 // We test from -1 (EOF) to 0xff, because that's the range for which behavior
22 // is actually defined. (It's explicitly undefined below or above that.) Most
23 // of our routines are no longer table-based and behave correctly for the
24 // entire int range, but that's not true of other C libraries that we might
25 // want to compare against, nor of our isalnum(3) and ispunt(3).
26 static constexpr int kMin = -1;
27 static constexpr int kMax = 256;
28 
TEST(ctype,isalnum)29 TEST(ctype, isalnum) {
30   for (int i = kMin; i < kMax; ++i) {
31     if ((i >= '0' && i <= '9') ||
32         (i >= 'A' && i <= 'Z') ||
33         (i >= 'a' && i <= 'z')) {
34       EXPECT_TRUE(isalnum(i)) << i;
35     } else {
36       EXPECT_FALSE(isalnum(i)) << i;
37     }
38   }
39 }
40 
TEST(ctype,isalnum_l)41 TEST(ctype, isalnum_l) {
42   for (int i = kMin; i < kMax; ++i) {
43     if ((i >= '0' && i <= '9') ||
44         (i >= 'A' && i <= 'Z') ||
45         (i >= 'a' && i <= 'z')) {
46       EXPECT_TRUE(isalnum_l(i, LC_GLOBAL_LOCALE)) << i;
47     } else {
48       EXPECT_FALSE(isalnum_l(i, LC_GLOBAL_LOCALE)) << i;
49     }
50   }
51 }
52 
TEST(ctype,isalpha)53 TEST(ctype, isalpha) {
54   for (int i = kMin; i < kMax; ++i) {
55     if ((i >= 'A' && i <= 'Z') ||
56         (i >= 'a' && i <= 'z')) {
57       EXPECT_TRUE(isalpha(i)) << i;
58     } else {
59       EXPECT_FALSE(isalpha(i)) << i;
60     }
61   }
62 }
63 
TEST(ctype,isalpha_l)64 TEST(ctype, isalpha_l) {
65   for (int i = kMin; i < kMax; ++i) {
66     if ((i >= 'A' && i <= 'Z') ||
67         (i >= 'a' && i <= 'z')) {
68       EXPECT_TRUE(isalpha_l(i, LC_GLOBAL_LOCALE)) << i;
69     } else {
70       EXPECT_FALSE(isalpha_l(i, LC_GLOBAL_LOCALE)) << i;
71     }
72   }
73 }
74 
TEST(ctype,isascii)75 TEST(ctype, isascii) {
76   for (int i = kMin; i < kMax; ++i) {
77     if (i >= 0 && i <= 0x7f) {
78       EXPECT_TRUE(isascii(i)) << i;
79     } else {
80       EXPECT_FALSE(isascii(i)) << i;
81     }
82   }
83 }
84 
TEST(ctype,isblank)85 TEST(ctype, isblank) {
86   for (int i = kMin; i < kMax; ++i) {
87     if (i == '\t' || i == ' ') {
88       EXPECT_TRUE(isblank(i)) << i;
89     } else {
90       EXPECT_FALSE(isblank(i)) << i;
91     }
92   }
93 }
94 
TEST(ctype,isblank_l)95 TEST(ctype, isblank_l) {
96   for (int i = kMin; i < kMax; ++i) {
97     if (i == '\t' || i == ' ') {
98       EXPECT_TRUE(isblank_l(i, LC_GLOBAL_LOCALE)) << i;
99     } else {
100       EXPECT_FALSE(isblank_l(i, LC_GLOBAL_LOCALE)) << i;
101     }
102   }
103 }
104 
TEST(ctype,iscntrl)105 TEST(ctype, iscntrl) {
106   for (int i = kMin; i < kMax; ++i) {
107     if ((i >= 0 && i < ' ') || i == 0x7f) {
108       EXPECT_TRUE(iscntrl(i)) << i;
109     } else {
110       EXPECT_FALSE(iscntrl(i)) << i;
111     }
112   }
113 }
114 
TEST(ctype,iscntrl_l)115 TEST(ctype, iscntrl_l) {
116   for (int i = kMin; i < kMax; ++i) {
117     if ((i >= 0 && i < ' ') || i == 0x7f) {
118       EXPECT_TRUE(iscntrl_l(i, LC_GLOBAL_LOCALE)) << i;
119     } else {
120       EXPECT_FALSE(iscntrl_l(i, LC_GLOBAL_LOCALE)) << i;
121     }
122   }
123 }
124 
TEST(ctype,isdigit)125 TEST(ctype, isdigit) {
126   for (int i = kMin; i < kMax; ++i) {
127     if (i >= '0' && i <= '9') {
128       EXPECT_TRUE(isdigit(i)) << i;
129     } else {
130       EXPECT_FALSE(isdigit(i)) << i;
131     }
132   }
133 }
134 
TEST(ctype,isdigit_l)135 TEST(ctype, isdigit_l) {
136   for (int i = kMin; i < kMax; ++i) {
137     if (i >= '0' && i <= '9') {
138       EXPECT_TRUE(isdigit_l(i, LC_GLOBAL_LOCALE)) << i;
139     } else {
140       EXPECT_FALSE(isdigit_l(i, LC_GLOBAL_LOCALE)) << i;
141     }
142   }
143 }
144 
TEST(ctype,isgraph)145 TEST(ctype, isgraph) {
146   for (int i = kMin; i < kMax; ++i) {
147     if (i >= '!' && i <= '~') {
148       EXPECT_TRUE(isgraph(i)) << i;
149     } else {
150       EXPECT_FALSE(isgraph(i)) << i;
151     }
152   }
153 }
154 
TEST(ctype,isgraph_l)155 TEST(ctype, isgraph_l) {
156   for (int i = kMin; i < kMax; ++i) {
157     if (i >= '!' && i <= '~') {
158       EXPECT_TRUE(isgraph_l(i, LC_GLOBAL_LOCALE)) << i;
159     } else {
160       EXPECT_FALSE(isgraph_l(i, LC_GLOBAL_LOCALE)) << i;
161     }
162   }
163 }
164 
TEST(ctype,islower)165 TEST(ctype, islower) {
166   for (int i = kMin; i < kMax; ++i) {
167     if (i >= 'a' && i <= 'z') {
168       EXPECT_TRUE(islower(i)) << i;
169     } else {
170       EXPECT_FALSE(islower(i)) << i;
171     }
172   }
173 }
174 
TEST(ctype,islower_l)175 TEST(ctype, islower_l) {
176   for (int i = kMin; i < kMax; ++i) {
177     if (i >= 'a' && i <= 'z') {
178       EXPECT_TRUE(islower_l(i, LC_GLOBAL_LOCALE)) << i;
179     } else {
180       EXPECT_FALSE(islower_l(i, LC_GLOBAL_LOCALE)) << i;
181     }
182   }
183 }
184 
TEST(ctype,isprint)185 TEST(ctype, isprint) {
186   for (int i = kMin; i < kMax; ++i) {
187     if (i >= ' ' && i <= '~') {
188       EXPECT_TRUE(isprint(i)) << i;
189     } else {
190       EXPECT_FALSE(isprint(i)) << i;
191     }
192   }
193 }
194 
TEST(ctype,isprint_l)195 TEST(ctype, isprint_l) {
196   for (int i = kMin; i < kMax; ++i) {
197     if (i >= ' ' && i <= '~') {
198       EXPECT_TRUE(isprint_l(i, LC_GLOBAL_LOCALE)) << i;
199     } else {
200       EXPECT_FALSE(isprint_l(i, LC_GLOBAL_LOCALE)) << i;
201     }
202   }
203 }
204 
TEST(ctype,ispunct)205 TEST(ctype, ispunct) {
206   for (int i = kMin; i < kMax; ++i) {
207     if ((i >= '!' && i <= '/') ||
208         (i >= ':' && i <= '@') ||
209         (i >= '[' && i <= '`') ||
210         (i >= '{' && i <= '~')) {
211       EXPECT_TRUE(ispunct(i)) << i;
212     } else {
213       EXPECT_FALSE(ispunct(i)) << i;
214     }
215   }
216 }
217 
TEST(ctype,ispunct_l)218 TEST(ctype, ispunct_l) {
219   for (int i = kMin; i < kMax; ++i) {
220     if ((i >= '!' && i <= '/') ||
221         (i >= ':' && i <= '@') ||
222         (i >= '[' && i <= '`') ||
223         (i >= '{' && i <= '~')) {
224       EXPECT_TRUE(ispunct_l(i, LC_GLOBAL_LOCALE)) << i;
225     } else {
226       EXPECT_FALSE(ispunct_l(i, LC_GLOBAL_LOCALE)) << i;
227     }
228   }
229 }
230 
TEST(ctype,isspace)231 TEST(ctype, isspace) {
232   for (int i = kMin; i < kMax; ++i) {
233     if ((i >= '\t' && i <= '\r') || i == ' ') {
234       EXPECT_TRUE(isspace(i)) << i;
235     } else {
236       EXPECT_FALSE(isspace(i)) << i;
237     }
238   }
239 }
240 
TEST(ctype,isspace_l)241 TEST(ctype, isspace_l) {
242   for (int i = kMin; i < kMax; ++i) {
243     if ((i >= '\t' && i <= '\r') || i == ' ') {
244       EXPECT_TRUE(isspace_l(i, LC_GLOBAL_LOCALE)) << i;
245     } else {
246       EXPECT_FALSE(isspace_l(i, LC_GLOBAL_LOCALE)) << i;
247     }
248   }
249 }
250 
TEST(ctype,isupper)251 TEST(ctype, isupper) {
252   for (int i = kMin; i < kMax; ++i) {
253     if (i >= 'A' && i <= 'Z') {
254       EXPECT_TRUE(isupper(i)) << i;
255     } else {
256       EXPECT_FALSE(isupper(i)) << i;
257     }
258   }
259 }
260 
TEST(ctype,isupper_l)261 TEST(ctype, isupper_l) {
262   for (int i = kMin; i < kMax; ++i) {
263     if (i >= 'A' && i <= 'Z') {
264       EXPECT_TRUE(isupper_l(i, LC_GLOBAL_LOCALE)) << i;
265     } else {
266       EXPECT_FALSE(isupper_l(i, LC_GLOBAL_LOCALE)) << i;
267     }
268   }
269 }
270 
TEST(ctype,isxdigit)271 TEST(ctype, isxdigit) {
272   for (int i = kMin; i < kMax; ++i) {
273     if ((i >= '0' && i <= '9') ||
274         (i >= 'A' && i <= 'F') ||
275         (i >= 'a' && i <= 'f')) {
276       EXPECT_TRUE(isxdigit(i)) << i;
277     } else {
278       EXPECT_FALSE(isxdigit(i)) << i;
279     }
280   }
281 }
282 
TEST(ctype,isxdigit_l)283 TEST(ctype, isxdigit_l) {
284   for (int i = kMin; i < kMax; ++i) {
285     if ((i >= '0' && i <= '9') ||
286         (i >= 'A' && i <= 'F') ||
287         (i >= 'a' && i <= 'f')) {
288       EXPECT_TRUE(isxdigit_l(i, LC_GLOBAL_LOCALE)) << i;
289     } else {
290       EXPECT_FALSE(isxdigit_l(i, LC_GLOBAL_LOCALE)) << i;
291     }
292   }
293 }
294 
TEST(ctype,toascii)295 TEST(ctype, toascii) {
296   EXPECT_EQ('a', toascii('a'));
297   EXPECT_EQ('a', toascii(0x80 | 'a'));
298 }
299 
TEST(ctype,tolower)300 TEST(ctype, tolower) {
301   EXPECT_EQ('!', tolower('!'));
302   EXPECT_EQ('a', tolower('a'));
303   EXPECT_EQ('a', tolower('A'));
304 }
305 
TEST(ctype,tolower_l)306 TEST(ctype, tolower_l) {
307   EXPECT_EQ('!', tolower_l('!', LC_GLOBAL_LOCALE));
308   EXPECT_EQ('a', tolower_l('a', LC_GLOBAL_LOCALE));
309   EXPECT_EQ('a', tolower_l('A', LC_GLOBAL_LOCALE));
310 }
311 
TEST(ctype,_tolower)312 TEST(ctype, _tolower) {
313   // _tolower may mangle characters for which isupper is false.
314   EXPECT_EQ('a', _tolower('A'));
315 }
316 
TEST(ctype,toupper)317 TEST(ctype, toupper) {
318   EXPECT_EQ('!', toupper('!'));
319   EXPECT_EQ('A', toupper('a'));
320   EXPECT_EQ('A', toupper('A'));
321 }
322 
TEST(ctype,toupper_l)323 TEST(ctype, toupper_l) {
324   EXPECT_EQ('!', toupper_l('!', LC_GLOBAL_LOCALE));
325   EXPECT_EQ('A', toupper_l('a', LC_GLOBAL_LOCALE));
326   EXPECT_EQ('A', toupper_l('A', LC_GLOBAL_LOCALE));
327 }
328 
TEST(ctype,_toupper)329 TEST(ctype, _toupper) {
330   // _toupper may mangle characters for which islower is false.
331   EXPECT_EQ('A', _toupper('a'));
332 }
333