abseil-cpp/absl/strings/ascii_test.cc
Go to the documentation of this file.
1 // Copyright 2017 The Abseil Authors.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "absl/strings/ascii.h"
16 
17 #include <cctype>
18 #include <clocale>
19 #include <cstring>
20 #include <string>
21 
22 #include "gtest/gtest.h"
23 #include "absl/base/macros.h"
24 #include "absl/base/port.h"
25 
26 namespace {
27 
28 TEST(AsciiIsFoo, All) {
29  for (int i = 0; i < 256; i++) {
30  if ((i >= 'a' && i <= 'z') || (i >= 'A' && i <= 'Z'))
31  EXPECT_TRUE(absl::ascii_isalpha(i)) << ": failed on " << i;
32  else
33  EXPECT_TRUE(!absl::ascii_isalpha(i)) << ": failed on " << i;
34  }
35  for (int i = 0; i < 256; i++) {
36  if ((i >= '0' && i <= '9'))
37  EXPECT_TRUE(absl::ascii_isdigit(i)) << ": failed on " << i;
38  else
39  EXPECT_TRUE(!absl::ascii_isdigit(i)) << ": failed on " << i;
40  }
41  for (int i = 0; i < 256; i++) {
43  EXPECT_TRUE(absl::ascii_isalnum(i)) << ": failed on " << i;
44  else
45  EXPECT_TRUE(!absl::ascii_isalnum(i)) << ": failed on " << i;
46  }
47  for (int i = 0; i < 256; i++) {
48  if (i != '\0' && strchr(" \r\n\t\v\f", i))
49  EXPECT_TRUE(absl::ascii_isspace(i)) << ": failed on " << i;
50  else
51  EXPECT_TRUE(!absl::ascii_isspace(i)) << ": failed on " << i;
52  }
53  for (int i = 0; i < 256; i++) {
54  if (i >= 32 && i < 127)
55  EXPECT_TRUE(absl::ascii_isprint(i)) << ": failed on " << i;
56  else
57  EXPECT_TRUE(!absl::ascii_isprint(i)) << ": failed on " << i;
58  }
59  for (int i = 0; i < 256; i++) {
62  EXPECT_TRUE(absl::ascii_ispunct(i)) << ": failed on " << i;
63  else
64  EXPECT_TRUE(!absl::ascii_ispunct(i)) << ": failed on " << i;
65  }
66  for (int i = 0; i < 256; i++) {
67  if (i == ' ' || i == '\t')
68  EXPECT_TRUE(absl::ascii_isblank(i)) << ": failed on " << i;
69  else
70  EXPECT_TRUE(!absl::ascii_isblank(i)) << ": failed on " << i;
71  }
72  for (int i = 0; i < 256; i++) {
73  if (i < 32 || i == 127)
74  EXPECT_TRUE(absl::ascii_iscntrl(i)) << ": failed on " << i;
75  else
76  EXPECT_TRUE(!absl::ascii_iscntrl(i)) << ": failed on " << i;
77  }
78  for (int i = 0; i < 256; i++) {
79  if (absl::ascii_isdigit(i) || (i >= 'A' && i <= 'F') ||
80  (i >= 'a' && i <= 'f'))
81  EXPECT_TRUE(absl::ascii_isxdigit(i)) << ": failed on " << i;
82  else
83  EXPECT_TRUE(!absl::ascii_isxdigit(i)) << ": failed on " << i;
84  }
85  for (int i = 0; i < 256; i++) {
86  if (i > 32 && i < 127)
87  EXPECT_TRUE(absl::ascii_isgraph(i)) << ": failed on " << i;
88  else
89  EXPECT_TRUE(!absl::ascii_isgraph(i)) << ": failed on " << i;
90  }
91  for (int i = 0; i < 256; i++) {
92  if (i >= 'A' && i <= 'Z')
93  EXPECT_TRUE(absl::ascii_isupper(i)) << ": failed on " << i;
94  else
95  EXPECT_TRUE(!absl::ascii_isupper(i)) << ": failed on " << i;
96  }
97  for (int i = 0; i < 256; i++) {
98  if (i >= 'a' && i <= 'z')
99  EXPECT_TRUE(absl::ascii_islower(i)) << ": failed on " << i;
100  else
101  EXPECT_TRUE(!absl::ascii_islower(i)) << ": failed on " << i;
102  }
103  for (int i = 0; i < 128; i++) {
104  EXPECT_TRUE(absl::ascii_isascii(i)) << ": failed on " << i;
105  }
106  for (int i = 128; i < 256; i++) {
107  EXPECT_TRUE(!absl::ascii_isascii(i)) << ": failed on " << i;
108  }
109 
110  // The official is* functions don't accept negative signed chars, but
111  // our absl::ascii_is* functions do.
112  for (int i = 0; i < 256; i++) {
113  signed char sc = static_cast<signed char>(static_cast<unsigned char>(i));
127  }
128 }
129 
130 // Checks that absl::ascii_isfoo returns the same value as isfoo in the C
131 // locale.
132 TEST(AsciiIsFoo, SameAsIsFoo) {
133 #ifndef __ANDROID__
134  // temporarily change locale to C. It should already be C, but just for safety
135  const char* old_locale = setlocale(LC_CTYPE, "C");
136  ASSERT_TRUE(old_locale != nullptr);
137 #endif
138 
139  for (int i = 0; i < 256; i++) {
140  EXPECT_EQ(isalpha(i) != 0, absl::ascii_isalpha(i)) << i;
141  EXPECT_EQ(isdigit(i) != 0, absl::ascii_isdigit(i)) << i;
142  EXPECT_EQ(isalnum(i) != 0, absl::ascii_isalnum(i)) << i;
143  EXPECT_EQ(isspace(i) != 0, absl::ascii_isspace(i)) << i;
144  EXPECT_EQ(ispunct(i) != 0, absl::ascii_ispunct(i)) << i;
145  EXPECT_EQ(isblank(i) != 0, absl::ascii_isblank(i)) << i;
146  EXPECT_EQ(iscntrl(i) != 0, absl::ascii_iscntrl(i)) << i;
149  EXPECT_EQ(isgraph(i) != 0, absl::ascii_isgraph(i)) << i;
150  EXPECT_EQ(isupper(i) != 0, absl::ascii_isupper(i)) << i;
151  EXPECT_EQ(islower(i) != 0, absl::ascii_islower(i)) << i;
152  EXPECT_EQ(isascii(i) != 0, absl::ascii_isascii(i)) << i;
153  }
154 
155 #ifndef __ANDROID__
156  // restore the old locale.
157  ASSERT_TRUE(setlocale(LC_CTYPE, old_locale));
158 #endif
159 }
160 
161 TEST(AsciiToFoo, All) {
162 #ifndef __ANDROID__
163  // temporarily change locale to C. It should already be C, but just for safety
164  const char* old_locale = setlocale(LC_CTYPE, "C");
165  ASSERT_TRUE(old_locale != nullptr);
166 #endif
167 
168  for (int i = 0; i < 256; i++) {
169  if (absl::ascii_islower(i))
170  EXPECT_EQ(absl::ascii_toupper(i), 'A' + (i - 'a')) << i;
171  else
172  EXPECT_EQ(absl::ascii_toupper(i), static_cast<char>(i)) << i;
173 
174  if (absl::ascii_isupper(i))
175  EXPECT_EQ(absl::ascii_tolower(i), 'a' + (i - 'A')) << i;
176  else
177  EXPECT_EQ(absl::ascii_tolower(i), static_cast<char>(i)) << i;
178 
179  // These CHECKs only hold in a C locale.
180  EXPECT_EQ(static_cast<char>(tolower(i)), absl::ascii_tolower(i)) << i;
181  EXPECT_EQ(static_cast<char>(toupper(i)), absl::ascii_toupper(i)) << i;
182 
183  // The official to* functions don't accept negative signed chars, but
184  // our absl::ascii_to* functions do.
185  signed char sc = static_cast<signed char>(static_cast<unsigned char>(i));
188  }
189 #ifndef __ANDROID__
190  // restore the old locale.
191  ASSERT_TRUE(setlocale(LC_CTYPE, old_locale));
192 #endif
193 }
194 
195 TEST(AsciiStrTo, Lower) {
196  const char buf[] = "ABCDEF";
197  const std::string str("GHIJKL");
198  const std::string str2("MNOPQR");
199  const absl::string_view sp(str2);
200  std::string mutable_str("STUVWX");
201 
202  EXPECT_EQ("abcdef", absl::AsciiStrToLower(buf));
203  EXPECT_EQ("ghijkl", absl::AsciiStrToLower(str));
204  EXPECT_EQ("mnopqr", absl::AsciiStrToLower(sp));
205 
206  absl::AsciiStrToLower(&mutable_str);
207  EXPECT_EQ("stuvwx", mutable_str);
208 
209  char mutable_buf[] = "Mutable";
210  std::transform(mutable_buf, mutable_buf + strlen(mutable_buf),
211  mutable_buf, absl::ascii_tolower);
212  EXPECT_STREQ("mutable", mutable_buf);
213 }
214 
215 TEST(AsciiStrTo, Upper) {
216  const char buf[] = "abcdef";
217  const std::string str("ghijkl");
218  const std::string str2("mnopqr");
219  const absl::string_view sp(str2);
220 
221  EXPECT_EQ("ABCDEF", absl::AsciiStrToUpper(buf));
222  EXPECT_EQ("GHIJKL", absl::AsciiStrToUpper(str));
223  EXPECT_EQ("MNOPQR", absl::AsciiStrToUpper(sp));
224 
225  char mutable_buf[] = "Mutable";
226  std::transform(mutable_buf, mutable_buf + strlen(mutable_buf),
227  mutable_buf, absl::ascii_toupper);
228  EXPECT_STREQ("MUTABLE", mutable_buf);
229 }
230 
231 TEST(StripLeadingAsciiWhitespace, FromStringView) {
235  EXPECT_EQ("foo", absl::StripLeadingAsciiWhitespace({"\t \n\f\r\n\vfoo"}));
236  EXPECT_EQ("foo foo\n ",
237  absl::StripLeadingAsciiWhitespace({"\t \n\f\r\n\vfoo foo\n "}));
239  {"\t \n\f\r\v\n\t \n\f\r\v\n"}));
240 }
241 
244 
246  EXPECT_EQ("", str);
247 
248  str = "foo";
250  EXPECT_EQ("foo", str);
251 
252  str = "\t \n\f\r\n\vfoo";
254  EXPECT_EQ("foo", str);
255 
256  str = "\t \n\f\r\n\vfoo foo\n ";
258  EXPECT_EQ("foo foo\n ", str);
259 
260  str = "\t \n\f\r\v\n\t \n\f\r\v\n";
263 }
264 
265 TEST(StripTrailingAsciiWhitespace, FromStringView) {
269  EXPECT_EQ("foo", absl::StripTrailingAsciiWhitespace({"foo\t \n\f\r\n\v"}));
270  EXPECT_EQ(" \nfoo foo",
271  absl::StripTrailingAsciiWhitespace({" \nfoo foo\t \n\f\r\n\v"}));
273  {"\t \n\f\r\v\n\t \n\f\r\v\n"}));
274 }
275 
278 
280  EXPECT_EQ("", str);
281 
282  str = "foo";
284  EXPECT_EQ("foo", str);
285 
286  str = "foo\t \n\f\r\n\v";
288  EXPECT_EQ("foo", str);
289 
290  str = " \nfoo foo\t \n\f\r\n\v";
292  EXPECT_EQ(" \nfoo foo", str);
293 
294  str = "\t \n\f\r\v\n\t \n\f\r\v\n";
297 }
298 
299 TEST(StripAsciiWhitespace, FromStringView) {
302  EXPECT_EQ("foo", absl::StripAsciiWhitespace({"foo"}));
303  EXPECT_EQ("foo",
304  absl::StripAsciiWhitespace({"\t \n\f\r\n\vfoo\t \n\f\r\n\v"}));
306  {"\t \n\f\r\n\vfoo foo\t \n\f\r\n\v"}));
308  absl::StripAsciiWhitespace({"\t \n\f\r\v\n\t \n\f\r\v\n"}));
309 }
310 
311 TEST(StripAsciiWhitespace, InPlace) {
313 
315  EXPECT_EQ("", str);
316 
317  str = "foo";
319  EXPECT_EQ("foo", str);
320 
321  str = "\t \n\f\r\n\vfoo\t \n\f\r\n\v";
323  EXPECT_EQ("foo", str);
324 
325  str = "\t \n\f\r\n\vfoo foo\t \n\f\r\n\v";
327  EXPECT_EQ("foo foo", str);
328 
329  str = "\t \n\f\r\v\n\t \n\f\r\v\n";
332 }
333 
335  const char* inputs[] = {"No extra space",
336  " Leading whitespace",
337  "Trailing whitespace ",
338  " Leading and trailing ",
339  " Whitespace \t in\v middle ",
340  "'Eeeeep! \n Newlines!\n",
341  "nospaces",
342  "",
343  "\n\t a\t\n\nb \t\n"};
344 
345  const char* outputs[] = {
346  "No extra space",
347  "Leading whitespace",
348  "Trailing whitespace",
349  "Leading and trailing",
350  "Whitespace in middle",
351  "'Eeeeep! Newlines!",
352  "nospaces",
353  "",
354  "a\nb",
355  };
356  const int NUM_TESTS = ABSL_ARRAYSIZE(inputs);
357 
358  for (int i = 0; i < NUM_TESTS; i++) {
359  std::string s(inputs[i]);
361  EXPECT_EQ(outputs[i], s);
362  }
363 }
364 
365 } // namespace
absl::StripAsciiWhitespace
ABSL_MUST_USE_RESULT absl::string_view StripAsciiWhitespace(absl::string_view str)
Definition: abseil-cpp/absl/strings/ascii.h:225
xds_interop_client.str
str
Definition: xds_interop_client.py:487
absl::ascii_isblank
bool ascii_isblank(unsigned char c)
Definition: abseil-cpp/absl/strings/ascii.h:109
absl::ascii_tolower
char ascii_tolower(unsigned char c)
Definition: abseil-cpp/absl/strings/ascii.h:163
absl::RemoveExtraAsciiWhitespace
void RemoveExtraAsciiWhitespace(std::string *str)
Definition: abseil-cpp/absl/strings/ascii.cc:170
buf
voidpf void * buf
Definition: bloaty/third_party/zlib/contrib/minizip/ioapi.h:136
absl::string_view
Definition: abseil-cpp/absl/strings/string_view.h:167
testing::internal::string
::std::string string
Definition: bloaty/third_party/protobuf/third_party/googletest/googletest/include/gtest/internal/gtest-port.h:881
absl::StripTrailingAsciiWhitespace
ABSL_MUST_USE_RESULT absl::string_view StripTrailingAsciiWhitespace(absl::string_view str)
Definition: abseil-cpp/absl/strings/ascii.h:211
absl::ascii_iscntrl
bool ascii_iscntrl(unsigned char c)
Definition: abseil-cpp/absl/strings/ascii.h:116
absl::ascii_isgraph
bool ascii_isgraph(unsigned char c)
Definition: abseil-cpp/absl/strings/ascii.h:142
absl::FormatConversionChar::s
@ s
ABSL_ARRAYSIZE
#define ABSL_ARRAYSIZE(array)
Definition: abseil-cpp/absl/base/macros.h:44
absl::AsciiStrToLower
void AsciiStrToLower(std::string *s)
Definition: abseil-cpp/absl/strings/ascii.cc:158
absl::ascii_isspace
bool ascii_isspace(unsigned char c)
Definition: abseil-cpp/absl/strings/ascii.h:95
EXPECT_EQ
#define EXPECT_EQ(a, b)
Definition: iomgr/time_averaged_stats_test.cc:27
absl::ascii_isalnum
bool ascii_isalnum(unsigned char c)
Definition: abseil-cpp/absl/strings/ascii.h:87
absl::ascii_isascii
bool ascii_isascii(unsigned char c)
Definition: abseil-cpp/absl/strings/ascii.h:157
absl::AsciiStrToUpper
void AsciiStrToUpper(std::string *s)
Definition: abseil-cpp/absl/strings/ascii.cc:164
TEST
#define TEST(name, init_size,...)
Definition: arena_test.cc:75
google::protobuf::isprint
bool isprint(char c)
Definition: bloaty/third_party/protobuf/src/google/protobuf/stubs/strutil.cc:79
absl::ascii_isprint
bool ascii_isprint(unsigned char c)
Definition: abseil-cpp/absl/strings/ascii.h:137
absl::StripLeadingAsciiWhitespace
ABSL_MUST_USE_RESULT absl::string_view StripLeadingAsciiWhitespace(absl::string_view str)
Definition: abseil-cpp/absl/strings/ascii.h:197
absl::ascii_islower
bool ascii_islower(unsigned char c)
Definition: abseil-cpp/absl/strings/ascii.h:152
absl::ascii_isupper
bool ascii_isupper(unsigned char c)
Definition: abseil-cpp/absl/strings/ascii.h:147
absl::ascii_isalpha
bool ascii_isalpha(unsigned char c)
Definition: abseil-cpp/absl/strings/ascii.h:80
google::protobuf::isxdigit
bool isxdigit(char c)
Definition: bloaty/third_party/protobuf/src/google/protobuf/stubs/strutil.cc:73
EXPECT_STREQ
#define EXPECT_STREQ(s1, s2)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:2095
absl::ascii_toupper
char ascii_toupper(unsigned char c)
Definition: abseil-cpp/absl/strings/ascii.h:181
absl::ascii_ispunct
bool ascii_ispunct(unsigned char c)
Definition: abseil-cpp/absl/strings/ascii.h:102
ASSERT_TRUE
#define ASSERT_TRUE(condition)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:1973
EXPECT_TRUE
#define EXPECT_TRUE(condition)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:1967
absl::ascii_isxdigit
bool ascii_isxdigit(unsigned char c)
Definition: abseil-cpp/absl/strings/ascii.h:124
absl::ascii_isdigit
bool ascii_isdigit(unsigned char c)
Definition: abseil-cpp/absl/strings/ascii.h:132
i
uint64_t i
Definition: abseil-cpp/absl/container/btree_benchmark.cc:230


grpc
Author(s):
autogenerated on Thu Mar 13 2025 02:58:34