23 #include <initializer_list> 28 #include <type_traits> 31 #include "gtest/gtest.h" 42 std::vector<std::string>
v = {
"foo",
"bar",
"baz"};
48 std::vector<absl::string_view> v = {
"foo",
"bar",
"baz"};
54 std::vector<const char*> v = {
"foo",
"bar",
"baz"};
60 std::string
a =
"foo",
b =
"bar", c =
"baz";
61 std::vector<char*> v = {&a[0], &
b[0], &c[0]};
67 std::vector<int> v = {1, 2, 3, -4};
74 EXPECT_EQ(
"a-b-c", s);
78 std::string s =
absl::StrJoin(std::make_tuple(123,
"abc", 0.456),
"-");
79 EXPECT_EQ(
"123-abc-0.456", s);
84 std::vector<std::unique_ptr<int>>
v;
85 v.emplace_back(
new int(1));
86 v.emplace_back(
new int(2));
87 v.emplace_back(
new int(3));
93 const int a[] = {1, 2, 3, -4};
99 int x = 1, y = 2, z = 3;
100 std::vector<int*> v = {&x, &y, &z};
106 int x = 1, y = 2, z = 3;
107 int *px = &x, *py = &y, *pz = &z;
108 std::vector<int**> v = {&px, &py, &pz};
114 std::string
a(
"a"),
b(
"b");
115 std::vector<std::string*> v = {&
a, &
b};
121 std::map<std::string, int> m = {{
"a", 1}, {
"b", 2}, {
"c", 3}};
128 const std::string s =
"a=b=c=d";
138 std::vector<std::string>
v;
145 std::vector<std::string> v = {
"foo"};
151 std::vector<std::string> v = {
""};
157 std::vector<std::string> v = {
"a",
""};
163 std::vector<std::string> v = {
"",
""};
169 std::vector<bool> v = {
true,
false,
true};
175 std::vector<std::string> v{
"One",
"Two",
"Three"};
181 EXPECT_EQ(
"(One)(Two)(Three)", joined);
184 class ImmovableFormatter {
186 void operator()(std::string*
out,
const std::string&
in) {
189 ImmovableFormatter() {}
190 ImmovableFormatter(
const ImmovableFormatter&) =
delete;
192 EXPECT_EQ(
"(One)(Two)(Three)",
absl::StrJoin(v,
"", ImmovableFormatter()));
195 class OverloadedFormatter {
197 void operator()(std::string*
out,
const std::string&
in) {
200 void operator()(std::string*
out,
const std::string&
in)
const {
204 EXPECT_EQ(
"(One)(Two)(Three)",
absl::StrJoin(v,
"", OverloadedFormatter()));
205 const OverloadedFormatter fmt = {};
220 f(&s, static_cast<int>(1));
221 f(&s, static_cast<int16_t>(2));
222 f(&s, static_cast<int64_t>(3));
223 f(&s, static_cast<float>(4));
224 f(&s, static_cast<double>(5));
225 f(&s, static_cast<unsigned>(6));
226 f(&s, static_cast<size_t>(7));
228 EXPECT_EQ(
"Testing: 1234567 OK", s);
236 std::vector<bool> v = {
true,
false,
true};
247 EXPECT_EQ(
"hello", s);
250 struct StreamableType {
251 std::string contents;
253 inline std::ostream&
operator<<(std::ostream& os,
const StreamableType& t) {
254 os <<
"Streamable:" << t.contents;
262 f(&s, static_cast<int>(1));
263 f(&s, static_cast<int16_t>(2));
264 f(&s, static_cast<int64_t>(3));
265 f(&s, static_cast<float>(4));
266 f(&s, static_cast<double>(5));
267 f(&s, static_cast<unsigned>(6));
268 f(&s, static_cast<size_t>(7));
270 StreamableType streamable = {
"object"};
272 EXPECT_EQ(
"Testing: 1234567 OK Streamable:object", s);
277 struct TestingParenFormatter {
278 template <
typename T>
279 void operator()(std::string* s,
const T& t) {
290 f(&s, std::make_pair(
"a",
"b"));
291 f(&s, std::make_pair(1, 2));
292 EXPECT_EQ(
"a=b1=2", s);
298 TestingParenFormatter());
300 f(&s, std::make_pair(
"a",
"b"));
301 f(&s, std::make_pair(1, 2));
302 EXPECT_EQ(
"(a)=(b)(1)=(2)", s);
312 int x = 1, y = 2, z = 3;
339 int x = 1, y = 2, z = 3;
344 EXPECT_EQ(
"(1)(2)(3)", s);
351 auto x = std::unique_ptr<int>(
new int(1));
352 auto y = std::unique_ptr<int>(
new int(2));
353 auto z = std::unique_ptr<int>(
new int(3));
367 std::vector<std::string> v = {
"a",
"b",
"c"};
389 auto a = {
"a",
"b",
"c"};
394 std::initializer_list<const char*> a = {
"a",
"b",
"c"};
399 std::initializer_list<std::string> a = {
"a",
"b",
"c"};
404 std::initializer_list<absl::string_view> a = {
"a",
"b",
"c"};
410 auto a = {
"a",
"b",
"c"};
411 TestingParenFormatter f;
423 TestingParenFormatter f;
430 EXPECT_EQ(
"hello",
absl::StrJoin(std::make_tuple(
"hello"),
"-"));
433 std::string y(
"hello");
435 EXPECT_EQ(
"10-hello-3.14",
absl::StrJoin(std::make_tuple(x, y, z),
"-"));
438 EXPECT_EQ(
"10-hello-3.14",
441 struct TestFormatter {
443 void operator()(std::string*
out,
int v) {
444 snprintf(buffer,
sizeof(buffer),
"%#.8x", v);
447 void operator()(std::string* out,
double v) {
448 snprintf(buffer,
sizeof(buffer),
"%#.0f", v);
451 void operator()(std::string* out,
const std::string& v) {
452 snprintf(buffer,
sizeof(buffer),
"%.4s", v.c_str());
456 EXPECT_EQ(
"0x0000000a-hell-3.",
457 absl::StrJoin(std::make_tuple(x, y, z),
"-", TestFormatter()));
459 "0x0000000a-hell-3.",
460 absl::StrJoin(std::make_tuple(x, std::cref(y), z),
"-", TestFormatter()));
461 EXPECT_EQ(
"0x0000000a-hell-3.",
464 EXPECT_EQ(
"0x0000000a-hell-3.",
466 absl::make_unique<std::string>(y),
467 absl::make_unique<double>(z)),
469 EXPECT_EQ(
"0x0000000a-hell-3.",
470 absl::StrJoin(std::make_tuple(absl::make_unique<int>(x), &y, &z),
void StrAppend(std::string *dest, const AlphaNum &a)
std::string StrJoin(Iterator start, Iterator end, absl::string_view sep, Formatter &&fmt)
std::ostream & operator<<(std::ostream &os, absl::LogSeverity s)
strings_internal::PairFormatterImpl< FirstFormatter, SecondFormatter > PairFormatter(FirstFormatter f1, absl::string_view sep, SecondFormatter f2)
strings_internal::Splitter< typename strings_internal::SelectDelimiter< Delimiter >::type, AllowEmpty > StrSplit(strings_internal::ConvertibleToStringView text, Delimiter d)
strings_internal::AlphaNumFormatterImpl AlphaNumFormatter()
strings_internal::DereferenceFormatterImpl< Formatter > DereferenceFormatter(Formatter &&f)
#define ABSL_ARRAYSIZE(array)
TEST(Symbolize, Unimplemented)
strings_internal::StreamFormatterImpl StreamFormatter()