Home | History | Annotate | Download | only in tests
      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:
     27   UtfLocale() : l(newlocale(LC_ALL, "C.UTF-8", 0)) {}
     28   ~UtfLocale() { freelocale(l); }
     29   locale_t l;
     30 };
     31 
     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 
     55 TEST(wctype, iswalnum) {
     56   TestIsWideFn(iswalnum, iswalnum_l, L"1aA", L"! \b");
     57 }
     58 
     59 TEST(wctype, iswalpha) {
     60   TestIsWideFn(iswalpha, iswalpha_l, L"aA", L"1! \b");
     61 }
     62 
     63 TEST(wctype, iswblank) {
     64   TestIsWideFn(iswblank, iswblank_l, L" \t", L"1aA!\b");
     65 }
     66 
     67 TEST(wctype, iswcntrl) {
     68   TestIsWideFn(iswcntrl, iswcntrl_l, L"\b\u009f", L"1aA! ");
     69 }
     70 
     71 TEST(wctype, iswdigit) {
     72   TestIsWideFn(iswdigit, iswdigit_l, L"1", L"aA! \b");
     73 }
     74 
     75 TEST(wctype, iswgraph) {
     76   TestIsWideFn(iswgraph, iswgraph_l, L"1aA!", L" \b");
     77 }
     78 
     79 TEST(wctype, iswlower) {
     80   TestIsWideFn(iswlower, iswlower_l, L"a", L"1A! \b");
     81 }
     82 
     83 TEST(wctype, iswprint) {
     84   TestIsWideFn(iswprint, iswprint_l, L"1aA! ", L"\b");
     85 }
     86 
     87 TEST(wctype, iswpunct) {
     88   TestIsWideFn(iswpunct, iswpunct_l, L"!", L"1aA \b");
     89 }
     90 
     91 TEST(wctype, iswspace) {
     92   TestIsWideFn(iswspace, iswspace_l, L" \f\t", L"1aA!\b");
     93 }
     94 
     95 TEST(wctype, iswupper) {
     96   TestIsWideFn(iswupper, iswupper_l, L"A", L"1a! \b");
     97 }
     98 
     99 TEST(wctype, iswxdigit) {
    100   TestIsWideFn(iswxdigit, iswxdigit_l, L"01aA", L"xg! \b");
    101 }
    102 
    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_LOG_(INFO) << "skipping unicode towlower tests";
    115   }
    116 }
    117 
    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_LOG_(INFO) << "skipping unicode towlower_l tests";
    131   }
    132 }
    133 
    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_LOG_(INFO) << "skipping unicode towupper tests";
    146   }
    147 }
    148 
    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_LOG_(INFO) << "skipping unicode towupper_l tests";
    162   }
    163 }
    164 
    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 
    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 
    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 
    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 
    217 TEST(wctype, towctrans) {
    218   EXPECT_TRUE(wctrans("tolower") != 0);
    219   EXPECT_TRUE(wctrans("toupper") != 0);
    220 
    221   EXPECT_TRUE(wctrans("monkeys") == 0);
    222 }
    223 
    224 TEST(wctype, towctrans_l) {
    225   UtfLocale l;
    226   EXPECT_TRUE(wctrans_l("tolower", l.l) != 0);
    227   EXPECT_TRUE(wctrans_l("toupper", l.l) != 0);
    228 
    229   EXPECT_TRUE(wctrans_l("monkeys", l.l) == 0);
    230 }
    231 
    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 
    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