• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2016 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 <wctype.h>
18 
19 #include <dlfcn.h>
20 
21 #include <gtest/gtest.h>
22 
23 #include "utils.h"
24 
25 class UtfLocale {
26  public:
UtfLocale()27   UtfLocale() : l(newlocale(LC_ALL, "C.UTF-8", nullptr)) {}
~UtfLocale()28   ~UtfLocale() { freelocale(l); }
29   locale_t l;
30 };
31 
TestIsWideFn(int fn (wint_t),int fn_l (wint_t,locale_t),const wchar_t * trues,const wchar_t * falses)32 static void TestIsWideFn(int fn(wint_t),
33                          int fn_l(wint_t, locale_t),
34                          const wchar_t* trues,
35                          const wchar_t* falses) {
36   UtfLocale l;
37   for (const wchar_t* p = trues; *p; ++p) {
38     if (!have_dl() && *p > 0x7f) {
39       GTEST_LOG_(INFO) << "skipping unicode test " << *p;
40       continue;
41     }
42     EXPECT_TRUE(fn(*p)) << *p;
43     EXPECT_TRUE(fn_l(*p, l.l)) << *p;
44   }
45   for (const wchar_t* p = falses; *p; ++p) {
46     if (!have_dl() && *p > 0x7f) {
47       GTEST_LOG_(INFO) << "skipping unicode test " << *p;
48       continue;
49     }
50     EXPECT_FALSE(fn(*p)) << *p;
51     EXPECT_FALSE(fn_l(*p, l.l)) << *p;
52   }
53 }
54 
TEST(wctype,iswalnum)55 TEST(wctype, iswalnum) {
56   TestIsWideFn(iswalnum, iswalnum_l, L"1aAÇçΔδ", L"! \b");
57 }
58 
TEST(wctype,iswalpha)59 TEST(wctype, iswalpha) {
60   TestIsWideFn(iswalpha, iswalpha_l, L"aAÇçΔδ", L"1! \b");
61 }
62 
TEST(wctype,iswblank)63 TEST(wctype, iswblank) {
64   TestIsWideFn(iswblank, iswblank_l, L" \t", L"1aA!\bÇçΔδ");
65 }
66 
TEST(wctype,iswcntrl)67 TEST(wctype, iswcntrl) {
68   TestIsWideFn(iswcntrl, iswcntrl_l, L"\b\u009f", L"1aA! ÇçΔδ");
69 }
70 
TEST(wctype,iswdigit)71 TEST(wctype, iswdigit) {
72   TestIsWideFn(iswdigit, iswdigit_l, L"1", L"aA! \bÇçΔδ");
73 }
74 
TEST(wctype,iswgraph)75 TEST(wctype, iswgraph) {
76   TestIsWideFn(iswgraph, iswgraph_l, L"1aA!ÇçΔδ", L" \b");
77 }
78 
TEST(wctype,iswlower)79 TEST(wctype, iswlower) {
80   TestIsWideFn(iswlower, iswlower_l, L"açδ", L"1A! \bÇΔ");
81 }
82 
TEST(wctype,iswprint)83 TEST(wctype, iswprint) {
84   TestIsWideFn(iswprint, iswprint_l, L"1aA! ÇçΔδ", L"\b");
85 }
86 
TEST(wctype,iswpunct)87 TEST(wctype, iswpunct) {
88   TestIsWideFn(iswpunct, iswpunct_l, L"!", L"1aA \bÇçΔδ");
89 }
90 
TEST(wctype,iswspace)91 TEST(wctype, iswspace) {
92   TestIsWideFn(iswspace, iswspace_l, L" \f\t", L"1aA!\bÇçΔδ");
93 }
94 
TEST(wctype,iswupper)95 TEST(wctype, iswupper) {
96   TestIsWideFn(iswupper, iswupper_l, L"AÇΔ", L"1a! \bçδ");
97 }
98 
TEST(wctype,iswxdigit)99 TEST(wctype, iswxdigit) {
100   TestIsWideFn(iswxdigit, iswxdigit_l, L"01aA", L"xg! \b");
101 }
102 
TEST(wctype,towlower)103 TEST(wctype, towlower) {
104   EXPECT_EQ(WEOF, towlower(WEOF));
105   EXPECT_EQ(wint_t('!'), towlower(L'!'));
106   EXPECT_EQ(wint_t('a'), towlower(L'a'));
107   EXPECT_EQ(wint_t('a'), towlower(L'A'));
108   if (have_dl()) {
109     EXPECT_EQ(wint_t(L'ç'), towlower(L'ç'));
110     EXPECT_EQ(wint_t(L'ç'), towlower(L'Ç'));
111     EXPECT_EQ(wint_t(L'δ'), towlower(L'δ'));
112     EXPECT_EQ(wint_t(L'δ'), towlower(L'Δ'));
113   } else {
114     GTEST_SKIP() << "icu not available";
115   }
116 }
117 
TEST(wctype,towlower_l)118 TEST(wctype, towlower_l) {
119   UtfLocale l;
120   EXPECT_EQ(WEOF, towlower(WEOF));
121   EXPECT_EQ(wint_t('!'), towlower_l(L'!', l.l));
122   EXPECT_EQ(wint_t('a'), towlower_l(L'a', l.l));
123   EXPECT_EQ(wint_t('a'), towlower_l(L'A', l.l));
124   if (have_dl()) {
125     EXPECT_EQ(wint_t(L'ç'), towlower_l(L'ç', l.l));
126     EXPECT_EQ(wint_t(L'ç'), towlower_l(L'Ç', l.l));
127     EXPECT_EQ(wint_t(L'δ'), towlower_l(L'δ', l.l));
128     EXPECT_EQ(wint_t(L'δ'), towlower_l(L'Δ', l.l));
129   } else {
130     GTEST_SKIP() << "icu not available";
131   }
132 }
133 
TEST(wctype,towupper)134 TEST(wctype, towupper) {
135   EXPECT_EQ(WEOF, towupper(WEOF));
136   EXPECT_EQ(wint_t('!'), towupper(L'!'));
137   EXPECT_EQ(wint_t('A'), towupper(L'a'));
138   EXPECT_EQ(wint_t('A'), towupper(L'A'));
139   if (have_dl()) {
140     EXPECT_EQ(wint_t(L'Ç'), towupper(L'ç'));
141     EXPECT_EQ(wint_t(L'Ç'), towupper(L'Ç'));
142     EXPECT_EQ(wint_t(L'Δ'), towupper(L'δ'));
143     EXPECT_EQ(wint_t(L'Δ'), towupper(L'Δ'));
144   } else {
145     GTEST_SKIP() << "icu not available";
146   }
147 }
148 
TEST(wctype,towupper_l)149 TEST(wctype, towupper_l) {
150   UtfLocale l;
151   EXPECT_EQ(WEOF, towupper_l(WEOF, l.l));
152   EXPECT_EQ(wint_t('!'), towupper_l(L'!', l.l));
153   EXPECT_EQ(wint_t('A'), towupper_l(L'a', l.l));
154   EXPECT_EQ(wint_t('A'), towupper_l(L'A', l.l));
155   if (have_dl()) {
156     EXPECT_EQ(wint_t(L'Ç'), towupper_l(L'ç', l.l));
157     EXPECT_EQ(wint_t(L'Ç'), towupper_l(L'Ç', l.l));
158     EXPECT_EQ(wint_t(L'Δ'), towupper_l(L'δ', l.l));
159     EXPECT_EQ(wint_t(L'Δ'), towupper_l(L'Δ', l.l));
160   } else {
161     GTEST_SKIP() << "icu not available";
162   }
163 }
164 
TEST(wctype,wctype)165 TEST(wctype, wctype) {
166   EXPECT_TRUE(wctype("alnum") != 0);
167   EXPECT_TRUE(wctype("alpha") != 0);
168   EXPECT_TRUE(wctype("blank") != 0);
169   EXPECT_TRUE(wctype("cntrl") != 0);
170   EXPECT_TRUE(wctype("digit") != 0);
171   EXPECT_TRUE(wctype("graph") != 0);
172   EXPECT_TRUE(wctype("lower") != 0);
173   EXPECT_TRUE(wctype("print") != 0);
174   EXPECT_TRUE(wctype("punct") != 0);
175   EXPECT_TRUE(wctype("space") != 0);
176   EXPECT_TRUE(wctype("upper") != 0);
177   EXPECT_TRUE(wctype("xdigit") != 0);
178 
179   EXPECT_TRUE(wctype("monkeys") == 0);
180 }
181 
TEST(wctype,wctype_l)182 TEST(wctype, wctype_l) {
183   UtfLocale l;
184   EXPECT_TRUE(wctype_l("alnum", l.l) != 0);
185   EXPECT_TRUE(wctype_l("alpha", l.l) != 0);
186   EXPECT_TRUE(wctype_l("blank", l.l) != 0);
187   EXPECT_TRUE(wctype_l("cntrl", l.l) != 0);
188   EXPECT_TRUE(wctype_l("digit", l.l) != 0);
189   EXPECT_TRUE(wctype_l("graph", l.l) != 0);
190   EXPECT_TRUE(wctype_l("lower", l.l) != 0);
191   EXPECT_TRUE(wctype_l("print", l.l) != 0);
192   EXPECT_TRUE(wctype_l("punct", l.l) != 0);
193   EXPECT_TRUE(wctype_l("space", l.l) != 0);
194   EXPECT_TRUE(wctype_l("upper", l.l) != 0);
195   EXPECT_TRUE(wctype_l("xdigit", l.l) != 0);
196 
197   EXPECT_TRUE(wctype_l("monkeys", l.l) == 0);
198 }
199 
TEST(wctype,iswctype)200 TEST(wctype, iswctype) {
201   EXPECT_TRUE(iswctype(L'a', wctype("alnum")));
202   EXPECT_TRUE(iswctype(L'1', wctype("alnum")));
203   EXPECT_FALSE(iswctype(L' ', wctype("alnum")));
204 
205   EXPECT_EQ(0, iswctype(WEOF, wctype("alnum")));
206 }
207 
TEST(wctype,iswctype_l)208 TEST(wctype, iswctype_l) {
209   UtfLocale l;
210   EXPECT_TRUE(iswctype_l(L'a', wctype_l("alnum", l.l), l.l));
211   EXPECT_TRUE(iswctype_l(L'1', wctype_l("alnum", l.l), l.l));
212   EXPECT_FALSE(iswctype_l(L' ', wctype_l("alnum", l.l), l.l));
213 
214   EXPECT_EQ(0, iswctype_l(WEOF, wctype_l("alnum", l.l), l.l));
215 }
216 
TEST(wctype,towctrans)217 TEST(wctype, towctrans) {
218   EXPECT_TRUE(wctrans("tolower") != nullptr);
219   EXPECT_TRUE(wctrans("toupper") != nullptr);
220 
221   EXPECT_TRUE(wctrans("monkeys") == nullptr);
222 }
223 
TEST(wctype,towctrans_l)224 TEST(wctype, towctrans_l) {
225   UtfLocale l;
226   EXPECT_TRUE(wctrans_l("tolower", l.l) != nullptr);
227   EXPECT_TRUE(wctrans_l("toupper", l.l) != nullptr);
228 
229   EXPECT_TRUE(wctrans_l("monkeys", l.l) == nullptr);
230 }
231 
TEST(wctype,wctrans)232 TEST(wctype, wctrans) {
233   EXPECT_EQ(wint_t('a'), towctrans(L'A', wctrans("tolower")));
234   EXPECT_EQ(WEOF, towctrans(WEOF, wctrans("tolower")));
235 
236   EXPECT_EQ(wint_t('A'), towctrans(L'a', wctrans("toupper")));
237   EXPECT_EQ(WEOF, towctrans(WEOF, wctrans("toupper")));
238 }
239 
TEST(wctype,wctrans_l)240 TEST(wctype, wctrans_l) {
241   UtfLocale l;
242   EXPECT_EQ(wint_t('a'), towctrans_l(L'A', wctrans_l("tolower", l.l), l.l));
243   EXPECT_EQ(WEOF, towctrans_l(WEOF, wctrans_l("tolower", l.l), l.l));
244 
245   EXPECT_EQ(wint_t('A'), towctrans_l(L'a', wctrans_l("toupper", l.l), l.l));
246   EXPECT_EQ(WEOF, towctrans_l(WEOF, wctrans_l("toupper", l.l), l.l));
247 }
248