xref: /llvm-project/libcxx/test/std/utilities/format/format.functions/format_to_n.locale.verify.cpp (revision 6a54dfbfe534276d644d7f9c027f0deeb748dd53)
1 //===----------------------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 // UNSUPPORTED: c++03, c++11, c++14, c++17
10 // UNSUPPORTED: no-localization
11 
12 // TODO FMT This test should not require std::to_chars(floating-point)
13 // XFAIL: availability-fp_to_chars-missing
14 
15 // Basic test to validate ill-formed code is properly detected.
16 
17 // <format>
18 
19 // template<class Out, class... Args>
20 //   format_to_n_result<Out> format_to_n(Out out, iter_difference_t<Out> n,
21 //                                       const locale& loc, format-string<Args...> fmt,
22 //                                       const Args&... args);
23 // template<class Out, class... Args>
24 //   format_to_n_result<Out> format_to_n(Out out, iter_difference_t<Out> n,
25 //                                       const locale& loc, wformat-string<Args...> fmt,
26 //                                       const Args&... args);
27 
28 #include <format>
29 #include <locale>
30 
31 #include "test_macros.h"
32 
33 extern char* out;
34 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
35 extern wchar_t* wout;
36 #endif
37 
38 // clang-format off
39 
40 void f() {
41   std::format_to_n(out, 42, std::locale(), "{"); // expected-error-re{{call to consteval function '{{.*}}' is not a constant expression}}
42   // expected-note@*:* {{non-constexpr function '__throw_format_error' cannot be used in a constant expression}}
43 
44   std::format_to_n(out, 42, std::locale(), "}"); // expected-error-re{{call to consteval function '{{.*}}' is not a constant expression}}
45   // expected-note@*:* {{non-constexpr function '__throw_format_error' cannot be used in a constant expression}}
46 
47   std::format_to_n(out, 42, std::locale(), "{}"); // expected-error-re{{call to consteval function '{{.*}}' is not a constant expression}}
48   // expected-note@*:* {{non-constexpr function '__throw_format_error' cannot be used in a constant expression}}
49 
50   std::format_to_n(out, 42, std::locale(), "{0}"); // expected-error-re{{call to consteval function '{{.*}}' is not a constant expression}}
51   // expected-note@*:* {{non-constexpr function '__throw_format_error' cannot be used in a constant expression}}
52 
53   std::format_to_n(out, 42, std::locale(), "{:-}", "Forty-two"); // expected-error-re{{call to consteval function '{{.*}}' is not a constant expression}}
54   // expected-note@*:* {{non-constexpr function '__throw_format_error' cannot be used in a constant expression}}
55 
56   std::format_to_n(out, 42, std::locale(), "{:#}", "Forty-two"); // expected-error-re{{call to consteval function '{{.*}}' is not a constant expression}}
57   // expected-note@*:* {{non-constexpr function '__throw_format_error' cannot be used in a constant expression}}
58 
59   std::format_to_n(out, 42, std::locale(), "{:L}", "Forty-two"); // expected-error-re{{call to consteval function '{{.*}}' is not a constant expression}}
60   // expected-note@*:* {{non-constexpr function '__throw_format_error' cannot be used in a constant expression}}
61 
62   std::format_to_n(out, 42, std::locale(), "{0:{0}}", "Forty-two"); // expected-error-re{{call to consteval function '{{.*}}' is not a constant expression}}
63   // expected-note@*:* {{non-constexpr function '__throw_format_error' cannot be used in a constant expression}}
64 
65   std::format_to_n(out, 42, std::locale(), "{:.42d}", "Forty-two"); // expected-error-re{{call to consteval function '{{.*}}' is not a constant expression}}
66   // expected-note@*:* {{non-constexpr function '__throw_format_error' cannot be used in a constant expression}}
67 
68   std::format_to_n(out, 42, std::locale(), "{:d}", "Forty-two"); // expected-error-re{{call to consteval function '{{.*}}' is not a constant expression}}
69   // expected-note@*:* {{non-constexpr function '__throw_format_error' cannot be used in a constant expression}}
70 
71 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
72   std::format_to_n(wout, 42, std::locale(), L"{"); // expected-error-re{{call to consteval function '{{.*}}' is not a constant expression}}
73   // expected-note@*:* {{non-constexpr function '__throw_format_error' cannot be used in a constant expression}}
74 
75   std::format_to_n(wout, 42, std::locale(), L"}"); // expected-error-re{{call to consteval function '{{.*}}' is not a constant expression}}
76   // expected-note@*:* {{non-constexpr function '__throw_format_error' cannot be used in a constant expression}}
77 
78   std::format_to_n(wout, 42, std::locale(), L"{}"); // expected-error-re{{call to consteval function '{{.*}}' is not a constant expression}}
79   // expected-note@*:* {{non-constexpr function '__throw_format_error' cannot be used in a constant expression}}
80 
81   std::format_to_n(wout, 42, std::locale(), L"{0}"); // expected-error-re{{call to consteval function '{{.*}}' is not a constant expression}}
82   // expected-note@*:* {{non-constexpr function '__throw_format_error' cannot be used in a constant expression}}
83 
84   std::format_to_n(wout, 42, std::locale(), L"{:-}", L"Forty-two"); // expected-error-re{{call to consteval function '{{.*}}' is not a constant expression}}
85   // expected-note@*:* {{non-constexpr function '__throw_format_error' cannot be used in a constant expression}}
86 
87   std::format_to_n(wout, 42, std::locale(), L"{:#}", L"Forty-two"); // expected-error-re{{call to consteval function '{{.*}}' is not a constant expression}}
88   // expected-note@*:* {{non-constexpr function '__throw_format_error' cannot be used in a constant expression}}
89 
90   std::format_to_n(wout, 42, std::locale(), L"{:L}", L"Forty-two"); // expected-error-re{{call to consteval function '{{.*}}' is not a constant expression}}
91   // expected-note@*:* {{non-constexpr function '__throw_format_error' cannot be used in a constant expression}}
92 
93   std::format_to_n(wout, 42, std::locale(), L"{0:{0}}", L"Forty-two"); // expected-error-re{{call to consteval function '{{.*}}' is not a constant expression}}
94   // expected-note@*:* {{non-constexpr function '__throw_format_error' cannot be used in a constant expression}}
95 
96   std::format_to_n(wout, 42, std::locale(), L"{:.42d}", L"Forty-two"); // expected-error-re{{call to consteval function '{{.*}}' is not a constant expression}}
97   // expected-note@*:* {{non-constexpr function '__throw_format_error' cannot be used in a constant expression}}
98 
99   std::format_to_n(wout, 42, std::locale(), L"{:d}", L"Forty-two"); // expected-error-re{{call to consteval function '{{.*}}' is not a constant expression}}
100   // expected-note@*:* {{non-constexpr function '__throw_format_error' cannot be used in a constant expression}}
101 #endif
102 }
103