bind_test.cc
Go to the documentation of this file.
2 
3 #include <string.h>
4 #include <limits>
5 
6 #include "gtest/gtest.h"
7 
8 namespace absl {
9 namespace str_format_internal {
10 namespace {
11 
12 class FormatBindTest : public ::testing::Test {
13  public:
14  bool Extract(const char *s, UnboundConversion *props, int *next) const {
15  return ConsumeUnboundConversion(s, s + strlen(s), props, next) ==
16  s + strlen(s);
17  }
18 };
19 
20 TEST_F(FormatBindTest, BindSingle) {
21  struct Expectation {
22  int line;
23  const char *fmt;
24  int ok_phases;
25  const FormatArgImpl *arg;
26  int width;
27  int precision;
28  int next_arg;
29  };
30  const int no = -1;
31  const int ia[] = { 10, 20, 30, 40};
32  const FormatArgImpl args[] = {FormatArgImpl(ia[0]), FormatArgImpl(ia[1]),
33  FormatArgImpl(ia[2]), FormatArgImpl(ia[3])};
34 #pragma GCC diagnostic push
35 #pragma GCC diagnostic ignored "-Wmissing-field-initializers"
36  const Expectation kExpect[] = {
37  {__LINE__, "d", 2, &args[0], no, no, 2},
38  {__LINE__, "4d", 2, &args[0], 4, no, 2},
39  {__LINE__, ".5d", 2, &args[0], no, 5, 2},
40  {__LINE__, "4.5d", 2, &args[0], 4, 5, 2},
41  {__LINE__, "*d", 2, &args[1], 10, no, 3},
42  {__LINE__, ".*d", 2, &args[1], no, 10, 3},
43  {__LINE__, "*.*d", 2, &args[2], 10, 20, 4},
44  {__LINE__, "1$d", 2, &args[0], no, no, 0},
45  {__LINE__, "2$d", 2, &args[1], no, no, 0},
46  {__LINE__, "3$d", 2, &args[2], no, no, 0},
47  {__LINE__, "4$d", 2, &args[3], no, no, 0},
48  {__LINE__, "2$*1$d", 2, &args[1], 10, no, 0},
49  {__LINE__, "2$*2$d", 2, &args[1], 20, no, 0},
50  {__LINE__, "2$*3$d", 2, &args[1], 30, no, 0},
51  {__LINE__, "2$.*1$d", 2, &args[1], no, 10, 0},
52  {__LINE__, "2$.*2$d", 2, &args[1], no, 20, 0},
53  {__LINE__, "2$.*3$d", 2, &args[1], no, 30, 0},
54  {__LINE__, "2$*3$.*1$d", 2, &args[1], 30, 10, 0},
55  {__LINE__, "2$*2$.*2$d", 2, &args[1], 20, 20, 0},
56  {__LINE__, "2$*1$.*3$d", 2, &args[1], 10, 30, 0},
57  {__LINE__, "2$*3$.*1$d", 2, &args[1], 30, 10, 0},
58  {__LINE__, "1$*d", 0}, // indexed, then positional
59  {__LINE__, "*2$d", 0}, // positional, then indexed
60  {__LINE__, "6$d", 1}, // arg position out of bounds
61  {__LINE__, "1$6$d", 0}, // width position incorrectly specified
62  {__LINE__, "1$.6$d", 0}, // precision position incorrectly specified
63  {__LINE__, "1$*6$d", 1}, // width position out of bounds
64  {__LINE__, "1$.*6$d", 1}, // precision position out of bounds
65  };
66 #pragma GCC diagnostic pop
67  for (const Expectation &e : kExpect) {
68  SCOPED_TRACE(e.line);
69  SCOPED_TRACE(e.fmt);
70  UnboundConversion props;
71  BoundConversion bound;
72  int ok_phases = 0;
73  int next = 0;
74  if (Extract(e.fmt, &props, &next)) {
75  ++ok_phases;
76  if (BindWithPack(&props, args, &bound)) {
77  ++ok_phases;
78  }
79  }
80  EXPECT_EQ(e.ok_phases, ok_phases);
81  if (e.ok_phases < 2) continue;
82  if (e.arg != nullptr) {
83  EXPECT_EQ(e.arg, bound.arg());
84  }
85  EXPECT_EQ(e.width, bound.width());
86  EXPECT_EQ(e.precision, bound.precision());
87  }
88 }
89 
90 TEST_F(FormatBindTest, WidthUnderflowRegression) {
91  UnboundConversion props;
92  BoundConversion bound;
93  int next = 0;
94  const int args_i[] = {std::numeric_limits<int>::min(), 17};
95  const FormatArgImpl args[] = {FormatArgImpl(args_i[0]),
96  FormatArgImpl(args_i[1])};
97  ASSERT_TRUE(Extract("*d", &props, &next));
98  ASSERT_TRUE(BindWithPack(&props, args, &bound));
99 
100  EXPECT_EQ(bound.width(), std::numeric_limits<int>::max());
101  EXPECT_EQ(bound.arg(), args + 1);
102 }
103 
104 TEST_F(FormatBindTest, FormatPack) {
105  struct Expectation {
106  int line;
107  const char *fmt;
108  const char *summary;
109  };
110  const int ia[] = { 10, 20, 30, 40, -10 };
111  const FormatArgImpl args[] = {FormatArgImpl(ia[0]), FormatArgImpl(ia[1]),
112  FormatArgImpl(ia[2]), FormatArgImpl(ia[3]),
113  FormatArgImpl(ia[4])};
114  const Expectation kExpect[] = {
115  {__LINE__, "a%4db%dc", "a{10:4d}b{20:d}c"},
116  {__LINE__, "a%.4db%dc", "a{10:.4d}b{20:d}c"},
117  {__LINE__, "a%4.5db%dc", "a{10:4.5d}b{20:d}c"},
118  {__LINE__, "a%db%4.5dc", "a{10:d}b{20:4.5d}c"},
119  {__LINE__, "a%db%*.*dc", "a{10:d}b{40:20.30d}c"},
120  {__LINE__, "a%.*fb", "a{20:.10f}b"},
121  {__LINE__, "a%1$db%2$*3$.*4$dc", "a{10:d}b{20:30.40d}c"},
122  {__LINE__, "a%4$db%3$*2$.*1$dc", "a{40:d}b{30:20.10d}c"},
123  {__LINE__, "a%04ldb", "a{10:04ld}b"},
124  {__LINE__, "a%-#04lldb", "a{10:-#04lld}b"},
125  {__LINE__, "a%1$*5$db", "a{10:-10d}b"},
126  {__LINE__, "a%1$.*5$db", "a{10:d}b"},
127  };
128  for (const Expectation &e : kExpect) {
129  absl::string_view fmt = e.fmt;
130  SCOPED_TRACE(e.line);
131  SCOPED_TRACE(e.fmt);
132  UntypedFormatSpecImpl format(fmt);
133  EXPECT_EQ(e.summary,
135  << "line:" << e.line;
136  }
137 }
138 
139 } // namespace
140 } // namespace str_format_internal
141 } // namespace absl
std::string Summarize(const UntypedFormatSpecImpl format, absl::Span< const FormatArgImpl > args)
Definition: bind.cc:162
Definition: algorithm.h:29
AllocList * next[kMaxLevel]
TEST_F(GraphCyclesTest, NoCycle)
std::string format(const std::string &, const time_point< seconds > &, const femtoseconds &, const time_zone &)
bool BindWithPack(const UnboundConversion *props, absl::Span< const FormatArgImpl > pack, BoundConversion *bound)
Definition: bind.cc:156
void * arg
Definition: mutex.cc:292
std::string FormatPack(const UntypedFormatSpecImpl format, absl::Span< const FormatArgImpl > args)
Definition: bind.h:167
constexpr Span< T > MakeSpan(T *ptr, size_t size) noexcept
Definition: span.h:647
const char * ConsumeUnboundConversion(const char *p, const char *end, UnboundConversion *conv, int *next_arg)
Definition: parser.cc:230


abseil_cpp
Author(s):
autogenerated on Tue Jun 18 2019 19:44:35