[docs] Add LICENSE.txt to the root of the mono-repo
[llvm-project.git] / llvm / unittests / Support / FormatVariadicTest.cpp
blob312b7b14f3b24723e8b458b77c10d0ba8ec28770
1 //===- FormatVariadicTest.cpp - Unit tests for string formatting ----------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
9 #include "llvm/Support/FormatVariadic.h"
10 #include "llvm/Support/Error.h"
11 #include "llvm/Support/FormatAdapters.h"
12 #include "gtest/gtest.h"
14 using namespace llvm;
16 // Compile-time tests templates in the detail namespace.
17 namespace {
18 struct Format : public FormatAdapter<int> {
19 Format(int N) : FormatAdapter<int>(std::move(N)) {}
20 void format(raw_ostream &OS, StringRef Opt) override { OS << "Format"; }
23 using detail::uses_format_member;
24 using detail::uses_missing_provider;
26 static_assert(uses_format_member<Format>::value, "");
27 static_assert(uses_format_member<Format &>::value, "");
28 static_assert(uses_format_member<Format &&>::value, "");
29 static_assert(uses_format_member<const Format>::value, "");
30 static_assert(uses_format_member<const Format &>::value, "");
31 static_assert(uses_format_member<const volatile Format>::value, "");
32 static_assert(uses_format_member<const volatile Format &>::value, "");
34 struct NoFormat {};
35 static_assert(uses_missing_provider<NoFormat>::value, "");
38 TEST(FormatVariadicTest, EmptyFormatString) {
39 auto Replacements = formatv_object_base::parseFormatString("");
40 EXPECT_EQ(0U, Replacements.size());
43 TEST(FormatVariadicTest, NoReplacements) {
44 const StringRef kFormatString = "This is a test";
45 auto Replacements = formatv_object_base::parseFormatString(kFormatString);
46 ASSERT_EQ(1U, Replacements.size());
47 EXPECT_EQ(kFormatString, Replacements[0].Spec);
48 EXPECT_EQ(ReplacementType::Literal, Replacements[0].Type);
51 TEST(FormatVariadicTest, EscapedBrace) {
52 // {{ should be replaced with {
53 auto Replacements = formatv_object_base::parseFormatString("{{");
54 ASSERT_EQ(1u, Replacements.size());
55 EXPECT_EQ("{", Replacements[0].Spec);
56 EXPECT_EQ(ReplacementType::Literal, Replacements[0].Type);
58 // An even number N of braces should be replaced with N/2 braces.
59 Replacements = formatv_object_base::parseFormatString("{{{{{{");
60 ASSERT_EQ(1u, Replacements.size());
61 EXPECT_EQ("{{{", Replacements[0].Spec);
62 EXPECT_EQ(ReplacementType::Literal, Replacements[0].Type);
64 // } does not require doubling up.
65 Replacements = formatv_object_base::parseFormatString("}");
66 ASSERT_EQ(1u, Replacements.size());
67 EXPECT_EQ("}", Replacements[0].Spec);
68 EXPECT_EQ(ReplacementType::Literal, Replacements[0].Type);
70 // } does not require doubling up.
71 Replacements = formatv_object_base::parseFormatString("}}}");
72 ASSERT_EQ(1u, Replacements.size());
73 EXPECT_EQ("}}}", Replacements[0].Spec);
74 EXPECT_EQ(ReplacementType::Literal, Replacements[0].Type);
77 TEST(FormatVariadicTest, ValidReplacementSequence) {
78 // 1. Simple replacement - parameter index only
79 auto Replacements = formatv_object_base::parseFormatString("{0}");
80 ASSERT_EQ(1u, Replacements.size());
81 EXPECT_EQ(ReplacementType::Format, Replacements[0].Type);
82 EXPECT_EQ(0u, Replacements[0].Index);
83 EXPECT_EQ(0u, Replacements[0].Align);
84 EXPECT_EQ("", Replacements[0].Options);
86 Replacements = formatv_object_base::parseFormatString("{1}");
87 ASSERT_EQ(1u, Replacements.size());
88 EXPECT_EQ(ReplacementType::Format, Replacements[0].Type);
89 EXPECT_EQ(1u, Replacements[0].Index);
90 EXPECT_EQ(0u, Replacements[0].Align);
91 EXPECT_EQ(AlignStyle::Right, Replacements[0].Where);
92 EXPECT_EQ("", Replacements[0].Options);
94 // 2. Parameter index with right alignment
95 Replacements = formatv_object_base::parseFormatString("{0,3}");
96 ASSERT_EQ(1u, Replacements.size());
97 EXPECT_EQ(ReplacementType::Format, Replacements[0].Type);
98 EXPECT_EQ(0u, Replacements[0].Index);
99 EXPECT_EQ(3u, Replacements[0].Align);
100 EXPECT_EQ(AlignStyle::Right, Replacements[0].Where);
101 EXPECT_EQ("", Replacements[0].Options);
103 // 3. And left alignment
104 Replacements = formatv_object_base::parseFormatString("{0,-3}");
105 ASSERT_EQ(1u, Replacements.size());
106 EXPECT_EQ(ReplacementType::Format, Replacements[0].Type);
107 EXPECT_EQ(0u, Replacements[0].Index);
108 EXPECT_EQ(3u, Replacements[0].Align);
109 EXPECT_EQ(AlignStyle::Left, Replacements[0].Where);
110 EXPECT_EQ("", Replacements[0].Options);
112 // 4. And center alignment
113 Replacements = formatv_object_base::parseFormatString("{0,=3}");
114 ASSERT_EQ(1u, Replacements.size());
115 EXPECT_EQ(ReplacementType::Format, Replacements[0].Type);
116 EXPECT_EQ(0u, Replacements[0].Index);
117 EXPECT_EQ(3u, Replacements[0].Align);
118 EXPECT_EQ(AlignStyle::Center, Replacements[0].Where);
119 EXPECT_EQ("", Replacements[0].Options);
121 // 4. Parameter index with option string
122 Replacements = formatv_object_base::parseFormatString("{0:foo}");
123 ASSERT_EQ(1u, Replacements.size());
124 EXPECT_EQ(ReplacementType::Format, Replacements[0].Type);
125 EXPECT_EQ(0u, Replacements[0].Index);
126 EXPECT_EQ(0u, Replacements[0].Align);
127 EXPECT_EQ(AlignStyle::Right, Replacements[0].Where);
128 EXPECT_EQ("foo", Replacements[0].Options);
130 // 5. Parameter index with alignment before option string
131 Replacements = formatv_object_base::parseFormatString("{0,-3:foo}");
132 ASSERT_EQ(1u, Replacements.size());
133 EXPECT_EQ(ReplacementType::Format, Replacements[0].Type);
134 EXPECT_EQ(0u, Replacements[0].Index);
135 EXPECT_EQ(3u, Replacements[0].Align);
136 EXPECT_EQ(AlignStyle::Left, Replacements[0].Where);
137 EXPECT_EQ("foo", Replacements[0].Options);
139 // 7. Parameter indices, options, and alignment can all have whitespace.
140 Replacements = formatv_object_base::parseFormatString("{ 0, -3 : foo }");
141 ASSERT_EQ(1u, Replacements.size());
142 EXPECT_EQ(ReplacementType::Format, Replacements[0].Type);
143 EXPECT_EQ(0u, Replacements[0].Index);
144 EXPECT_EQ(3u, Replacements[0].Align);
145 EXPECT_EQ(AlignStyle::Left, Replacements[0].Where);
146 EXPECT_EQ("foo", Replacements[0].Options);
148 // 8. Everything after the first option specifier is part of the style, even
149 // if it contains another option specifier.
150 Replacements = formatv_object_base::parseFormatString("{0:0:1}");
151 ASSERT_EQ(1u, Replacements.size());
152 EXPECT_EQ("0:0:1", Replacements[0].Spec);
153 EXPECT_EQ(ReplacementType::Format, Replacements[0].Type);
154 EXPECT_EQ(0u, Replacements[0].Index);
155 EXPECT_EQ(0u, Replacements[0].Align);
156 EXPECT_EQ(AlignStyle::Right, Replacements[0].Where);
157 EXPECT_EQ("0:1", Replacements[0].Options);
159 // 9. Custom padding character
160 Replacements = formatv_object_base::parseFormatString("{0,p+4:foo}");
161 ASSERT_EQ(1u, Replacements.size());
162 EXPECT_EQ("0,p+4:foo", Replacements[0].Spec);
163 EXPECT_EQ(ReplacementType::Format, Replacements[0].Type);
164 EXPECT_EQ(0u, Replacements[0].Index);
165 EXPECT_EQ(4u, Replacements[0].Align);
166 EXPECT_EQ(AlignStyle::Right, Replacements[0].Where);
167 EXPECT_EQ('p', Replacements[0].Pad);
168 EXPECT_EQ("foo", Replacements[0].Options);
170 // Format string special characters are allowed as padding character
171 Replacements = formatv_object_base::parseFormatString("{0,-+4:foo}");
172 ASSERT_EQ(1u, Replacements.size());
173 EXPECT_EQ("0,-+4:foo", Replacements[0].Spec);
174 EXPECT_EQ(ReplacementType::Format, Replacements[0].Type);
175 EXPECT_EQ(0u, Replacements[0].Index);
176 EXPECT_EQ(4u, Replacements[0].Align);
177 EXPECT_EQ(AlignStyle::Right, Replacements[0].Where);
178 EXPECT_EQ('-', Replacements[0].Pad);
179 EXPECT_EQ("foo", Replacements[0].Options);
181 Replacements = formatv_object_base::parseFormatString("{0,+-4:foo}");
182 ASSERT_EQ(1u, Replacements.size());
183 EXPECT_EQ("0,+-4:foo", Replacements[0].Spec);
184 EXPECT_EQ(ReplacementType::Format, Replacements[0].Type);
185 EXPECT_EQ(0u, Replacements[0].Index);
186 EXPECT_EQ(4u, Replacements[0].Align);
187 EXPECT_EQ(AlignStyle::Left, Replacements[0].Where);
188 EXPECT_EQ('+', Replacements[0].Pad);
189 EXPECT_EQ("foo", Replacements[0].Options);
191 Replacements = formatv_object_base::parseFormatString("{0,==4:foo}");
192 ASSERT_EQ(1u, Replacements.size());
193 EXPECT_EQ("0,==4:foo", Replacements[0].Spec);
194 EXPECT_EQ(ReplacementType::Format, Replacements[0].Type);
195 EXPECT_EQ(0u, Replacements[0].Index);
196 EXPECT_EQ(4u, Replacements[0].Align);
197 EXPECT_EQ(AlignStyle::Center, Replacements[0].Where);
198 EXPECT_EQ('=', Replacements[0].Pad);
199 EXPECT_EQ("foo", Replacements[0].Options);
201 Replacements = formatv_object_base::parseFormatString("{0,:=4:foo}");
202 ASSERT_EQ(1u, Replacements.size());
203 EXPECT_EQ("0,:=4:foo", Replacements[0].Spec);
204 EXPECT_EQ(ReplacementType::Format, Replacements[0].Type);
205 EXPECT_EQ(0u, Replacements[0].Index);
206 EXPECT_EQ(4u, Replacements[0].Align);
207 EXPECT_EQ(AlignStyle::Center, Replacements[0].Where);
208 EXPECT_EQ(':', Replacements[0].Pad);
209 EXPECT_EQ("foo", Replacements[0].Options);
212 TEST(FormatVariadicTest, DefaultReplacementValues) {
213 // 2. If options string is missing, it defaults to empty.
214 auto Replacements = formatv_object_base::parseFormatString("{0,3}");
215 ASSERT_EQ(1u, Replacements.size());
216 EXPECT_EQ(ReplacementType::Format, Replacements[0].Type);
217 EXPECT_EQ(0u, Replacements[0].Index);
218 EXPECT_EQ(3u, Replacements[0].Align);
219 EXPECT_EQ("", Replacements[0].Options);
221 // Including if the colon is present but contains no text.
222 Replacements = formatv_object_base::parseFormatString("{0,3:}");
223 ASSERT_EQ(1u, Replacements.size());
224 EXPECT_EQ(ReplacementType::Format, Replacements[0].Type);
225 EXPECT_EQ(0u, Replacements[0].Index);
226 EXPECT_EQ(3u, Replacements[0].Align);
227 EXPECT_EQ("", Replacements[0].Options);
229 // 3. If alignment is missing, it defaults to 0, right, space
230 Replacements = formatv_object_base::parseFormatString("{0:foo}");
231 ASSERT_EQ(1u, Replacements.size());
232 EXPECT_EQ(ReplacementType::Format, Replacements[0].Type);
233 EXPECT_EQ(AlignStyle::Right, Replacements[0].Where);
234 EXPECT_EQ(' ', Replacements[0].Pad);
235 EXPECT_EQ(0u, Replacements[0].Index);
236 EXPECT_EQ(0u, Replacements[0].Align);
237 EXPECT_EQ("foo", Replacements[0].Options);
240 TEST(FormatVariadicTest, MultipleReplacements) {
241 auto Replacements =
242 formatv_object_base::parseFormatString("{0} {1:foo}-{2,-3:bar}");
243 ASSERT_EQ(5u, Replacements.size());
244 // {0}
245 EXPECT_EQ(ReplacementType::Format, Replacements[0].Type);
246 EXPECT_EQ(0u, Replacements[0].Index);
247 EXPECT_EQ(0u, Replacements[0].Align);
248 EXPECT_EQ(AlignStyle::Right, Replacements[0].Where);
249 EXPECT_EQ("", Replacements[0].Options);
251 // " "
252 EXPECT_EQ(ReplacementType::Literal, Replacements[1].Type);
253 EXPECT_EQ(" ", Replacements[1].Spec);
255 // {1:foo} - Options=foo
256 EXPECT_EQ(ReplacementType::Format, Replacements[2].Type);
257 EXPECT_EQ(1u, Replacements[2].Index);
258 EXPECT_EQ(0u, Replacements[2].Align);
259 EXPECT_EQ(AlignStyle::Right, Replacements[2].Where);
260 EXPECT_EQ("foo", Replacements[2].Options);
262 // "-"
263 EXPECT_EQ(ReplacementType::Literal, Replacements[3].Type);
264 EXPECT_EQ("-", Replacements[3].Spec);
266 // {2:bar,-3} - Options=bar, Align=-3
267 EXPECT_EQ(ReplacementType::Format, Replacements[4].Type);
268 EXPECT_EQ(2u, Replacements[4].Index);
269 EXPECT_EQ(3u, Replacements[4].Align);
270 EXPECT_EQ(AlignStyle::Left, Replacements[4].Where);
271 EXPECT_EQ("bar", Replacements[4].Options);
274 TEST(FormatVariadicTest, FormatNoReplacements) {
275 EXPECT_EQ("", formatv("").str());
276 EXPECT_EQ("Test", formatv("Test").str());
279 TEST(FormatVariadicTest, FormatBasicTypesOneReplacement) {
280 EXPECT_EQ("1", formatv("{0}", 1).str());
281 EXPECT_EQ("c", formatv("{0}", 'c').str());
282 EXPECT_EQ("-3", formatv("{0}", -3).str());
283 EXPECT_EQ("Test", formatv("{0}", "Test").str());
284 EXPECT_EQ("Test2", formatv("{0}", StringRef("Test2")).str());
285 EXPECT_EQ("Test3", formatv("{0}", std::string("Test3")).str());
288 TEST(FormatVariadicTest, IntegralHexFormatting) {
289 // 1. Trivial cases. Make sure hex is not the default.
290 EXPECT_EQ("0", formatv("{0}", 0).str());
291 EXPECT_EQ("2748", formatv("{0}", 0xABC).str());
292 EXPECT_EQ("-2748", formatv("{0}", -0xABC).str());
294 // 3. various hex prefixes.
295 EXPECT_EQ("0xFF", formatv("{0:X}", 255).str());
296 EXPECT_EQ("0xFF", formatv("{0:X+}", 255).str());
297 EXPECT_EQ("0xff", formatv("{0:x}", 255).str());
298 EXPECT_EQ("0xff", formatv("{0:x+}", 255).str());
299 EXPECT_EQ("FF", formatv("{0:X-}", 255).str());
300 EXPECT_EQ("ff", formatv("{0:x-}", 255).str());
302 // 5. Precision pads left of the most significant digit but right of the
303 // prefix (if one exists).
304 EXPECT_EQ("0xFF", formatv("{0:X2}", 255).str());
305 EXPECT_EQ("0xFF", formatv("{0:X+2}", 255).str());
306 EXPECT_EQ("0x0ff", formatv("{0:x3}", 255).str());
307 EXPECT_EQ("0x0ff", formatv("{0:x+3}", 255).str());
308 EXPECT_EQ("00FF", formatv("{0:X-4}", 255).str());
309 EXPECT_EQ("00ff", formatv("{0:x-4}", 255).str());
311 // 6. Try some larger types.
312 EXPECT_EQ("0xDEADBEEFDEADBEEF",
313 formatv("{0:X16}", -2401053088876216593LL).str());
314 EXPECT_EQ("0xFEEBDAEDFEEBDAED",
315 formatv("{0:X16}", 0xFEEBDAEDFEEBDAEDULL).str());
316 EXPECT_EQ("0x00000000DEADBEEF", formatv("{0:X16}", 0xDEADBEEF).str());
318 // 7. Padding should take into account the prefix
319 EXPECT_EQ("0xff", formatv("{0,4:x}", 255).str());
320 EXPECT_EQ(" 0xff", formatv("{0,5:x+}", 255).str());
321 EXPECT_EQ(" FF", formatv("{0,4:X-}", 255).str());
322 EXPECT_EQ(" ff", formatv("{0,5:x-}", 255).str());
324 // 8. Including when it's been zero-padded
325 EXPECT_EQ(" 0x0ff", formatv("{0,7:x3}", 255).str());
326 EXPECT_EQ(" 0x00ff", formatv("{0,7:x+4}", 255).str());
327 EXPECT_EQ(" 000FF", formatv("{0,7:X-5}", 255).str());
328 EXPECT_EQ(" 0000ff", formatv("{0,7:x-6}", 255).str());
330 // 9. Precision with default format specifier should work too
331 EXPECT_EQ(" 255", formatv("{0,7:3}", 255).str());
332 EXPECT_EQ(" 0255", formatv("{0,7:4}", 255).str());
333 EXPECT_EQ(" 00255", formatv("{0,7:5}", 255).str());
334 EXPECT_EQ(" 000255", formatv("{0,7:6}", 255).str());
337 TEST(FormatVariadicTest, PointerFormatting) {
338 // 1. Trivial cases. Hex is default. Default Precision is pointer width.
339 if (sizeof(void *) == 4) {
340 EXPECT_EQ("0x00000000", formatv("{0}", (void *)0).str());
341 EXPECT_EQ("0x00000ABC", formatv("{0}", (void *)0xABC).str());
342 } else {
343 EXPECT_EQ("0x0000000000000000", formatv("{0}", (void *)0).str());
344 EXPECT_EQ("0x0000000000000ABC", formatv("{0}", (void *)0xABC).str());
347 // 2. But we can reduce the precision explicitly.
348 EXPECT_EQ("0x0", formatv("{0:0}", (void *)0).str());
349 EXPECT_EQ("0xABC", formatv("{0:0}", (void *)0xABC).str());
350 EXPECT_EQ("0x0000", formatv("{0:4}", (void *)0).str());
351 EXPECT_EQ("0x0ABC", formatv("{0:4}", (void *)0xABC).str());
353 // 3. various hex prefixes.
354 EXPECT_EQ("0x0ABC", formatv("{0:X4}", (void *)0xABC).str());
355 EXPECT_EQ("0x0abc", formatv("{0:x4}", (void *)0xABC).str());
356 EXPECT_EQ("0ABC", formatv("{0:X-4}", (void *)0xABC).str());
357 EXPECT_EQ("0abc", formatv("{0:x-4}", (void *)0xABC).str());
360 TEST(FormatVariadicTest, IntegralNumberFormatting) {
361 // 1. Test comma grouping with default widths and precisions.
362 EXPECT_EQ("0", formatv("{0:N}", 0).str());
363 EXPECT_EQ("10", formatv("{0:N}", 10).str());
364 EXPECT_EQ("100", formatv("{0:N}", 100).str());
365 EXPECT_EQ("1,000", formatv("{0:N}", 1000).str());
366 EXPECT_EQ("1,234,567,890", formatv("{0:N}", 1234567890).str());
367 EXPECT_EQ("-10", formatv("{0:N}", -10).str());
368 EXPECT_EQ("-100", formatv("{0:N}", -100).str());
369 EXPECT_EQ("-1,000", formatv("{0:N}", -1000).str());
370 EXPECT_EQ("-1,234,567,890", formatv("{0:N}", -1234567890).str());
372 // 2. If there is no comma, width and precision pad to the same absolute
373 // size.
374 EXPECT_EQ(" 1", formatv("{0,2:N}", 1).str());
376 // 3. But if there is a comma or negative sign, width factors them in but
377 // precision doesn't.
378 EXPECT_EQ(" 1,000", formatv("{0,6:N}", 1000).str());
379 EXPECT_EQ(" -1,000", formatv("{0,7:N}", -1000).str());
381 // 4. Large widths all line up.
382 EXPECT_EQ(" 1,000", formatv("{0,11:N}", 1000).str());
383 EXPECT_EQ(" -1,000", formatv("{0,11:N}", -1000).str());
384 EXPECT_EQ(" -100,000", formatv("{0,11:N}", -100000).str());
387 TEST(FormatVariadicTest, StringFormatting) {
388 const char FooArray[] = "FooArray";
389 const char *FooPtr = "FooPtr";
390 llvm::StringRef FooRef("FooRef");
391 constexpr StringLiteral FooLiteral("FooLiteral");
392 std::string FooString("FooString");
393 // 1. Test that we can print various types of strings.
394 EXPECT_EQ(FooArray, formatv("{0}", FooArray).str());
395 EXPECT_EQ(FooPtr, formatv("{0}", FooPtr).str());
396 EXPECT_EQ(FooRef, formatv("{0}", FooRef).str());
397 EXPECT_EQ(FooLiteral, formatv("{0}", FooLiteral).str());
398 EXPECT_EQ(FooString, formatv("{0}", FooString).str());
400 // 2. Test that the precision specifier prints the correct number of
401 // characters.
402 EXPECT_EQ("FooA", formatv("{0:4}", FooArray).str());
403 EXPECT_EQ("FooP", formatv("{0:4}", FooPtr).str());
404 EXPECT_EQ("FooR", formatv("{0:4}", FooRef).str());
405 EXPECT_EQ("FooS", formatv("{0:4}", FooString).str());
407 // 3. And that padding works.
408 EXPECT_EQ(" FooA", formatv("{0,6:4}", FooArray).str());
409 EXPECT_EQ(" FooP", formatv("{0,6:4}", FooPtr).str());
410 EXPECT_EQ(" FooR", formatv("{0,6:4}", FooRef).str());
411 EXPECT_EQ(" FooS", formatv("{0,6:4}", FooString).str());
414 TEST(FormatVariadicTest, CharFormatting) {
415 // 1. Not much to see here. Just print a char with and without padding.
416 EXPECT_EQ("C", formatv("{0}", 'C').str());
417 EXPECT_EQ(" C", formatv("{0,3}", 'C').str());
419 // 2. char is really an integral type though, where the only difference is
420 // that the "default" is to print the ASCII. So if a non-default presentation
421 // specifier exists, it should print as an integer.
422 EXPECT_EQ("37", formatv("{0:D}", (char)37).str());
423 EXPECT_EQ(" 037", formatv("{0,5:D3}", (char)37).str());
426 TEST(FormatVariadicTest, BoolTest) {
427 // 1. Default style is lowercase text (same as 't')
428 EXPECT_EQ("true", formatv("{0}", true).str());
429 EXPECT_EQ("false", formatv("{0}", false).str());
430 EXPECT_EQ("true", formatv("{0:t}", true).str());
431 EXPECT_EQ("false", formatv("{0:t}", false).str());
433 // 2. T - uppercase text
434 EXPECT_EQ("TRUE", formatv("{0:T}", true).str());
435 EXPECT_EQ("FALSE", formatv("{0:T}", false).str());
437 // 3. D / d - integral
438 EXPECT_EQ("1", formatv("{0:D}", true).str());
439 EXPECT_EQ("0", formatv("{0:D}", false).str());
440 EXPECT_EQ("1", formatv("{0:d}", true).str());
441 EXPECT_EQ("0", formatv("{0:d}", false).str());
443 // 4. Y - uppercase yes/no
444 EXPECT_EQ("YES", formatv("{0:Y}", true).str());
445 EXPECT_EQ("NO", formatv("{0:Y}", false).str());
447 // 5. y - lowercase yes/no
448 EXPECT_EQ("yes", formatv("{0:y}", true).str());
449 EXPECT_EQ("no", formatv("{0:y}", false).str());
452 TEST(FormatVariadicTest, DoubleFormatting) {
453 // Test exponents, fixed point, and percent formatting.
455 // 1. Signed, unsigned, and zero exponent format.
456 EXPECT_EQ("0.000000E+00", formatv("{0:E}", 0.0).str());
457 EXPECT_EQ("-0.000000E+00", formatv("{0:E}", -0.0).str());
458 EXPECT_EQ("1.100000E+00", formatv("{0:E}", 1.1).str());
459 EXPECT_EQ("-1.100000E+00", formatv("{0:E}", -1.1).str());
460 EXPECT_EQ("1.234568E+03", formatv("{0:E}", 1234.5678).str());
461 EXPECT_EQ("-1.234568E+03", formatv("{0:E}", -1234.5678).str());
462 EXPECT_EQ("1.234568E-03", formatv("{0:E}", .0012345678).str());
463 EXPECT_EQ("-1.234568E-03", formatv("{0:E}", -.0012345678).str());
465 // 2. With padding and precision.
466 EXPECT_EQ(" 0.000E+00", formatv("{0,11:E3}", 0.0).str());
467 EXPECT_EQ(" -1.100E+00", formatv("{0,11:E3}", -1.1).str());
468 EXPECT_EQ(" 1.235E+03", formatv("{0,11:E3}", 1234.5678).str());
469 EXPECT_EQ(" -1.235E-03", formatv("{0,11:E3}", -.0012345678).str());
471 // 3. Signed, unsigned, and zero fixed point format.
472 EXPECT_EQ("0.00", formatv("{0:F}", 0.0).str());
473 EXPECT_EQ("-0.00", formatv("{0:F}", -0.0).str());
474 EXPECT_EQ("1.10", formatv("{0:F}", 1.1).str());
475 EXPECT_EQ("-1.10", formatv("{0:F}", -1.1).str());
476 EXPECT_EQ("1234.57", formatv("{0:F}", 1234.5678).str());
477 EXPECT_EQ("-1234.57", formatv("{0:F}", -1234.5678).str());
478 EXPECT_EQ("0.00", formatv("{0:F}", .0012345678).str());
479 EXPECT_EQ("-0.00", formatv("{0:F}", -.0012345678).str());
481 // 2. With padding and precision.
482 EXPECT_EQ(" 0.000", formatv("{0,8:F3}", 0.0).str());
483 EXPECT_EQ(" -1.100", formatv("{0,8:F3}", -1.1).str());
484 EXPECT_EQ("1234.568", formatv("{0,8:F3}", 1234.5678).str());
485 EXPECT_EQ(" -0.001", formatv("{0,8:F3}", -.0012345678).str());
488 TEST(FormatVariadicTest, CustomPaddingCharacter) {
489 // 1. Padding with custom character
490 EXPECT_EQ("==123", formatv("{0,=+5}", 123).str());
491 EXPECT_EQ("=123=", formatv("{0,==5}", 123).str());
492 EXPECT_EQ("123==", formatv("{0,=-5}", 123).str());
494 // 2. Combined with zero padding
495 EXPECT_EQ("=00123=", formatv("{0,==7:5}", 123).str());
498 struct format_tuple {
499 const char *Fmt;
500 explicit format_tuple(const char *Fmt) : Fmt(Fmt) {}
502 template <typename... Ts> auto operator()(Ts &&... Values) const {
503 return formatv(Fmt, std::forward<Ts>(Values)...);
507 TEST(FormatVariadicTest, BigTest) {
508 using Tuple =
509 std::tuple<char, int, const char *, StringRef, std::string, double, float,
510 void *, int, double, int64_t, uint64_t, double, uint8_t>;
511 Tuple Ts[] = {
512 Tuple('a', 1, "Str", StringRef(), std::string(), 3.14159, -.17532f,
513 (void *)nullptr, 123456, 6.02E23, -908234908423, 908234908422234,
514 std::numeric_limits<double>::quiet_NaN(), 0xAB),
515 Tuple('x', 0xDDB5B, "LongerStr", "StringRef", "std::string", -2.7,
516 .08215f, (void *)nullptr, 0, 6.62E-34, -908234908423,
517 908234908422234, std::numeric_limits<double>::infinity(), 0x0)};
518 // Test long string formatting with many edge cases combined.
519 const char *Intro =
520 "There are {{{0}} items in the tuple, and {{{1}} tuple(s) in the array.";
521 const char *Header =
522 "{0,6}|{1,8}|{2,=10}|{3,=10}|{4,=13}|{5,7}|{6,7}|{7,10}|{8,"
523 "-7}|{9,10}|{10,16}|{11,17}|{12,6}|{13,4}";
524 const char *Line =
525 "{0,6}|{1,8:X}|{2,=10}|{3,=10:5}|{4,=13}|{5,7:3}|{6,7:P2}|{7,"
526 "10:X8}|{8,-7:N}|{9,10:E4}|{10,16:N}|{11,17:D}|{12,6}|{13,"
527 "4:X}";
529 std::string S;
530 llvm::raw_string_ostream Stream(S);
531 Stream << formatv(Intro, std::tuple_size<Tuple>::value,
532 llvm::array_lengthof(Ts))
533 << "\n";
534 Stream << formatv(Header, "Char", "HexInt", "Str", "Ref", "std::str",
535 "double", "float", "pointer", "comma", "exp", "bigint",
536 "bigint2", "limit", "byte")
537 << "\n";
538 for (auto &Item : Ts) {
539 Stream << std::apply(format_tuple(Line), Item) << "\n";
541 Stream.flush();
542 const char *Expected =
543 R"foo(There are {14} items in the tuple, and {2} tuple(s) in the array.
544 Char| HexInt| Str | Ref | std::str | double| float| pointer|comma | exp| bigint| bigint2| limit|byte
545 a| 0x1| Str | | | 3.142|-17.53%|0x00000000|123,456|6.0200E+23|-908,234,908,423| 908234908422234| nan|0xAB
546 x| 0xDDB5B|LongerStr | Strin | std::string | -2.700| 8.21%|0x00000000|0 |6.6200E-34|-908,234,908,423| 908234908422234| INF| 0x0
547 )foo";
549 EXPECT_EQ(Expected, S);
552 TEST(FormatVariadicTest, Range) {
553 std::vector<int> IntRange = {1, 1, 2, 3, 5, 8, 13};
555 // 1. Simple range with default separator and element style.
556 EXPECT_EQ("1, 1, 2, 3, 5, 8, 13",
557 formatv("{0}", make_range(IntRange.begin(), IntRange.end())).str());
558 EXPECT_EQ("1, 2, 3, 5, 8",
559 formatv("{0}", make_range(IntRange.begin() + 1, IntRange.end() - 1))
560 .str());
562 // 2. Non-default separator
563 EXPECT_EQ(
564 "1/1/2/3/5/8/13",
565 formatv("{0:$[/]}", make_range(IntRange.begin(), IntRange.end())).str());
567 // 3. Default separator, non-default element style.
568 EXPECT_EQ(
569 "0x1, 0x1, 0x2, 0x3, 0x5, 0x8, 0xd",
570 formatv("{0:@[x]}", make_range(IntRange.begin(), IntRange.end())).str());
572 // 4. Non-default separator and element style.
573 EXPECT_EQ(
574 "0x1 + 0x1 + 0x2 + 0x3 + 0x5 + 0x8 + 0xd",
575 formatv("{0:$[ + ]@[x]}", make_range(IntRange.begin(), IntRange.end()))
576 .str());
578 // 5. Element style and/or separator using alternate delimeters to allow using
579 // delimeter characters as part of the separator.
580 EXPECT_EQ(
581 "<0x1><0x1><0x2><0x3><0x5><0x8><0xd>",
582 formatv("<{0:$[><]@(x)}>", make_range(IntRange.begin(), IntRange.end()))
583 .str());
584 EXPECT_EQ(
585 "[0x1][0x1][0x2][0x3][0x5][0x8][0xd]",
586 formatv("[{0:$(][)@[x]}]", make_range(IntRange.begin(), IntRange.end()))
587 .str());
588 EXPECT_EQ(
589 "(0x1)(0x1)(0x2)(0x3)(0x5)(0x8)(0xd)",
590 formatv("({0:$<)(>@<x>})", make_range(IntRange.begin(), IntRange.end()))
591 .str());
593 // 5. Empty range.
594 EXPECT_EQ("", formatv("{0:$[+]@[x]}",
595 make_range(IntRange.begin(), IntRange.begin()))
596 .str());
598 // 6. Empty separator and style.
599 EXPECT_EQ("11235813",
600 formatv("{0:$[]@<>}", make_range(IntRange.begin(), IntRange.end()))
601 .str());
604 TEST(FormatVariadicTest, Adapter) {
605 class Negative : public FormatAdapter<int> {
606 public:
607 explicit Negative(int N) : FormatAdapter<int>(std::move(N)) {}
608 void format(raw_ostream &S, StringRef Options) override { S << -Item; }
611 EXPECT_EQ("-7", formatv("{0}", Negative(7)).str());
613 int N = 171;
615 EXPECT_EQ(" 171 ",
616 formatv("{0}", fmt_align(N, AlignStyle::Center, 7)).str());
617 EXPECT_EQ("--171--",
618 formatv("{0}", fmt_align(N, AlignStyle::Center, 7, '-')).str());
619 EXPECT_EQ(" 171 ", formatv("{0}", fmt_pad(N, 1, 3)).str());
620 EXPECT_EQ("171171171171171", formatv("{0}", fmt_repeat(N, 5)).str());
622 EXPECT_EQ(" ABABABABAB ",
623 formatv("{0:X-}", fmt_pad(fmt_repeat(N, 5), 1, 3)).str());
624 EXPECT_EQ(" AB AB AB AB AB ",
625 formatv("{0,=34:X-}", fmt_repeat(fmt_pad(N, 1, 3), 5)).str());
628 TEST(FormatVariadicTest, MoveConstructor) {
629 auto fmt = formatv("{0} {1}", 1, 2);
630 auto fmt2 = std::move(fmt);
631 std::string S = std::string(fmt2);
632 EXPECT_EQ("1 2", S);
634 TEST(FormatVariadicTest, ImplicitConversions) {
635 std::string S = std::string(formatv("{0} {1}", 1, 2));
636 EXPECT_EQ("1 2", S);
638 SmallString<4> S2 = formatv("{0} {1}", 1, 2);
639 EXPECT_EQ("1 2", S2);
642 TEST(FormatVariadicTest, FormatAdapter) {
643 EXPECT_EQ("Format", formatv("{0}", Format(1)).str());
645 Format var(1);
646 EXPECT_EQ("Format", formatv("{0}", var).str());
647 EXPECT_EQ("Format", formatv("{0}", std::move(var)).str());
649 // Not supposed to compile
650 // const Format cvar(1);
651 // EXPECT_EQ("Format", formatv("{0}", cvar).str());
654 TEST(FormatVariadicTest, FormatFormatvObject) {
655 EXPECT_EQ("Format", formatv("F{0}t", formatv("o{0}a", "rm")).str());
656 EXPECT_EQ("[ ! ]", formatv("[{0,+5}]", formatv("{0,-2}", "!")).str());
659 namespace {
660 struct Recorder {
661 int Copied = 0, Moved = 0;
662 Recorder() = default;
663 Recorder(const Recorder &Copy) : Copied(1 + Copy.Copied), Moved(Copy.Moved) {}
664 Recorder(const Recorder &&Move)
665 : Copied(Move.Copied), Moved(1 + Move.Moved) {}
667 } // namespace
668 namespace llvm {
669 template <> struct format_provider<Recorder> {
670 static void format(const Recorder &R, raw_ostream &OS, StringRef style) {
671 OS << R.Copied << "C " << R.Moved << "M";
674 } // namespace
676 TEST(FormatVariadicTest, CopiesAndMoves) {
677 Recorder R;
678 EXPECT_EQ("0C 0M", formatv("{0}", R).str());
679 EXPECT_EQ("0C 3M", formatv("{0}", std::move(R)).str());
680 EXPECT_EQ("0C 3M", formatv("{0}", Recorder()).str());
681 EXPECT_EQ(0, R.Copied);
682 EXPECT_EQ(0, R.Moved);
685 namespace adl {
686 struct X {};
687 raw_ostream &operator<<(raw_ostream &OS, const X &) { return OS << "X"; }
688 } // namespace adl
689 TEST(FormatVariadicTest, FormatStreamable) {
690 adl::X X;
691 EXPECT_EQ("X", formatv("{0}", X).str());
694 TEST(FormatVariadicTest, FormatError) {
695 auto E1 = make_error<StringError>("X", inconvertibleErrorCode());
696 EXPECT_EQ("X", formatv("{0}", E1).str());
697 EXPECT_TRUE(E1.isA<StringError>()); // not consumed
698 EXPECT_EQ("X", formatv("{0}", fmt_consume(std::move(E1))).str());
699 EXPECT_FALSE(E1.isA<StringError>()); // consumed