• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2020 The Abseil Authors.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //      https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include <string>
16 
17 #include "gmock/gmock.h"
18 #include "gtest/gtest.h"
19 #include "absl/strings/str_format.h"
20 
21 namespace absl {
22 ABSL_NAMESPACE_BEGIN
23 namespace str_format_internal {
24 namespace {
25 
ConvToString(FormatConversionCharSet conv)26 std::string ConvToString(FormatConversionCharSet conv) {
27   std::string out;
28 #define CONV_SET_CASE(c)                                    \
29   if (Contains(conv, FormatConversionCharSetInternal::c)) { \
30     out += #c;                                              \
31   }
32   ABSL_INTERNAL_CONVERSION_CHARS_EXPAND_(CONV_SET_CASE, )
33 #undef CONV_SET_CASE
34   if (Contains(conv, FormatConversionCharSetInternal::kStar)) {
35     out += "*";
36   }
37   return out;
38 }
39 
TEST(StrFormatChecker,ArgumentToConv)40 TEST(StrFormatChecker, ArgumentToConv) {
41   FormatConversionCharSet conv = ArgumentToConv<std::string>();
42   EXPECT_EQ(ConvToString(conv), "s");
43 
44   conv = ArgumentToConv<const char*>();
45   EXPECT_EQ(ConvToString(conv), "sp");
46 
47   conv = ArgumentToConv<double>();
48   EXPECT_EQ(ConvToString(conv), "fFeEgGaA");
49 
50   conv = ArgumentToConv<int>();
51   EXPECT_EQ(ConvToString(conv), "cdiouxXfFeEgGaA*");
52 
53   conv = ArgumentToConv<std::string*>();
54   EXPECT_EQ(ConvToString(conv), "p");
55 }
56 
57 #ifdef ABSL_INTERNAL_ENABLE_FORMAT_CHECKER
58 
59 struct Case {
60   bool result;
61   const char* format;
62 };
63 
64 template <typename... Args>
ValidFormat(const char * format)65 constexpr Case ValidFormat(const char* format) {
66   return {ValidFormatImpl<ArgumentToConv<Args>()...>(format), format};
67 }
68 
TEST(StrFormatChecker,ValidFormat)69 TEST(StrFormatChecker, ValidFormat) {
70   // We want to make sure these expressions are constexpr and they have the
71   // expected value.
72   // If they are not constexpr the attribute will just ignore them and not give
73   // a compile time error.
74   enum e {};
75   enum class e2 {};
76   constexpr Case trues[] = {
77       ValidFormat<>("abc"),  //
78 
79       ValidFormat<e>("%d"),                             //
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)"),
96 
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 "
112           "repeat=%3$s)")};
113 
114   for (Case c : trues) {
115     EXPECT_TRUE(c.result) << c.format;
116   }
117 
118   constexpr Case falses[] = {
119       ValidFormat<int>(""),  //
120 
121       ValidFormat<e>("%s"),                  //
122       ValidFormat<e2>("%s"),                 //
123       ValidFormat<>("%s"),                   //
124       ValidFormat<>("%r"),                   //
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"),       //
133 
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"),
142 
143       ValidFormat<int, int>("%d %2$d"),  //
144   };
145 
146   for (Case c : falses) {
147     EXPECT_FALSE(c.result) << c.format;
148   }
149 }
150 
TEST(StrFormatChecker,LongFormat)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;
162   EXPECT_TRUE(is_valid);
163 }
164 
165 #endif  // ABSL_INTERNAL_ENABLE_FORMAT_CHECKER
166 
167 }  // namespace
168 }  // namespace str_format_internal
169 ABSL_NAMESPACE_END
170 }  // namespace absl
171