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