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