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, c++20 10 11 // UNSUPPORTED: GCC-ALWAYS_INLINE-FIXME 12 13 // <format> 14 15 // template<ranges::input_range R, class charT> 16 // struct range-default-formatter<range_format::sequence, R, charT> 17 18 // template<class FormatContext> 19 // typename FormatContext::iterator 20 // format(maybe-const-r& elems, FormatContext& ctx) const; 21 22 #include <array> 23 #include <cassert> 24 #include <concepts> 25 #include <format> 26 #include <iterator> 27 28 #include "assert_macros.h" 29 #include "format.functions.common.h" 30 #include "test_format_context.h" 31 #include "test_macros.h" 32 #include "make_string.h" 33 34 #define SV(S) MAKE_STRING_VIEW(CharT, S) 35 36 template <class StringViewT> 37 void test_format(StringViewT expected, std::array<int, 2> arg) { 38 using CharT = typename StringViewT::value_type; 39 using String = std::basic_string<CharT>; 40 using OutIt = std::back_insert_iterator<String>; 41 using FormatCtxT = std::basic_format_context<OutIt, CharT>; 42 43 std::formatter<std::array<int, 2>, CharT> formatter; 44 45 String result; 46 OutIt out = std::back_inserter(result); 47 FormatCtxT format_ctx = test_format_context_create<OutIt, CharT>(out, std::make_format_args<FormatCtxT>(arg)); 48 formatter.format(arg, format_ctx); 49 assert(result == expected); 50 } 51 52 template <class CharT> 53 void test_assure_parse_is_called(std::basic_string_view<CharT> fmt) { 54 using String = std::basic_string<CharT>; 55 using OutIt = std::back_insert_iterator<String>; 56 using FormatCtxT = std::basic_format_context<OutIt, CharT>; 57 std::array<parse_call_validator, 2> arg; 58 59 String result; 60 OutIt out = std::back_inserter(result); 61 FormatCtxT format_ctx = test_format_context_create<OutIt, CharT>(out, std::make_format_args<FormatCtxT>(arg)); 62 63 std::formatter<decltype(arg), CharT> formatter; 64 std::basic_format_parse_context<CharT> ctx{fmt}; 65 66 formatter.parse(ctx); 67 formatter.format(arg, format_ctx); 68 } 69 70 template <class CharT> 71 void test_assure_parse_is_called() { 72 using String = std::basic_string<CharT>; 73 using OutIt = std::back_insert_iterator<String>; 74 using FormatCtxT = std::basic_format_context<OutIt, CharT>; 75 std::array<parse_call_validator, 2> arg; 76 77 String result; 78 OutIt out = std::back_inserter(result); 79 [[maybe_unused]] FormatCtxT format_ctx = 80 test_format_context_create<OutIt, CharT>(out, std::make_format_args<FormatCtxT>(arg)); 81 82 { // parse not called 83 [[maybe_unused]] const std::formatter<decltype(arg), CharT> formatter; 84 TEST_THROWS_TYPE(parse_call_validator::parse_function_not_called, formatter.format(arg, format_ctx)); 85 } 86 87 test_assure_parse_is_called(SV("5")); 88 test_assure_parse_is_called(SV("n")); 89 test_assure_parse_is_called(SV("5n")); 90 } 91 92 template <class CharT> 93 void test_fmt() { 94 test_format(SV("[1, 42]"), std::array<int, 2>{{1, 42}}); 95 test_format(SV("[0, 99]"), std::array<int, 2>{{0, 99}}); 96 97 test_assure_parse_is_called<CharT>(); 98 } 99 100 void test() { 101 test_fmt<char>(); 102 #ifndef TEST_HAS_NO_WIDE_CHARACTERS 103 test_fmt<wchar_t>(); 104 #endif 105 } 106 107 int main(int, char**) { 108 test(); 109 110 return 0; 111 } 112