abseil-cpp/absl/strings/str_cat_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 // Unit tests for all str_cat.h functions
16 
17 #include "absl/strings/str_cat.h"
18 
19 #include <cstdint>
20 #include <string>
21 #include <vector>
22 
23 #include "gtest/gtest.h"
24 #include "absl/strings/substitute.h"
25 
26 #ifdef __ANDROID__
27 // Android assert messages only go to system log, so death tests cannot inspect
28 // the message for matching.
29 #define ABSL_EXPECT_DEBUG_DEATH(statement, regex) \
30  EXPECT_DEBUG_DEATH(statement, ".*")
31 #else
32 #define ABSL_EXPECT_DEBUG_DEATH(statement, regex) \
33  EXPECT_DEBUG_DEATH(statement, regex)
34 #endif
35 
36 namespace {
37 
38 // Test absl::StrCat of ints and longs of various sizes and signdedness.
39 TEST(StrCat, Ints) {
40  const short s = -1; // NOLINT(runtime/int)
41  const uint16_t us = 2;
42  const int i = -3;
43  const unsigned int ui = 4;
44  const long l = -5; // NOLINT(runtime/int)
45  const unsigned long ul = 6; // NOLINT(runtime/int)
46  const long long ll = -7; // NOLINT(runtime/int)
47  const unsigned long long ull = 8; // NOLINT(runtime/int)
48  const ptrdiff_t ptrdiff = -9;
49  const size_t size = 10;
50  const intptr_t intptr = -12;
51  const uintptr_t uintptr = 13;
52  std::string answer;
53  answer = absl::StrCat(s, us);
54  EXPECT_EQ(answer, "-12");
55  answer = absl::StrCat(i, ui);
56  EXPECT_EQ(answer, "-34");
57  answer = absl::StrCat(l, ul);
58  EXPECT_EQ(answer, "-56");
59  answer = absl::StrCat(ll, ull);
60  EXPECT_EQ(answer, "-78");
61  answer = absl::StrCat(ptrdiff, size);
62  EXPECT_EQ(answer, "-910");
63  answer = absl::StrCat(ptrdiff, intptr);
64  EXPECT_EQ(answer, "-9-12");
65  answer = absl::StrCat(uintptr, 0);
66  EXPECT_EQ(answer, "130");
67 }
68 
69 TEST(StrCat, Enums) {
70  enum SmallNumbers { One = 1, Ten = 10 } e = Ten;
71  EXPECT_EQ("10", absl::StrCat(e));
72  EXPECT_EQ("-5", absl::StrCat(SmallNumbers(-5)));
73 
74  enum class Option { Boxers = 1, Briefs = -1 };
75 
76  EXPECT_EQ("-1", absl::StrCat(Option::Briefs));
77 
78  enum class Airplane : uint64_t {
79  Airbus = 1,
80  Boeing = 1000,
81  Canary = 10000000000 // too big for "int"
82  };
83 
84  EXPECT_EQ("10000000000", absl::StrCat(Airplane::Canary));
85 
86  enum class TwoGig : int32_t {
87  TwoToTheZero = 1,
88  TwoToTheSixteenth = 1 << 16,
89  TwoToTheThirtyFirst = INT32_MIN
90  };
91  EXPECT_EQ("65536", absl::StrCat(TwoGig::TwoToTheSixteenth));
92  EXPECT_EQ("-2147483648", absl::StrCat(TwoGig::TwoToTheThirtyFirst));
93  EXPECT_EQ("-1", absl::StrCat(static_cast<TwoGig>(-1)));
94 
95  enum class FourGig : uint32_t {
96  TwoToTheZero = 1,
97  TwoToTheSixteenth = 1 << 16,
98  TwoToTheThirtyFirst = 1U << 31 // too big for "int"
99  };
100  EXPECT_EQ("65536", absl::StrCat(FourGig::TwoToTheSixteenth));
101  EXPECT_EQ("2147483648", absl::StrCat(FourGig::TwoToTheThirtyFirst));
102  EXPECT_EQ("4294967295", absl::StrCat(static_cast<FourGig>(-1)));
103 
104  EXPECT_EQ("10000000000", absl::StrCat(Airplane::Canary));
105 }
106 
107 TEST(StrCat, Basics) {
109 
110  std::string strs[] = {"Hello", "Cruel", "World"};
111 
112  std::string stdstrs[] = {
113  "std::Hello",
114  "std::Cruel",
115  "std::World"
116  };
117 
118  absl::string_view pieces[] = {"Hello", "Cruel", "World"};
119 
120  const char* c_strs[] = {
121  "Hello",
122  "Cruel",
123  "World"
124  };
125 
126  int32_t i32s[] = {'H', 'C', 'W'};
127  uint64_t ui64s[] = {12345678910LL, 10987654321LL};
128 
129  EXPECT_EQ(absl::StrCat(), "");
130 
131  result = absl::StrCat(false, true, 2, 3);
132  EXPECT_EQ(result, "0123");
133 
134  result = absl::StrCat(-1);
135  EXPECT_EQ(result, "-1");
136 
138  EXPECT_EQ(result, "0.5");
139 
140  result = absl::StrCat(strs[1], pieces[2]);
141  EXPECT_EQ(result, "CruelWorld");
142 
143  result = absl::StrCat(stdstrs[1], " ", stdstrs[2]);
144  EXPECT_EQ(result, "std::Cruel std::World");
145 
146  result = absl::StrCat(strs[0], ", ", pieces[2]);
147  EXPECT_EQ(result, "Hello, World");
148 
149  result = absl::StrCat(strs[0], ", ", strs[1], " ", strs[2], "!");
150  EXPECT_EQ(result, "Hello, Cruel World!");
151 
152  result = absl::StrCat(pieces[0], ", ", pieces[1], " ", pieces[2]);
153  EXPECT_EQ(result, "Hello, Cruel World");
154 
155  result = absl::StrCat(c_strs[0], ", ", c_strs[1], " ", c_strs[2]);
156  EXPECT_EQ(result, "Hello, Cruel World");
157 
158  result = absl::StrCat("ASCII ", i32s[0], ", ", i32s[1], " ", i32s[2], "!");
159  EXPECT_EQ(result, "ASCII 72, 67 87!");
160 
161  result = absl::StrCat(ui64s[0], ", ", ui64s[1], "!");
162  EXPECT_EQ(result, "12345678910, 10987654321!");
163 
164  std::string one =
165  "1"; // Actually, it's the size of this string that we want; a
166  // 64-bit build distinguishes between size_t and uint64_t,
167  // even though they're both unsigned 64-bit values.
168  result = absl::StrCat("And a ", one.size(), " and a ",
169  &result[2] - &result[0], " and a ", one, " 2 3 4", "!");
170  EXPECT_EQ(result, "And a 1 and a 2 and a 1 2 3 4!");
171 
172  // result = absl::StrCat("Single chars won't compile", '!');
173  // result = absl::StrCat("Neither will nullptrs", nullptr);
174  result =
175  absl::StrCat("To output a char by ASCII/numeric value, use +: ", '!' + 0);
176  EXPECT_EQ(result, "To output a char by ASCII/numeric value, use +: 33");
177 
178  float f = 100000.5;
179  result = absl::StrCat("A hundred K and a half is ", absl::SixDigits(f));
180  EXPECT_EQ(result, "A hundred K and a half is 100000");
181 
182  f = 100001.5;
183  result =
184  absl::StrCat("A hundred K and one and a half is ", absl::SixDigits(f));
185  EXPECT_EQ(result, "A hundred K and one and a half is 100002");
186 
187  double d = 100000.5;
188  d *= d;
189  result =
190  absl::StrCat("A hundred K and a half squared is ", absl::SixDigits(d));
191  EXPECT_EQ(result, "A hundred K and a half squared is 1.00001e+10");
192 
193  result = absl::StrCat(1, 2, 333, 4444, 55555, 666666, 7777777, 88888888,
194  999999999);
195  EXPECT_EQ(result, "12333444455555666666777777788888888999999999");
196 }
197 
198 TEST(StrCat, CornerCases) {
200 
201  result = absl::StrCat(""); // NOLINT
202  EXPECT_EQ(result, "");
203  result = absl::StrCat("", "");
204  EXPECT_EQ(result, "");
205  result = absl::StrCat("", "", "");
206  EXPECT_EQ(result, "");
207  result = absl::StrCat("", "", "", "");
208  EXPECT_EQ(result, "");
209  result = absl::StrCat("", "", "", "", "");
210  EXPECT_EQ(result, "");
211 }
212 
213 TEST(StrCat, NullConstCharPtr) {
214  const char* null = nullptr;
215  EXPECT_EQ(absl::StrCat("mon", null, "key"), "monkey");
216 }
217 
218 // A minimal allocator that uses malloc().
219 template <typename T>
220 struct Mallocator {
221  typedef T value_type;
222  typedef size_t size_type;
223  typedef ptrdiff_t difference_type;
224  typedef T* pointer;
225  typedef const T* const_pointer;
226  typedef T& reference;
227  typedef const T& const_reference;
228 
229  size_type max_size() const {
230  return size_t(std::numeric_limits<size_type>::max()) / sizeof(value_type);
231  }
232  template <typename U>
233  struct rebind {
234  typedef Mallocator<U> other;
235  };
236  Mallocator() = default;
237  template <class U>
238  Mallocator(const Mallocator<U>&) {} // NOLINT(runtime/explicit)
239 
240  T* allocate(size_t n) { return static_cast<T*>(std::malloc(n * sizeof(T))); }
241  void deallocate(T* p, size_t) { std::free(p); }
242 };
243 template <typename T, typename U>
244 bool operator==(const Mallocator<T>&, const Mallocator<U>&) {
245  return true;
246 }
247 template <typename T, typename U>
248 bool operator!=(const Mallocator<T>&, const Mallocator<U>&) {
249  return false;
250 }
251 
252 TEST(StrCat, CustomAllocator) {
253  using mstring =
254  std::basic_string<char, std::char_traits<char>, Mallocator<char>>;
255  const mstring str1("PARACHUTE OFF A BLIMP INTO MOSCONE!!");
256 
257  const mstring str2("Read this book about coffee tables");
258 
259  std::string result = absl::StrCat(str1, str2);
261  "PARACHUTE OFF A BLIMP INTO MOSCONE!!"
262  "Read this book about coffee tables");
263 }
264 
265 TEST(StrCat, MaxArgs) {
267  // Test 10 up to 26 arguments, the old maximum
268  result = absl::StrCat(1, 2, 3, 4, 5, 6, 7, 8, 9, "a");
269  EXPECT_EQ(result, "123456789a");
270  result = absl::StrCat(1, 2, 3, 4, 5, 6, 7, 8, 9, "a", "b");
271  EXPECT_EQ(result, "123456789ab");
272  result = absl::StrCat(1, 2, 3, 4, 5, 6, 7, 8, 9, "a", "b", "c");
273  EXPECT_EQ(result, "123456789abc");
274  result = absl::StrCat(1, 2, 3, 4, 5, 6, 7, 8, 9, "a", "b", "c", "d");
275  EXPECT_EQ(result, "123456789abcd");
276  result = absl::StrCat(1, 2, 3, 4, 5, 6, 7, 8, 9, "a", "b", "c", "d", "e");
277  EXPECT_EQ(result, "123456789abcde");
278  result =
279  absl::StrCat(1, 2, 3, 4, 5, 6, 7, 8, 9, "a", "b", "c", "d", "e", "f");
280  EXPECT_EQ(result, "123456789abcdef");
281  result = absl::StrCat(1, 2, 3, 4, 5, 6, 7, 8, 9, "a", "b", "c", "d", "e", "f",
282  "g");
283  EXPECT_EQ(result, "123456789abcdefg");
284  result = absl::StrCat(1, 2, 3, 4, 5, 6, 7, 8, 9, "a", "b", "c", "d", "e", "f",
285  "g", "h");
286  EXPECT_EQ(result, "123456789abcdefgh");
287  result = absl::StrCat(1, 2, 3, 4, 5, 6, 7, 8, 9, "a", "b", "c", "d", "e", "f",
288  "g", "h", "i");
289  EXPECT_EQ(result, "123456789abcdefghi");
290  result = absl::StrCat(1, 2, 3, 4, 5, 6, 7, 8, 9, "a", "b", "c", "d", "e", "f",
291  "g", "h", "i", "j");
292  EXPECT_EQ(result, "123456789abcdefghij");
293  result = absl::StrCat(1, 2, 3, 4, 5, 6, 7, 8, 9, "a", "b", "c", "d", "e", "f",
294  "g", "h", "i", "j", "k");
295  EXPECT_EQ(result, "123456789abcdefghijk");
296  result = absl::StrCat(1, 2, 3, 4, 5, 6, 7, 8, 9, "a", "b", "c", "d", "e", "f",
297  "g", "h", "i", "j", "k", "l");
298  EXPECT_EQ(result, "123456789abcdefghijkl");
299  result = absl::StrCat(1, 2, 3, 4, 5, 6, 7, 8, 9, "a", "b", "c", "d", "e", "f",
300  "g", "h", "i", "j", "k", "l", "m");
301  EXPECT_EQ(result, "123456789abcdefghijklm");
302  result = absl::StrCat(1, 2, 3, 4, 5, 6, 7, 8, 9, "a", "b", "c", "d", "e", "f",
303  "g", "h", "i", "j", "k", "l", "m", "n");
304  EXPECT_EQ(result, "123456789abcdefghijklmn");
305  result = absl::StrCat(1, 2, 3, 4, 5, 6, 7, 8, 9, "a", "b", "c", "d", "e", "f",
306  "g", "h", "i", "j", "k", "l", "m", "n", "o");
307  EXPECT_EQ(result, "123456789abcdefghijklmno");
308  result = absl::StrCat(1, 2, 3, 4, 5, 6, 7, 8, 9, "a", "b", "c", "d", "e", "f",
309  "g", "h", "i", "j", "k", "l", "m", "n", "o", "p");
310  EXPECT_EQ(result, "123456789abcdefghijklmnop");
311  result = absl::StrCat(1, 2, 3, 4, 5, 6, 7, 8, 9, "a", "b", "c", "d", "e", "f",
312  "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q");
313  EXPECT_EQ(result, "123456789abcdefghijklmnopq");
314  // No limit thanks to C++11's variadic templates
316  1, 2, 3, 4, 5, 6, 7, 8, 9, 10, "a", "b", "c", "d", "e", "f", "g", "h",
317  "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w",
318  "x", "y", "z", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L",
319  "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z");
321  "12345678910abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ");
322 }
323 
324 TEST(StrAppend, Basics) {
325  std::string result = "existing text";
326 
327  std::string strs[] = {"Hello", "Cruel", "World"};
328 
329  std::string stdstrs[] = {
330  "std::Hello",
331  "std::Cruel",
332  "std::World"
333  };
334 
335  absl::string_view pieces[] = {"Hello", "Cruel", "World"};
336 
337  const char* c_strs[] = {
338  "Hello",
339  "Cruel",
340  "World"
341  };
342 
343  int32_t i32s[] = {'H', 'C', 'W'};
344  uint64_t ui64s[] = {12345678910LL, 10987654321LL};
345 
346  std::string::size_type old_size = result.size();
348  EXPECT_EQ(result.size(), old_size);
349 
350  old_size = result.size();
351  absl::StrAppend(&result, strs[0]);
352  EXPECT_EQ(result.substr(old_size), "Hello");
353 
354  old_size = result.size();
355  absl::StrAppend(&result, strs[1], pieces[2]);
356  EXPECT_EQ(result.substr(old_size), "CruelWorld");
357 
358  old_size = result.size();
359  absl::StrAppend(&result, stdstrs[0], ", ", pieces[2]);
360  EXPECT_EQ(result.substr(old_size), "std::Hello, World");
361 
362  old_size = result.size();
363  absl::StrAppend(&result, strs[0], ", ", stdstrs[1], " ", strs[2], "!");
364  EXPECT_EQ(result.substr(old_size), "Hello, std::Cruel World!");
365 
366  old_size = result.size();
367  absl::StrAppend(&result, pieces[0], ", ", pieces[1], " ", pieces[2]);
368  EXPECT_EQ(result.substr(old_size), "Hello, Cruel World");
369 
370  old_size = result.size();
371  absl::StrAppend(&result, c_strs[0], ", ", c_strs[1], " ", c_strs[2]);
372  EXPECT_EQ(result.substr(old_size), "Hello, Cruel World");
373 
374  old_size = result.size();
375  absl::StrAppend(&result, "ASCII ", i32s[0], ", ", i32s[1], " ", i32s[2], "!");
376  EXPECT_EQ(result.substr(old_size), "ASCII 72, 67 87!");
377 
378  old_size = result.size();
379  absl::StrAppend(&result, ui64s[0], ", ", ui64s[1], "!");
380  EXPECT_EQ(result.substr(old_size), "12345678910, 10987654321!");
381 
382  std::string one =
383  "1"; // Actually, it's the size of this string that we want; a
384  // 64-bit build distinguishes between size_t and uint64_t,
385  // even though they're both unsigned 64-bit values.
386  old_size = result.size();
387  absl::StrAppend(&result, "And a ", one.size(), " and a ",
388  &result[2] - &result[0], " and a ", one, " 2 3 4", "!");
389  EXPECT_EQ(result.substr(old_size), "And a 1 and a 2 and a 1 2 3 4!");
390 
391  // result = absl::StrCat("Single chars won't compile", '!');
392  // result = absl::StrCat("Neither will nullptrs", nullptr);
393  old_size = result.size();
395  "To output a char by ASCII/numeric value, use +: ", '!' + 0);
396  EXPECT_EQ(result.substr(old_size),
397  "To output a char by ASCII/numeric value, use +: 33");
398 
399  // Test 9 arguments, the old maximum
400  old_size = result.size();
401  absl::StrAppend(&result, 1, 22, 333, 4444, 55555, 666666, 7777777, 88888888,
402  9);
403  EXPECT_EQ(result.substr(old_size), "1223334444555556666667777777888888889");
404 
405  // No limit thanks to C++11's variadic templates
406  old_size = result.size();
408  &result, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, //
409  "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", //
410  "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", //
411  "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", //
412  "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", //
413  "No limit thanks to C++11's variadic templates");
414  EXPECT_EQ(result.substr(old_size),
415  "12345678910abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
416  "No limit thanks to C++11's variadic templates");
417 }
418 
419 TEST(StrCat, VectorBoolReferenceTypes) {
420  std::vector<bool> v;
421  v.push_back(true);
422  v.push_back(false);
423  std::vector<bool> const& cv = v;
424  // Test that vector<bool>::reference and vector<bool>::const_reference
425  // are handled as if the were really bool types and not the proxy types
426  // they really are.
427  std::string result = absl::StrCat(v[0], v[1], cv[0], cv[1]); // NOLINT
428  EXPECT_EQ(result, "1010");
429 }
430 
431 // Passing nullptr to memcpy is undefined behavior and this test
432 // provides coverage of codepaths that handle empty strings with nullptrs.
433 TEST(StrCat, AvoidsMemcpyWithNullptr) {
435 
436  // Cover CatPieces code.
437  EXPECT_EQ(absl::StrCat(1, 2, 3, 4, 5, absl::string_view{}), "12345");
438 
439  // Cover AppendPieces.
441  absl::StrAppend(&result, 1, 2, 3, 4, 5, absl::string_view{});
442  EXPECT_EQ(result, "12345");
443 }
444 
445 #ifdef GTEST_HAS_DEATH_TEST
446 TEST(StrAppend, Death) {
447  std::string s = "self";
448  // on linux it's "assertion", on mac it's "Assertion",
449  // on chromiumos it's "Assertion ... failed".
450  ABSL_EXPECT_DEBUG_DEATH(absl::StrAppend(&s, s.c_str() + 1),
451  "ssertion.*failed");
452  ABSL_EXPECT_DEBUG_DEATH(absl::StrAppend(&s, s), "ssertion.*failed");
453 }
454 #endif // GTEST_HAS_DEATH_TEST
455 
456 TEST(StrAppend, CornerCases) {
458  absl::StrAppend(&result, "");
459  EXPECT_EQ(result, "");
460  absl::StrAppend(&result, "", "");
461  EXPECT_EQ(result, "");
462  absl::StrAppend(&result, "", "", "");
463  EXPECT_EQ(result, "");
464  absl::StrAppend(&result, "", "", "", "");
465  EXPECT_EQ(result, "");
466  absl::StrAppend(&result, "", "", "", "", "");
467  EXPECT_EQ(result, "");
468 }
469 
470 TEST(StrAppend, CornerCasesNonEmptyAppend) {
471  for (std::string result : {"hello", "a string too long to fit in the SSO"}) {
472  const std::string expected = result;
473  absl::StrAppend(&result, "");
474  EXPECT_EQ(result, expected);
475  absl::StrAppend(&result, "", "");
476  EXPECT_EQ(result, expected);
477  absl::StrAppend(&result, "", "", "");
478  EXPECT_EQ(result, expected);
479  absl::StrAppend(&result, "", "", "", "");
480  EXPECT_EQ(result, expected);
481  absl::StrAppend(&result, "", "", "", "", "");
482  EXPECT_EQ(result, expected);
483  }
484 }
485 
486 template <typename IntType>
487 void CheckHex(IntType v, const char* nopad_format, const char* zeropad_format,
488  const char* spacepad_format) {
489  char expected[256];
490 
492  snprintf(expected, sizeof(expected), nopad_format, v);
493  EXPECT_EQ(expected, actual) << " decimal value " << v;
494 
495  for (int spec = absl::kZeroPad2; spec <= absl::kZeroPad20; ++spec) {
496  std::string actual =
497  absl::StrCat(absl::Hex(v, static_cast<absl::PadSpec>(spec)));
498  snprintf(expected, sizeof(expected), zeropad_format,
499  spec - absl::kZeroPad2 + 2, v);
500  EXPECT_EQ(expected, actual) << " decimal value " << v;
501  }
502 
503  for (int spec = absl::kSpacePad2; spec <= absl::kSpacePad20; ++spec) {
504  std::string actual =
505  absl::StrCat(absl::Hex(v, static_cast<absl::PadSpec>(spec)));
506  snprintf(expected, sizeof(expected), spacepad_format,
507  spec - absl::kSpacePad2 + 2, v);
508  EXPECT_EQ(expected, actual) << " decimal value " << v;
509  }
510 }
511 
512 template <typename IntType>
513 void CheckDec(IntType v, const char* nopad_format, const char* zeropad_format,
514  const char* spacepad_format) {
515  char expected[256];
516 
518  snprintf(expected, sizeof(expected), nopad_format, v);
519  EXPECT_EQ(expected, actual) << " decimal value " << v;
520 
521  for (int spec = absl::kZeroPad2; spec <= absl::kZeroPad20; ++spec) {
522  std::string actual =
523  absl::StrCat(absl::Dec(v, static_cast<absl::PadSpec>(spec)));
524  snprintf(expected, sizeof(expected), zeropad_format,
525  spec - absl::kZeroPad2 + 2, v);
526  EXPECT_EQ(expected, actual)
527  << " decimal value " << v << " format '" << zeropad_format
528  << "' digits " << (spec - absl::kZeroPad2 + 2);
529  }
530 
531  for (int spec = absl::kSpacePad2; spec <= absl::kSpacePad20; ++spec) {
532  std::string actual =
533  absl::StrCat(absl::Dec(v, static_cast<absl::PadSpec>(spec)));
534  snprintf(expected, sizeof(expected), spacepad_format,
535  spec - absl::kSpacePad2 + 2, v);
536  EXPECT_EQ(expected, actual)
537  << " decimal value " << v << " format '" << spacepad_format
538  << "' digits " << (spec - absl::kSpacePad2 + 2);
539  }
540 }
541 
542 void CheckHexDec64(uint64_t v) {
543  unsigned long long ullv = v; // NOLINT(runtime/int)
544 
545  CheckHex(ullv, "%llx", "%0*llx", "%*llx");
546  CheckDec(ullv, "%llu", "%0*llu", "%*llu");
547 
548  long long llv = static_cast<long long>(ullv); // NOLINT(runtime/int)
549  CheckDec(llv, "%lld", "%0*lld", "%*lld");
550 
551  if (sizeof(v) == sizeof(&v)) {
552  auto uintptr = static_cast<uintptr_t>(v);
553  void* ptr = reinterpret_cast<void*>(uintptr);
554  CheckHex(ptr, "%llx", "%0*llx", "%*llx");
555  }
556 }
557 
558 void CheckHexDec32(uint32_t uv) {
559  CheckHex(uv, "%x", "%0*x", "%*x");
560  CheckDec(uv, "%u", "%0*u", "%*u");
561  int32_t v = static_cast<int32_t>(uv);
562  CheckDec(v, "%d", "%0*d", "%*d");
563 
564  if (sizeof(v) == sizeof(&v)) {
565  auto uintptr = static_cast<uintptr_t>(v);
566  void* ptr = reinterpret_cast<void*>(uintptr);
567  CheckHex(ptr, "%x", "%0*x", "%*x");
568  }
569 }
570 
571 void CheckAll(uint64_t v) {
572  CheckHexDec64(v);
573  CheckHexDec32(static_cast<uint32_t>(v));
574 }
575 
576 void TestFastPrints() {
577  // Test all small ints; there aren't many and they're common.
578  for (int i = 0; i < 10000; i++) {
579  CheckAll(i);
580  }
581 
583  CheckAll(std::numeric_limits<uint64_t>::max() - 1);
585  CheckAll(std::numeric_limits<int64_t>::min() + 1);
587  CheckAll(std::numeric_limits<uint32_t>::max() - 1);
589  CheckAll(std::numeric_limits<int32_t>::min() + 1);
590  CheckAll(999999999); // fits in 32 bits
591  CheckAll(1000000000); // fits in 32 bits
592  CheckAll(9999999999); // doesn't fit in 32 bits
593  CheckAll(10000000000); // doesn't fit in 32 bits
594  CheckAll(999999999999999999); // fits in signed 64-bit
595  CheckAll(9999999999999999999u); // fits in unsigned 64-bit, but not signed.
596  CheckAll(1000000000000000000); // fits in signed 64-bit
597  CheckAll(10000000000000000000u); // fits in unsigned 64-bit, but not signed.
598 
599  CheckAll(999999999876543210); // check all decimal digits, signed
600  CheckAll(9999999999876543210u); // check all decimal digits, unsigned.
601  CheckAll(0x123456789abcdef0); // check all hex digits
602  CheckAll(0x12345678);
603 
604  int8_t minus_one_8bit = -1;
605  EXPECT_EQ("ff", absl::StrCat(absl::Hex(minus_one_8bit)));
606 
607  int16_t minus_one_16bit = -1;
608  EXPECT_EQ("ffff", absl::StrCat(absl::Hex(minus_one_16bit)));
609 }
610 
611 TEST(Numbers, TestFunctionsMovedOverFromNumbersMain) {
612  TestFastPrints();
613 }
614 
615 } // namespace
ptr
char * ptr
Definition: abseil-cpp/absl/base/internal/low_level_alloc_test.cc:45
_gevent_test_main.result
result
Definition: _gevent_test_main.py:96
grpc_event_engine::experimental::slice_detail::operator==
bool operator==(const BaseSlice &a, const BaseSlice &b)
Definition: include/grpc/event_engine/slice.h:117
absl::StrAppend
void StrAppend(std::string *dest, const AlphaNum &a)
Definition: abseil-cpp/absl/strings/str_cat.cc:193
absl::StrCat
std::string StrCat(const AlphaNum &a, const AlphaNum &b)
Definition: abseil-cpp/absl/strings/str_cat.cc:98
absl::kNoPad
@ kNoPad
Definition: abseil-cpp/absl/strings/str_cat.h:86
uint16_t
unsigned short uint16_t
Definition: stdint-msvc2008.h:79
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
u
OPENSSL_EXPORT pem_password_cb void * u
Definition: pem.h:351
xds_manager.p
p
Definition: xds_manager.py:60
run_interop_tests.spec
def spec
Definition: run_interop_tests.py:1394
T
#define T(upbtypeconst, upbtype, ctype, default_value)
EXPECT_EQ
#define EXPECT_EQ(a, b)
Definition: iomgr/time_averaged_stats_test.cc:27
uint32_t
unsigned int uint32_t
Definition: stdint-msvc2008.h:80
absl::Hex
Definition: abseil-cpp/absl/strings/str_cat.h:134
autogen_x86imm.f
f
Definition: autogen_x86imm.py:9
int16_t
signed short int16_t
Definition: stdint-msvc2008.h:76
ABSL_EXPECT_DEBUG_DEATH
#define ABSL_EXPECT_DEBUG_DEATH(statement, regex)
Definition: abseil-cpp/absl/strings/str_cat_test.cc:32
absl::kZeroPad20
@ kZeroPad20
Definition: abseil-cpp/absl/strings/str_cat.h:105
TEST
#define TEST(name, init_size,...)
Definition: arena_test.cc:75
max
int max
Definition: bloaty/third_party/zlib/examples/enough.c:170
setup.v
v
Definition: third_party/bloaty/third_party/capstone/bindings/python/setup.py:42
uint64_t
unsigned __int64 uint64_t
Definition: stdint-msvc2008.h:90
absl::compare_internal::value_type
int8_t value_type
Definition: abseil-cpp/absl/types/compare.h:45
absl::kSpacePad20
@ kSpacePad20
Definition: abseil-cpp/absl/strings/str_cat.h:125
operator!=
bool operator!=(const Bytes &a, const Bytes &b)
Definition: boringssl-with-bazel/src/crypto/test/test_util.h:58
intptr_t
_W64 signed int intptr_t
Definition: stdint-msvc2008.h:118
uintptr_t
_W64 unsigned int uintptr_t
Definition: stdint-msvc2008.h:119
min
#define min(a, b)
Definition: qsort.h:83
absl::SixDigits
strings_internal::AlphaNumBuffer< numbers_internal::kSixDigitsToBufferSize > SixDigits(double d)
Definition: abseil-cpp/absl/strings/str_cat.h:405
d
static const fe d
Definition: curve25519_tables.h:19
n
int n
Definition: abseil-cpp/absl/container/btree_test.cc:1080
absl::kSpacePad2
@ kSpacePad2
Definition: abseil-cpp/absl/strings/str_cat.h:107
INT32_MIN
#define INT32_MIN
Definition: stdint-msvc2008.h:136
Option
Definition: bloaty/third_party/protobuf/src/google/protobuf/type.pb.h:1303
cv
unsigned cv
Definition: cxa_demangle.cpp:4908
absl::kZeroPad2
@ kZeroPad2
Definition: abseil-cpp/absl/strings/str_cat.h:87
absl::Dec
Definition: abseil-cpp/absl/strings/str_cat.h:184
int8_t
signed char int8_t
Definition: stdint-msvc2008.h:75
absl::PadSpec
PadSpec
Definition: abseil-cpp/absl/strings/str_cat.h:85
run_grpclb_interop_tests.l
dictionary l
Definition: run_grpclb_interop_tests.py:410
size
voidpf void uLong size
Definition: bloaty/third_party/zlib/contrib/minizip/ioapi.h:136
int32_t
signed int int32_t
Definition: stdint-msvc2008.h:77
const_reference
const typedef T & const_reference
Definition: cxa_demangle.cpp:4831
binary_size.old_size
old_size
Definition: binary_size.py:125
i
uint64_t i
Definition: abseil-cpp/absl/container/btree_benchmark.cc:230
LL
#define LL(x)
const_pointer
const typedef T * const_pointer
Definition: cxa_demangle.cpp:4833


grpc
Author(s):
autogenerated on Fri May 16 2025 03:00:19