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