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"
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) {
108  std::string result;
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 
137  result = absl::StrCat(absl::SixDigits(0.5));
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 std::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 // A minimal allocator that uses malloc().
199 template <typename T>
200 struct Mallocator {
201  typedef T value_type;
202  typedef size_t size_type;
203  typedef ptrdiff_t difference_type;
204  typedef T* pointer;
205  typedef const T* const_pointer;
206  typedef T& reference;
207  typedef const T& const_reference;
208 
209  size_type max_size() const {
210  return size_t(std::numeric_limits<size_type>::max()) / sizeof(value_type);
211  }
212  template <typename U>
213  struct rebind {
214  typedef Mallocator<U> other;
215  };
216  Mallocator() = default;
217  template <class U>
218  Mallocator(const Mallocator<U>&) {} // NOLINT(runtime/explicit)
219 
220  T* allocate(size_t n) { return static_cast<T*>(std::malloc(n * sizeof(T))); }
221  void deallocate(T* p, size_t) { std::free(p); }
222 };
223 template <typename T, typename U>
224 bool operator==(const Mallocator<T>&, const Mallocator<U>&) {
225  return true;
226 }
227 template <typename T, typename U>
228 bool operator!=(const Mallocator<T>&, const Mallocator<U>&) {
229  return false;
230 }
231 
232 TEST(StrCat, CustomAllocator) {
233  using mstring =
234  std::basic_string<char, std::char_traits<char>, Mallocator<char>>;
235  const mstring str1("PARACHUTE OFF A BLIMP INTO MOSCONE!!");
236 
237  const mstring str2("Read this book about coffee tables");
238 
239  std::string result = absl::StrCat(str1, str2);
240  EXPECT_EQ(result,
241  "PARACHUTE OFF A BLIMP INTO MOSCONE!!"
242  "Read this book about coffee tables");
243 }
244 
245 TEST(StrCat, MaxArgs) {
246  std::string result;
247  // Test 10 up to 26 arguments, the old maximum
248  result = absl::StrCat(1, 2, 3, 4, 5, 6, 7, 8, 9, "a");
249  EXPECT_EQ(result, "123456789a");
250  result = absl::StrCat(1, 2, 3, 4, 5, 6, 7, 8, 9, "a", "b");
251  EXPECT_EQ(result, "123456789ab");
252  result = absl::StrCat(1, 2, 3, 4, 5, 6, 7, 8, 9, "a", "b", "c");
253  EXPECT_EQ(result, "123456789abc");
254  result = absl::StrCat(1, 2, 3, 4, 5, 6, 7, 8, 9, "a", "b", "c", "d");
255  EXPECT_EQ(result, "123456789abcd");
256  result = absl::StrCat(1, 2, 3, 4, 5, 6, 7, 8, 9, "a", "b", "c", "d", "e");
257  EXPECT_EQ(result, "123456789abcde");
258  result =
259  absl::StrCat(1, 2, 3, 4, 5, 6, 7, 8, 9, "a", "b", "c", "d", "e", "f");
260  EXPECT_EQ(result, "123456789abcdef");
261  result = absl::StrCat(1, 2, 3, 4, 5, 6, 7, 8, 9, "a", "b", "c", "d", "e", "f",
262  "g");
263  EXPECT_EQ(result, "123456789abcdefg");
264  result = absl::StrCat(1, 2, 3, 4, 5, 6, 7, 8, 9, "a", "b", "c", "d", "e", "f",
265  "g", "h");
266  EXPECT_EQ(result, "123456789abcdefgh");
267  result = absl::StrCat(1, 2, 3, 4, 5, 6, 7, 8, 9, "a", "b", "c", "d", "e", "f",
268  "g", "h", "i");
269  EXPECT_EQ(result, "123456789abcdefghi");
270  result = absl::StrCat(1, 2, 3, 4, 5, 6, 7, 8, 9, "a", "b", "c", "d", "e", "f",
271  "g", "h", "i", "j");
272  EXPECT_EQ(result, "123456789abcdefghij");
273  result = absl::StrCat(1, 2, 3, 4, 5, 6, 7, 8, 9, "a", "b", "c", "d", "e", "f",
274  "g", "h", "i", "j", "k");
275  EXPECT_EQ(result, "123456789abcdefghijk");
276  result = absl::StrCat(1, 2, 3, 4, 5, 6, 7, 8, 9, "a", "b", "c", "d", "e", "f",
277  "g", "h", "i", "j", "k", "l");
278  EXPECT_EQ(result, "123456789abcdefghijkl");
279  result = absl::StrCat(1, 2, 3, 4, 5, 6, 7, 8, 9, "a", "b", "c", "d", "e", "f",
280  "g", "h", "i", "j", "k", "l", "m");
281  EXPECT_EQ(result, "123456789abcdefghijklm");
282  result = absl::StrCat(1, 2, 3, 4, 5, 6, 7, 8, 9, "a", "b", "c", "d", "e", "f",
283  "g", "h", "i", "j", "k", "l", "m", "n");
284  EXPECT_EQ(result, "123456789abcdefghijklmn");
285  result = absl::StrCat(1, 2, 3, 4, 5, 6, 7, 8, 9, "a", "b", "c", "d", "e", "f",
286  "g", "h", "i", "j", "k", "l", "m", "n", "o");
287  EXPECT_EQ(result, "123456789abcdefghijklmno");
288  result = absl::StrCat(1, 2, 3, 4, 5, 6, 7, 8, 9, "a", "b", "c", "d", "e", "f",
289  "g", "h", "i", "j", "k", "l", "m", "n", "o", "p");
290  EXPECT_EQ(result, "123456789abcdefghijklmnop");
291  result = absl::StrCat(1, 2, 3, 4, 5, 6, 7, 8, 9, "a", "b", "c", "d", "e", "f",
292  "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q");
293  EXPECT_EQ(result, "123456789abcdefghijklmnopq");
294  // No limit thanks to C++11's variadic templates
295  result = absl::StrCat(
296  1, 2, 3, 4, 5, 6, 7, 8, 9, 10, "a", "b", "c", "d", "e", "f", "g", "h",
297  "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w",
298  "x", "y", "z", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L",
299  "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z");
300  EXPECT_EQ(result,
301  "12345678910abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ");
302 }
303 
304 TEST(StrAppend, Basics) {
305  std::string result = "existing text";
306 
307  std::string strs[] = {"Hello", "Cruel", "World"};
308 
309  std::string stdstrs[] = {
310  "std::Hello",
311  "std::Cruel",
312  "std::World"
313  };
314 
315  absl::string_view pieces[] = {"Hello", "Cruel", "World"};
316 
317  const char* c_strs[] = {
318  "Hello",
319  "Cruel",
320  "World"
321  };
322 
323  int32_t i32s[] = {'H', 'C', 'W'};
324  uint64_t ui64s[] = {12345678910LL, 10987654321LL};
325 
326  std::string::size_type old_size = result.size();
327  absl::StrAppend(&result);
328  EXPECT_EQ(result.size(), old_size);
329 
330  old_size = result.size();
331  absl::StrAppend(&result, strs[0]);
332  EXPECT_EQ(result.substr(old_size), "Hello");
333 
334  old_size = result.size();
335  absl::StrAppend(&result, strs[1], pieces[2]);
336  EXPECT_EQ(result.substr(old_size), "CruelWorld");
337 
338  old_size = result.size();
339  absl::StrAppend(&result, stdstrs[0], ", ", pieces[2]);
340  EXPECT_EQ(result.substr(old_size), "std::Hello, World");
341 
342  old_size = result.size();
343  absl::StrAppend(&result, strs[0], ", ", stdstrs[1], " ", strs[2], "!");
344  EXPECT_EQ(result.substr(old_size), "Hello, std::Cruel World!");
345 
346  old_size = result.size();
347  absl::StrAppend(&result, pieces[0], ", ", pieces[1], " ", pieces[2]);
348  EXPECT_EQ(result.substr(old_size), "Hello, Cruel World");
349 
350  old_size = result.size();
351  absl::StrAppend(&result, c_strs[0], ", ", c_strs[1], " ", c_strs[2]);
352  EXPECT_EQ(result.substr(old_size), "Hello, Cruel World");
353 
354  old_size = result.size();
355  absl::StrAppend(&result, "ASCII ", i32s[0], ", ", i32s[1], " ", i32s[2], "!");
356  EXPECT_EQ(result.substr(old_size), "ASCII 72, 67 87!");
357 
358  old_size = result.size();
359  absl::StrAppend(&result, ui64s[0], ", ", ui64s[1], "!");
360  EXPECT_EQ(result.substr(old_size), "12345678910, 10987654321!");
361 
362  std::string one =
363  "1"; // Actually, it's the size of this std::string that we want; a
364  // 64-bit build distinguishes between size_t and uint64_t,
365  // even though they're both unsigned 64-bit values.
366  old_size = result.size();
367  absl::StrAppend(&result, "And a ", one.size(), " and a ",
368  &result[2] - &result[0], " and a ", one, " 2 3 4", "!");
369  EXPECT_EQ(result.substr(old_size), "And a 1 and a 2 and a 1 2 3 4!");
370 
371  // result = absl::StrCat("Single chars won't compile", '!');
372  // result = absl::StrCat("Neither will nullptrs", nullptr);
373  old_size = result.size();
374  absl::StrAppend(&result,
375  "To output a char by ASCII/numeric value, use +: ", '!' + 0);
376  EXPECT_EQ(result.substr(old_size),
377  "To output a char by ASCII/numeric value, use +: 33");
378 
379  // Test 9 arguments, the old maximum
380  old_size = result.size();
381  absl::StrAppend(&result, 1, 22, 333, 4444, 55555, 666666, 7777777, 88888888,
382  9);
383  EXPECT_EQ(result.substr(old_size), "1223334444555556666667777777888888889");
384 
385  // No limit thanks to C++11's variadic templates
386  old_size = result.size();
388  &result, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, //
389  "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", //
390  "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", //
391  "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", //
392  "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", //
393  "No limit thanks to C++11's variadic templates");
394  EXPECT_EQ(result.substr(old_size),
395  "12345678910abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
396  "No limit thanks to C++11's variadic templates");
397 }
398 
399 TEST(StrCat, VectorBoolReferenceTypes) {
400  std::vector<bool> v;
401  v.push_back(true);
402  v.push_back(false);
403  std::vector<bool> const& cv = v;
404  // Test that vector<bool>::reference and vector<bool>::const_reference
405  // are handled as if the were really bool types and not the proxy types
406  // they really are.
407  std::string result = absl::StrCat(v[0], v[1], cv[0], cv[1]); // NOLINT
408  EXPECT_EQ(result, "1010");
409 }
410 
411 // Passing nullptr to memcpy is undefined behavior and this test
412 // provides coverage of codepaths that handle empty strings with nullptrs.
413 TEST(StrCat, AvoidsMemcpyWithNullptr) {
414  EXPECT_EQ(absl::StrCat(42, absl::string_view{}), "42");
415 
416  // Cover CatPieces code.
417  EXPECT_EQ(absl::StrCat(1, 2, 3, 4, 5, absl::string_view{}), "12345");
418 
419  // Cover AppendPieces.
420  std::string result;
421  absl::StrAppend(&result, 1, 2, 3, 4, 5, absl::string_view{});
422  EXPECT_EQ(result, "12345");
423 }
424 
425 #ifdef GTEST_HAS_DEATH_TEST
426 TEST(StrAppend, Death) {
427  std::string s = "self";
428  // on linux it's "assertion", on mac it's "Assertion",
429  // on chromiumos it's "Assertion ... failed".
430  ABSL_EXPECT_DEBUG_DEATH(absl::StrAppend(&s, s.c_str() + 1),
431  "ssertion.*failed");
432  ABSL_EXPECT_DEBUG_DEATH(absl::StrAppend(&s, s), "ssertion.*failed");
433 }
434 #endif // GTEST_HAS_DEATH_TEST
435 
436 TEST(StrAppend, EmptyString) {
437  std::string s = "";
438  absl::StrAppend(&s, s);
439  EXPECT_EQ(s, "");
440 }
441 
442 template <typename IntType>
443 void CheckHex(IntType v, const char* nopad_format, const char* zeropad_format,
444  const char* spacepad_format) {
445  char expected[256];
446 
447  std::string actual = absl::StrCat(absl::Hex(v, absl::kNoPad));
448  snprintf(expected, sizeof(expected), nopad_format, v);
449  EXPECT_EQ(expected, actual) << " decimal value " << v;
450 
451  for (int spec = absl::kZeroPad2; spec <= absl::kZeroPad20; ++spec) {
452  std::string actual =
453  absl::StrCat(absl::Hex(v, static_cast<absl::PadSpec>(spec)));
454  snprintf(expected, sizeof(expected), zeropad_format,
455  spec - absl::kZeroPad2 + 2, v);
456  EXPECT_EQ(expected, actual) << " decimal value " << v;
457  }
458 
459  for (int spec = absl::kSpacePad2; spec <= absl::kSpacePad20; ++spec) {
460  std::string actual =
461  absl::StrCat(absl::Hex(v, static_cast<absl::PadSpec>(spec)));
462  snprintf(expected, sizeof(expected), spacepad_format,
463  spec - absl::kSpacePad2 + 2, v);
464  EXPECT_EQ(expected, actual) << " decimal value " << v;
465  }
466 }
467 
468 template <typename IntType>
469 void CheckDec(IntType v, const char* nopad_format, const char* zeropad_format,
470  const char* spacepad_format) {
471  char expected[256];
472 
473  std::string actual = absl::StrCat(absl::Dec(v, absl::kNoPad));
474  snprintf(expected, sizeof(expected), nopad_format, v);
475  EXPECT_EQ(expected, actual) << " decimal value " << v;
476 
477  for (int spec = absl::kZeroPad2; spec <= absl::kZeroPad20; ++spec) {
478  std::string actual =
479  absl::StrCat(absl::Dec(v, static_cast<absl::PadSpec>(spec)));
480  snprintf(expected, sizeof(expected), zeropad_format,
481  spec - absl::kZeroPad2 + 2, v);
482  EXPECT_EQ(expected, actual)
483  << " decimal value " << v << " format '" << zeropad_format
484  << "' digits " << (spec - absl::kZeroPad2 + 2);
485  }
486 
487  for (int spec = absl::kSpacePad2; spec <= absl::kSpacePad20; ++spec) {
488  std::string actual =
489  absl::StrCat(absl::Dec(v, static_cast<absl::PadSpec>(spec)));
490  snprintf(expected, sizeof(expected), spacepad_format,
491  spec - absl::kSpacePad2 + 2, v);
492  EXPECT_EQ(expected, actual)
493  << " decimal value " << v << " format '" << spacepad_format
494  << "' digits " << (spec - absl::kSpacePad2 + 2);
495  }
496 }
497 
498 void CheckHexDec64(uint64_t v) {
499  unsigned long long ullv = v; // NOLINT(runtime/int)
500 
501  CheckHex(ullv, "%llx", "%0*llx", "%*llx");
502  CheckDec(ullv, "%llu", "%0*llu", "%*llu");
503 
504  long long llv = static_cast<long long>(ullv); // NOLINT(runtime/int)
505  CheckDec(llv, "%lld", "%0*lld", "%*lld");
506 
507  if (sizeof(v) == sizeof(&v)) {
508  auto uintptr = static_cast<uintptr_t>(v);
509  void* ptr = reinterpret_cast<void*>(uintptr);
510  CheckHex(ptr, "%llx", "%0*llx", "%*llx");
511  }
512 }
513 
514 void CheckHexDec32(uint32_t uv) {
515  CheckHex(uv, "%x", "%0*x", "%*x");
516  CheckDec(uv, "%u", "%0*u", "%*u");
517  int32_t v = static_cast<int32_t>(uv);
518  CheckDec(v, "%d", "%0*d", "%*d");
519 
520  if (sizeof(v) == sizeof(&v)) {
521  auto uintptr = static_cast<uintptr_t>(v);
522  void* ptr = reinterpret_cast<void*>(uintptr);
523  CheckHex(ptr, "%x", "%0*x", "%*x");
524  }
525 }
526 
527 void CheckAll(uint64_t v) {
528  CheckHexDec64(v);
529  CheckHexDec32(static_cast<uint32_t>(v));
530 }
531 
532 void TestFastPrints() {
533  // Test all small ints; there aren't many and they're common.
534  for (int i = 0; i < 10000; i++) {
535  CheckAll(i);
536  }
537 
538  CheckAll(std::numeric_limits<uint64_t>::max());
539  CheckAll(std::numeric_limits<uint64_t>::max() - 1);
540  CheckAll(std::numeric_limits<int64_t>::min());
541  CheckAll(std::numeric_limits<int64_t>::min() + 1);
542  CheckAll(std::numeric_limits<uint32_t>::max());
543  CheckAll(std::numeric_limits<uint32_t>::max() - 1);
544  CheckAll(std::numeric_limits<int32_t>::min());
545  CheckAll(std::numeric_limits<int32_t>::min() + 1);
546  CheckAll(999999999); // fits in 32 bits
547  CheckAll(1000000000); // fits in 32 bits
548  CheckAll(9999999999); // doesn't fit in 32 bits
549  CheckAll(10000000000); // doesn't fit in 32 bits
550  CheckAll(999999999999999999); // fits in signed 64-bit
551  CheckAll(9999999999999999999u); // fits in unsigned 64-bit, but not signed.
552  CheckAll(1000000000000000000); // fits in signed 64-bit
553  CheckAll(10000000000000000000u); // fits in unsigned 64-bit, but not signed.
554 
555  CheckAll(999999999876543210); // check all decimal digits, signed
556  CheckAll(9999999999876543210u); // check all decimal digits, unsigned.
557  CheckAll(0x123456789abcdef0); // check all hex digits
558  CheckAll(0x12345678);
559 
560  int8_t minus_one_8bit = -1;
561  EXPECT_EQ("ff", absl::StrCat(absl::Hex(minus_one_8bit)));
562 
563  int16_t minus_one_16bit = -1;
564  EXPECT_EQ("ffff", absl::StrCat(absl::Hex(minus_one_16bit)));
565 }
566 
567 TEST(Numbers, TestFunctionsMovedOverFromNumbersMain) {
568  TestFastPrints();
569 }
570 
571 } // namespace
int v
Definition: variant_test.cc:81
void StrAppend(std::string *dest, const AlphaNum &a)
Definition: str_cat.cc:193
std::string StrCat(const AlphaNum &a, const AlphaNum &b)
Definition: str_cat.cc:98
#define ABSL_EXPECT_DEBUG_DEATH(statement, regex)
Definition: str_cat_test.cc:32
constexpr size_type size() const noexcept
Definition: string_view.h:260
strings_internal::AlphaNumBuffer< numbers_internal::kSixDigitsToBufferSize > SixDigits(double d)
Definition: str_cat.h:395
bool operator==(const absl::InlinedVector< T, N, A > &a, const absl::InlinedVector< T, N, A > &b)
char * ptr
bool operator!=(const absl::InlinedVector< T, N, A > &a, const absl::InlinedVector< T, N, A > &b)
uintptr_t size
TEST(Symbolize, Unimplemented)


abseil_cpp
Author(s):
autogenerated on Wed Jun 19 2019 19:19:58