6 #include "gtest/gtest.h" 12 class FormatBindTest :
public ::testing::Test {
14 bool Extract(
const char *s, UnboundConversion *props,
int *
next)
const {
20 TEST_F(FormatBindTest, BindSingle) {
25 const FormatArgImpl *
arg;
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},
59 {__LINE__,
"*2$d", 0},
61 {__LINE__,
"1$6$d", 0},
62 {__LINE__,
"1$.6$d", 0},
63 {__LINE__,
"1$*6$d", 1},
64 {__LINE__,
"1$.*6$d", 1},
66 #pragma GCC diagnostic pop 67 for (
const Expectation &e : kExpect) {
70 UnboundConversion props;
71 BoundConversion bound;
74 if (Extract(e.fmt, &props, &next)) {
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());
85 EXPECT_EQ(e.width, bound.width());
86 EXPECT_EQ(e.precision, bound.precision());
90 TEST_F(FormatBindTest, WidthUnderflowRegression) {
91 UnboundConversion props;
92 BoundConversion bound;
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));
100 EXPECT_EQ(bound.width(), std::numeric_limits<int>::max());
101 EXPECT_EQ(bound.arg(), args + 1);
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"},
128 for (
const Expectation &e : kExpect) {
130 SCOPED_TRACE(e.line);
132 UntypedFormatSpecImpl
format(fmt);
135 <<
"line:" << e.line;
AllocList * next[kMaxLevel]
TEST_F(GraphCyclesTest, NoCycle)
std::string format(const std::string &, const time_point< seconds > &, const femtoseconds &, const time_zone &)
constexpr Span< T > MakeSpan(T *ptr, size_t size) noexcept