• 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 
TEST(ctype,isalnum)21 TEST(ctype, isalnum) {
22   EXPECT_TRUE(isalnum('1'));
23   EXPECT_TRUE(isalnum('a'));
24   EXPECT_TRUE(isalnum('A'));
25   EXPECT_FALSE(isalnum('!'));
26   EXPECT_FALSE(isalnum(' '));
27 }
28 
TEST(ctype,isalnum_l)29 TEST(ctype, isalnum_l) {
30   EXPECT_TRUE(isalnum_l('1', LC_GLOBAL_LOCALE));
31   EXPECT_TRUE(isalnum_l('a', LC_GLOBAL_LOCALE));
32   EXPECT_TRUE(isalnum_l('A', LC_GLOBAL_LOCALE));
33   EXPECT_FALSE(isalnum_l('!', LC_GLOBAL_LOCALE));
34   EXPECT_FALSE(isalnum_l(' ', LC_GLOBAL_LOCALE));
35 }
36 
TEST(ctype,isalpha)37 TEST(ctype, isalpha) {
38   EXPECT_FALSE(isalpha('1'));
39   EXPECT_TRUE(isalpha('a'));
40   EXPECT_TRUE(isalpha('A'));
41   EXPECT_FALSE(isalpha('!'));
42   EXPECT_FALSE(isalpha(' '));
43 }
44 
TEST(ctype,isalpha_l)45 TEST(ctype, isalpha_l) {
46   EXPECT_FALSE(isalpha_l('1', LC_GLOBAL_LOCALE));
47   EXPECT_TRUE(isalpha_l('a', LC_GLOBAL_LOCALE));
48   EXPECT_TRUE(isalpha_l('A', LC_GLOBAL_LOCALE));
49   EXPECT_FALSE(isalpha_l('!', LC_GLOBAL_LOCALE));
50   EXPECT_FALSE(isalpha_l(' ', LC_GLOBAL_LOCALE));
51 }
52 
TEST(ctype,isascii)53 TEST(ctype, isascii) {
54   EXPECT_TRUE(isascii('\x7f'));
55   EXPECT_FALSE(isascii('\x80'));
56 }
57 
TEST(ctype,isblank)58 TEST(ctype, isblank) {
59   EXPECT_FALSE(isblank('1'));
60   EXPECT_TRUE(isblank(' '));
61   EXPECT_TRUE(isblank('\t'));
62 }
63 
TEST(ctype,isblank_l)64 TEST(ctype, isblank_l) {
65   EXPECT_FALSE(isblank_l('1', LC_GLOBAL_LOCALE));
66   EXPECT_TRUE(isblank_l(' ', LC_GLOBAL_LOCALE));
67   EXPECT_TRUE(isblank_l('\t', LC_GLOBAL_LOCALE));
68 }
69 
TEST(ctype,iscntrl)70 TEST(ctype, iscntrl) {
71   EXPECT_FALSE(iscntrl('1'));
72   EXPECT_TRUE(iscntrl('\b'));
73 }
74 
TEST(ctype,iscntrl_l)75 TEST(ctype, iscntrl_l) {
76   EXPECT_FALSE(iscntrl_l('1', LC_GLOBAL_LOCALE));
77   EXPECT_TRUE(iscntrl_l('\b', LC_GLOBAL_LOCALE));
78 }
79 
TEST(ctype,isdigit)80 TEST(ctype, isdigit) {
81   EXPECT_TRUE(isdigit('1'));
82   EXPECT_FALSE(isdigit('a'));
83   EXPECT_FALSE(isdigit('x'));
84 }
85 
TEST(ctype,isdigit_l)86 TEST(ctype, isdigit_l) {
87   EXPECT_TRUE(isdigit_l('1', LC_GLOBAL_LOCALE));
88   EXPECT_FALSE(isdigit_l('a', LC_GLOBAL_LOCALE));
89   EXPECT_FALSE(isdigit_l('x', LC_GLOBAL_LOCALE));
90 }
91 
TEST(ctype,isgraph)92 TEST(ctype, isgraph) {
93   EXPECT_TRUE(isgraph('a'));
94   EXPECT_TRUE(isgraph('A'));
95   EXPECT_TRUE(isgraph('1'));
96   EXPECT_TRUE(isgraph('!'));
97   EXPECT_FALSE(isgraph(' '));
98 }
99 
TEST(ctype,isgraph_l)100 TEST(ctype, isgraph_l) {
101   EXPECT_TRUE(isgraph_l('a', LC_GLOBAL_LOCALE));
102   EXPECT_TRUE(isgraph_l('A', LC_GLOBAL_LOCALE));
103   EXPECT_TRUE(isgraph_l('1', LC_GLOBAL_LOCALE));
104   EXPECT_TRUE(isgraph_l('!', LC_GLOBAL_LOCALE));
105   EXPECT_FALSE(isgraph_l(' ', LC_GLOBAL_LOCALE));
106 }
107 
TEST(ctype,islower)108 TEST(ctype, islower) {
109   EXPECT_TRUE(islower('a'));
110   EXPECT_FALSE(islower('A'));
111   EXPECT_FALSE(islower('!'));
112 }
113 
TEST(ctype,islower_l)114 TEST(ctype, islower_l) {
115   EXPECT_TRUE(islower_l('a', LC_GLOBAL_LOCALE));
116   EXPECT_FALSE(islower_l('A', LC_GLOBAL_LOCALE));
117   EXPECT_FALSE(islower_l('!', LC_GLOBAL_LOCALE));
118 }
119 
TEST(ctype,isprint)120 TEST(ctype, isprint) {
121   EXPECT_TRUE(isprint('a'));
122   EXPECT_TRUE(isprint(' '));
123   EXPECT_FALSE(isprint('\b'));
124 }
125 
TEST(ctype,isprint_l)126 TEST(ctype, isprint_l) {
127   EXPECT_TRUE(isprint_l('a', LC_GLOBAL_LOCALE));
128   EXPECT_TRUE(isprint_l(' ', LC_GLOBAL_LOCALE));
129   EXPECT_FALSE(isprint_l('\b', LC_GLOBAL_LOCALE));
130 }
131 
TEST(ctype,ispunct)132 TEST(ctype, ispunct) {
133   EXPECT_TRUE(ispunct('!'));
134   EXPECT_FALSE(ispunct('a'));
135   EXPECT_FALSE(ispunct(' '));
136   EXPECT_FALSE(ispunct('\b'));
137 }
138 
TEST(ctype,ispunct_l)139 TEST(ctype, ispunct_l) {
140   EXPECT_TRUE(ispunct_l('!', LC_GLOBAL_LOCALE));
141   EXPECT_FALSE(ispunct_l('a', LC_GLOBAL_LOCALE));
142   EXPECT_FALSE(ispunct_l(' ', LC_GLOBAL_LOCALE));
143   EXPECT_FALSE(ispunct_l('\b', LC_GLOBAL_LOCALE));
144 }
145 
TEST(ctype,isspace)146 TEST(ctype, isspace) {
147   EXPECT_TRUE(isspace(' '));
148   EXPECT_TRUE(isspace('\f'));
149   EXPECT_TRUE(isspace('\n'));
150   EXPECT_TRUE(isspace('\r'));
151   EXPECT_TRUE(isspace('\t'));
152   EXPECT_TRUE(isspace('\v'));
153   EXPECT_FALSE(isspace('a'));
154   EXPECT_FALSE(isspace('!'));
155 }
156 
TEST(ctype,isspace_l)157 TEST(ctype, isspace_l) {
158   EXPECT_TRUE(isspace_l(' ', LC_GLOBAL_LOCALE));
159   EXPECT_TRUE(isspace_l('\f', LC_GLOBAL_LOCALE));
160   EXPECT_TRUE(isspace_l('\n', LC_GLOBAL_LOCALE));
161   EXPECT_TRUE(isspace_l('\r', LC_GLOBAL_LOCALE));
162   EXPECT_TRUE(isspace_l('\t', LC_GLOBAL_LOCALE));
163   EXPECT_TRUE(isspace_l('\v', LC_GLOBAL_LOCALE));
164   EXPECT_FALSE(isspace_l('a', LC_GLOBAL_LOCALE));
165   EXPECT_FALSE(isspace_l('!', LC_GLOBAL_LOCALE));
166 }
167 
TEST(ctype,isupper)168 TEST(ctype, isupper) {
169   EXPECT_TRUE(isupper('A'));
170   EXPECT_FALSE(isupper('a'));
171   EXPECT_FALSE(isupper('!'));
172 }
173 
TEST(ctype,isupper_l)174 TEST(ctype, isupper_l) {
175   EXPECT_TRUE(isupper_l('A', LC_GLOBAL_LOCALE));
176   EXPECT_FALSE(isupper_l('a', LC_GLOBAL_LOCALE));
177   EXPECT_FALSE(isupper_l('!', LC_GLOBAL_LOCALE));
178 }
179 
TEST(ctype,isxdigit)180 TEST(ctype, isxdigit) {
181   EXPECT_TRUE(isxdigit('0'));
182   EXPECT_FALSE(isxdigit('x'));
183   EXPECT_TRUE(isxdigit('1'));
184   EXPECT_TRUE(isxdigit('a'));
185   EXPECT_TRUE(isxdigit('A'));
186   EXPECT_FALSE(isxdigit('g'));
187   EXPECT_FALSE(isxdigit(' '));
188 }
189 
TEST(ctype,isxdigit_l)190 TEST(ctype, isxdigit_l) {
191   EXPECT_TRUE(isxdigit_l('0', LC_GLOBAL_LOCALE));
192   EXPECT_FALSE(isxdigit_l('x', LC_GLOBAL_LOCALE));
193   EXPECT_TRUE(isxdigit_l('1', LC_GLOBAL_LOCALE));
194   EXPECT_TRUE(isxdigit_l('a', LC_GLOBAL_LOCALE));
195   EXPECT_TRUE(isxdigit_l('A', LC_GLOBAL_LOCALE));
196   EXPECT_FALSE(isxdigit_l('g', LC_GLOBAL_LOCALE));
197   EXPECT_FALSE(isxdigit_l(' ', LC_GLOBAL_LOCALE));
198 }
199 
TEST(ctype,toascii)200 TEST(ctype, toascii) {
201   EXPECT_EQ('a', toascii('a'));
202   EXPECT_EQ('a', toascii(0x80 | 'a'));
203 }
204 
TEST(ctype,tolower)205 TEST(ctype, tolower) {
206   EXPECT_EQ('!', tolower('!'));
207   EXPECT_EQ('a', tolower('a'));
208   EXPECT_EQ('a', tolower('A'));
209 }
210 
TEST(ctype,tolower_l)211 TEST(ctype, tolower_l) {
212   EXPECT_EQ('!', tolower_l('!', LC_GLOBAL_LOCALE));
213   EXPECT_EQ('a', tolower_l('a', LC_GLOBAL_LOCALE));
214   EXPECT_EQ('a', tolower_l('A', LC_GLOBAL_LOCALE));
215 }
216 
TEST(ctype,_tolower)217 TEST(ctype, _tolower) {
218   // _tolower may mangle characters for which isupper is false.
219   EXPECT_EQ('a', _tolower('A'));
220 }
221 
TEST(ctype,toupper)222 TEST(ctype, toupper) {
223   EXPECT_EQ('!', toupper('!'));
224   EXPECT_EQ('A', toupper('a'));
225   EXPECT_EQ('A', toupper('A'));
226 }
227 
TEST(ctype,toupper_l)228 TEST(ctype, toupper_l) {
229   EXPECT_EQ('!', toupper_l('!', LC_GLOBAL_LOCALE));
230   EXPECT_EQ('A', toupper_l('a', LC_GLOBAL_LOCALE));
231   EXPECT_EQ('A', toupper_l('A', LC_GLOBAL_LOCALE));
232 }
233 
TEST(ctype,_toupper)234 TEST(ctype, _toupper) {
235   // _toupper may mangle characters for which islower is false.
236   EXPECT_EQ('A', _toupper('a'));
237 }
238