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 // UNSUPPORTED: c++03, c++11, c++14, c++17 9 // UNSUPPORTED: libcpp-has-no-localization 10 // UNSUPPORTED: libcpp-has-no-incomplete-format 11 // TODO FMT Evaluate gcc-11 status 12 // UNSUPPORTED: gcc-11 13 // TODO FMT Investigate AppleClang ICE 14 // UNSUPPORTED: apple-clang-13 15 16 // <format> 17 18 // template<class... Args> 19 // string format(const locale& loc, string_view fmt, const Args&... args); 20 // template<class... Args> 21 // wstring format(const locale& loc, wstring_view fmt, const Args&... args); 22 23 #include <format> 24 #include <cassert> 25 #include <iostream> 26 #include <vector> 27 28 #include "test_macros.h" 29 #include "format_tests.h" 30 #include "string_literal.h" 31 32 auto test = []<string_literal fmt, class CharT, class... Args>(std::basic_string_view<CharT> expected, 33 const Args&... args) constexpr { 34 std::basic_string<CharT> out = std::format(std::locale(), fmt.template sv<CharT>(), args...); 35 if constexpr (std::same_as<CharT, char>) 36 if (out != expected) 37 std::cerr << "\nFormat string " << fmt.template sv<char>() << "\nExpected output " << expected 38 << "\nActual output " << out << '\n'; 39 assert(out == expected); 40 }; 41 42 auto test_exception = []<class CharT, class... Args>(std::string_view what, std::basic_string_view<CharT> fmt, 43 const Args&... args) { 44 #ifndef TEST_HAS_NO_EXCEPTIONS 45 try { 46 std::format(std::locale(), fmt, args...); 47 if constexpr (std::same_as<CharT, char>) 48 std::cerr << "\nFormat string " << fmt << "\nDidn't throw an exception.\n"; 49 assert(false); 50 } catch (const std::format_error& e) { 51 # ifdef _LIBCPP_VERSION 52 if constexpr (std::same_as<CharT, char>) 53 if (e.what() != what) 54 std::cerr << "\nFormat string " << fmt << "\nExpected exception " << what << "\nActual exception " 55 << e.what() << '\n'; 56 # endif 57 LIBCPP_ASSERT(e.what() == what); 58 return; 59 } 60 assert(false); 61 #else 62 (void)what; 63 (void)fmt; 64 (void)sizeof...(args); 65 #endif 66 }; 67 68 int main(int, char**) { 69 format_tests<char>(test, test_exception); 70 71 #ifndef TEST_HAS_NO_WIDE_CHARACTERS 72 format_tests_char_to_wchar_t(test); 73 format_tests<wchar_t>(test, test_exception); 74 #endif 75 76 return 0; 77 } 78