• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 
2 #include "absl/strings/str_format.h"
3 
4 #include <cstdarg>
5 #include <cstdint>
6 #include <cstdio>
7 #include <string>
8 
9 #include "gmock/gmock.h"
10 #include "gtest/gtest.h"
11 #include "absl/strings/cord.h"
12 #include "absl/strings/str_cat.h"
13 #include "absl/strings/string_view.h"
14 
15 namespace absl {
16 ABSL_NAMESPACE_BEGIN
17 namespace {
18 using str_format_internal::FormatArgImpl;
19 
20 using FormatEntryPointTest = ::testing::Test;
21 
TEST_F(FormatEntryPointTest,Format)22 TEST_F(FormatEntryPointTest, Format) {
23   std::string sink;
24   EXPECT_TRUE(Format(&sink, "A format %d", 123));
25   EXPECT_EQ("A format 123", sink);
26   sink.clear();
27 
28   ParsedFormat<'d'> pc("A format %d");
29   EXPECT_TRUE(Format(&sink, pc, 123));
30   EXPECT_EQ("A format 123", sink);
31 }
TEST_F(FormatEntryPointTest,UntypedFormat)32 TEST_F(FormatEntryPointTest, UntypedFormat) {
33   constexpr const char* formats[] = {
34     "",
35     "a",
36     "%80d",
37 #if !defined(_MSC_VER) && !defined(__ANDROID__) && !defined(__native_client__)
38     // MSVC, NaCL and Android don't support positional syntax.
39     "complicated multipart %% %1$d format %1$0999d",
40 #endif  // _MSC_VER
41   };
42   for (const char* fmt : formats) {
43     std::string actual;
44     int i = 123;
45     FormatArgImpl arg_123(i);
46     absl::Span<const FormatArgImpl> args(&arg_123, 1);
47     UntypedFormatSpec format(fmt);
48 
49     EXPECT_TRUE(FormatUntyped(&actual, format, args));
50     char buf[4096]{};
51     snprintf(buf, sizeof(buf), fmt, 123);
52     EXPECT_EQ(
53         str_format_internal::FormatPack(
54             str_format_internal::UntypedFormatSpecImpl::Extract(format), args),
55         buf);
56     EXPECT_EQ(actual, buf);
57   }
58   // The internal version works with a preparsed format.
59   ParsedFormat<'d'> pc("A format %d");
60   int i = 345;
61   FormatArg arg(i);
62   std::string out;
63   EXPECT_TRUE(str_format_internal::FormatUntyped(
64       &out, str_format_internal::UntypedFormatSpecImpl(&pc), {&arg, 1}));
65   EXPECT_EQ("A format 345", out);
66 }
67 
TEST_F(FormatEntryPointTest,StringFormat)68 TEST_F(FormatEntryPointTest, StringFormat) {
69   EXPECT_EQ("123", StrFormat("%d", 123));
70   constexpr absl::string_view view("=%d=", 4);
71   EXPECT_EQ("=123=", StrFormat(view, 123));
72 }
73 
TEST_F(FormatEntryPointTest,AppendFormat)74 TEST_F(FormatEntryPointTest, AppendFormat) {
75   std::string s;
76   std::string& r = StrAppendFormat(&s, "%d", 123);
77   EXPECT_EQ(&s, &r);  // should be same object
78   EXPECT_EQ("123", r);
79 }
80 
TEST_F(FormatEntryPointTest,AppendFormatFail)81 TEST_F(FormatEntryPointTest, AppendFormatFail) {
82   std::string s = "orig";
83 
84   UntypedFormatSpec format(" more %d");
85   FormatArgImpl arg("not an int");
86 
87   EXPECT_EQ("orig",
88             str_format_internal::AppendPack(
89                 &s, str_format_internal::UntypedFormatSpecImpl::Extract(format),
90                 {&arg, 1}));
91 }
92 
93 
TEST_F(FormatEntryPointTest,ManyArgs)94 TEST_F(FormatEntryPointTest, ManyArgs) {
95   EXPECT_EQ("24", StrFormat("%24$d", 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13,
96                             14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24));
97   EXPECT_EQ("60", StrFormat("%60$d", 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13,
98                             14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26,
99                             27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39,
100                             40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52,
101                             53, 54, 55, 56, 57, 58, 59, 60));
102 }
103 
TEST_F(FormatEntryPointTest,Preparsed)104 TEST_F(FormatEntryPointTest, Preparsed) {
105   ParsedFormat<'d'> pc("%d");
106   EXPECT_EQ("123", StrFormat(pc, 123));
107   // rvalue ok?
108   EXPECT_EQ("123", StrFormat(ParsedFormat<'d'>("%d"), 123));
109   constexpr absl::string_view view("=%d=", 4);
110   EXPECT_EQ("=123=", StrFormat(ParsedFormat<'d'>(view), 123));
111 }
112 
TEST_F(FormatEntryPointTest,FormatCountCapture)113 TEST_F(FormatEntryPointTest, FormatCountCapture) {
114   int n = 0;
115   EXPECT_EQ("", StrFormat("%n", FormatCountCapture(&n)));
116   EXPECT_EQ(0, n);
117   EXPECT_EQ("123", StrFormat("%d%n", 123, FormatCountCapture(&n)));
118   EXPECT_EQ(3, n);
119 }
120 
TEST_F(FormatEntryPointTest,FormatCountCaptureWrongType)121 TEST_F(FormatEntryPointTest, FormatCountCaptureWrongType) {
122   // Should reject int*.
123   int n = 0;
124   UntypedFormatSpec format("%d%n");
125   int i = 123, *ip = &n;
126   FormatArgImpl args[2] = {FormatArgImpl(i), FormatArgImpl(ip)};
127 
128   EXPECT_EQ("", str_format_internal::FormatPack(
129                     str_format_internal::UntypedFormatSpecImpl::Extract(format),
130                     absl::MakeSpan(args)));
131 }
132 
TEST_F(FormatEntryPointTest,FormatCountCaptureMultiple)133 TEST_F(FormatEntryPointTest, FormatCountCaptureMultiple) {
134   int n1 = 0;
135   int n2 = 0;
136   EXPECT_EQ("    1         2",
137             StrFormat("%5d%n%10d%n", 1, FormatCountCapture(&n1), 2,
138                       FormatCountCapture(&n2)));
139   EXPECT_EQ(5, n1);
140   EXPECT_EQ(15, n2);
141 }
142 
TEST_F(FormatEntryPointTest,FormatCountCaptureExample)143 TEST_F(FormatEntryPointTest, FormatCountCaptureExample) {
144   int n;
145   std::string s;
146   StrAppendFormat(&s, "%s: %n%s\n", "(1,1)", FormatCountCapture(&n), "(1,2)");
147   StrAppendFormat(&s, "%*s%s\n", n, "", "(2,2)");
148   EXPECT_EQ(7, n);
149   EXPECT_EQ(
150       "(1,1): (1,2)\n"
151       "       (2,2)\n",
152       s);
153 }
154 
TEST_F(FormatEntryPointTest,Stream)155 TEST_F(FormatEntryPointTest, Stream) {
156   const std::string formats[] = {
157     "",
158     "a",
159     "%80d",
160     "%d %u %c %s %f %g",
161 #if !defined(_MSC_VER) && !defined(__ANDROID__) && !defined(__native_client__)
162     // MSVC, NaCL and Android don't support positional syntax.
163     "complicated multipart %% %1$d format %1$080d",
164 #endif  // _MSC_VER
165   };
166   std::string buf(4096, '\0');
167   for (const auto& fmt : formats) {
168     const auto parsed =
169         ParsedFormat<'d', 'u', 'c', 's', 'f', 'g'>::NewAllowIgnored(fmt);
170     std::ostringstream oss;
171     oss << StreamFormat(*parsed, 123, 3, 49, "multistreaming!!!", 1.01, 1.01);
172     int fmt_result = snprintf(&*buf.begin(), buf.size(), fmt.c_str(),  //
173                                  123, 3, 49, "multistreaming!!!", 1.01, 1.01);
174     ASSERT_TRUE(oss) << fmt;
175     ASSERT_TRUE(fmt_result >= 0 && static_cast<size_t>(fmt_result) < buf.size())
176         << fmt_result;
177     EXPECT_EQ(buf.c_str(), oss.str());
178   }
179 }
180 
TEST_F(FormatEntryPointTest,StreamOk)181 TEST_F(FormatEntryPointTest, StreamOk) {
182   std::ostringstream oss;
183   oss << StreamFormat("hello %d", 123);
184   EXPECT_EQ("hello 123", oss.str());
185   EXPECT_TRUE(oss.good());
186 }
187 
TEST_F(FormatEntryPointTest,StreamFail)188 TEST_F(FormatEntryPointTest, StreamFail) {
189   std::ostringstream oss;
190   UntypedFormatSpec format("hello %d");
191   FormatArgImpl arg("non-numeric");
192   oss << str_format_internal::Streamable(
193       str_format_internal::UntypedFormatSpecImpl::Extract(format), {&arg, 1});
194   EXPECT_EQ("hello ", oss.str());  // partial write
195   EXPECT_TRUE(oss.fail());
196 }
197 
WithSnprintf(const char * fmt,...)198 std::string WithSnprintf(const char* fmt, ...) {
199   std::string buf;
200   buf.resize(128);
201   va_list va;
202   va_start(va, fmt);
203   int r = vsnprintf(&*buf.begin(), buf.size(), fmt, va);
204   va_end(va);
205   EXPECT_GE(r, 0);
206   EXPECT_LT(r, buf.size());
207   buf.resize(r);
208   return buf;
209 }
210 
TEST_F(FormatEntryPointTest,FloatPrecisionArg)211 TEST_F(FormatEntryPointTest, FloatPrecisionArg) {
212   // Test that positional parameters for width and precision
213   // are indexed to precede the value.
214   // Also sanity check the same formats against snprintf.
215   EXPECT_EQ("0.1", StrFormat("%.1f", 0.1));
216   EXPECT_EQ("0.1", WithSnprintf("%.1f", 0.1));
217   EXPECT_EQ("  0.1", StrFormat("%*.1f", 5, 0.1));
218   EXPECT_EQ("  0.1", WithSnprintf("%*.1f", 5, 0.1));
219   EXPECT_EQ("0.1", StrFormat("%.*f", 1, 0.1));
220   EXPECT_EQ("0.1", WithSnprintf("%.*f", 1, 0.1));
221   EXPECT_EQ("  0.1", StrFormat("%*.*f", 5, 1, 0.1));
222   EXPECT_EQ("  0.1", WithSnprintf("%*.*f", 5, 1, 0.1));
223 }
224 namespace streamed_test {
225 struct X {};
operator <<(std::ostream & os,const X &)226 std::ostream& operator<<(std::ostream& os, const X&) {
227   return os << "X";
228 }
229 }  // streamed_test
230 
TEST_F(FormatEntryPointTest,FormatStreamed)231 TEST_F(FormatEntryPointTest, FormatStreamed) {
232   EXPECT_EQ("123", StrFormat("%s", FormatStreamed(123)));
233   EXPECT_EQ("  123", StrFormat("%5s", FormatStreamed(123)));
234   EXPECT_EQ("123  ", StrFormat("%-5s", FormatStreamed(123)));
235   EXPECT_EQ("X", StrFormat("%s", FormatStreamed(streamed_test::X())));
236   EXPECT_EQ("123", StrFormat("%s", FormatStreamed(StreamFormat("%d", 123))));
237 }
238 
239 // Helper class that creates a temporary file and exposes a FILE* to it.
240 // It will close the file on destruction.
241 class TempFile {
242  public:
TempFile()243   TempFile() : file_(std::tmpfile()) {}
~TempFile()244   ~TempFile() { std::fclose(file_); }
245 
file() const246   std::FILE* file() const { return file_; }
247 
248   // Read the file into a string.
ReadFile()249   std::string ReadFile() {
250     std::fseek(file_, 0, SEEK_END);
251     int size = std::ftell(file_);
252     EXPECT_GT(size, 0);
253     std::rewind(file_);
254     std::string str(2 * size, ' ');
255     int read_bytes = std::fread(&str[0], 1, str.size(), file_);
256     EXPECT_EQ(read_bytes, size);
257     str.resize(read_bytes);
258     EXPECT_TRUE(std::feof(file_));
259     return str;
260   }
261 
262  private:
263   std::FILE* file_;
264 };
265 
TEST_F(FormatEntryPointTest,FPrintF)266 TEST_F(FormatEntryPointTest, FPrintF) {
267   TempFile tmp;
268   int result =
269       FPrintF(tmp.file(), "STRING: %s NUMBER: %010d", std::string("ABC"), -19);
270   EXPECT_EQ(result, 30);
271   EXPECT_EQ(tmp.ReadFile(), "STRING: ABC NUMBER: -000000019");
272 }
273 
TEST_F(FormatEntryPointTest,FPrintFError)274 TEST_F(FormatEntryPointTest, FPrintFError) {
275   errno = 0;
276   int result = FPrintF(stdin, "ABC");
277   EXPECT_LT(result, 0);
278   EXPECT_EQ(errno, EBADF);
279 }
280 
281 #ifdef __GLIBC__
TEST_F(FormatEntryPointTest,FprintfTooLarge)282 TEST_F(FormatEntryPointTest, FprintfTooLarge) {
283   std::FILE* f = std::fopen("/dev/null", "w");
284   int width = 2000000000;
285   errno = 0;
286   int result = FPrintF(f, "%*d %*d", width, 0, width, 0);
287   EXPECT_LT(result, 0);
288   EXPECT_EQ(errno, EFBIG);
289   std::fclose(f);
290 }
291 
TEST_F(FormatEntryPointTest,PrintF)292 TEST_F(FormatEntryPointTest, PrintF) {
293   int stdout_tmp = dup(STDOUT_FILENO);
294 
295   TempFile tmp;
296   std::fflush(stdout);
297   dup2(fileno(tmp.file()), STDOUT_FILENO);
298 
299   int result = PrintF("STRING: %s NUMBER: %010d", std::string("ABC"), -19);
300 
301   std::fflush(stdout);
302   dup2(stdout_tmp, STDOUT_FILENO);
303   close(stdout_tmp);
304 
305   EXPECT_EQ(result, 30);
306   EXPECT_EQ(tmp.ReadFile(), "STRING: ABC NUMBER: -000000019");
307 }
308 #endif  // __GLIBC__
309 
TEST_F(FormatEntryPointTest,SNPrintF)310 TEST_F(FormatEntryPointTest, SNPrintF) {
311   char buffer[16];
312   int result =
313       SNPrintF(buffer, sizeof(buffer), "STRING: %s", std::string("ABC"));
314   EXPECT_EQ(result, 11);
315   EXPECT_EQ(std::string(buffer), "STRING: ABC");
316 
317   result = SNPrintF(buffer, sizeof(buffer), "NUMBER: %d", 123456);
318   EXPECT_EQ(result, 14);
319   EXPECT_EQ(std::string(buffer), "NUMBER: 123456");
320 
321   result = SNPrintF(buffer, sizeof(buffer), "NUMBER: %d", 1234567);
322   EXPECT_EQ(result, 15);
323   EXPECT_EQ(std::string(buffer), "NUMBER: 1234567");
324 
325   result = SNPrintF(buffer, sizeof(buffer), "NUMBER: %d", 12345678);
326   EXPECT_EQ(result, 16);
327   EXPECT_EQ(std::string(buffer), "NUMBER: 1234567");
328 
329   result = SNPrintF(buffer, sizeof(buffer), "NUMBER: %d", 123456789);
330   EXPECT_EQ(result, 17);
331   EXPECT_EQ(std::string(buffer), "NUMBER: 1234567");
332 
333   result = SNPrintF(nullptr, 0, "Just checking the %s of the output.", "size");
334   EXPECT_EQ(result, 37);
335 }
336 
TEST(StrFormat,BehavesAsDocumented)337 TEST(StrFormat, BehavesAsDocumented) {
338   std::string s = absl::StrFormat("%s, %d!", "Hello", 123);
339   EXPECT_EQ("Hello, 123!", s);
340   // The format of a replacement is
341   // '%'[position][flags][width['.'precision]][length_modifier][format]
342   EXPECT_EQ(absl::StrFormat("%1$+3.2Lf", 1.1), "+1.10");
343   // Text conversion:
344   //     "c" - Character.              Eg: 'a' -> "A", 20 -> " "
345   EXPECT_EQ(StrFormat("%c", 'a'), "a");
346   EXPECT_EQ(StrFormat("%c", 0x20), " ");
347   //           Formats char and integral types: int, long, uint64_t, etc.
348   EXPECT_EQ(StrFormat("%c", int{'a'}), "a");
349   EXPECT_EQ(StrFormat("%c", long{'a'}), "a");  // NOLINT
350   EXPECT_EQ(StrFormat("%c", uint64_t{'a'}), "a");
351   //     "s" - string       Eg: "C" -> "C", std::string("C++") -> "C++"
352   //           Formats std::string, char*, string_view, and Cord.
353   EXPECT_EQ(StrFormat("%s", "C"), "C");
354   EXPECT_EQ(StrFormat("%s", std::string("C++")), "C++");
355   EXPECT_EQ(StrFormat("%s", string_view("view")), "view");
356   EXPECT_EQ(StrFormat("%s", absl::Cord("cord")), "cord");
357   // Integral Conversion
358   //     These format integral types: char, int, long, uint64_t, etc.
359   EXPECT_EQ(StrFormat("%d", char{10}), "10");
360   EXPECT_EQ(StrFormat("%d", int{10}), "10");
361   EXPECT_EQ(StrFormat("%d", long{10}), "10");  // NOLINT
362   EXPECT_EQ(StrFormat("%d", uint64_t{10}), "10");
363   //     d,i - signed decimal          Eg: -10 -> "-10"
364   EXPECT_EQ(StrFormat("%d", -10), "-10");
365   EXPECT_EQ(StrFormat("%i", -10), "-10");
366   //      o  - octal                   Eg:  10 -> "12"
367   EXPECT_EQ(StrFormat("%o", 10), "12");
368   //      u  - unsigned decimal        Eg:  10 -> "10"
369   EXPECT_EQ(StrFormat("%u", 10), "10");
370   //     x/X - lower,upper case hex    Eg:  10 -> "a"/"A"
371   EXPECT_EQ(StrFormat("%x", 10), "a");
372   EXPECT_EQ(StrFormat("%X", 10), "A");
373   // Floating-point, with upper/lower-case output.
374   //     These format floating points types: float, double, long double, etc.
375   EXPECT_EQ(StrFormat("%.1f", float{1}), "1.0");
376   EXPECT_EQ(StrFormat("%.1f", double{1}), "1.0");
377   const long double long_double = 1.0;
378   EXPECT_EQ(StrFormat("%.1f", long_double), "1.0");
379   //     These also format integral types: char, int, long, uint64_t, etc.:
380   EXPECT_EQ(StrFormat("%.1f", char{1}), "1.0");
381   EXPECT_EQ(StrFormat("%.1f", int{1}), "1.0");
382   EXPECT_EQ(StrFormat("%.1f", long{1}), "1.0");  // NOLINT
383   EXPECT_EQ(StrFormat("%.1f", uint64_t{1}), "1.0");
384   //     f/F - decimal.                Eg: 123456789 -> "123456789.000000"
385   EXPECT_EQ(StrFormat("%f", 123456789), "123456789.000000");
386   EXPECT_EQ(StrFormat("%F", 123456789), "123456789.000000");
387   //     e/E - exponentiated           Eg: .01 -> "1.00000e-2"/"1.00000E-2"
388   EXPECT_EQ(StrFormat("%e", .01), "1.000000e-02");
389   EXPECT_EQ(StrFormat("%E", .01), "1.000000E-02");
390   //     g/G - exponentiate to fit     Eg: .01 -> "0.01", 1e10 ->"1e+10"/"1E+10"
391   EXPECT_EQ(StrFormat("%g", .01), "0.01");
392   EXPECT_EQ(StrFormat("%g", 1e10), "1e+10");
393   EXPECT_EQ(StrFormat("%G", 1e10), "1E+10");
394   //     a/A - lower,upper case hex    Eg: -3.0 -> "-0x1.8p+1"/"-0X1.8P+1"
395 
396 // On Android platform <=21, there is a regression in hexfloat formatting.
397 #if !defined(__ANDROID_API__) || __ANDROID_API__ > 21
398   EXPECT_EQ(StrFormat("%.1a", -3.0), "-0x1.8p+1");  // .1 to fix MSVC output
399   EXPECT_EQ(StrFormat("%.1A", -3.0), "-0X1.8P+1");  // .1 to fix MSVC output
400 #endif
401 
402   // Other conversion
403   int64_t value = 0x7ffdeb4;
404   auto ptr_value = static_cast<uintptr_t>(value);
405   const int& something = *reinterpret_cast<const int*>(ptr_value);
406   EXPECT_EQ(StrFormat("%p", &something), StrFormat("0x%x", ptr_value));
407 
408   // Output widths are supported, with optional flags.
409   EXPECT_EQ(StrFormat("%3d", 1), "  1");
410   EXPECT_EQ(StrFormat("%3d", 123456), "123456");
411   EXPECT_EQ(StrFormat("%06.2f", 1.234), "001.23");
412   EXPECT_EQ(StrFormat("%+d", 1), "+1");
413   EXPECT_EQ(StrFormat("% d", 1), " 1");
414   EXPECT_EQ(StrFormat("%-4d", -1), "-1  ");
415   EXPECT_EQ(StrFormat("%#o", 10), "012");
416   EXPECT_EQ(StrFormat("%#x", 15), "0xf");
417   EXPECT_EQ(StrFormat("%04d", 8), "0008");
418   // Posix positional substitution.
419   EXPECT_EQ(absl::StrFormat("%2$s, %3$s, %1$s!", "vici", "veni", "vidi"),
420             "veni, vidi, vici!");
421   // Length modifiers are ignored.
422   EXPECT_EQ(StrFormat("%hhd", int{1}), "1");
423   EXPECT_EQ(StrFormat("%hd", int{1}), "1");
424   EXPECT_EQ(StrFormat("%ld", int{1}), "1");
425   EXPECT_EQ(StrFormat("%lld", int{1}), "1");
426   EXPECT_EQ(StrFormat("%Ld", int{1}), "1");
427   EXPECT_EQ(StrFormat("%jd", int{1}), "1");
428   EXPECT_EQ(StrFormat("%zd", int{1}), "1");
429   EXPECT_EQ(StrFormat("%td", int{1}), "1");
430   EXPECT_EQ(StrFormat("%qd", int{1}), "1");
431 }
432 
433 using str_format_internal::ExtendedParsedFormat;
434 using str_format_internal::ParsedFormatBase;
435 
436 struct SummarizeConsumer {
437   std::string* out;
SummarizeConsumerabsl::__anon9e07202f0111::SummarizeConsumer438   explicit SummarizeConsumer(std::string* out) : out(out) {}
439 
Appendabsl::__anon9e07202f0111::SummarizeConsumer440   bool Append(string_view s) {
441     *out += "[" + std::string(s) + "]";
442     return true;
443   }
444 
ConvertOneabsl::__anon9e07202f0111::SummarizeConsumer445   bool ConvertOne(const str_format_internal::UnboundConversion& conv,
446                   string_view s) {
447     *out += "{";
448     *out += std::string(s);
449     *out += ":";
450     *out += std::to_string(conv.arg_position) + "$";
451     if (conv.width.is_from_arg()) {
452       *out += std::to_string(conv.width.get_from_arg()) + "$*";
453     }
454     if (conv.precision.is_from_arg()) {
455       *out += "." + std::to_string(conv.precision.get_from_arg()) + "$*";
456     }
457     *out += str_format_internal::FormatConversionCharToChar(conv.conv);
458     *out += "}";
459     return true;
460   }
461 };
462 
SummarizeParsedFormat(const ParsedFormatBase & pc)463 std::string SummarizeParsedFormat(const ParsedFormatBase& pc) {
464   std::string out;
465   if (!pc.ProcessFormat(SummarizeConsumer(&out))) out += "!";
466   return out;
467 }
468 
469 using ParsedFormatTest = ::testing::Test;
470 
TEST_F(ParsedFormatTest,SimpleChecked)471 TEST_F(ParsedFormatTest, SimpleChecked) {
472   EXPECT_EQ("[ABC]{d:1$d}[DEF]",
473             SummarizeParsedFormat(ParsedFormat<'d'>("ABC%dDEF")));
474   EXPECT_EQ("{s:1$s}[FFF]{d:2$d}[ZZZ]{f:3$f}",
475             SummarizeParsedFormat(ParsedFormat<'s', 'd', 'f'>("%sFFF%dZZZ%f")));
476   EXPECT_EQ("{s:1$s}[ ]{.*d:3$.2$*d}",
477             SummarizeParsedFormat(ParsedFormat<'s', '*', 'd'>("%s %.*d")));
478 }
479 
TEST_F(ParsedFormatTest,SimpleUncheckedCorrect)480 TEST_F(ParsedFormatTest, SimpleUncheckedCorrect) {
481   auto f = ParsedFormat<'d'>::New("ABC%dDEF");
482   ASSERT_TRUE(f);
483   EXPECT_EQ("[ABC]{d:1$d}[DEF]", SummarizeParsedFormat(*f));
484 
485   std::string format = "%sFFF%dZZZ%f";
486   auto f2 = ParsedFormat<'s', 'd', 'f'>::New(format);
487 
488   ASSERT_TRUE(f2);
489   EXPECT_EQ("{s:1$s}[FFF]{d:2$d}[ZZZ]{f:3$f}", SummarizeParsedFormat(*f2));
490 
491   f2 = ParsedFormat<'s', 'd', 'f'>::New("%s %d %f");
492 
493   ASSERT_TRUE(f2);
494   EXPECT_EQ("{s:1$s}[ ]{d:2$d}[ ]{f:3$f}", SummarizeParsedFormat(*f2));
495 
496   auto star = ParsedFormat<'*', 'd'>::New("%*d");
497   ASSERT_TRUE(star);
498   EXPECT_EQ("{*d:2$1$*d}", SummarizeParsedFormat(*star));
499 
500   auto dollar = ParsedFormat<'d', 's'>::New("%2$s %1$d");
501   ASSERT_TRUE(dollar);
502   EXPECT_EQ("{2$s:2$s}[ ]{1$d:1$d}", SummarizeParsedFormat(*dollar));
503   // with reuse
504   dollar = ParsedFormat<'d', 's'>::New("%2$s %1$d %1$d");
505   ASSERT_TRUE(dollar);
506   EXPECT_EQ("{2$s:2$s}[ ]{1$d:1$d}[ ]{1$d:1$d}",
507             SummarizeParsedFormat(*dollar));
508 }
509 
TEST_F(ParsedFormatTest,SimpleUncheckedIgnoredArgs)510 TEST_F(ParsedFormatTest, SimpleUncheckedIgnoredArgs) {
511   EXPECT_FALSE((ParsedFormat<'d', 's'>::New("ABC")));
512   EXPECT_FALSE((ParsedFormat<'d', 's'>::New("%dABC")));
513   EXPECT_FALSE((ParsedFormat<'d', 's'>::New("ABC%2$s")));
514   auto f = ParsedFormat<'d', 's'>::NewAllowIgnored("ABC");
515   ASSERT_TRUE(f);
516   EXPECT_EQ("[ABC]", SummarizeParsedFormat(*f));
517   f = ParsedFormat<'d', 's'>::NewAllowIgnored("%dABC");
518   ASSERT_TRUE(f);
519   EXPECT_EQ("{d:1$d}[ABC]", SummarizeParsedFormat(*f));
520   f = ParsedFormat<'d', 's'>::NewAllowIgnored("ABC%2$s");
521   ASSERT_TRUE(f);
522   EXPECT_EQ("[ABC]{2$s:2$s}", SummarizeParsedFormat(*f));
523 }
524 
TEST_F(ParsedFormatTest,SimpleUncheckedUnsupported)525 TEST_F(ParsedFormatTest, SimpleUncheckedUnsupported) {
526   EXPECT_FALSE(ParsedFormat<'d'>::New("%1$d %1$x"));
527   EXPECT_FALSE(ParsedFormat<'x'>::New("%1$d %1$x"));
528 }
529 
TEST_F(ParsedFormatTest,SimpleUncheckedIncorrect)530 TEST_F(ParsedFormatTest, SimpleUncheckedIncorrect) {
531   EXPECT_FALSE(ParsedFormat<'d'>::New(""));
532 
533   EXPECT_FALSE(ParsedFormat<'d'>::New("ABC%dDEF%d"));
534 
535   std::string format = "%sFFF%dZZZ%f";
536   EXPECT_FALSE((ParsedFormat<'s', 'd', 'g'>::New(format)));
537 }
538 
539 #if defined(__cpp_nontype_template_parameter_auto)
540 
541 template <auto T>
542 std::true_type IsValidParsedFormatArgTest(ParsedFormat<T>*);
543 
544 template <auto T>
545 std::false_type IsValidParsedFormatArgTest(...);
546 
547 template <auto T>
548 using IsValidParsedFormatArg = decltype(IsValidParsedFormatArgTest<T>(nullptr));
549 
TEST_F(ParsedFormatTest,OnlyValidTypesAllowed)550 TEST_F(ParsedFormatTest, OnlyValidTypesAllowed) {
551   ASSERT_TRUE(IsValidParsedFormatArg<'c'>::value);
552 
553   ASSERT_TRUE(IsValidParsedFormatArg<FormatConversionCharSet::d>::value);
554 
555   ASSERT_TRUE(IsValidParsedFormatArg<absl::FormatConversionCharSet::d |
556                                      absl::FormatConversionCharSet::x>::value);
557   ASSERT_TRUE(
558       IsValidParsedFormatArg<absl::FormatConversionCharSet::kIntegral>::value);
559 
560   // This is an easy mistake to make, however, this will reduce to an integer
561   // which has no meaning, so we need to ensure it doesn't compile.
562   ASSERT_FALSE(IsValidParsedFormatArg<'x' | 'd'>::value);
563 
564   // For now, we disallow construction based on ConversionChar (rather than
565   // CharSet)
566   ASSERT_FALSE(IsValidParsedFormatArg<absl::FormatConversionChar::d>::value);
567 }
568 
TEST_F(ParsedFormatTest,ExtendedTyping)569 TEST_F(ParsedFormatTest, ExtendedTyping) {
570   EXPECT_FALSE(ParsedFormat<FormatConversionCharSet::d>::New(""));
571   ASSERT_TRUE(ParsedFormat<absl::FormatConversionCharSet::d>::New("%d"));
572   auto v1 = ParsedFormat<'d', absl::FormatConversionCharSet::s>::New("%d%s");
573   ASSERT_TRUE(v1);
574   auto v2 = ParsedFormat<absl::FormatConversionCharSet::d, 's'>::New("%d%s");
575   ASSERT_TRUE(v2);
576   auto v3 = ParsedFormat<absl::FormatConversionCharSet::d |
577                              absl::FormatConversionCharSet::s,
578                          's'>::New("%d%s");
579   ASSERT_TRUE(v3);
580   auto v4 = ParsedFormat<absl::FormatConversionCharSet::d |
581                              absl::FormatConversionCharSet::s,
582                          's'>::New("%s%s");
583   ASSERT_TRUE(v4);
584 }
585 #endif
586 
TEST_F(ParsedFormatTest,UncheckedCorrect)587 TEST_F(ParsedFormatTest, UncheckedCorrect) {
588   auto f =
589       ExtendedParsedFormat<absl::FormatConversionCharSet::d>::New("ABC%dDEF");
590   ASSERT_TRUE(f);
591   EXPECT_EQ("[ABC]{d:1$d}[DEF]", SummarizeParsedFormat(*f));
592 
593   std::string format = "%sFFF%dZZZ%f";
594   auto f2 = ExtendedParsedFormat<
595       absl::FormatConversionCharSet::kString, absl::FormatConversionCharSet::d,
596       absl::FormatConversionCharSet::kFloating>::New(format);
597 
598   ASSERT_TRUE(f2);
599   EXPECT_EQ("{s:1$s}[FFF]{d:2$d}[ZZZ]{f:3$f}", SummarizeParsedFormat(*f2));
600 
601   f2 = ExtendedParsedFormat<
602       absl::FormatConversionCharSet::kString, absl::FormatConversionCharSet::d,
603       absl::FormatConversionCharSet::kFloating>::New("%s %d %f");
604 
605   ASSERT_TRUE(f2);
606   EXPECT_EQ("{s:1$s}[ ]{d:2$d}[ ]{f:3$f}", SummarizeParsedFormat(*f2));
607 
608   auto star =
609       ExtendedParsedFormat<absl::FormatConversionCharSet::kStar,
610                            absl::FormatConversionCharSet::d>::New("%*d");
611   ASSERT_TRUE(star);
612   EXPECT_EQ("{*d:2$1$*d}", SummarizeParsedFormat(*star));
613 
614   auto dollar =
615       ExtendedParsedFormat<absl::FormatConversionCharSet::d,
616                            absl::FormatConversionCharSet::s>::New("%2$s %1$d");
617   ASSERT_TRUE(dollar);
618   EXPECT_EQ("{2$s:2$s}[ ]{1$d:1$d}", SummarizeParsedFormat(*dollar));
619   // with reuse
620   dollar = ExtendedParsedFormat<
621       absl::FormatConversionCharSet::d,
622       absl::FormatConversionCharSet::s>::New("%2$s %1$d %1$d");
623   ASSERT_TRUE(dollar);
624   EXPECT_EQ("{2$s:2$s}[ ]{1$d:1$d}[ ]{1$d:1$d}",
625             SummarizeParsedFormat(*dollar));
626 }
627 
TEST_F(ParsedFormatTest,UncheckedIgnoredArgs)628 TEST_F(ParsedFormatTest, UncheckedIgnoredArgs) {
629   EXPECT_FALSE(
630       (ExtendedParsedFormat<absl::FormatConversionCharSet::d,
631                             absl::FormatConversionCharSet::s>::New("ABC")));
632   EXPECT_FALSE(
633       (ExtendedParsedFormat<absl::FormatConversionCharSet::d,
634                             absl::FormatConversionCharSet::s>::New("%dABC")));
635   EXPECT_FALSE(
636       (ExtendedParsedFormat<absl::FormatConversionCharSet::d,
637                             absl::FormatConversionCharSet::s>::New("ABC%2$s")));
638   auto f = ExtendedParsedFormat<
639       absl::FormatConversionCharSet::d,
640       absl::FormatConversionCharSet::s>::NewAllowIgnored("ABC");
641   ASSERT_TRUE(f);
642   EXPECT_EQ("[ABC]", SummarizeParsedFormat(*f));
643   f = ExtendedParsedFormat<
644       absl::FormatConversionCharSet::d,
645       absl::FormatConversionCharSet::s>::NewAllowIgnored("%dABC");
646   ASSERT_TRUE(f);
647   EXPECT_EQ("{d:1$d}[ABC]", SummarizeParsedFormat(*f));
648   f = ExtendedParsedFormat<
649       absl::FormatConversionCharSet::d,
650       absl::FormatConversionCharSet::s>::NewAllowIgnored("ABC%2$s");
651   ASSERT_TRUE(f);
652   EXPECT_EQ("[ABC]{2$s:2$s}", SummarizeParsedFormat(*f));
653 }
654 
TEST_F(ParsedFormatTest,UncheckedMultipleTypes)655 TEST_F(ParsedFormatTest, UncheckedMultipleTypes) {
656   auto dx =
657       ExtendedParsedFormat<absl::FormatConversionCharSet::d |
658                            absl::FormatConversionCharSet::x>::New("%1$d %1$x");
659   EXPECT_TRUE(dx);
660   EXPECT_EQ("{1$d:1$d}[ ]{1$x:1$x}", SummarizeParsedFormat(*dx));
661 
662   dx = ExtendedParsedFormat<absl::FormatConversionCharSet::d |
663                             absl::FormatConversionCharSet::x>::New("%1$d");
664   EXPECT_TRUE(dx);
665   EXPECT_EQ("{1$d:1$d}", SummarizeParsedFormat(*dx));
666 }
667 
TEST_F(ParsedFormatTest,UncheckedIncorrect)668 TEST_F(ParsedFormatTest, UncheckedIncorrect) {
669   EXPECT_FALSE(ExtendedParsedFormat<absl::FormatConversionCharSet::d>::New(""));
670 
671   EXPECT_FALSE(ExtendedParsedFormat<absl::FormatConversionCharSet::d>::New(
672       "ABC%dDEF%d"));
673 
674   std::string format = "%sFFF%dZZZ%f";
675   EXPECT_FALSE(
676       (ExtendedParsedFormat<absl::FormatConversionCharSet::s,
677                             absl::FormatConversionCharSet::d,
678                             absl::FormatConversionCharSet::g>::New(format)));
679 }
680 
TEST_F(ParsedFormatTest,RegressionMixPositional)681 TEST_F(ParsedFormatTest, RegressionMixPositional) {
682   EXPECT_FALSE(
683       (ExtendedParsedFormat<absl::FormatConversionCharSet::d,
684                             absl::FormatConversionCharSet::o>::New("%1$d %o")));
685 }
686 
687 using FormatWrapperTest = ::testing::Test;
688 
689 // Plain wrapper for StrFormat.
690 template <typename... Args>
WrappedFormat(const absl::FormatSpec<Args...> & format,const Args &...args)691 std::string WrappedFormat(const absl::FormatSpec<Args...>& format,
692                           const Args&... args) {
693   return StrFormat(format, args...);
694 }
695 
TEST_F(FormatWrapperTest,ConstexprStringFormat)696 TEST_F(FormatWrapperTest, ConstexprStringFormat) {
697   EXPECT_EQ(WrappedFormat("%s there", "hello"), "hello there");
698 }
699 
TEST_F(FormatWrapperTest,ParsedFormat)700 TEST_F(FormatWrapperTest, ParsedFormat) {
701   ParsedFormat<'s'> format("%s there");
702   EXPECT_EQ(WrappedFormat(format, "hello"), "hello there");
703 }
704 
705 }  // namespace
706 ABSL_NAMESPACE_END
707 }  // namespace absl
708 
709 using FormatExtensionTest = ::testing::Test;
710 
711 struct Point {
712   friend absl::FormatConvertResult<absl::FormatConversionCharSet::kString |
713                                    absl::FormatConversionCharSet::kIntegral>
AbslFormatConvert(const Point & p,const absl::FormatConversionSpec & spec,absl::FormatSink * s)714   AbslFormatConvert(const Point& p, const absl::FormatConversionSpec& spec,
715                     absl::FormatSink* s) {
716     if (spec.conversion_char() == absl::FormatConversionChar::s) {
717       s->Append(absl::StrCat("x=", p.x, " y=", p.y));
718     } else {
719       s->Append(absl::StrCat(p.x, ",", p.y));
720     }
721     return {true};
722   }
723 
724   int x = 10;
725   int y = 20;
726 };
727 
TEST_F(FormatExtensionTest,AbslFormatConvertExample)728 TEST_F(FormatExtensionTest, AbslFormatConvertExample) {
729   Point p;
730   EXPECT_EQ(absl::StrFormat("a %s z", p), "a x=10 y=20 z");
731   EXPECT_EQ(absl::StrFormat("a %d z", p), "a 10,20 z");
732 
733   // Typed formatting will fail to compile an invalid format.
734   // StrFormat("%f", p);  // Does not compile.
735   std::string actual;
736   absl::UntypedFormatSpec f1("%f");
737   // FormatUntyped will return false for bad character.
738   EXPECT_FALSE(absl::FormatUntyped(&actual, f1, {absl::FormatArg(p)}));
739 }
740 
741 // Some codegen thunks that we can use to easily dump the generated assembly for
742 // different StrFormat calls.
743 
CodegenAbslStrFormatInt(int i)744 std::string CodegenAbslStrFormatInt(int i) {  // NOLINT
745   return absl::StrFormat("%d", i);
746 }
747 
CodegenAbslStrFormatIntStringInt64(int i,const std::string & s,int64_t i64)748 std::string CodegenAbslStrFormatIntStringInt64(int i, const std::string& s,
749                                                int64_t i64) {  // NOLINT
750   return absl::StrFormat("%d %s %d", i, s, i64);
751 }
752 
CodegenAbslStrAppendFormatInt(std::string * out,int i)753 void CodegenAbslStrAppendFormatInt(std::string* out, int i) {  // NOLINT
754   absl::StrAppendFormat(out, "%d", i);
755 }
756 
CodegenAbslStrAppendFormatIntStringInt64(std::string * out,int i,const std::string & s,int64_t i64)757 void CodegenAbslStrAppendFormatIntStringInt64(std::string* out, int i,
758                                               const std::string& s,
759                                               int64_t i64) {  // NOLINT
760   absl::StrAppendFormat(out, "%d %s %d", i, s, i64);
761 }
762