23 #include "gtest/gtest.h" 29 #define ABSL_EXPECT_DEBUG_DEATH(statement, regex) \ 30 EXPECT_DEBUG_DEATH(statement, ".*") 32 #define ABSL_EXPECT_DEBUG_DEATH(statement, regex) \ 33 EXPECT_DEBUG_DEATH(statement, regex) 41 const uint16_t us = 2;
43 const unsigned int ui = 4;
45 const unsigned long ul = 6;
46 const long long ll = -7;
47 const unsigned long long ull = 8;
48 const ptrdiff_t ptrdiff = -9;
49 const size_t size = 10;
50 const intptr_t intptr = -12;
51 const uintptr_t uintptr = 13;
54 EXPECT_EQ(answer,
"-12");
56 EXPECT_EQ(answer,
"-34");
58 EXPECT_EQ(answer,
"-56");
60 EXPECT_EQ(answer,
"-78");
62 EXPECT_EQ(answer,
"-910");
64 EXPECT_EQ(answer,
"-9-12");
66 EXPECT_EQ(answer,
"130");
70 enum SmallNumbers { One = 1, Ten = 10 } e = Ten;
74 enum class Option { Boxers = 1, Briefs = -1 };
78 enum class Airplane : uint64_t {
84 EXPECT_EQ(
"10000000000",
absl::StrCat(Airplane::Canary));
86 enum class TwoGig : int32_t {
88 TwoToTheSixteenth = 1 << 16,
89 TwoToTheThirtyFirst = INT32_MIN
91 EXPECT_EQ(
"65536",
absl::StrCat(TwoGig::TwoToTheSixteenth));
92 EXPECT_EQ(
"-2147483648",
absl::StrCat(TwoGig::TwoToTheThirtyFirst));
95 enum class FourGig : uint32_t {
97 TwoToTheSixteenth = 1 << 16,
98 TwoToTheThirtyFirst = 1U << 31
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)));
104 EXPECT_EQ(
"10000000000",
absl::StrCat(Airplane::Canary));
110 std::string strs[] = {
"Hello",
"Cruel",
"World"};
112 std::string stdstrs[] = {
120 const char* c_strs[] = {
126 int32_t i32s[] = {
'H',
'C',
'W'};
127 uint64_t ui64s[] = {12345678910LL, 10987654321LL};
132 EXPECT_EQ(result,
"0123");
135 EXPECT_EQ(result,
"-1");
138 EXPECT_EQ(result,
"0.5");
141 EXPECT_EQ(result,
"CruelWorld");
144 EXPECT_EQ(result,
"std::Cruel std::World");
147 EXPECT_EQ(result,
"Hello, World");
149 result =
absl::StrCat(strs[0],
", ", strs[1],
" ", strs[2],
"!");
150 EXPECT_EQ(result,
"Hello, Cruel World!");
152 result =
absl::StrCat(pieces[0],
", ", pieces[1],
" ", pieces[2]);
153 EXPECT_EQ(result,
"Hello, Cruel World");
155 result =
absl::StrCat(c_strs[0],
", ", c_strs[1],
" ", c_strs[2]);
156 EXPECT_EQ(result,
"Hello, Cruel World");
158 result =
absl::StrCat(
"ASCII ", i32s[0],
", ", i32s[1],
" ", i32s[2],
"!");
159 EXPECT_EQ(result,
"ASCII 72, 67 87!");
162 EXPECT_EQ(result,
"12345678910, 10987654321!");
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!");
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");
180 EXPECT_EQ(result,
"A hundred K and a half is 100000");
185 EXPECT_EQ(result,
"A hundred K and one and a half is 100002");
191 EXPECT_EQ(result,
"A hundred K and a half squared is 1.00001e+10");
193 result =
absl::StrCat(1, 2, 333, 4444, 55555, 666666, 7777777, 88888888,
195 EXPECT_EQ(result,
"12333444455555666666777777788888888999999999");
199 template <
typename T>
202 typedef size_t size_type;
203 typedef ptrdiff_t difference_type;
205 typedef const T* const_pointer;
206 typedef T& reference;
207 typedef const T& const_reference;
209 size_type max_size()
const {
210 return size_t(std::numeric_limits<size_type>::max()) /
sizeof(
value_type);
212 template <
typename U>
214 typedef Mallocator<U> other;
216 Mallocator() =
default;
218 Mallocator(
const Mallocator<U>&) {}
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); }
223 template <
typename T,
typename U>
224 bool operator==(
const Mallocator<T>&,
const Mallocator<U>&) {
227 template <
typename T,
typename U>
228 bool operator!=(
const Mallocator<T>&,
const Mallocator<U>&) {
234 std::basic_string<char, std::char_traits<char>, Mallocator<char>>;
235 const mstring str1(
"PARACHUTE OFF A BLIMP INTO MOSCONE!!");
237 const mstring str2(
"Read this book about coffee tables");
241 "PARACHUTE OFF A BLIMP INTO MOSCONE!!" 242 "Read this book about coffee tables");
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");
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",
263 EXPECT_EQ(result,
"123456789abcdefg");
264 result =
absl::StrCat(1, 2, 3, 4, 5, 6, 7, 8, 9,
"a",
"b",
"c",
"d",
"e",
"f",
266 EXPECT_EQ(result,
"123456789abcdefgh");
267 result =
absl::StrCat(1, 2, 3, 4, 5, 6, 7, 8, 9,
"a",
"b",
"c",
"d",
"e",
"f",
269 EXPECT_EQ(result,
"123456789abcdefghi");
270 result =
absl::StrCat(1, 2, 3, 4, 5, 6, 7, 8, 9,
"a",
"b",
"c",
"d",
"e",
"f",
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");
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");
301 "12345678910abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ");
305 std::string result =
"existing text";
307 std::string strs[] = {
"Hello",
"Cruel",
"World"};
309 std::string stdstrs[] = {
317 const char* c_strs[] = {
323 int32_t i32s[] = {
'H',
'C',
'W'};
324 uint64_t ui64s[] = {12345678910LL, 10987654321LL};
326 std::string::size_type old_size = result.
size();
328 EXPECT_EQ(result.size(), old_size);
330 old_size = result.size();
332 EXPECT_EQ(result.substr(old_size),
"Hello");
334 old_size = result.size();
336 EXPECT_EQ(result.substr(old_size),
"CruelWorld");
338 old_size = result.size();
340 EXPECT_EQ(result.substr(old_size),
"std::Hello, World");
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!");
346 old_size = result.size();
348 EXPECT_EQ(result.substr(old_size),
"Hello, Cruel World");
350 old_size = result.size();
352 EXPECT_EQ(result.substr(old_size),
"Hello, Cruel World");
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!");
358 old_size = result.size();
360 EXPECT_EQ(result.substr(old_size),
"12345678910, 10987654321!");
366 old_size = result.size();
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!");
373 old_size = result.size();
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");
380 old_size = result.size();
381 absl::StrAppend(&result, 1, 22, 333, 4444, 55555, 666666, 7777777, 88888888,
383 EXPECT_EQ(result.substr(old_size),
"1223334444555556666667777777888888889");
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");
403 std::vector<bool>
const& cv =
v;
407 std::string result =
absl::StrCat(v[0], v[1], cv[0], cv[1]);
408 EXPECT_EQ(result,
"1010");
422 EXPECT_EQ(result,
"12345");
425 #ifdef GTEST_HAS_DEATH_TEST 427 std::string s =
"self";
434 #endif // GTEST_HAS_DEATH_TEST 442 template <
typename IntType>
443 void CheckHex(IntType
v,
const char* nopad_format,
const char* zeropad_format,
444 const char* spacepad_format) {
448 snprintf(expected,
sizeof(expected), nopad_format, v);
449 EXPECT_EQ(expected, actual) <<
" decimal value " <<
v;
454 snprintf(expected,
sizeof(expected), zeropad_format,
456 EXPECT_EQ(expected, actual) <<
" decimal value " <<
v;
462 snprintf(expected,
sizeof(expected), spacepad_format,
464 EXPECT_EQ(expected, actual) <<
" decimal value " <<
v;
468 template <
typename IntType>
469 void CheckDec(IntType v,
const char* nopad_format,
const char* zeropad_format,
470 const char* spacepad_format) {
474 snprintf(expected,
sizeof(expected), nopad_format, v);
475 EXPECT_EQ(expected, actual) <<
" decimal value " <<
v;
480 snprintf(expected,
sizeof(expected), zeropad_format,
482 EXPECT_EQ(expected, actual)
483 <<
" decimal value " << v <<
" format '" << zeropad_format
490 snprintf(expected,
sizeof(expected), spacepad_format,
492 EXPECT_EQ(expected, actual)
493 <<
" decimal value " << v <<
" format '" << spacepad_format
498 void CheckHexDec64(uint64_t v) {
499 unsigned long long ullv =
v;
501 CheckHex(ullv,
"%llx",
"%0*llx",
"%*llx");
502 CheckDec(ullv,
"%llu",
"%0*llu",
"%*llu");
504 long long llv =
static_cast<long long>(ullv);
505 CheckDec(llv,
"%lld",
"%0*lld",
"%*lld");
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");
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");
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");
527 void CheckAll(uint64_t v) {
529 CheckHexDec32(static_cast<uint32_t>(v));
532 void TestFastPrints() {
534 for (
int i = 0;
i < 10000;
i++) {
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);
547 CheckAll(1000000000);
548 CheckAll(9999999999);
549 CheckAll(10000000000);
550 CheckAll(999999999999999999);
551 CheckAll(9999999999999999999u);
552 CheckAll(1000000000000000000);
553 CheckAll(10000000000000000000u);
555 CheckAll(999999999876543210);
556 CheckAll(9999999999876543210u);
557 CheckAll(0x123456789abcdef0);
558 CheckAll(0x12345678);
560 int8_t minus_one_8bit = -1;
563 int16_t minus_one_16bit = -1;
567 TEST(Numbers, TestFunctionsMovedOverFromNumbersMain) {
void StrAppend(std::string *dest, const AlphaNum &a)
std::string StrCat(const AlphaNum &a, const AlphaNum &b)
#define ABSL_EXPECT_DEBUG_DEATH(statement, regex)
constexpr size_type size() const noexcept
strings_internal::AlphaNumBuffer< numbers_internal::kSixDigitsToBufferSize > SixDigits(double d)
bool operator==(const absl::InlinedVector< T, N, A > &a, const absl::InlinedVector< T, N, A > &b)
bool operator!=(const absl::InlinedVector< T, N, A > &a, const absl::InlinedVector< T, N, A > &b)
TEST(Symbolize, Unimplemented)