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-incomplete-format 10 // TODO FMT Evaluate gcc-11 status 11 // UNSUPPORTED: gcc-11 12 13 // Note this formatter shows additional information when tests are failing. 14 // This aids the development. Since other formatters fail in the same fashion 15 // they don't have this additional output. 16 17 // <format> 18 19 // template<class... Args> 20 // string format(string_view fmt, const Args&... args); 21 // template<class... Args> 22 // wstring format(wstring_view fmt, const Args&... args); 23 24 #include <format> 25 #include <cassert> 26 #include <vector> 27 28 #include "test_macros.h" 29 #include "format_tests.h" 30 31 #ifndef TEST_HAS_NO_LOCALIZATION 32 # include <iostream> 33 #endif 34 35 auto test = []<class CharT, class... Args>(std::basic_string_view<CharT> expected, std::basic_string_view<CharT> fmt, 36 const Args&... args) { 37 std::basic_string<CharT> out = std::format(fmt, args...); 38 #ifndef TEST_HAS_NO_LOCALIZATION 39 if constexpr (std::same_as<CharT, char>) 40 if (out != expected) 41 std::cerr << "\nFormat string " << fmt << "\nExpected output " << expected << "\nActual output " << out 42 << '\n'; 43 #endif 44 assert(out == expected); 45 }; 46 47 auto test_exception = []<class CharT, class... Args>(std::string_view what, std::basic_string_view<CharT> fmt, 48 const Args&... args) { 49 #ifndef TEST_HAS_NO_EXCEPTIONS 50 try { 51 std::format(fmt, args...); 52 # ifndef TEST_HAS_NO_LOCALIZATION 53 if constexpr (std::same_as<CharT, char>) 54 std::cerr << "\nFormat string " << fmt << "\nDidn't throw an exception.\n"; 55 # endif 56 assert(false); 57 } catch (const std::format_error& e) { 58 if constexpr (std::same_as<CharT, char>) 59 # if defined(_LIBCPP_VERSION) && !defined(TEST_HAS_NO_LOCALIZATION) 60 if (e.what() != what) 61 std::cerr << "\nFormat string " << fmt << "\nExpected exception " << what << "\nActual exception " 62 << e.what() << '\n'; 63 # endif 64 LIBCPP_ASSERT(e.what() == what); 65 return; 66 } 67 assert(false); 68 #endif 69 (void)what; 70 (void)fmt; 71 (void)sizeof...(args); 72 }; 73 74 int main(int, char**) { 75 format_tests<char>(test, test_exception); 76 77 #ifndef TEST_HAS_NO_WIDE_CHARACTERS 78 format_tests_char_to_wchar_t(test); 79 format_tests<wchar_t>(test, test_exception); 80 #endif 81 82 return 0; 83 } 84