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