17 #include "gmock/gmock.h"
18 #include "gtest/gtest.h"
19 #include "absl/strings/str_format.h"
28 #define CONV_SET_CASE(c) \
29 if (Contains(conv, FormatConversionCharSetInternal::c)) { \
44 conv = ArgumentToConv<const char*>();
47 conv = ArgumentToConv<double>();
50 conv = ArgumentToConv<int>();
53 conv = ArgumentToConv<std::string*>();
57 #ifdef ABSL_INTERNAL_ENABLE_FORMAT_CHECKER
64 template <
typename...
Args>
65 constexpr Case ValidFormat(
const char*
format) {
66 return {ValidFormatImpl<ArgumentToConv<Args>()...>(
format),
format};
69 TEST(StrFormatChecker, ValidFormat) {
76 constexpr Case trues[] = {
80 ValidFormat<e2>(
"%d"),
81 ValidFormat<int>(
"%% %d"),
82 ValidFormat<int>(
"%ld"),
83 ValidFormat<int>(
"%lld"),
84 ValidFormat<std::string>(
"%s"),
85 ValidFormat<std::string>(
"%10s"),
86 ValidFormat<int>(
"%.10x"),
87 ValidFormat<int, int>(
"%*.3x"),
88 ValidFormat<int>(
"%1.d"),
89 ValidFormat<int>(
"%.d"),
90 ValidFormat<int, double>(
"%d %g"),
91 ValidFormat<int, std::string>(
"%*s"),
92 ValidFormat<int, double>(
"%.*f"),
93 ValidFormat<void (*)(), volatile int*>(
"%p %p"),
94 ValidFormat<string_view, const char*, double, void*>(
95 "string_view=%s const char*=%s double=%f void*=%p)"),
97 ValidFormat<int>(
"%% %1$d"),
98 ValidFormat<int>(
"%1$ld"),
99 ValidFormat<int>(
"%1$lld"),
100 ValidFormat<std::string>(
"%1$s"),
101 ValidFormat<std::string>(
"%1$10s"),
102 ValidFormat<int>(
"%1$.10x"),
103 ValidFormat<int>(
"%1$*1$.*1$d"),
104 ValidFormat<int, int>(
"%1$*2$.3x"),
105 ValidFormat<int>(
"%1$1.d"),
106 ValidFormat<int>(
"%1$.d"),
107 ValidFormat<double, int>(
"%2$d %1$g"),
108 ValidFormat<int, std::string>(
"%2$*1$s"),
109 ValidFormat<int, double>(
"%2$.*1$f"),
110 ValidFormat<void*, string_view, const char*, double>(
111 "string_view=%2$s const char*=%3$s double=%4$f void*=%1$p "
114 for (Case c : trues) {
118 constexpr Case falses[] = {
119 ValidFormat<int>(
""),
121 ValidFormat<e>(
"%s"),
122 ValidFormat<e2>(
"%s"),
125 ValidFormat<int>(
"%s"),
126 ValidFormat<int>(
"%.1.d"),
127 ValidFormat<int>(
"%*1d"),
128 ValidFormat<int>(
"%1-d"),
129 ValidFormat<std::string, int>(
"%*s"),
130 ValidFormat<int>(
"%*d"),
131 ValidFormat<std::string>(
"%p"),
132 ValidFormat<int (*)(int)>(
"%d"),
134 ValidFormat<>(
"%3$d"),
135 ValidFormat<>(
"%1$r"),
136 ValidFormat<int>(
"%1$s"),
137 ValidFormat<int>(
"%1$.1.d"),
138 ValidFormat<int>(
"%1$*2$1d"),
139 ValidFormat<int>(
"%1$1-d"),
140 ValidFormat<std::string, int>(
"%2$*1$s"),
141 ValidFormat<std::string>(
"%1$p"),
143 ValidFormat<int, int>(
"%d %2$d"),
146 for (Case c : falses) {
151 TEST(StrFormatChecker, LongFormat) {
152 #define CHARS_X_40 "1234567890123456789012345678901234567890"
153 #define CHARS_X_400 \
154 CHARS_X_40 CHARS_X_40 CHARS_X_40 CHARS_X_40 CHARS_X_40 CHARS_X_40 CHARS_X_40 \
155 CHARS_X_40 CHARS_X_40 CHARS_X_40
156 #define CHARS_X_4000 \
157 CHARS_X_400 CHARS_X_400 CHARS_X_400 CHARS_X_400 CHARS_X_400 CHARS_X_400 \
158 CHARS_X_400 CHARS_X_400 CHARS_X_400 CHARS_X_400
159 constexpr
char long_format[] =
160 CHARS_X_4000
"%d" CHARS_X_4000
"%s" CHARS_X_4000;
161 constexpr
bool is_valid = ValidFormat<int, std::string>(long_format).result;
165 #endif // ABSL_INTERNAL_ENABLE_FORMAT_CHECKER