1 //===- FormatVariadicTest.cpp - Unit tests for string formatting ----------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "llvm/Support/FormatVariadic.h"
10 #include "llvm/Support/Error.h"
11 #include "llvm/Support/FormatAdapters.h"
12 #include "gmock/gmock.h"
13 #include "gtest/gtest.h"
16 using ::testing::HasSubstr
;
18 // Compile-time tests templates in the detail namespace.
20 struct Format
: public FormatAdapter
<int> {
21 Format(int N
) : FormatAdapter
<int>(std::move(N
)) {}
22 void format(raw_ostream
&OS
, StringRef Opt
) override
{ OS
<< "Format"; }
25 using support::detail::uses_format_member
;
26 using support::detail::uses_missing_provider
;
28 static_assert(uses_format_member
<Format
>::value
, "");
29 static_assert(uses_format_member
<Format
&>::value
, "");
30 static_assert(uses_format_member
<Format
&&>::value
, "");
31 static_assert(uses_format_member
<const Format
>::value
, "");
32 static_assert(uses_format_member
<const Format
&>::value
, "");
33 static_assert(uses_format_member
<const volatile Format
>::value
, "");
34 static_assert(uses_format_member
<const volatile Format
&>::value
, "");
37 static_assert(uses_missing_provider
<NoFormat
>::value
, "");
40 // Helper to parse format string with no validation.
41 static SmallVector
<ReplacementItem
, 2> parseFormatString(StringRef Fmt
) {
42 return formatv_object_base::parseFormatString(Fmt
, 0, false);
45 TEST(FormatVariadicTest
, EmptyFormatString
) {
46 auto Replacements
= parseFormatString("");
47 EXPECT_EQ(0U, Replacements
.size());
50 TEST(FormatVariadicTest
, NoReplacements
) {
51 const StringRef kFormatString
= "This is a test";
52 auto Replacements
= parseFormatString(kFormatString
);
53 ASSERT_EQ(1U, Replacements
.size());
54 EXPECT_EQ(kFormatString
, Replacements
[0].Spec
);
55 EXPECT_EQ(ReplacementType::Literal
, Replacements
[0].Type
);
58 TEST(FormatVariadicTest
, EscapedBrace
) {
59 // {{ should be replaced with {
60 auto Replacements
= parseFormatString("{{");
61 ASSERT_EQ(1u, Replacements
.size());
62 EXPECT_EQ("{", Replacements
[0].Spec
);
63 EXPECT_EQ(ReplacementType::Literal
, Replacements
[0].Type
);
65 // An even number N of braces should be replaced with N/2 braces.
66 Replacements
= parseFormatString("{{{{{{");
67 ASSERT_EQ(1u, Replacements
.size());
68 EXPECT_EQ("{{{", Replacements
[0].Spec
);
69 EXPECT_EQ(ReplacementType::Literal
, Replacements
[0].Type
);
71 // } does not require doubling up.
72 Replacements
= parseFormatString("}");
73 ASSERT_EQ(1u, Replacements
.size());
74 EXPECT_EQ("}", Replacements
[0].Spec
);
75 EXPECT_EQ(ReplacementType::Literal
, Replacements
[0].Type
);
77 // } does not require doubling up.
78 Replacements
= parseFormatString("}}}");
79 ASSERT_EQ(1u, Replacements
.size());
80 EXPECT_EQ("}}}", Replacements
[0].Spec
);
81 EXPECT_EQ(ReplacementType::Literal
, Replacements
[0].Type
);
84 TEST(FormatVariadicTest
, ValidReplacementSequence
) {
85 // 1. Simple replacement - parameter index only
86 auto Replacements
= parseFormatString("{0}");
87 ASSERT_EQ(1u, Replacements
.size());
88 EXPECT_EQ(ReplacementType::Format
, Replacements
[0].Type
);
89 EXPECT_EQ(0u, Replacements
[0].Index
);
90 EXPECT_EQ(0u, Replacements
[0].Width
);
91 EXPECT_EQ("", Replacements
[0].Options
);
93 Replacements
= parseFormatString("{1}");
94 ASSERT_EQ(1u, Replacements
.size());
95 EXPECT_EQ(ReplacementType::Format
, Replacements
[0].Type
);
96 EXPECT_EQ(1u, Replacements
[0].Index
);
97 EXPECT_EQ(0u, Replacements
[0].Width
);
98 EXPECT_EQ(AlignStyle::Right
, Replacements
[0].Where
);
99 EXPECT_EQ("", Replacements
[0].Options
);
101 // 2. Parameter index with right alignment
102 Replacements
= 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].Width
);
107 EXPECT_EQ(AlignStyle::Right
, Replacements
[0].Where
);
108 EXPECT_EQ("", Replacements
[0].Options
);
110 // 3. And left alignment
111 Replacements
= parseFormatString("{0,-3}");
112 ASSERT_EQ(1u, Replacements
.size());
113 EXPECT_EQ(ReplacementType::Format
, Replacements
[0].Type
);
114 EXPECT_EQ(0u, Replacements
[0].Index
);
115 EXPECT_EQ(3u, Replacements
[0].Width
);
116 EXPECT_EQ(AlignStyle::Left
, Replacements
[0].Where
);
117 EXPECT_EQ("", Replacements
[0].Options
);
119 // 4. And center alignment
120 Replacements
= parseFormatString("{0,=3}");
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].Width
);
125 EXPECT_EQ(AlignStyle::Center
, Replacements
[0].Where
);
126 EXPECT_EQ("", Replacements
[0].Options
);
128 // 4. Parameter index with option string
129 Replacements
= parseFormatString("{0: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(0u, Replacements
[0].Width
);
134 EXPECT_EQ(AlignStyle::Right
, Replacements
[0].Where
);
135 EXPECT_EQ("foo", Replacements
[0].Options
);
137 // 5. Parameter index with alignment before option string
138 Replacements
= parseFormatString("{0,-3:foo}");
139 ASSERT_EQ(1u, Replacements
.size());
140 EXPECT_EQ(ReplacementType::Format
, Replacements
[0].Type
);
141 EXPECT_EQ(0u, Replacements
[0].Index
);
142 EXPECT_EQ(3u, Replacements
[0].Width
);
143 EXPECT_EQ(AlignStyle::Left
, Replacements
[0].Where
);
144 EXPECT_EQ("foo", Replacements
[0].Options
);
146 // 7. Parameter indices, options, and alignment can all have whitespace.
147 Replacements
= parseFormatString("{ 0, -3 : foo }");
148 ASSERT_EQ(1u, Replacements
.size());
149 EXPECT_EQ(ReplacementType::Format
, Replacements
[0].Type
);
150 EXPECT_EQ(0u, Replacements
[0].Index
);
151 EXPECT_EQ(3u, Replacements
[0].Width
);
152 EXPECT_EQ(AlignStyle::Left
, Replacements
[0].Where
);
153 EXPECT_EQ(" foo ", Replacements
[0].Options
);
155 // 8. Everything after the first option specifier is part of the style, even
156 // if it contains another option specifier.
157 Replacements
= parseFormatString("{0:0:1}");
158 ASSERT_EQ(1u, Replacements
.size());
159 EXPECT_EQ("0:0:1", Replacements
[0].Spec
);
160 EXPECT_EQ(ReplacementType::Format
, Replacements
[0].Type
);
161 EXPECT_EQ(0u, Replacements
[0].Index
);
162 EXPECT_EQ(0u, Replacements
[0].Width
);
163 EXPECT_EQ(AlignStyle::Right
, Replacements
[0].Where
);
164 EXPECT_EQ("0:1", Replacements
[0].Options
);
166 // 9. Custom padding character
167 Replacements
= parseFormatString("{0,p+4:foo}");
168 ASSERT_EQ(1u, Replacements
.size());
169 EXPECT_EQ("0,p+4:foo", Replacements
[0].Spec
);
170 EXPECT_EQ(ReplacementType::Format
, Replacements
[0].Type
);
171 EXPECT_EQ(0u, Replacements
[0].Index
);
172 EXPECT_EQ(4u, Replacements
[0].Width
);
173 EXPECT_EQ(AlignStyle::Right
, Replacements
[0].Where
);
174 EXPECT_EQ('p', Replacements
[0].Pad
);
175 EXPECT_EQ("foo", Replacements
[0].Options
);
177 // Format string special characters are allowed as padding character
178 Replacements
= parseFormatString("{0,-+4:foo}");
179 ASSERT_EQ(1u, Replacements
.size());
180 EXPECT_EQ("0,-+4:foo", Replacements
[0].Spec
);
181 EXPECT_EQ(ReplacementType::Format
, Replacements
[0].Type
);
182 EXPECT_EQ(0u, Replacements
[0].Index
);
183 EXPECT_EQ(4u, Replacements
[0].Width
);
184 EXPECT_EQ(AlignStyle::Right
, Replacements
[0].Where
);
185 EXPECT_EQ('-', Replacements
[0].Pad
);
186 EXPECT_EQ("foo", Replacements
[0].Options
);
188 Replacements
= parseFormatString("{0,+-4:foo}");
189 ASSERT_EQ(1u, Replacements
.size());
190 EXPECT_EQ("0,+-4:foo", Replacements
[0].Spec
);
191 EXPECT_EQ(ReplacementType::Format
, Replacements
[0].Type
);
192 EXPECT_EQ(0u, Replacements
[0].Index
);
193 EXPECT_EQ(4u, Replacements
[0].Width
);
194 EXPECT_EQ(AlignStyle::Left
, Replacements
[0].Where
);
195 EXPECT_EQ('+', Replacements
[0].Pad
);
196 EXPECT_EQ("foo", Replacements
[0].Options
);
198 Replacements
= parseFormatString("{0,==4:foo}");
199 ASSERT_EQ(1u, Replacements
.size());
200 EXPECT_EQ("0,==4:foo", Replacements
[0].Spec
);
201 EXPECT_EQ(ReplacementType::Format
, Replacements
[0].Type
);
202 EXPECT_EQ(0u, Replacements
[0].Index
);
203 EXPECT_EQ(4u, Replacements
[0].Width
);
204 EXPECT_EQ(AlignStyle::Center
, Replacements
[0].Where
);
205 EXPECT_EQ('=', Replacements
[0].Pad
);
206 EXPECT_EQ("foo", Replacements
[0].Options
);
208 Replacements
= parseFormatString("{0,:=4:foo}");
209 ASSERT_EQ(1u, Replacements
.size());
210 EXPECT_EQ("0,:=4:foo", Replacements
[0].Spec
);
211 EXPECT_EQ(ReplacementType::Format
, Replacements
[0].Type
);
212 EXPECT_EQ(0u, Replacements
[0].Index
);
213 EXPECT_EQ(4u, Replacements
[0].Width
);
214 EXPECT_EQ(AlignStyle::Center
, Replacements
[0].Where
);
215 EXPECT_EQ(':', Replacements
[0].Pad
);
216 EXPECT_EQ("foo", Replacements
[0].Options
);
219 TEST(FormatVariadicTest
, DefaultReplacementValues
) {
220 // 2. If options string is missing, it defaults to empty.
221 auto Replacements
= parseFormatString("{0,3}");
222 ASSERT_EQ(1u, Replacements
.size());
223 EXPECT_EQ(ReplacementType::Format
, Replacements
[0].Type
);
224 EXPECT_EQ(0u, Replacements
[0].Index
);
225 EXPECT_EQ(3u, Replacements
[0].Width
);
226 EXPECT_EQ("", Replacements
[0].Options
);
228 // Including if the colon is present but contains no text.
229 Replacements
= parseFormatString("{0,3:}");
230 ASSERT_EQ(1u, Replacements
.size());
231 EXPECT_EQ(ReplacementType::Format
, Replacements
[0].Type
);
232 EXPECT_EQ(0u, Replacements
[0].Index
);
233 EXPECT_EQ(3u, Replacements
[0].Width
);
234 EXPECT_EQ("", Replacements
[0].Options
);
236 // 3. If alignment is missing, it defaults to 0, right, space
237 Replacements
= parseFormatString("{0:foo}");
238 ASSERT_EQ(1u, Replacements
.size());
239 EXPECT_EQ(ReplacementType::Format
, Replacements
[0].Type
);
240 EXPECT_EQ(AlignStyle::Right
, Replacements
[0].Where
);
241 EXPECT_EQ(' ', Replacements
[0].Pad
);
242 EXPECT_EQ(0u, Replacements
[0].Index
);
243 EXPECT_EQ(0u, Replacements
[0].Width
);
244 EXPECT_EQ("foo", Replacements
[0].Options
);
247 TEST(FormatVariadicTest
, MultipleReplacements
) {
248 auto Replacements
= parseFormatString("{0} {1:foo}-{2,-3:bar}");
249 ASSERT_EQ(5u, Replacements
.size());
251 EXPECT_EQ(ReplacementType::Format
, Replacements
[0].Type
);
252 EXPECT_EQ(0u, Replacements
[0].Index
);
253 EXPECT_EQ(0u, Replacements
[0].Width
);
254 EXPECT_EQ(AlignStyle::Right
, Replacements
[0].Where
);
255 EXPECT_EQ("", Replacements
[0].Options
);
258 EXPECT_EQ(ReplacementType::Literal
, Replacements
[1].Type
);
259 EXPECT_EQ(" ", Replacements
[1].Spec
);
261 // {1:foo} - Options=foo
262 EXPECT_EQ(ReplacementType::Format
, Replacements
[2].Type
);
263 EXPECT_EQ(1u, Replacements
[2].Index
);
264 EXPECT_EQ(0u, Replacements
[2].Width
);
265 EXPECT_EQ(AlignStyle::Right
, Replacements
[2].Where
);
266 EXPECT_EQ("foo", Replacements
[2].Options
);
269 EXPECT_EQ(ReplacementType::Literal
, Replacements
[3].Type
);
270 EXPECT_EQ("-", Replacements
[3].Spec
);
272 // {2,-3:bar} - Options=bar, Align=-3
273 EXPECT_EQ(ReplacementType::Format
, Replacements
[4].Type
);
274 EXPECT_EQ(2u, Replacements
[4].Index
);
275 EXPECT_EQ(3u, Replacements
[4].Width
);
276 EXPECT_EQ(AlignStyle::Left
, Replacements
[4].Where
);
277 EXPECT_EQ("bar", Replacements
[4].Options
);
280 TEST(FormatVariadicTest
, AutomaticIndices
) {
281 auto Replacements
= parseFormatString("{}");
282 ASSERT_EQ(1u, Replacements
.size());
283 EXPECT_EQ(ReplacementType::Format
, Replacements
[0].Type
);
284 EXPECT_EQ(0u, Replacements
[0].Index
);
286 Replacements
= parseFormatString("{}bar{}");
287 ASSERT_EQ(3u, Replacements
.size());
288 EXPECT_EQ(ReplacementType::Format
, Replacements
[0].Type
);
289 EXPECT_EQ(0u, Replacements
[0].Index
);
290 EXPECT_EQ(ReplacementType::Literal
, Replacements
[1].Type
);
291 EXPECT_EQ("bar", Replacements
[1].Spec
);
292 EXPECT_EQ(ReplacementType::Format
, Replacements
[2].Type
);
293 EXPECT_EQ(1u, Replacements
[2].Index
);
295 Replacements
= parseFormatString("{}{}");
296 ASSERT_EQ(2u, Replacements
.size());
297 EXPECT_EQ(ReplacementType::Format
, Replacements
[0].Type
);
298 EXPECT_EQ(0u, Replacements
[0].Index
);
299 EXPECT_EQ(ReplacementType::Format
, Replacements
[1].Type
);
300 EXPECT_EQ(1u, Replacements
[1].Index
);
302 Replacements
= parseFormatString("{}{:foo}{,-3:bar}");
303 ASSERT_EQ(3u, Replacements
.size());
304 EXPECT_EQ(ReplacementType::Format
, Replacements
[0].Type
);
305 EXPECT_EQ(0u, Replacements
[0].Index
);
306 EXPECT_EQ(0u, Replacements
[0].Width
);
307 EXPECT_EQ(AlignStyle::Right
, Replacements
[0].Where
);
308 EXPECT_EQ("", Replacements
[0].Options
);
310 // {:foo} - Options=foo
311 EXPECT_EQ(ReplacementType::Format
, Replacements
[1].Type
);
312 EXPECT_EQ(1u, Replacements
[1].Index
);
313 EXPECT_EQ(0u, Replacements
[1].Width
);
314 EXPECT_EQ(AlignStyle::Right
, Replacements
[1].Where
);
315 EXPECT_EQ("foo", Replacements
[1].Options
);
317 // {,-3:bar} - Options=bar, Align=-3
318 EXPECT_EQ(ReplacementType::Format
, Replacements
[2].Type
);
319 EXPECT_EQ(2u, Replacements
[2].Index
);
320 EXPECT_EQ(3u, Replacements
[2].Width
);
321 EXPECT_EQ(AlignStyle::Left
, Replacements
[2].Where
);
322 EXPECT_EQ("bar", Replacements
[2].Options
);
325 TEST(FormatVariadicTest
, FormatNoReplacements
) {
326 EXPECT_EQ("", formatv("").str());
327 EXPECT_EQ("Test", formatv("Test").str());
330 TEST(FormatVariadicTest
, FormatBasicTypesOneReplacement
) {
331 EXPECT_EQ("1", formatv("{0}", 1).str());
332 EXPECT_EQ("c", formatv("{0}", 'c').str());
333 EXPECT_EQ("-3", formatv("{0}", -3).str());
334 EXPECT_EQ("Test", formatv("{0}", "Test").str());
335 EXPECT_EQ("Test2", formatv("{0}", StringRef("Test2")).str());
336 EXPECT_EQ("Test3", formatv("{0}", std::string("Test3")).str());
339 TEST(FormatVariadicTest
, FormatAutomaticIndices
) {
340 EXPECT_EQ("1", formatv("{}", 1).str());
341 EXPECT_EQ("c1", formatv("{}{}", 'c', 1).str());
342 EXPECT_EQ("c-1rrr-0xFF", formatv("{}-{,r-4}-{:X}", 'c', 1, 255).str());
345 TEST(FormatVariadicTest
, IntegralHexFormatting
) {
346 // 1. Trivial cases. Make sure hex is not the default.
347 EXPECT_EQ("0", formatv("{0}", 0).str());
348 EXPECT_EQ("2748", formatv("{0}", 0xABC).str());
349 EXPECT_EQ("-2748", formatv("{0}", -0xABC).str());
351 // 3. various hex prefixes.
352 EXPECT_EQ("0xFF", formatv("{0:X}", 255).str());
353 EXPECT_EQ("0xFF", formatv("{0:X+}", 255).str());
354 EXPECT_EQ("0xff", formatv("{0:x}", 255).str());
355 EXPECT_EQ("0xff", formatv("{0:x+}", 255).str());
356 EXPECT_EQ("FF", formatv("{0:X-}", 255).str());
357 EXPECT_EQ("ff", formatv("{0:x-}", 255).str());
359 // 5. Precision pads left of the most significant digit but right of the
360 // prefix (if one exists).
361 EXPECT_EQ("0xFF", formatv("{0:X2}", 255).str());
362 EXPECT_EQ("0xFF", formatv("{0:X+2}", 255).str());
363 EXPECT_EQ("0x0ff", formatv("{0:x3}", 255).str());
364 EXPECT_EQ("0x0ff", formatv("{0:x+3}", 255).str());
365 EXPECT_EQ("00FF", formatv("{0:X-4}", 255).str());
366 EXPECT_EQ("00ff", formatv("{0:x-4}", 255).str());
368 // 6. Try some larger types.
369 EXPECT_EQ("0xDEADBEEFDEADBEEF",
370 formatv("{0:X16}", -2401053088876216593LL).str());
371 EXPECT_EQ("0xFEEBDAEDFEEBDAED",
372 formatv("{0:X16}", 0xFEEBDAEDFEEBDAEDULL
).str());
373 EXPECT_EQ("0x00000000DEADBEEF", formatv("{0:X16}", 0xDEADBEEF).str());
375 // 7. Padding should take into account the prefix
376 EXPECT_EQ("0xff", formatv("{0,4:x}", 255).str());
377 EXPECT_EQ(" 0xff", formatv("{0,5:x+}", 255).str());
378 EXPECT_EQ(" FF", formatv("{0,4:X-}", 255).str());
379 EXPECT_EQ(" ff", formatv("{0,5:x-}", 255).str());
381 // 8. Including when it's been zero-padded
382 EXPECT_EQ(" 0x0ff", formatv("{0,7:x3}", 255).str());
383 EXPECT_EQ(" 0x00ff", formatv("{0,7:x+4}", 255).str());
384 EXPECT_EQ(" 000FF", formatv("{0,7:X-5}", 255).str());
385 EXPECT_EQ(" 0000ff", formatv("{0,7:x-6}", 255).str());
387 // 9. Precision with default format specifier should work too
388 EXPECT_EQ(" 255", formatv("{0,7:3}", 255).str());
389 EXPECT_EQ(" 0255", formatv("{0,7:4}", 255).str());
390 EXPECT_EQ(" 00255", formatv("{0,7:5}", 255).str());
391 EXPECT_EQ(" 000255", formatv("{0,7:6}", 255).str());
394 TEST(FormatVariadicTest
, PointerFormatting
) {
395 // 1. Trivial cases. Hex is default. Default Precision is pointer width.
396 if (sizeof(void *) == 4) {
397 EXPECT_EQ("0x00000000", formatv("{0}", (void *)0).str());
398 EXPECT_EQ("0x00000ABC", formatv("{0}", (void *)0xABC).str());
400 EXPECT_EQ("0x0000000000000000", formatv("{0}", (void *)0).str());
401 EXPECT_EQ("0x0000000000000ABC", formatv("{0}", (void *)0xABC).str());
404 // 2. But we can reduce the precision explicitly.
405 EXPECT_EQ("0x0", formatv("{0:0}", (void *)0).str());
406 EXPECT_EQ("0xABC", formatv("{0:0}", (void *)0xABC).str());
407 EXPECT_EQ("0x0000", formatv("{0:4}", (void *)0).str());
408 EXPECT_EQ("0x0ABC", formatv("{0:4}", (void *)0xABC).str());
410 // 3. various hex prefixes.
411 EXPECT_EQ("0x0ABC", formatv("{0:X4}", (void *)0xABC).str());
412 EXPECT_EQ("0x0abc", formatv("{0:x4}", (void *)0xABC).str());
413 EXPECT_EQ("0ABC", formatv("{0:X-4}", (void *)0xABC).str());
414 EXPECT_EQ("0abc", formatv("{0:x-4}", (void *)0xABC).str());
417 TEST(FormatVariadicTest
, IntegralNumberFormatting
) {
418 // 1. Test comma grouping with default widths and precisions.
419 EXPECT_EQ("0", formatv("{0:N}", 0).str());
420 EXPECT_EQ("10", formatv("{0:N}", 10).str());
421 EXPECT_EQ("100", formatv("{0:N}", 100).str());
422 EXPECT_EQ("1,000", formatv("{0:N}", 1000).str());
423 EXPECT_EQ("1,234,567,890", formatv("{0:N}", 1234567890).str());
424 EXPECT_EQ("-10", formatv("{0:N}", -10).str());
425 EXPECT_EQ("-100", formatv("{0:N}", -100).str());
426 EXPECT_EQ("-1,000", formatv("{0:N}", -1000).str());
427 EXPECT_EQ("-1,234,567,890", formatv("{0:N}", -1234567890).str());
429 // 2. If there is no comma, width and precision pad to the same absolute
431 EXPECT_EQ(" 1", formatv("{0,2:N}", 1).str());
433 // 3. But if there is a comma or negative sign, width factors them in but
434 // precision doesn't.
435 EXPECT_EQ(" 1,000", formatv("{0,6:N}", 1000).str());
436 EXPECT_EQ(" -1,000", formatv("{0,7:N}", -1000).str());
438 // 4. Large widths all line up.
439 EXPECT_EQ(" 1,000", formatv("{0,11:N}", 1000).str());
440 EXPECT_EQ(" -1,000", formatv("{0,11:N}", -1000).str());
441 EXPECT_EQ(" -100,000", formatv("{0,11:N}", -100000).str());
444 TEST(FormatVariadicTest
, StringFormatting
) {
445 const char FooArray
[] = "FooArray";
446 const char *FooPtr
= "FooPtr";
447 llvm::StringRef
FooRef("FooRef");
448 constexpr StringLiteral
FooLiteral("FooLiteral");
449 std::string
FooString("FooString");
450 // 1. Test that we can print various types of strings.
451 EXPECT_EQ(FooArray
, formatv("{0}", FooArray
).str());
452 EXPECT_EQ(FooPtr
, formatv("{0}", FooPtr
).str());
453 EXPECT_EQ(FooRef
, formatv("{0}", FooRef
).str());
454 EXPECT_EQ(FooLiteral
, formatv("{0}", FooLiteral
).str());
455 EXPECT_EQ(FooString
, formatv("{0}", FooString
).str());
457 // 2. Test that the precision specifier prints the correct number of
459 EXPECT_EQ("FooA", formatv("{0:4}", FooArray
).str());
460 EXPECT_EQ("FooP", formatv("{0:4}", FooPtr
).str());
461 EXPECT_EQ("FooR", formatv("{0:4}", FooRef
).str());
462 EXPECT_EQ("FooS", formatv("{0:4}", FooString
).str());
464 // 3. And that padding works.
465 EXPECT_EQ(" FooA", formatv("{0,6:4}", FooArray
).str());
466 EXPECT_EQ(" FooP", formatv("{0,6:4}", FooPtr
).str());
467 EXPECT_EQ(" FooR", formatv("{0,6:4}", FooRef
).str());
468 EXPECT_EQ(" FooS", formatv("{0,6:4}", FooString
).str());
471 TEST(FormatVariadicTest
, CharFormatting
) {
472 // 1. Not much to see here. Just print a char with and without padding.
473 EXPECT_EQ("C", formatv("{0}", 'C').str());
474 EXPECT_EQ(" C", formatv("{0,3}", 'C').str());
476 // 2. char is really an integral type though, where the only difference is
477 // that the "default" is to print the ASCII. So if a non-default presentation
478 // specifier exists, it should print as an integer.
479 EXPECT_EQ("37", formatv("{0:D}", (char)37).str());
480 EXPECT_EQ(" 037", formatv("{0,5:D3}", (char)37).str());
483 TEST(FormatVariadicTest
, BoolTest
) {
484 // 1. Default style is lowercase text (same as 't')
485 EXPECT_EQ("true", formatv("{0}", true).str());
486 EXPECT_EQ("false", formatv("{0}", false).str());
487 EXPECT_EQ("true", formatv("{0:t}", true).str());
488 EXPECT_EQ("false", formatv("{0:t}", false).str());
490 // 2. T - uppercase text
491 EXPECT_EQ("TRUE", formatv("{0:T}", true).str());
492 EXPECT_EQ("FALSE", formatv("{0:T}", false).str());
494 // 3. D / d - integral
495 EXPECT_EQ("1", formatv("{0:D}", true).str());
496 EXPECT_EQ("0", formatv("{0:D}", false).str());
497 EXPECT_EQ("1", formatv("{0:d}", true).str());
498 EXPECT_EQ("0", formatv("{0:d}", false).str());
500 // 4. Y - uppercase yes/no
501 EXPECT_EQ("YES", formatv("{0:Y}", true).str());
502 EXPECT_EQ("NO", formatv("{0:Y}", false).str());
504 // 5. y - lowercase yes/no
505 EXPECT_EQ("yes", formatv("{0:y}", true).str());
506 EXPECT_EQ("no", formatv("{0:y}", false).str());
509 TEST(FormatVariadicTest
, DoubleFormatting
) {
510 // Test exponents, fixed point, and percent formatting.
512 // 1. Signed, unsigned, and zero exponent format.
513 EXPECT_EQ("0.000000E+00", formatv("{0:E}", 0.0).str());
514 EXPECT_EQ("-0.000000E+00", formatv("{0:E}", -0.0).str());
515 EXPECT_EQ("1.100000E+00", formatv("{0:E}", 1.1).str());
516 EXPECT_EQ("-1.100000E+00", formatv("{0:E}", -1.1).str());
517 EXPECT_EQ("1.234568E+03", formatv("{0:E}", 1234.5678).str());
518 EXPECT_EQ("-1.234568E+03", formatv("{0:E}", -1234.5678).str());
519 EXPECT_EQ("1.234568E-03", formatv("{0:E}", .0012345678).str());
520 EXPECT_EQ("-1.234568E-03", formatv("{0:E}", -.0012345678).str());
522 // 2. With padding and precision.
523 EXPECT_EQ(" 0.000E+00", formatv("{0,11:E3}", 0.0).str());
524 EXPECT_EQ(" -1.100E+00", formatv("{0,11:E3}", -1.1).str());
525 EXPECT_EQ(" 1.235E+03", formatv("{0,11:E3}", 1234.5678).str());
526 EXPECT_EQ(" -1.235E-03", formatv("{0,11:E3}", -.0012345678).str());
528 // 3. Signed, unsigned, and zero fixed point format.
529 EXPECT_EQ("0.00", formatv("{0:F}", 0.0).str());
530 EXPECT_EQ("-0.00", formatv("{0:F}", -0.0).str());
531 EXPECT_EQ("1.10", formatv("{0:F}", 1.1).str());
532 EXPECT_EQ("-1.10", formatv("{0:F}", -1.1).str());
533 EXPECT_EQ("1234.57", formatv("{0:F}", 1234.5678).str());
534 EXPECT_EQ("-1234.57", formatv("{0:F}", -1234.5678).str());
535 EXPECT_EQ("0.00", formatv("{0:F}", .0012345678).str());
536 EXPECT_EQ("-0.00", formatv("{0:F}", -.0012345678).str());
538 // 2. With padding and precision.
539 EXPECT_EQ(" 0.000", formatv("{0,8:F3}", 0.0).str());
540 EXPECT_EQ(" -1.100", formatv("{0,8:F3}", -1.1).str());
541 EXPECT_EQ("1234.568", formatv("{0,8:F3}", 1234.5678).str());
542 EXPECT_EQ(" -0.001", formatv("{0,8:F3}", -.0012345678).str());
545 TEST(FormatVariadicTest
, CustomPaddingCharacter
) {
546 // 1. Padding with custom character
547 EXPECT_EQ("==123", formatv("{0,=+5}", 123).str());
548 EXPECT_EQ("=123=", formatv("{0,==5}", 123).str());
549 EXPECT_EQ("123==", formatv("{0,=-5}", 123).str());
551 // 2. Combined with zero padding
552 EXPECT_EQ("=00123=", formatv("{0,==7:5}", 123).str());
555 struct format_tuple
{
557 explicit format_tuple(const char *Fmt
) : Fmt(Fmt
) {}
559 template <typename
... Ts
> auto operator()(Ts
&&... Values
) const {
560 return formatv(Fmt
, std::forward
<Ts
>(Values
)...);
564 TEST(FormatVariadicTest
, BigTest
) {
566 std::tuple
<char, int, const char *, StringRef
, std::string
, double, float,
567 void *, int, double, int64_t, uint64_t, double, uint8_t>;
569 Tuple('a', 1, "Str", StringRef(), std::string(), 3.14159, -.17532f
,
570 (void *)nullptr, 123456, 6.02E23
, -908234908423, 908234908422234,
571 std::numeric_limits
<double>::quiet_NaN(), 0xAB),
572 Tuple('x', 0xDDB5B, "LongerStr", "StringRef", "std::string", -2.7,
573 .08215f
, (void *)nullptr, 0, 6.62E-34, -908234908423,
574 908234908422234, std::numeric_limits
<double>::infinity(), 0x0)};
575 // Test long string formatting with many edge cases combined.
577 "There are {{{0}} items in the tuple, and {{{1}} tuple(s) in the array.";
579 "{0,6}|{1,8}|{2,=10}|{3,=10}|{4,=13}|{5,7}|{6,7}|{7,10}|{8,"
580 "-7}|{9,10}|{10,16}|{11,17}|{12,6}|{13,4}";
582 "{0,6}|{1,8:X}|{2,=10}|{3,=10:5}|{4,=13}|{5,7:3}|{6,7:P2}|{7,"
583 "10:X8}|{8,-7:N}|{9,10:E4}|{10,16:N}|{11,17:D}|{12,6}|{13,"
587 llvm::raw_string_ostream
Stream(S
);
588 Stream
<< formatv(Intro
, std::tuple_size
<Tuple
>::value
, std::size(Ts
))
590 Stream
<< formatv(Header
, "Char", "HexInt", "Str", "Ref", "std::str",
591 "double", "float", "pointer", "comma", "exp", "bigint",
592 "bigint2", "limit", "byte")
594 for (auto &Item
: Ts
) {
595 Stream
<< std::apply(format_tuple(Line
), Item
) << "\n";
597 const char *Expected
=
598 R
"foo(There are {14} items in the tuple, and {2} tuple(s) in the array.
599 Char| HexInt| Str | Ref | std::str | double| float| pointer|comma | exp| bigint| bigint2| limit|byte
600 a| 0x1| Str | | | 3.142|-17.53%|0x00000000|123,456|6.0200E+23|-908,234,908,423| 908234908422234| nan|0xAB
601 x| 0xDDB5B|LongerStr | Strin | std::string | -2.700| 8.21%|0x00000000|0 |6.6200E-34|-908,234,908,423| 908234908422234| INF| 0x0
604 EXPECT_EQ(Expected
, S
);
607 TEST(FormatVariadicTest
, Range
) {
608 std::vector
<int> IntRange
= {1, 1, 2, 3, 5, 8, 13};
610 // 1. Simple range with default separator and element style.
611 EXPECT_EQ("1, 1, 2, 3, 5, 8, 13",
612 formatv("{0}", make_range(IntRange
.begin(), IntRange
.end())).str());
613 EXPECT_EQ("1, 2, 3, 5, 8",
614 formatv("{0}", make_range(IntRange
.begin() + 1, IntRange
.end() - 1))
617 // 2. Non-default separator
620 formatv("{0:$[/]}", make_range(IntRange
.begin(), IntRange
.end())).str());
622 // 3. Default separator, non-default element style.
624 "0x1, 0x1, 0x2, 0x3, 0x5, 0x8, 0xd",
625 formatv("{0:@[x]}", make_range(IntRange
.begin(), IntRange
.end())).str());
627 // 4. Non-default separator and element style.
629 "0x1 + 0x1 + 0x2 + 0x3 + 0x5 + 0x8 + 0xd",
630 formatv("{0:$[ + ]@[x]}", make_range(IntRange
.begin(), IntRange
.end()))
633 // 5. Element style and/or separator using alternate delimeters to allow using
634 // delimeter characters as part of the separator.
636 "<0x1><0x1><0x2><0x3><0x5><0x8><0xd>",
637 formatv("<{0:$[><]@(x)}>", make_range(IntRange
.begin(), IntRange
.end()))
640 "[0x1][0x1][0x2][0x3][0x5][0x8][0xd]",
641 formatv("[{0:$(][)@[x]}]", make_range(IntRange
.begin(), IntRange
.end()))
644 "(0x1)(0x1)(0x2)(0x3)(0x5)(0x8)(0xd)",
645 formatv("({0:$<)(>@<x>})", make_range(IntRange
.begin(), IntRange
.end()))
649 EXPECT_EQ("", formatv("{0:$[+]@[x]}",
650 make_range(IntRange
.begin(), IntRange
.begin()))
653 // 6. Empty separator and style.
654 EXPECT_EQ("11235813",
655 formatv("{0:$[]@<>}", make_range(IntRange
.begin(), IntRange
.end()))
659 TEST(FormatVariadicTest
, Adapter
) {
660 class Negative
: public FormatAdapter
<int> {
662 explicit Negative(int N
) : FormatAdapter
<int>(std::move(N
)) {}
663 void format(raw_ostream
&S
, StringRef Options
) override
{ S
<< -Item
; }
666 EXPECT_EQ("-7", formatv("{0}", Negative(7)).str());
671 formatv("{0}", fmt_align(N
, AlignStyle::Center
, 7)).str());
673 formatv("{0}", fmt_align(N
, AlignStyle::Center
, 7, '-')).str());
674 EXPECT_EQ(" 171 ", formatv("{0}", fmt_pad(N
, 1, 3)).str());
675 EXPECT_EQ("171171171171171", formatv("{0}", fmt_repeat(N
, 5)).str());
677 EXPECT_EQ(" ABABABABAB ",
678 formatv("{0:X-}", fmt_pad(fmt_repeat(N
, 5), 1, 3)).str());
679 EXPECT_EQ(" AB AB AB AB AB ",
680 formatv("{0,=34:X-}", fmt_repeat(fmt_pad(N
, 1, 3), 5)).str());
683 TEST(FormatVariadicTest
, MoveConstructor
) {
684 auto fmt
= formatv("{0} {1}", 1, 2);
685 auto fmt2
= std::move(fmt
);
686 std::string S
= std::string(fmt2
);
689 TEST(FormatVariadicTest
, ImplicitConversions
) {
690 std::string S
= std::string(formatv("{0} {1}", 1, 2));
693 SmallString
<4> S2
= formatv("{0} {1}", 1, 2);
694 EXPECT_EQ("1 2", S2
);
697 TEST(FormatVariadicTest
, FormatAdapter
) {
698 EXPECT_EQ("Format", formatv("{0}", Format(1)).str());
701 EXPECT_EQ("Format", formatv("{0}", var
).str());
702 EXPECT_EQ("Format", formatv("{0}", std::move(var
)).str());
704 // Not supposed to compile
705 // const Format cvar(1);
706 // EXPECT_EQ("Format", formatv("{0}", cvar).str());
709 TEST(FormatVariadicTest
, FormatFormatvObject
) {
710 EXPECT_EQ("Format", formatv("F{0}t", formatv("o{0}a", "rm")).str());
711 EXPECT_EQ("[ ! ]", formatv("[{0,+5}]", formatv("{0,-2}", "!")).str());
716 int Copied
= 0, Moved
= 0;
717 Recorder() = default;
718 Recorder(const Recorder
&Copy
) : Copied(1 + Copy
.Copied
), Moved(Copy
.Moved
) {}
719 Recorder(const Recorder
&&Move
)
720 : Copied(Move
.Copied
), Moved(1 + Move
.Moved
) {}
724 template <> struct format_provider
<Recorder
> {
725 static void format(const Recorder
&R
, raw_ostream
&OS
, StringRef style
) {
726 OS
<< R
.Copied
<< "C " << R
.Moved
<< "M";
731 TEST(FormatVariadicTest
, CopiesAndMoves
) {
733 EXPECT_EQ("0C 0M", formatv("{0}", R
).str());
734 EXPECT_EQ("0C 3M", formatv("{0}", std::move(R
)).str());
735 EXPECT_EQ("0C 3M", formatv("{0}", Recorder()).str());
736 EXPECT_EQ(0, R
.Copied
);
737 EXPECT_EQ(0, R
.Moved
);
742 raw_ostream
&operator<<(raw_ostream
&OS
, const X
&) { return OS
<< "X"; }
744 TEST(FormatVariadicTest
, FormatStreamable
) {
746 EXPECT_EQ("X", formatv("{0}", X
).str());
749 TEST(FormatVariadicTest
, FormatError
) {
750 auto E1
= make_error
<StringError
>("X", inconvertibleErrorCode());
751 EXPECT_EQ("X", formatv("{0}", E1
).str());
752 EXPECT_TRUE(E1
.isA
<StringError
>()); // not consumed
753 EXPECT_EQ("X", formatv("{0}", fmt_consume(std::move(E1
))).str());
754 EXPECT_FALSE(E1
.isA
<StringError
>()); // consumed
757 TEST(FormatVariadicTest
, FormatFilterRange
) {
758 std::vector
<int> Vec
{0, 1, 2};
759 auto Range
= map_range(Vec
, [](int V
) { return V
+ 1; });
760 EXPECT_EQ("1, 2, 3", formatv("{0}", Range
).str());
763 TEST(FormatVariadicTest
, Validate
) {
765 #if GTEST_HAS_DEATH_TEST
766 // If asserts are enabled, verify that invalid formatv calls cause assertions.
767 EXPECT_DEATH(formatv("{0}", 1, 2).str(), "Expected 1 Args, but got 2");
768 EXPECT_DEATH(formatv("{0} {2}", 1, 2, 3).str(),
769 "Replacement field indices cannot have holes");
770 EXPECT_DEATH(formatv("{}{1}", 0, 1).str(),
771 "Cannot mix automatic and explicit indices");
772 EXPECT_DEATH(formatv("{}{0}{}", 0, 1).str(),
773 "Cannot mix automatic and explicit indices");
774 #else // GTEST_HAS_DEATH_TEST
775 GTEST_SKIP() << "No support for EXPECT_DEATH";
776 #endif // GTEST_HAS_DEATH_TEST
778 // If asserts are disabled, verify that validation is disabled.
779 EXPECT_EQ(formatv("{0}", 1, 2).str(), "1");
780 EXPECT_EQ(formatv("{0} {2}", 1, 2, 3).str(), "1 3");
781 EXPECT_EQ(formatv("{}{1}", 0, 1).str(), "01");
782 EXPECT_EQ(formatv("{}{0}{}", 0, 1).str(), "001");
788 enum class Base
{ First
};
790 class IntegerValuesRange final
791 : public indexed_accessor_range
<IntegerValuesRange
, Base
, int, int *, int> {
793 using indexed_accessor_range
<IntegerValuesRange
, Base
, int, int *,
794 int>::indexed_accessor_range
;
796 static int dereference(const Base
&, ptrdiff_t Index
) {
797 return static_cast<int>(Index
);
801 TEST(FormatVariadicTest
, FormatRangeNonRef
) {
802 IntegerValuesRange
Range(Base(), 0, 3);
804 formatv("{0}", make_range(Range
.begin(), Range
.end())).str());