Home | History | Annotate | Download | only in tests
      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 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 
     29 TEST(ctype, isalpha) {
     30   EXPECT_FALSE(isalpha('1'));
     31   EXPECT_TRUE(isalpha('a'));
     32   EXPECT_TRUE(isalpha('A'));
     33   EXPECT_FALSE(isalpha('!'));
     34   EXPECT_FALSE(isalpha(' '));
     35 }
     36 
     37 TEST(ctype, isascii) {
     38   EXPECT_TRUE(isascii('\x7f'));
     39   EXPECT_FALSE(isascii('\x80'));
     40 }
     41 
     42 TEST(ctype, isblank) {
     43   EXPECT_FALSE(isblank('1'));
     44   EXPECT_TRUE(isblank(' '));
     45   EXPECT_TRUE(isblank('\t'));
     46 }
     47 
     48 TEST(ctype, iscntrl) {
     49   EXPECT_FALSE(iscntrl('1'));
     50   EXPECT_TRUE(iscntrl('\b'));
     51 }
     52 
     53 TEST(ctype, isdigit) {
     54   EXPECT_TRUE(isdigit('1'));
     55   EXPECT_FALSE(isdigit('a'));
     56   EXPECT_FALSE(isdigit('x'));
     57 }
     58 
     59 TEST(ctype, isgraph) {
     60   EXPECT_TRUE(isgraph('a'));
     61   EXPECT_TRUE(isgraph('A'));
     62   EXPECT_TRUE(isgraph('1'));
     63   EXPECT_TRUE(isgraph('!'));
     64   EXPECT_FALSE(isgraph(' '));
     65 }
     66 
     67 TEST(ctype, islower) {
     68   EXPECT_TRUE(islower('a'));
     69   EXPECT_FALSE(islower('A'));
     70   EXPECT_FALSE(islower('!'));
     71 }
     72 
     73 TEST(ctype, isprint) {
     74   EXPECT_TRUE(isprint('a'));
     75   EXPECT_TRUE(isprint(' '));
     76   EXPECT_FALSE(isprint('\b'));
     77 }
     78 
     79 TEST(ctype, ispunct) {
     80   EXPECT_TRUE(ispunct('!'));
     81   EXPECT_FALSE(ispunct('a'));
     82   EXPECT_FALSE(ispunct(' '));
     83   EXPECT_FALSE(ispunct('\b'));
     84 }
     85 
     86 TEST(ctype, isspace) {
     87   EXPECT_TRUE(isspace(' '));
     88   EXPECT_TRUE(isspace('\f'));
     89   EXPECT_TRUE(isspace('\n'));
     90   EXPECT_TRUE(isspace('\r'));
     91   EXPECT_TRUE(isspace('\t'));
     92   EXPECT_TRUE(isspace('\v'));
     93   EXPECT_FALSE(isspace('a'));
     94   EXPECT_FALSE(isspace('!'));
     95 }
     96 
     97 TEST(ctype, isupper) {
     98   EXPECT_TRUE(isupper('A'));
     99   EXPECT_FALSE(isupper('a'));
    100   EXPECT_FALSE(isupper('!'));
    101 }
    102 
    103 TEST(ctype, isxdigit) {
    104   EXPECT_TRUE(isxdigit('0'));
    105   EXPECT_FALSE(isxdigit('x'));
    106   EXPECT_TRUE(isxdigit('1'));
    107   EXPECT_TRUE(isxdigit('a'));
    108   EXPECT_TRUE(isxdigit('A'));
    109   EXPECT_FALSE(isxdigit('g'));
    110   EXPECT_FALSE(isxdigit(' '));
    111 }
    112 
    113 TEST(ctype, toascii) {
    114   EXPECT_EQ('a', toascii('a'));
    115   EXPECT_EQ('a', toascii(0x80 | 'a'));
    116 }
    117 
    118 TEST(ctype, tolower) {
    119   EXPECT_EQ('!', tolower('!'));
    120   EXPECT_EQ('a', tolower('a'));
    121   EXPECT_EQ('a', tolower('A'));
    122 }
    123 
    124 TEST(ctype, _tolower) {
    125   // _tolower may mangle characters for which isupper is false.
    126   EXPECT_EQ('a', _tolower('A'));
    127 }
    128 
    129 TEST(ctype, toupper) {
    130   EXPECT_EQ('!', toupper('!'));
    131   EXPECT_EQ('A', toupper('a'));
    132   EXPECT_EQ('A', toupper('A'));
    133 }
    134 
    135 TEST(ctype, _toupper) {
    136   // _toupper may mangle characters for which islower is false.
    137   EXPECT_EQ('A', _toupper('a'));
    138 }
    139