1 #include <string>
2
3 #include "gmock/gmock.h"
4 #include "gtest/gtest.h"
5 #include "absl/strings/str_format.h"
6
7 namespace absl {
8 ABSL_NAMESPACE_BEGIN
9 namespace str_format_internal {
10 namespace {
11
ConvToString(Conv conv)12 std::string ConvToString(Conv conv) {
13 std::string out;
14 #define CONV_SET_CASE(c) \
15 if (Contains(conv, Conv::c)) out += #c;
16 ABSL_INTERNAL_CONVERSION_CHARS_EXPAND_(CONV_SET_CASE, )
17 #undef CONV_SET_CASE
18 if (Contains(conv, Conv::star)) out += "*";
19 return out;
20 }
21
TEST(StrFormatChecker,ArgumentToConv)22 TEST(StrFormatChecker, ArgumentToConv) {
23 Conv conv = ArgumentToConv<std::string>();
24 EXPECT_EQ(ConvToString(conv), "s");
25
26 conv = ArgumentToConv<const char*>();
27 EXPECT_EQ(ConvToString(conv), "sp");
28
29 conv = ArgumentToConv<double>();
30 EXPECT_EQ(ConvToString(conv), "fFeEgGaA");
31
32 conv = ArgumentToConv<int>();
33 EXPECT_EQ(ConvToString(conv), "cdiouxXfFeEgGaA*");
34
35 conv = ArgumentToConv<std::string*>();
36 EXPECT_EQ(ConvToString(conv), "p");
37 }
38
39 #ifdef ABSL_INTERNAL_ENABLE_FORMAT_CHECKER
40
41 struct Case {
42 bool result;
43 const char* format;
44 };
45
46 template <typename... Args>
ValidFormat(const char * format)47 constexpr Case ValidFormat(const char* format) {
48 return {ValidFormatImpl<ArgumentToConv<Args>()...>(format), format};
49 }
50
TEST(StrFormatChecker,ValidFormat)51 TEST(StrFormatChecker, ValidFormat) {
52 // We want to make sure these expressions are constexpr and they have the
53 // expected value.
54 // If they are not constexpr the attribute will just ignore them and not give
55 // a compile time error.
56 enum e {};
57 enum class e2 {};
58 constexpr Case trues[] = {
59 ValidFormat<>("abc"), //
60
61 ValidFormat<e>("%d"), //
62 ValidFormat<e2>("%d"), //
63 ValidFormat<int>("%% %d"), //
64 ValidFormat<int>("%ld"), //
65 ValidFormat<int>("%lld"), //
66 ValidFormat<std::string>("%s"), //
67 ValidFormat<std::string>("%10s"), //
68 ValidFormat<int>("%.10x"), //
69 ValidFormat<int, int>("%*.3x"), //
70 ValidFormat<int>("%1.d"), //
71 ValidFormat<int>("%.d"), //
72 ValidFormat<int, double>("%d %g"), //
73 ValidFormat<int, std::string>("%*s"), //
74 ValidFormat<int, double>("%.*f"), //
75 ValidFormat<void (*)(), volatile int*>("%p %p"), //
76 ValidFormat<string_view, const char*, double, void*>(
77 "string_view=%s const char*=%s double=%f void*=%p)"),
78
79 ValidFormat<int>("%% %1$d"), //
80 ValidFormat<int>("%1$ld"), //
81 ValidFormat<int>("%1$lld"), //
82 ValidFormat<std::string>("%1$s"), //
83 ValidFormat<std::string>("%1$10s"), //
84 ValidFormat<int>("%1$.10x"), //
85 ValidFormat<int>("%1$*1$.*1$d"), //
86 ValidFormat<int, int>("%1$*2$.3x"), //
87 ValidFormat<int>("%1$1.d"), //
88 ValidFormat<int>("%1$.d"), //
89 ValidFormat<double, int>("%2$d %1$g"), //
90 ValidFormat<int, std::string>("%2$*1$s"), //
91 ValidFormat<int, double>("%2$.*1$f"), //
92 ValidFormat<void*, string_view, const char*, double>(
93 "string_view=%2$s const char*=%3$s double=%4$f void*=%1$p "
94 "repeat=%3$s)")};
95
96 for (Case c : trues) {
97 EXPECT_TRUE(c.result) << c.format;
98 }
99
100 constexpr Case falses[] = {
101 ValidFormat<int>(""), //
102
103 ValidFormat<e>("%s"), //
104 ValidFormat<e2>("%s"), //
105 ValidFormat<>("%s"), //
106 ValidFormat<>("%r"), //
107 ValidFormat<int>("%s"), //
108 ValidFormat<int>("%.1.d"), //
109 ValidFormat<int>("%*1d"), //
110 ValidFormat<int>("%1-d"), //
111 ValidFormat<std::string, int>("%*s"), //
112 ValidFormat<int>("%*d"), //
113 ValidFormat<std::string>("%p"), //
114 ValidFormat<int (*)(int)>("%d"), //
115
116 ValidFormat<>("%3$d"), //
117 ValidFormat<>("%1$r"), //
118 ValidFormat<int>("%1$s"), //
119 ValidFormat<int>("%1$.1.d"), //
120 ValidFormat<int>("%1$*2$1d"), //
121 ValidFormat<int>("%1$1-d"), //
122 ValidFormat<std::string, int>("%2$*1$s"), //
123 ValidFormat<std::string>("%1$p"),
124
125 ValidFormat<int, int>("%d %2$d"), //
126 };
127
128 for (Case c : falses) {
129 EXPECT_FALSE(c.result) << c.format;
130 }
131 }
132
TEST(StrFormatChecker,LongFormat)133 TEST(StrFormatChecker, LongFormat) {
134 #define CHARS_X_40 "1234567890123456789012345678901234567890"
135 #define CHARS_X_400 \
136 CHARS_X_40 CHARS_X_40 CHARS_X_40 CHARS_X_40 CHARS_X_40 CHARS_X_40 CHARS_X_40 \
137 CHARS_X_40 CHARS_X_40 CHARS_X_40
138 #define CHARS_X_4000 \
139 CHARS_X_400 CHARS_X_400 CHARS_X_400 CHARS_X_400 CHARS_X_400 CHARS_X_400 \
140 CHARS_X_400 CHARS_X_400 CHARS_X_400 CHARS_X_400
141 constexpr char long_format[] =
142 CHARS_X_4000 "%d" CHARS_X_4000 "%s" CHARS_X_4000;
143 constexpr bool is_valid = ValidFormat<int, std::string>(long_format).result;
144 EXPECT_TRUE(is_valid);
145 }
146
147 #endif // ABSL_INTERNAL_ENABLE_FORMAT_CHECKER
148
149 } // namespace
150 } // namespace str_format_internal
151 ABSL_NAMESPACE_END
152 } // namespace absl
153