5 #include "gmock/gmock.h" 6 #include "gtest/gtest.h" 16 TEST(LengthModTest, Names) {
22 const Expectation kExpect[] = {
35 for (
auto e : kExpect) {
38 EXPECT_EQ(e.id, mod.id());
39 EXPECT_EQ(e.name, mod.name());
43 TEST(ConversionCharTest, Names) {
49 const Expectation kExpect[] = {
50 #define X(c) {ConversionChar::c, #c[0]} 51 X(c),
X(C),
X(s),
X(S),
53 X(f),
X(F),
X(e),
X(E),
X(g),
X(G),
X(
a),
X(A),
60 for (
auto e : kExpect) {
63 EXPECT_EQ(e.id, v.id());
64 EXPECT_EQ(e.name, v.Char());
68 class ConsumeUnboundConversionTest :
public ::testing::Test {
70 std::pair<string_view, string_view> Consume(string_view src) {
72 o = UnboundConversion();
74 src.data(), src.data() + src.size(), &
o, &
next);
75 if (!p)
return {{}, src};
76 return {string_view(src.data(), p - src.data()),
77 string_view(p, src.data() + src.size() - p)};
80 bool Run(
const char *fmt,
bool force_positional =
false) {
81 int next = force_positional ? -1 : 0;
82 o = UnboundConversion();
89 TEST_F(ConsumeUnboundConversionTest, ConsumeSpecification) {
96 const Expectation kExpect[] = {
97 {__LINE__,
"",
"",
"" },
98 {__LINE__,
"b",
"",
"b" },
99 {__LINE__,
"ba",
"",
"ba"},
100 {__LINE__,
"l",
"",
"l" },
101 {__LINE__,
"d",
"d",
"" },
102 {__LINE__,
"d ",
"d",
" " },
103 {__LINE__,
"dd",
"d",
"d" },
104 {__LINE__,
"d9",
"d",
"9" },
105 {__LINE__,
"dzz",
"d",
"zz"},
106 {__LINE__,
"1$*2$d",
"1$*2$d",
"" },
107 {__LINE__,
"0-14.3hhd",
"0-14.3hhd",
""},
108 {__LINE__,
" 0-+#14.3hhd",
" 0-+#14.3hhd",
""},
110 for (
const auto& e : kExpect) {
111 SCOPED_TRACE(e.line);
112 EXPECT_THAT(Consume(e.src), Pair(e.out, e.src_post));
116 TEST_F(ConsumeUnboundConversionTest, BasicConversion) {
117 EXPECT_FALSE(Run(
""));
118 EXPECT_FALSE(Run(
"z"));
120 EXPECT_FALSE(Run(
"dd"));
122 EXPECT_TRUE(Run(
"d"));
123 EXPECT_EQ(
'd',
o.conv.Char());
124 EXPECT_FALSE(
o.width.is_from_arg());
125 EXPECT_LT(
o.width.value(), 0);
126 EXPECT_FALSE(
o.precision.is_from_arg());
127 EXPECT_LT(
o.precision.value(), 0);
128 EXPECT_EQ(1,
o.arg_position);
132 TEST_F(ConsumeUnboundConversionTest, ArgPosition) {
133 EXPECT_TRUE(Run(
"d"));
134 EXPECT_EQ(1,
o.arg_position);
135 EXPECT_TRUE(Run(
"3$d"));
136 EXPECT_EQ(3,
o.arg_position);
137 EXPECT_TRUE(Run(
"1$d"));
138 EXPECT_EQ(1,
o.arg_position);
139 EXPECT_TRUE(Run(
"1$d",
true));
140 EXPECT_EQ(1,
o.arg_position);
141 EXPECT_TRUE(Run(
"123$d"));
142 EXPECT_EQ(123,
o.arg_position);
143 EXPECT_TRUE(Run(
"123$d",
true));
144 EXPECT_EQ(123,
o.arg_position);
145 EXPECT_TRUE(Run(
"10$d"));
146 EXPECT_EQ(10,
o.arg_position);
147 EXPECT_TRUE(Run(
"10$d",
true));
148 EXPECT_EQ(10,
o.arg_position);
151 EXPECT_FALSE(Run(
"0$d"));
152 EXPECT_FALSE(Run(
"0$d",
true));
153 EXPECT_FALSE(Run(
"1$*0$d"));
154 EXPECT_FALSE(Run(
"1$.*0$d"));
157 EXPECT_FALSE(Run(
"01$p"));
158 EXPECT_FALSE(Run(
"01$p",
true));
159 EXPECT_FALSE(Run(
"1$*01$p"));
160 EXPECT_FALSE(Run(
"1$.*01$p"));
163 TEST_F(ConsumeUnboundConversionTest, WidthAndPrecision) {
164 EXPECT_TRUE(Run(
"14d"));
165 EXPECT_EQ(
'd',
o.conv.Char());
166 EXPECT_FALSE(
o.width.is_from_arg());
167 EXPECT_EQ(14,
o.width.value());
168 EXPECT_FALSE(
o.precision.is_from_arg());
169 EXPECT_LT(
o.precision.value(), 0);
171 EXPECT_TRUE(Run(
"14.d"));
172 EXPECT_FALSE(
o.width.is_from_arg());
173 EXPECT_FALSE(
o.precision.is_from_arg());
174 EXPECT_EQ(14,
o.width.value());
175 EXPECT_EQ(0,
o.precision.value());
177 EXPECT_TRUE(Run(
".d"));
178 EXPECT_FALSE(
o.width.is_from_arg());
179 EXPECT_LT(
o.width.value(), 0);
180 EXPECT_FALSE(
o.precision.is_from_arg());
181 EXPECT_EQ(0,
o.precision.value());
183 EXPECT_TRUE(Run(
".5d"));
184 EXPECT_FALSE(
o.width.is_from_arg());
185 EXPECT_LT(
o.width.value(), 0);
186 EXPECT_FALSE(
o.precision.is_from_arg());
187 EXPECT_EQ(5,
o.precision.value());
189 EXPECT_TRUE(Run(
".0d"));
190 EXPECT_FALSE(
o.width.is_from_arg());
191 EXPECT_LT(
o.width.value(), 0);
192 EXPECT_FALSE(
o.precision.is_from_arg());
193 EXPECT_EQ(0,
o.precision.value());
195 EXPECT_TRUE(Run(
"14.5d"));
196 EXPECT_FALSE(
o.width.is_from_arg());
197 EXPECT_FALSE(
o.precision.is_from_arg());
198 EXPECT_EQ(14,
o.width.value());
199 EXPECT_EQ(5,
o.precision.value());
201 EXPECT_TRUE(Run(
"*.*d"));
202 EXPECT_TRUE(
o.width.is_from_arg());
203 EXPECT_EQ(1,
o.width.get_from_arg());
204 EXPECT_TRUE(
o.precision.is_from_arg());
205 EXPECT_EQ(2,
o.precision.get_from_arg());
206 EXPECT_EQ(3,
o.arg_position);
208 EXPECT_TRUE(Run(
"*d"));
209 EXPECT_TRUE(
o.width.is_from_arg());
210 EXPECT_EQ(1,
o.width.get_from_arg());
211 EXPECT_FALSE(
o.precision.is_from_arg());
212 EXPECT_LT(
o.precision.value(), 0);
213 EXPECT_EQ(2,
o.arg_position);
215 EXPECT_TRUE(Run(
".*d"));
216 EXPECT_FALSE(
o.width.is_from_arg());
217 EXPECT_LT(
o.width.value(), 0);
218 EXPECT_TRUE(
o.precision.is_from_arg());
219 EXPECT_EQ(1,
o.precision.get_from_arg());
220 EXPECT_EQ(2,
o.arg_position);
223 EXPECT_FALSE(Run(
"*23$.*34$d"));
225 EXPECT_TRUE(Run(
"12$*23$.*34$d"));
226 EXPECT_EQ(12,
o.arg_position);
227 EXPECT_TRUE(
o.width.is_from_arg());
228 EXPECT_EQ(23,
o.width.get_from_arg());
229 EXPECT_TRUE(
o.precision.is_from_arg());
230 EXPECT_EQ(34,
o.precision.get_from_arg());
232 EXPECT_TRUE(Run(
"2$*5$.*9$d"));
233 EXPECT_EQ(2,
o.arg_position);
234 EXPECT_TRUE(
o.width.is_from_arg());
235 EXPECT_EQ(5,
o.width.get_from_arg());
236 EXPECT_TRUE(
o.precision.is_from_arg());
237 EXPECT_EQ(9,
o.precision.get_from_arg());
239 EXPECT_FALSE(Run(
".*0$d")) <<
"no arg 0";
242 EXPECT_TRUE(Run(
"999999999.999999999d"));
243 EXPECT_FALSE(
o.width.is_from_arg());
244 EXPECT_EQ(999999999,
o.width.value());
245 EXPECT_FALSE(
o.precision.is_from_arg());
246 EXPECT_EQ(999999999,
o.precision.value());
248 EXPECT_FALSE(Run(
"1000000000.999999999d"));
249 EXPECT_FALSE(Run(
"999999999.1000000000d"));
250 EXPECT_FALSE(Run(
"9999999999d"));
251 EXPECT_FALSE(Run(
".9999999999d"));
254 TEST_F(ConsumeUnboundConversionTest, Flags) {
255 static const char kAllFlags[] =
"-+ #0";
257 for (
int rev = 0; rev < 2; ++rev) {
258 for (
int i = 0;
i < 1 << kNumFlags; ++
i) {
260 for (
int k = 0; k < kNumFlags; ++k)
261 if ((
i >> k) & 1) fmt += kAllFlags[k];
263 if (rev == 1) { std::reverse(fmt.begin(), fmt.end()); }
266 EXPECT_TRUE(Run(fmt.c_str()));
267 EXPECT_EQ(fmt.find(
'-') == std::string::npos, !
o.flags.left);
268 EXPECT_EQ(fmt.find(
'+') == std::string::npos, !
o.flags.show_pos);
269 EXPECT_EQ(fmt.find(
' ') == std::string::npos, !
o.flags.sign_col);
270 EXPECT_EQ(fmt.find(
'#') == std::string::npos, !
o.flags.alt);
271 EXPECT_EQ(fmt.find(
'0') == std::string::npos, !
o.flags.zero);
276 TEST_F(ConsumeUnboundConversionTest, BasicFlag) {
278 for (
const char* fmt : {
"d",
"llx",
"G",
"1$X"}) {
280 EXPECT_TRUE(Run(fmt));
281 EXPECT_TRUE(
o.flags.basic);
285 for (
const char* fmt : {
"3d",
".llx",
"-G",
"1$#X"}) {
287 EXPECT_TRUE(Run(fmt));
288 EXPECT_FALSE(
o.flags.basic);
292 struct SummarizeConsumer {
294 explicit SummarizeConsumer(
std::string* out) : out(out) {}
296 bool Append(string_view s) {
301 bool ConvertOne(
const UnboundConversion& conv, string_view s) {
305 *out += std::to_string(conv.arg_position) +
"$";
306 if (conv.width.is_from_arg()) {
307 *out += std::to_string(conv.width.get_from_arg()) +
"$*";
309 if (conv.precision.is_from_arg()) {
310 *out +=
"." + std::to_string(conv.precision.get_from_arg()) +
"$*";
312 *out += conv.conv.Char();
318 std::string SummarizeParsedFormat(
const ParsedFormatBase& pc) {
320 if (!pc.ProcessFormat(SummarizeConsumer(&out))) out +=
"!";
324 class ParsedFormatTest :
public testing::Test {};
326 TEST_F(ParsedFormatTest, ValueSemantics) {
327 ParsedFormatBase p1({},
true, {});
328 EXPECT_EQ(
"", SummarizeParsedFormat(p1));
330 ParsedFormatBase p2 = p1;
331 EXPECT_EQ(SummarizeParsedFormat(p1), SummarizeParsedFormat(p2));
333 p1 = ParsedFormatBase(
"hello%s",
true, {Conv::s});
334 EXPECT_EQ(
"[hello]{s:1$s}", SummarizeParsedFormat(p1));
336 ParsedFormatBase p3 = p1;
337 EXPECT_EQ(SummarizeParsedFormat(p1), SummarizeParsedFormat(p3));
341 EXPECT_EQ(
"", SummarizeParsedFormat(p1));
342 EXPECT_EQ(
"[hello]{s:1$s}", SummarizeParsedFormat(p2));
346 EXPECT_EQ(SummarizeParsedFormat(p1), SummarizeParsedFormat(p2));
355 TEST_F(ParsedFormatTest, Parsing) {
358 const ExpectParse kExpect[] = {
361 {
"a%d", {Conv::d},
"[a]{d:1$d}"},
362 {
"a%+d", {Conv::d},
"[a]{+d:1$d}"},
363 {
"a% d", {Conv::d},
"[a]{ d:1$d}"},
364 {
"a%b %d", {},
"[a]!"},
366 for (
const auto& e : kExpect) {
369 SummarizeParsedFormat(ParsedFormatBase(e.in,
false, e.conv_set)));
373 TEST_F(ParsedFormatTest, ParsingFlagOrder) {
374 const ExpectParse kExpect[] = {
375 {
"a%+ 0d", {Conv::d},
"[a]{+ 0d:1$d}"},
376 {
"a%+0 d", {Conv::d},
"[a]{+0 d:1$d}"},
377 {
"a%0+ d", {Conv::d},
"[a]{0+ d:1$d}"},
378 {
"a% +0d", {Conv::d},
"[a]{ +0d:1$d}"},
379 {
"a%0 +d", {Conv::d},
"[a]{0 +d:1$d}"},
380 {
"a% 0+d", {Conv::d},
"[a]{ 0+d:1$d}"},
381 {
"a%+ 0+d", {Conv::d},
"[a]{+ 0+d:1$d}"},
383 for (
const auto& e : kExpect) {
386 SummarizeParsedFormat(ParsedFormatBase(e.in,
false, e.conv_set)));
TEST(NotificationTest, SanityTest)
AllocList * next[kMaxLevel]
TEST_F(GraphCyclesTest, NoCycle)
void swap(absl::InlinedVector< T, N, A > &a, absl::InlinedVector< T, N, A > &b) noexcept(noexcept(a.swap(b)))
#define ABSL_ARRAYSIZE(array)
static char * Append(char *out, const AlphaNum &x)
std::initializer_list< Conv > conv_set