bind_test.cc
Go to the documentation of this file.
00001 #include "absl/strings/internal/str_format/bind.h"
00002 
00003 #include <string.h>
00004 #include <limits>
00005 
00006 #include "gtest/gtest.h"
00007 
00008 namespace absl {
00009 namespace str_format_internal {
00010 namespace {
00011 
00012 class FormatBindTest : public ::testing::Test {
00013  public:
00014   bool Extract(const char *s, UnboundConversion *props, int *next) const {
00015     return ConsumeUnboundConversion(s, s + strlen(s), props, next) ==
00016            s + strlen(s);
00017   }
00018 };
00019 
00020 TEST_F(FormatBindTest, BindSingle) {
00021   struct Expectation {
00022     int line;
00023     const char *fmt;
00024     int ok_phases;
00025     const FormatArgImpl *arg;
00026     int width;
00027     int precision;
00028     int next_arg;
00029   };
00030   const int no = -1;
00031   const int ia[] = { 10, 20, 30, 40};
00032   const FormatArgImpl args[] = {FormatArgImpl(ia[0]), FormatArgImpl(ia[1]),
00033                                 FormatArgImpl(ia[2]), FormatArgImpl(ia[3])};
00034 #pragma GCC diagnostic push
00035 #pragma GCC diagnostic ignored "-Wmissing-field-initializers"
00036   const Expectation kExpect[] = {
00037     {__LINE__, "d",          2, &args[0], no, no, 2},
00038     {__LINE__, "4d",         2, &args[0],  4, no, 2},
00039     {__LINE__, ".5d",        2, &args[0], no,  5, 2},
00040     {__LINE__, "4.5d",       2, &args[0],  4,  5, 2},
00041     {__LINE__, "*d",         2, &args[1], 10, no, 3},
00042     {__LINE__, ".*d",        2, &args[1], no, 10, 3},
00043     {__LINE__, "*.*d",       2, &args[2], 10, 20, 4},
00044     {__LINE__, "1$d",        2, &args[0], no, no, 0},
00045     {__LINE__, "2$d",        2, &args[1], no, no, 0},
00046     {__LINE__, "3$d",        2, &args[2], no, no, 0},
00047     {__LINE__, "4$d",        2, &args[3], no, no, 0},
00048     {__LINE__, "2$*1$d",     2, &args[1], 10, no, 0},
00049     {__LINE__, "2$*2$d",     2, &args[1], 20, no, 0},
00050     {__LINE__, "2$*3$d",     2, &args[1], 30, no, 0},
00051     {__LINE__, "2$.*1$d",    2, &args[1], no, 10, 0},
00052     {__LINE__, "2$.*2$d",    2, &args[1], no, 20, 0},
00053     {__LINE__, "2$.*3$d",    2, &args[1], no, 30, 0},
00054     {__LINE__, "2$*3$.*1$d", 2, &args[1], 30, 10, 0},
00055     {__LINE__, "2$*2$.*2$d", 2, &args[1], 20, 20, 0},
00056     {__LINE__, "2$*1$.*3$d", 2, &args[1], 10, 30, 0},
00057     {__LINE__, "2$*3$.*1$d", 2, &args[1], 30, 10, 0},
00058     {__LINE__, "1$*d",       0},  // indexed, then positional
00059     {__LINE__, "*2$d",       0},  // positional, then indexed
00060     {__LINE__, "6$d",        1},  // arg position out of bounds
00061     {__LINE__, "1$6$d",      0},  // width position incorrectly specified
00062     {__LINE__, "1$.6$d",     0},  // precision position incorrectly specified
00063     {__LINE__, "1$*6$d",     1},  // width position out of bounds
00064     {__LINE__, "1$.*6$d",    1},  // precision position out of bounds
00065   };
00066 #pragma GCC diagnostic pop
00067   for (const Expectation &e : kExpect) {
00068     SCOPED_TRACE(e.line);
00069     SCOPED_TRACE(e.fmt);
00070     UnboundConversion props;
00071     BoundConversion bound;
00072     int ok_phases = 0;
00073     int next = 0;
00074     if (Extract(e.fmt, &props, &next)) {
00075       ++ok_phases;
00076       if (BindWithPack(&props, args, &bound)) {
00077         ++ok_phases;
00078       }
00079     }
00080     EXPECT_EQ(e.ok_phases, ok_phases);
00081     if (e.ok_phases < 2) continue;
00082     if (e.arg != nullptr) {
00083       EXPECT_EQ(e.arg, bound.arg());
00084     }
00085     EXPECT_EQ(e.width, bound.width());
00086     EXPECT_EQ(e.precision, bound.precision());
00087   }
00088 }
00089 
00090 TEST_F(FormatBindTest, WidthUnderflowRegression) {
00091   UnboundConversion props;
00092   BoundConversion bound;
00093   int next = 0;
00094   const int args_i[] = {std::numeric_limits<int>::min(), 17};
00095   const FormatArgImpl args[] = {FormatArgImpl(args_i[0]),
00096                                 FormatArgImpl(args_i[1])};
00097   ASSERT_TRUE(Extract("*d", &props, &next));
00098   ASSERT_TRUE(BindWithPack(&props, args, &bound));
00099 
00100   EXPECT_EQ(bound.width(), std::numeric_limits<int>::max());
00101   EXPECT_EQ(bound.arg(), args + 1);
00102 }
00103 
00104 TEST_F(FormatBindTest, FormatPack) {
00105   struct Expectation {
00106     int line;
00107     const char *fmt;
00108     const char *summary;
00109   };
00110   const int ia[] = { 10, 20, 30, 40, -10 };
00111   const FormatArgImpl args[] = {FormatArgImpl(ia[0]), FormatArgImpl(ia[1]),
00112                                 FormatArgImpl(ia[2]), FormatArgImpl(ia[3]),
00113                                 FormatArgImpl(ia[4])};
00114   const Expectation kExpect[] = {
00115     {__LINE__, "a%4db%dc", "a{10:4d}b{20:d}c"},
00116     {__LINE__, "a%.4db%dc", "a{10:.4d}b{20:d}c"},
00117     {__LINE__, "a%4.5db%dc", "a{10:4.5d}b{20:d}c"},
00118     {__LINE__, "a%db%4.5dc", "a{10:d}b{20:4.5d}c"},
00119     {__LINE__, "a%db%*.*dc", "a{10:d}b{40:20.30d}c"},
00120     {__LINE__, "a%.*fb", "a{20:.10f}b"},
00121     {__LINE__, "a%1$db%2$*3$.*4$dc", "a{10:d}b{20:30.40d}c"},
00122     {__LINE__, "a%4$db%3$*2$.*1$dc", "a{40:d}b{30:20.10d}c"},
00123     {__LINE__, "a%04ldb", "a{10:04ld}b"},
00124     {__LINE__, "a%-#04lldb", "a{10:-#04lld}b"},
00125     {__LINE__, "a%1$*5$db", "a{10:-10d}b"},
00126     {__LINE__, "a%1$.*5$db", "a{10:d}b"},
00127   };
00128   for (const Expectation &e : kExpect) {
00129     absl::string_view fmt = e.fmt;
00130     SCOPED_TRACE(e.line);
00131     SCOPED_TRACE(e.fmt);
00132     UntypedFormatSpecImpl format(fmt);
00133     EXPECT_EQ(e.summary,
00134               str_format_internal::Summarize(format, absl::MakeSpan(args)))
00135         << "line:" << e.line;
00136   }
00137 }
00138 
00139 }  // namespace
00140 }  // namespace str_format_internal
00141 }  // namespace absl


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