[yaml2obj/obj2yaml] - Add support for .stack_sizes sections.
[llvm-complete.git] / unittests / Support / FormatVariadicTest.cpp
blobe9565c13dd372859aa5abf60922322f47e3405f8
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);
65 TEST(FormatVariadicTest, ValidReplacementSequence) {
66 // 1. Simple replacement - parameter index only
67 auto Replacements = formatv_object_base::parseFormatString("{0}");
68 ASSERT_EQ(1u, Replacements.size());
69 EXPECT_EQ(ReplacementType::Format, Replacements[0].Type);
70 EXPECT_EQ(0u, Replacements[0].Index);
71 EXPECT_EQ(0u, Replacements[0].Align);
72 EXPECT_EQ("", Replacements[0].Options);
74 Replacements = formatv_object_base::parseFormatString("{1}");
75 ASSERT_EQ(1u, Replacements.size());
76 EXPECT_EQ(ReplacementType::Format, Replacements[0].Type);
77 EXPECT_EQ(1u, Replacements[0].Index);
78 EXPECT_EQ(0u, Replacements[0].Align);
79 EXPECT_EQ(AlignStyle::Right, Replacements[0].Where);
80 EXPECT_EQ("", Replacements[0].Options);
82 // 2. Parameter index with right alignment
83 Replacements = formatv_object_base::parseFormatString("{0,3}");
84 ASSERT_EQ(1u, Replacements.size());
85 EXPECT_EQ(ReplacementType::Format, Replacements[0].Type);
86 EXPECT_EQ(0u, Replacements[0].Index);
87 EXPECT_EQ(3u, Replacements[0].Align);
88 EXPECT_EQ(AlignStyle::Right, Replacements[0].Where);
89 EXPECT_EQ("", Replacements[0].Options);
91 // 3. And left alignment
92 Replacements = formatv_object_base::parseFormatString("{0,-3}");
93 ASSERT_EQ(1u, Replacements.size());
94 EXPECT_EQ(ReplacementType::Format, Replacements[0].Type);
95 EXPECT_EQ(0u, Replacements[0].Index);
96 EXPECT_EQ(3u, Replacements[0].Align);
97 EXPECT_EQ(AlignStyle::Left, Replacements[0].Where);
98 EXPECT_EQ("", Replacements[0].Options);
100 // 4. And center alignment
101 Replacements = formatv_object_base::parseFormatString("{0,=3}");
102 ASSERT_EQ(1u, Replacements.size());
103 EXPECT_EQ(ReplacementType::Format, Replacements[0].Type);
104 EXPECT_EQ(0u, Replacements[0].Index);
105 EXPECT_EQ(3u, Replacements[0].Align);
106 EXPECT_EQ(AlignStyle::Center, Replacements[0].Where);
107 EXPECT_EQ("", Replacements[0].Options);
109 // 4. Parameter index with option string
110 Replacements = formatv_object_base::parseFormatString("{0:foo}");
111 ASSERT_EQ(1u, Replacements.size());
112 EXPECT_EQ(ReplacementType::Format, Replacements[0].Type);
113 EXPECT_EQ(0u, Replacements[0].Index);
114 EXPECT_EQ(0u, Replacements[0].Align);
115 EXPECT_EQ(AlignStyle::Right, Replacements[0].Where);
116 EXPECT_EQ("foo", Replacements[0].Options);
118 // 5. Parameter index with alignment before option string
119 Replacements = formatv_object_base::parseFormatString("{0,-3:foo}");
120 ASSERT_EQ(1u, Replacements.size());
121 EXPECT_EQ(ReplacementType::Format, Replacements[0].Type);
122 EXPECT_EQ(0u, Replacements[0].Index);
123 EXPECT_EQ(3u, Replacements[0].Align);
124 EXPECT_EQ(AlignStyle::Left, Replacements[0].Where);
125 EXPECT_EQ("foo", Replacements[0].Options);
127 // 7. Parameter indices, options, and alignment can all have whitespace.
128 Replacements = formatv_object_base::parseFormatString("{ 0, -3 : foo }");
129 ASSERT_EQ(1u, Replacements.size());
130 EXPECT_EQ(ReplacementType::Format, Replacements[0].Type);
131 EXPECT_EQ(0u, Replacements[0].Index);
132 EXPECT_EQ(3u, Replacements[0].Align);
133 EXPECT_EQ(AlignStyle::Left, Replacements[0].Where);
134 EXPECT_EQ("foo", Replacements[0].Options);
136 // 8. Everything after the first option specifier is part of the style, even
137 // if it contains another option specifier.
138 Replacements = formatv_object_base::parseFormatString("{0:0:1}");
139 ASSERT_EQ(1u, Replacements.size());
140 EXPECT_EQ("0:0:1", Replacements[0].Spec);
141 EXPECT_EQ(ReplacementType::Format, Replacements[0].Type);
142 EXPECT_EQ(0u, Replacements[0].Index);
143 EXPECT_EQ(0u, Replacements[0].Align);
144 EXPECT_EQ(AlignStyle::Right, Replacements[0].Where);
145 EXPECT_EQ("0:1", Replacements[0].Options);
147 // 9. Custom padding character
148 Replacements = formatv_object_base::parseFormatString("{0,p+4:foo}");
149 ASSERT_EQ(1u, Replacements.size());
150 EXPECT_EQ("0,p+4:foo", Replacements[0].Spec);
151 EXPECT_EQ(ReplacementType::Format, Replacements[0].Type);
152 EXPECT_EQ(0u, Replacements[0].Index);
153 EXPECT_EQ(4u, Replacements[0].Align);
154 EXPECT_EQ(AlignStyle::Right, Replacements[0].Where);
155 EXPECT_EQ('p', Replacements[0].Pad);
156 EXPECT_EQ("foo", Replacements[0].Options);
158 // Format string special characters are allowed as padding character
159 Replacements = formatv_object_base::parseFormatString("{0,-+4:foo}");
160 ASSERT_EQ(1u, Replacements.size());
161 EXPECT_EQ("0,-+4:foo", Replacements[0].Spec);
162 EXPECT_EQ(ReplacementType::Format, Replacements[0].Type);
163 EXPECT_EQ(0u, Replacements[0].Index);
164 EXPECT_EQ(4u, Replacements[0].Align);
165 EXPECT_EQ(AlignStyle::Right, Replacements[0].Where);
166 EXPECT_EQ('-', Replacements[0].Pad);
167 EXPECT_EQ("foo", Replacements[0].Options);
169 Replacements = formatv_object_base::parseFormatString("{0,+-4:foo}");
170 ASSERT_EQ(1u, Replacements.size());
171 EXPECT_EQ("0,+-4:foo", Replacements[0].Spec);
172 EXPECT_EQ(ReplacementType::Format, Replacements[0].Type);
173 EXPECT_EQ(0u, Replacements[0].Index);
174 EXPECT_EQ(4u, Replacements[0].Align);
175 EXPECT_EQ(AlignStyle::Left, Replacements[0].Where);
176 EXPECT_EQ('+', Replacements[0].Pad);
177 EXPECT_EQ("foo", Replacements[0].Options);
179 Replacements = formatv_object_base::parseFormatString("{0,==4:foo}");
180 ASSERT_EQ(1u, Replacements.size());
181 EXPECT_EQ("0,==4:foo", Replacements[0].Spec);
182 EXPECT_EQ(ReplacementType::Format, Replacements[0].Type);
183 EXPECT_EQ(0u, Replacements[0].Index);
184 EXPECT_EQ(4u, Replacements[0].Align);
185 EXPECT_EQ(AlignStyle::Center, Replacements[0].Where);
186 EXPECT_EQ('=', Replacements[0].Pad);
187 EXPECT_EQ("foo", Replacements[0].Options);
189 Replacements = formatv_object_base::parseFormatString("{0,:=4:foo}");
190 ASSERT_EQ(1u, Replacements.size());
191 EXPECT_EQ("0,:=4:foo", Replacements[0].Spec);
192 EXPECT_EQ(ReplacementType::Format, Replacements[0].Type);
193 EXPECT_EQ(0u, Replacements[0].Index);
194 EXPECT_EQ(4u, Replacements[0].Align);
195 EXPECT_EQ(AlignStyle::Center, Replacements[0].Where);
196 EXPECT_EQ(':', Replacements[0].Pad);
197 EXPECT_EQ("foo", Replacements[0].Options);
200 TEST(FormatVariadicTest, DefaultReplacementValues) {
201 // 2. If options string is missing, it defaults to empty.
202 auto Replacements = formatv_object_base::parseFormatString("{0,3}");
203 ASSERT_EQ(1u, Replacements.size());
204 EXPECT_EQ(ReplacementType::Format, Replacements[0].Type);
205 EXPECT_EQ(0u, Replacements[0].Index);
206 EXPECT_EQ(3u, Replacements[0].Align);
207 EXPECT_EQ("", Replacements[0].Options);
209 // Including if the colon is present but contains no text.
210 Replacements = formatv_object_base::parseFormatString("{0,3:}");
211 ASSERT_EQ(1u, Replacements.size());
212 EXPECT_EQ(ReplacementType::Format, Replacements[0].Type);
213 EXPECT_EQ(0u, Replacements[0].Index);
214 EXPECT_EQ(3u, Replacements[0].Align);
215 EXPECT_EQ("", Replacements[0].Options);
217 // 3. If alignment is missing, it defaults to 0, right, space
218 Replacements = formatv_object_base::parseFormatString("{0:foo}");
219 ASSERT_EQ(1u, Replacements.size());
220 EXPECT_EQ(ReplacementType::Format, Replacements[0].Type);
221 EXPECT_EQ(AlignStyle::Right, Replacements[0].Where);
222 EXPECT_EQ(' ', Replacements[0].Pad);
223 EXPECT_EQ(0u, Replacements[0].Index);
224 EXPECT_EQ(0u, Replacements[0].Align);
225 EXPECT_EQ("foo", Replacements[0].Options);
228 TEST(FormatVariadicTest, MultipleReplacements) {
229 auto Replacements =
230 formatv_object_base::parseFormatString("{0} {1:foo}-{2,-3:bar}");
231 ASSERT_EQ(5u, Replacements.size());
232 // {0}
233 EXPECT_EQ(ReplacementType::Format, Replacements[0].Type);
234 EXPECT_EQ(0u, Replacements[0].Index);
235 EXPECT_EQ(0u, Replacements[0].Align);
236 EXPECT_EQ(AlignStyle::Right, Replacements[0].Where);
237 EXPECT_EQ("", Replacements[0].Options);
239 // " "
240 EXPECT_EQ(ReplacementType::Literal, Replacements[1].Type);
241 EXPECT_EQ(" ", Replacements[1].Spec);
243 // {1:foo} - Options=foo
244 EXPECT_EQ(ReplacementType::Format, Replacements[2].Type);
245 EXPECT_EQ(1u, Replacements[2].Index);
246 EXPECT_EQ(0u, Replacements[2].Align);
247 EXPECT_EQ(AlignStyle::Right, Replacements[2].Where);
248 EXPECT_EQ("foo", Replacements[2].Options);
250 // "-"
251 EXPECT_EQ(ReplacementType::Literal, Replacements[3].Type);
252 EXPECT_EQ("-", Replacements[3].Spec);
254 // {2:bar,-3} - Options=bar, Align=-3
255 EXPECT_EQ(ReplacementType::Format, Replacements[4].Type);
256 EXPECT_EQ(2u, Replacements[4].Index);
257 EXPECT_EQ(3u, Replacements[4].Align);
258 EXPECT_EQ(AlignStyle::Left, Replacements[4].Where);
259 EXPECT_EQ("bar", Replacements[4].Options);
262 TEST(FormatVariadicTest, FormatNoReplacements) {
263 EXPECT_EQ("", formatv("").str());
264 EXPECT_EQ("Test", formatv("Test").str());
267 TEST(FormatVariadicTest, FormatBasicTypesOneReplacement) {
268 EXPECT_EQ("1", formatv("{0}", 1).str());
269 EXPECT_EQ("c", formatv("{0}", 'c').str());
270 EXPECT_EQ("-3", formatv("{0}", -3).str());
271 EXPECT_EQ("Test", formatv("{0}", "Test").str());
272 EXPECT_EQ("Test2", formatv("{0}", StringRef("Test2")).str());
273 EXPECT_EQ("Test3", formatv("{0}", std::string("Test3")).str());
276 TEST(FormatVariadicTest, IntegralHexFormatting) {
277 // 1. Trivial cases. Make sure hex is not the default.
278 EXPECT_EQ("0", formatv("{0}", 0).str());
279 EXPECT_EQ("2748", formatv("{0}", 0xABC).str());
280 EXPECT_EQ("-2748", formatv("{0}", -0xABC).str());
282 // 3. various hex prefixes.
283 EXPECT_EQ("0xFF", formatv("{0:X}", 255).str());
284 EXPECT_EQ("0xFF", formatv("{0:X+}", 255).str());
285 EXPECT_EQ("0xff", formatv("{0:x}", 255).str());
286 EXPECT_EQ("0xff", formatv("{0:x+}", 255).str());
287 EXPECT_EQ("FF", formatv("{0:X-}", 255).str());
288 EXPECT_EQ("ff", formatv("{0:x-}", 255).str());
290 // 5. Precision pads left of the most significant digit but right of the
291 // prefix (if one exists).
292 EXPECT_EQ("0xFF", formatv("{0:X2}", 255).str());
293 EXPECT_EQ("0xFF", formatv("{0:X+2}", 255).str());
294 EXPECT_EQ("0x0ff", formatv("{0:x3}", 255).str());
295 EXPECT_EQ("0x0ff", formatv("{0:x+3}", 255).str());
296 EXPECT_EQ("00FF", formatv("{0:X-4}", 255).str());
297 EXPECT_EQ("00ff", formatv("{0:x-4}", 255).str());
299 // 6. Try some larger types.
300 EXPECT_EQ("0xDEADBEEFDEADBEEF",
301 formatv("{0:X16}", -2401053088876216593LL).str());
302 EXPECT_EQ("0xFEEBDAEDFEEBDAED",
303 formatv("{0:X16}", 0xFEEBDAEDFEEBDAEDULL).str());
304 EXPECT_EQ("0x00000000DEADBEEF", formatv("{0:X16}", 0xDEADBEEF).str());
306 // 7. Padding should take into account the prefix
307 EXPECT_EQ("0xff", formatv("{0,4:x}", 255).str());
308 EXPECT_EQ(" 0xff", formatv("{0,5:x+}", 255).str());
309 EXPECT_EQ(" FF", formatv("{0,4:X-}", 255).str());
310 EXPECT_EQ(" ff", formatv("{0,5:x-}", 255).str());
312 // 8. Including when it's been zero-padded
313 EXPECT_EQ(" 0x0ff", formatv("{0,7:x3}", 255).str());
314 EXPECT_EQ(" 0x00ff", formatv("{0,7:x+4}", 255).str());
315 EXPECT_EQ(" 000FF", formatv("{0,7:X-5}", 255).str());
316 EXPECT_EQ(" 0000ff", formatv("{0,7:x-6}", 255).str());
318 // 9. Precision with default format specifier should work too
319 EXPECT_EQ(" 255", formatv("{0,7:3}", 255).str());
320 EXPECT_EQ(" 0255", formatv("{0,7:4}", 255).str());
321 EXPECT_EQ(" 00255", formatv("{0,7:5}", 255).str());
322 EXPECT_EQ(" 000255", formatv("{0,7:6}", 255).str());
325 TEST(FormatVariadicTest, PointerFormatting) {
326 // 1. Trivial cases. Hex is default. Default Precision is pointer width.
327 if (sizeof(void *) == 4) {
328 EXPECT_EQ("0x00000000", formatv("{0}", (void *)0).str());
329 EXPECT_EQ("0x00000ABC", formatv("{0}", (void *)0xABC).str());
330 } else {
331 EXPECT_EQ("0x0000000000000000", formatv("{0}", (void *)0).str());
332 EXPECT_EQ("0x0000000000000ABC", formatv("{0}", (void *)0xABC).str());
335 // 2. But we can reduce the precision explicitly.
336 EXPECT_EQ("0x0", formatv("{0:0}", (void *)0).str());
337 EXPECT_EQ("0xABC", formatv("{0:0}", (void *)0xABC).str());
338 EXPECT_EQ("0x0000", formatv("{0:4}", (void *)0).str());
339 EXPECT_EQ("0x0ABC", formatv("{0:4}", (void *)0xABC).str());
341 // 3. various hex prefixes.
342 EXPECT_EQ("0x0ABC", formatv("{0:X4}", (void *)0xABC).str());
343 EXPECT_EQ("0x0abc", formatv("{0:x4}", (void *)0xABC).str());
344 EXPECT_EQ("0ABC", formatv("{0:X-4}", (void *)0xABC).str());
345 EXPECT_EQ("0abc", formatv("{0:x-4}", (void *)0xABC).str());
348 TEST(FormatVariadicTest, IntegralNumberFormatting) {
349 // 1. Test comma grouping with default widths and precisions.
350 EXPECT_EQ("0", formatv("{0:N}", 0).str());
351 EXPECT_EQ("10", formatv("{0:N}", 10).str());
352 EXPECT_EQ("100", formatv("{0:N}", 100).str());
353 EXPECT_EQ("1,000", formatv("{0:N}", 1000).str());
354 EXPECT_EQ("1,234,567,890", formatv("{0:N}", 1234567890).str());
355 EXPECT_EQ("-10", formatv("{0:N}", -10).str());
356 EXPECT_EQ("-100", formatv("{0:N}", -100).str());
357 EXPECT_EQ("-1,000", formatv("{0:N}", -1000).str());
358 EXPECT_EQ("-1,234,567,890", formatv("{0:N}", -1234567890).str());
360 // 2. If there is no comma, width and precision pad to the same absolute
361 // size.
362 EXPECT_EQ(" 1", formatv("{0,2:N}", 1).str());
364 // 3. But if there is a comma or negative sign, width factors them in but
365 // precision doesn't.
366 EXPECT_EQ(" 1,000", formatv("{0,6:N}", 1000).str());
367 EXPECT_EQ(" -1,000", formatv("{0,7:N}", -1000).str());
369 // 4. Large widths all line up.
370 EXPECT_EQ(" 1,000", formatv("{0,11:N}", 1000).str());
371 EXPECT_EQ(" -1,000", formatv("{0,11:N}", -1000).str());
372 EXPECT_EQ(" -100,000", formatv("{0,11:N}", -100000).str());
375 TEST(FormatVariadicTest, StringFormatting) {
376 const char FooArray[] = "FooArray";
377 const char *FooPtr = "FooPtr";
378 llvm::StringRef FooRef("FooRef");
379 constexpr StringLiteral FooLiteral("FooLiteral");
380 std::string FooString("FooString");
381 // 1. Test that we can print various types of strings.
382 EXPECT_EQ(FooArray, formatv("{0}", FooArray).str());
383 EXPECT_EQ(FooPtr, formatv("{0}", FooPtr).str());
384 EXPECT_EQ(FooRef, formatv("{0}", FooRef).str());
385 EXPECT_EQ(FooLiteral, formatv("{0}", FooLiteral).str());
386 EXPECT_EQ(FooString, formatv("{0}", FooString).str());
388 // 2. Test that the precision specifier prints the correct number of
389 // characters.
390 EXPECT_EQ("FooA", formatv("{0:4}", FooArray).str());
391 EXPECT_EQ("FooP", formatv("{0:4}", FooPtr).str());
392 EXPECT_EQ("FooR", formatv("{0:4}", FooRef).str());
393 EXPECT_EQ("FooS", formatv("{0:4}", FooString).str());
395 // 3. And that padding works.
396 EXPECT_EQ(" FooA", formatv("{0,6:4}", FooArray).str());
397 EXPECT_EQ(" FooP", formatv("{0,6:4}", FooPtr).str());
398 EXPECT_EQ(" FooR", formatv("{0,6:4}", FooRef).str());
399 EXPECT_EQ(" FooS", formatv("{0,6:4}", FooString).str());
402 TEST(FormatVariadicTest, CharFormatting) {
403 // 1. Not much to see here. Just print a char with and without padding.
404 EXPECT_EQ("C", formatv("{0}", 'C').str());
405 EXPECT_EQ(" C", formatv("{0,3}", 'C').str());
407 // 2. char is really an integral type though, where the only difference is
408 // that the "default" is to print the ASCII. So if a non-default presentation
409 // specifier exists, it should print as an integer.
410 EXPECT_EQ("37", formatv("{0:D}", (char)37).str());
411 EXPECT_EQ(" 037", formatv("{0,5:D3}", (char)37).str());
414 TEST(FormatVariadicTest, BoolTest) {
415 // 1. Default style is lowercase text (same as 't')
416 EXPECT_EQ("true", formatv("{0}", true).str());
417 EXPECT_EQ("false", formatv("{0}", false).str());
418 EXPECT_EQ("true", formatv("{0:t}", true).str());
419 EXPECT_EQ("false", formatv("{0:t}", false).str());
421 // 2. T - uppercase text
422 EXPECT_EQ("TRUE", formatv("{0:T}", true).str());
423 EXPECT_EQ("FALSE", formatv("{0:T}", false).str());
425 // 3. D / d - integral
426 EXPECT_EQ("1", formatv("{0:D}", true).str());
427 EXPECT_EQ("0", formatv("{0:D}", false).str());
428 EXPECT_EQ("1", formatv("{0:d}", true).str());
429 EXPECT_EQ("0", formatv("{0:d}", false).str());
431 // 4. Y - uppercase yes/no
432 EXPECT_EQ("YES", formatv("{0:Y}", true).str());
433 EXPECT_EQ("NO", formatv("{0:Y}", false).str());
435 // 5. y - lowercase yes/no
436 EXPECT_EQ("yes", formatv("{0:y}", true).str());
437 EXPECT_EQ("no", formatv("{0:y}", false).str());
440 TEST(FormatVariadicTest, DoubleFormatting) {
441 // Test exponents, fixed point, and percent formatting.
443 // 1. Signed, unsigned, and zero exponent format.
444 EXPECT_EQ("0.000000E+00", formatv("{0:E}", 0.0).str());
445 EXPECT_EQ("-0.000000E+00", formatv("{0:E}", -0.0).str());
446 EXPECT_EQ("1.100000E+00", formatv("{0:E}", 1.1).str());
447 EXPECT_EQ("-1.100000E+00", formatv("{0:E}", -1.1).str());
448 EXPECT_EQ("1.234568E+03", formatv("{0:E}", 1234.5678).str());
449 EXPECT_EQ("-1.234568E+03", formatv("{0:E}", -1234.5678).str());
450 EXPECT_EQ("1.234568E-03", formatv("{0:E}", .0012345678).str());
451 EXPECT_EQ("-1.234568E-03", formatv("{0:E}", -.0012345678).str());
453 // 2. With padding and precision.
454 EXPECT_EQ(" 0.000E+00", formatv("{0,11:E3}", 0.0).str());
455 EXPECT_EQ(" -1.100E+00", formatv("{0,11:E3}", -1.1).str());
456 EXPECT_EQ(" 1.235E+03", formatv("{0,11:E3}", 1234.5678).str());
457 EXPECT_EQ(" -1.235E-03", formatv("{0,11:E3}", -.0012345678).str());
459 // 3. Signed, unsigned, and zero fixed point format.
460 EXPECT_EQ("0.00", formatv("{0:F}", 0.0).str());
461 EXPECT_EQ("-0.00", formatv("{0:F}", -0.0).str());
462 EXPECT_EQ("1.10", formatv("{0:F}", 1.1).str());
463 EXPECT_EQ("-1.10", formatv("{0:F}", -1.1).str());
464 EXPECT_EQ("1234.57", formatv("{0:F}", 1234.5678).str());
465 EXPECT_EQ("-1234.57", formatv("{0:F}", -1234.5678).str());
466 EXPECT_EQ("0.00", formatv("{0:F}", .0012345678).str());
467 EXPECT_EQ("-0.00", formatv("{0:F}", -.0012345678).str());
469 // 2. With padding and precision.
470 EXPECT_EQ(" 0.000", formatv("{0,8:F3}", 0.0).str());
471 EXPECT_EQ(" -1.100", formatv("{0,8:F3}", -1.1).str());
472 EXPECT_EQ("1234.568", formatv("{0,8:F3}", 1234.5678).str());
473 EXPECT_EQ(" -0.001", formatv("{0,8:F3}", -.0012345678).str());
476 TEST(FormatVariadicTest, CustomPaddingCharacter) {
477 // 1. Padding with custom character
478 EXPECT_EQ("==123", formatv("{0,=+5}", 123).str());
479 EXPECT_EQ("=123=", formatv("{0,==5}", 123).str());
480 EXPECT_EQ("123==", formatv("{0,=-5}", 123).str());
482 // 2. Combined with zero padding
483 EXPECT_EQ("=00123=", formatv("{0,==7:5}", 123).str());
486 struct format_tuple {
487 const char *Fmt;
488 explicit format_tuple(const char *Fmt) : Fmt(Fmt) {}
490 template <typename... Ts>
491 auto operator()(Ts &&... Values) const
492 -> decltype(formatv(Fmt, std::forward<Ts>(Values)...)) {
493 return formatv(Fmt, std::forward<Ts>(Values)...);
497 TEST(FormatVariadicTest, BigTest) {
498 using Tuple =
499 std::tuple<char, int, const char *, StringRef, std::string, double, float,
500 void *, int, double, int64_t, uint64_t, double, uint8_t>;
501 Tuple Ts[] = {
502 Tuple('a', 1, "Str", StringRef(), std::string(), 3.14159, -.17532f,
503 (void *)nullptr, 123456, 6.02E23, -908234908423, 908234908422234,
504 std::numeric_limits<double>::quiet_NaN(), 0xAB),
505 Tuple('x', 0xDDB5B, "LongerStr", "StringRef", "std::string", -2.7,
506 .08215f, (void *)nullptr, 0, 6.62E-34, -908234908423,
507 908234908422234, std::numeric_limits<double>::infinity(), 0x0)};
508 // Test long string formatting with many edge cases combined.
509 const char *Intro =
510 "There are {{{0}} items in the tuple, and {{{1}} tuple(s) in the array.";
511 const char *Header =
512 "{0,6}|{1,8}|{2,=10}|{3,=10}|{4,=13}|{5,7}|{6,7}|{7,10}|{8,"
513 "-7}|{9,10}|{10,16}|{11,17}|{12,6}|{13,4}";
514 const char *Line =
515 "{0,6}|{1,8:X}|{2,=10}|{3,=10:5}|{4,=13}|{5,7:3}|{6,7:P2}|{7,"
516 "10:X8}|{8,-7:N}|{9,10:E4}|{10,16:N}|{11,17:D}|{12,6}|{13,"
517 "4:X}";
519 std::string S;
520 llvm::raw_string_ostream Stream(S);
521 Stream << formatv(Intro, std::tuple_size<Tuple>::value,
522 llvm::array_lengthof(Ts))
523 << "\n";
524 Stream << formatv(Header, "Char", "HexInt", "Str", "Ref", "std::str",
525 "double", "float", "pointer", "comma", "exp", "bigint",
526 "bigint2", "limit", "byte")
527 << "\n";
528 for (auto &Item : Ts) {
529 Stream << llvm::apply_tuple(format_tuple(Line), Item) << "\n";
531 Stream.flush();
532 const char *Expected =
533 R"foo(There are {14} items in the tuple, and {2} tuple(s) in the array.
534 Char| HexInt| Str | Ref | std::str | double| float| pointer|comma | exp| bigint| bigint2| limit|byte
535 a| 0x1| Str | | | 3.142|-17.53%|0x00000000|123,456|6.0200E+23|-908,234,908,423| 908234908422234| nan|0xAB
536 x| 0xDDB5B|LongerStr | Strin | std::string | -2.700| 8.21%|0x00000000|0 |6.6200E-34|-908,234,908,423| 908234908422234| INF| 0x0
537 )foo";
539 EXPECT_EQ(Expected, S);
542 TEST(FormatVariadicTest, Range) {
543 std::vector<int> IntRange = {1, 1, 2, 3, 5, 8, 13};
545 // 1. Simple range with default separator and element style.
546 EXPECT_EQ("1, 1, 2, 3, 5, 8, 13",
547 formatv("{0}", make_range(IntRange.begin(), IntRange.end())).str());
548 EXPECT_EQ("1, 2, 3, 5, 8",
549 formatv("{0}", make_range(IntRange.begin() + 1, IntRange.end() - 1))
550 .str());
552 // 2. Non-default separator
553 EXPECT_EQ(
554 "1/1/2/3/5/8/13",
555 formatv("{0:$[/]}", make_range(IntRange.begin(), IntRange.end())).str());
557 // 3. Default separator, non-default element style.
558 EXPECT_EQ(
559 "0x1, 0x1, 0x2, 0x3, 0x5, 0x8, 0xd",
560 formatv("{0:@[x]}", make_range(IntRange.begin(), IntRange.end())).str());
562 // 4. Non-default separator and element style.
563 EXPECT_EQ(
564 "0x1 + 0x1 + 0x2 + 0x3 + 0x5 + 0x8 + 0xd",
565 formatv("{0:$[ + ]@[x]}", make_range(IntRange.begin(), IntRange.end()))
566 .str());
568 // 5. Element style and/or separator using alternate delimeters to allow using
569 // delimeter characters as part of the separator.
570 EXPECT_EQ(
571 "<0x1><0x1><0x2><0x3><0x5><0x8><0xd>",
572 formatv("<{0:$[><]@(x)}>", make_range(IntRange.begin(), IntRange.end()))
573 .str());
574 EXPECT_EQ(
575 "[0x1][0x1][0x2][0x3][0x5][0x8][0xd]",
576 formatv("[{0:$(][)@[x]}]", make_range(IntRange.begin(), IntRange.end()))
577 .str());
578 EXPECT_EQ(
579 "(0x1)(0x1)(0x2)(0x3)(0x5)(0x8)(0xd)",
580 formatv("({0:$<)(>@<x>})", make_range(IntRange.begin(), IntRange.end()))
581 .str());
583 // 5. Empty range.
584 EXPECT_EQ("", formatv("{0:$[+]@[x]}",
585 make_range(IntRange.begin(), IntRange.begin()))
586 .str());
588 // 6. Empty separator and style.
589 EXPECT_EQ("11235813",
590 formatv("{0:$[]@<>}", make_range(IntRange.begin(), IntRange.end()))
591 .str());
594 TEST(FormatVariadicTest, Adapter) {
595 class Negative : public FormatAdapter<int> {
596 public:
597 explicit Negative(int N) : FormatAdapter<int>(std::move(N)) {}
598 void format(raw_ostream &S, StringRef Options) override { S << -Item; }
601 EXPECT_EQ("-7", formatv("{0}", Negative(7)).str());
603 int N = 171;
605 EXPECT_EQ(" 171 ",
606 formatv("{0}", fmt_align(N, AlignStyle::Center, 7)).str());
607 EXPECT_EQ("--171--",
608 formatv("{0}", fmt_align(N, AlignStyle::Center, 7, '-')).str());
609 EXPECT_EQ(" 171 ", formatv("{0}", fmt_pad(N, 1, 3)).str());
610 EXPECT_EQ("171171171171171", formatv("{0}", fmt_repeat(N, 5)).str());
612 EXPECT_EQ(" ABABABABAB ",
613 formatv("{0:X-}", fmt_pad(fmt_repeat(N, 5), 1, 3)).str());
614 EXPECT_EQ(" AB AB AB AB AB ",
615 formatv("{0,=34:X-}", fmt_repeat(fmt_pad(N, 1, 3), 5)).str());
618 TEST(FormatVariadicTest, MoveConstructor) {
619 auto fmt = formatv("{0} {1}", 1, 2);
620 auto fmt2 = std::move(fmt);
621 std::string S = fmt2;
622 EXPECT_EQ("1 2", S);
624 TEST(FormatVariadicTest, ImplicitConversions) {
625 std::string S = formatv("{0} {1}", 1, 2);
626 EXPECT_EQ("1 2", S);
628 SmallString<4> S2 = formatv("{0} {1}", 1, 2);
629 EXPECT_EQ("1 2", S2);
632 TEST(FormatVariadicTest, FormatAdapter) {
633 EXPECT_EQ("Format", formatv("{0}", Format(1)).str());
635 Format var(1);
636 EXPECT_EQ("Format", formatv("{0}", var).str());
637 EXPECT_EQ("Format", formatv("{0}", std::move(var)).str());
639 // Not supposed to compile
640 // const Format cvar(1);
641 // EXPECT_EQ("Format", formatv("{0}", cvar).str());
644 TEST(FormatVariadicTest, FormatFormatvObject) {
645 EXPECT_EQ("Format", formatv("F{0}t", formatv("o{0}a", "rm")).str());
646 EXPECT_EQ("[ ! ]", formatv("[{0,+5}]", formatv("{0,-2}", "!")).str());
649 namespace {
650 struct Recorder {
651 int Copied = 0, Moved = 0;
652 Recorder() = default;
653 Recorder(const Recorder &Copy) : Copied(1 + Copy.Copied), Moved(Copy.Moved) {}
654 Recorder(const Recorder &&Move)
655 : Copied(Move.Copied), Moved(1 + Move.Moved) {}
657 } // namespace
658 namespace llvm {
659 template <> struct format_provider<Recorder> {
660 static void format(const Recorder &R, raw_ostream &OS, StringRef style) {
661 OS << R.Copied << "C " << R.Moved << "M";
664 } // namespace
666 TEST(FormatVariadicTest, CopiesAndMoves) {
667 Recorder R;
668 EXPECT_EQ("0C 0M", formatv("{0}", R).str());
669 EXPECT_EQ("0C 3M", formatv("{0}", std::move(R)).str());
670 EXPECT_EQ("0C 3M", formatv("{0}", Recorder()).str());
671 EXPECT_EQ(0, R.Copied);
672 EXPECT_EQ(0, R.Moved);
675 namespace adl {
676 struct X {};
677 raw_ostream &operator<<(raw_ostream &OS, const X &) { return OS << "X"; }
678 } // namespace adl
679 TEST(FormatVariadicTest, FormatStreamable) {
680 adl::X X;
681 EXPECT_EQ("X", formatv("{0}", X).str());
684 TEST(FormatVariadicTest, FormatError) {
685 auto E1 = make_error<StringError>("X", inconvertibleErrorCode());
686 EXPECT_EQ("X", formatv("{0}", E1).str());
687 EXPECT_TRUE(E1.isA<StringError>()); // not consumed
688 EXPECT_EQ("X", formatv("{0}", fmt_consume(std::move(E1))).str());
689 EXPECT_FALSE(E1.isA<StringError>()); // consumed