| 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 | // [container.adaptors.format] |
| 14 | // For each of queue, priority_queue, and stack, the library provides the |
| 15 | // following formatter specialization where adaptor-type is the name of the |
| 16 | // template: |
| 17 | // |
| 18 | // template<class charT, class T, formattable<charT> Container, class... U> |
| 19 | // struct formatter<adaptor-type<T, Container, U...>, charT> |
| 20 | |
| 21 | // template<class FormatContext> |
| 22 | // typename FormatContext::iterator |
| 23 | // format(maybe-const-adaptor& r, FormatContext& ctx) const; |
| 24 | |
| 25 | // Note this tests the basics of this function. It's tested in more detail in |
| 26 | // the format functions test. |
| 27 | |
| 28 | #include <array> |
| 29 | #include <cassert> |
| 30 | #include <concepts> |
| 31 | #include <format> |
| 32 | #include <queue> |
| 33 | #include <stack> |
| 34 | |
| 35 | #include "test_format_context.h" |
| 36 | #include "test_macros.h" |
| 37 | #include "make_string.h" |
| 38 | |
| 39 | #define SV(S) MAKE_STRING_VIEW(CharT, S) |
| 40 | |
| 41 | template <class StringViewT, class Arg> |
| 42 | void test_format(StringViewT expected, Arg arg) { |
| 43 | using CharT = typename StringViewT::value_type; |
| 44 | using String = std::basic_string<CharT>; |
| 45 | using OutIt = std::back_insert_iterator<String>; |
| 46 | using FormatCtxT = std::basic_format_context<OutIt, CharT>; |
| 47 | |
| 48 | const std::formatter<Arg, CharT> formatter; |
| 49 | |
| 50 | String result; |
| 51 | OutIt out = std::back_inserter(result); |
| 52 | FormatCtxT format_ctx = test_format_context_create<OutIt, CharT>(out, std::make_format_args<FormatCtxT>(arg)); |
| 53 | formatter.format(arg, format_ctx); |
| 54 | assert(result == expected); |
| 55 | } |
| 56 | |
| 57 | template <class CharT> |
| 58 | void test_fmt() { |
| 59 | std::array input{1, 42, 99, 0}; |
| 60 | test_format(SV("[1, 42, 99, 0]" ), std::queue<int>{input.begin(), input.end()}); |
| 61 | test_format(SV("[99, 42, 1, 0]" ), std::priority_queue<int>{input.begin(), input.end()}); |
| 62 | test_format(SV("[1, 42, 99, 0]" ), std::stack<int>{input.begin(), input.end()}); |
| 63 | } |
| 64 | |
| 65 | void test() { |
| 66 | test_fmt<char>(); |
| 67 | #ifndef TEST_HAS_NO_WIDE_CHARACTERS |
| 68 | test_fmt<wchar_t>(); |
| 69 | #endif |
| 70 | } |
| 71 | |
| 72 | int main(int, char**) { |
| 73 | test(); |
| 74 | |
| 75 | return 0; |
| 76 | } |
| 77 | |