00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015 #include "absl/strings/ascii.h"
00016
00017 #include <cctype>
00018 #include <clocale>
00019 #include <cstring>
00020 #include <string>
00021
00022 #include "gtest/gtest.h"
00023 #include "absl/base/macros.h"
00024 #include "absl/base/port.h"
00025
00026 namespace {
00027
00028 TEST(AsciiIsFoo, All) {
00029 for (int i = 0; i < 256; i++) {
00030 if ((i >= 'a' && i <= 'z') || (i >= 'A' && i <= 'Z'))
00031 EXPECT_TRUE(absl::ascii_isalpha(i)) << ": failed on " << i;
00032 else
00033 EXPECT_TRUE(!absl::ascii_isalpha(i)) << ": failed on " << i;
00034 }
00035 for (int i = 0; i < 256; i++) {
00036 if ((i >= '0' && i <= '9'))
00037 EXPECT_TRUE(absl::ascii_isdigit(i)) << ": failed on " << i;
00038 else
00039 EXPECT_TRUE(!absl::ascii_isdigit(i)) << ": failed on " << i;
00040 }
00041 for (int i = 0; i < 256; i++) {
00042 if (absl::ascii_isalpha(i) || absl::ascii_isdigit(i))
00043 EXPECT_TRUE(absl::ascii_isalnum(i)) << ": failed on " << i;
00044 else
00045 EXPECT_TRUE(!absl::ascii_isalnum(i)) << ": failed on " << i;
00046 }
00047 for (int i = 0; i < 256; i++) {
00048 if (i != '\0' && strchr(" \r\n\t\v\f", i))
00049 EXPECT_TRUE(absl::ascii_isspace(i)) << ": failed on " << i;
00050 else
00051 EXPECT_TRUE(!absl::ascii_isspace(i)) << ": failed on " << i;
00052 }
00053 for (int i = 0; i < 256; i++) {
00054 if (i >= 32 && i < 127)
00055 EXPECT_TRUE(absl::ascii_isprint(i)) << ": failed on " << i;
00056 else
00057 EXPECT_TRUE(!absl::ascii_isprint(i)) << ": failed on " << i;
00058 }
00059 for (int i = 0; i < 256; i++) {
00060 if (absl::ascii_isprint(i) && !absl::ascii_isspace(i) &&
00061 !absl::ascii_isalnum(i))
00062 EXPECT_TRUE(absl::ascii_ispunct(i)) << ": failed on " << i;
00063 else
00064 EXPECT_TRUE(!absl::ascii_ispunct(i)) << ": failed on " << i;
00065 }
00066 for (int i = 0; i < 256; i++) {
00067 if (i == ' ' || i == '\t')
00068 EXPECT_TRUE(absl::ascii_isblank(i)) << ": failed on " << i;
00069 else
00070 EXPECT_TRUE(!absl::ascii_isblank(i)) << ": failed on " << i;
00071 }
00072 for (int i = 0; i < 256; i++) {
00073 if (i < 32 || i == 127)
00074 EXPECT_TRUE(absl::ascii_iscntrl(i)) << ": failed on " << i;
00075 else
00076 EXPECT_TRUE(!absl::ascii_iscntrl(i)) << ": failed on " << i;
00077 }
00078 for (int i = 0; i < 256; i++) {
00079 if (absl::ascii_isdigit(i) || (i >= 'A' && i <= 'F') ||
00080 (i >= 'a' && i <= 'f'))
00081 EXPECT_TRUE(absl::ascii_isxdigit(i)) << ": failed on " << i;
00082 else
00083 EXPECT_TRUE(!absl::ascii_isxdigit(i)) << ": failed on " << i;
00084 }
00085 for (int i = 0; i < 256; i++) {
00086 if (i > 32 && i < 127)
00087 EXPECT_TRUE(absl::ascii_isgraph(i)) << ": failed on " << i;
00088 else
00089 EXPECT_TRUE(!absl::ascii_isgraph(i)) << ": failed on " << i;
00090 }
00091 for (int i = 0; i < 256; i++) {
00092 if (i >= 'A' && i <= 'Z')
00093 EXPECT_TRUE(absl::ascii_isupper(i)) << ": failed on " << i;
00094 else
00095 EXPECT_TRUE(!absl::ascii_isupper(i)) << ": failed on " << i;
00096 }
00097 for (int i = 0; i < 256; i++) {
00098 if (i >= 'a' && i <= 'z')
00099 EXPECT_TRUE(absl::ascii_islower(i)) << ": failed on " << i;
00100 else
00101 EXPECT_TRUE(!absl::ascii_islower(i)) << ": failed on " << i;
00102 }
00103 for (int i = 0; i < 128; i++) {
00104 EXPECT_TRUE(absl::ascii_isascii(i)) << ": failed on " << i;
00105 }
00106 for (int i = 128; i < 256; i++) {
00107 EXPECT_TRUE(!absl::ascii_isascii(i)) << ": failed on " << i;
00108 }
00109
00110
00111
00112 for (int i = 0; i < 256; i++) {
00113 signed char sc = static_cast<signed char>(static_cast<unsigned char>(i));
00114 EXPECT_EQ(absl::ascii_isalpha(i), absl::ascii_isalpha(sc)) << i;
00115 EXPECT_EQ(absl::ascii_isdigit(i), absl::ascii_isdigit(sc)) << i;
00116 EXPECT_EQ(absl::ascii_isalnum(i), absl::ascii_isalnum(sc)) << i;
00117 EXPECT_EQ(absl::ascii_isspace(i), absl::ascii_isspace(sc)) << i;
00118 EXPECT_EQ(absl::ascii_ispunct(i), absl::ascii_ispunct(sc)) << i;
00119 EXPECT_EQ(absl::ascii_isblank(i), absl::ascii_isblank(sc)) << i;
00120 EXPECT_EQ(absl::ascii_iscntrl(i), absl::ascii_iscntrl(sc)) << i;
00121 EXPECT_EQ(absl::ascii_isxdigit(i), absl::ascii_isxdigit(sc)) << i;
00122 EXPECT_EQ(absl::ascii_isprint(i), absl::ascii_isprint(sc)) << i;
00123 EXPECT_EQ(absl::ascii_isgraph(i), absl::ascii_isgraph(sc)) << i;
00124 EXPECT_EQ(absl::ascii_isupper(i), absl::ascii_isupper(sc)) << i;
00125 EXPECT_EQ(absl::ascii_islower(i), absl::ascii_islower(sc)) << i;
00126 EXPECT_EQ(absl::ascii_isascii(i), absl::ascii_isascii(sc)) << i;
00127 }
00128 }
00129
00130
00131
00132 TEST(AsciiIsFoo, SameAsIsFoo) {
00133 #ifndef __ANDROID__
00134
00135 const char* old_locale = setlocale(LC_CTYPE, "C");
00136 ASSERT_TRUE(old_locale != nullptr);
00137 #endif
00138
00139 for (int i = 0; i < 256; i++) {
00140 EXPECT_EQ(isalpha(i) != 0, absl::ascii_isalpha(i)) << i;
00141 EXPECT_EQ(isdigit(i) != 0, absl::ascii_isdigit(i)) << i;
00142 EXPECT_EQ(isalnum(i) != 0, absl::ascii_isalnum(i)) << i;
00143 EXPECT_EQ(isspace(i) != 0, absl::ascii_isspace(i)) << i;
00144 EXPECT_EQ(ispunct(i) != 0, absl::ascii_ispunct(i)) << i;
00145 EXPECT_EQ(isblank(i) != 0, absl::ascii_isblank(i)) << i;
00146 EXPECT_EQ(iscntrl(i) != 0, absl::ascii_iscntrl(i)) << i;
00147 EXPECT_EQ(isxdigit(i) != 0, absl::ascii_isxdigit(i)) << i;
00148 EXPECT_EQ(isprint(i) != 0, absl::ascii_isprint(i)) << i;
00149 EXPECT_EQ(isgraph(i) != 0, absl::ascii_isgraph(i)) << i;
00150 EXPECT_EQ(isupper(i) != 0, absl::ascii_isupper(i)) << i;
00151 EXPECT_EQ(islower(i) != 0, absl::ascii_islower(i)) << i;
00152 EXPECT_EQ(isascii(i) != 0, absl::ascii_isascii(i)) << i;
00153 }
00154
00155 #ifndef __ANDROID__
00156
00157 ASSERT_TRUE(setlocale(LC_CTYPE, old_locale));
00158 #endif
00159 }
00160
00161 TEST(AsciiToFoo, All) {
00162 #ifndef __ANDROID__
00163
00164 const char* old_locale = setlocale(LC_CTYPE, "C");
00165 ASSERT_TRUE(old_locale != nullptr);
00166 #endif
00167
00168 for (int i = 0; i < 256; i++) {
00169 if (absl::ascii_islower(i))
00170 EXPECT_EQ(absl::ascii_toupper(i), 'A' + (i - 'a')) << i;
00171 else
00172 EXPECT_EQ(absl::ascii_toupper(i), static_cast<char>(i)) << i;
00173
00174 if (absl::ascii_isupper(i))
00175 EXPECT_EQ(absl::ascii_tolower(i), 'a' + (i - 'A')) << i;
00176 else
00177 EXPECT_EQ(absl::ascii_tolower(i), static_cast<char>(i)) << i;
00178
00179
00180 EXPECT_EQ(static_cast<char>(tolower(i)), absl::ascii_tolower(i)) << i;
00181 EXPECT_EQ(static_cast<char>(toupper(i)), absl::ascii_toupper(i)) << i;
00182
00183
00184
00185 signed char sc = static_cast<signed char>(static_cast<unsigned char>(i));
00186 EXPECT_EQ(absl::ascii_tolower(i), absl::ascii_tolower(sc)) << i;
00187 EXPECT_EQ(absl::ascii_toupper(i), absl::ascii_toupper(sc)) << i;
00188 }
00189 #ifndef __ANDROID__
00190
00191 ASSERT_TRUE(setlocale(LC_CTYPE, old_locale));
00192 #endif
00193 }
00194
00195 TEST(AsciiStrTo, Lower) {
00196 const char buf[] = "ABCDEF";
00197 const std::string str("GHIJKL");
00198 const std::string str2("MNOPQR");
00199 const absl::string_view sp(str2);
00200
00201 EXPECT_EQ("abcdef", absl::AsciiStrToLower(buf));
00202 EXPECT_EQ("ghijkl", absl::AsciiStrToLower(str));
00203 EXPECT_EQ("mnopqr", absl::AsciiStrToLower(sp));
00204
00205 char mutable_buf[] = "Mutable";
00206 std::transform(mutable_buf, mutable_buf + strlen(mutable_buf),
00207 mutable_buf, absl::ascii_tolower);
00208 EXPECT_STREQ("mutable", mutable_buf);
00209 }
00210
00211 TEST(AsciiStrTo, Upper) {
00212 const char buf[] = "abcdef";
00213 const std::string str("ghijkl");
00214 const std::string str2("mnopqr");
00215 const absl::string_view sp(str2);
00216
00217 EXPECT_EQ("ABCDEF", absl::AsciiStrToUpper(buf));
00218 EXPECT_EQ("GHIJKL", absl::AsciiStrToUpper(str));
00219 EXPECT_EQ("MNOPQR", absl::AsciiStrToUpper(sp));
00220
00221 char mutable_buf[] = "Mutable";
00222 std::transform(mutable_buf, mutable_buf + strlen(mutable_buf),
00223 mutable_buf, absl::ascii_toupper);
00224 EXPECT_STREQ("MUTABLE", mutable_buf);
00225 }
00226
00227 TEST(StripLeadingAsciiWhitespace, FromStringView) {
00228 EXPECT_EQ(absl::string_view{},
00229 absl::StripLeadingAsciiWhitespace(absl::string_view{}));
00230 EXPECT_EQ("foo", absl::StripLeadingAsciiWhitespace({"foo"}));
00231 EXPECT_EQ("foo", absl::StripLeadingAsciiWhitespace({"\t \n\f\r\n\vfoo"}));
00232 EXPECT_EQ("foo foo\n ",
00233 absl::StripLeadingAsciiWhitespace({"\t \n\f\r\n\vfoo foo\n "}));
00234 EXPECT_EQ(absl::string_view{}, absl::StripLeadingAsciiWhitespace(
00235 {"\t \n\f\r\v\n\t \n\f\r\v\n"}));
00236 }
00237
00238 TEST(StripLeadingAsciiWhitespace, InPlace) {
00239 std::string str;
00240
00241 absl::StripLeadingAsciiWhitespace(&str);
00242 EXPECT_EQ("", str);
00243
00244 str = "foo";
00245 absl::StripLeadingAsciiWhitespace(&str);
00246 EXPECT_EQ("foo", str);
00247
00248 str = "\t \n\f\r\n\vfoo";
00249 absl::StripLeadingAsciiWhitespace(&str);
00250 EXPECT_EQ("foo", str);
00251
00252 str = "\t \n\f\r\n\vfoo foo\n ";
00253 absl::StripLeadingAsciiWhitespace(&str);
00254 EXPECT_EQ("foo foo\n ", str);
00255
00256 str = "\t \n\f\r\v\n\t \n\f\r\v\n";
00257 absl::StripLeadingAsciiWhitespace(&str);
00258 EXPECT_EQ(absl::string_view{}, str);
00259 }
00260
00261 TEST(StripTrailingAsciiWhitespace, FromStringView) {
00262 EXPECT_EQ(absl::string_view{},
00263 absl::StripTrailingAsciiWhitespace(absl::string_view{}));
00264 EXPECT_EQ("foo", absl::StripTrailingAsciiWhitespace({"foo"}));
00265 EXPECT_EQ("foo", absl::StripTrailingAsciiWhitespace({"foo\t \n\f\r\n\v"}));
00266 EXPECT_EQ(" \nfoo foo",
00267 absl::StripTrailingAsciiWhitespace({" \nfoo foo\t \n\f\r\n\v"}));
00268 EXPECT_EQ(absl::string_view{}, absl::StripTrailingAsciiWhitespace(
00269 {"\t \n\f\r\v\n\t \n\f\r\v\n"}));
00270 }
00271
00272 TEST(StripTrailingAsciiWhitespace, InPlace) {
00273 std::string str;
00274
00275 absl::StripTrailingAsciiWhitespace(&str);
00276 EXPECT_EQ("", str);
00277
00278 str = "foo";
00279 absl::StripTrailingAsciiWhitespace(&str);
00280 EXPECT_EQ("foo", str);
00281
00282 str = "foo\t \n\f\r\n\v";
00283 absl::StripTrailingAsciiWhitespace(&str);
00284 EXPECT_EQ("foo", str);
00285
00286 str = " \nfoo foo\t \n\f\r\n\v";
00287 absl::StripTrailingAsciiWhitespace(&str);
00288 EXPECT_EQ(" \nfoo foo", str);
00289
00290 str = "\t \n\f\r\v\n\t \n\f\r\v\n";
00291 absl::StripTrailingAsciiWhitespace(&str);
00292 EXPECT_EQ(absl::string_view{}, str);
00293 }
00294
00295 TEST(StripAsciiWhitespace, FromStringView) {
00296 EXPECT_EQ(absl::string_view{},
00297 absl::StripAsciiWhitespace(absl::string_view{}));
00298 EXPECT_EQ("foo", absl::StripAsciiWhitespace({"foo"}));
00299 EXPECT_EQ("foo",
00300 absl::StripAsciiWhitespace({"\t \n\f\r\n\vfoo\t \n\f\r\n\v"}));
00301 EXPECT_EQ("foo foo", absl::StripAsciiWhitespace(
00302 {"\t \n\f\r\n\vfoo foo\t \n\f\r\n\v"}));
00303 EXPECT_EQ(absl::string_view{},
00304 absl::StripAsciiWhitespace({"\t \n\f\r\v\n\t \n\f\r\v\n"}));
00305 }
00306
00307 TEST(StripAsciiWhitespace, InPlace) {
00308 std::string str;
00309
00310 absl::StripAsciiWhitespace(&str);
00311 EXPECT_EQ("", str);
00312
00313 str = "foo";
00314 absl::StripAsciiWhitespace(&str);
00315 EXPECT_EQ("foo", str);
00316
00317 str = "\t \n\f\r\n\vfoo\t \n\f\r\n\v";
00318 absl::StripAsciiWhitespace(&str);
00319 EXPECT_EQ("foo", str);
00320
00321 str = "\t \n\f\r\n\vfoo foo\t \n\f\r\n\v";
00322 absl::StripAsciiWhitespace(&str);
00323 EXPECT_EQ("foo foo", str);
00324
00325 str = "\t \n\f\r\v\n\t \n\f\r\v\n";
00326 absl::StripAsciiWhitespace(&str);
00327 EXPECT_EQ(absl::string_view{}, str);
00328 }
00329
00330 TEST(RemoveExtraAsciiWhitespace, InPlace) {
00331 const char* inputs[] = {"No extra space",
00332 " Leading whitespace",
00333 "Trailing whitespace ",
00334 " Leading and trailing ",
00335 " Whitespace \t in\v middle ",
00336 "'Eeeeep! \n Newlines!\n",
00337 "nospaces",
00338 "",
00339 "\n\t a\t\n\nb \t\n"};
00340
00341 const char* outputs[] = {
00342 "No extra space",
00343 "Leading whitespace",
00344 "Trailing whitespace",
00345 "Leading and trailing",
00346 "Whitespace in middle",
00347 "'Eeeeep! Newlines!",
00348 "nospaces",
00349 "",
00350 "a\nb",
00351 };
00352 const int NUM_TESTS = ABSL_ARRAYSIZE(inputs);
00353
00354 for (int i = 0; i < NUM_TESTS; i++) {
00355 std::string s(inputs[i]);
00356 absl::RemoveExtraAsciiWhitespace(&s);
00357 EXPECT_EQ(outputs[i], s);
00358 }
00359 }
00360
00361 }