• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===----------------------------------------------------------------------===//
2 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
3 // See https://llvm.org/LICENSE.txt for license information.
4 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
5 //
6 //===----------------------------------------------------------------------===//
7 
8 #ifndef TEST_STD_UTILITIES_FORMAT_FORMAT_FUNCTIONS_FORMAT_TESTS_H
9 #define TEST_STD_UTILITIES_FORMAT_FORMAT_FUNCTIONS_FORMAT_TESTS_H
10 
11 #include <format>
12 
13 #include <algorithm>
14 #include <cassert>
15 #include <charconv>
16 #include <cmath>
17 #include <cstdint>
18 #include <iterator>
19 
20 #include "string_literal.h"
21 #include "test_macros.h"
22 #include "format.functions.common.h"
23 
24 // In this file the following template types are used:
25 // TestFunction must be callable as check(expected-result, string-to-format, args-to-format...)
26 // ExceptionTest must be callable as check_exception(expected-exception, string-to-format, args-to-format...)
27 
28 enum class execution_modus { partial, full };
29 
30 template <class CharT>
invalid_types(std::string_view valid)31 std::vector<std::basic_string_view<CharT>> invalid_types(std::string_view valid) {
32   std::vector<std::basic_string_view<CharT>> result;
33 
34 #define CASE(T)                                                                                                        \
35 case #T[0]:                                                                                                            \
36   result.push_back(SV("Invalid formatter type {:" #T "}"));                                                            \
37   break;
38 
39 #if TEST_STD_VER > 20
40   constexpr std::string_view types = "aAbBcdeEfFgGopsxX?";
41 #else
42   constexpr std::string_view types = "aAbBcdeEfFgGopsxX";
43 #endif
44 
45   for (auto type : types) {
46     if (valid.find(type) != std::string::npos)
47       continue;
48 
49     switch (type) {
50       CASE(a)
51       CASE(A)
52       CASE(b)
53       CASE(B)
54       CASE(c)
55       CASE(d)
56       CASE(e)
57       CASE(E)
58       CASE(f)
59       CASE(F)
60       CASE(g)
61       CASE(G)
62       CASE(o)
63       CASE(p)
64       CASE(s)
65       CASE(x)
66       CASE(X)
67       CASE(?)
68     case 0:
69       break;
70     default:
71       assert(false && "Add the type to the list of cases.");
72     }
73   }
74 #undef CASE
75 
76   return result;
77 }
78 
79 // Using a const ref for world and universe so a string literal will be a character array.
80 // When passed as character array W and U have different types.
81 template <class CharT, class W, class U, class TestFunction, class ExceptionTest>
format_test_string(const W & world,const U & universe,TestFunction check,ExceptionTest check_exception)82 void format_test_string(const W& world, const U& universe, TestFunction check, ExceptionTest check_exception) {
83 
84   // *** Valid input tests ***
85   // Unused argument is ignored. TODO FMT what does the Standard mandate?
86   check(SV("hello world"), SV("hello {}"), world, universe);
87   check(SV("hello world and universe"), SV("hello {} and {}"), world, universe);
88   check(SV("hello world"), SV("hello {0}"), world, universe);
89   check(SV("hello universe"), SV("hello {1}"), world, universe);
90   check(SV("hello universe and world"), SV("hello {1} and {0}"), world, universe);
91 
92   check(SV("hello world"), SV("hello {:_>}"), world);
93   check(SV("hello world   "), SV("hello {:8}"), world);
94   check(SV("hello    world"), SV("hello {:>8}"), world);
95   check(SV("hello ___world"), SV("hello {:_>8}"), world);
96   check(SV("hello _world__"), SV("hello {:_^8}"), world);
97   check(SV("hello world___"), SV("hello {:_<8}"), world);
98 
99   // The fill character ':' is allowed here (P0645) but not in ranges (P2286).
100   check(SV("hello :::world"), SV("hello {::>8}"), world);
101   check(SV("hello <<<world"), SV("hello {:<>8}"), world);
102   check(SV("hello ^^^world"), SV("hello {:^>8}"), world);
103 
104   check(SV("hello $world"), SV("hello {:$>{}}"), world, 6);
105   check(SV("hello $world"), SV("hello {0:$>{1}}"), world, 6);
106   check(SV("hello $world"), SV("hello {1:$>{0}}"), 6, world);
107 
108   check(SV("hello world"), SV("hello {:.5}"), world);
109   check(SV("hello unive"), SV("hello {:.5}"), universe);
110 
111   check(SV("hello univer"), SV("hello {:.{}}"), universe, 6);
112   check(SV("hello univer"), SV("hello {0:.{1}}"), universe, 6);
113   check(SV("hello univer"), SV("hello {1:.{0}}"), 6, universe);
114 
115   check(SV("hello %world%"), SV("hello {:%^7.7}"), world);
116   check(SV("hello univers"), SV("hello {:%^7.7}"), universe);
117   check(SV("hello %world%"), SV("hello {:%^{}.{}}"), world, 7, 7);
118   check(SV("hello %world%"), SV("hello {0:%^{1}.{2}}"), world, 7, 7);
119   check(SV("hello %world%"), SV("hello {0:%^{2}.{1}}"), world, 7, 7);
120   check(SV("hello %world%"), SV("hello {1:%^{0}.{2}}"), 7, world, 7);
121 
122   check(SV("hello world"), SV("hello {:_>s}"), world);
123   check(SV("hello $world"), SV("hello {:$>{}s}"), world, 6);
124   check(SV("hello world"), SV("hello {:.5s}"), world);
125   check(SV("hello univer"), SV("hello {:.{}s}"), universe, 6);
126   check(SV("hello %world%"), SV("hello {:%^7.7s}"), world);
127 
128   check(SV("hello #####uni"), SV("hello {:#>8.3s}"), universe);
129   check(SV("hello ##uni###"), SV("hello {:#^8.3s}"), universe);
130   check(SV("hello uni#####"), SV("hello {:#<8.3s}"), universe);
131 
132   // *** sign ***
133   check_exception("The format-spec should consume the input or end with a '}'", SV("hello {:-}"), world);
134 
135   // *** alternate form ***
136   check_exception("The format-spec should consume the input or end with a '}'", SV("hello {:#}"), world);
137 
138   // *** zero-padding ***
139   check_exception("A format-spec width field shouldn't have a leading zero", SV("hello {:0}"), world);
140 
141   // *** width ***
142   // Width 0 allowed, but not useful for string arguments.
143   check(SV("hello world"), SV("hello {:{}}"), world, 0);
144 
145 #ifdef _LIBCPP_VERSION
146   // This limit isn't specified in the Standard.
147   static_assert(std::__format::__number_max == 2'147'483'647, "Update the assert and the test.");
148   check_exception("The numeric value of the format-spec is too large", SV("{:2147483648}"), world);
149   check_exception("The numeric value of the format-spec is too large", SV("{:5000000000}"), world);
150   check_exception("The numeric value of the format-spec is too large", SV("{:10000000000}"), world);
151 #endif
152 
153   check_exception("A format-spec arg-id replacement shouldn't have a negative value", SV("hello {:{}}"), world, -1);
154   check_exception("A format-spec arg-id replacement exceeds the maximum supported value", SV("hello {:{}}"), world,
155                   unsigned(-1));
156   check_exception("Argument index out of bounds", SV("hello {:{}}"), world);
157   check_exception(
158       "Replacement argument isn't a standard signed or unsigned integer type", SV("hello {:{}}"), world, universe);
159   check_exception("Using manual argument numbering in automatic argument numbering mode", SV("hello {:{0}}"), world, 1);
160   check_exception("Using automatic argument numbering in manual argument numbering mode", SV("hello {0:{}}"), world, 1);
161   // Arg-id may not have leading zeros.
162   check_exception("Invalid arg-id", SV("hello {0:{01}}"), world, 1);
163 
164   // *** precision ***
165 #ifdef _LIBCPP_VERSION
166   // This limit isn't specified in the Standard.
167   static_assert(std::__format::__number_max == 2'147'483'647, "Update the assert and the test.");
168   check_exception("The numeric value of the format-spec is too large", SV("{:.2147483648}"), world);
169   check_exception("The numeric value of the format-spec is too large", SV("{:.5000000000}"), world);
170   check_exception("The numeric value of the format-spec is too large", SV("{:.10000000000}"), world);
171 #endif
172 
173   // Precision 0 allowed, but not useful for string arguments.
174   check(SV("hello "), SV("hello {:.{}}"), world, 0);
175   // Precision may have leading zeros. Secondly tests the value is still base 10.
176   check(SV("hello 0123456789"), SV("hello {:.000010}"), STR("0123456789abcdef"));
177   check_exception("A format-spec arg-id replacement shouldn't have a negative value", SV("hello {:.{}}"), world, -1);
178   check_exception("A format-spec arg-id replacement exceeds the maximum supported value", SV("hello {:.{}}"), world,
179                   ~0u);
180   check_exception("Argument index out of bounds", SV("hello {:.{}}"), world);
181   check_exception(
182       "Replacement argument isn't a standard signed or unsigned integer type", SV("hello {:.{}}"), world, universe);
183   check_exception("Using manual argument numbering in automatic argument numbering mode", SV("hello {:.{0}}"), world,
184                   1);
185   check_exception("Using automatic argument numbering in manual argument numbering mode", SV("hello {0:.{}}"), world,
186                   1);
187   // Arg-id may not have leading zeros.
188   check_exception("Invalid arg-id", SV("hello {0:.{01}}"), world, 1);
189 
190   // *** locale-specific form ***
191   check_exception("The format-spec should consume the input or end with a '}'", SV("hello {:L}"), world);
192 
193   // *** type ***
194 #if TEST_STD_VER > 20
195   const char* valid_types = "s?";
196 #else
197   const char* valid_types = "s";
198 #endif
199   for (const auto& fmt : invalid_types<CharT>(valid_types))
200     check_exception("The format-spec type has a type not supported for a string argument", fmt, world);
201 }
202 
203 template <class CharT, class TestFunction>
format_test_string_unicode(TestFunction check)204 void format_test_string_unicode([[maybe_unused]] TestFunction check) {
205   // unicode.pass.cpp and ascii.pass.cpp have additional tests.
206 #ifndef TEST_HAS_NO_UNICODE
207   // Make sure all possible types are tested. For clarity don't use macros.
208   if constexpr (std::same_as<CharT, char>) {
209     const char* c_string = "aßc";
210     check(SV("*aßc*"), SV("{:*^5}"), c_string);
211     check(SV("*aß*"), SV("{:*^4.2}"), c_string);
212 
213     check(SV("*aßc*"), SV("{:*^5}"), const_cast<char*>(c_string));
214     check(SV("*aß*"), SV("{:*^4.2}"), const_cast<char*>(c_string));
215 
216     check(SV("*aßc*"), SV("{:*^5}"), "aßc");
217     check(SV("*aß*"), SV("{:*^4.2}"), "aßc");
218 
219     check(SV("*aßc*"), SV("{:*^5}"), std::string("aßc"));
220     check(SV("*aß*"), SV("{:*^4.2}"), std::string("aßc"));
221 
222     check(SV("*aßc*"), SV("{:*^5}"), std::string_view("aßc"));
223     check(SV("*aß*"), SV("{:*^4.2}"), std::string_view("aßc"));
224   }
225 #  ifndef TEST_HAS_NO_WIDE_CHARACTERS
226   else {
227     const wchar_t* c_string = L"aßc";
228     check(SV("*aßc*"), SV("{:*^5}"), c_string);
229     check(SV("*aß*"), SV("{:*^4.2}"), c_string);
230 
231     check(SV("*aßc*"), SV("{:*^5}"), const_cast<wchar_t*>(c_string));
232     check(SV("*aß*"), SV("{:*^4.2}"), const_cast<wchar_t*>(c_string));
233 
234     check(SV("*aßc*"), SV("{:*^5}"), L"aßc");
235     check(SV("*aß*"), SV("{:*^4.2}"), L"aßc");
236 
237     check(SV("*aßc*"), SV("{:*^5}"), std::wstring(L"aßc"));
238     check(SV("*aß*"), SV("{:*^4.2}"), std::wstring(L"aßc"));
239 
240     check(SV("*aßc*"), SV("{:*^5}"), std::wstring_view(L"aßc"));
241     check(SV("*aß*"), SV("{:*^4.2}"), std::wstring_view(L"aßc"));
242   }
243 #  endif // TEST_HAS_NO_WIDE_CHARACTERS
244 
245   // ß requires one column
246   check(SV("aßc"), SV("{}"), STR("aßc"));
247 
248   check(SV("aßc"), SV("{:.3}"), STR("aßc"));
249   check(SV("aß"), SV("{:.2}"), STR("aßc"));
250   check(SV("a"), SV("{:.1}"), STR("aßc"));
251 
252   check(SV("aßc"), SV("{:3.3}"), STR("aßc"));
253   check(SV("aß"), SV("{:2.2}"), STR("aßc"));
254   check(SV("a"), SV("{:1.1}"), STR("aßc"));
255 
256   check(SV("aßc---"), SV("{:-<6}"), STR("aßc"));
257   check(SV("-aßc--"), SV("{:-^6}"), STR("aßc"));
258   check(SV("---aßc"), SV("{:->6}"), STR("aßc"));
259 
260   // \u1000 requires two columns
261   check(SV("a\u1110c"), SV("{}"), STR("a\u1110c"));
262 
263   check(SV("a\u1100c"), SV("{:.4}"), STR("a\u1100c"));
264   check(SV("a\u1100"), SV("{:.3}"), STR("a\u1100c"));
265   check(SV("a"), SV("{:.2}"), STR("a\u1100c"));
266   check(SV("a"), SV("{:.1}"), STR("a\u1100c"));
267 
268   check(SV("a\u1100c"), SV("{:-<4.4}"), STR("a\u1100c"));
269   check(SV("a\u1100"), SV("{:-<3.3}"), STR("a\u1100c"));
270   check(SV("a-"), SV("{:-<2.2}"), STR("a\u1100c"));
271   check(SV("a"), SV("{:-<1.1}"), STR("a\u1100c"));
272 
273   check(SV("a\u1110c---"), SV("{:-<7}"), STR("a\u1110c"));
274   check(SV("-a\u1110c--"), SV("{:-^7}"), STR("a\u1110c"));
275   check(SV("---a\u1110c"), SV("{:->7}"), STR("a\u1110c"));
276 
277   // Examples used in P1868R2
278   check(SV("*\u0041*"), SV("{:*^3}"), STR("\u0041")); // { LATIN CAPITAL LETTER A }
279   check(SV("*\u00c1*"), SV("{:*^3}"), STR("\u00c1")); // { LATIN CAPITAL LETTER A WITH ACUTE }
280   check(SV("*\u0041\u0301*"),
281         SV("{:*^3}"),
282         STR("\u0041\u0301"));                         // { LATIN CAPITAL LETTER A } { COMBINING ACUTE ACCENT }
283   check(SV("*\u0132*"), SV("{:*^3}"), STR("\u0132")); // { LATIN CAPITAL LIGATURE IJ }
284   check(SV("*\u0394*"), SV("{:*^3}"), STR("\u0394")); // { GREEK CAPITAL LETTER DELTA }
285 
286   check(SV("*\u0429*"), SV("{:*^3}"), STR("\u0429"));         // { CYRILLIC CAPITAL LETTER SHCHA }
287   check(SV("*\u05d0*"), SV("{:*^3}"), STR("\u05d0"));         // { HEBREW LETTER ALEF }
288   check(SV("*\u0634*"), SV("{:*^3}"), STR("\u0634"));         // { ARABIC LETTER SHEEN }
289   check(SV("*\u3009*"), SV("{:*^4}"), STR("\u3009"));         // { RIGHT-POINTING ANGLE BRACKET }
290   check(SV("*\u754c*"), SV("{:*^4}"), STR("\u754c"));         // { CJK Unified Ideograph-754C }
291   check(SV("*\U0001f921*"), SV("{:*^4}"), STR("\U0001f921")); // { UNICORN FACE }
292   check(SV("*\U0001f468\u200d\U0001F469\u200d\U0001F467\u200d\U0001F466*"),
293         SV("{:*^4}"),
294         STR("\U0001f468\u200d\U0001F469\u200d\U0001F467\u200d\U0001F466")); // { Family: Man, Woman, Girl, Boy }
295 #endif // TEST_HAS_NO_UNICODE
296 }
297 
298 template <class CharT, class TestFunction, class ExceptionTest>
format_string_tests(TestFunction check,ExceptionTest check_exception)299 void format_string_tests(TestFunction check, ExceptionTest check_exception) {
300   std::basic_string<CharT> world = STR("world");
301   std::basic_string<CharT> universe = STR("universe");
302 
303   // Test a string literal in a way it won't decay to a pointer.
304   if constexpr (std::same_as<CharT, char>)
305     format_test_string<CharT>("world", "universe", check, check_exception);
306 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
307   else
308     format_test_string<CharT>(L"world", L"universe", check, check_exception);
309 #endif
310 
311   format_test_string<CharT>(world.c_str(), universe.c_str(), check, check_exception);
312   format_test_string<CharT>(const_cast<CharT*>(world.c_str()), const_cast<CharT*>(universe.c_str()), check,
313                             check_exception);
314   format_test_string<CharT>(std::basic_string_view<CharT>(world), std::basic_string_view<CharT>(universe), check,
315                             check_exception);
316   format_test_string<CharT>(world, universe, check, check_exception);
317   format_test_string_unicode<CharT>(check);
318 }
319 
320 template <class CharT, class TestFunction, class ExceptionTest>
format_test_bool(TestFunction check,ExceptionTest check_exception)321 void format_test_bool(TestFunction check, ExceptionTest check_exception) {
322 
323   // *** align-fill & width ***
324   check(SV("answer is 'true   '"), SV("answer is '{:7}'"), true);
325   check(SV("answer is '   true'"), SV("answer is '{:>7}'"), true);
326   check(SV("answer is 'true   '"), SV("answer is '{:<7}'"), true);
327   check(SV("answer is ' true  '"), SV("answer is '{:^7}'"), true);
328 
329   check(SV("answer is 'false   '"), SV("answer is '{:8s}'"), false);
330   check(SV("answer is '   false'"), SV("answer is '{:>8s}'"), false);
331   check(SV("answer is 'false   '"), SV("answer is '{:<8s}'"), false);
332   check(SV("answer is ' false  '"), SV("answer is '{:^8s}'"), false);
333 
334   // The fill character ':' is allowed here (P0645) but not in ranges (P2286).
335   check(SV("answer is ':::true'"), SV("answer is '{::>7}'"), true);
336   check(SV("answer is 'true:::'"), SV("answer is '{::<7}'"), true);
337   check(SV("answer is ':true::'"), SV("answer is '{::^7}'"), true);
338 
339   check(SV("answer is '---false'"), SV("answer is '{:->8s}'"), false);
340   check(SV("answer is 'false---'"), SV("answer is '{:-<8s}'"), false);
341   check(SV("answer is '-false--'"), SV("answer is '{:-^8s}'"), false);
342 
343   // *** Sign ***
344   check_exception("A sign field isn't allowed in this format-spec", SV("{:-}"), true);
345   check_exception("A sign field isn't allowed in this format-spec", SV("{:+}"), true);
346   check_exception("A sign field isn't allowed in this format-spec", SV("{: }"), true);
347 
348   check_exception("A sign field isn't allowed in this format-spec", SV("{:-s}"), true);
349   check_exception("A sign field isn't allowed in this format-spec", SV("{:+s}"), true);
350   check_exception("A sign field isn't allowed in this format-spec", SV("{: s}"), true);
351 
352   // *** alternate form ***
353   check_exception("An alternate form field isn't allowed in this format-spec", SV("{:#}"), true);
354   check_exception("An alternate form field isn't allowed in this format-spec", SV("{:#s}"), true);
355 
356   // *** zero-padding ***
357   check_exception("A zero-padding field isn't allowed in this format-spec", SV("{:0}"), true);
358   check_exception("A zero-padding field isn't allowed in this format-spec", SV("{:0s}"), true);
359 
360   // *** precision ***
361   check_exception("The format-spec should consume the input or end with a '}'", SV("{:.}"), true);
362   check_exception("The format-spec should consume the input or end with a '}'", SV("{:.0}"), true);
363   check_exception("The format-spec should consume the input or end with a '}'", SV("{:.42}"), true);
364 
365   check_exception("The format-spec should consume the input or end with a '}'", SV("{:.s}"), true);
366   check_exception("The format-spec should consume the input or end with a '}'", SV("{:.0s}"), true);
367   check_exception("The format-spec should consume the input or end with a '}'", SV("{:.42s}"), true);
368 
369   // *** locale-specific form ***
370   // See locale-specific_form.pass.cpp
371 
372   // *** type ***
373   for (const auto& fmt : invalid_types<CharT>("bBdosxX"))
374     check_exception("The format-spec type has a type not supported for a bool argument", fmt, true);
375 }
376 
377 template <class CharT, class TestFunction, class ExceptionTest>
format_test_bool_as_integer(TestFunction check,ExceptionTest check_exception)378 void format_test_bool_as_integer(TestFunction check, ExceptionTest check_exception) {
379   // *** align-fill & width ***
380   check(SV("answer is '1'"), SV("answer is '{:<1d}'"), true);
381   check(SV("answer is '1 '"), SV("answer is '{:<2d}'"), true);
382   check(SV("answer is '0 '"), SV("answer is '{:<2d}'"), false);
383 
384   check(SV("answer is '     1'"), SV("answer is '{:6d}'"), true);
385   check(SV("answer is '     1'"), SV("answer is '{:>6d}'"), true);
386   check(SV("answer is '1     '"), SV("answer is '{:<6d}'"), true);
387   check(SV("answer is '  1   '"), SV("answer is '{:^6d}'"), true);
388 
389   // The fill character ':' is allowed here (P0645) but not in ranges (P2286).
390   check(SV("answer is ':::::0'"), SV("answer is '{::>6d}'"), false);
391   check(SV("answer is '0:::::'"), SV("answer is '{::<6d}'"), false);
392   check(SV("answer is '::0:::'"), SV("answer is '{::^6d}'"), false);
393 
394   // Test whether zero padding is ignored
395   check(SV("answer is '     1'"), SV("answer is '{:>06d}'"), true);
396   check(SV("answer is '1     '"), SV("answer is '{:<06d}'"), true);
397   check(SV("answer is '  1   '"), SV("answer is '{:^06d}'"), true);
398 
399   // *** Sign ***
400   check(SV("answer is 1"), SV("answer is {:d}"), true);
401   check(SV("answer is 0"), SV("answer is {:-d}"), false);
402   check(SV("answer is +1"), SV("answer is {:+d}"), true);
403   check(SV("answer is  0"), SV("answer is {: d}"), false);
404 
405   // *** alternate form ***
406   check(SV("answer is +1"), SV("answer is {:+#d}"), true);
407   check(SV("answer is +1"), SV("answer is {:+b}"), true);
408   check(SV("answer is +0b1"), SV("answer is {:+#b}"), true);
409   check(SV("answer is +0B1"), SV("answer is {:+#B}"), true);
410   check(SV("answer is +1"), SV("answer is {:+o}"), true);
411   check(SV("answer is +01"), SV("answer is {:+#o}"), true);
412   check(SV("answer is +1"), SV("answer is {:+x}"), true);
413   check(SV("answer is +0x1"), SV("answer is {:+#x}"), true);
414   check(SV("answer is +1"), SV("answer is {:+X}"), true);
415   check(SV("answer is +0X1"), SV("answer is {:+#X}"), true);
416 
417   check(SV("answer is 0"), SV("answer is {:#d}"), false);
418   check(SV("answer is 0"), SV("answer is {:b}"), false);
419   check(SV("answer is 0b0"), SV("answer is {:#b}"), false);
420   check(SV("answer is 0B0"), SV("answer is {:#B}"), false);
421   check(SV("answer is 0"), SV("answer is {:o}"), false);
422   check(SV("answer is 0"), SV("answer is {:#o}"), false);
423   check(SV("answer is 0"), SV("answer is {:x}"), false);
424   check(SV("answer is 0x0"), SV("answer is {:#x}"), false);
425   check(SV("answer is 0"), SV("answer is {:X}"), false);
426   check(SV("answer is 0X0"), SV("answer is {:#X}"), false);
427 
428   // *** zero-padding & width ***
429   check(SV("answer is +00000000001"), SV("answer is {:+#012d}"), true);
430   check(SV("answer is +00000000001"), SV("answer is {:+012b}"), true);
431   check(SV("answer is +0b000000001"), SV("answer is {:+#012b}"), true);
432   check(SV("answer is +0B000000001"), SV("answer is {:+#012B}"), true);
433   check(SV("answer is +00000000001"), SV("answer is {:+012o}"), true);
434   check(SV("answer is +00000000001"), SV("answer is {:+#012o}"), true);
435   check(SV("answer is +00000000001"), SV("answer is {:+012x}"), true);
436   check(SV("answer is +0x000000001"), SV("answer is {:+#012x}"), true);
437   check(SV("answer is +00000000001"), SV("answer is {:+012X}"), true);
438   check(SV("answer is +0X000000001"), SV("answer is {:+#012X}"), true);
439 
440   check(SV("answer is 000000000000"), SV("answer is {:#012d}"), false);
441   check(SV("answer is 000000000000"), SV("answer is {:012b}"), false);
442   check(SV("answer is 0b0000000000"), SV("answer is {:#012b}"), false);
443   check(SV("answer is 0B0000000000"), SV("answer is {:#012B}"), false);
444   check(SV("answer is 000000000000"), SV("answer is {:012o}"), false);
445   check(SV("answer is 000000000000"), SV("answer is {:#012o}"), false);
446   check(SV("answer is 000000000000"), SV("answer is {:012x}"), false);
447   check(SV("answer is 0x0000000000"), SV("answer is {:#012x}"), false);
448   check(SV("answer is 000000000000"), SV("answer is {:012X}"), false);
449   check(SV("answer is 0X0000000000"), SV("answer is {:#012X}"), false);
450 
451   // *** precision ***
452   check_exception("The format-spec should consume the input or end with a '}'", SV("{:.}"), true);
453   check_exception("The format-spec should consume the input or end with a '}'", SV("{:.0}"), true);
454   check_exception("The format-spec should consume the input or end with a '}'", SV("{:.42}"), true);
455 
456   // *** locale-specific form ***
457   // See locale-specific_form.pass.cpp
458 
459   // *** type ***
460   for (const auto& fmt : invalid_types<CharT>("bBcdosxX"))
461     check_exception("The format-spec type has a type not supported for a bool argument", fmt, true);
462 }
463 
464 template <class I, class CharT, class TestFunction, class ExceptionTest>
format_test_integer_as_integer(TestFunction check,ExceptionTest check_exception)465 void format_test_integer_as_integer(TestFunction check, ExceptionTest check_exception) {
466   // *** align-fill & width ***
467   check(SV("answer is '42'"), SV("answer is '{:<1}'"), I(42));
468   check(SV("answer is '42'"), SV("answer is '{:<2}'"), I(42));
469   check(SV("answer is '42 '"), SV("answer is '{:<3}'"), I(42));
470 
471   check(SV("answer is '     42'"), SV("answer is '{:7}'"), I(42));
472   check(SV("answer is '     42'"), SV("answer is '{:>7}'"), I(42));
473   check(SV("answer is '42     '"), SV("answer is '{:<7}'"), I(42));
474   check(SV("answer is '  42   '"), SV("answer is '{:^7}'"), I(42));
475 
476   // The fill character ':' is allowed here (P0645) but not in ranges (P2286).
477   check(SV("answer is ':::::42'"), SV("answer is '{::>7}'"), I(42));
478   check(SV("answer is '42:::::'"), SV("answer is '{::<7}'"), I(42));
479   check(SV("answer is '::42:::'"), SV("answer is '{::^7}'"), I(42));
480 
481   // Test whether zero padding is ignored
482   check(SV("answer is '     42'"), SV("answer is '{:>07}'"), I(42));
483   check(SV("answer is '42     '"), SV("answer is '{:<07}'"), I(42));
484   check(SV("answer is '  42   '"), SV("answer is '{:^07}'"), I(42));
485 
486   // *** Sign ***
487   if constexpr (std::signed_integral<I>)
488     check(SV("answer is -42"), SV("answer is {}"), I(-42));
489   check(SV("answer is 0"), SV("answer is {}"), I(0));
490   check(SV("answer is 42"), SV("answer is {}"), I(42));
491 
492   if constexpr (std::signed_integral<I>)
493     check(SV("answer is -42"), SV("answer is {:-}"), I(-42));
494   check(SV("answer is 0"), SV("answer is {:-}"), I(0));
495   check(SV("answer is 42"), SV("answer is {:-}"), I(42));
496 
497   if constexpr (std::signed_integral<I>)
498     check(SV("answer is -42"), SV("answer is {:+}"), I(-42));
499   check(SV("answer is +0"), SV("answer is {:+}"), I(0));
500   check(SV("answer is +42"), SV("answer is {:+}"), I(42));
501 
502   if constexpr (std::signed_integral<I>)
503     check(SV("answer is -42"), SV("answer is {: }"), I(-42));
504   check(SV("answer is  0"), SV("answer is {: }"), I(0));
505   check(SV("answer is  42"), SV("answer is {: }"), I(42));
506 
507   // *** alternate form ***
508   if constexpr (std::signed_integral<I>) {
509     check(SV("answer is -42"), SV("answer is {:#}"), I(-42));
510     check(SV("answer is -42"), SV("answer is {:#d}"), I(-42));
511     check(SV("answer is -101010"), SV("answer is {:b}"), I(-42));
512     check(SV("answer is -0b101010"), SV("answer is {:#b}"), I(-42));
513     check(SV("answer is -0B101010"), SV("answer is {:#B}"), I(-42));
514     check(SV("answer is -52"), SV("answer is {:o}"), I(-42));
515     check(SV("answer is -052"), SV("answer is {:#o}"), I(-42));
516     check(SV("answer is -2a"), SV("answer is {:x}"), I(-42));
517     check(SV("answer is -0x2a"), SV("answer is {:#x}"), I(-42));
518     check(SV("answer is -2A"), SV("answer is {:X}"), I(-42));
519     check(SV("answer is -0X2A"), SV("answer is {:#X}"), I(-42));
520   }
521   check(SV("answer is 0"), SV("answer is {:#}"), I(0));
522   check(SV("answer is 0"), SV("answer is {:#d}"), I(0));
523   check(SV("answer is 0"), SV("answer is {:b}"), I(0));
524   check(SV("answer is 0b0"), SV("answer is {:#b}"), I(0));
525   check(SV("answer is 0B0"), SV("answer is {:#B}"), I(0));
526   check(SV("answer is 0"), SV("answer is {:o}"), I(0));
527   check(SV("answer is 0"), SV("answer is {:#o}"), I(0));
528   check(SV("answer is 0"), SV("answer is {:x}"), I(0));
529   check(SV("answer is 0x0"), SV("answer is {:#x}"), I(0));
530   check(SV("answer is 0"), SV("answer is {:X}"), I(0));
531   check(SV("answer is 0X0"), SV("answer is {:#X}"), I(0));
532 
533   check(SV("answer is +42"), SV("answer is {:+#}"), I(42));
534   check(SV("answer is +42"), SV("answer is {:+#d}"), I(42));
535   check(SV("answer is +101010"), SV("answer is {:+b}"), I(42));
536   check(SV("answer is +0b101010"), SV("answer is {:+#b}"), I(42));
537   check(SV("answer is +0B101010"), SV("answer is {:+#B}"), I(42));
538   check(SV("answer is +52"), SV("answer is {:+o}"), I(42));
539   check(SV("answer is +052"), SV("answer is {:+#o}"), I(42));
540   check(SV("answer is +2a"), SV("answer is {:+x}"), I(42));
541   check(SV("answer is +0x2a"), SV("answer is {:+#x}"), I(42));
542   check(SV("answer is +2A"), SV("answer is {:+X}"), I(42));
543   check(SV("answer is +0X2A"), SV("answer is {:+#X}"), I(42));
544 
545   // *** zero-padding & width ***
546   if constexpr (std::signed_integral<I>) {
547     check(SV("answer is -00000000042"), SV("answer is {:#012}"), I(-42));
548     check(SV("answer is -00000000042"), SV("answer is {:#012d}"), I(-42));
549     check(SV("answer is -00000101010"), SV("answer is {:012b}"), I(-42));
550     check(SV("answer is -0b000101010"), SV("answer is {:#012b}"), I(-42));
551     check(SV("answer is -0B000101010"), SV("answer is {:#012B}"), I(-42));
552     check(SV("answer is -00000000052"), SV("answer is {:012o}"), I(-42));
553     check(SV("answer is -00000000052"), SV("answer is {:#012o}"), I(-42));
554     check(SV("answer is -0000000002a"), SV("answer is {:012x}"), I(-42));
555     check(SV("answer is -0x00000002a"), SV("answer is {:#012x}"), I(-42));
556     check(SV("answer is -0000000002A"), SV("answer is {:012X}"), I(-42));
557     check(SV("answer is -0X00000002A"), SV("answer is {:#012X}"), I(-42));
558   }
559 
560   check(SV("answer is 000000000000"), SV("answer is {:#012}"), I(0));
561   check(SV("answer is 000000000000"), SV("answer is {:#012d}"), I(0));
562   check(SV("answer is 000000000000"), SV("answer is {:012b}"), I(0));
563   check(SV("answer is 0b0000000000"), SV("answer is {:#012b}"), I(0));
564   check(SV("answer is 0B0000000000"), SV("answer is {:#012B}"), I(0));
565   check(SV("answer is 000000000000"), SV("answer is {:012o}"), I(0));
566   check(SV("answer is 000000000000"), SV("answer is {:#012o}"), I(0));
567   check(SV("answer is 000000000000"), SV("answer is {:012x}"), I(0));
568   check(SV("answer is 0x0000000000"), SV("answer is {:#012x}"), I(0));
569   check(SV("answer is 000000000000"), SV("answer is {:012X}"), I(0));
570   check(SV("answer is 0X0000000000"), SV("answer is {:#012X}"), I(0));
571 
572   check(SV("answer is +00000000042"), SV("answer is {:+#012}"), I(42));
573   check(SV("answer is +00000000042"), SV("answer is {:+#012d}"), I(42));
574   check(SV("answer is +00000101010"), SV("answer is {:+012b}"), I(42));
575   check(SV("answer is +0b000101010"), SV("answer is {:+#012b}"), I(42));
576   check(SV("answer is +0B000101010"), SV("answer is {:+#012B}"), I(42));
577   check(SV("answer is +00000000052"), SV("answer is {:+012o}"), I(42));
578   check(SV("answer is +00000000052"), SV("answer is {:+#012o}"), I(42));
579   check(SV("answer is +0000000002a"), SV("answer is {:+012x}"), I(42));
580   check(SV("answer is +0x00000002a"), SV("answer is {:+#012x}"), I(42));
581   check(SV("answer is +0000000002A"), SV("answer is {:+012X}"), I(42));
582   check(SV("answer is +0X00000002A"), SV("answer is {:+#012X}"), I(42));
583 
584   // *** precision ***
585   check_exception("The format-spec should consume the input or end with a '}'", SV("{:.}"), I(0));
586   check_exception("The format-spec should consume the input or end with a '}'", SV("{:.0}"), I(0));
587   check_exception("The format-spec should consume the input or end with a '}'", SV("{:.42}"), I(0));
588 
589   // *** locale-specific form ***
590   // See locale-specific_form.pass.cpp
591 
592   // *** type ***
593   for (const auto& fmt : invalid_types<CharT>("bBcdoxX"))
594     check_exception("The format-spec type has a type not supported for an integer argument", fmt, 42);
595 }
596 
597 template <class I, class CharT, class TestFunction, class ExceptionTest>
format_test_integer_as_char(TestFunction check,ExceptionTest check_exception)598 void format_test_integer_as_char(TestFunction check, ExceptionTest check_exception) {
599   // *** align-fill & width ***
600   check(SV("answer is '*     '"), SV("answer is '{:6c}'"), I(42));
601   check(SV("answer is '     *'"), SV("answer is '{:>6c}'"), I(42));
602   check(SV("answer is '*     '"), SV("answer is '{:<6c}'"), I(42));
603   check(SV("answer is '  *   '"), SV("answer is '{:^6c}'"), I(42));
604 
605   // The fill character ':' is allowed here (P0645) but not in ranges (P2286).
606   check(SV("answer is ':::::*'"), SV("answer is '{::>6c}'"), I(42));
607   check(SV("answer is '*:::::'"), SV("answer is '{::<6c}'"), I(42));
608   check(SV("answer is '::*:::'"), SV("answer is '{::^6c}'"), I(42));
609 
610   // *** Sign ***
611   check(SV("answer is *"), SV("answer is {:c}"), I(42));
612   check_exception("A sign field isn't allowed in this format-spec", SV("answer is {:-c}"), I(42));
613   check_exception("A sign field isn't allowed in this format-spec", SV("answer is {:+c}"), I(42));
614   check_exception("A sign field isn't allowed in this format-spec", SV("answer is {: c}"), I(42));
615 
616   // *** alternate form ***
617   check_exception("An alternate form field isn't allowed in this format-spec", SV("answer is {:#c}"), I(42));
618 
619   // *** zero-padding & width ***
620   check_exception("A zero-padding field isn't allowed in this format-spec", SV("answer is {:01c}"), I(42));
621 
622   // *** precision ***
623   check_exception("The format-spec should consume the input or end with a '}'", SV("{:.c}"), I(0));
624   check_exception("The format-spec should consume the input or end with a '}'", SV("{:.0c}"), I(0));
625   check_exception("The format-spec should consume the input or end with a '}'", SV("{:.42c}"), I(0));
626 
627   // *** locale-specific form ***
628   // Note it has no effect but it's allowed.
629   check(SV("answer is '*'"), SV("answer is '{:Lc}'"), I(42));
630 
631   // *** type ***
632   for (const auto& fmt : invalid_types<CharT>("bBcdoxX"))
633     check_exception("The format-spec type has a type not supported for an integer argument", fmt, I(42));
634 
635   // *** Validate range ***
636   // The code has some duplications to keep the if statement readable.
637   if constexpr (std::signed_integral<CharT>) {
638     if constexpr (std::signed_integral<I> && sizeof(I) > sizeof(CharT)) {
639       check_exception("Integral value outside the range of the char type", SV("{:c}"), std::numeric_limits<I>::min());
640       check_exception("Integral value outside the range of the char type", SV("{:c}"), std::numeric_limits<I>::max());
641     } else if constexpr (std::unsigned_integral<I> && sizeof(I) >= sizeof(CharT)) {
642       check_exception("Integral value outside the range of the char type", SV("{:c}"), std::numeric_limits<I>::max());
643     }
644   } else if constexpr (sizeof(I) > sizeof(CharT)) {
645     check_exception("Integral value outside the range of the char type", SV("{:c}"), std::numeric_limits<I>::max());
646   }
647 }
648 
649 template <class I, class CharT, class TestFunction, class ExceptionTest>
format_test_integer(TestFunction check,ExceptionTest check_exception)650 void format_test_integer(TestFunction check, ExceptionTest check_exception) {
651   format_test_integer_as_integer<I, CharT>(check, check_exception);
652   format_test_integer_as_char<I, CharT>(check, check_exception);
653 }
654 
655 template <class CharT, class TestFunction, class ExceptionTest>
format_test_signed_integer(TestFunction check,ExceptionTest check_exception)656 void format_test_signed_integer(TestFunction check, ExceptionTest check_exception) {
657   format_test_integer<signed char, CharT>(check, check_exception);
658   format_test_integer<short, CharT>(check, check_exception);
659   format_test_integer<int, CharT>(check, check_exception);
660   format_test_integer<long, CharT>(check, check_exception);
661   format_test_integer<long long, CharT>(check, check_exception);
662 #ifndef TEST_HAS_NO_INT128
663   format_test_integer<__int128_t, CharT>(check, check_exception);
664 #endif
665   // *** check the minima and maxima ***
666   check(SV("-0b10000000"), SV("{:#b}"), std::numeric_limits<std::int8_t>::min());
667   check(SV("-0200"), SV("{:#o}"), std::numeric_limits<std::int8_t>::min());
668   check(SV("-128"), SV("{:#}"), std::numeric_limits<std::int8_t>::min());
669   check(SV("-0x80"), SV("{:#x}"), std::numeric_limits<std::int8_t>::min());
670 
671   check(SV("-0b1000000000000000"), SV("{:#b}"), std::numeric_limits<std::int16_t>::min());
672   check(SV("-0100000"), SV("{:#o}"), std::numeric_limits<std::int16_t>::min());
673   check(SV("-32768"), SV("{:#}"), std::numeric_limits<std::int16_t>::min());
674   check(SV("-0x8000"), SV("{:#x}"), std::numeric_limits<std::int16_t>::min());
675 
676   check(SV("-0b10000000000000000000000000000000"), SV("{:#b}"), std::numeric_limits<std::int32_t>::min());
677   check(SV("-020000000000"), SV("{:#o}"), std::numeric_limits<std::int32_t>::min());
678   check(SV("-2147483648"), SV("{:#}"), std::numeric_limits<std::int32_t>::min());
679   check(SV("-0x80000000"), SV("{:#x}"), std::numeric_limits<std::int32_t>::min());
680 
681   check(SV("-0b1000000000000000000000000000000000000000000000000000000000000000"),
682         SV("{:#b}"),
683         std::numeric_limits<std::int64_t>::min());
684   check(SV("-01000000000000000000000"), SV("{:#o}"), std::numeric_limits<std::int64_t>::min());
685   check(SV("-9223372036854775808"), SV("{:#}"), std::numeric_limits<std::int64_t>::min());
686   check(SV("-0x8000000000000000"), SV("{:#x}"), std::numeric_limits<std::int64_t>::min());
687 
688 #ifndef TEST_HAS_NO_INT128
689   check(SV("-0b1000000000000000000000000000000000000000000000000000000000000000"
690            "0000000000000000000000000000000000000000000000000000000000000000"),
691         SV("{:#b}"),
692         std::numeric_limits<__int128_t>::min());
693   check(SV("-02000000000000000000000000000000000000000000"), SV("{:#o}"), std::numeric_limits<__int128_t>::min());
694   check(SV("-170141183460469231731687303715884105728"), SV("{:#}"), std::numeric_limits<__int128_t>::min());
695   check(SV("-0x80000000000000000000000000000000"), SV("{:#x}"), std::numeric_limits<__int128_t>::min());
696 #endif
697 
698   check(SV("0b1111111"), SV("{:#b}"), std::numeric_limits<std::int8_t>::max());
699   check(SV("0177"), SV("{:#o}"), std::numeric_limits<std::int8_t>::max());
700   check(SV("127"), SV("{:#}"), std::numeric_limits<std::int8_t>::max());
701   check(SV("0x7f"), SV("{:#x}"), std::numeric_limits<std::int8_t>::max());
702 
703   check(SV("0b111111111111111"), SV("{:#b}"), std::numeric_limits<std::int16_t>::max());
704   check(SV("077777"), SV("{:#o}"), std::numeric_limits<std::int16_t>::max());
705   check(SV("32767"), SV("{:#}"), std::numeric_limits<std::int16_t>::max());
706   check(SV("0x7fff"), SV("{:#x}"), std::numeric_limits<std::int16_t>::max());
707 
708   check(SV("0b1111111111111111111111111111111"), SV("{:#b}"), std::numeric_limits<std::int32_t>::max());
709   check(SV("017777777777"), SV("{:#o}"), std::numeric_limits<std::int32_t>::max());
710   check(SV("2147483647"), SV("{:#}"), std::numeric_limits<std::int32_t>::max());
711   check(SV("0x7fffffff"), SV("{:#x}"), std::numeric_limits<std::int32_t>::max());
712 
713   check(SV("0b111111111111111111111111111111111111111111111111111111111111111"),
714         SV("{:#b}"),
715         std::numeric_limits<std::int64_t>::max());
716   check(SV("0777777777777777777777"), SV("{:#o}"), std::numeric_limits<std::int64_t>::max());
717   check(SV("9223372036854775807"), SV("{:#}"), std::numeric_limits<std::int64_t>::max());
718   check(SV("0x7fffffffffffffff"), SV("{:#x}"), std::numeric_limits<std::int64_t>::max());
719 
720 #ifndef TEST_HAS_NO_INT128
721   check(SV("0b111111111111111111111111111111111111111111111111111111111111111"
722            "1111111111111111111111111111111111111111111111111111111111111111"),
723         SV("{:#b}"),
724         std::numeric_limits<__int128_t>::max());
725   check(SV("01777777777777777777777777777777777777777777"), SV("{:#o}"), std::numeric_limits<__int128_t>::max());
726   check(SV("170141183460469231731687303715884105727"), SV("{:#}"), std::numeric_limits<__int128_t>::max());
727   check(SV("0x7fffffffffffffffffffffffffffffff"), SV("{:#x}"), std::numeric_limits<__int128_t>::max());
728 #endif
729 }
730 
731 template <class CharT, class TestFunction, class ExceptionTest>
format_test_unsigned_integer(TestFunction check,ExceptionTest check_exception)732 void format_test_unsigned_integer(TestFunction check, ExceptionTest check_exception) {
733   format_test_integer<unsigned char, CharT>(check, check_exception);
734   format_test_integer<unsigned short, CharT>(check, check_exception);
735   format_test_integer<unsigned, CharT>(check, check_exception);
736   format_test_integer<unsigned long, CharT>(check, check_exception);
737   format_test_integer<unsigned long long, CharT>(check, check_exception);
738 #ifndef TEST_HAS_NO_INT128
739   format_test_integer<__uint128_t, CharT>(check, check_exception);
740 #endif
741   // *** test the maxima ***
742   check(SV("0b11111111"), SV("{:#b}"), std::numeric_limits<std::uint8_t>::max());
743   check(SV("0377"), SV("{:#o}"), std::numeric_limits<std::uint8_t>::max());
744   check(SV("255"), SV("{:#}"), std::numeric_limits<std::uint8_t>::max());
745   check(SV("0xff"), SV("{:#x}"), std::numeric_limits<std::uint8_t>::max());
746 
747   check(SV("0b1111111111111111"), SV("{:#b}"), std::numeric_limits<std::uint16_t>::max());
748   check(SV("0177777"), SV("{:#o}"), std::numeric_limits<std::uint16_t>::max());
749   check(SV("65535"), SV("{:#}"), std::numeric_limits<std::uint16_t>::max());
750   check(SV("0xffff"), SV("{:#x}"), std::numeric_limits<std::uint16_t>::max());
751 
752   check(SV("0b11111111111111111111111111111111"), SV("{:#b}"), std::numeric_limits<std::uint32_t>::max());
753   check(SV("037777777777"), SV("{:#o}"), std::numeric_limits<std::uint32_t>::max());
754   check(SV("4294967295"), SV("{:#}"), std::numeric_limits<std::uint32_t>::max());
755   check(SV("0xffffffff"), SV("{:#x}"), std::numeric_limits<std::uint32_t>::max());
756 
757   check(SV("0b1111111111111111111111111111111111111111111111111111111111111111"),
758         SV("{:#b}"),
759         std::numeric_limits<std::uint64_t>::max());
760   check(SV("01777777777777777777777"), SV("{:#o}"), std::numeric_limits<std::uint64_t>::max());
761   check(SV("18446744073709551615"), SV("{:#}"), std::numeric_limits<std::uint64_t>::max());
762   check(SV("0xffffffffffffffff"), SV("{:#x}"), std::numeric_limits<std::uint64_t>::max());
763 
764 #ifndef TEST_HAS_NO_INT128
765   check(SV("0b1111111111111111111111111111111111111111111111111111111111111111"
766            "1111111111111111111111111111111111111111111111111111111111111111"),
767         SV("{:#b}"),
768         std::numeric_limits<__uint128_t>::max());
769   check(SV("03777777777777777777777777777777777777777777"), SV("{:#o}"), std::numeric_limits<__uint128_t>::max());
770   check(SV("340282366920938463463374607431768211455"), SV("{:#}"), std::numeric_limits<__uint128_t>::max());
771   check(SV("0xffffffffffffffffffffffffffffffff"), SV("{:#x}"), std::numeric_limits<__uint128_t>::max());
772 #endif
773 }
774 
775 template <class CharT, class TestFunction, class ExceptionTest>
format_test_char(TestFunction check,ExceptionTest check_exception)776 void format_test_char(TestFunction check, ExceptionTest check_exception) {
777 
778   // ***** Char type *****
779   // *** align-fill & width ***
780   check(SV("answer is '*     '"), SV("answer is '{:6}'"), CharT('*'));
781   check(SV("answer is '     *'"), SV("answer is '{:>6}'"), CharT('*'));
782   check(SV("answer is '*     '"), SV("answer is '{:<6}'"), CharT('*'));
783   check(SV("answer is '  *   '"), SV("answer is '{:^6}'"), CharT('*'));
784 
785   check(SV("answer is '*     '"), SV("answer is '{:6c}'"), CharT('*'));
786   check(SV("answer is '     *'"), SV("answer is '{:>6c}'"), CharT('*'));
787   check(SV("answer is '*     '"), SV("answer is '{:<6c}'"), CharT('*'));
788   check(SV("answer is '  *   '"), SV("answer is '{:^6c}'"), CharT('*'));
789 
790   // The fill character ':' is allowed here (P0645) but not in ranges (P2286).
791   check(SV("answer is ':::::*'"), SV("answer is '{::>6}'"), CharT('*'));
792   check(SV("answer is '*:::::'"), SV("answer is '{::<6}'"), CharT('*'));
793   check(SV("answer is '::*:::'"), SV("answer is '{::^6}'"), CharT('*'));
794 
795   check(SV("answer is '-----*'"), SV("answer is '{:->6c}'"), CharT('*'));
796   check(SV("answer is '*-----'"), SV("answer is '{:-<6c}'"), CharT('*'));
797   check(SV("answer is '--*---'"), SV("answer is '{:-^6c}'"), CharT('*'));
798 
799   // *** Sign ***
800   check_exception("A sign field isn't allowed in this format-spec", SV("{:-}"), CharT('*'));
801   check_exception("A sign field isn't allowed in this format-spec", SV("{:+}"), CharT('*'));
802   check_exception("A sign field isn't allowed in this format-spec", SV("{: }"), CharT('*'));
803 
804   check_exception("A sign field isn't allowed in this format-spec", SV("{:-c}"), CharT('*'));
805   check_exception("A sign field isn't allowed in this format-spec", SV("{:+c}"), CharT('*'));
806   check_exception("A sign field isn't allowed in this format-spec", SV("{: c}"), CharT('*'));
807 
808   // *** alternate form ***
809   check_exception("An alternate form field isn't allowed in this format-spec", SV("{:#}"), CharT('*'));
810   check_exception("An alternate form field isn't allowed in this format-spec", SV("{:#c}"), CharT('*'));
811 
812   // *** zero-padding ***
813   check_exception("A zero-padding field isn't allowed in this format-spec", SV("{:0}"), CharT('*'));
814   check_exception("A zero-padding field isn't allowed in this format-spec", SV("{:0c}"), CharT('*'));
815 
816   // *** precision ***
817   check_exception("The format-spec should consume the input or end with a '}'", SV("{:.}"), CharT('*'));
818   check_exception("The format-spec should consume the input or end with a '}'", SV("{:.0}"), CharT('*'));
819   check_exception("The format-spec should consume the input or end with a '}'", SV("{:.42}"), CharT('*'));
820 
821   check_exception("The format-spec should consume the input or end with a '}'", SV("{:.c}"), CharT('*'));
822   check_exception("The format-spec should consume the input or end with a '}'", SV("{:.0c}"), CharT('*'));
823   check_exception("The format-spec should consume the input or end with a '}'", SV("{:.42c}"), CharT('*'));
824 
825   // *** locale-specific form ***
826   // Note it has no effect but it's allowed.
827   check(SV("answer is '*'"), SV("answer is '{:L}'"), '*');
828   check(SV("answer is '*'"), SV("answer is '{:Lc}'"), '*');
829 
830   // *** type ***
831 #if TEST_STD_VER > 20
832   const char* valid_types = "bBcdoxX?";
833 #else
834   const char* valid_types = "bBcdoxX";
835 #endif
836   for (const auto& fmt : invalid_types<CharT>(valid_types))
837     check_exception("The format-spec type has a type not supported for a char argument", fmt, CharT('*'));
838 }
839 
840 template <class CharT, class TestFunction, class ExceptionTest>
format_test_char_as_integer(TestFunction check,ExceptionTest check_exception)841 void format_test_char_as_integer(TestFunction check, ExceptionTest check_exception) {
842   // *** align-fill & width ***
843   check(SV("answer is '42'"), SV("answer is '{:<1d}'"), CharT('*'));
844 
845   check(SV("answer is '42'"), SV("answer is '{:<2d}'"), CharT('*'));
846   check(SV("answer is '42 '"), SV("answer is '{:<3d}'"), CharT('*'));
847 
848   check(SV("answer is '     42'"), SV("answer is '{:7d}'"), CharT('*'));
849   check(SV("answer is '     42'"), SV("answer is '{:>7d}'"), CharT('*'));
850   check(SV("answer is '42     '"), SV("answer is '{:<7d}'"), CharT('*'));
851   check(SV("answer is '  42   '"), SV("answer is '{:^7d}'"), CharT('*'));
852 
853   // The fill character ':' is allowed here (P0645) but not in ranges (P2286).
854   check(SV("answer is ':::::42'"), SV("answer is '{::>7d}'"), CharT('*'));
855   check(SV("answer is '42:::::'"), SV("answer is '{::<7d}'"), CharT('*'));
856   check(SV("answer is '::42:::'"), SV("answer is '{::^7d}'"), CharT('*'));
857 
858   // Test whether zero padding is ignored
859   check(SV("answer is '     42'"), SV("answer is '{:>07d}'"), CharT('*'));
860   check(SV("answer is '42     '"), SV("answer is '{:<07d}'"), CharT('*'));
861   check(SV("answer is '  42   '"), SV("answer is '{:^07d}'"), CharT('*'));
862 
863   // *** Sign ***
864   check(SV("answer is 42"), SV("answer is {:d}"), CharT('*'));
865   check(SV("answer is 42"), SV("answer is {:-d}"), CharT('*'));
866   check(SV("answer is +42"), SV("answer is {:+d}"), CharT('*'));
867   check(SV("answer is  42"), SV("answer is {: d}"), CharT('*'));
868 
869   // *** alternate form ***
870   check(SV("answer is +42"), SV("answer is {:+#d}"), CharT('*'));
871   check(SV("answer is +101010"), SV("answer is {:+b}"), CharT('*'));
872   check(SV("answer is +0b101010"), SV("answer is {:+#b}"), CharT('*'));
873   check(SV("answer is +0B101010"), SV("answer is {:+#B}"), CharT('*'));
874   check(SV("answer is +52"), SV("answer is {:+o}"), CharT('*'));
875   check(SV("answer is +052"), SV("answer is {:+#o}"), CharT('*'));
876   check(SV("answer is +2a"), SV("answer is {:+x}"), CharT('*'));
877   check(SV("answer is +0x2a"), SV("answer is {:+#x}"), CharT('*'));
878   check(SV("answer is +2A"), SV("answer is {:+X}"), CharT('*'));
879   check(SV("answer is +0X2A"), SV("answer is {:+#X}"), CharT('*'));
880 
881   // *** zero-padding & width ***
882   check(SV("answer is +00000000042"), SV("answer is {:+#012d}"), CharT('*'));
883   check(SV("answer is +00000101010"), SV("answer is {:+012b}"), CharT('*'));
884   check(SV("answer is +0b000101010"), SV("answer is {:+#012b}"), CharT('*'));
885   check(SV("answer is +0B000101010"), SV("answer is {:+#012B}"), CharT('*'));
886   check(SV("answer is +00000000052"), SV("answer is {:+012o}"), CharT('*'));
887   check(SV("answer is +00000000052"), SV("answer is {:+#012o}"), CharT('*'));
888   check(SV("answer is +0000000002a"), SV("answer is {:+012x}"), CharT('*'));
889   check(SV("answer is +0x00000002a"), SV("answer is {:+#012x}"), CharT('*'));
890   check(SV("answer is +0000000002A"), SV("answer is {:+012X}"), CharT('*'));
891 
892   check(SV("answer is +0X00000002A"), SV("answer is {:+#012X}"), CharT('*'));
893 
894   // *** precision ***
895   check_exception("The format-spec should consume the input or end with a '}'", SV("{:.d}"), CharT('*'));
896   check_exception("The format-spec should consume the input or end with a '}'", SV("{:.0d}"), CharT('*'));
897   check_exception("The format-spec should consume the input or end with a '}'", SV("{:.42d}"), CharT('*'));
898 
899   // *** locale-specific form ***
900   // See locale-specific_form.pass.cpp
901 
902   // *** type ***
903 #if TEST_STD_VER > 20
904   const char* valid_types = "bBcdoxX?";
905 #else
906   const char* valid_types = "bBcdoxX";
907 #endif
908   for (const auto& fmt : invalid_types<CharT>(valid_types))
909     check_exception("The format-spec type has a type not supported for a char argument", fmt, '*');
910 }
911 
912 template <class F, class CharT, class TestFunction>
format_test_floating_point_hex_lower_case(TestFunction check)913 void format_test_floating_point_hex_lower_case(TestFunction check) {
914   auto nan_pos = std::numeric_limits<F>::quiet_NaN();                          // "nan"
915   auto nan_neg = std::copysign(nan_pos, static_cast<decltype(nan_pos)>(-1.0)); // "-nan"
916 
917   // Test whether the hexadecimal letters are the proper case.
918   // The precision is too large for float, so two tests are used.
919   check(SV("answer is '1.abcp+0'"), SV("answer is '{:a}'"), F(0x1.abcp+0));
920   check(SV("answer is '1.defp+0'"), SV("answer is '{:a}'"), F(0x1.defp+0));
921 
922   // *** align-fill & width ***
923   check(SV("answer is '   1p-2'"), SV("answer is '{:7a}'"), F(0.25));
924   check(SV("answer is '   1p-2'"), SV("answer is '{:>7a}'"), F(0.25));
925   check(SV("answer is '1p-2   '"), SV("answer is '{:<7a}'"), F(0.25));
926   check(SV("answer is ' 1p-2  '"), SV("answer is '{:^7a}'"), F(0.25));
927 
928   // The fill character ':' is allowed here (P0645) but not in ranges (P2286).
929   check(SV("answer is ':::1p-3'"), SV("answer is '{::>7a}'"), F(125e-3));
930   check(SV("answer is '1p-3:::'"), SV("answer is '{::<7a}'"), F(125e-3));
931   check(SV("answer is ':1p-3::'"), SV("answer is '{::^7a}'"), F(125e-3));
932 
933   check(SV("answer is '***inf'"), SV("answer is '{:*>6a}'"), std::numeric_limits<F>::infinity());
934   check(SV("answer is 'inf***'"), SV("answer is '{:*<6a}'"), std::numeric_limits<F>::infinity());
935   check(SV("answer is '*inf**'"), SV("answer is '{:*^6a}'"), std::numeric_limits<F>::infinity());
936 
937   check(SV("answer is '###-inf'"), SV("answer is '{:#>7a}'"), -std::numeric_limits<F>::infinity());
938   check(SV("answer is '-inf###'"), SV("answer is '{:#<7a}'"), -std::numeric_limits<F>::infinity());
939   check(SV("answer is '#-inf##'"), SV("answer is '{:#^7a}'"), -std::numeric_limits<F>::infinity());
940 
941   check(SV("answer is '^^^nan'"), SV("answer is '{:^>6a}'"), nan_pos);
942   check(SV("answer is 'nan^^^'"), SV("answer is '{:^<6a}'"), nan_pos);
943   check(SV("answer is '^nan^^'"), SV("answer is '{:^^6a}'"), nan_pos);
944 
945   check(SV("answer is '000-nan'"), SV("answer is '{:0>7a}'"), nan_neg);
946   check(SV("answer is '-nan000'"), SV("answer is '{:0<7a}'"), nan_neg);
947   check(SV("answer is '0-nan00'"), SV("answer is '{:0^7a}'"), nan_neg);
948 
949   // Test whether zero padding is ignored
950   check(SV("answer is '   1p-2'"), SV("answer is '{:>07a}'"), F(0.25));
951   check(SV("answer is '1p-2   '"), SV("answer is '{:<07a}'"), F(0.25));
952   check(SV("answer is ' 1p-2  '"), SV("answer is '{:^07a}'"), F(0.25));
953 
954   // *** Sign ***
955   check(SV("answer is '0p+0'"), SV("answer is '{:a}'"), F(0));
956   check(SV("answer is '0p+0'"), SV("answer is '{:-a}'"), F(0));
957   check(SV("answer is '+0p+0'"), SV("answer is '{:+a}'"), F(0));
958   check(SV("answer is ' 0p+0'"), SV("answer is '{: a}'"), F(0));
959 
960   check(SV("answer is '-0p+0'"), SV("answer is '{:a}'"), F(-0.));
961   check(SV("answer is '-0p+0'"), SV("answer is '{:-a}'"), F(-0.));
962   check(SV("answer is '-0p+0'"), SV("answer is '{:+a}'"), F(-0.));
963   check(SV("answer is '-0p+0'"), SV("answer is '{: a}'"), F(-0.));
964 
965   // [format.string.std]/5 The sign option applies to floating-point infinity and NaN.
966   check(SV("answer is 'inf'"), SV("answer is '{:a}'"), std::numeric_limits<F>::infinity());
967   check(SV("answer is 'inf'"), SV("answer is '{:-a}'"), std::numeric_limits<F>::infinity());
968   check(SV("answer is '+inf'"), SV("answer is '{:+a}'"), std::numeric_limits<F>::infinity());
969   check(SV("answer is ' inf'"), SV("answer is '{: a}'"), std::numeric_limits<F>::infinity());
970 
971   check(SV("answer is '-inf'"), SV("answer is '{:a}'"), -std::numeric_limits<F>::infinity());
972   check(SV("answer is '-inf'"), SV("answer is '{:-a}'"), -std::numeric_limits<F>::infinity());
973   check(SV("answer is '-inf'"), SV("answer is '{:+a}'"), -std::numeric_limits<F>::infinity());
974   check(SV("answer is '-inf'"), SV("answer is '{: a}'"), -std::numeric_limits<F>::infinity());
975 
976   check(SV("answer is 'nan'"), SV("answer is '{:a}'"), nan_pos);
977   check(SV("answer is 'nan'"), SV("answer is '{:-a}'"), nan_pos);
978   check(SV("answer is '+nan'"), SV("answer is '{:+a}'"), nan_pos);
979   check(SV("answer is ' nan'"), SV("answer is '{: a}'"), nan_pos);
980 
981   check(SV("answer is '-nan'"), SV("answer is '{:a}'"), nan_neg);
982   check(SV("answer is '-nan'"), SV("answer is '{:-a}'"), nan_neg);
983   check(SV("answer is '-nan'"), SV("answer is '{:+a}'"), nan_neg);
984   check(SV("answer is '-nan'"), SV("answer is '{: a}'"), nan_neg);
985 
986   // *** alternate form ***
987   // When precision is zero there's no decimal point except when the alternate form is specified.
988   check(SV("answer is '0p+0'"), SV("answer is '{:a}'"), F(0));
989   check(SV("answer is '0.p+0'"), SV("answer is '{:#a}'"), F(0));
990 
991   check(SV("answer is '1p+1'"), SV("answer is '{:.0a}'"), F(2.5));
992   check(SV("answer is '1.p+1'"), SV("answer is '{:#.0a}'"), F(2.5));
993   check(SV("answer is '1.4p+1'"), SV("answer is '{:#a}'"), F(2.5));
994 
995   check(SV("answer is 'inf'"), SV("answer is '{:#a}'"), std::numeric_limits<F>::infinity());
996   check(SV("answer is '-inf'"), SV("answer is '{:#a}'"), -std::numeric_limits<F>::infinity());
997 
998   check(SV("answer is 'nan'"), SV("answer is '{:#a}'"), nan_pos);
999   check(SV("answer is '-nan'"), SV("answer is '{:#a}'"), nan_neg);
1000 
1001   // *** zero-padding & width ***
1002   check(SV("answer is '1p-5'"), SV("answer is '{:04a}'"), 0.03125);
1003   check(SV("answer is '+1p-5'"), SV("answer is '{:+05a}'"), 0.03125);
1004   check(SV("answer is '+01p-5'"), SV("answer is '{:+06a}'"), 0.03125);
1005 
1006   check(SV("answer is '0001p-5'"), SV("answer is '{:07a}'"), 0.03125);
1007   check(SV("answer is '0001p-5'"), SV("answer is '{:-07a}'"), 0.03125);
1008   check(SV("answer is '+001p-5'"), SV("answer is '{:+07a}'"), 0.03125);
1009   check(SV("answer is ' 001p-5'"), SV("answer is '{: 07a}'"), 0.03125);
1010 
1011   check(SV("answer is '       inf'"), SV("answer is '{:010a}'"), std::numeric_limits<F>::infinity());
1012   check(SV("answer is '       inf'"), SV("answer is '{:-010a}'"), std::numeric_limits<F>::infinity());
1013   check(SV("answer is '      +inf'"), SV("answer is '{:+010a}'"), std::numeric_limits<F>::infinity());
1014   check(SV("answer is '       inf'"), SV("answer is '{: 010a}'"), std::numeric_limits<F>::infinity());
1015 
1016   check(SV("answer is '      -inf'"), SV("answer is '{:010a}'"), -std::numeric_limits<F>::infinity());
1017   check(SV("answer is '      -inf'"), SV("answer is '{:-010a}'"), -std::numeric_limits<F>::infinity());
1018   check(SV("answer is '      -inf'"), SV("answer is '{:+010a}'"), -std::numeric_limits<F>::infinity());
1019   check(SV("answer is '      -inf'"), SV("answer is '{: 010a}'"), -std::numeric_limits<F>::infinity());
1020 
1021   check(SV("answer is '       nan'"), SV("answer is '{:010a}'"), nan_pos);
1022   check(SV("answer is '       nan'"), SV("answer is '{:-010a}'"), nan_pos);
1023   check(SV("answer is '      +nan'"), SV("answer is '{:+010a}'"), nan_pos);
1024   check(SV("answer is '       nan'"), SV("answer is '{: 010a}'"), nan_pos);
1025 
1026   check(SV("answer is '      -nan'"), SV("answer is '{:010a}'"), nan_neg);
1027   check(SV("answer is '      -nan'"), SV("answer is '{:-010a}'"), nan_neg);
1028   check(SV("answer is '      -nan'"), SV("answer is '{:+010a}'"), nan_neg);
1029   check(SV("answer is '      -nan'"), SV("answer is '{: 010a}'"), nan_neg);
1030 
1031   // *** precision ***
1032   // See format_test_floating_point_hex_lower_case_precision
1033 
1034   // *** locale-specific form ***
1035   // See locale-specific_form.pass.cpp
1036 }
1037 
1038 template <class F, class CharT, class TestFunction>
format_test_floating_point_hex_upper_case(TestFunction check)1039 void format_test_floating_point_hex_upper_case(TestFunction check) {
1040   auto nan_pos = std::numeric_limits<F>::quiet_NaN();                          // "nan"
1041   auto nan_neg = std::copysign(nan_pos, static_cast<decltype(nan_pos)>(-1.0)); // "-nan"
1042 
1043   // Test whether the hexadecimal letters are the proper case.
1044   // The precision is too large for float, so two tests are used.
1045   check(SV("answer is '1.ABCP+0'"), SV("answer is '{:A}'"), F(0x1.abcp+0));
1046   check(SV("answer is '1.DEFP+0'"), SV("answer is '{:A}'"), F(0x1.defp+0));
1047 
1048   // *** align-fill & width ***
1049   check(SV("answer is '   1P-2'"), SV("answer is '{:7A}'"), F(0.25));
1050   check(SV("answer is '   1P-2'"), SV("answer is '{:>7A}'"), F(0.25));
1051   check(SV("answer is '1P-2   '"), SV("answer is '{:<7A}'"), F(0.25));
1052   check(SV("answer is ' 1P-2  '"), SV("answer is '{:^7A}'"), F(0.25));
1053 
1054   check(SV("answer is '---1P-3'"), SV("answer is '{:->7A}'"), F(125e-3));
1055   check(SV("answer is '1P-3---'"), SV("answer is '{:-<7A}'"), F(125e-3));
1056   check(SV("answer is '-1P-3--'"), SV("answer is '{:-^7A}'"), F(125e-3));
1057 
1058   check(SV("answer is '***INF'"), SV("answer is '{:*>6A}'"), std::numeric_limits<F>::infinity());
1059   check(SV("answer is 'INF***'"), SV("answer is '{:*<6A}'"), std::numeric_limits<F>::infinity());
1060   check(SV("answer is '*INF**'"), SV("answer is '{:*^6A}'"), std::numeric_limits<F>::infinity());
1061 
1062   check(SV("answer is '###-INF'"), SV("answer is '{:#>7A}'"), -std::numeric_limits<F>::infinity());
1063   check(SV("answer is '-INF###'"), SV("answer is '{:#<7A}'"), -std::numeric_limits<F>::infinity());
1064   check(SV("answer is '#-INF##'"), SV("answer is '{:#^7A}'"), -std::numeric_limits<F>::infinity());
1065 
1066   check(SV("answer is '^^^NAN'"), SV("answer is '{:^>6A}'"), nan_pos);
1067   check(SV("answer is 'NAN^^^'"), SV("answer is '{:^<6A}'"), nan_pos);
1068   check(SV("answer is '^NAN^^'"), SV("answer is '{:^^6A}'"), nan_pos);
1069 
1070   check(SV("answer is '000-NAN'"), SV("answer is '{:0>7A}'"), nan_neg);
1071   check(SV("answer is '-NAN000'"), SV("answer is '{:0<7A}'"), nan_neg);
1072   check(SV("answer is '0-NAN00'"), SV("answer is '{:0^7A}'"), nan_neg);
1073 
1074   // Test whether zero padding is ignored
1075   check(SV("answer is '   1P-2'"), SV("answer is '{:>07A}'"), F(0.25));
1076   check(SV("answer is '1P-2   '"), SV("answer is '{:<07A}'"), F(0.25));
1077   check(SV("answer is ' 1P-2  '"), SV("answer is '{:^07A}'"), F(0.25));
1078 
1079   // *** Sign ***
1080   check(SV("answer is '0P+0'"), SV("answer is '{:A}'"), F(0));
1081   check(SV("answer is '0P+0'"), SV("answer is '{:-A}'"), F(0));
1082   check(SV("answer is '+0P+0'"), SV("answer is '{:+A}'"), F(0));
1083   check(SV("answer is ' 0P+0'"), SV("answer is '{: A}'"), F(0));
1084 
1085   check(SV("answer is '-0P+0'"), SV("answer is '{:A}'"), F(-0.));
1086   check(SV("answer is '-0P+0'"), SV("answer is '{:-A}'"), F(-0.));
1087   check(SV("answer is '-0P+0'"), SV("answer is '{:+A}'"), F(-0.));
1088   check(SV("answer is '-0P+0'"), SV("answer is '{: A}'"), F(-0.));
1089 
1090   // [format.string.std]/5 The sign option applies to floating-point infinity and NaN.
1091   check(SV("answer is 'INF'"), SV("answer is '{:A}'"), std::numeric_limits<F>::infinity());
1092   check(SV("answer is 'INF'"), SV("answer is '{:-A}'"), std::numeric_limits<F>::infinity());
1093   check(SV("answer is '+INF'"), SV("answer is '{:+A}'"), std::numeric_limits<F>::infinity());
1094   check(SV("answer is ' INF'"), SV("answer is '{: A}'"), std::numeric_limits<F>::infinity());
1095 
1096   check(SV("answer is '-INF'"), SV("answer is '{:A}'"), -std::numeric_limits<F>::infinity());
1097   check(SV("answer is '-INF'"), SV("answer is '{:-A}'"), -std::numeric_limits<F>::infinity());
1098   check(SV("answer is '-INF'"), SV("answer is '{:+A}'"), -std::numeric_limits<F>::infinity());
1099   check(SV("answer is '-INF'"), SV("answer is '{: A}'"), -std::numeric_limits<F>::infinity());
1100 
1101   check(SV("answer is 'NAN'"), SV("answer is '{:A}'"), nan_pos);
1102   check(SV("answer is 'NAN'"), SV("answer is '{:-A}'"), nan_pos);
1103   check(SV("answer is '+NAN'"), SV("answer is '{:+A}'"), nan_pos);
1104   check(SV("answer is ' NAN'"), SV("answer is '{: A}'"), nan_pos);
1105 
1106   check(SV("answer is '-NAN'"), SV("answer is '{:A}'"), nan_neg);
1107   check(SV("answer is '-NAN'"), SV("answer is '{:-A}'"), nan_neg);
1108   check(SV("answer is '-NAN'"), SV("answer is '{:+A}'"), nan_neg);
1109   check(SV("answer is '-NAN'"), SV("answer is '{: A}'"), nan_neg);
1110 
1111   // *** alternate form ***
1112   // When precision is zero there's no decimal point except when the alternate form is specified.
1113   check(SV("answer is '0P+0'"), SV("answer is '{:A}'"), F(0));
1114   check(SV("answer is '0.P+0'"), SV("answer is '{:#A}'"), F(0));
1115 
1116   check(SV("answer is '1P+1'"), SV("answer is '{:.0A}'"), F(2.5));
1117   check(SV("answer is '1.P+1'"), SV("answer is '{:#.0A}'"), F(2.5));
1118   check(SV("answer is '1.4P+1'"), SV("answer is '{:#A}'"), F(2.5));
1119 
1120   check(SV("answer is 'INF'"), SV("answer is '{:#A}'"), std::numeric_limits<F>::infinity());
1121   check(SV("answer is '-INF'"), SV("answer is '{:#A}'"), -std::numeric_limits<F>::infinity());
1122 
1123   check(SV("answer is 'NAN'"), SV("answer is '{:#A}'"), nan_pos);
1124   check(SV("answer is '-NAN'"), SV("answer is '{:#A}'"), nan_neg);
1125 
1126   // *** zero-padding & width ***
1127   check(SV("answer is '1P-5'"), SV("answer is '{:04A}'"), 0.03125);
1128   check(SV("answer is '+1P-5'"), SV("answer is '{:+05A}'"), 0.03125);
1129   check(SV("answer is '+01P-5'"), SV("answer is '{:+06A}'"), 0.03125);
1130 
1131   check(SV("answer is '0001P-5'"), SV("answer is '{:07A}'"), 0.03125);
1132   check(SV("answer is '0001P-5'"), SV("answer is '{:-07A}'"), 0.03125);
1133   check(SV("answer is '+001P-5'"), SV("answer is '{:+07A}'"), 0.03125);
1134   check(SV("answer is ' 001P-5'"), SV("answer is '{: 07A}'"), 0.03125);
1135 
1136   check(SV("answer is '       INF'"), SV("answer is '{:010A}'"), std::numeric_limits<F>::infinity());
1137   check(SV("answer is '       INF'"), SV("answer is '{:-010A}'"), std::numeric_limits<F>::infinity());
1138   check(SV("answer is '      +INF'"), SV("answer is '{:+010A}'"), std::numeric_limits<F>::infinity());
1139   check(SV("answer is '       INF'"), SV("answer is '{: 010A}'"), std::numeric_limits<F>::infinity());
1140 
1141   check(SV("answer is '      -INF'"), SV("answer is '{:010A}'"), -std::numeric_limits<F>::infinity());
1142   check(SV("answer is '      -INF'"), SV("answer is '{:-010A}'"), -std::numeric_limits<F>::infinity());
1143   check(SV("answer is '      -INF'"), SV("answer is '{:+010A}'"), -std::numeric_limits<F>::infinity());
1144   check(SV("answer is '      -INF'"), SV("answer is '{: 010A}'"), -std::numeric_limits<F>::infinity());
1145 
1146   check(SV("answer is '       NAN'"), SV("answer is '{:010A}'"), nan_pos);
1147   check(SV("answer is '       NAN'"), SV("answer is '{:-010A}'"), nan_pos);
1148   check(SV("answer is '      +NAN'"), SV("answer is '{:+010A}'"), nan_pos);
1149   check(SV("answer is '       NAN'"), SV("answer is '{: 010A}'"), nan_pos);
1150 
1151   check(SV("answer is '      -NAN'"), SV("answer is '{:010A}'"), nan_neg);
1152   check(SV("answer is '      -NAN'"), SV("answer is '{:-010A}'"), nan_neg);
1153   check(SV("answer is '      -NAN'"), SV("answer is '{:+010A}'"), nan_neg);
1154   check(SV("answer is '      -NAN'"), SV("answer is '{: 010A}'"), nan_neg);
1155 
1156   // *** precision ***
1157   // See format_test_floating_point_hex_upper_case_precision
1158 
1159   // *** locale-specific form ***
1160   // See locale-specific_form.pass.cpp
1161 }
1162 
1163 template <class F, class CharT, class TestFunction>
format_test_floating_point_hex_lower_case_precision(TestFunction check)1164 void format_test_floating_point_hex_lower_case_precision(TestFunction check) {
1165   auto nan_pos = std::numeric_limits<F>::quiet_NaN();                          // "nan"
1166   auto nan_neg = std::copysign(nan_pos, static_cast<decltype(nan_pos)>(-1.0)); // "-nan"
1167 
1168   // *** align-fill & width ***
1169   check(SV("answer is '   1.000000p-2'"), SV("answer is '{:14.6a}'"), F(0.25));
1170   check(SV("answer is '   1.000000p-2'"), SV("answer is '{:>14.6a}'"), F(0.25));
1171   check(SV("answer is '1.000000p-2   '"), SV("answer is '{:<14.6a}'"), F(0.25));
1172   check(SV("answer is ' 1.000000p-2  '"), SV("answer is '{:^14.6a}'"), F(0.25));
1173 
1174   check(SV("answer is '---1.000000p-3'"), SV("answer is '{:->14.6a}'"), F(125e-3));
1175   check(SV("answer is '1.000000p-3---'"), SV("answer is '{:-<14.6a}'"), F(125e-3));
1176   check(SV("answer is '-1.000000p-3--'"), SV("answer is '{:-^14.6a}'"), F(125e-3));
1177 
1178   check(SV("answer is '***inf'"), SV("answer is '{:*>6.6a}'"), std::numeric_limits<F>::infinity());
1179   check(SV("answer is 'inf***'"), SV("answer is '{:*<6.6a}'"), std::numeric_limits<F>::infinity());
1180   check(SV("answer is '*inf**'"), SV("answer is '{:*^6.6a}'"), std::numeric_limits<F>::infinity());
1181 
1182   check(SV("answer is '###-inf'"), SV("answer is '{:#>7.6a}'"), -std::numeric_limits<F>::infinity());
1183   check(SV("answer is '-inf###'"), SV("answer is '{:#<7.6a}'"), -std::numeric_limits<F>::infinity());
1184   check(SV("answer is '#-inf##'"), SV("answer is '{:#^7.6a}'"), -std::numeric_limits<F>::infinity());
1185 
1186   check(SV("answer is '^^^nan'"), SV("answer is '{:^>6.6a}'"), nan_pos);
1187   check(SV("answer is 'nan^^^'"), SV("answer is '{:^<6.6a}'"), nan_pos);
1188   check(SV("answer is '^nan^^'"), SV("answer is '{:^^6.6a}'"), nan_pos);
1189 
1190   check(SV("answer is '000-nan'"), SV("answer is '{:0>7.6a}'"), nan_neg);
1191   check(SV("answer is '-nan000'"), SV("answer is '{:0<7.6a}'"), nan_neg);
1192   check(SV("answer is '0-nan00'"), SV("answer is '{:0^7.6a}'"), nan_neg);
1193 
1194   // Test whether zero padding is ignored
1195   check(SV("answer is '   1.000000p-2'"), SV("answer is '{:>014.6a}'"), F(0.25));
1196   check(SV("answer is '1.000000p-2   '"), SV("answer is '{:<014.6a}'"), F(0.25));
1197   check(SV("answer is ' 1.000000p-2  '"), SV("answer is '{:^014.6a}'"), F(0.25));
1198 
1199   // *** Sign ***
1200   check(SV("answer is '0.000000p+0'"), SV("answer is '{:.6a}'"), F(0));
1201   check(SV("answer is '0.000000p+0'"), SV("answer is '{:-.6a}'"), F(0));
1202   check(SV("answer is '+0.000000p+0'"), SV("answer is '{:+.6a}'"), F(0));
1203   check(SV("answer is ' 0.000000p+0'"), SV("answer is '{: .6a}'"), F(0));
1204 
1205   check(SV("answer is '-0.000000p+0'"), SV("answer is '{:.6a}'"), F(-0.));
1206   check(SV("answer is '-0.000000p+0'"), SV("answer is '{:-.6a}'"), F(-0.));
1207   check(SV("answer is '-0.000000p+0'"), SV("answer is '{:+.6a}'"), F(-0.));
1208   check(SV("answer is '-0.000000p+0'"), SV("answer is '{: .6a}'"), F(-0.));
1209 
1210   // [format.string.std]/5 The sign option applies to floating-point infinity and NaN.
1211   check(SV("answer is 'inf'"), SV("answer is '{:.6a}'"), std::numeric_limits<F>::infinity());
1212   check(SV("answer is 'inf'"), SV("answer is '{:-.6a}'"), std::numeric_limits<F>::infinity());
1213   check(SV("answer is '+inf'"), SV("answer is '{:+.6a}'"), std::numeric_limits<F>::infinity());
1214   check(SV("answer is ' inf'"), SV("answer is '{: .6a}'"), std::numeric_limits<F>::infinity());
1215 
1216   check(SV("answer is '-inf'"), SV("answer is '{:.6a}'"), -std::numeric_limits<F>::infinity());
1217   check(SV("answer is '-inf'"), SV("answer is '{:-.6a}'"), -std::numeric_limits<F>::infinity());
1218   check(SV("answer is '-inf'"), SV("answer is '{:+.6a}'"), -std::numeric_limits<F>::infinity());
1219   check(SV("answer is '-inf'"), SV("answer is '{: .6a}'"), -std::numeric_limits<F>::infinity());
1220 
1221   check(SV("answer is 'nan'"), SV("answer is '{:.6a}'"), nan_pos);
1222   check(SV("answer is 'nan'"), SV("answer is '{:-.6a}'"), nan_pos);
1223   check(SV("answer is '+nan'"), SV("answer is '{:+.6a}'"), nan_pos);
1224   check(SV("answer is ' nan'"), SV("answer is '{: .6a}'"), nan_pos);
1225 
1226   check(SV("answer is '-nan'"), SV("answer is '{:.6a}'"), nan_neg);
1227   check(SV("answer is '-nan'"), SV("answer is '{:-.6a}'"), nan_neg);
1228   check(SV("answer is '-nan'"), SV("answer is '{:+.6a}'"), nan_neg);
1229   check(SV("answer is '-nan'"), SV("answer is '{: .6a}'"), nan_neg);
1230 
1231   // *** alternate form ***
1232   check(SV("answer is '1.400000p+1'"), SV("answer is '{:#.6a}'"), F(2.5));
1233 
1234   check(SV("answer is 'inf'"), SV("answer is '{:#.6a}'"), std::numeric_limits<F>::infinity());
1235   check(SV("answer is '-inf'"), SV("answer is '{:#.6a}'"), -std::numeric_limits<F>::infinity());
1236 
1237   check(SV("answer is 'nan'"), SV("answer is '{:#.6a}'"), nan_pos);
1238   check(SV("answer is '-nan'"), SV("answer is '{:#.6a}'"), nan_neg);
1239 
1240   // *** zero-padding & width ***
1241   check(SV("answer is '1.000000p-5'"), SV("answer is '{:011.6a}'"), 0.03125);
1242   check(SV("answer is '+1.000000p-5'"), SV("answer is '{:+012.6a}'"), 0.03125);
1243   check(SV("answer is '+01.000000p-5'"), SV("answer is '{:+013.6a}'"), 0.03125);
1244 
1245   check(SV("answer is '0001.000000p-5'"), SV("answer is '{:014.6a}'"), 0.03125);
1246   check(SV("answer is '0001.000000p-5'"), SV("answer is '{:-014.6a}'"), 0.03125);
1247   check(SV("answer is '+001.000000p-5'"), SV("answer is '{:+014.6a}'"), 0.03125);
1248   check(SV("answer is ' 001.000000p-5'"), SV("answer is '{: 014.6a}'"), 0.03125);
1249 
1250   check(SV("answer is '       inf'"), SV("answer is '{:010.6a}'"), std::numeric_limits<F>::infinity());
1251   check(SV("answer is '       inf'"), SV("answer is '{:-010.6a}'"), std::numeric_limits<F>::infinity());
1252   check(SV("answer is '      +inf'"), SV("answer is '{:+010.6a}'"), std::numeric_limits<F>::infinity());
1253   check(SV("answer is '       inf'"), SV("answer is '{: 010.6a}'"), std::numeric_limits<F>::infinity());
1254 
1255   check(SV("answer is '      -inf'"), SV("answer is '{:010.6a}'"), -std::numeric_limits<F>::infinity());
1256   check(SV("answer is '      -inf'"), SV("answer is '{:-010.6a}'"), -std::numeric_limits<F>::infinity());
1257   check(SV("answer is '      -inf'"), SV("answer is '{:+010.6a}'"), -std::numeric_limits<F>::infinity());
1258   check(SV("answer is '      -inf'"), SV("answer is '{: 010.6a}'"), -std::numeric_limits<F>::infinity());
1259 
1260   check(SV("answer is '       nan'"), SV("answer is '{:010.6a}'"), nan_pos);
1261   check(SV("answer is '       nan'"), SV("answer is '{:-010.6a}'"), nan_pos);
1262   check(SV("answer is '      +nan'"), SV("answer is '{:+010.6a}'"), nan_pos);
1263   check(SV("answer is '       nan'"), SV("answer is '{: 010.6a}'"), nan_pos);
1264 
1265   check(SV("answer is '      -nan'"), SV("answer is '{:010.6a}'"), nan_neg);
1266   check(SV("answer is '      -nan'"), SV("answer is '{:-010.6a}'"), nan_neg);
1267   check(SV("answer is '      -nan'"), SV("answer is '{:+010.6a}'"), nan_neg);
1268   check(SV("answer is '      -nan'"), SV("answer is '{: 010.6a}'"), nan_neg);
1269 
1270   // *** locale-specific form ***
1271   // See locale-specific_form.pass.cpp
1272 }
1273 
1274 template <class F, class CharT, class TestFunction>
format_test_floating_point_hex_upper_case_precision(TestFunction check)1275 void format_test_floating_point_hex_upper_case_precision(TestFunction check) {
1276   auto nan_pos = std::numeric_limits<F>::quiet_NaN();                          // "nan"
1277   auto nan_neg = std::copysign(nan_pos, static_cast<decltype(nan_pos)>(-1.0)); // "-nan"
1278 
1279   // *** align-fill & width ***
1280   check(SV("answer is '   1.000000P-2'"), SV("answer is '{:14.6A}'"), F(0.25));
1281   check(SV("answer is '   1.000000P-2'"), SV("answer is '{:>14.6A}'"), F(0.25));
1282   check(SV("answer is '1.000000P-2   '"), SV("answer is '{:<14.6A}'"), F(0.25));
1283   check(SV("answer is ' 1.000000P-2  '"), SV("answer is '{:^14.6A}'"), F(0.25));
1284 
1285   check(SV("answer is '---1.000000P-3'"), SV("answer is '{:->14.6A}'"), F(125e-3));
1286   check(SV("answer is '1.000000P-3---'"), SV("answer is '{:-<14.6A}'"), F(125e-3));
1287   check(SV("answer is '-1.000000P-3--'"), SV("answer is '{:-^14.6A}'"), F(125e-3));
1288 
1289   check(SV("answer is '***INF'"), SV("answer is '{:*>6.6A}'"), std::numeric_limits<F>::infinity());
1290   check(SV("answer is 'INF***'"), SV("answer is '{:*<6.6A}'"), std::numeric_limits<F>::infinity());
1291   check(SV("answer is '*INF**'"), SV("answer is '{:*^6.6A}'"), std::numeric_limits<F>::infinity());
1292 
1293   check(SV("answer is '###-INF'"), SV("answer is '{:#>7.6A}'"), -std::numeric_limits<F>::infinity());
1294   check(SV("answer is '-INF###'"), SV("answer is '{:#<7.6A}'"), -std::numeric_limits<F>::infinity());
1295   check(SV("answer is '#-INF##'"), SV("answer is '{:#^7.6A}'"), -std::numeric_limits<F>::infinity());
1296 
1297   check(SV("answer is '^^^NAN'"), SV("answer is '{:^>6.6A}'"), nan_pos);
1298   check(SV("answer is 'NAN^^^'"), SV("answer is '{:^<6.6A}'"), nan_pos);
1299   check(SV("answer is '^NAN^^'"), SV("answer is '{:^^6.6A}'"), nan_pos);
1300 
1301   check(SV("answer is '000-NAN'"), SV("answer is '{:0>7.6A}'"), nan_neg);
1302   check(SV("answer is '-NAN000'"), SV("answer is '{:0<7.6A}'"), nan_neg);
1303   check(SV("answer is '0-NAN00'"), SV("answer is '{:0^7.6A}'"), nan_neg);
1304 
1305   // Test whether zero padding is ignored
1306   check(SV("answer is '   1.000000P-2'"), SV("answer is '{:>014.6A}'"), F(0.25));
1307   check(SV("answer is '1.000000P-2   '"), SV("answer is '{:<014.6A}'"), F(0.25));
1308   check(SV("answer is ' 1.000000P-2  '"), SV("answer is '{:^014.6A}'"), F(0.25));
1309 
1310   // *** Sign ***
1311   check(SV("answer is '0.000000P+0'"), SV("answer is '{:.6A}'"), F(0));
1312   check(SV("answer is '0.000000P+0'"), SV("answer is '{:-.6A}'"), F(0));
1313   check(SV("answer is '+0.000000P+0'"), SV("answer is '{:+.6A}'"), F(0));
1314   check(SV("answer is ' 0.000000P+0'"), SV("answer is '{: .6A}'"), F(0));
1315 
1316   check(SV("answer is '-0.000000P+0'"), SV("answer is '{:.6A}'"), F(-0.));
1317   check(SV("answer is '-0.000000P+0'"), SV("answer is '{:-.6A}'"), F(-0.));
1318   check(SV("answer is '-0.000000P+0'"), SV("answer is '{:+.6A}'"), F(-0.));
1319   check(SV("answer is '-0.000000P+0'"), SV("answer is '{: .6A}'"), F(-0.));
1320 
1321   // [format.string.std]/5 The sign option applies to floating-point infinity and NaN.
1322   check(SV("answer is 'INF'"), SV("answer is '{:.6A}'"), std::numeric_limits<F>::infinity());
1323   check(SV("answer is 'INF'"), SV("answer is '{:-.6A}'"), std::numeric_limits<F>::infinity());
1324   check(SV("answer is '+INF'"), SV("answer is '{:+.6A}'"), std::numeric_limits<F>::infinity());
1325   check(SV("answer is ' INF'"), SV("answer is '{: .6A}'"), std::numeric_limits<F>::infinity());
1326 
1327   check(SV("answer is '-INF'"), SV("answer is '{:.6A}'"), -std::numeric_limits<F>::infinity());
1328   check(SV("answer is '-INF'"), SV("answer is '{:-.6A}'"), -std::numeric_limits<F>::infinity());
1329   check(SV("answer is '-INF'"), SV("answer is '{:+.6A}'"), -std::numeric_limits<F>::infinity());
1330   check(SV("answer is '-INF'"), SV("answer is '{: .6A}'"), -std::numeric_limits<F>::infinity());
1331 
1332   check(SV("answer is 'NAN'"), SV("answer is '{:.6A}'"), nan_pos);
1333   check(SV("answer is 'NAN'"), SV("answer is '{:-.6A}'"), nan_pos);
1334   check(SV("answer is '+NAN'"), SV("answer is '{:+.6A}'"), nan_pos);
1335   check(SV("answer is ' NAN'"), SV("answer is '{: .6A}'"), nan_pos);
1336 
1337   check(SV("answer is '-NAN'"), SV("answer is '{:.6A}'"), nan_neg);
1338   check(SV("answer is '-NAN'"), SV("answer is '{:-.6A}'"), nan_neg);
1339   check(SV("answer is '-NAN'"), SV("answer is '{:+.6A}'"), nan_neg);
1340   check(SV("answer is '-NAN'"), SV("answer is '{: .6A}'"), nan_neg);
1341 
1342   // *** alternate form ***
1343   check(SV("answer is '1.400000P+1'"), SV("answer is '{:#.6A}'"), F(2.5));
1344 
1345   check(SV("answer is 'INF'"), SV("answer is '{:#.6A}'"), std::numeric_limits<F>::infinity());
1346   check(SV("answer is '-INF'"), SV("answer is '{:#.6A}'"), -std::numeric_limits<F>::infinity());
1347 
1348   check(SV("answer is 'NAN'"), SV("answer is '{:#.6A}'"), nan_pos);
1349   check(SV("answer is '-NAN'"), SV("answer is '{:#.6A}'"), nan_neg);
1350 
1351   // *** zero-padding & width ***
1352   check(SV("answer is '1.000000P-5'"), SV("answer is '{:011.6A}'"), 0.03125);
1353   check(SV("answer is '+1.000000P-5'"), SV("answer is '{:+012.6A}'"), 0.03125);
1354   check(SV("answer is '+01.000000P-5'"), SV("answer is '{:+013.6A}'"), 0.03125);
1355 
1356   check(SV("answer is '0001.000000P-5'"), SV("answer is '{:014.6A}'"), 0.03125);
1357   check(SV("answer is '0001.000000P-5'"), SV("answer is '{:-014.6A}'"), 0.03125);
1358   check(SV("answer is '+001.000000P-5'"), SV("answer is '{:+014.6A}'"), 0.03125);
1359   check(SV("answer is ' 001.000000P-5'"), SV("answer is '{: 014.6A}'"), 0.03125);
1360 
1361   check(SV("answer is '       INF'"), SV("answer is '{:010.6A}'"), std::numeric_limits<F>::infinity());
1362   check(SV("answer is '       INF'"), SV("answer is '{:-010.6A}'"), std::numeric_limits<F>::infinity());
1363   check(SV("answer is '      +INF'"), SV("answer is '{:+010.6A}'"), std::numeric_limits<F>::infinity());
1364   check(SV("answer is '       INF'"), SV("answer is '{: 010.6A}'"), std::numeric_limits<F>::infinity());
1365 
1366   check(SV("answer is '      -INF'"), SV("answer is '{:010.6A}'"), -std::numeric_limits<F>::infinity());
1367   check(SV("answer is '      -INF'"), SV("answer is '{:-010.6A}'"), -std::numeric_limits<F>::infinity());
1368   check(SV("answer is '      -INF'"), SV("answer is '{:+010.6A}'"), -std::numeric_limits<F>::infinity());
1369   check(SV("answer is '      -INF'"), SV("answer is '{: 010.6A}'"), -std::numeric_limits<F>::infinity());
1370 
1371   check(SV("answer is '       NAN'"), SV("answer is '{:010.6A}'"), nan_pos);
1372   check(SV("answer is '       NAN'"), SV("answer is '{:-010.6A}'"), nan_pos);
1373   check(SV("answer is '      +NAN'"), SV("answer is '{:+010.6A}'"), nan_pos);
1374   check(SV("answer is '       NAN'"), SV("answer is '{: 010.6A}'"), nan_pos);
1375 
1376   check(SV("answer is '      -NAN'"), SV("answer is '{:010.6A}'"), nan_neg);
1377   check(SV("answer is '      -NAN'"), SV("answer is '{:-010.6A}'"), nan_neg);
1378   check(SV("answer is '      -NAN'"), SV("answer is '{:+010.6A}'"), nan_neg);
1379   check(SV("answer is '      -NAN'"), SV("answer is '{: 010.6A}'"), nan_neg);
1380 
1381   // *** locale-specific form ***
1382   // See locale-specific_form.pass.cpp
1383 }
1384 
1385 template <class F, class CharT, class TestFunction>
format_test_floating_point_scientific_lower_case(TestFunction check)1386 void format_test_floating_point_scientific_lower_case(TestFunction check) {
1387   auto nan_pos = std::numeric_limits<F>::quiet_NaN();                          // "nan"
1388   auto nan_neg = std::copysign(nan_pos, static_cast<decltype(nan_pos)>(-1.0)); // "-nan"
1389 
1390   // *** align-fill & width ***
1391   check(SV("answer is '   2.500000e-01'"), SV("answer is '{:15e}'"), F(0.25));
1392   check(SV("answer is '   2.500000e-01'"), SV("answer is '{:>15e}'"), F(0.25));
1393   check(SV("answer is '2.500000e-01   '"), SV("answer is '{:<15e}'"), F(0.25));
1394   check(SV("answer is ' 2.500000e-01  '"), SV("answer is '{:^15e}'"), F(0.25));
1395 
1396   check(SV("answer is '---1.250000e-01'"), SV("answer is '{:->15e}'"), F(125e-3));
1397   check(SV("answer is '1.250000e-01---'"), SV("answer is '{:-<15e}'"), F(125e-3));
1398   check(SV("answer is '-1.250000e-01--'"), SV("answer is '{:-^15e}'"), F(125e-3));
1399 
1400   check(SV("answer is '***inf'"), SV("answer is '{:*>6e}'"), std::numeric_limits<F>::infinity());
1401   check(SV("answer is 'inf***'"), SV("answer is '{:*<6e}'"), std::numeric_limits<F>::infinity());
1402   check(SV("answer is '*inf**'"), SV("answer is '{:*^6e}'"), std::numeric_limits<F>::infinity());
1403 
1404   check(SV("answer is '###-inf'"), SV("answer is '{:#>7e}'"), -std::numeric_limits<F>::infinity());
1405   check(SV("answer is '-inf###'"), SV("answer is '{:#<7e}'"), -std::numeric_limits<F>::infinity());
1406   check(SV("answer is '#-inf##'"), SV("answer is '{:#^7e}'"), -std::numeric_limits<F>::infinity());
1407 
1408   check(SV("answer is '^^^nan'"), SV("answer is '{:^>6e}'"), nan_pos);
1409   check(SV("answer is 'nan^^^'"), SV("answer is '{:^<6e}'"), nan_pos);
1410   check(SV("answer is '^nan^^'"), SV("answer is '{:^^6e}'"), nan_pos);
1411 
1412   check(SV("answer is '000-nan'"), SV("answer is '{:0>7e}'"), nan_neg);
1413   check(SV("answer is '-nan000'"), SV("answer is '{:0<7e}'"), nan_neg);
1414   check(SV("answer is '0-nan00'"), SV("answer is '{:0^7e}'"), nan_neg);
1415 
1416   // Test whether zero padding is ignored
1417   check(SV("answer is '   2.500000e-01'"), SV("answer is '{:>015e}'"), F(0.25));
1418   check(SV("answer is '2.500000e-01   '"), SV("answer is '{:<015e}'"), F(0.25));
1419   check(SV("answer is ' 2.500000e-01  '"), SV("answer is '{:^015e}'"), F(0.25));
1420 
1421   // *** Sign ***
1422   check(SV("answer is '0.000000e+00'"), SV("answer is '{:e}'"), F(0));
1423   check(SV("answer is '0.000000e+00'"), SV("answer is '{:-e}'"), F(0));
1424   check(SV("answer is '+0.000000e+00'"), SV("answer is '{:+e}'"), F(0));
1425   check(SV("answer is ' 0.000000e+00'"), SV("answer is '{: e}'"), F(0));
1426 
1427   check(SV("answer is '-0.000000e+00'"), SV("answer is '{:e}'"), F(-0.));
1428   check(SV("answer is '-0.000000e+00'"), SV("answer is '{:-e}'"), F(-0.));
1429   check(SV("answer is '-0.000000e+00'"), SV("answer is '{:+e}'"), F(-0.));
1430   check(SV("answer is '-0.000000e+00'"), SV("answer is '{: e}'"), F(-0.));
1431 
1432   // [format.string.std]/5 The sign option applies to floating-point infinity and NaN.
1433   check(SV("answer is 'inf'"), SV("answer is '{:e}'"), std::numeric_limits<F>::infinity());
1434   check(SV("answer is 'inf'"), SV("answer is '{:-e}'"), std::numeric_limits<F>::infinity());
1435   check(SV("answer is '+inf'"), SV("answer is '{:+e}'"), std::numeric_limits<F>::infinity());
1436   check(SV("answer is ' inf'"), SV("answer is '{: e}'"), std::numeric_limits<F>::infinity());
1437 
1438   check(SV("answer is '-inf'"), SV("answer is '{:e}'"), -std::numeric_limits<F>::infinity());
1439   check(SV("answer is '-inf'"), SV("answer is '{:-e}'"), -std::numeric_limits<F>::infinity());
1440   check(SV("answer is '-inf'"), SV("answer is '{:+e}'"), -std::numeric_limits<F>::infinity());
1441   check(SV("answer is '-inf'"), SV("answer is '{: e}'"), -std::numeric_limits<F>::infinity());
1442 
1443   check(SV("answer is 'nan'"), SV("answer is '{:e}'"), nan_pos);
1444   check(SV("answer is 'nan'"), SV("answer is '{:-e}'"), nan_pos);
1445   check(SV("answer is '+nan'"), SV("answer is '{:+e}'"), nan_pos);
1446   check(SV("answer is ' nan'"), SV("answer is '{: e}'"), nan_pos);
1447 
1448   check(SV("answer is '-nan'"), SV("answer is '{:e}'"), nan_neg);
1449   check(SV("answer is '-nan'"), SV("answer is '{:-e}'"), nan_neg);
1450   check(SV("answer is '-nan'"), SV("answer is '{:+e}'"), nan_neg);
1451   check(SV("answer is '-nan'"), SV("answer is '{: e}'"), nan_neg);
1452 
1453   // *** alternate form **
1454   // When precision is zero there's no decimal point except when the alternate form is specified.
1455   check(SV("answer is '0e+00'"), SV("answer is '{:.0e}'"), F(0));
1456   check(SV("answer is '0.e+00'"), SV("answer is '{:#.0e}'"), F(0));
1457 
1458   check(SV("answer is '0.000000e+00'"), SV("answer is '{:#e}'"), F(0));
1459   check(SV("answer is '2.500000e+00'"), SV("answer is '{:#e}'"), F(2.5));
1460 
1461   check(SV("answer is 'inf'"), SV("answer is '{:#e}'"), std::numeric_limits<F>::infinity());
1462   check(SV("answer is '-inf'"), SV("answer is '{:#e}'"), -std::numeric_limits<F>::infinity());
1463 
1464   check(SV("answer is 'nan'"), SV("answer is '{:#e}'"), nan_pos);
1465   check(SV("answer is '-nan'"), SV("answer is '{:#e}'"), nan_neg);
1466 
1467   // *** zero-padding & width ***
1468   check(SV("answer is '3.125000e-02'"), SV("answer is '{:07e}'"), 0.03125);
1469   check(SV("answer is '+3.125000e-02'"), SV("answer is '{:+07e}'"), 0.03125);
1470   check(SV("answer is '+3.125000e-02'"), SV("answer is '{:+08e}'"), 0.03125);
1471   check(SV("answer is '+3.125000e-02'"), SV("answer is '{:+09e}'"), 0.03125);
1472 
1473   check(SV("answer is '003.125000e-02'"), SV("answer is '{:014e}'"), 0.03125);
1474   check(SV("answer is '003.125000e-02'"), SV("answer is '{:-014e}'"), 0.03125);
1475   check(SV("answer is '+03.125000e-02'"), SV("answer is '{:+014e}'"), 0.03125);
1476   check(SV("answer is ' 03.125000e-02'"), SV("answer is '{: 014e}'"), 0.03125);
1477 
1478   check(SV("answer is '       inf'"), SV("answer is '{:010e}'"), std::numeric_limits<F>::infinity());
1479   check(SV("answer is '       inf'"), SV("answer is '{:-010e}'"), std::numeric_limits<F>::infinity());
1480   check(SV("answer is '      +inf'"), SV("answer is '{:+010e}'"), std::numeric_limits<F>::infinity());
1481   check(SV("answer is '       inf'"), SV("answer is '{: 010e}'"), std::numeric_limits<F>::infinity());
1482 
1483   check(SV("answer is '      -inf'"), SV("answer is '{:010e}'"), -std::numeric_limits<F>::infinity());
1484   check(SV("answer is '      -inf'"), SV("answer is '{:-010e}'"), -std::numeric_limits<F>::infinity());
1485   check(SV("answer is '      -inf'"), SV("answer is '{:+010e}'"), -std::numeric_limits<F>::infinity());
1486   check(SV("answer is '      -inf'"), SV("answer is '{: 010e}'"), -std::numeric_limits<F>::infinity());
1487 
1488   check(SV("answer is '       nan'"), SV("answer is '{:010e}'"), nan_pos);
1489   check(SV("answer is '       nan'"), SV("answer is '{:-010e}'"), nan_pos);
1490   check(SV("answer is '      +nan'"), SV("answer is '{:+010e}'"), nan_pos);
1491   check(SV("answer is '       nan'"), SV("answer is '{: 010e}'"), nan_pos);
1492 
1493   check(SV("answer is '      -nan'"), SV("answer is '{:010e}'"), nan_neg);
1494   check(SV("answer is '      -nan'"), SV("answer is '{:-010e}'"), nan_neg);
1495   check(SV("answer is '      -nan'"), SV("answer is '{:+010e}'"), nan_neg);
1496   check(SV("answer is '      -nan'"), SV("answer is '{: 010e}'"), nan_neg);
1497 
1498   // *** precision ***
1499   check(SV("answer is '3e-02'"), SV("answer is '{:.0e}'"), 0.03125);
1500   check(SV("answer is '3.1e-02'"), SV("answer is '{:.1e}'"), 0.03125);
1501   check(SV("answer is '3.125e-02'"), SV("answer is '{:.3e}'"), 0.03125);
1502   check(SV("answer is '3.1250000000e-02'"), SV("answer is '{:.10e}'"), 0.03125);
1503 
1504   // *** locale-specific form ***
1505   // See locale-specific_form.pass.cpp
1506 }
1507 
1508 template <class F, class CharT, class TestFunction>
format_test_floating_point_scientific_upper_case(TestFunction check)1509 void format_test_floating_point_scientific_upper_case(TestFunction check) {
1510   auto nan_pos = std::numeric_limits<F>::quiet_NaN();                          // "nan"
1511   auto nan_neg = std::copysign(nan_pos, static_cast<decltype(nan_pos)>(-1.0)); // "-nan"
1512 
1513   // *** align-fill & width ***
1514   check(SV("answer is '   2.500000E-01'"), SV("answer is '{:15E}'"), F(0.25));
1515   check(SV("answer is '   2.500000E-01'"), SV("answer is '{:>15E}'"), F(0.25));
1516   check(SV("answer is '2.500000E-01   '"), SV("answer is '{:<15E}'"), F(0.25));
1517   check(SV("answer is ' 2.500000E-01  '"), SV("answer is '{:^15E}'"), F(0.25));
1518 
1519   check(SV("answer is '---1.250000E-01'"), SV("answer is '{:->15E}'"), F(125e-3));
1520   check(SV("answer is '1.250000E-01---'"), SV("answer is '{:-<15E}'"), F(125e-3));
1521   check(SV("answer is '-1.250000E-01--'"), SV("answer is '{:-^15E}'"), F(125e-3));
1522 
1523   check(SV("answer is '***INF'"), SV("answer is '{:*>6E}'"), std::numeric_limits<F>::infinity());
1524   check(SV("answer is 'INF***'"), SV("answer is '{:*<6E}'"), std::numeric_limits<F>::infinity());
1525   check(SV("answer is '*INF**'"), SV("answer is '{:*^6E}'"), std::numeric_limits<F>::infinity());
1526 
1527   check(SV("answer is '###-INF'"), SV("answer is '{:#>7E}'"), -std::numeric_limits<F>::infinity());
1528   check(SV("answer is '-INF###'"), SV("answer is '{:#<7E}'"), -std::numeric_limits<F>::infinity());
1529   check(SV("answer is '#-INF##'"), SV("answer is '{:#^7E}'"), -std::numeric_limits<F>::infinity());
1530 
1531   check(SV("answer is '^^^NAN'"), SV("answer is '{:^>6E}'"), nan_pos);
1532   check(SV("answer is 'NAN^^^'"), SV("answer is '{:^<6E}'"), nan_pos);
1533   check(SV("answer is '^NAN^^'"), SV("answer is '{:^^6E}'"), nan_pos);
1534 
1535   check(SV("answer is '000-NAN'"), SV("answer is '{:0>7E}'"), nan_neg);
1536   check(SV("answer is '-NAN000'"), SV("answer is '{:0<7E}'"), nan_neg);
1537   check(SV("answer is '0-NAN00'"), SV("answer is '{:0^7E}'"), nan_neg);
1538 
1539   // Test whether zero padding is ignored
1540   check(SV("answer is '   2.500000E-01'"), SV("answer is '{:>015E}'"), F(0.25));
1541   check(SV("answer is '2.500000E-01   '"), SV("answer is '{:<015E}'"), F(0.25));
1542   check(SV("answer is ' 2.500000E-01  '"), SV("answer is '{:^015E}'"), F(0.25));
1543 
1544   // *** Sign ***
1545   check(SV("answer is '0.000000E+00'"), SV("answer is '{:E}'"), F(0));
1546   check(SV("answer is '0.000000E+00'"), SV("answer is '{:-E}'"), F(0));
1547   check(SV("answer is '+0.000000E+00'"), SV("answer is '{:+E}'"), F(0));
1548   check(SV("answer is ' 0.000000E+00'"), SV("answer is '{: E}'"), F(0));
1549 
1550   check(SV("answer is '-0.000000E+00'"), SV("answer is '{:E}'"), F(-0.));
1551   check(SV("answer is '-0.000000E+00'"), SV("answer is '{:-E}'"), F(-0.));
1552   check(SV("answer is '-0.000000E+00'"), SV("answer is '{:+E}'"), F(-0.));
1553   check(SV("answer is '-0.000000E+00'"), SV("answer is '{: E}'"), F(-0.));
1554 
1555   // [format.string.std]/5 The sign option applies to floating-point infinity and NaN.
1556   check(SV("answer is 'INF'"), SV("answer is '{:E}'"), std::numeric_limits<F>::infinity());
1557   check(SV("answer is 'INF'"), SV("answer is '{:-E}'"), std::numeric_limits<F>::infinity());
1558   check(SV("answer is '+INF'"), SV("answer is '{:+E}'"), std::numeric_limits<F>::infinity());
1559   check(SV("answer is ' INF'"), SV("answer is '{: E}'"), std::numeric_limits<F>::infinity());
1560 
1561   check(SV("answer is '-INF'"), SV("answer is '{:E}'"), -std::numeric_limits<F>::infinity());
1562   check(SV("answer is '-INF'"), SV("answer is '{:-E}'"), -std::numeric_limits<F>::infinity());
1563   check(SV("answer is '-INF'"), SV("answer is '{:+E}'"), -std::numeric_limits<F>::infinity());
1564   check(SV("answer is '-INF'"), SV("answer is '{: E}'"), -std::numeric_limits<F>::infinity());
1565 
1566   check(SV("answer is 'NAN'"), SV("answer is '{:E}'"), nan_pos);
1567   check(SV("answer is 'NAN'"), SV("answer is '{:-E}'"), nan_pos);
1568   check(SV("answer is '+NAN'"), SV("answer is '{:+E}'"), nan_pos);
1569   check(SV("answer is ' NAN'"), SV("answer is '{: E}'"), nan_pos);
1570 
1571   check(SV("answer is '-NAN'"), SV("answer is '{:E}'"), nan_neg);
1572   check(SV("answer is '-NAN'"), SV("answer is '{:-E}'"), nan_neg);
1573   check(SV("answer is '-NAN'"), SV("answer is '{:+E}'"), nan_neg);
1574   check(SV("answer is '-NAN'"), SV("answer is '{: E}'"), nan_neg);
1575 
1576   // *** alternate form **
1577   // When precision is zero there's no decimal point except when the alternate form is specified.
1578   check(SV("answer is '0E+00'"), SV("answer is '{:.0E}'"), F(0));
1579   check(SV("answer is '0.E+00'"), SV("answer is '{:#.0E}'"), F(0));
1580 
1581   check(SV("answer is '0.000000E+00'"), SV("answer is '{:#E}'"), F(0));
1582   check(SV("answer is '2.500000E+00'"), SV("answer is '{:#E}'"), F(2.5));
1583 
1584   check(SV("answer is 'INF'"), SV("answer is '{:#E}'"), std::numeric_limits<F>::infinity());
1585   check(SV("answer is '-INF'"), SV("answer is '{:#E}'"), -std::numeric_limits<F>::infinity());
1586 
1587   check(SV("answer is 'NAN'"), SV("answer is '{:#E}'"), nan_pos);
1588   check(SV("answer is '-NAN'"), SV("answer is '{:#E}'"), nan_neg);
1589 
1590   // *** zero-padding & width ***
1591   check(SV("answer is '3.125000E-02'"), SV("answer is '{:07E}'"), 0.03125);
1592   check(SV("answer is '+3.125000E-02'"), SV("answer is '{:+07E}'"), 0.03125);
1593   check(SV("answer is '+3.125000E-02'"), SV("answer is '{:+08E}'"), 0.03125);
1594   check(SV("answer is '+3.125000E-02'"), SV("answer is '{:+09E}'"), 0.03125);
1595 
1596   check(SV("answer is '003.125000E-02'"), SV("answer is '{:014E}'"), 0.03125);
1597   check(SV("answer is '003.125000E-02'"), SV("answer is '{:-014E}'"), 0.03125);
1598   check(SV("answer is '+03.125000E-02'"), SV("answer is '{:+014E}'"), 0.03125);
1599   check(SV("answer is ' 03.125000E-02'"), SV("answer is '{: 014E}'"), 0.03125);
1600 
1601   check(SV("answer is '       INF'"), SV("answer is '{:010E}'"), std::numeric_limits<F>::infinity());
1602   check(SV("answer is '       INF'"), SV("answer is '{:-010E}'"), std::numeric_limits<F>::infinity());
1603   check(SV("answer is '      +INF'"), SV("answer is '{:+010E}'"), std::numeric_limits<F>::infinity());
1604   check(SV("answer is '       INF'"), SV("answer is '{: 010E}'"), std::numeric_limits<F>::infinity());
1605 
1606   check(SV("answer is '      -INF'"), SV("answer is '{:010E}'"), -std::numeric_limits<F>::infinity());
1607   check(SV("answer is '      -INF'"), SV("answer is '{:-010E}'"), -std::numeric_limits<F>::infinity());
1608   check(SV("answer is '      -INF'"), SV("answer is '{:+010E}'"), -std::numeric_limits<F>::infinity());
1609   check(SV("answer is '      -INF'"), SV("answer is '{: 010E}'"), -std::numeric_limits<F>::infinity());
1610 
1611   check(SV("answer is '       NAN'"), SV("answer is '{:010E}'"), nan_pos);
1612   check(SV("answer is '       NAN'"), SV("answer is '{:-010E}'"), nan_pos);
1613   check(SV("answer is '      +NAN'"), SV("answer is '{:+010E}'"), nan_pos);
1614   check(SV("answer is '       NAN'"), SV("answer is '{: 010E}'"), nan_pos);
1615 
1616   check(SV("answer is '      -NAN'"), SV("answer is '{:010E}'"), nan_neg);
1617   check(SV("answer is '      -NAN'"), SV("answer is '{:-010E}'"), nan_neg);
1618   check(SV("answer is '      -NAN'"), SV("answer is '{:+010E}'"), nan_neg);
1619   check(SV("answer is '      -NAN'"), SV("answer is '{: 010E}'"), nan_neg);
1620 
1621   // *** precision ***
1622   check(SV("answer is '3E-02'"), SV("answer is '{:.0E}'"), 0.03125);
1623   check(SV("answer is '3.1E-02'"), SV("answer is '{:.1E}'"), 0.03125);
1624   check(SV("answer is '3.125E-02'"), SV("answer is '{:.3E}'"), 0.03125);
1625   check(SV("answer is '3.1250000000E-02'"), SV("answer is '{:.10E}'"), 0.03125);
1626 
1627   // *** locale-specific form ***
1628   // See locale-specific_form.pass.cpp
1629 }
1630 
1631 template <class F, class CharT, class TestFunction>
format_test_floating_point_fixed_lower_case(TestFunction check)1632 void format_test_floating_point_fixed_lower_case(TestFunction check) {
1633   auto nan_pos = std::numeric_limits<F>::quiet_NaN();                          // "nan"
1634   auto nan_neg = std::copysign(nan_pos, static_cast<decltype(nan_pos)>(-1.0)); // "-nan"
1635 
1636   // *** align-fill & width ***
1637   check(SV("answer is '   0.250000'"), SV("answer is '{:11f}'"), F(0.25));
1638   check(SV("answer is '   0.250000'"), SV("answer is '{:>11f}'"), F(0.25));
1639   check(SV("answer is '0.250000   '"), SV("answer is '{:<11f}'"), F(0.25));
1640   check(SV("answer is ' 0.250000  '"), SV("answer is '{:^11f}'"), F(0.25));
1641 
1642   check(SV("answer is '---0.125000'"), SV("answer is '{:->11f}'"), F(125e-3));
1643   check(SV("answer is '0.125000---'"), SV("answer is '{:-<11f}'"), F(125e-3));
1644   check(SV("answer is '-0.125000--'"), SV("answer is '{:-^11f}'"), F(125e-3));
1645 
1646   check(SV("answer is '***inf'"), SV("answer is '{:*>6f}'"), std::numeric_limits<F>::infinity());
1647   check(SV("answer is 'inf***'"), SV("answer is '{:*<6f}'"), std::numeric_limits<F>::infinity());
1648   check(SV("answer is '*inf**'"), SV("answer is '{:*^6f}'"), std::numeric_limits<F>::infinity());
1649 
1650   check(SV("answer is '###-inf'"), SV("answer is '{:#>7f}'"), -std::numeric_limits<F>::infinity());
1651   check(SV("answer is '-inf###'"), SV("answer is '{:#<7f}'"), -std::numeric_limits<F>::infinity());
1652   check(SV("answer is '#-inf##'"), SV("answer is '{:#^7f}'"), -std::numeric_limits<F>::infinity());
1653 
1654   check(SV("answer is '^^^nan'"), SV("answer is '{:^>6f}'"), nan_pos);
1655   check(SV("answer is 'nan^^^'"), SV("answer is '{:^<6f}'"), nan_pos);
1656   check(SV("answer is '^nan^^'"), SV("answer is '{:^^6f}'"), nan_pos);
1657 
1658   check(SV("answer is '000-nan'"), SV("answer is '{:0>7f}'"), nan_neg);
1659   check(SV("answer is '-nan000'"), SV("answer is '{:0<7f}'"), nan_neg);
1660   check(SV("answer is '0-nan00'"), SV("answer is '{:0^7f}'"), nan_neg);
1661 
1662   // Test whether zero padding is ignored
1663   check(SV("answer is '   0.250000'"), SV("answer is '{:>011f}'"), F(0.25));
1664   check(SV("answer is '0.250000   '"), SV("answer is '{:<011f}'"), F(0.25));
1665   check(SV("answer is ' 0.250000  '"), SV("answer is '{:^011f}'"), F(0.25));
1666 
1667   // *** Sign ***
1668   check(SV("answer is '0.000000'"), SV("answer is '{:f}'"), F(0));
1669   check(SV("answer is '0.000000'"), SV("answer is '{:-f}'"), F(0));
1670   check(SV("answer is '+0.000000'"), SV("answer is '{:+f}'"), F(0));
1671   check(SV("answer is ' 0.000000'"), SV("answer is '{: f}'"), F(0));
1672 
1673   check(SV("answer is '-0.000000'"), SV("answer is '{:f}'"), F(-0.));
1674   check(SV("answer is '-0.000000'"), SV("answer is '{:-f}'"), F(-0.));
1675   check(SV("answer is '-0.000000'"), SV("answer is '{:+f}'"), F(-0.));
1676   check(SV("answer is '-0.000000'"), SV("answer is '{: f}'"), F(-0.));
1677 
1678   // [format.string.std]/5 The sign option applies to floating-point infinity and NaN.
1679   check(SV("answer is 'inf'"), SV("answer is '{:f}'"), std::numeric_limits<F>::infinity());
1680   check(SV("answer is 'inf'"), SV("answer is '{:-f}'"), std::numeric_limits<F>::infinity());
1681   check(SV("answer is '+inf'"), SV("answer is '{:+f}'"), std::numeric_limits<F>::infinity());
1682   check(SV("answer is ' inf'"), SV("answer is '{: f}'"), std::numeric_limits<F>::infinity());
1683 
1684   check(SV("answer is '-inf'"), SV("answer is '{:f}'"), -std::numeric_limits<F>::infinity());
1685   check(SV("answer is '-inf'"), SV("answer is '{:-f}'"), -std::numeric_limits<F>::infinity());
1686   check(SV("answer is '-inf'"), SV("answer is '{:+f}'"), -std::numeric_limits<F>::infinity());
1687   check(SV("answer is '-inf'"), SV("answer is '{: f}'"), -std::numeric_limits<F>::infinity());
1688 
1689   check(SV("answer is 'nan'"), SV("answer is '{:f}'"), nan_pos);
1690   check(SV("answer is 'nan'"), SV("answer is '{:-f}'"), nan_pos);
1691   check(SV("answer is '+nan'"), SV("answer is '{:+f}'"), nan_pos);
1692   check(SV("answer is ' nan'"), SV("answer is '{: f}'"), nan_pos);
1693 
1694   check(SV("answer is '-nan'"), SV("answer is '{:f}'"), nan_neg);
1695   check(SV("answer is '-nan'"), SV("answer is '{:-f}'"), nan_neg);
1696   check(SV("answer is '-nan'"), SV("answer is '{:+f}'"), nan_neg);
1697   check(SV("answer is '-nan'"), SV("answer is '{: f}'"), nan_neg);
1698 
1699   // *** alternate form **
1700   // When precision is zero there's no decimal point except when the alternate form is specified.
1701   check(SV("answer is '0'"), SV("answer is '{:.0f}'"), F(0));
1702   check(SV("answer is '0.'"), SV("answer is '{:#.0f}'"), F(0));
1703 
1704   check(SV("answer is '0.000000'"), SV("answer is '{:#f}'"), F(0));
1705   check(SV("answer is '2.500000'"), SV("answer is '{:#f}'"), F(2.5));
1706 
1707   check(SV("answer is 'inf'"), SV("answer is '{:#f}'"), std::numeric_limits<F>::infinity());
1708   check(SV("answer is '-inf'"), SV("answer is '{:#f}'"), -std::numeric_limits<F>::infinity());
1709 
1710   check(SV("answer is 'nan'"), SV("answer is '{:#f}'"), nan_pos);
1711   check(SV("answer is '-nan'"), SV("answer is '{:#f}'"), nan_neg);
1712 
1713   // *** zero-padding & width ***
1714   check(SV("answer is '0.031250'"), SV("answer is '{:07f}'"), 0.03125);
1715   check(SV("answer is '+0.031250'"), SV("answer is '{:+07f}'"), 0.03125);
1716   check(SV("answer is '+0.031250'"), SV("answer is '{:+08f}'"), 0.03125);
1717   check(SV("answer is '+0.031250'"), SV("answer is '{:+09f}'"), 0.03125);
1718 
1719   check(SV("answer is '000.031250'"), SV("answer is '{:010f}'"), 0.03125);
1720   check(SV("answer is '000.031250'"), SV("answer is '{:-010f}'"), 0.03125);
1721   check(SV("answer is '+00.031250'"), SV("answer is '{:+010f}'"), 0.03125);
1722   check(SV("answer is ' 00.031250'"), SV("answer is '{: 010f}'"), 0.03125);
1723 
1724   check(SV("answer is '       inf'"), SV("answer is '{:010f}'"), std::numeric_limits<F>::infinity());
1725   check(SV("answer is '       inf'"), SV("answer is '{:-010f}'"), std::numeric_limits<F>::infinity());
1726   check(SV("answer is '      +inf'"), SV("answer is '{:+010f}'"), std::numeric_limits<F>::infinity());
1727   check(SV("answer is '       inf'"), SV("answer is '{: 010f}'"), std::numeric_limits<F>::infinity());
1728 
1729   check(SV("answer is '      -inf'"), SV("answer is '{:010f}'"), -std::numeric_limits<F>::infinity());
1730   check(SV("answer is '      -inf'"), SV("answer is '{:-010f}'"), -std::numeric_limits<F>::infinity());
1731   check(SV("answer is '      -inf'"), SV("answer is '{:+010f}'"), -std::numeric_limits<F>::infinity());
1732   check(SV("answer is '      -inf'"), SV("answer is '{: 010f}'"), -std::numeric_limits<F>::infinity());
1733 
1734   check(SV("answer is '       nan'"), SV("answer is '{:010f}'"), nan_pos);
1735   check(SV("answer is '       nan'"), SV("answer is '{:-010f}'"), nan_pos);
1736   check(SV("answer is '      +nan'"), SV("answer is '{:+010f}'"), nan_pos);
1737   check(SV("answer is '       nan'"), SV("answer is '{: 010f}'"), nan_pos);
1738 
1739   check(SV("answer is '      -nan'"), SV("answer is '{:010f}'"), nan_neg);
1740   check(SV("answer is '      -nan'"), SV("answer is '{:-010f}'"), nan_neg);
1741   check(SV("answer is '      -nan'"), SV("answer is '{:+010f}'"), nan_neg);
1742   check(SV("answer is '      -nan'"), SV("answer is '{: 010f}'"), nan_neg);
1743 
1744   // *** precision ***
1745   check(SV("answer is '0'"), SV("answer is '{:.0f}'"), 0.03125);
1746   check(SV("answer is '0.0'"), SV("answer is '{:.1f}'"), 0.03125);
1747   check(SV("answer is '0.03125'"), SV("answer is '{:.5f}'"), 0.03125);
1748   check(SV("answer is '0.0312500000'"), SV("answer is '{:.10f}'"), 0.03125);
1749 
1750   // *** locale-specific form ***
1751   // See locale-specific_form.pass.cpp
1752 }
1753 
1754 template <class F, class CharT, class TestFunction>
format_test_floating_point_fixed_upper_case(TestFunction check)1755 void format_test_floating_point_fixed_upper_case(TestFunction check) {
1756   auto nan_pos = std::numeric_limits<F>::quiet_NaN();                          // "nan"
1757   auto nan_neg = std::copysign(nan_pos, static_cast<decltype(nan_pos)>(-1.0)); // "-nan"
1758 
1759   // *** align-fill & width ***
1760   check(SV("answer is '   0.250000'"), SV("answer is '{:11F}'"), F(0.25));
1761   check(SV("answer is '   0.250000'"), SV("answer is '{:>11F}'"), F(0.25));
1762   check(SV("answer is '0.250000   '"), SV("answer is '{:<11F}'"), F(0.25));
1763   check(SV("answer is ' 0.250000  '"), SV("answer is '{:^11F}'"), F(0.25));
1764 
1765   check(SV("answer is '---0.125000'"), SV("answer is '{:->11F}'"), F(125e-3));
1766   check(SV("answer is '0.125000---'"), SV("answer is '{:-<11F}'"), F(125e-3));
1767   check(SV("answer is '-0.125000--'"), SV("answer is '{:-^11F}'"), F(125e-3));
1768 
1769   check(SV("answer is '***INF'"), SV("answer is '{:*>6F}'"), std::numeric_limits<F>::infinity());
1770   check(SV("answer is 'INF***'"), SV("answer is '{:*<6F}'"), std::numeric_limits<F>::infinity());
1771   check(SV("answer is '*INF**'"), SV("answer is '{:*^6F}'"), std::numeric_limits<F>::infinity());
1772 
1773   check(SV("answer is '###-INF'"), SV("answer is '{:#>7F}'"), -std::numeric_limits<F>::infinity());
1774   check(SV("answer is '-INF###'"), SV("answer is '{:#<7F}'"), -std::numeric_limits<F>::infinity());
1775   check(SV("answer is '#-INF##'"), SV("answer is '{:#^7F}'"), -std::numeric_limits<F>::infinity());
1776 
1777   check(SV("answer is '^^^NAN'"), SV("answer is '{:^>6F}'"), nan_pos);
1778   check(SV("answer is 'NAN^^^'"), SV("answer is '{:^<6F}'"), nan_pos);
1779   check(SV("answer is '^NAN^^'"), SV("answer is '{:^^6F}'"), nan_pos);
1780 
1781   check(SV("answer is '000-NAN'"), SV("answer is '{:0>7F}'"), nan_neg);
1782   check(SV("answer is '-NAN000'"), SV("answer is '{:0<7F}'"), nan_neg);
1783   check(SV("answer is '0-NAN00'"), SV("answer is '{:0^7F}'"), nan_neg);
1784 
1785   // Test whether zero padding is ignored
1786   check(SV("answer is '   0.250000'"), SV("answer is '{:>011F}'"), F(0.25));
1787   check(SV("answer is '0.250000   '"), SV("answer is '{:<011F}'"), F(0.25));
1788   check(SV("answer is ' 0.250000  '"), SV("answer is '{:^011F}'"), F(0.25));
1789 
1790   // *** Sign ***
1791   check(SV("answer is '0.000000'"), SV("answer is '{:F}'"), F(0));
1792   check(SV("answer is '0.000000'"), SV("answer is '{:-F}'"), F(0));
1793   check(SV("answer is '+0.000000'"), SV("answer is '{:+F}'"), F(0));
1794   check(SV("answer is ' 0.000000'"), SV("answer is '{: F}'"), F(0));
1795 
1796   check(SV("answer is '-0.000000'"), SV("answer is '{:F}'"), F(-0.));
1797   check(SV("answer is '-0.000000'"), SV("answer is '{:-F}'"), F(-0.));
1798   check(SV("answer is '-0.000000'"), SV("answer is '{:+F}'"), F(-0.));
1799   check(SV("answer is '-0.000000'"), SV("answer is '{: F}'"), F(-0.));
1800 
1801   // [format.string.std]/5 The sign option applies to floating-point infinity and NaN.
1802   check(SV("answer is 'INF'"), SV("answer is '{:F}'"), std::numeric_limits<F>::infinity());
1803   check(SV("answer is 'INF'"), SV("answer is '{:-F}'"), std::numeric_limits<F>::infinity());
1804   check(SV("answer is '+INF'"), SV("answer is '{:+F}'"), std::numeric_limits<F>::infinity());
1805   check(SV("answer is ' INF'"), SV("answer is '{: F}'"), std::numeric_limits<F>::infinity());
1806 
1807   check(SV("answer is '-INF'"), SV("answer is '{:F}'"), -std::numeric_limits<F>::infinity());
1808   check(SV("answer is '-INF'"), SV("answer is '{:-F}'"), -std::numeric_limits<F>::infinity());
1809   check(SV("answer is '-INF'"), SV("answer is '{:+F}'"), -std::numeric_limits<F>::infinity());
1810   check(SV("answer is '-INF'"), SV("answer is '{: F}'"), -std::numeric_limits<F>::infinity());
1811 
1812   check(SV("answer is 'NAN'"), SV("answer is '{:F}'"), nan_pos);
1813   check(SV("answer is 'NAN'"), SV("answer is '{:-F}'"), nan_pos);
1814   check(SV("answer is '+NAN'"), SV("answer is '{:+F}'"), nan_pos);
1815   check(SV("answer is ' NAN'"), SV("answer is '{: F}'"), nan_pos);
1816 
1817   check(SV("answer is '-NAN'"), SV("answer is '{:F}'"), nan_neg);
1818   check(SV("answer is '-NAN'"), SV("answer is '{:-F}'"), nan_neg);
1819   check(SV("answer is '-NAN'"), SV("answer is '{:+F}'"), nan_neg);
1820   check(SV("answer is '-NAN'"), SV("answer is '{: F}'"), nan_neg);
1821 
1822   // *** alternate form **
1823   // When precision is zero there's no decimal point except when the alternate form is specified.
1824   check(SV("answer is '0'"), SV("answer is '{:.0F}'"), F(0));
1825   check(SV("answer is '0.'"), SV("answer is '{:#.0F}'"), F(0));
1826 
1827   check(SV("answer is '0.000000'"), SV("answer is '{:#F}'"), F(0));
1828   check(SV("answer is '2.500000'"), SV("answer is '{:#F}'"), F(2.5));
1829 
1830   check(SV("answer is 'INF'"), SV("answer is '{:#F}'"), std::numeric_limits<F>::infinity());
1831   check(SV("answer is '-INF'"), SV("answer is '{:#F}'"), -std::numeric_limits<F>::infinity());
1832 
1833   check(SV("answer is 'NAN'"), SV("answer is '{:#F}'"), nan_pos);
1834   check(SV("answer is '-NAN'"), SV("answer is '{:#F}'"), nan_neg);
1835 
1836   // *** zero-padding & width ***
1837   check(SV("answer is '0.031250'"), SV("answer is '{:07F}'"), 0.03125);
1838   check(SV("answer is '+0.031250'"), SV("answer is '{:+07F}'"), 0.03125);
1839   check(SV("answer is '+0.031250'"), SV("answer is '{:+08F}'"), 0.03125);
1840   check(SV("answer is '+0.031250'"), SV("answer is '{:+09F}'"), 0.03125);
1841 
1842   check(SV("answer is '000.031250'"), SV("answer is '{:010F}'"), 0.03125);
1843   check(SV("answer is '000.031250'"), SV("answer is '{:-010F}'"), 0.03125);
1844   check(SV("answer is '+00.031250'"), SV("answer is '{:+010F}'"), 0.03125);
1845   check(SV("answer is ' 00.031250'"), SV("answer is '{: 010F}'"), 0.03125);
1846 
1847   check(SV("answer is '       INF'"), SV("answer is '{:010F}'"), std::numeric_limits<F>::infinity());
1848   check(SV("answer is '       INF'"), SV("answer is '{:-010F}'"), std::numeric_limits<F>::infinity());
1849   check(SV("answer is '      +INF'"), SV("answer is '{:+010F}'"), std::numeric_limits<F>::infinity());
1850   check(SV("answer is '       INF'"), SV("answer is '{: 010F}'"), std::numeric_limits<F>::infinity());
1851 
1852   check(SV("answer is '      -INF'"), SV("answer is '{:010F}'"), -std::numeric_limits<F>::infinity());
1853   check(SV("answer is '      -INF'"), SV("answer is '{:-010F}'"), -std::numeric_limits<F>::infinity());
1854   check(SV("answer is '      -INF'"), SV("answer is '{:+010F}'"), -std::numeric_limits<F>::infinity());
1855   check(SV("answer is '      -INF'"), SV("answer is '{: 010F}'"), -std::numeric_limits<F>::infinity());
1856 
1857   check(SV("answer is '       NAN'"), SV("answer is '{:010F}'"), nan_pos);
1858   check(SV("answer is '       NAN'"), SV("answer is '{:-010F}'"), nan_pos);
1859   check(SV("answer is '      +NAN'"), SV("answer is '{:+010F}'"), nan_pos);
1860   check(SV("answer is '       NAN'"), SV("answer is '{: 010F}'"), nan_pos);
1861 
1862   check(SV("answer is '      -NAN'"), SV("answer is '{:010F}'"), nan_neg);
1863   check(SV("answer is '      -NAN'"), SV("answer is '{:-010F}'"), nan_neg);
1864   check(SV("answer is '      -NAN'"), SV("answer is '{:+010F}'"), nan_neg);
1865   check(SV("answer is '      -NAN'"), SV("answer is '{: 010F}'"), nan_neg);
1866 
1867   // *** precision ***
1868   check(SV("answer is '0'"), SV("answer is '{:.0F}'"), 0.03125);
1869   check(SV("answer is '0.0'"), SV("answer is '{:.1F}'"), 0.03125);
1870   check(SV("answer is '0.03125'"), SV("answer is '{:.5F}'"), 0.03125);
1871   check(SV("answer is '0.0312500000'"), SV("answer is '{:.10F}'"), 0.03125);
1872 
1873   // *** locale-specific form ***
1874   // See locale-specific_form.pass.cpp
1875 }
1876 
1877 template <class F, class CharT, class TestFunction>
format_test_floating_point_general_lower_case(TestFunction check)1878 void format_test_floating_point_general_lower_case(TestFunction check) {
1879   auto nan_pos = std::numeric_limits<F>::quiet_NaN();                          // "nan"
1880   auto nan_neg = std::copysign(nan_pos, static_cast<decltype(nan_pos)>(-1.0)); // "-nan"
1881 
1882   // *** align-fill & width ***
1883   check(SV("answer is '   0.25'"), SV("answer is '{:7g}'"), F(0.25));
1884   check(SV("answer is '   0.25'"), SV("answer is '{:>7g}'"), F(0.25));
1885   check(SV("answer is '0.25   '"), SV("answer is '{:<7g}'"), F(0.25));
1886   check(SV("answer is ' 0.25  '"), SV("answer is '{:^7g}'"), F(0.25));
1887 
1888   check(SV("answer is '---0.125'"), SV("answer is '{:->8g}'"), F(125e-3));
1889   check(SV("answer is '0.125---'"), SV("answer is '{:-<8g}'"), F(125e-3));
1890   check(SV("answer is '-0.125--'"), SV("answer is '{:-^8g}'"), F(125e-3));
1891 
1892   check(SV("answer is '***inf'"), SV("answer is '{:*>6g}'"), std::numeric_limits<F>::infinity());
1893   check(SV("answer is 'inf***'"), SV("answer is '{:*<6g}'"), std::numeric_limits<F>::infinity());
1894   check(SV("answer is '*inf**'"), SV("answer is '{:*^6g}'"), std::numeric_limits<F>::infinity());
1895 
1896   check(SV("answer is '###-inf'"), SV("answer is '{:#>7g}'"), -std::numeric_limits<F>::infinity());
1897   check(SV("answer is '-inf###'"), SV("answer is '{:#<7g}'"), -std::numeric_limits<F>::infinity());
1898   check(SV("answer is '#-inf##'"), SV("answer is '{:#^7g}'"), -std::numeric_limits<F>::infinity());
1899 
1900   check(SV("answer is '^^^nan'"), SV("answer is '{:^>6g}'"), nan_pos);
1901   check(SV("answer is 'nan^^^'"), SV("answer is '{:^<6g}'"), nan_pos);
1902   check(SV("answer is '^nan^^'"), SV("answer is '{:^^6g}'"), nan_pos);
1903 
1904   check(SV("answer is '000-nan'"), SV("answer is '{:0>7g}'"), nan_neg);
1905   check(SV("answer is '-nan000'"), SV("answer is '{:0<7g}'"), nan_neg);
1906   check(SV("answer is '0-nan00'"), SV("answer is '{:0^7g}'"), nan_neg);
1907 
1908   // Test whether zero padding is ignored
1909   check(SV("answer is '   0.25'"), SV("answer is '{:>07g}'"), F(0.25));
1910   check(SV("answer is '0.25   '"), SV("answer is '{:<07g}'"), F(0.25));
1911   check(SV("answer is ' 0.25  '"), SV("answer is '{:^07g}'"), F(0.25));
1912 
1913   // *** Sign ***
1914   check(SV("answer is '0'"), SV("answer is '{:g}'"), F(0));
1915   check(SV("answer is '0'"), SV("answer is '{:-g}'"), F(0));
1916   check(SV("answer is '+0'"), SV("answer is '{:+g}'"), F(0));
1917   check(SV("answer is ' 0'"), SV("answer is '{: g}'"), F(0));
1918 
1919   check(SV("answer is '-0'"), SV("answer is '{:g}'"), F(-0.));
1920   check(SV("answer is '-0'"), SV("answer is '{:-g}'"), F(-0.));
1921   check(SV("answer is '-0'"), SV("answer is '{:+g}'"), F(-0.));
1922   check(SV("answer is '-0'"), SV("answer is '{: g}'"), F(-0.));
1923 
1924   // [format.string.std]/5 The sign option applies to floating-point infinity and NaN.
1925   check(SV("answer is 'inf'"), SV("answer is '{:g}'"), std::numeric_limits<F>::infinity());
1926   check(SV("answer is 'inf'"), SV("answer is '{:-g}'"), std::numeric_limits<F>::infinity());
1927   check(SV("answer is '+inf'"), SV("answer is '{:+g}'"), std::numeric_limits<F>::infinity());
1928   check(SV("answer is ' inf'"), SV("answer is '{: g}'"), std::numeric_limits<F>::infinity());
1929 
1930   check(SV("answer is '-inf'"), SV("answer is '{:g}'"), -std::numeric_limits<F>::infinity());
1931   check(SV("answer is '-inf'"), SV("answer is '{:-g}'"), -std::numeric_limits<F>::infinity());
1932   check(SV("answer is '-inf'"), SV("answer is '{:+g}'"), -std::numeric_limits<F>::infinity());
1933   check(SV("answer is '-inf'"), SV("answer is '{: g}'"), -std::numeric_limits<F>::infinity());
1934 
1935   check(SV("answer is 'nan'"), SV("answer is '{:g}'"), nan_pos);
1936   check(SV("answer is 'nan'"), SV("answer is '{:-g}'"), nan_pos);
1937   check(SV("answer is '+nan'"), SV("answer is '{:+g}'"), nan_pos);
1938   check(SV("answer is ' nan'"), SV("answer is '{: g}'"), nan_pos);
1939 
1940   check(SV("answer is '-nan'"), SV("answer is '{:g}'"), nan_neg);
1941   check(SV("answer is '-nan'"), SV("answer is '{:-g}'"), nan_neg);
1942   check(SV("answer is '-nan'"), SV("answer is '{:+g}'"), nan_neg);
1943   check(SV("answer is '-nan'"), SV("answer is '{: g}'"), nan_neg);
1944 
1945   // *** alternate form **
1946   // When precision is zero there's no decimal point except when the alternate form is specified.
1947   check(SV("answer is '0'"), SV("answer is '{:.0g}'"), F(0));
1948   check(SV("answer is '0.'"), SV("answer is '{:#.0g}'"), F(0));
1949 
1950   check(SV("answer is '0.00000'"), SV("answer is '{:#g}'"), F(0));
1951   check(SV("answer is '2.50000'"), SV("answer is '{:#g}'"), F(2.5));
1952 
1953   check(SV("answer is 'inf'"), SV("answer is '{:#g}'"), std::numeric_limits<F>::infinity());
1954   check(SV("answer is '-inf'"), SV("answer is '{:#g}'"), -std::numeric_limits<F>::infinity());
1955 
1956   check(SV("answer is 'nan'"), SV("answer is '{:#g}'"), nan_pos);
1957   check(SV("answer is '-nan'"), SV("answer is '{:#g}'"), nan_neg);
1958 
1959   // *** zero-padding & width ***
1960   check(SV("answer is '0.03125'"), SV("answer is '{:06g}'"), 0.03125);
1961   check(SV("answer is '+0.03125'"), SV("answer is '{:+06g}'"), 0.03125);
1962   check(SV("answer is '+0.03125'"), SV("answer is '{:+07g}'"), 0.03125);
1963   check(SV("answer is '+0.03125'"), SV("answer is '{:+08g}'"), 0.03125);
1964 
1965   check(SV("answer is '000.03125'"), SV("answer is '{:09g}'"), 0.03125);
1966   check(SV("answer is '000.03125'"), SV("answer is '{:-09g}'"), 0.03125);
1967   check(SV("answer is '+00.03125'"), SV("answer is '{:+09g}'"), 0.03125);
1968   check(SV("answer is ' 00.03125'"), SV("answer is '{: 09g}'"), 0.03125);
1969 
1970   check(SV("answer is '       inf'"), SV("answer is '{:010g}'"), std::numeric_limits<F>::infinity());
1971   check(SV("answer is '       inf'"), SV("answer is '{:-010g}'"), std::numeric_limits<F>::infinity());
1972   check(SV("answer is '      +inf'"), SV("answer is '{:+010g}'"), std::numeric_limits<F>::infinity());
1973   check(SV("answer is '       inf'"), SV("answer is '{: 010g}'"), std::numeric_limits<F>::infinity());
1974 
1975   check(SV("answer is '      -inf'"), SV("answer is '{:010g}'"), -std::numeric_limits<F>::infinity());
1976   check(SV("answer is '      -inf'"), SV("answer is '{:-010g}'"), -std::numeric_limits<F>::infinity());
1977   check(SV("answer is '      -inf'"), SV("answer is '{:+010g}'"), -std::numeric_limits<F>::infinity());
1978   check(SV("answer is '      -inf'"), SV("answer is '{: 010g}'"), -std::numeric_limits<F>::infinity());
1979 
1980   check(SV("answer is '       nan'"), SV("answer is '{:010g}'"), nan_pos);
1981   check(SV("answer is '       nan'"), SV("answer is '{:-010g}'"), nan_pos);
1982   check(SV("answer is '      +nan'"), SV("answer is '{:+010g}'"), nan_pos);
1983   check(SV("answer is '       nan'"), SV("answer is '{: 010g}'"), nan_pos);
1984 
1985   check(SV("answer is '      -nan'"), SV("answer is '{:010g}'"), nan_neg);
1986   check(SV("answer is '      -nan'"), SV("answer is '{:-010g}'"), nan_neg);
1987   check(SV("answer is '      -nan'"), SV("answer is '{:+010g}'"), nan_neg);
1988   check(SV("answer is '      -nan'"), SV("answer is '{: 010g}'"), nan_neg);
1989 
1990   // *** precision ***
1991   check(SV("answer is '0.03'"), SV("answer is '{:.0g}'"), 0.03125);
1992   check(SV("answer is '0.03'"), SV("answer is '{:.1g}'"), 0.03125);
1993   check(SV("answer is '0.031'"), SV("answer is '{:.2g}'"), 0.03125);
1994   check(SV("answer is '0.0312'"), SV("answer is '{:.3g}'"), 0.03125);
1995   check(SV("answer is '0.03125'"), SV("answer is '{:.4g}'"), 0.03125);
1996   check(SV("answer is '0.03125'"), SV("answer is '{:.5g}'"), 0.03125);
1997   check(SV("answer is '0.03125'"), SV("answer is '{:.10g}'"), 0.03125);
1998 
1999   // *** precision & alternate form ***
2000 
2001   // Output validated with  printf("%#xg")
2002   check(SV("answer is '1.'"), SV("answer is '{:#.{}g}'"), 1.2, 0);
2003   check(SV("answer is '1.'"), SV("answer is '{:#.{}g}'"), 1.2, 1);
2004   check(SV("answer is '1.2'"), SV("answer is '{:#.{}g}'"), 1.2, 2);
2005   check(SV("answer is '1.20'"), SV("answer is '{:#.{}g}'"), 1.2, 3);
2006   check(SV("answer is '1.200'"), SV("answer is '{:#.{}g}'"), 1.2, 4);
2007   check(SV("answer is '1.2000'"), SV("answer is '{:#.{}g}'"), 1.2, 5);
2008   check(SV("answer is '1.20000'"), SV("answer is '{:#.{}g}'"), 1.2, 6);
2009 
2010   check(SV("answer is '1.e+03'"), SV("answer is '{:#.{}g}'"), 1200.0, 0);
2011   check(SV("answer is '1.e+03'"), SV("answer is '{:#.{}g}'"), 1200.0, 1);
2012   check(SV("answer is '1.2e+03'"), SV("answer is '{:#.{}g}'"), 1200.0, 2);
2013   check(SV("answer is '1.20e+03'"), SV("answer is '{:#.{}g}'"), 1200.0, 3);
2014   check(SV("answer is '1200.'"), SV("answer is '{:#.{}g}'"), 1200.0, 4);
2015   check(SV("answer is '1200.0'"), SV("answer is '{:#.{}g}'"), 1200.0, 5);
2016   check(SV("answer is '1200.00'"), SV("answer is '{:#.{}g}'"), 1200.0, 6);
2017 
2018   check(SV("answer is '1.e+06'"), SV("answer is '{:#.{}g}'"), 1200000.0, 0);
2019   check(SV("answer is '1.e+06'"), SV("answer is '{:#.{}g}'"), 1200000.0, 1);
2020   check(SV("answer is '1.2e+06'"), SV("answer is '{:#.{}g}'"), 1200000.0, 2);
2021   check(SV("answer is '1.20e+06'"), SV("answer is '{:#.{}g}'"), 1200000.0, 3);
2022   check(SV("answer is '1.200e+06'"), SV("answer is '{:#.{}g}'"), 1200000.0, 4);
2023   check(SV("answer is '1.2000e+06'"), SV("answer is '{:#.{}g}'"), 1200000.0, 5);
2024   check(SV("answer is '1.20000e+06'"), SV("answer is '{:#.{}g}'"), 1200000.0, 6);
2025 
2026   // *** locale-specific form ***
2027   // See locale-specific_form.pass.cpp
2028 }
2029 
2030 template <class F, class CharT, class TestFunction>
format_test_floating_point_general_upper_case(TestFunction check)2031 void format_test_floating_point_general_upper_case(TestFunction check) {
2032   auto nan_pos = std::numeric_limits<F>::quiet_NaN();                          // "nan"
2033   auto nan_neg = std::copysign(nan_pos, static_cast<decltype(nan_pos)>(-1.0)); // "-nan"
2034 
2035   // *** align-fill & width ***
2036   check(SV("answer is '   0.25'"), SV("answer is '{:7G}'"), F(0.25));
2037   check(SV("answer is '   0.25'"), SV("answer is '{:>7G}'"), F(0.25));
2038   check(SV("answer is '0.25   '"), SV("answer is '{:<7G}'"), F(0.25));
2039   check(SV("answer is ' 0.25  '"), SV("answer is '{:^7G}'"), F(0.25));
2040 
2041   check(SV("answer is '---0.125'"), SV("answer is '{:->8G}'"), F(125e-3));
2042   check(SV("answer is '0.125---'"), SV("answer is '{:-<8G}'"), F(125e-3));
2043   check(SV("answer is '-0.125--'"), SV("answer is '{:-^8G}'"), F(125e-3));
2044 
2045   check(SV("answer is '***INF'"), SV("answer is '{:*>6G}'"), std::numeric_limits<F>::infinity());
2046   check(SV("answer is 'INF***'"), SV("answer is '{:*<6G}'"), std::numeric_limits<F>::infinity());
2047   check(SV("answer is '*INF**'"), SV("answer is '{:*^6G}'"), std::numeric_limits<F>::infinity());
2048 
2049   check(SV("answer is '###-INF'"), SV("answer is '{:#>7G}'"), -std::numeric_limits<F>::infinity());
2050   check(SV("answer is '-INF###'"), SV("answer is '{:#<7G}'"), -std::numeric_limits<F>::infinity());
2051   check(SV("answer is '#-INF##'"), SV("answer is '{:#^7G}'"), -std::numeric_limits<F>::infinity());
2052 
2053   check(SV("answer is '^^^NAN'"), SV("answer is '{:^>6G}'"), nan_pos);
2054   check(SV("answer is 'NAN^^^'"), SV("answer is '{:^<6G}'"), nan_pos);
2055   check(SV("answer is '^NAN^^'"), SV("answer is '{:^^6G}'"), nan_pos);
2056 
2057   check(SV("answer is '000-NAN'"), SV("answer is '{:0>7G}'"), nan_neg);
2058   check(SV("answer is '-NAN000'"), SV("answer is '{:0<7G}'"), nan_neg);
2059   check(SV("answer is '0-NAN00'"), SV("answer is '{:0^7G}'"), nan_neg);
2060 
2061   // Test whether zero padding is ignored
2062   check(SV("answer is '   0.25'"), SV("answer is '{:>07G}'"), F(0.25));
2063   check(SV("answer is '0.25   '"), SV("answer is '{:<07G}'"), F(0.25));
2064   check(SV("answer is ' 0.25  '"), SV("answer is '{:^07G}'"), F(0.25));
2065 
2066   // *** Sign ***
2067   check(SV("answer is '0'"), SV("answer is '{:G}'"), F(0));
2068   check(SV("answer is '0'"), SV("answer is '{:-G}'"), F(0));
2069   check(SV("answer is '+0'"), SV("answer is '{:+G}'"), F(0));
2070   check(SV("answer is ' 0'"), SV("answer is '{: G}'"), F(0));
2071 
2072   check(SV("answer is '-0'"), SV("answer is '{:G}'"), F(-0.));
2073   check(SV("answer is '-0'"), SV("answer is '{:-G}'"), F(-0.));
2074   check(SV("answer is '-0'"), SV("answer is '{:+G}'"), F(-0.));
2075   check(SV("answer is '-0'"), SV("answer is '{: G}'"), F(-0.));
2076 
2077   // [format.string.std]/5 The sign option applies to floating-point infinity and NaN.
2078   check(SV("answer is 'INF'"), SV("answer is '{:G}'"), std::numeric_limits<F>::infinity());
2079   check(SV("answer is 'INF'"), SV("answer is '{:-G}'"), std::numeric_limits<F>::infinity());
2080   check(SV("answer is '+INF'"), SV("answer is '{:+G}'"), std::numeric_limits<F>::infinity());
2081   check(SV("answer is ' INF'"), SV("answer is '{: G}'"), std::numeric_limits<F>::infinity());
2082 
2083   check(SV("answer is '-INF'"), SV("answer is '{:G}'"), -std::numeric_limits<F>::infinity());
2084   check(SV("answer is '-INF'"), SV("answer is '{:-G}'"), -std::numeric_limits<F>::infinity());
2085   check(SV("answer is '-INF'"), SV("answer is '{:+G}'"), -std::numeric_limits<F>::infinity());
2086   check(SV("answer is '-INF'"), SV("answer is '{: G}'"), -std::numeric_limits<F>::infinity());
2087 
2088   check(SV("answer is 'NAN'"), SV("answer is '{:G}'"), nan_pos);
2089   check(SV("answer is 'NAN'"), SV("answer is '{:-G}'"), nan_pos);
2090   check(SV("answer is '+NAN'"), SV("answer is '{:+G}'"), nan_pos);
2091   check(SV("answer is ' NAN'"), SV("answer is '{: G}'"), nan_pos);
2092 
2093   check(SV("answer is '-NAN'"), SV("answer is '{:G}'"), nan_neg);
2094   check(SV("answer is '-NAN'"), SV("answer is '{:-G}'"), nan_neg);
2095   check(SV("answer is '-NAN'"), SV("answer is '{:+G}'"), nan_neg);
2096   check(SV("answer is '-NAN'"), SV("answer is '{: G}'"), nan_neg);
2097 
2098   // *** alternate form **
2099   // When precision is zero there's no decimal point except when the alternate form is specified.
2100   check(SV("answer is '0'"), SV("answer is '{:.0G}'"), F(0));
2101   check(SV("answer is '0.'"), SV("answer is '{:#.0G}'"), F(0));
2102 
2103   check(SV("answer is '0.00000'"), SV("answer is '{:#G}'"), F(0));
2104   check(SV("answer is '2.50000'"), SV("answer is '{:#G}'"), F(2.5));
2105 
2106   check(SV("answer is 'INF'"), SV("answer is '{:#G}'"), std::numeric_limits<F>::infinity());
2107   check(SV("answer is '-INF'"), SV("answer is '{:#G}'"), -std::numeric_limits<F>::infinity());
2108 
2109   check(SV("answer is 'NAN'"), SV("answer is '{:#G}'"), nan_pos);
2110   check(SV("answer is '-NAN'"), SV("answer is '{:#G}'"), nan_neg);
2111 
2112   // *** zero-padding & width ***
2113   check(SV("answer is '0.03125'"), SV("answer is '{:06G}'"), 0.03125);
2114   check(SV("answer is '+0.03125'"), SV("answer is '{:+06G}'"), 0.03125);
2115   check(SV("answer is '+0.03125'"), SV("answer is '{:+07G}'"), 0.03125);
2116   check(SV("answer is '+0.03125'"), SV("answer is '{:+08G}'"), 0.03125);
2117 
2118   check(SV("answer is '000.03125'"), SV("answer is '{:09G}'"), 0.03125);
2119   check(SV("answer is '000.03125'"), SV("answer is '{:-09G}'"), 0.03125);
2120   check(SV("answer is '+00.03125'"), SV("answer is '{:+09G}'"), 0.03125);
2121   check(SV("answer is ' 00.03125'"), SV("answer is '{: 09G}'"), 0.03125);
2122 
2123   check(SV("answer is '       INF'"), SV("answer is '{:010G}'"), std::numeric_limits<F>::infinity());
2124   check(SV("answer is '       INF'"), SV("answer is '{:-010G}'"), std::numeric_limits<F>::infinity());
2125   check(SV("answer is '      +INF'"), SV("answer is '{:+010G}'"), std::numeric_limits<F>::infinity());
2126   check(SV("answer is '       INF'"), SV("answer is '{: 010G}'"), std::numeric_limits<F>::infinity());
2127 
2128   check(SV("answer is '      -INF'"), SV("answer is '{:010G}'"), -std::numeric_limits<F>::infinity());
2129   check(SV("answer is '      -INF'"), SV("answer is '{:-010G}'"), -std::numeric_limits<F>::infinity());
2130   check(SV("answer is '      -INF'"), SV("answer is '{:+010G}'"), -std::numeric_limits<F>::infinity());
2131   check(SV("answer is '      -INF'"), SV("answer is '{: 010G}'"), -std::numeric_limits<F>::infinity());
2132 
2133   check(SV("answer is '       NAN'"), SV("answer is '{:010G}'"), nan_pos);
2134   check(SV("answer is '       NAN'"), SV("answer is '{:-010G}'"), nan_pos);
2135   check(SV("answer is '      +NAN'"), SV("answer is '{:+010G}'"), nan_pos);
2136   check(SV("answer is '       NAN'"), SV("answer is '{: 010G}'"), nan_pos);
2137 
2138   check(SV("answer is '      -NAN'"), SV("answer is '{:010G}'"), nan_neg);
2139   check(SV("answer is '      -NAN'"), SV("answer is '{:-010G}'"), nan_neg);
2140   check(SV("answer is '      -NAN'"), SV("answer is '{:+010G}'"), nan_neg);
2141   check(SV("answer is '      -NAN'"), SV("answer is '{: 010G}'"), nan_neg);
2142 
2143   // *** precision ***
2144   check(SV("answer is '0.03'"), SV("answer is '{:.0G}'"), 0.03125);
2145   check(SV("answer is '0.03'"), SV("answer is '{:.1G}'"), 0.03125);
2146   check(SV("answer is '0.031'"), SV("answer is '{:.2G}'"), 0.03125);
2147   check(SV("answer is '0.0312'"), SV("answer is '{:.3G}'"), 0.03125);
2148   check(SV("answer is '0.03125'"), SV("answer is '{:.4G}'"), 0.03125);
2149   check(SV("answer is '0.03125'"), SV("answer is '{:.5G}'"), 0.03125);
2150   check(SV("answer is '0.03125'"), SV("answer is '{:.10G}'"), 0.03125);
2151 
2152   // *** precision & alternate form ***
2153 
2154   // Output validated with  printf("%#xg")
2155   check(SV("answer is '1.'"), SV("answer is '{:#.{}G}'"), 1.2, 0);
2156   check(SV("answer is '1.'"), SV("answer is '{:#.{}G}'"), 1.2, 1);
2157   check(SV("answer is '1.2'"), SV("answer is '{:#.{}G}'"), 1.2, 2);
2158   check(SV("answer is '1.20'"), SV("answer is '{:#.{}G}'"), 1.2, 3);
2159   check(SV("answer is '1.200'"), SV("answer is '{:#.{}G}'"), 1.2, 4);
2160   check(SV("answer is '1.2000'"), SV("answer is '{:#.{}G}'"), 1.2, 5);
2161   check(SV("answer is '1.20000'"), SV("answer is '{:#.{}G}'"), 1.2, 6);
2162 
2163   check(SV("answer is '1.E+03'"), SV("answer is '{:#.{}G}'"), 1200.0, 0);
2164   check(SV("answer is '1.E+03'"), SV("answer is '{:#.{}G}'"), 1200.0, 1);
2165   check(SV("answer is '1.2E+03'"), SV("answer is '{:#.{}G}'"), 1200.0, 2);
2166   check(SV("answer is '1.20E+03'"), SV("answer is '{:#.{}G}'"), 1200.0, 3);
2167   check(SV("answer is '1200.'"), SV("answer is '{:#.{}G}'"), 1200.0, 4);
2168   check(SV("answer is '1200.0'"), SV("answer is '{:#.{}G}'"), 1200.0, 5);
2169   check(SV("answer is '1200.00'"), SV("answer is '{:#.{}G}'"), 1200.0, 6);
2170 
2171   check(SV("answer is '1.E+06'"), SV("answer is '{:#.{}G}'"), 1200000.0, 0);
2172   check(SV("answer is '1.E+06'"), SV("answer is '{:#.{}G}'"), 1200000.0, 1);
2173   check(SV("answer is '1.2E+06'"), SV("answer is '{:#.{}G}'"), 1200000.0, 2);
2174   check(SV("answer is '1.20E+06'"), SV("answer is '{:#.{}G}'"), 1200000.0, 3);
2175   check(SV("answer is '1.200E+06'"), SV("answer is '{:#.{}G}'"), 1200000.0, 4);
2176   check(SV("answer is '1.2000E+06'"), SV("answer is '{:#.{}G}'"), 1200000.0, 5);
2177   check(SV("answer is '1.20000E+06'"), SV("answer is '{:#.{}G}'"), 1200000.0, 6);
2178 
2179   // *** locale-specific form ***
2180   // See locale-specific_form.pass.cpp
2181 }
2182 
2183 template <class F, class CharT, class TestFunction>
format_test_floating_point_default(TestFunction check)2184 void format_test_floating_point_default(TestFunction check) {
2185   auto nan_pos = std::numeric_limits<F>::quiet_NaN();                          // "nan"
2186   auto nan_neg = std::copysign(nan_pos, static_cast<decltype(nan_pos)>(-1.0)); // "-nan"
2187 
2188   // *** align-fill & width ***
2189   check(SV("answer is '   0.25'"), SV("answer is '{:7}'"), F(0.25));
2190   check(SV("answer is '   0.25'"), SV("answer is '{:>7}'"), F(0.25));
2191   check(SV("answer is '0.25   '"), SV("answer is '{:<7}'"), F(0.25));
2192   check(SV("answer is ' 0.25  '"), SV("answer is '{:^7}'"), F(0.25));
2193 
2194   check(SV("answer is '---0.125'"), SV("answer is '{:->8}'"), F(125e-3));
2195   check(SV("answer is '0.125---'"), SV("answer is '{:-<8}'"), F(125e-3));
2196   check(SV("answer is '-0.125--'"), SV("answer is '{:-^8}'"), F(125e-3));
2197 
2198   check(SV("answer is '***inf'"), SV("answer is '{:*>6}'"), std::numeric_limits<F>::infinity());
2199   check(SV("answer is 'inf***'"), SV("answer is '{:*<6}'"), std::numeric_limits<F>::infinity());
2200   check(SV("answer is '*inf**'"), SV("answer is '{:*^6}'"), std::numeric_limits<F>::infinity());
2201 
2202   check(SV("answer is '###-inf'"), SV("answer is '{:#>7}'"), -std::numeric_limits<F>::infinity());
2203   check(SV("answer is '-inf###'"), SV("answer is '{:#<7}'"), -std::numeric_limits<F>::infinity());
2204   check(SV("answer is '#-inf##'"), SV("answer is '{:#^7}'"), -std::numeric_limits<F>::infinity());
2205 
2206   check(SV("answer is '^^^nan'"), SV("answer is '{:^>6}'"), nan_pos);
2207   check(SV("answer is 'nan^^^'"), SV("answer is '{:^<6}'"), nan_pos);
2208   check(SV("answer is '^nan^^'"), SV("answer is '{:^^6}'"), nan_pos);
2209 
2210   check(SV("answer is '000-nan'"), SV("answer is '{:0>7}'"), nan_neg);
2211   check(SV("answer is '-nan000'"), SV("answer is '{:0<7}'"), nan_neg);
2212   check(SV("answer is '0-nan00'"), SV("answer is '{:0^7}'"), nan_neg);
2213 
2214   // Test whether zero padding is ignored
2215   check(SV("answer is '   0.25'"), SV("answer is '{:>07}'"), F(0.25));
2216   check(SV("answer is '0.25   '"), SV("answer is '{:<07}'"), F(0.25));
2217   check(SV("answer is ' 0.25  '"), SV("answer is '{:^07}'"), F(0.25));
2218 
2219   // *** Sign ***
2220   check(SV("answer is '0'"), SV("answer is '{:}'"), F(0));
2221   check(SV("answer is '0'"), SV("answer is '{:-}'"), F(0));
2222   check(SV("answer is '+0'"), SV("answer is '{:+}'"), F(0));
2223   check(SV("answer is ' 0'"), SV("answer is '{: }'"), F(0));
2224 
2225   check(SV("answer is '-0'"), SV("answer is '{:}'"), F(-0.));
2226   check(SV("answer is '-0'"), SV("answer is '{:-}'"), F(-0.));
2227   check(SV("answer is '-0'"), SV("answer is '{:+}'"), F(-0.));
2228   check(SV("answer is '-0'"), SV("answer is '{: }'"), F(-0.));
2229 
2230   // [format.string.std]/5 The sign option applies to floating-point infinity and NaN.
2231   check(SV("answer is 'inf'"), SV("answer is '{:}'"), std::numeric_limits<F>::infinity());
2232   check(SV("answer is 'inf'"), SV("answer is '{:-}'"), std::numeric_limits<F>::infinity());
2233   check(SV("answer is '+inf'"), SV("answer is '{:+}'"), std::numeric_limits<F>::infinity());
2234   check(SV("answer is ' inf'"), SV("answer is '{: }'"), std::numeric_limits<F>::infinity());
2235 
2236   check(SV("answer is '-inf'"), SV("answer is '{:}'"), -std::numeric_limits<F>::infinity());
2237   check(SV("answer is '-inf'"), SV("answer is '{:-}'"), -std::numeric_limits<F>::infinity());
2238   check(SV("answer is '-inf'"), SV("answer is '{:+}'"), -std::numeric_limits<F>::infinity());
2239   check(SV("answer is '-inf'"), SV("answer is '{: }'"), -std::numeric_limits<F>::infinity());
2240 
2241   check(SV("answer is 'nan'"), SV("answer is '{:}'"), nan_pos);
2242   check(SV("answer is 'nan'"), SV("answer is '{:-}'"), nan_pos);
2243   check(SV("answer is '+nan'"), SV("answer is '{:+}'"), nan_pos);
2244   check(SV("answer is ' nan'"), SV("answer is '{: }'"), nan_pos);
2245 
2246   check(SV("answer is '-nan'"), SV("answer is '{:}'"), nan_neg);
2247   check(SV("answer is '-nan'"), SV("answer is '{:-}'"), nan_neg);
2248   check(SV("answer is '-nan'"), SV("answer is '{:+}'"), nan_neg);
2249   check(SV("answer is '-nan'"), SV("answer is '{: }'"), nan_neg);
2250 
2251   // *** alternate form ***
2252   check(SV("answer is '0.'"), SV("answer is '{:#}'"), F(0));
2253   check(SV("answer is '2.5'"), SV("answer is '{:#}'"), F(2.5));
2254 
2255   check(SV("answer is 'inf'"), SV("answer is '{:#}'"), std::numeric_limits<F>::infinity());
2256   check(SV("answer is '-inf'"), SV("answer is '{:#}'"), -std::numeric_limits<F>::infinity());
2257 
2258   check(SV("answer is 'nan'"), SV("answer is '{:#}'"), nan_pos);
2259   check(SV("answer is '-nan'"), SV("answer is '{:#}'"), nan_neg);
2260 
2261   // *** zero-padding & width ***
2262   check(SV("answer is '0.03125'"), SV("answer is '{:07}'"), 0.03125);
2263   check(SV("answer is '+0.03125'"), SV("answer is '{:+07}'"), 0.03125);
2264   check(SV("answer is '+0.03125'"), SV("answer is '{:+08}'"), 0.03125);
2265   check(SV("answer is '+00.03125'"), SV("answer is '{:+09}'"), 0.03125);
2266 
2267   check(SV("answer is '0000.03125'"), SV("answer is '{:010}'"), 0.03125);
2268   check(SV("answer is '0000.03125'"), SV("answer is '{:-010}'"), 0.03125);
2269   check(SV("answer is '+000.03125'"), SV("answer is '{:+010}'"), 0.03125);
2270   check(SV("answer is ' 000.03125'"), SV("answer is '{: 010}'"), 0.03125);
2271 
2272   check(SV("answer is '       inf'"), SV("answer is '{:010}'"), std::numeric_limits<F>::infinity());
2273   check(SV("answer is '       inf'"), SV("answer is '{:-010}'"), std::numeric_limits<F>::infinity());
2274   check(SV("answer is '      +inf'"), SV("answer is '{:+010}'"), std::numeric_limits<F>::infinity());
2275   check(SV("answer is '       inf'"), SV("answer is '{: 010}'"), std::numeric_limits<F>::infinity());
2276 
2277   check(SV("answer is '      -inf'"), SV("answer is '{:010}'"), -std::numeric_limits<F>::infinity());
2278   check(SV("answer is '      -inf'"), SV("answer is '{:-010}'"), -std::numeric_limits<F>::infinity());
2279   check(SV("answer is '      -inf'"), SV("answer is '{:+010}'"), -std::numeric_limits<F>::infinity());
2280   check(SV("answer is '      -inf'"), SV("answer is '{: 010}'"), -std::numeric_limits<F>::infinity());
2281 
2282   check(SV("answer is '       nan'"), SV("answer is '{:010}'"), nan_pos);
2283   check(SV("answer is '       nan'"), SV("answer is '{:-010}'"), nan_pos);
2284   check(SV("answer is '      +nan'"), SV("answer is '{:+010}'"), nan_pos);
2285   check(SV("answer is '       nan'"), SV("answer is '{: 010}'"), nan_pos);
2286 
2287   check(SV("answer is '      -nan'"), SV("answer is '{:010}'"), nan_neg);
2288   check(SV("answer is '      -nan'"), SV("answer is '{:-010}'"), nan_neg);
2289   check(SV("answer is '      -nan'"), SV("answer is '{:+010}'"), nan_neg);
2290   check(SV("answer is '      -nan'"), SV("answer is '{: 010}'"), nan_neg);
2291 
2292   // *** precision ***
2293   // See format_test_floating_point_default_precision
2294 
2295   // *** locale-specific form ***
2296   // See locale-specific_form.pass.cpp
2297 }
2298 
2299 template <class F, class CharT, class TestFunction>
format_test_floating_point_default_precision(TestFunction check)2300 void format_test_floating_point_default_precision(TestFunction check) {
2301 
2302   auto nan_pos = std::numeric_limits<F>::quiet_NaN();                          // "nan"
2303   auto nan_neg = std::copysign(nan_pos, static_cast<decltype(nan_pos)>(-1.0)); // "-nan"
2304 
2305   // *** align-fill & width ***
2306   check(SV("answer is '   0.25'"), SV("answer is '{:7.6}'"), F(0.25));
2307   check(SV("answer is '   0.25'"), SV("answer is '{:>7.6}'"), F(0.25));
2308   check(SV("answer is '0.25   '"), SV("answer is '{:<7.6}'"), F(0.25));
2309   check(SV("answer is ' 0.25  '"), SV("answer is '{:^7.6}'"), F(0.25));
2310 
2311   check(SV("answer is '---0.125'"), SV("answer is '{:->8.6}'"), F(125e-3));
2312   check(SV("answer is '0.125---'"), SV("answer is '{:-<8.6}'"), F(125e-3));
2313   check(SV("answer is '-0.125--'"), SV("answer is '{:-^8.6}'"), F(125e-3));
2314 
2315   check(SV("answer is '***inf'"), SV("answer is '{:*>6.6}'"), std::numeric_limits<F>::infinity());
2316   check(SV("answer is 'inf***'"), SV("answer is '{:*<6.6}'"), std::numeric_limits<F>::infinity());
2317   check(SV("answer is '*inf**'"), SV("answer is '{:*^6.6}'"), std::numeric_limits<F>::infinity());
2318 
2319   check(SV("answer is '###-inf'"), SV("answer is '{:#>7.6}'"), -std::numeric_limits<F>::infinity());
2320   check(SV("answer is '-inf###'"), SV("answer is '{:#<7.6}'"), -std::numeric_limits<F>::infinity());
2321   check(SV("answer is '#-inf##'"), SV("answer is '{:#^7.6}'"), -std::numeric_limits<F>::infinity());
2322 
2323   check(SV("answer is '^^^nan'"), SV("answer is '{:^>6.6}'"), nan_pos);
2324   check(SV("answer is 'nan^^^'"), SV("answer is '{:^<6.6}'"), nan_pos);
2325   check(SV("answer is '^nan^^'"), SV("answer is '{:^^6.6}'"), nan_pos);
2326 
2327   check(SV("answer is '000-nan'"), SV("answer is '{:0>7.6}'"), nan_neg);
2328   check(SV("answer is '-nan000'"), SV("answer is '{:0<7.6}'"), nan_neg);
2329   check(SV("answer is '0-nan00'"), SV("answer is '{:0^7.6}'"), nan_neg);
2330 
2331   // Test whether zero padding is ignored
2332   check(SV("answer is '   0.25'"), SV("answer is '{:>07.6}'"), F(0.25));
2333   check(SV("answer is '0.25   '"), SV("answer is '{:<07.6}'"), F(0.25));
2334   check(SV("answer is ' 0.25  '"), SV("answer is '{:^07.6}'"), F(0.25));
2335 
2336   // *** Sign ***
2337   check(SV("answer is '0'"), SV("answer is '{:.6}'"), F(0));
2338   check(SV("answer is '0'"), SV("answer is '{:-.6}'"), F(0));
2339   check(SV("answer is '+0'"), SV("answer is '{:+.6}'"), F(0));
2340   check(SV("answer is ' 0'"), SV("answer is '{: .6}'"), F(0));
2341 
2342   check(SV("answer is '-0'"), SV("answer is '{:.6}'"), F(-0.));
2343   check(SV("answer is '-0'"), SV("answer is '{:-.6}'"), F(-0.));
2344   check(SV("answer is '-0'"), SV("answer is '{:+.6}'"), F(-0.));
2345   check(SV("answer is '-0'"), SV("answer is '{: .6}'"), F(-0.));
2346 
2347   // [format.string.std]/5 The sign option applies to floating-point infinity and NaN.
2348   check(SV("answer is 'inf'"), SV("answer is '{:.6}'"), std::numeric_limits<F>::infinity());
2349   check(SV("answer is 'inf'"), SV("answer is '{:-.6}'"), std::numeric_limits<F>::infinity());
2350   check(SV("answer is '+inf'"), SV("answer is '{:+.6}'"), std::numeric_limits<F>::infinity());
2351   check(SV("answer is ' inf'"), SV("answer is '{: .6}'"), std::numeric_limits<F>::infinity());
2352 
2353   check(SV("answer is '-inf'"), SV("answer is '{:.6}'"), -std::numeric_limits<F>::infinity());
2354   check(SV("answer is '-inf'"), SV("answer is '{:-.6}'"), -std::numeric_limits<F>::infinity());
2355   check(SV("answer is '-inf'"), SV("answer is '{:+.6}'"), -std::numeric_limits<F>::infinity());
2356   check(SV("answer is '-inf'"), SV("answer is '{: .6}'"), -std::numeric_limits<F>::infinity());
2357 
2358   check(SV("answer is 'nan'"), SV("answer is '{:.6}'"), nan_pos);
2359   check(SV("answer is 'nan'"), SV("answer is '{:-.6}'"), nan_pos);
2360   check(SV("answer is '+nan'"), SV("answer is '{:+.6}'"), nan_pos);
2361   check(SV("answer is ' nan'"), SV("answer is '{: .6}'"), nan_pos);
2362 
2363   check(SV("answer is '-nan'"), SV("answer is '{:.6}'"), nan_neg);
2364   check(SV("answer is '-nan'"), SV("answer is '{:-.6}'"), nan_neg);
2365   check(SV("answer is '-nan'"), SV("answer is '{:+.6}'"), nan_neg);
2366   check(SV("answer is '-nan'"), SV("answer is '{: .6}'"), nan_neg);
2367 
2368   // *** alternate form **
2369   // When precision is zero there's no decimal point except when the alternate form is specified.
2370   // Note unlike the g and G option the trailing zeros are still removed.
2371   check(SV("answer is '0'"), SV("answer is '{:.0}'"), F(0));
2372   check(SV("answer is '0.'"), SV("answer is '{:#.0}'"), F(0));
2373 
2374   check(SV("answer is '0.'"), SV("answer is '{:#.6}'"), F(0));
2375   check(SV("answer is '2.5'"), SV("answer is '{:#.6}'"), F(2.5));
2376 
2377   check(SV("answer is 'inf'"), SV("answer is '{:#.6}'"), std::numeric_limits<F>::infinity());
2378   check(SV("answer is '-inf'"), SV("answer is '{:#.6}'"), -std::numeric_limits<F>::infinity());
2379 
2380   check(SV("answer is 'nan'"), SV("answer is '{:#.6}'"), nan_pos);
2381   check(SV("answer is '-nan'"), SV("answer is '{:#.6}'"), nan_neg);
2382 
2383   // *** zero-padding & width ***
2384   check(SV("answer is '0.03125'"), SV("answer is '{:06.6}'"), 0.03125);
2385   check(SV("answer is '+0.03125'"), SV("answer is '{:+06.6}'"), 0.03125);
2386   check(SV("answer is '+0.03125'"), SV("answer is '{:+07.6}'"), 0.03125);
2387   check(SV("answer is '+0.03125'"), SV("answer is '{:+08.6}'"), 0.03125);
2388 
2389   check(SV("answer is '000.03125'"), SV("answer is '{:09.6}'"), 0.03125);
2390   check(SV("answer is '000.03125'"), SV("answer is '{:-09.6}'"), 0.03125);
2391   check(SV("answer is '+00.03125'"), SV("answer is '{:+09.6}'"), 0.03125);
2392   check(SV("answer is ' 00.03125'"), SV("answer is '{: 09.6}'"), 0.03125);
2393 
2394   check(SV("answer is '       inf'"), SV("answer is '{:010.6}'"), std::numeric_limits<F>::infinity());
2395   check(SV("answer is '       inf'"), SV("answer is '{:-010.6}'"), std::numeric_limits<F>::infinity());
2396   check(SV("answer is '      +inf'"), SV("answer is '{:+010.6}'"), std::numeric_limits<F>::infinity());
2397   check(SV("answer is '       inf'"), SV("answer is '{: 010.6}'"), std::numeric_limits<F>::infinity());
2398 
2399   check(SV("answer is '      -inf'"), SV("answer is '{:010.6}'"), -std::numeric_limits<F>::infinity());
2400   check(SV("answer is '      -inf'"), SV("answer is '{:-010.6}'"), -std::numeric_limits<F>::infinity());
2401   check(SV("answer is '      -inf'"), SV("answer is '{:+010.6}'"), -std::numeric_limits<F>::infinity());
2402   check(SV("answer is '      -inf'"), SV("answer is '{: 010.6}'"), -std::numeric_limits<F>::infinity());
2403 
2404   check(SV("answer is '       nan'"), SV("answer is '{:010.6}'"), nan_pos);
2405   check(SV("answer is '       nan'"), SV("answer is '{:-010.6}'"), nan_pos);
2406   check(SV("answer is '      +nan'"), SV("answer is '{:+010.6}'"), nan_pos);
2407   check(SV("answer is '       nan'"), SV("answer is '{: 010.6}'"), nan_pos);
2408 
2409   check(SV("answer is '      -nan'"), SV("answer is '{:010.6}'"), nan_neg);
2410   check(SV("answer is '      -nan'"), SV("answer is '{:-010.6}'"), nan_neg);
2411   check(SV("answer is '      -nan'"), SV("answer is '{:+010.6}'"), nan_neg);
2412   check(SV("answer is '      -nan'"), SV("answer is '{: 010.6}'"), nan_neg);
2413 
2414   // *** precision ***
2415   check(SV("answer is '0.03'"), SV("answer is '{:.0}'"), 0.03125);
2416   check(SV("answer is '0.03'"), SV("answer is '{:.1}'"), 0.03125);
2417   check(SV("answer is '0.031'"), SV("answer is '{:.2}'"), 0.03125);
2418   check(SV("answer is '0.0312'"), SV("answer is '{:.3}'"), 0.03125);
2419   check(SV("answer is '0.03125'"), SV("answer is '{:.4}'"), 0.03125);
2420   check(SV("answer is '0.03125'"), SV("answer is '{:.5}'"), 0.03125);
2421   check(SV("answer is '0.03125'"), SV("answer is '{:.10}'"), 0.03125);
2422 
2423   // *** precision & alternate form ***
2424 
2425   check(SV("answer is '1.'"), SV("answer is '{:#.{}}'"), 1.2, 0);
2426   check(SV("answer is '1.'"), SV("answer is '{:#.{}}'"), 1.2, 1);
2427   check(SV("answer is '1.2'"), SV("answer is '{:#.{}}'"), 1.2, 2);
2428   check(SV("answer is '1.2'"), SV("answer is '{:#.{}}'"), 1.2, 3);
2429   check(SV("answer is '1.2'"), SV("answer is '{:#.{}}'"), 1.2, 4);
2430   check(SV("answer is '1.2'"), SV("answer is '{:#.{}}'"), 1.2, 5);
2431   check(SV("answer is '1.2'"), SV("answer is '{:#.{}}'"), 1.2, 6);
2432 
2433   check(SV("answer is '1.e+03'"), SV("answer is '{:#.{}}'"), 1200.0, 0);
2434   check(SV("answer is '1.e+03'"), SV("answer is '{:#.{}}'"), 1200.0, 1);
2435   check(SV("answer is '1.2e+03'"), SV("answer is '{:#.{}}'"), 1200.0, 2);
2436   check(SV("answer is '1.2e+03'"), SV("answer is '{:#.{}}'"), 1200.0, 3);
2437   check(SV("answer is '1200.'"), SV("answer is '{:#.{}}'"), 1200.0, 4);
2438   check(SV("answer is '1200.'"), SV("answer is '{:#.{}}'"), 1200.0, 5);
2439   check(SV("answer is '1200.'"), SV("answer is '{:#.{}}'"), 1200.0, 6);
2440 
2441   check(SV("answer is '1.e+06'"), SV("answer is '{:#.{}}'"), 1200000.0, 0);
2442   check(SV("answer is '1.e+06'"), SV("answer is '{:#.{}}'"), 1200000.0, 1);
2443   check(SV("answer is '1.2e+06'"), SV("answer is '{:#.{}}'"), 1200000.0, 2);
2444   check(SV("answer is '1.2e+06'"), SV("answer is '{:#.{}}'"), 1200000.0, 3);
2445   check(SV("answer is '1.2e+06'"), SV("answer is '{:#.{}}'"), 1200000.0, 4);
2446   check(SV("answer is '1.2e+06'"), SV("answer is '{:#.{}}'"), 1200000.0, 5);
2447   check(SV("answer is '1.2e+06'"), SV("answer is '{:#.{}}'"), 1200000.0, 6);
2448 
2449   // *** locale-specific form ***
2450   // See locale-specific_form.pass.cpp
2451 }
2452 
2453 template <class F, class CharT, class TestFunction>
format_test_floating_point_PR58714(TestFunction check)2454 void format_test_floating_point_PR58714(TestFunction check) {
2455   check(SV("+1234"), SV("{:+}"), F(1234.0));
2456   check(SV("+1.348p+10"), SV("{:+a}"), F(1234.0));
2457   check(SV("+1.234000e+03"), SV("{:+e}"), F(1234.0));
2458   check(SV("+1234.000000"), SV("{:+f}"), F(1234.0));
2459   check(SV("+1234"), SV("{:+g}"), F(1234.0));
2460 
2461   check(SV("1234."), SV("{:#}"), F(1234.0));
2462   check(SV("1.348p+10"), SV("{:#a}"), F(1234.0));
2463   check(SV("1.234000e+03"), SV("{:#e}"), F(1234.0));
2464   check(SV("1234.000000"), SV("{:#f}"), F(1234.0));
2465   check(SV("1234.00"), SV("{:#g}"), F(1234.0));
2466 
2467   check(SV("4.e+30"), SV("{:#}"), F(4.0e+30));
2468   check(SV("1.p+102"), SV("{:#a}"), F(0x4.0p+100));
2469   check(SV("4.000000e+30"), SV("{:#e}"), F(4.0e+30));
2470   check(SV("5070602400912917605986812821504.000000"), SV("{:#f}"), F(0x4.0p+100));
2471   check(SV("4.00000e+30"), SV("{:#g}"), F(4.0e+30));
2472 
2473   check(SV("1234."), SV("{:#.6}"), F(1234.0)); // # does not restore zeros
2474   check(SV("1.348000p+10"), SV("{:#.6a}"), F(1234.0));
2475   check(SV("1.234000e+03"), SV("{:#.6e}"), F(1234.0));
2476   check(SV("1234.000000"), SV("{:#.6f}"), F(1234.0));
2477   check(SV("1234.00"), SV("{:#.6g}"), F(1234.0));
2478 
2479   check(SV("-1234."), SV("{:#}"), F(-1234.0));
2480   check(SV("-1.348p+10"), SV("{:#a}"), F(-1234.0));
2481   check(SV("-1.234000e+03"), SV("{:#e}"), F(-1234.0));
2482   check(SV("-1234.000000"), SV("{:#f}"), F(-1234.0));
2483   check(SV("-1234.00"), SV("{:#g}"), F(-1234.0));
2484 
2485   check(SV("-1234."), SV("{:#.6}"), F(-1234.0)); // # does not restore zeros
2486   check(SV("-1.348000p+10"), SV("{:#.6a}"), F(-1234.0));
2487   check(SV("-1.234000e+03"), SV("{:#.6e}"), F(-1234.0));
2488   check(SV("-1234.000000"), SV("{:#.6f}"), F(-1234.0));
2489   check(SV("-1234.00"), SV("{:#.6g}"), F(-1234.0));
2490 
2491   check(SV("+1234."), SV("{:+#}"), F(1234.0));
2492   check(SV("+1.348p+10"), SV("{:+#a}"), F(1234.0));
2493   check(SV("+1.234000e+03"), SV("{:+#e}"), F(1234.0));
2494   check(SV("+1234.000000"), SV("{:+#f}"), F(1234.0));
2495   check(SV("+1234.00"), SV("{:+#g}"), F(1234.0));
2496 
2497   check(SV("+1234."), SV("{:+#.6}"), F(1234.0)); // # does not restore zeros
2498   check(SV("+1.348000p+10"), SV("{:+#.6a}"), F(1234.0));
2499   check(SV("+1.234000e+03"), SV("{:+#.6e}"), F(1234.0));
2500   check(SV("+1234.000000"), SV("{:+#.6f}"), F(1234.0));
2501   check(SV("+1234.00"), SV("{:+#.6g}"), F(1234.0));
2502 }
2503 
2504 template <class F, class CharT, class TestFunction, class ExceptionTest>
format_test_floating_point(TestFunction check,ExceptionTest check_exception)2505 void format_test_floating_point(TestFunction check, ExceptionTest check_exception) {
2506   format_test_floating_point_hex_lower_case<F, CharT>(check);
2507   format_test_floating_point_hex_upper_case<F, CharT>(check);
2508   format_test_floating_point_hex_lower_case_precision<F, CharT>(check);
2509   format_test_floating_point_hex_upper_case_precision<F, CharT>(check);
2510 
2511   format_test_floating_point_scientific_lower_case<F, CharT>(check);
2512   format_test_floating_point_scientific_upper_case<F, CharT>(check);
2513 
2514   format_test_floating_point_fixed_lower_case<F, CharT>(check);
2515   format_test_floating_point_fixed_upper_case<F, CharT>(check);
2516 
2517   format_test_floating_point_general_lower_case<F, CharT>(check);
2518   format_test_floating_point_general_upper_case<F, CharT>(check);
2519 
2520   format_test_floating_point_default<F, CharT>(check);
2521   format_test_floating_point_default_precision<F, CharT>(check);
2522 
2523   format_test_floating_point_PR58714<F, CharT>(check);
2524 
2525   // *** type ***
2526   for (const auto& fmt : invalid_types<CharT>("aAeEfFgG"))
2527     check_exception("The format-spec type has a type not supported for a floating-point argument", fmt, F(1));
2528 }
2529 
2530 template <class CharT, class TestFunction, class ExceptionTest>
format_test_floating_point(TestFunction check,ExceptionTest check_exception)2531 void format_test_floating_point(TestFunction check, ExceptionTest check_exception) {
2532   format_test_floating_point<float, CharT>(check, check_exception);
2533   format_test_floating_point<double, CharT>(check, check_exception);
2534   format_test_floating_point<long double, CharT>(check, check_exception);
2535 }
2536 
2537 template <class P, class CharT, class TestFunction, class ExceptionTest>
format_test_pointer(TestFunction check,ExceptionTest check_exception)2538 void format_test_pointer(TestFunction check, ExceptionTest check_exception) {
2539   // *** align-fill & width ***
2540   check(SV("answer is '   0x0'"), SV("answer is '{:6}'"), P(nullptr));
2541   check(SV("answer is '   0x0'"), SV("answer is '{:>6}'"), P(nullptr));
2542   check(SV("answer is '0x0   '"), SV("answer is '{:<6}'"), P(nullptr));
2543   check(SV("answer is ' 0x0  '"), SV("answer is '{:^6}'"), P(nullptr));
2544 
2545   // The fill character ':' is allowed here (P0645) but not in ranges (P2286).
2546   check(SV("answer is ':::0x0'"), SV("answer is '{::>6}'"), P(nullptr));
2547   check(SV("answer is '0x0:::'"), SV("answer is '{::<6}'"), P(nullptr));
2548   check(SV("answer is ':0x0::'"), SV("answer is '{::^6}'"), P(nullptr));
2549 
2550   // *** Sign ***
2551   check_exception("The format-spec should consume the input or end with a '}'", SV("{:-}"), P(nullptr));
2552   check_exception("The format-spec should consume the input or end with a '}'", SV("{:+}"), P(nullptr));
2553   check_exception("The format-spec should consume the input or end with a '}'", SV("{: }"), P(nullptr));
2554 
2555   // *** alternate form ***
2556   check_exception("The format-spec should consume the input or end with a '}'", SV("{:#}"), P(nullptr));
2557 
2558   // *** zero-padding ***
2559   check_exception("A format-spec width field shouldn't have a leading zero", SV("{:0}"), P(nullptr));
2560 
2561   // *** precision ***
2562   check_exception("The format-spec should consume the input or end with a '}'", SV("{:.}"), P(nullptr));
2563 
2564   // *** locale-specific form ***
2565   check_exception("The format-spec should consume the input or end with a '}'", SV("{:L}"), P(nullptr));
2566 
2567   // *** type ***
2568   for (const auto& fmt : invalid_types<CharT>("p"))
2569     check_exception("The format-spec type has a type not supported for a pointer argument", fmt, P(nullptr));
2570 }
2571 
2572 template <class CharT, class TestFunction, class ExceptionTest>
format_test_handle(TestFunction check,ExceptionTest check_exception)2573 void format_test_handle(TestFunction check, ExceptionTest check_exception) {
2574   // *** Valid permuatations ***
2575   check(SV("answer is '0xaaaa'"), SV("answer is '{}'"), status::foo);
2576   check(SV("answer is '0xaaaa'"), SV("answer is '{:x}'"), status::foo);
2577   check(SV("answer is '0XAAAA'"), SV("answer is '{:X}'"), status::foo);
2578   check(SV("answer is 'foo'"), SV("answer is '{:s}'"), status::foo);
2579 
2580   check(SV("answer is '0x5555'"), SV("answer is '{}'"), status::bar);
2581   check(SV("answer is '0x5555'"), SV("answer is '{:x}'"), status::bar);
2582   check(SV("answer is '0X5555'"), SV("answer is '{:X}'"), status::bar);
2583   check(SV("answer is 'bar'"), SV("answer is '{:s}'"), status::bar);
2584 
2585   check(SV("answer is '0xaa55'"), SV("answer is '{}'"), status::foobar);
2586   check(SV("answer is '0xaa55'"), SV("answer is '{:x}'"), status::foobar);
2587   check(SV("answer is '0XAA55'"), SV("answer is '{:X}'"), status::foobar);
2588   check(SV("answer is 'foobar'"), SV("answer is '{:s}'"), status::foobar);
2589 
2590   // P2418 Changed the argument from a const reference to a forwarding reference.
2591   // This mainly affects handle classes, however since we use an abstraction
2592   // layer here it's "tricky" to verify whether this test would do the "right"
2593   // thing. So these tests are done separately.
2594 
2595   // *** type ***
2596   for (const auto& fmt : invalid_types<CharT>("xXs"))
2597     check_exception("The format-spec type has a type not supported for a status argument", fmt, status::foo);
2598 }
2599 
2600 template <class CharT, class TestFunction, class ExceptionTest>
format_test_pointer(TestFunction check,ExceptionTest check_exception)2601 void format_test_pointer(TestFunction check, ExceptionTest check_exception) {
2602   format_test_pointer<std::nullptr_t, CharT>(check, check_exception);
2603   format_test_pointer<void*, CharT>(check, check_exception);
2604   format_test_pointer<const void*, CharT>(check, check_exception);
2605 }
2606 
2607 /// Tests special buffer functions with a "large" input.
2608 ///
2609 /// This is a test specific for libc++, however the code should behave the same
2610 /// on all implementations.
2611 /// In \c __format::__output_buffer there are some special functions to optimize
2612 /// outputting multiple characters, \c __copy, \c __transform, \c __fill. This
2613 /// test validates whether the functions behave properly when the output size
2614 /// doesn't fit in its internal buffer.
2615 template <class CharT, class TestFunction>
format_test_buffer_optimizations(TestFunction check)2616 void format_test_buffer_optimizations(TestFunction check) {
2617 #ifdef _LIBCPP_VERSION
2618   // Used to validate our test sets are the proper size.
2619   // To test the chunked operations it needs to be larger than the internal
2620   // buffer. Picked a nice looking number.
2621   constexpr int minimum = 3 * std::__format::__internal_storage<CharT>::__buffer_size;
2622 #else
2623   constexpr int minimum = 1;
2624 #endif
2625 
2626   // Copy
2627   std::basic_string<CharT> str = STR(
2628       "The quick brown fox jumps over the lazy dog."
2629       "The quick brown fox jumps over the lazy dog."
2630       "The quick brown fox jumps over the lazy dog."
2631       "The quick brown fox jumps over the lazy dog."
2632       "The quick brown fox jumps over the lazy dog."
2633       "The quick brown fox jumps over the lazy dog."
2634       "The quick brown fox jumps over the lazy dog."
2635       "The quick brown fox jumps over the lazy dog."
2636       "The quick brown fox jumps over the lazy dog."
2637       "The quick brown fox jumps over the lazy dog."
2638       "The quick brown fox jumps over the lazy dog."
2639       "The quick brown fox jumps over the lazy dog."
2640       "The quick brown fox jumps over the lazy dog."
2641       "The quick brown fox jumps over the lazy dog."
2642       "The quick brown fox jumps over the lazy dog."
2643       "The quick brown fox jumps over the lazy dog."
2644       "The quick brown fox jumps over the lazy dog."
2645       "The quick brown fox jumps over the lazy dog."
2646       "The quick brown fox jumps over the lazy dog."
2647       "The quick brown fox jumps over the lazy dog."
2648       "The quick brown fox jumps over the lazy dog."
2649       "The quick brown fox jumps over the lazy dog."
2650       "The quick brown fox jumps over the lazy dog."
2651       "The quick brown fox jumps over the lazy dog."
2652       "The quick brown fox jumps over the lazy dog."
2653       "The quick brown fox jumps over the lazy dog."
2654       "The quick brown fox jumps over the lazy dog."
2655       "The quick brown fox jumps over the lazy dog."
2656       "The quick brown fox jumps over the lazy dog."
2657       "The quick brown fox jumps over the lazy dog.");
2658   assert(str.size() > minimum);
2659   check(std::basic_string_view<CharT>{str}, SV("{}"), str);
2660 
2661   // Fill
2662   std::basic_string<CharT> fill(minimum, CharT('*'));
2663   check(std::basic_string_view<CharT>{str + fill}, SV("{:*<{}}"), str, str.size() + minimum);
2664   check(std::basic_string_view<CharT>{fill + str + fill}, SV("{:*^{}}"), str, minimum + str.size() + minimum);
2665   check(std::basic_string_view<CharT>{fill + str}, SV("{:*>{}}"), str, minimum + str.size());
2666 }
2667 
2668 template <class CharT, execution_modus modus, class TestFunction, class ExceptionTest>
format_tests(TestFunction check,ExceptionTest check_exception)2669 void format_tests(TestFunction check, ExceptionTest check_exception) {
2670   // *** Test escaping  ***
2671 
2672   check(SV("{"), SV("{{"));
2673   check(SV("}"), SV("}}"));
2674   check(SV("{:^}"), SV("{{:^}}"));
2675   check(SV("{: ^}"), SV("{{:{}^}}"), CharT(' '));
2676   check(SV("{:{}^}"), SV("{{:{{}}^}}"));
2677   check(SV("{:{ }^}"), SV("{{:{{{}}}^}}"), CharT(' '));
2678 
2679   // *** Test argument ID ***
2680   check(SV("hello false true"), SV("hello {0:} {1:}"), false, true);
2681   check(SV("hello true false"), SV("hello {1:} {0:}"), false, true);
2682 
2683   // *** Test many arguments ***
2684 
2685   // [format.args]/1
2686   // An instance of basic_format_args provides access to formatting arguments.
2687   // Implementations should optimize the representation of basic_format_args
2688   // for a small number of formatting arguments.
2689   //
2690   // These's no guidances what "a small number of formatting arguments" is.
2691   // - fmtlib uses a 15 elements
2692   // - libc++ uses 12 elements
2693   // - MSVC STL uses a different approach regardless of the number of arguments
2694   // - libstdc++ has no implementation yet
2695   // fmtlib and libc++ use a similar approach, this approach can support 16
2696   // elements (based on design choices both support less elements). This test
2697   // makes sure "the large number of formatting arguments" code path is tested.
2698   check(
2699       SV("1234567890\t1234567890"),
2700       SV("{}{}{}{}{}{}{}{}{}{}\t{}{}{}{}{}{}{}{}{}{}"),
2701       1,
2702       2,
2703       3,
2704       4,
2705       5,
2706       6,
2707       7,
2708       8,
2709       9,
2710       0,
2711       1,
2712       2,
2713       3,
2714       4,
2715       5,
2716       6,
2717       7,
2718       8,
2719       9,
2720       0);
2721 
2722   // ** Test invalid format strings ***
2723   check_exception("The format string terminates at a '{'", SV("{"));
2724   check_exception("The replacement field misses a terminating '}'", SV("{:"), 42);
2725 
2726   check_exception("The format string contains an invalid escape sequence", SV("}"));
2727   check_exception("The format string contains an invalid escape sequence", SV("{:}-}"), 42);
2728 
2729   check_exception("The format string contains an invalid escape sequence", SV("} "));
2730   check_exception("The arg-id of the format-spec starts with an invalid character", SV("{-"), 42);
2731   check_exception("Argument index out of bounds", SV("hello {}"));
2732   check_exception("Argument index out of bounds", SV("hello {0}"));
2733   check_exception("Argument index out of bounds", SV("hello {1}"), 42);
2734 
2735   // *** Test char format argument ***
2736   // The `char` to `wchar_t` formatting is tested separately.
2737   check(SV("hello 09azAZ!"),
2738         SV("hello {}{}{}{}{}{}{}"),
2739         CharT('0'),
2740         CharT('9'),
2741         CharT('a'),
2742         CharT('z'),
2743         CharT('A'),
2744         CharT('Z'),
2745         CharT('!'));
2746   if constexpr (modus == execution_modus::full) {
2747     format_test_char<CharT>(check, check_exception);
2748     format_test_char_as_integer<CharT>(check, check_exception);
2749   }
2750 
2751   // *** Test string format argument ***
2752   {
2753     CharT buffer[] = {CharT('0'), CharT('9'), CharT('a'), CharT('z'), CharT('A'), CharT('Z'), CharT('!'), 0};
2754     CharT* data = buffer;
2755     check(SV("hello 09azAZ!"), SV("hello {}"), data);
2756   }
2757   {
2758     CharT buffer[] = {CharT('0'), CharT('9'), CharT('a'), CharT('z'), CharT('A'), CharT('Z'), CharT('!'), 0};
2759     const CharT* data = buffer;
2760     check(SV("hello 09azAZ!"), SV("hello {}"), data);
2761   }
2762   {
2763     std::basic_string<CharT> data = STR("world");
2764     check(SV("hello world"), SV("hello {}"), data);
2765   }
2766   {
2767     std::basic_string<CharT> buffer = STR("world");
2768     std::basic_string_view<CharT> data = buffer;
2769     check(SV("hello world"), SV("hello {}"), data);
2770   }
2771   if constexpr (modus == execution_modus::full)
2772     format_string_tests<CharT>(check, check_exception);
2773 
2774   // *** Test Boolean format argument ***
2775   check(SV("hello false true"), SV("hello {} {}"), false, true);
2776 
2777   if constexpr (modus == execution_modus::full) {
2778     format_test_bool<CharT>(check, check_exception);
2779     format_test_bool_as_integer<CharT>(check, check_exception);
2780   }
2781 
2782   // *** Test signed integral format argument ***
2783   check(SV("hello 42"), SV("hello {}"), static_cast<signed char>(42));
2784   check(SV("hello 42"), SV("hello {}"), static_cast<short>(42));
2785   check(SV("hello 42"), SV("hello {}"), static_cast<int>(42));
2786   check(SV("hello 42"), SV("hello {}"), static_cast<long>(42));
2787   check(SV("hello 42"), SV("hello {}"), static_cast<long long>(42));
2788 #ifndef TEST_HAS_NO_INT128
2789   check(SV("hello 42"), SV("hello {}"), static_cast<__int128_t>(42));
2790 #endif
2791   if constexpr (modus == execution_modus::full)
2792     format_test_signed_integer<CharT>(check, check_exception);
2793 
2794   // ** Test unsigned integral format argument ***
2795   check(SV("hello 42"), SV("hello {}"), static_cast<unsigned char>(42));
2796   check(SV("hello 42"), SV("hello {}"), static_cast<unsigned short>(42));
2797   check(SV("hello 42"), SV("hello {}"), static_cast<unsigned>(42));
2798   check(SV("hello 42"), SV("hello {}"), static_cast<unsigned long>(42));
2799   check(SV("hello 42"), SV("hello {}"), static_cast<unsigned long long>(42));
2800 #ifndef TEST_HAS_NO_INT128
2801   check(SV("hello 42"), SV("hello {}"), static_cast<__uint128_t>(42));
2802 #endif
2803   if constexpr (modus == execution_modus::full)
2804     format_test_unsigned_integer<CharT>(check, check_exception);
2805 
2806   // *** Test floating point format argument ***
2807   check(SV("hello 42"), SV("hello {}"), static_cast<float>(42));
2808   check(SV("hello 42"), SV("hello {}"), static_cast<double>(42));
2809   check(SV("hello 42"), SV("hello {}"), static_cast<long double>(42));
2810   if constexpr (modus == execution_modus::full)
2811     format_test_floating_point<CharT>(check, check_exception);
2812 
2813   // *** Test pointer formater argument ***
2814   check(SV("hello 0x0"), SV("hello {}"), nullptr);
2815   check(SV("hello 0x42"), SV("hello {}"), reinterpret_cast<void*>(0x42));
2816   check(SV("hello 0x42"), SV("hello {}"), reinterpret_cast<const void*>(0x42));
2817   if constexpr (modus == execution_modus::full)
2818     format_test_pointer<CharT>(check, check_exception);
2819 
2820   // *** Test handle formatter argument ***
2821   format_test_handle<CharT>(check, check_exception);
2822 
2823   // *** Test the interal buffer optimizations ***
2824   if constexpr (modus == execution_modus::full)
2825     format_test_buffer_optimizations<CharT>(check);
2826 }
2827 
2828 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
2829 template <class TestFunction>
format_tests_char_to_wchar_t(TestFunction check)2830 void format_tests_char_to_wchar_t(TestFunction check) {
2831   using CharT = wchar_t;
2832   check(SV("hello 09azA"), SV("hello {}{}{}{}{}"), '0', '9', 'a', 'z', 'A');
2833 }
2834 #endif
2835 
2836 #endif // TEST_STD_UTILITIES_FORMAT_FORMAT_FUNCTIONS_FORMAT_TESTS_H
2837