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> 18 // Out vformat_to(Out out, const locale& loc, string_view fmt, 19 // format_args args); 20 // template<class Out> 21 // Out vformat_to(Out out, const locale& loc, wstring_view fmt, 22 // wformat_args args); 23 24 #include <format> 25 #include <algorithm> 26 #include <cassert> 27 #include <list> 28 #include <vector> 29 30 #include "assert_macros.h" 31 #include "concat_macros.h" 32 #include "format_tests.h" 33 #include "string_literal.h" 34 35 auto test = []<class CharT, class... Args>( 36 std::basic_string_view<CharT> expected, std::basic_string_view<CharT> fmt, Args&&... args) constexpr { 37 { 38 std::basic_string<CharT> out(expected.size(), CharT(' ')); 39 auto it = std::vformat_to(out.begin(), std::locale(), fmt, std::make_format_args<context_t<CharT>>(args...)); 40 assert(it == out.end()); 41 assert(out == expected); 42 } 43 { 44 std::list<CharT> out; 45 std::vformat_to(std::back_inserter(out), std::locale(), fmt, std::make_format_args<context_t<CharT>>(args...)); 46 assert(std::equal(out.begin(), out.end(), expected.begin(), expected.end())); 47 } 48 { 49 std::vector<CharT> out; 50 std::vformat_to(std::back_inserter(out), std::locale(), fmt, std::make_format_args<context_t<CharT>>(args...)); 51 assert(std::equal(out.begin(), out.end(), expected.begin(), expected.end())); 52 } 53 { 54 assert(expected.size() < 4096 && "Update the size of the buffer."); 55 CharT out[4096]; 56 CharT* it = std::vformat_to(out, std::locale(), fmt, std::make_format_args<context_t<CharT>>(args...)); 57 assert(std::distance(out, it) == int(expected.size())); 58 // Convert to std::string since output contains '\0' for boolean tests. 59 assert(std::basic_string<CharT>(out, it) == expected); 60 } 61 }; 62 63 auto test_exception = []<class CharT, class... Args>([[maybe_unused]] std::string_view what, 64 [[maybe_unused]] std::basic_string_view<CharT> fmt, 65 [[maybe_unused]] Args&&... args) { 66 TEST_VALIDATE_EXCEPTION( 67 std::format_error, 68 [&]([[maybe_unused]] const std::format_error& e) { 69 TEST_LIBCPP_REQUIRE( 70 e.what() == what, 71 TEST_WRITE_CONCATENATED( 72 "\nFormat string ", fmt, "\nExpected exception ", what, "\nActual exception ", e.what(), '\n')); 73 }, 74 [&] { 75 std::basic_string<CharT> out; 76 std::vformat_to(std::back_inserter(out), std::locale(), fmt, std::make_format_args<context_t<CharT>>(args...)); 77 }()); 78 }; 79 80 int main(int, char**) { 81 format_tests<char, execution_modus::partial>(test, test_exception); 82 83 #ifndef TEST_HAS_NO_WIDE_CHARACTERS 84 format_tests_char_to_wchar_t(test); 85 format_tests<wchar_t, execution_modus::partial>(test, test_exception); 86 #endif 87 88 return 0; 89 } 90