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