bloaty/third_party/abseil-cpp/absl/strings/internal/str_format/checker_test.cc
Go to the documentation of this file.
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 {
23 namespace str_format_internal {
24 namespace {
25 
28 #define CONV_SET_CASE(c) \
29  if (Contains(conv, FormatConversionCharSetInternal::c)) { \
30  out += #c; \
31  }
33 #undef CONV_SET_CASE
35  out += "*";
36  }
37  return out;
38 }
39 
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>
65 constexpr Case ValidFormat(const char* format) {
66  return {ValidFormatImpl<ArgumentToConv<Args>()...>(format), format};
67 }
68 
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 
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
170 } // namespace absl
EXPECT_FALSE
#define EXPECT_FALSE(condition)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:1970
_gevent_test_main.result
result
Definition: _gevent_test_main.py:96
absl::str_format_internal::FormatConversionCharSetInternal::kStar
static constexpr FormatConversionCharSet kStar
Definition: abseil-cpp/absl/strings/internal/str_format/extension.h:382
http2_test_server.format
format
Definition: http2_test_server.py:118
absl::str_format_internal::Contains
constexpr bool Contains(FormatConversionCharSet set, char c)
Definition: abseil-cpp/absl/strings/internal/str_format/extension.h:421
fix_build_deps.c
list c
Definition: fix_build_deps.py:490
testing::internal::string
::std::string string
Definition: bloaty/third_party/protobuf/third_party/googletest/googletest/include/gtest/internal/gtest-port.h:881
absl::TEST
TEST(NotificationTest, SanityTest)
Definition: abseil-cpp/absl/synchronization/notification_test.cc:126
ABSL_NAMESPACE_END
#define ABSL_NAMESPACE_END
Definition: third_party/abseil-cpp/absl/base/config.h:171
EXPECT_EQ
#define EXPECT_EQ(a, b)
Definition: iomgr/time_averaged_stats_test.cc:27
absl::FormatConversionChar::e
@ e
ABSL_NAMESPACE_BEGIN
#define ABSL_NAMESPACE_BEGIN
Definition: third_party/abseil-cpp/absl/base/config.h:170
hpack_encoder_fixtures::Args
Args({0, 16384})
absl::str_format_internal::ArgumentToConv
constexpr FormatConversionCharSet ArgumentToConv()
Definition: abseil-cpp/absl/strings/internal/str_format/arg.h:303
conv
const FormatConversionSpecImpl & conv
Definition: abseil-cpp/absl/strings/internal/str_format/float_conversion.cc:449
CONV_SET_CASE
#define CONV_SET_CASE(c)
absl::FormatConversionCharSet
FormatConversionCharSet
Definition: abseil-cpp/absl/strings/str_format.h:737
EXPECT_TRUE
#define EXPECT_TRUE(condition)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:1967
absl
Definition: abseil-cpp/absl/algorithm/algorithm.h:31
absl::out
char * out
Definition: abseil-cpp/absl/synchronization/mutex.h:1048
ABSL_INTERNAL_CONVERSION_CHARS_EXPAND_
#define ABSL_INTERNAL_CONVERSION_CHARS_EXPAND_(X_VAL, X_SEP)
Definition: abseil-cpp/absl/strings/internal/str_format/extension.h:162
str_format_internal


grpc
Author(s):
autogenerated on Fri May 16 2025 02:57:53