| 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 | // UNSUPPORTED: GCC-ALWAYS_INLINE-FIXME |
| 12 | |
| 13 | // XFAIL: availability-fp_to_chars-missing |
| 14 | |
| 15 | // <format> |
| 16 | |
| 17 | // template<class Out, class... Args> |
| 18 | // Out format_to(Out out, const locale& loc, |
| 19 | // format-string<Args...> fmt, const Args&... args); |
| 20 | // template<class Out, class... Args> |
| 21 | // Out format_to(Out out, const locale& loc, |
| 22 | // wformat-string<Args...> fmt, const Args&... args); |
| 23 | |
| 24 | #include <format> |
| 25 | #include <algorithm> |
| 26 | #include <cassert> |
| 27 | #include <iterator> |
| 28 | #include <list> |
| 29 | #include <locale> |
| 30 | #include <vector> |
| 31 | |
| 32 | #include "test_macros.h" |
| 33 | #include "format_tests.h" |
| 34 | #include "string_literal.h" |
| 35 | #include "test_format_string.h" |
| 36 | #include "test_iterators.h" |
| 37 | |
| 38 | auto test = |
| 39 | []<class CharT, class... Args>( |
| 40 | std::basic_string_view<CharT> expected, test_format_string<CharT, Args...> fmt, Args&&... args) constexpr { |
| 41 | { |
| 42 | std::basic_string<CharT> out(expected.size(), CharT(' ')); |
| 43 | auto it = std::format_to(out.begin(), std::locale(), fmt, std::forward<Args>(args)...); |
| 44 | assert(it == out.end()); |
| 45 | assert(out == expected); |
| 46 | } |
| 47 | { |
| 48 | std::list<CharT> out; |
| 49 | std::format_to(std::back_inserter(out), std::locale(), fmt, std::forward<Args>(args)...); |
| 50 | assert(std::equal(out.begin(), out.end(), expected.begin(), expected.end())); |
| 51 | } |
| 52 | { |
| 53 | std::vector<CharT> out; |
| 54 | std::format_to(std::back_inserter(out), std::locale(), fmt, std::forward<Args>(args)...); |
| 55 | assert(std::equal(out.begin(), out.end(), expected.begin(), expected.end())); |
| 56 | } |
| 57 | { |
| 58 | assert(expected.size() < 4096 && "Update the size of the buffer." ); |
| 59 | CharT out[4096]; |
| 60 | cpp20_output_iterator<CharT*> it = |
| 61 | std::format_to(cpp20_output_iterator{out}, std::locale(), fmt, std::forward<Args>(args)...); |
| 62 | assert(std::distance(out, base(it)) == int(expected.size())); |
| 63 | // Convert to std::string since output contains '\0' for boolean tests. |
| 64 | assert(std::basic_string<CharT>(out, base(it)) == expected); |
| 65 | } |
| 66 | }; |
| 67 | |
| 68 | auto test_exception = []<class CharT, class... Args>(std::string_view, std::basic_string_view<CharT>, Args&&...) { |
| 69 | // After P2216 most exceptions thrown by std::format_to become ill-formed. |
| 70 | // Therefore this tests does nothing. |
| 71 | // A basic ill-formed test is done in format_to.locale.verify.cpp |
| 72 | // The exceptions are tested by other functions that don't use the basic-format-string as fmt argument. |
| 73 | }; |
| 74 | |
| 75 | int main(int, char**) { |
| 76 | format_tests<char, execution_modus::partial>(check: test, check_exception: test_exception); |
| 77 | |
| 78 | #ifndef TEST_HAS_NO_WIDE_CHARACTERS |
| 79 | format_tests_char_to_wchar_t(check: test); |
| 80 | format_tests<wchar_t, execution_modus::partial>(check: test, check_exception: test_exception); |
| 81 | #endif |
| 82 | |
| 83 | return 0; |
| 84 | } |
| 85 | |